Move map/tool controls to new button API
This commit is contained in:
+38
-39
@@ -8,9 +8,7 @@
|
||||
#include <mutex>
|
||||
#include <utility>
|
||||
|
||||
#define TEXT_OUTLINE 0.2
|
||||
|
||||
struct ButtonColors {
|
||||
struct ButtonConfig {
|
||||
std::string font_color;
|
||||
std::string font_outline_color;
|
||||
std::string bg_color;
|
||||
@@ -20,6 +18,7 @@ struct ButtonColors {
|
||||
std::string font_color_disabled;
|
||||
std::string font_outline_color_disabled;
|
||||
std::string bg_color_disabled;
|
||||
double outline;
|
||||
};
|
||||
|
||||
class Button : public SDLPP::TextRenderer {
|
||||
@@ -27,73 +26,73 @@ public:
|
||||
Button() = delete;
|
||||
Button(double x, double y, double w, double h,
|
||||
std::shared_ptr<SDLPP::Renderer> &r, const std::string &text,
|
||||
const ButtonColors &colors,
|
||||
std::function<void(void *)> click_fun, void *input)
|
||||
: TextRenderer(x, y, w, h, r, g_text_config->getFont(), text, colors.font_color,
|
||||
colors.font_outline_color, TEXT_OUTLINE),
|
||||
const ButtonConfig &config,
|
||||
std::function<void(void *, Button *)> click_fun, void *input)
|
||||
: 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),
|
||||
colors(colors) {
|
||||
setColor(colors.bg_color);
|
||||
config(config) {
|
||||
setColor(config.bg_color);
|
||||
setId(BUTTON_ID);
|
||||
addCollision(SDLPP::RectColider(0, 0, 1, 1));
|
||||
}
|
||||
void setFontColor(const std::string &color, std::mutex &mutex) {
|
||||
colors.font_color = color;
|
||||
config.font_color = color;
|
||||
if(!highlighted && !disabled) {
|
||||
updateTextColor(colors.font_color, colors.font_outline_color, mutex);
|
||||
updateTextColor(config.font_color, config.font_outline_color, mutex);
|
||||
}
|
||||
}
|
||||
void setFontColorHighlight(const std::string &color, std::mutex &mutex) {
|
||||
colors.font_color_highlight = color;
|
||||
config.font_color_highlight = color;
|
||||
if(highlighted && !disabled) {
|
||||
updateTextColor(colors.font_color_highlight, colors.font_outline_color_highlight, mutex);
|
||||
updateTextColor(config.font_color_highlight, config.font_outline_color_highlight, mutex);
|
||||
}
|
||||
}
|
||||
void setFontColorDisabled(const std::string &color, std::mutex &mutex) {
|
||||
colors.font_color_disabled = color;
|
||||
config.font_color_disabled = color;
|
||||
if(disabled) {
|
||||
updateTextColor(colors.font_color_disabled, colors.font_outline_color_disabled, mutex);
|
||||
updateTextColor(config.font_color_disabled, config.font_outline_color_disabled, mutex);
|
||||
}
|
||||
}
|
||||
void setFontOutlineColor(const std::string &color, std::mutex &mutex) {
|
||||
colors.font_outline_color = color;
|
||||
config.font_outline_color = color;
|
||||
if(!highlighted && !disabled) {
|
||||
updateTextColor(colors.font_color, colors.font_outline_color, mutex);
|
||||
updateTextColor(config.font_color, config.font_outline_color, mutex);
|
||||
}
|
||||
}
|
||||
void setFontOutlineColorHighlight(const std::string &color, std::mutex &mutex) {
|
||||
colors.font_outline_color_highlight = color;
|
||||
config.font_outline_color_highlight = color;
|
||||
if(highlighted && !disabled) {
|
||||
updateTextColor(colors.font_color_highlight, colors.font_outline_color_highlight, mutex);
|
||||
updateTextColor(config.font_color_highlight, config.font_outline_color_highlight, mutex);
|
||||
}
|
||||
}
|
||||
void setFontOutlineColorDisabled(const std::string &color, std::mutex &mutex) {
|
||||
colors.font_outline_color_disabled = color;
|
||||
config.font_outline_color_disabled = color;
|
||||
if(disabled) {
|
||||
updateTextColor(colors.font_color_disabled, colors.font_outline_color_disabled, mutex);
|
||||
updateTextColor(config.font_color_disabled, config.font_outline_color_disabled, mutex);
|
||||
}
|
||||
}
|
||||
void setBackgroundColor(const std::string &color) {
|
||||
colors.bg_color = color;
|
||||
config.bg_color = color;
|
||||
if(!highlighted && !disabled) {
|
||||
setColor(color);
|
||||
}
|
||||
}
|
||||
void setBackgroundColorHighlight(const std::string &color) {
|
||||
colors.bg_color_highlight = color;
|
||||
config.bg_color_highlight = color;
|
||||
if(highlighted && !disabled) {
|
||||
setColor(color);
|
||||
}
|
||||
}
|
||||
void setBackgroundColorDisabled(const std::string &color) {
|
||||
colors.bg_color_disabled = color;
|
||||
config.bg_color_disabled = color;
|
||||
if(disabled) {
|
||||
setColor(color);
|
||||
}
|
||||
}
|
||||
void performFunction() const {
|
||||
void performFunction() {
|
||||
if(!disabled) {
|
||||
click_fun(func_input);
|
||||
click_fun(func_input, this);
|
||||
}
|
||||
}
|
||||
uint64_t getButtonIndex() const {
|
||||
@@ -104,43 +103,43 @@ public:
|
||||
}
|
||||
void setHighlight(std::mutex &mutex) {
|
||||
if(!disabled) {
|
||||
setColor(colors.bg_color_highlight);
|
||||
updateTextColor(colors.font_color_highlight, colors.font_outline_color_highlight, mutex);
|
||||
setColor(config.bg_color_highlight);
|
||||
updateTextColor(config.font_color_highlight, config.font_outline_color_highlight, mutex);
|
||||
}
|
||||
highlighted = true;
|
||||
}
|
||||
void unsetHighlight(std::mutex &mutex) {
|
||||
if(!disabled) {
|
||||
setColor(colors.bg_color);
|
||||
updateTextColor(colors.font_color, colors.font_outline_color, mutex);
|
||||
setColor(config.bg_color);
|
||||
updateTextColor(config.font_color, config.font_outline_color, mutex);
|
||||
}
|
||||
highlighted = false;
|
||||
}
|
||||
void disable(std::mutex &mutex) {
|
||||
setColor(colors.bg_color_disabled);
|
||||
updateTextColor(colors.font_color_disabled, colors.font_outline_color_disabled, mutex);
|
||||
setColor(config.bg_color_disabled);
|
||||
updateTextColor(config.font_color_disabled, config.font_outline_color_disabled, mutex);
|
||||
disabled = true;
|
||||
}
|
||||
void enable(std::mutex &mutex) {
|
||||
if(!highlighted) {
|
||||
setColor(colors.bg_color);
|
||||
updateTextColor(colors.font_color, colors.font_outline_color, mutex);
|
||||
setColor(config.bg_color);
|
||||
updateTextColor(config.font_color, config.font_outline_color, mutex);
|
||||
} else {
|
||||
setColor(colors.bg_color_highlight);
|
||||
updateTextColor(colors.font_color_highlight, colors.font_outline_color_highlight, mutex);
|
||||
setColor(config.bg_color_highlight);
|
||||
updateTextColor(config.font_color_highlight, config.font_outline_color_highlight, mutex);
|
||||
}
|
||||
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, TEXT_OUTLINE);
|
||||
setTextColor(g_text_config->getFont(), font, outline, config.outline);
|
||||
}
|
||||
|
||||
std::function<void(void *)> click_fun;
|
||||
std::function<void(void *, Button *)> click_fun;
|
||||
void *func_input;
|
||||
uint64_t _id{};
|
||||
ButtonColors colors{};
|
||||
ButtonConfig config{};
|
||||
bool highlighted = false;
|
||||
bool disabled = false;
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user