game/mario/gui/gui.hpp

195 lines
5.8 KiB
C++
Raw Normal View History

2022-06-21 12:52:36 +00:00
#ifndef GUIELEMENTS
#define GUIELEMENTS
#include "../../sdlpp/sdlpp.hpp"
#include "../global_vars.hpp"
#include "../objectids.hpp"
2022-06-22 19:42:57 +00:00
#include <functional>
#include <utility>
2022-06-24 19:50:30 +00:00
enum ButtonState {
NORMAL,
HIGHLIGHTED,
DISABLED,
};
struct ButtonConfig {
2022-06-22 19:42:57 +00:00
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;
2022-06-22 19:42:57 +00:00
};
2022-06-21 12:52:36 +00:00
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)
2022-07-21 18:17:24 +00:00
: TextRenderer(x, y, w, h, r, g_text_config->getFont(), text,
config.font_color, config.font_outline_color,
config.outline),
2022-06-22 19:42:57 +00:00
click_fun(std::move(click_fun)), func_input(input),
2022-06-24 19:50:30 +00:00
config(config), button_text(text) {
setColor(config.bg_color);
2022-06-21 12:52:36 +00:00
setId(BUTTON_ID);
addCollision(SDLPP::RectColider(0, 0, 1, 1));
2022-06-24 19:50:30 +00:00
state = NORMAL;
}
void setButtonText(const std::string &text) {
button_text = text;
should_update_text = true;
2022-06-21 12:52:36 +00:00
}
2022-06-24 19:50:30 +00:00
void setFontColor(const std::string &color) {
config.font_color = color;
2022-07-21 18:17:24 +00:00
if (!highlighted && !disabled) {
2022-06-24 19:50:30 +00:00
should_update_color = true;
2022-06-22 19:42:57 +00:00
}
}
2022-06-24 19:50:30 +00:00
void setFontColorHighlight(const std::string &color) {
config.font_color_highlight = color;
2022-07-21 18:17:24 +00:00
if (highlighted && !disabled) {
2022-06-24 19:50:30 +00:00
should_update_color = true;
2022-06-22 19:42:57 +00:00
}
}
2022-06-24 19:50:30 +00:00
void setFontColorDisabled(const std::string &color) {
config.font_color_disabled = color;
2022-07-21 18:17:24 +00:00
if (disabled) {
2022-06-24 19:50:30 +00:00
should_update_color = true;
2022-06-22 19:42:57 +00:00
}
}
2022-06-24 19:50:30 +00:00
void setFontOutlineColor(const std::string &color) {
config.font_outline_color = color;
2022-07-21 18:17:24 +00:00
if (!highlighted && !disabled) {
2022-06-24 19:50:30 +00:00
should_update_color = true;
2022-06-22 19:42:57 +00:00
}
}
2022-06-24 19:50:30 +00:00
void setFontOutlineColorHighlight(const std::string &color) {
config.font_outline_color_highlight = color;
2022-07-21 18:17:24 +00:00
if (highlighted && !disabled) {
2022-06-24 19:50:30 +00:00
should_update_color = true;
2022-06-22 19:42:57 +00:00
}
}
2022-06-24 19:50:30 +00:00
void setFontOutlineColorDisabled(const std::string &color) {
config.font_outline_color_disabled = color;
2022-07-21 18:17:24 +00:00
if (disabled) {
2022-06-24 19:50:30 +00:00
should_update_color = true;
2022-06-22 19:42:57 +00:00
}
}
2022-06-21 12:52:36 +00:00
void setBackgroundColor(const std::string &color) {
config.bg_color = color;
2022-07-21 18:17:24 +00:00
if (!highlighted && !disabled) {
2022-06-22 19:42:57 +00:00
setColor(color);
}
}
void setBackgroundColorHighlight(const std::string &color) {
config.bg_color_highlight = color;
2022-07-21 18:17:24 +00:00
if (highlighted && !disabled) {
2022-06-22 19:42:57 +00:00
setColor(color);
}
}
void setBackgroundColorDisabled(const std::string &color) {
config.bg_color_disabled = color;
2022-07-21 18:17:24 +00:00
if (disabled) {
2022-06-22 19:42:57 +00:00
setColor(color);
}
2022-06-21 12:52:36 +00:00
}
void performFunction() {
2022-07-21 18:17:24 +00:00
if (!disabled) {
click_fun(func_input, this);
2022-06-22 19:42:57 +00:00
}
}
uint64_t getButtonIndex() const {
return _id;
}
void setButtonIndex(uint64_t id) {
_id = id;
}
2022-06-24 19:50:30 +00:00
void setHighlight() {
2022-07-21 18:17:24 +00:00
if (!disabled) {
setColor(config.bg_color_highlight);
2022-06-24 19:50:30 +00:00
should_update_color = true;
state = HIGHLIGHTED;
2022-06-22 19:42:57 +00:00
}
highlighted = true;
}
2022-06-24 19:50:30 +00:00
void unsetHighlight() {
2022-07-21 18:17:24 +00:00
if (!disabled) {
setColor(config.bg_color);
2022-06-24 19:50:30 +00:00
should_update_color = true;
state = NORMAL;
2022-06-22 19:42:57 +00:00
}
highlighted = false;
}
2022-06-24 19:50:30 +00:00
void disable() {
setColor(config.bg_color_disabled);
2022-06-24 19:50:30 +00:00
should_update_color = true;
state = DISABLED;
2022-06-22 19:42:57 +00:00
disabled = true;
}
2022-06-24 19:50:30 +00:00
void enable() {
2022-07-21 18:17:24 +00:00
if (!highlighted) {
setColor(config.bg_color);
2022-06-24 19:50:30 +00:00
should_update_color = true;
state = NORMAL;
2022-06-22 19:42:57 +00:00
} else {
setColor(config.bg_color_highlight);
2022-06-24 19:50:30 +00:00
should_update_color = true;
state = HIGHLIGHTED;
2022-06-22 19:42:57 +00:00
}
disabled = false;
2022-06-21 12:52:36 +00:00
}
2022-06-22 19:42:57 +00:00
2022-06-24 19:50:30 +00:00
void update() {
2022-07-21 18:17:24 +00:00
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;
2022-06-24 19:50:30 +00:00
}
}
2022-07-21 18:17:24 +00:00
if (should_update_text) {
2022-06-24 19:50:30 +00:00
changeText(button_text);
}
}
2022-07-21 18:17:24 +00:00
2022-06-24 19:50:30 +00:00
private:
std::function<void(void *, Button *)> click_fun;
2022-06-21 12:52:36 +00:00
void *func_input;
2022-06-22 19:42:57 +00:00
uint64_t _id{};
ButtonConfig config{};
2022-06-22 19:42:57 +00:00
bool highlighted = false;
bool disabled = false;
2022-06-24 19:50:30 +00:00
std::string button_text;
ButtonState state = NORMAL;
bool should_update_color = false;
bool should_update_text = false;
2022-06-21 12:52:36 +00:00
};
2022-06-22 19:42:57 +00:00
/*std::shared_ptr<SDLPP::RectangleRender>
2022-06-21 12:52:36 +00:00
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,
2022-06-22 19:42:57 +00:00
std::function<void(void *)> click_fun, void *input);*/
2022-06-21 12:52:36 +00:00
#endif