Playing around with buttons
Some checks failed
continuous-integration/drone/push Build is failing

This commit is contained in:
zvon 2022-06-22 21:42:57 +02:00
parent 2402567330
commit 94d24e37fa
6 changed files with 166 additions and 27 deletions

View File

@ -57,7 +57,7 @@ struct MouseInfo {
SDLPP::Vec2D<int> edit_box; SDLPP::Vec2D<int> edit_box;
SDLPP::Vec2D<int> tool_box; SDLPP::Vec2D<int> tool_box;
ToolType::Value tool_type{}; ToolType::Value tool_type{};
const Button *cur_button{}; uint64_t cur_button_index{};
}; };
struct MapInfo { struct MapInfo {
@ -89,6 +89,7 @@ struct GlobalVars {
std::vector<std::shared_ptr<SDLPP::RenderObject>> tool_boxes; std::vector<std::shared_ptr<SDLPP::RenderObject>> tool_boxes;
std::vector<std::shared_ptr<SDLPP::RenderObject>> mod_boxes; std::vector<std::shared_ptr<SDLPP::RenderObject>> mod_boxes;
std::vector<std::shared_ptr<SDLPP::RenderObject>> character_boxes; std::vector<std::shared_ptr<SDLPP::RenderObject>> character_boxes;
std::vector<std::shared_ptr<Button>> buttons;
enum LandType::Value current_world_type{}; enum LandType::Value current_world_type{};
std::shared_ptr<MarioBlock> coin_tool; std::shared_ptr<MarioBlock> coin_tool;
std::shared_ptr<MarioBlock> generic_tool; std::shared_ptr<MarioBlock> generic_tool;
@ -103,6 +104,7 @@ struct GlobalVars {
GlobalVars global_vars; GlobalVars global_vars;
std::mutex destruction_mutex; std::mutex destruction_mutex;
std::mutex render_mutex;
void updateTool() { void updateTool() {
auto tool_index = global_vars.tool.index; auto tool_index = global_vars.tool.index;
@ -493,7 +495,15 @@ void getMousePositionFlags(SDLPP::Scene &scene) {
MouseVisitor visitor; MouseVisitor visitor;
scene.visitCollisions(*mouse, visitor); scene.visitCollisions(*mouse, visitor);
global_vars.mouse.cur_button = visitor.getCurButton(); if(visitor.getCurButton() != global_vars.mouse.cur_button_index) {
if(global_vars.mouse.cur_button_index != (uint64_t)-1) {
global_vars.buttons[global_vars.mouse.cur_button_index]->unsetHighlight(render_mutex);
}
if(visitor.getCurButton() != (uint64_t)-1) {
global_vars.buttons[visitor.getCurButton()]->setHighlight(render_mutex);
}
}
global_vars.mouse.cur_button_index = visitor.getCurButton();
global_vars.mouse.cur_flags = visitor.getFlags(); global_vars.mouse.cur_flags = visitor.getFlags();
// + 1 because the left map arrow is on position 0 // + 1 because the left map arrow is on position 0
global_vars.mouse.edit_box = global_vars.mouse.edit_box =
@ -560,7 +570,7 @@ void mouseUpAction(uint64_t flags, SDLPP::Scene &scene) {
ToolType::CHARACTER); ToolType::CHARACTER);
} }
if(MouseVisitor::button(flags)) { if(MouseVisitor::button(flags)) {
global_vars.mouse.cur_button->performFunction(); global_vars.buttons[global_vars.mouse.cur_button_index]->performFunction();
} }
} }
@ -957,6 +967,7 @@ void checkArrowsEnabled(uint64_t cur_page, uint64_t max_page,
void testButtonFunc(void *input) { void testButtonFunc(void *input) {
auto actual = static_cast<int*>(input); auto actual = static_cast<int*>(input);
std::cout << "NUM: " << *actual << std::endl; std::cout << "NUM: " << *actual << std::endl;
global_vars.buttons[0]->disable(render_mutex);
} }
#ifdef _WIN32 #ifdef _WIN32
@ -1137,9 +1148,20 @@ int main() {
updateToolSelection(0, ToolType::CHARACTER); updateToolSelection(0, ToolType::CHARACTER);
setToolColor(); setToolColor();
auto button = createButton(0, 0, 0.2, 0.2, renderer, "CLICK ME", "#FFFFFF", "#000000", "#FF0088", testButtonFunc, &num); ButtonColors default_button_theme{};
button->setStatic(); default_button_theme.bg_color = "#FF0088";
scene->addObject(button); default_button_theme.bg_color_highlight = "#FF00FF";
default_button_theme.bg_color_disabled = "#FFAAFF";
default_button_theme.font_color = "#FFFFFF";
default_button_theme.font_color_highlight = "#FFFFFF";
default_button_theme.font_color_disabled = "#BBBBBB";
default_button_theme.font_outline_color = "#000000";
default_button_theme.font_outline_color_highlight = "#444444";
default_button_theme.font_outline_color_disabled = "#888888";
global_vars.buttons.emplace_back(std::make_shared<Button>(0, 0, 0.2, 0.2, renderer, "CLICK ME", default_button_theme, testButtonFunc, &num));
global_vars.buttons.back()->setStatic();
global_vars.buttons.back()->setButtonIndex(global_vars.buttons.size() - 1);
scene->addObject(global_vars.buttons.back());
auto base = SDL_GetTicks(); auto base = SDL_GetTicks();
int frames = 0; int frames = 0;
@ -1166,8 +1188,11 @@ int main() {
SDL_framerateDelay(&gFPS); SDL_framerateDelay(&gFPS);
SDL_PumpEvents(); SDL_PumpEvents();
std::lock_guard<std::mutex> lock(destruction_mutex); std::lock_guard<std::mutex> lock(destruction_mutex);
scene->renderScene(); {
renderer->presentRenderer(); std::lock_guard<std::mutex> render(render_mutex);
scene->renderScene();
renderer->presentRenderer();
}
frames++; frames++;
if (SDL_GetTicks() - base >= 1000) { if (SDL_GetTicks() - base >= 1000) {
std::cout << "FPS: " << frames << std::endl; std::cout << "FPS: " << frames << std::endl;

View File

@ -56,7 +56,7 @@ void MouseVisitor::visit(const SDLPP::RenderObject &obj) {
break; break;
case BUTTON_ID: case BUTTON_ID:
select_flags |= SELECTED_BUTTON; select_flags |= SELECTED_BUTTON;
cur_button = reinterpret_cast<const Button*>(&obj); cur_button = reinterpret_cast<const Button*>(&obj)->getButtonIndex();
break; break;
default: default:
break; break;

View File

@ -47,7 +47,7 @@ public:
uint64_t getToolType() const { uint64_t getToolType() const {
return tool_box_type; return tool_box_type;
} }
const Button *getCurButton() { uint64_t getCurButton() const {
return cur_button; return cur_button;
} }
@ -69,7 +69,7 @@ private:
SDLPP::Vec2D<int> tool_box_location = { -1, -1 }; SDLPP::Vec2D<int> tool_box_location = { -1, -1 };
uint64_t _type{}; uint64_t _type{};
uint64_t tool_box_type = 0; uint64_t tool_box_type = 0;
const Button *cur_button; uint64_t cur_button = -1;
}; };
class ToolVisitor : public SDLPP::Visitor { class ToolVisitor : public SDLPP::Visitor {

View File

@ -1,6 +1,6 @@
#include "gui.hpp" #include "gui.hpp"
std::shared_ptr<SDLPP::RectangleRender> /*std::shared_ptr<SDLPP::RectangleRender>
createButton(double x, double y, double w, double h, createButton(double x, double y, double w, double h,
std::shared_ptr<SDLPP::Renderer> &r, const std::string &text, std::shared_ptr<SDLPP::Renderer> &r, const std::string &text,
const std::string &color, const std::string &outline_color, const std::string &color, const std::string &outline_color,
@ -9,4 +9,4 @@ createButton(double x, double y, double w, double h,
auto button = std::make_shared<Button>( auto button = std::make_shared<Button>(
x, y, w, h, r, text, color, outline_color, background_color, click_fun, input); x, y, w, h, r, text, color, outline_color, background_color, click_fun, input);
return std::static_pointer_cast<SDLPP::RectangleRender>(button); return std::static_pointer_cast<SDLPP::RectangleRender>(button);
} }*/

View File

@ -4,39 +4,152 @@
#include "../../sdlpp/sdlpp.hpp" #include "../../sdlpp/sdlpp.hpp"
#include "../global_vars.hpp" #include "../global_vars.hpp"
#include "../objectids.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 { class Button : public SDLPP::TextRenderer {
public: public:
Button() = delete; Button() = delete;
Button(double x, double y, double w, double h, Button(double x, double y, double w, double h,
std::shared_ptr<SDLPP::Renderer> &r, const std::string &text, std::shared_ptr<SDLPP::Renderer> &r, const std::string &text,
const std::string &color, const std::string &outline_color, const ButtonColors &colors,
const std::string &background_color,
std::function<void(void *)> click_fun, void *input) std::function<void(void *)> click_fun, void *input)
: TextRenderer(x, y, w, h, r, g_text_config->getFont(), text, color, : TextRenderer(x, y, w, h, r, g_text_config->getFont(), text, colors.font_color,
outline_color, 0.2), colors.font_outline_color, TEXT_OUTLINE),
click_fun(click_fun), func_input(input) { click_fun(std::move(click_fun)), func_input(input),
setColor(background_color); colors(colors) {
setColor(colors.bg_color);
setId(BUTTON_ID); setId(BUTTON_ID);
addCollision(SDLPP::RectColider(0, 0, 1, 1)); 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) { 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 { 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: 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; std::function<void(void *)> click_fun;
void *func_input; 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, createButton(double x, double y, double w, double h,
std::shared_ptr<SDLPP::Renderer> &r, const std::string &text, std::shared_ptr<SDLPP::Renderer> &r, const std::string &text,
const std::string &color, const std::string &outline_color, const std::string &color, const std::string &outline_color,
const std::string &background_color, const std::string &background_color,
std::function<void(void *)> click_fun, void *input); std::function<void(void *)> click_fun, void *input);*/
#endif #endif

View File

@ -148,12 +148,13 @@ void TextRenderer::saveFontConfig( std::shared_ptr< Font > font,
const std::string &color, const std::string &color,
const std::string &outline_color, const std::string &outline_color,
double outline_size ) { double outline_size ) {
_config = std::make_shared< FontConfiguration >( font, color, outline_color, auto config = std::make_shared< FontConfiguration >( font, color, outline_color,
outline_size ); outline_size );
text_change = true; saveFontConfig(config);
} }
void TextRenderer::saveFontConfig( void TextRenderer::saveFontConfig(
std::shared_ptr< FontConfiguration > config ) { std::shared_ptr< FontConfiguration > config ) {
_config = config; _config = std::move(config);
text_change = true;
} }
} // namespace SDLPP } // namespace SDLPP