RichButton removed, use Container instead

This commit is contained in:
MihailRis 2024-03-20 18:53:53 +03:00
parent b836ec514a
commit 9611252d84
10 changed files with 58 additions and 90 deletions

View File

@ -42,6 +42,13 @@ namespace menus {
extern std::string generatorID;
}
static void load_page(Engine* engine, const std::string& name) {
auto menu = engine->getGUI()->getMenu();
auto file = engine->getResPaths()->find("layouts/pages/"+name+".xml");
auto node = UiDocument::readElement(file);
menu->addPage(name, node);
}
void menus::create_version_label(Engine* engine) {
auto gui = engine->getGUI();
auto vlabel = std::make_shared<gui::Label>(
@ -237,7 +244,7 @@ std::shared_ptr<Panel> create_worlds_panel(Engine* engine) {
auto name = folder.filename().u8string();
auto namews = util::str2wstr_utf8(name);
auto btn = std::make_shared<RichButton>(glm::vec2(390, 46));
auto btn = std::make_shared<Container>(glm::vec2(390, 46));
btn->setColor(glm::vec4(0.06f, 0.12f, 0.18f, 0.7f));
btn->setHoverColor(glm::vec4(0.09f, 0.17f, 0.2f, 0.6f));
btn->listenAction([=](GUI*) {
@ -245,11 +252,11 @@ std::shared_ptr<Panel> create_worlds_panel(Engine* engine) {
});
btn->add(std::make_shared<Label>(namews), glm::vec2(8, 8));
auto delbtn = std::dynamic_pointer_cast<Button>(guiutil::create(
auto delbtn = guiutil::create(
"<button color='#00000000' hover-color='#FFFFFF2B' padding='2,2,2,2'>"
" <image src='gui/delete_icon' size='32,32' color='#FFFFFF80'/>"
"</button>"
));
);
delbtn->listenAction([=](GUI* gui) {
guiutil::confirm(gui, langs::get(L"delete-confirm", L"world")+
L" ("+util::str2wstr_utf8(folder.u8string())+L")", [=]() {
@ -280,11 +287,6 @@ void create_main_menu_panel(Engine* engine) {
));
}
void create_404_page(Engine* engine) {
auto menu = engine->getGUI()->getMenu();
menu->addPage("404", UiDocument::readElement(engine->getResPaths()->find("layouts/404.xml")));
}
void menus::create_menus(Engine* engine) {
menus::generatorID = WorldGenerators::getDefaultGeneratorID();
create_new_world_panel(engine);
@ -292,12 +294,12 @@ void menus::create_menus(Engine* engine) {
create_languages_panel(engine);
create_main_menu_panel(engine);
create_world_generators_panel(engine);
create_404_page(engine);
load_page(engine, "404");
}
void menus::refresh_menus(Engine* engine) {
create_main_menu_panel(engine);
create_new_world_panel(engine);
create_world_generators_panel(engine);
create_404_page(engine);
load_page(engine, "404");
}

View File

@ -69,7 +69,7 @@ void menus::create_world_generators_panel(Engine* engine) {
std::sort(generatorsIDs.begin(), generatorsIDs.end());
for (std::string& id : generatorsIDs) {
const std::string& fullName = translate_generator_id(id);
auto button = std::make_shared<RichButton>(glm::vec2(80, 30));
auto button = std::make_shared<Container>(glm::vec2(80, 30));
auto idlabel = std::make_shared<Label>("["+id+"]");
idlabel->setColor(glm::vec4(1, 1, 1, 0.5f));

View File

@ -31,7 +31,7 @@ std::shared_ptr<Panel> menus::create_packs_panel(
panel->setScrollable(true);
for (auto& pack : packs) {
auto packpanel = std::make_shared<RichButton>(glm::vec2(540, 80));
auto packpanel = std::make_shared<Container>(glm::vec2(540, 80));
packpanel->setColor(glm::vec4(0.06f, 0.12f, 0.18f, 0.7f));
if (callback) {
packpanel->listenAction([=](GUI*) {
@ -77,11 +77,11 @@ std::shared_ptr<Panel> menus::create_packs_panel(
packpanel->add(std::make_shared<Image>(icon, glm::vec2(64)), glm::vec2(8));
if (remover && pack.id != "base") {
auto rembtn = std::dynamic_pointer_cast<Button>(guiutil::create(
auto rembtn = guiutil::create(
"<button color='#00000000' hover-color='#FFFFFF2B'>"
" <image src='gui/cross' size='32,32'/>"
"</button>"
));
);
rembtn->listenAction([=](GUI* gui) {
remover(pack);
});

View File

@ -43,12 +43,22 @@ UINode* UINode::getParent() const {
return parent;
}
UINode* UINode::listenAction(onaction action) {
actions.push_back(action);
return this;
}
void UINode::click(GUI*, int x, int y) {
pressed = true;
}
void UINode::mouseRelease(GUI*, int x, int y) {
void UINode::mouseRelease(GUI* gui, int x, int y) {
pressed = false;
if (isInside(glm::vec2(x, y))) {
for (auto callback : actions) {
callback(gui);
}
}
}
bool UINode::isPressed() const {
@ -136,6 +146,7 @@ void UINode::setMinSize(glm::vec2 minSize) {
void UINode::setColor(glm::vec4 color) {
this->color = color;
this->hoverColor = color;
this->pressedColor = color;
}
void UINode::setHoverColor(glm::vec4 newColor) {
@ -150,6 +161,14 @@ glm::vec4 UINode::getColor() const {
return color;
}
glm::vec4 UINode::getPressedColor() const {
return pressedColor;
}
void UINode::setPressedColor(glm::vec4 color) {
pressedColor = color;
}
void UINode::setMargin(glm::vec4 margin) {
this->margin = margin;
}

View File

@ -55,6 +55,8 @@ namespace gui {
glm::vec4 color {1.0f};
/// @brief element color when mouse is over it
glm::vec4 hoverColor {1.0f};
/// @brief element color when clicked
glm::vec4 pressedColor {1.0f};
/// @brief element margin (only supported for Panel sub-nodes)
glm::vec4 margin {1.0f};
/// @brief is element visible
@ -77,6 +79,8 @@ namespace gui {
UINode* parent = nullptr;
/// @brief position supplier for the element (called on parent element size update)
vec2supplier positionfunc = nullptr;
/// @brief 'onclick' callbacks
std::vector<onaction> actions;
UINode(glm::vec2 size);
public:
@ -110,6 +114,9 @@ namespace gui {
virtual void setHoverColor(glm::vec4 newColor);
glm::vec4 getHoverColor() const;
virtual glm::vec4 getPressedColor() const;
virtual void setPressedColor(glm::vec4 color);
virtual void setMargin(glm::vec4 margin);
glm::vec4 getMargin() const;
@ -120,6 +127,8 @@ namespace gui {
/// @brief Get element z-index
int getZIndex() const;
virtual UINode* listenAction(onaction action);
virtual void onFocus(GUI*) {focused = true;}
virtual void click(GUI*, int x, int y);
virtual void clicked(GUI*, mousecode button) {}

View File

@ -97,7 +97,8 @@ void Container::draw(const GfxContext* pctx, Assets* assets) {
}
void Container::drawBackground(const GfxContext* pctx, Assets* assets) {
if (color.a <= 0.0f)
glm::vec4 color = isPressed() ? pressedColor : (hover ? hoverColor : this->color);
if (color.a <= 0.001f)
return;
glm::vec2 pos = calcPos();

View File

@ -229,6 +229,7 @@ Button::Button(std::shared_ptr<UINode> content, glm::vec4 padding)
add(content);
setScrollable(false);
setHoverColor(glm::vec4(0.05f, 0.1f, 0.15f, 0.75f));
setPressedColor(glm::vec4(0.0f, 0.0f, 0.0f, 0.95f));
content->setInteractive(false);
}
@ -258,6 +259,7 @@ Button::Button(
label->setInteractive(false);
add(label);
setHoverColor(glm::vec4(0.05f, 0.1f, 0.15f, 0.75f));
setPressedColor(glm::vec4(0.0f, 0.0f, 0.0f, 0.95f));
}
void Button::setText(std::wstring text) {
@ -273,14 +275,6 @@ std::wstring Button::getText() const {
return L"";
}
glm::vec4 Button::getPressedColor() const {
return pressedColor;
}
void Button::setPressedColor(glm::vec4 color) {
pressedColor = color;
}
Button* Button::textSupplier(wstringsupplier supplier) {
if (label) {
label->textSupplier(supplier);
@ -303,20 +297,6 @@ void Button::drawBackground(const GfxContext* pctx, Assets* assets) {
batch->rect(pos.x, pos.y, size.x, size.y);
}
void Button::mouseRelease(GUI* gui, int x, int y) {
UINode::mouseRelease(gui, x, y);
if (isInside(glm::vec2(x, y))) {
for (auto callback : actions) {
callback(gui);
}
}
}
Button* Button::listenAction(onaction action) {
actions.push_back(action);
return this;
}
void Button::setTextAlign(Align align) {
if (label) {
label->setAlign(align);
@ -331,33 +311,6 @@ Align Button::getTextAlign() const {
return Align::left;
}
// ============================== RichButton ==================================
RichButton::RichButton(glm::vec2 size) : Container(size) {
setHoverColor(glm::vec4(0.05f, 0.1f, 0.15f, 0.75f));
}
void RichButton::mouseRelease(GUI* gui, int x, int y) {
UINode::mouseRelease(gui, x, y);
if (isInside(glm::vec2(x, y))) {
for (auto callback : actions) {
callback(gui);
}
}
}
RichButton* RichButton::listenAction(onaction action) {
actions.push_back(action);
return this;
}
void RichButton::drawBackground(const GfxContext* pctx, Assets* assets) {
glm::vec2 pos = calcPos();
auto batch = pctx->getBatch2D();
batch->texture(nullptr);
batch->setColor(isPressed() ? pressedColor : (hover ? hoverColor : color));
batch->rect(pos.x, pos.y, size.x, size.y);
}
// ================================ TextBox ===================================
TextBox::TextBox(std::wstring placeholder, glm::vec4 padding)
: Panel(glm::vec2(200,32), padding, 0),

View File

@ -104,8 +104,6 @@ namespace gui {
class Button : public Panel {
protected:
glm::vec4 pressedColor {0.0f, 0.0f, 0.0f, 0.95f};
std::vector<onaction> actions;
std::shared_ptr<Label> label = nullptr;
public:
Button(std::shared_ptr<UINode> content,
@ -118,36 +116,17 @@ namespace gui {
virtual void drawBackground(const GfxContext* pctx, Assets* assets) override;
virtual void mouseRelease(GUI*, int x, int y) override;
virtual Button* listenAction(onaction action);
virtual Align getTextAlign() const;
virtual void setTextAlign(Align align);
virtual void setText(std::wstring text);
virtual std::wstring getText() const;
virtual glm::vec4 getPressedColor() const;
virtual void setPressedColor(glm::vec4 color);
virtual Button* textSupplier(wstringsupplier supplier);
virtual void refresh() override;
};
class RichButton : public Container {
protected:
glm::vec4 pressedColor {0.0f, 0.0f, 0.0f, 0.95f};
std::vector<onaction> actions;
public:
RichButton(glm::vec2 size);
virtual void drawBackground(const GfxContext* pctx, Assets* assets) override;
virtual void mouseRelease(GUI*, int x, int y) override;
virtual RichButton* listenAction(onaction action);
};
class TextBox : public Panel {
protected:
glm::vec4 focusedColor {0.0f, 0.0f, 0.0f, 1.0f};

View File

@ -54,11 +54,16 @@ static void _readUINode(UiXmlReader& reader, xml::xmlelement element, UINode& no
if (element->has("color")) {
glm::vec4 color = element->attr("color").asColor();
glm::vec4 hoverColor = color;
glm::vec4 pressedColor = color;
if (element->has("hover-color")) {
hoverColor = node.getHoverColor();
}
if (element->has("pressed-color")) {
pressedColor = node.getPressedColor();
}
node.setColor(color);
node.setHoverColor(hoverColor);
node.setPressedColor(pressedColor);
}
if (element->has("margin")) {
node.setMargin(element->attr("margin").asVec4());
@ -83,6 +88,9 @@ static void _readUINode(UiXmlReader& reader, xml::xmlelement element, UINode& no
if (element->has("hover-color")) {
node.setHoverColor(element->attr("hover-color").asColor());
}
if (element->has("pressed-color")) {
node.setPressedColor(element->attr("pressed-color").asColor());
}
std::string alignName = element->attr("align", "").getText();
node.setAlign(align_from_string(alignName, node.getAlign()));
@ -230,9 +238,6 @@ static std::shared_ptr<UINode> readButton(UiXmlReader& reader, xml::xmlelement e
if (element->has("text-align")) {
button->setTextAlign(align_from_string(element->attr("text-align").getText(), button->getTextAlign()));
}
if (element->has("pressed-color")) {
button->setPressedColor(element->attr("pressed-color").asColor());
}
return button;
}