From 38dfa533c42ca1a94080ba9af2fb95bdffe2675a Mon Sep 17 00:00:00 2001 From: zvon Date: Fri, 24 Jun 2022 21:50:30 +0200 Subject: [PATCH] Button update function --- mario/gui/gui.hpp | 90 ++++++++++++++++++++++++++++++++--------------- 1 file changed, 62 insertions(+), 28 deletions(-) diff --git a/mario/gui/gui.hpp b/mario/gui/gui.hpp index 8132667..96f4d62 100644 --- a/mario/gui/gui.hpp +++ b/mario/gui/gui.hpp @@ -5,9 +5,14 @@ #include "../global_vars.hpp" #include "../objectids.hpp" #include -#include #include +enum ButtonState { + NORMAL, + HIGHLIGHTED, + DISABLED, +}; + struct ButtonConfig { std::string font_color; std::string font_outline_color; @@ -31,45 +36,50 @@ public: : TextRenderer(x, y, w, h, r, g_text_config->getFont(), text, config.font_color, config.font_outline_color, config.outline), click_fun(std::move(click_fun)), func_input(input), - config(config) { + config(config), button_text(text) { setColor(config.bg_color); setId(BUTTON_ID); addCollision(SDLPP::RectColider(0, 0, 1, 1)); + state = NORMAL; } - void setFontColor(const std::string &color, std::mutex &mutex) { + void setButtonText(const std::string &text) { + button_text = text; + should_update_text = true; + } + void setFontColor(const std::string &color) { config.font_color = color; if(!highlighted && !disabled) { - updateTextColor(config.font_color, config.font_outline_color, mutex); + should_update_color = true; } } - void setFontColorHighlight(const std::string &color, std::mutex &mutex) { + void setFontColorHighlight(const std::string &color) { config.font_color_highlight = color; if(highlighted && !disabled) { - updateTextColor(config.font_color_highlight, config.font_outline_color_highlight, mutex); + should_update_color = true; } } - void setFontColorDisabled(const std::string &color, std::mutex &mutex) { + void setFontColorDisabled(const std::string &color) { config.font_color_disabled = color; if(disabled) { - updateTextColor(config.font_color_disabled, config.font_outline_color_disabled, mutex); + should_update_color = true; } } - void setFontOutlineColor(const std::string &color, std::mutex &mutex) { + void setFontOutlineColor(const std::string &color) { config.font_outline_color = color; if(!highlighted && !disabled) { - updateTextColor(config.font_color, config.font_outline_color, mutex); + should_update_color = true; } } - void setFontOutlineColorHighlight(const std::string &color, std::mutex &mutex) { + void setFontOutlineColorHighlight(const std::string &color) { config.font_outline_color_highlight = color; if(highlighted && !disabled) { - updateTextColor(config.font_color_highlight, config.font_outline_color_highlight, mutex); + should_update_color = true; } } - void setFontOutlineColorDisabled(const std::string &color, std::mutex &mutex) { + void setFontOutlineColorDisabled(const std::string &color) { config.font_outline_color_disabled = color; if(disabled) { - updateTextColor(config.font_color_disabled, config.font_outline_color_disabled, mutex); + should_update_color = true; } } void setBackgroundColor(const std::string &color) { @@ -101,47 +111,71 @@ public: void setButtonIndex(uint64_t id) { _id = id; } - void setHighlight(std::mutex &mutex) { + void setHighlight() { if(!disabled) { setColor(config.bg_color_highlight); - updateTextColor(config.font_color_highlight, config.font_outline_color_highlight, mutex); + should_update_color = true; + state = HIGHLIGHTED; } highlighted = true; } - void unsetHighlight(std::mutex &mutex) { + void unsetHighlight() { if(!disabled) { setColor(config.bg_color); - updateTextColor(config.font_color, config.font_outline_color, mutex); + should_update_color = true; + state = NORMAL; } highlighted = false; } - void disable(std::mutex &mutex) { + void disable() { setColor(config.bg_color_disabled); - updateTextColor(config.font_color_disabled, config.font_outline_color_disabled, mutex); + should_update_color = true; + state = DISABLED; disabled = true; } - void enable(std::mutex &mutex) { + void enable() { if(!highlighted) { setColor(config.bg_color); - updateTextColor(config.font_color, config.font_outline_color, mutex); + should_update_color = true; + state = NORMAL; } else { setColor(config.bg_color_highlight); - updateTextColor(config.font_color_highlight, config.font_outline_color_highlight, mutex); + should_update_color = true; + state = HIGHLIGHTED; } disabled = false; } -private: - void updateTextColor(const std::string &font, const std::string &outline, std::mutex &mutex) { - std::lock_guard textUpdate(mutex); - setTextColor(g_text_config->getFont(), font, outline, config.outline); - } + void update() { + if(should_update_color) { + switch(state) { + case NORMAL: + setTextColor(g_text_config->getFont(), config.font_color, config.font_outline_color, config.outline); + break; + case HIGHLIGHTED: + setTextColor(g_text_config->getFont(), config.font_color_highlight, config.font_outline_color_highlight, config.outline); + break; + case DISABLED: + setTextColor(g_text_config->getFont(), config.font_color_disabled, config.font_outline_color_disabled, config.outline); + default: + break; + } + } + if(should_update_text) { + changeText(button_text); + } + } +private: std::function click_fun; void *func_input; uint64_t _id{}; ButtonConfig config{}; bool highlighted = false; bool disabled = false; + std::string button_text; + ButtonState state = NORMAL; + bool should_update_color = false; + bool should_update_text = false; }; /*std::shared_ptr