Playing around with buttons
This commit is contained in:
+124
-11
@@ -4,39 +4,152 @@
|
||||
#include "../../sdlpp/sdlpp.hpp"
|
||||
#include "../global_vars.hpp"
|
||||
#include "../objectids.hpp"
|
||||
#include <functional>
|
||||
#include <mutex>
|
||||
#include <utility>
|
||||
|
||||
#define TEXT_OUTLINE 0.2
|
||||
|
||||
struct ButtonColors {
|
||||
std::string font_color;
|
||||
std::string font_outline_color;
|
||||
std::string bg_color;
|
||||
std::string font_color_highlight;
|
||||
std::string font_outline_color_highlight;
|
||||
std::string bg_color_highlight;
|
||||
std::string font_color_disabled;
|
||||
std::string font_outline_color_disabled;
|
||||
std::string bg_color_disabled;
|
||||
};
|
||||
|
||||
class Button : public SDLPP::TextRenderer {
|
||||
public:
|
||||
Button() = delete;
|
||||
Button(double x, double y, double w, double h,
|
||||
std::shared_ptr<SDLPP::Renderer> &r, const std::string &text,
|
||||
const std::string &color, const std::string &outline_color,
|
||||
const std::string &background_color,
|
||||
const ButtonColors &colors,
|
||||
std::function<void(void *)> click_fun, void *input)
|
||||
: TextRenderer(x, y, w, h, r, g_text_config->getFont(), text, color,
|
||||
outline_color, 0.2),
|
||||
click_fun(click_fun), func_input(input) {
|
||||
setColor(background_color);
|
||||
: TextRenderer(x, y, w, h, r, g_text_config->getFont(), text, colors.font_color,
|
||||
colors.font_outline_color, TEXT_OUTLINE),
|
||||
click_fun(std::move(click_fun)), func_input(input),
|
||||
colors(colors) {
|
||||
setColor(colors.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;
|
||||
if(!highlighted && !disabled) {
|
||||
updateTextColor(colors.font_color, colors.font_outline_color, mutex);
|
||||
}
|
||||
}
|
||||
void setFontColorHighlight(const std::string &color, std::mutex &mutex) {
|
||||
colors.font_color_highlight = color;
|
||||
if(highlighted && !disabled) {
|
||||
updateTextColor(colors.font_color_highlight, colors.font_outline_color_highlight, mutex);
|
||||
}
|
||||
}
|
||||
void setFontColorDisabled(const std::string &color, std::mutex &mutex) {
|
||||
colors.font_color_disabled = color;
|
||||
if(disabled) {
|
||||
updateTextColor(colors.font_color_disabled, colors.font_outline_color_disabled, mutex);
|
||||
}
|
||||
}
|
||||
void setFontOutlineColor(const std::string &color, std::mutex &mutex) {
|
||||
colors.font_outline_color = color;
|
||||
if(!highlighted && !disabled) {
|
||||
updateTextColor(colors.font_color, colors.font_outline_color, mutex);
|
||||
}
|
||||
}
|
||||
void setFontOutlineColorHighlight(const std::string &color, std::mutex &mutex) {
|
||||
colors.font_outline_color_highlight = color;
|
||||
if(highlighted && !disabled) {
|
||||
updateTextColor(colors.font_color_highlight, colors.font_outline_color_highlight, mutex);
|
||||
}
|
||||
}
|
||||
void setFontOutlineColorDisabled(const std::string &color, std::mutex &mutex) {
|
||||
colors.font_outline_color_disabled = color;
|
||||
if(disabled) {
|
||||
updateTextColor(colors.font_color_disabled, colors.font_outline_color_disabled, mutex);
|
||||
}
|
||||
}
|
||||
void setBackgroundColor(const std::string &color) {
|
||||
setColor(color);
|
||||
colors.bg_color = color;
|
||||
if(!highlighted && !disabled) {
|
||||
setColor(color);
|
||||
}
|
||||
}
|
||||
void setBackgroundColorHighlight(const std::string &color) {
|
||||
colors.bg_color_highlight = color;
|
||||
if(highlighted && !disabled) {
|
||||
setColor(color);
|
||||
}
|
||||
}
|
||||
void setBackgroundColorDisabled(const std::string &color) {
|
||||
colors.bg_color_disabled = color;
|
||||
if(disabled) {
|
||||
setColor(color);
|
||||
}
|
||||
}
|
||||
void performFunction() const {
|
||||
click_fun(func_input);
|
||||
if(!disabled) {
|
||||
click_fun(func_input);
|
||||
}
|
||||
}
|
||||
uint64_t getButtonIndex() const {
|
||||
return _id;
|
||||
}
|
||||
void setButtonIndex(uint64_t id) {
|
||||
_id = id;
|
||||
}
|
||||
void setHighlight(std::mutex &mutex) {
|
||||
if(!disabled) {
|
||||
setColor(colors.bg_color_highlight);
|
||||
updateTextColor(colors.font_color_highlight, colors.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);
|
||||
}
|
||||
highlighted = false;
|
||||
}
|
||||
void disable(std::mutex &mutex) {
|
||||
setColor(colors.bg_color_disabled);
|
||||
updateTextColor(colors.font_color_disabled, colors.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);
|
||||
} else {
|
||||
setColor(colors.bg_color_highlight);
|
||||
updateTextColor(colors.font_color_highlight, colors.font_outline_color_highlight, mutex);
|
||||
}
|
||||
disabled = false;
|
||||
}
|
||||
// TODO highlight visit
|
||||
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);
|
||||
}
|
||||
|
||||
std::function<void(void *)> click_fun;
|
||||
void *func_input;
|
||||
uint64_t _id{};
|
||||
ButtonColors colors{};
|
||||
bool highlighted = false;
|
||||
bool disabled = false;
|
||||
};
|
||||
|
||||
std::shared_ptr<SDLPP::RectangleRender>
|
||||
/*std::shared_ptr<SDLPP::RectangleRender>
|
||||
createButton(double x, double y, double w, double h,
|
||||
std::shared_ptr<SDLPP::Renderer> &r, const std::string &text,
|
||||
const std::string &color, const std::string &outline_color,
|
||||
const std::string &background_color,
|
||||
std::function<void(void *)> click_fun, void *input);
|
||||
std::function<void(void *)> click_fun, void *input);*/
|
||||
|
||||
#endif
|
||||
Reference in New Issue
Block a user