Button update function

This commit is contained in:
zvon 2022-06-24 21:50:30 +02:00
parent 194415500d
commit 38dfa533c4

View File

@ -5,9 +5,14 @@
#include "../global_vars.hpp" #include "../global_vars.hpp"
#include "../objectids.hpp" #include "../objectids.hpp"
#include <functional> #include <functional>
#include <mutex>
#include <utility> #include <utility>
enum ButtonState {
NORMAL,
HIGHLIGHTED,
DISABLED,
};
struct ButtonConfig { struct ButtonConfig {
std::string font_color; std::string font_color;
std::string font_outline_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, : TextRenderer(x, y, w, h, r, g_text_config->getFont(), text, config.font_color,
config.font_outline_color, config.outline), config.font_outline_color, config.outline),
click_fun(std::move(click_fun)), func_input(input), click_fun(std::move(click_fun)), func_input(input),
config(config) { config(config), button_text(text) {
setColor(config.bg_color); setColor(config.bg_color);
setId(BUTTON_ID); setId(BUTTON_ID);
addCollision(SDLPP::RectColider(0, 0, 1, 1)); 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; config.font_color = color;
if(!highlighted && !disabled) { 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; config.font_color_highlight = color;
if(highlighted && !disabled) { 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; config.font_color_disabled = color;
if(disabled) { 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; config.font_outline_color = color;
if(!highlighted && !disabled) { 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; config.font_outline_color_highlight = color;
if(highlighted && !disabled) { 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; config.font_outline_color_disabled = color;
if(disabled) { if(disabled) {
updateTextColor(config.font_color_disabled, config.font_outline_color_disabled, mutex); should_update_color = true;
} }
} }
void setBackgroundColor(const std::string &color) { void setBackgroundColor(const std::string &color) {
@ -101,47 +111,71 @@ public:
void setButtonIndex(uint64_t id) { void setButtonIndex(uint64_t id) {
_id = id; _id = id;
} }
void setHighlight(std::mutex &mutex) { void setHighlight() {
if(!disabled) { if(!disabled) {
setColor(config.bg_color_highlight); setColor(config.bg_color_highlight);
updateTextColor(config.font_color_highlight, config.font_outline_color_highlight, mutex); should_update_color = true;
state = HIGHLIGHTED;
} }
highlighted = true; highlighted = true;
} }
void unsetHighlight(std::mutex &mutex) { void unsetHighlight() {
if(!disabled) { if(!disabled) {
setColor(config.bg_color); setColor(config.bg_color);
updateTextColor(config.font_color, config.font_outline_color, mutex); should_update_color = true;
state = NORMAL;
} }
highlighted = false; highlighted = false;
} }
void disable(std::mutex &mutex) { void disable() {
setColor(config.bg_color_disabled); setColor(config.bg_color_disabled);
updateTextColor(config.font_color_disabled, config.font_outline_color_disabled, mutex); should_update_color = true;
state = DISABLED;
disabled = true; disabled = true;
} }
void enable(std::mutex &mutex) { void enable() {
if(!highlighted) { if(!highlighted) {
setColor(config.bg_color); setColor(config.bg_color);
updateTextColor(config.font_color, config.font_outline_color, mutex); should_update_color = true;
state = NORMAL;
} else { } else {
setColor(config.bg_color_highlight); setColor(config.bg_color_highlight);
updateTextColor(config.font_color_highlight, config.font_outline_color_highlight, mutex); should_update_color = true;
state = HIGHLIGHTED;
} }
disabled = false; disabled = false;
} }
private:
void updateTextColor(const std::string &font, const std::string &outline, std::mutex &mutex) {
std::lock_guard<std::mutex> 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<void(void *, Button *)> click_fun; std::function<void(void *, Button *)> click_fun;
void *func_input; void *func_input;
uint64_t _id{}; uint64_t _id{};
ButtonConfig config{}; ButtonConfig config{};
bool highlighted = false; bool highlighted = false;
bool disabled = false; bool disabled = false;
std::string button_text;
ButtonState state = NORMAL;
bool should_update_color = false;
bool should_update_text = false;
}; };
/*std::shared_ptr<SDLPP::RectangleRender> /*std::shared_ptr<SDLPP::RectangleRender>