195 lines
5.8 KiB
C++
195 lines
5.8 KiB
C++
#ifndef GUIELEMENTS
|
|
#define GUIELEMENTS
|
|
|
|
#include "../../sdlpp/sdlpp.hpp"
|
|
#include "../global_vars.hpp"
|
|
#include "../objectids.hpp"
|
|
#include <functional>
|
|
#include <utility>
|
|
|
|
enum ButtonState {
|
|
NORMAL,
|
|
HIGHLIGHTED,
|
|
DISABLED,
|
|
};
|
|
|
|
struct ButtonConfig {
|
|
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;
|
|
double outline;
|
|
};
|
|
|
|
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 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),
|
|
config(config), button_text(text) {
|
|
setColor(config.bg_color);
|
|
setId(BUTTON_ID);
|
|
addCollision(SDLPP::RectColider(0, 0, 1, 1));
|
|
state = NORMAL;
|
|
}
|
|
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) {
|
|
should_update_color = true;
|
|
}
|
|
}
|
|
void setFontColorHighlight(const std::string &color) {
|
|
config.font_color_highlight = color;
|
|
if (highlighted && !disabled) {
|
|
should_update_color = true;
|
|
}
|
|
}
|
|
void setFontColorDisabled(const std::string &color) {
|
|
config.font_color_disabled = color;
|
|
if (disabled) {
|
|
should_update_color = true;
|
|
}
|
|
}
|
|
void setFontOutlineColor(const std::string &color) {
|
|
config.font_outline_color = color;
|
|
if (!highlighted && !disabled) {
|
|
should_update_color = true;
|
|
}
|
|
}
|
|
void setFontOutlineColorHighlight(const std::string &color) {
|
|
config.font_outline_color_highlight = color;
|
|
if (highlighted && !disabled) {
|
|
should_update_color = true;
|
|
}
|
|
}
|
|
void setFontOutlineColorDisabled(const std::string &color) {
|
|
config.font_outline_color_disabled = color;
|
|
if (disabled) {
|
|
should_update_color = true;
|
|
}
|
|
}
|
|
void setBackgroundColor(const std::string &color) {
|
|
config.bg_color = color;
|
|
if (!highlighted && !disabled) {
|
|
setColor(color);
|
|
}
|
|
}
|
|
void setBackgroundColorHighlight(const std::string &color) {
|
|
config.bg_color_highlight = color;
|
|
if (highlighted && !disabled) {
|
|
setColor(color);
|
|
}
|
|
}
|
|
void setBackgroundColorDisabled(const std::string &color) {
|
|
config.bg_color_disabled = color;
|
|
if (disabled) {
|
|
setColor(color);
|
|
}
|
|
}
|
|
void performFunction() {
|
|
if (!disabled) {
|
|
click_fun(func_input, this);
|
|
}
|
|
}
|
|
uint64_t getButtonIndex() const {
|
|
return _id;
|
|
}
|
|
void setButtonIndex(uint64_t id) {
|
|
_id = id;
|
|
}
|
|
void setHighlight() {
|
|
if (!disabled) {
|
|
setColor(config.bg_color_highlight);
|
|
should_update_color = true;
|
|
state = HIGHLIGHTED;
|
|
}
|
|
highlighted = true;
|
|
}
|
|
void unsetHighlight() {
|
|
if (!disabled) {
|
|
setColor(config.bg_color);
|
|
should_update_color = true;
|
|
state = NORMAL;
|
|
}
|
|
highlighted = false;
|
|
}
|
|
void disable() {
|
|
setColor(config.bg_color_disabled);
|
|
should_update_color = true;
|
|
state = DISABLED;
|
|
disabled = true;
|
|
}
|
|
void enable() {
|
|
if (!highlighted) {
|
|
setColor(config.bg_color);
|
|
should_update_color = true;
|
|
state = NORMAL;
|
|
} else {
|
|
setColor(config.bg_color_highlight);
|
|
should_update_color = true;
|
|
state = HIGHLIGHTED;
|
|
}
|
|
disabled = false;
|
|
}
|
|
|
|
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;
|
|
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<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);*/
|
|
|
|
#endif |