This commit is contained in:
IlyaShurupov 2024-05-03 15:09:26 +03:00
parent 86a41c1876
commit e2072d257f
14 changed files with 594 additions and 341 deletions

View file

@ -1,5 +1,5 @@
#include "ExampleGUI.hpp"
#include "ChatGUI.hpp"
#include "GraphicApplication.hpp"
@ -7,16 +7,20 @@ using namespace tp;
class ExampleGUI : public Application {
public:
ExampleGUI() = default;
ExampleGUI() { mGui.setupConfig(mWidgetManager); }
void processFrame(EventHandler* eventHandler) override {
auto rec = RectF({ 0, 0 }, mWindow->getSize());
mGui.updateConfigCache(mWidgetManager);
mGui.proc(*eventHandler, rec, rec);
}
void drawFrame(Canvas* canvas) override { mGui.draw(*canvas); }
private:
WidgetManager mWidgetManager;
ComplexWidget<EventHandler, Canvas> mGui;
};

View file

@ -7,15 +7,7 @@ namespace tp {
template <typename Events, typename Canvas>
class UserWidget : public Widget<Events, Canvas> {
public:
UserWidget() {
this->createConfig("User");
this->addColor("Base", "Base");
this->addValue("Size", "FontSize");
this->addValue("Padding", "Padding");
this->addColor("ColUser", "Front");
this->addColor("Accent", "Accent");
this->addValue("Rounding", "Rounding");
}
UserWidget() { this->mId = "UserWidget"; }
void proc(const Events& events, const RectF& areaParent, const RectF& aArea) override {
this->mArea = aArea;
@ -28,34 +20,48 @@ namespace tp {
void draw(Canvas& canvas) override {
if (!this->mVisible) return;
if (mIsHover) canvas.rect(this->mArea, this->getColor("Accent"), this->getValue("Rounding"));
else canvas.rect(this->mArea, this->getColor("Base"), this->getValue("Rounding"));
if (mIsHover) canvas.rect(this->mArea, mAccentColor, mRounding);
else canvas.rect(this->mArea, mBaseColor, mRounding);
const auto padding = this->getValue("Padding");
const auto size = this->getValue("Size");
const auto colUser = this->getColor("ColUser");
canvas.text(mUser.c_str(), this->mArea, mFontSize, Canvas::CC, mPadding, mUserColor);
}
canvas.text(mUser.c_str(), this->mArea, size, Canvas::CC, padding, colUser);
public:
void setupConfig(WidgetManager& wm) {
if (!wm.createWidgetConfig(this->mId)) return;
wm.addReference(this->mId, "Base", "Base");
wm.addReference(this->mId, "Size", "FontSize");
wm.addReference(this->mId, "Padding", "Padding");
wm.addReference(this->mId, "ColUser", "Front");
wm.addReference(this->mId, "Accent", "Accent");
wm.addReference(this->mId, "Rounding", "Rounding");
}
void updateConfigCache(const WidgetManager& wm) override {
mBaseColor = wm.getColor(this->mId, "Base");
mFontSize = wm.getNumber(this->mId, "Size");
mPadding = wm.getNumber(this->mId, "Padding");
mUserColor = wm.getColor(this->mId, "ColUser");
mAccentColor = wm.getColor(this->mId, "Accent");
mRounding = wm.getNumber(this->mId, "Rounding");
}
public:
std::string mUser = "UserName";
bool mIsHover = false;
RGBA mBaseColor;
RGBA mUserColor;
RGBA mAccentColor;
halnf mPadding = 0;
halnf mFontSize = 0;
halnf mRounding = 0;
};
template <typename Events, typename Canvas>
class MessageWidget : public Widget<Events, Canvas> {
public:
MessageWidget() {
this->createConfig("Message");
this->addColor("Base", "Base");
this->addValue("Size", "FontSize");
this->addValue("SizeUser", "FontSizeDim");
this->addValue("Padding", "Padding");
this->addColor("ColUser", "Front");
this->addColor("Col", "FrontDim");
this->addValue("Rounding", "Rounding");
}
MessageWidget() { this->mId = "MessageWidget"; }
void proc(const Events& events, const RectF& areaParent, const RectF& aArea) override {
this->mArea = aArea;
@ -67,14 +73,7 @@ namespace tp {
void draw(Canvas& canvas) override {
if (!this->mVisible) return;
if (mIsHover) canvas.rect(this->mArea, this->getColor("Base"), this->getValue("Rounding"));
const auto padding = this->getValue("Padding");
const auto size = this->getValue("Size");
const auto sizeUser = this->getValue("SizeUser");
const auto col = this->getColor("Col");
const auto colUser = this->getColor("ColUser");
if (mIsHover) canvas.rect(this->mArea, mBaseColor, mRounding);
auto userName = this->mArea;
userName.w = 25;
@ -83,22 +82,51 @@ namespace tp {
content.y = userName.y + userName.w;
content.w = this->mArea.w - userName.w;
canvas.text(mContent.c_str(), content, size, Canvas::LC, padding, col);
canvas.text(mUser.c_str(), userName, sizeUser, Canvas::LC, padding, colUser);
canvas.text(mContent.c_str(), content, mFontSize, Canvas::LC, mPadding, mUserColorDim);
canvas.text(mUser.c_str(), userName, mFontSizeDim, Canvas::LC, mPadding, mUserColor);
}
void setupConfig(WidgetManager& wm) {
if (!wm.createWidgetConfig(this->mId)) return;
wm.addReference(this->mId, "Base", "Base");
wm.addReference(this->mId, "Size", "FontSize");
wm.addReference(this->mId, "SizeUser", "FontSizeDim");
wm.addReference(this->mId, "Padding", "Padding");
wm.addReference(this->mId, "UserColor", "Front");
wm.addReference(this->mId, "UserColorDim", "FrontDim");
wm.addReference(this->mId, "Rounding", "Rounding");
}
void updateConfigCache(const WidgetManager& wm) override {
mBaseColor = wm.getColor(this->mId, "Base");
mFontSize = wm.getNumber(this->mId, "Size");
mFontSizeDim = wm.getNumber(this->mId, "SizeUser");
mPadding = wm.getNumber(this->mId, "Padding");
mUserColor = wm.getColor(this->mId, "UserColor");
mUserColorDim = wm.getColor(this->mId, "UserColorDim");
mRounding = wm.getNumber(this->mId, "Rounding");
}
public:
std::string mContent = "Message Content";
std::string mUser = "UserName";
bool mIsHover = false;
RGBA mBaseColor;
RGBA mUserColor;
RGBA mUserColorDim;
halnf mPadding = 0;
halnf mFontSize = 0;
halnf mFontSizeDim = 0;
halnf mRounding = 0;
};
template <typename Events, typename Canvas>
class LoginWidget : public Widget<Events, Canvas> {
public:
explicit LoginWidget() {
this->createConfig("Login");
this->addColor("Back", "Background");
this->mId = "Login";
mPass.mId = "pass";
mUser.mId = "user";
mButton.mLabel.mLabel = "Login";
@ -120,26 +148,45 @@ namespace tp {
}
void draw(Canvas& canvas) override {
canvas.rect(this->mArea, this->getColor("Back"));
canvas.rect(this->mArea, mBGColor);
mButton.draw(canvas);
mUser.draw(canvas);
mPass.draw(canvas);
}
public:
void setupConfig(WidgetManager& wm) {
if (!wm.createWidgetConfig(this->mId)) return;
wm.addReference(this->mId, "Back", "Base");
mUser.setupConfig(wm);
mPass.setupConfig(wm);
mButton.setupConfig(wm);
}
void updateConfigCache(const WidgetManager& wm) override {
mBGColor = wm.getColor(this->mId, "Back");
mUser.updateConfigCache(wm);
mPass.updateConfigCache(wm);
mButton.updateConfigCache(wm);
}
public:
TextInputWidget<Events, Canvas> mUser;
TextInputWidget<Events, Canvas> mPass;
ButtonWidget<Events, Canvas> mButton;
bool mLogged = false;
RGBA mBGColor;
};
template <typename Events, typename Canvas>
class ActiveChatWidget : public Widget<Events, Canvas> {
public:
ActiveChatWidget() {
this->createConfig("ActiveChat");
this->addColor("Back", "Background");
this->addValue("Padding", "Padding");
this->mId = "ActiveWidget";
mSend.mLabel.mLabel = "Send";
mMessage.mId = "Message";
}
@ -152,16 +199,16 @@ namespace tp {
auto input = this->mArea;
input.y = history.w + 10;
input.w = 40 - this->getValue("Padding");
input.x += this->getValue("Padding");
input.z -= this->getValue("Padding");
input.w = 40 - mPadding;
input.x += mPadding;
input.z -= mPadding;
auto inputMessage = input;
inputMessage.z -= 100;
auto inputSend = input;
inputSend.x = inputMessage.x + inputMessage.z + this->getValue("Padding");
inputSend.z = 100 - this->getValue("Padding") * 2;
inputSend.x = inputMessage.x + inputMessage.z + mPadding;
inputSend.z = 100 - mPadding * 2;
mSend.proc(events, this->mArea, inputSend);
mMessage.proc(events, this->mArea, inputMessage);
@ -174,26 +221,53 @@ namespace tp {
}
void draw(Canvas& canvas) override {
canvas.rect(this->mArea, this->getColor("Back"));
canvas.rect(this->mArea, mBGColor);
mHistoryView.draw(canvas);
mMessage.draw(canvas);
mSend.draw(canvas);
}
void setupConfig(WidgetManager& wm) {
if (!wm.createWidgetConfig(this->mId)) return;
wm.addReference(this->mId, "Back", "Background");
wm.addReference(this->mId, "Padding", "Padding");
mHistoryView.setupConfig(wm);
mMessage.setupConfig(wm);
mSend.setupConfig(wm);
MessageWidget<Events, Canvas>().setupConfig(wm);
}
void updateConfigCache(const WidgetManager& wm) override {
mBGColor = wm.getColor(this->mId, "Back");
mPadding = wm.getNumber(this->mId, "Padding");
mHistoryView.updateConfigCache(wm);
mMessage.updateConfigCache(wm);
mSend.updateConfigCache(wm);
for (auto message : mMessages) {
message->updateConfigCache(wm);
}
}
public:
Buffer<MessageWidget<Events, Canvas>> mMessages;
ScrollableWindow<Events, Canvas> mHistoryView;
TextInputWidget<Events, Canvas> mMessage;
ButtonWidget<Events, Canvas> mSend;
RGBA mBGColor;
halnf mPadding = 0;
};
template <typename Events, typename Canvas>
class ChattingWidget : public Widget<Events, Canvas> {
public:
ChattingWidget() {
this->createConfig("Chatting");
this->addColor("Back", "Background");
this->addValue("Padding", "Padding");
this->mId = "Chatting";
// todo : fetch code
mUsers.append(UserWidget<Events, Canvas>());
@ -225,34 +299,54 @@ namespace tp {
this->mArea = aArea;
mSplitView.proc(events, aArea, aArea);
const auto padding = this->getValue("Padding");
mSideView.proc(events, this->mArea, mSplitView.getSecond());
mActive.proc(events, aArea, mSplitView.getFirst());
}
void draw(Canvas& canvas) override {
canvas.rect(this->mArea, this->getColor("Back"));
canvas.rect(this->mArea, mBGColor);
mSplitView.draw(canvas);
mSideView.draw(canvas);
mActive.draw(canvas);
}
void setupConfig(WidgetManager& wm) {
if (!wm.createWidgetConfig(this->mId)) return;
wm.addReference(this->mId, "Back", "Background");
mSideView.setupConfig(wm);
mActive.setupConfig(wm);
mSplitView.setupConfig(wm);
UserWidget<Events, Canvas>().setupConfig(wm);
}
void updateConfigCache(const WidgetManager& wm) override {
mBGColor = wm.getColor(this->mId, "Back");
mSideView.updateConfigCache(wm);
mActive.updateConfigCache(wm);
mSplitView.updateConfigCache(wm);
for (auto user : mUsers) {
user->updateConfigCache(wm);
}
}
public:
Buffer<UserWidget<Events, Canvas>> mUsers;
ScrollableWindow<Events, Canvas> mSideView;
ActiveChatWidget<Events, Canvas> mActive;
SplitView<Events, Canvas> mSplitView;
RGBA mBGColor;
};
template <typename Events, typename Canvas>
class ComplexWidget : public Widget<Events, Canvas> {
public:
ComplexWidget() {
this->createConfig("chat");
this->addColor("Back", "Background");
}
ComplexWidget() { this->mId = "Chat"; }
void proc(const Events& events, const RectF& areaParent, const RectF& aArea) override {
this->mArea = aArea;
@ -268,14 +362,32 @@ namespace tp {
}
void draw(Canvas& canvas) override {
canvas.rect(this->mArea, this->getColor("Back"));
canvas.rect(this->mArea, mBGColor);
if (mLogged) mChatting.draw(canvas);
else mLogin.draw(canvas);
}
void setupConfig(WidgetManager& wm) {
if (!wm.createWidgetConfig(this->mId)) return;
wm.addReference(this->mId, "Back", "Background");
mLogin.setupConfig(wm);
mChatting.setupConfig(wm);
}
void updateConfigCache(const WidgetManager& wm) override {
mBGColor = wm.getColor(this->mId, "Back");
mLogin.updateConfigCache(wm);
mChatting.updateConfigCache(wm);
}
private:
bool mLogged = false;
LoginWidget<Events, Canvas> mLogin;
ChattingWidget<Events, Canvas> mChatting;
RGBA mBGColor;
};
}

View file

@ -0,0 +1,50 @@
#include "ChatGUI.hpp"
#include "GraphicApplication.hpp"
using namespace tp;
class SimpleGUI : public Application {
public:
SimpleGUI() {
mGui.setupConfig(mWidgetManager);
mButton.setupConfig(mWidgetManager);
mSlider.setupConfig(mWidgetManager);
mLabel.setupConfig(mWidgetManager);
mGui.mContents.append(&mButton);
mGui.mContents.append(&mSlider);
mGui.mContents.append(&mLabel);
}
void processFrame(EventHandler* eventHandler) override {
mGui.updateConfigCache(mWidgetManager);
mSlider.updateConfigCache(mWidgetManager);
mLabel.updateConfigCache(mWidgetManager);
mButton.updateConfigCache(mWidgetManager);
const auto rec = RectF({ 0, 0 }, mWindow->getSize());
mGui.proc(*eventHandler, rec, rec);
}
void drawFrame(Canvas* canvas) override { mGui.draw(*canvas); }
private:
WidgetManager mWidgetManager;
ScrollableWindow<EventHandler, Canvas> mGui;
ButtonWidget<EventHandler, Canvas> mButton;
LabelWidget<EventHandler, Canvas> mLabel;
SliderWidget<EventHandler, Canvas> mSlider;
};
int main() {
{
SimpleGUI gui;
gui.run();
}
}