frontend/gui refactor

This commit is contained in:
MihailRis
2024-01-31 05:55:01 +03:00
parent 8c722b146e
commit 1ba563d42a
13 changed files with 207 additions and 148 deletions
+2 -2
View File
@@ -26,7 +26,7 @@ GUI::GUI() {
uicamera->perspective = false;
uicamera->flipped = true;
menu = new PagesControl();
menu = std::make_shared<PagesControl>();
container->add(menu);
container->scrollable(false);
}
@@ -36,7 +36,7 @@ GUI::~GUI() {
delete container;
}
PagesControl* GUI::getMenu() {
std::shared_ptr<PagesControl> GUI::getMenu() {
return menu;
}
+2 -2
View File
@@ -59,13 +59,13 @@ namespace gui {
std::unordered_map<std::string, std::shared_ptr<UINode>> storage;
Camera* uicamera;
PagesControl* menu;
std::shared_ptr<PagesControl> menu;
void actMouse(float delta);
public:
GUI();
~GUI();
PagesControl* getMenu();
std::shared_ptr<PagesControl> getMenu();
std::shared_ptr<UINode> getFocused() const;
bool isFocusCaught() const;
+3 -1
View File
@@ -20,6 +20,7 @@ Label::Label(std::string text, std::string fontName)
: UINode(vec2(), vec2(text.length() * 8, 15)),
text(util::str2wstr_utf8(text)),
fontName_(fontName) {
setInteractive(false);
}
@@ -27,6 +28,7 @@ Label::Label(std::wstring text, std::string fontName)
: UINode(vec2(), vec2(text.length() * 8, 15)),
text(text),
fontName_(fontName) {
setInteractive(false);
}
void Label::setText(std::wstring text) {
@@ -314,7 +316,7 @@ void TextBox::text(std::wstring value) {
InputBindBox::InputBindBox(Binding& binding, vec4 padding)
: Panel(vec2(100,32), padding, 0, false),
binding(binding) {
label = new Label(L"");
label = std::make_shared<Label>(L"");
add(label);
scrollable(false);
}
+1 -1
View File
@@ -137,7 +137,7 @@ namespace gui {
protected:
glm::vec4 hoverColor {0.05f, 0.1f, 0.2f, 0.75f};
glm::vec4 focusedColor {0.0f, 0.0f, 0.0f, 1.0f};
Label* label;
std::shared_ptr<Label> label;
Binding& binding;
public:
InputBindBox(Binding& binding, glm::vec4 padding=glm::vec4(6.0f));
+36 -20
View File
@@ -10,24 +10,31 @@ using namespace gui;
using glm::vec2;
using glm::vec4;
Button* guiutil::backButton(PagesControl* menu) {
return (new Button(langs::get(L"Back"), vec4(10.f)))->listenAction([=](GUI* gui) {
std::shared_ptr<Button> guiutil::backButton(std::shared_ptr<PagesControl> menu) {
auto button = std::make_shared<Button>(
langs::get(L"Back"), vec4(10.f)
);
button->listenAction([=](GUI* gui) {
menu->back();
});
return button;
}
Button* guiutil::gotoButton(
std::wstring text,
const std::string& page,
PagesControl* menu) {
std::shared_ptr<Button> guiutil::gotoButton(
std::wstring text,
const std::string& page,
std::shared_ptr<PagesControl> menu
) {
text = langs::get(text, L"menu");
return (new Button(text, vec4(10.f)))->listenAction([=](GUI* gui) {
auto button = std::make_shared<Button>(text, vec4(10.f));
button->listenAction([=](GUI* gui) {
menu->set(page);
});
return button;
}
void guiutil::alert(GUI* gui, const std::wstring& text, gui::runnable on_hidden) {
PagesControl* menu = gui->getMenu();
auto menu = gui->getMenu();
Panel* panel = new Panel(vec2(500, 200), vec4(8.0f), 8.0f);
panel->setColor(vec4(0.0f, 0.0f, 0.0f, 0.5f));
@@ -43,17 +50,19 @@ void guiutil::alert(GUI* gui, const std::wstring& text, gui::runnable on_hidden)
}
extra = std::min(extra, wrap_length);
std::wstring part = text.substr(offset, extra);
panel->add(new Label(part));
panel->add(std::make_shared<Label>(part));
offset += extra;
}
} else {
panel->add(new Label(text));
panel->add(std::make_shared<Label>(text));
}
panel->add((new Button(langs::get(L"Ok"), vec4(10.f)))->listenAction([=](GUI* gui) {
auto button = std::make_shared<Button>(langs::get(L"Ok"), vec4(10.f));
button->listenAction([=](GUI* gui) {
if (on_hidden)
on_hidden();
menu->back();
}));
});
panel->add(button);
panel->refresh();
menu->add("<alert>", panel);
menu->set("<alert>");
@@ -68,20 +77,27 @@ void guiutil::confirm(
if (yestext.empty()) yestext = langs::get(L"Yes");
if (notext.empty()) notext = langs::get(L"No");
PagesControl* menu = gui->getMenu();
Panel* panel = new Panel(vec2(600, 200), vec4(8.0f), 8.0f);
auto menu = gui->getMenu();
auto panel = std::make_shared<Panel>(vec2(600, 200), vec4(8.0f), 8.0f);
panel->setColor(vec4(0.0f, 0.0f, 0.0f, 0.5f));
panel->add(new Label(text));
Panel* subpanel = new Panel(vec2(600, 53));
panel->add(std::make_shared<Label>(text));
auto subpanel = std::make_shared<Panel>(vec2(600, 53));
subpanel->setColor(vec4(0));
subpanel->add((new Button(yestext, vec4(8.0f)))->listenAction([=](GUI*){
auto yesbtn = std::make_shared<Button>(yestext, vec4(8.f));
yesbtn->listenAction([=](GUI*){
if (on_confirm)
on_confirm();
menu->back();
}));
subpanel->add((new Button(notext, vec4(8.0f)))->listenAction([=](GUI*){
});
subpanel->add(yesbtn);
auto nobtn = std::make_shared<Button>(notext, vec4(8.f));
nobtn->listenAction([=](GUI*){
menu->back();
}));
});
subpanel->add(nobtn);
panel->add(subpanel);
panel->refresh();
+23 -5
View File
@@ -1,6 +1,7 @@
#ifndef FRONTEND_GUI_GUI_UTIL_H_
#define FRONTEND_GUI_GUI_UTIL_H_
#include <memory>
#include <string>
#include "GUI.h"
@@ -9,11 +10,28 @@ namespace gui {
}
namespace guiutil {
gui::Button* backButton(gui::PagesControl* menu);
gui::Button* gotoButton(std::wstring text, const std::string& page, gui::PagesControl* menu);
void alert(gui::GUI* gui, const std::wstring& text, gui::runnable on_hidden=nullptr);
void confirm(gui::GUI* gui, const std::wstring& text, gui::runnable on_confirm=nullptr,
std::wstring yestext=L"", std::wstring notext=L"");
std::shared_ptr<gui::Button> backButton(
std::shared_ptr<gui::PagesControl> menu
);
std::shared_ptr<gui::Button> gotoButton(
std::wstring text,
const std::string& page,
std::shared_ptr<gui::PagesControl> menu
);
void alert(
gui::GUI* gui,
const std::wstring& text,
gui::runnable on_hidden=nullptr
);
void confirm(
gui::GUI* gui,
const std::wstring& text,
gui::runnable on_confirm=nullptr,
std::wstring yestext=L"",
std::wstring notext=L"");
}
#endif // FRONTEND_GUI_GUI_UTIL_H_
-4
View File
@@ -108,10 +108,6 @@ void Container::add(std::shared_ptr<UINode> node) {
refresh();
}
void Container::add(UINode* node) {
add(std::shared_ptr<UINode>(node));
}
void Container::add(std::shared_ptr<UINode> node, glm::vec2 coord) {
node->setCoord(coord);
add(node);
-1
View File
@@ -39,7 +39,6 @@ namespace gui {
virtual std::shared_ptr<UINode> getAt(glm::vec2 pos, std::shared_ptr<UINode> self) override;
virtual void addBack(std::shared_ptr<UINode> node);
virtual void add(std::shared_ptr<UINode> node);
virtual void add(UINode* node);
virtual void add(std::shared_ptr<UINode> node, glm::vec2 coord);
virtual void remove(std::shared_ptr<UINode> node);
virtual void scrolled(int value) override;