Compare commits

...

2 Commits

Author SHA1 Message Date
c59851b490 Ensure arrows are enabled/disabled even when moving tools with wsad
Some checks failed
continuous-integration/drone/push Build is failing
2022-06-24 21:51:47 +02:00
38dfa533c4 Button update function 2022-06-24 21:50:30 +02:00
2 changed files with 122 additions and 84 deletions

View File

@ -76,6 +76,12 @@ struct ToolInfo {
int max_page_mods;
int cur_page_characters;
int max_page_characters;
std::shared_ptr<Button> button_left_tools;
std::shared_ptr<Button> button_right_tools;
std::shared_ptr<Button> button_left_mods;
std::shared_ptr<Button> button_right_mods;
std::shared_ptr<Button> button_left_characters;
std::shared_ptr<Button> button_right_characters;
};
struct GlobalVars {
@ -105,7 +111,6 @@ struct GlobalVars {
GlobalVars global_vars;
std::mutex destruction_mutex;
std::mutex render_mutex;
void updateTool() {
auto tool_index = global_vars.tool.index;
@ -204,9 +209,39 @@ void unsetToolColor() {
setToolColor("#FFFFFF00");
}
void toolMoveUpdateButtons(Button *left, Button *right, int &cur_page, int &max_page, bool canAdd) {
if(cur_page == 0) {
left->disable();
} else {
left->enable();
}
if(cur_page != max_page) {
if(canAdd) {
// TOOD global button config
right->changeText(">");
right->setFontColor("#000000");
right->setFontOutlineColor("#FFFFFF88");
right->setFontColorHighlight("#000000");
right->setFontOutlineColorHighlight("#FFFFFFAA");
} else {
right->enable();
}
} else {
if(canAdd) {
right->changeText("+");
right->setFontColor("#00FF00");
right->setFontOutlineColor("#000000");
right->setFontColorHighlight("#88FF88");
right->setFontOutlineColorHighlight("#444444");
} else {
right->disable();
}
}
}
void updateToolSelection(int prev_index, ToolType::Value type) {
unsetToolColor();
size_t cur_page = 0;
int cur_page = 0;
size_t multiplier = 0;
std::vector<std::shared_ptr<MarioBlock>> *tool_vec = nullptr;
switch (type) {
@ -214,16 +249,19 @@ void updateToolSelection(int prev_index, ToolType::Value type) {
cur_page = global_vars.tool.cur_page_tools;
multiplier = 2 * TOOLS_WIDTH;
tool_vec = &global_vars.tools;
toolMoveUpdateButtons(global_vars.tool.button_left_tools.get(), global_vars.tool.button_right_tools.get(), cur_page, global_vars.tool.max_page_tools, false);
break;
case ToolType::MOD:
cur_page = global_vars.tool.cur_page_mods;
multiplier = 2 * MOD_WIDTH;
tool_vec = &global_vars.mods;
toolMoveUpdateButtons(global_vars.tool.button_left_mods.get(), global_vars.tool.button_right_mods.get(), cur_page, global_vars.tool.max_page_mods, false);
break;
case ToolType::CHARACTER:
cur_page = global_vars.tool.cur_page_characters;
multiplier = 2 * CHARACTER_WIDTH;
tool_vec = &global_vars.characters;
toolMoveUpdateButtons(global_vars.tool.button_left_characters.get(), global_vars.tool.button_right_characters.get(), cur_page, global_vars.tool.max_page_characters, false);
default:
break;
}
@ -498,10 +536,10 @@ void getMousePositionFlags(SDLPP::Scene &scene) {
scene.visitCollisions(*mouse, visitor);
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);
global_vars.buttons[global_vars.mouse.cur_button_index]->unsetHighlight();
}
if(visitor.getCurButton() != (uint64_t)-1) {
global_vars.buttons[visitor.getCurButton()]->setHighlight(render_mutex);
global_vars.buttons[visitor.getCurButton()]->setHighlight();
}
}
global_vars.mouse.cur_button_index = visitor.getCurButton();
@ -526,49 +564,13 @@ struct moveStruct {
std::shared_ptr<Button> other_button;
};
void toolMoveLeftUpdateButtons(Button *left, Button *right, int &cur_page, int &max_page, bool canAdd) {
if(cur_page == 0) {
left->disable(render_mutex);
}
if(cur_page != max_page) {
if(canAdd) {
// TOOD global button config
right->changeText(">");
right->setFontColor("#000000", render_mutex);
right->setFontOutlineColor("#FFFFFF88", render_mutex);
right->setFontColorHighlight("#000000", render_mutex);
right->setFontOutlineColorHighlight("#FFFFFFAA", render_mutex);
} else {
right->enable(render_mutex);
}
}
}
void toolMoveLeft(Button *left, Button *right, int &cur_page, int &max_page, bool canAdd) {
cur_page--;
toolMoveLeftUpdateButtons(left, right, cur_page, max_page, canAdd);
toolMoveUpdateButtons(left, right, cur_page, max_page, canAdd);
}
void toolMoveRightUpdateButtons(Button *left, Button *right, int &cur_page, int &max_page, bool canAdd) {
if(cur_page == max_page) {
if(canAdd) {
right->changeText("+");
right->setFontColor("#00FF00", render_mutex);
right->setFontOutlineColor("#000000", render_mutex);
right->setFontColorHighlight("#88FF88", render_mutex);
right->setFontOutlineColorHighlight("#444444", render_mutex);
} else {
right->disable(render_mutex);
}
}
if(cur_page != 0) {
left->enable(render_mutex);
}
}
void toolMoveRight(Button *left, Button *right, int &cur_page, int &max_page, bool canAdd) {
cur_page += 1;
toolMoveRightUpdateButtons(left, right, cur_page, max_page, canAdd);
toolMoveUpdateButtons(left, right, cur_page, max_page, canAdd);
}
void moveMapLeft(void *input, Button *caller) {
@ -1028,7 +1030,7 @@ void populateWorldType(
void testButtonFunc(void *input, Button *caller) {
auto actual = static_cast<int*>(input);
std::cout << "NUM: " << *actual << std::endl;
caller->disable(render_mutex);
caller->disable();
}
#ifdef _WIN32
@ -1127,8 +1129,7 @@ int main() {
global_vars.buttons.back()->setButtonIndex(global_vars.buttons.size() - 1);
left_map_input.other_button = global_vars.buttons.back();
// ensure disabled/enabled properly
toolMoveRightUpdateButtons(right_map_input.other_button.get(), global_vars.buttons.back().get(), global_vars.map.cur_page, global_vars.map.max_page, true);
toolMoveLeftUpdateButtons(right_map_input.other_button.get(), global_vars.buttons.back().get(), global_vars.map.cur_page, global_vars.map.max_page, true);
toolMoveUpdateButtons(right_map_input.other_button.get(), global_vars.buttons.back().get(), global_vars.map.cur_page, global_vars.map.max_page, true);
createGrid(BLOCK_SIZE, 1 - MAP_HEIGHT * BLOCK_SIZE, MAP_WIDTH, MAP_HEIGHT,
scene);
// edit blocks on map
@ -1169,6 +1170,7 @@ int main() {
global_vars.buttons.back()->setAlignment(SDLPP::OBJ_CENTER, SDLPP::OBJ_CENTER);
global_vars.buttons.back()->setPermanent();
global_vars.buttons.back()->setButtonIndex(global_vars.buttons.size() - 1);
global_vars.tool.button_left_tools = global_vars.buttons.back();
// right tool arrow
moveStruct right_tool_input{
.scene = scene,
@ -1181,9 +1183,9 @@ int main() {
global_vars.buttons.back()->setPermanent();
global_vars.buttons.back()->setButtonIndex(global_vars.buttons.size() - 1);
left_tool_input.other_button = global_vars.buttons.back();
global_vars.tool.button_right_tools = global_vars.buttons.back();
// ensure disabled/enabled properly
toolMoveRightUpdateButtons(right_tool_input.other_button.get(), global_vars.buttons.back().get(), global_vars.tool.cur_page_tools, global_vars.tool.max_page_tools, false);
toolMoveLeftUpdateButtons(right_tool_input.other_button.get(), global_vars.buttons.back().get(), global_vars.tool.cur_page_tools, global_vars.tool.max_page_tools, false);
toolMoveUpdateButtons(right_tool_input.other_button.get(), global_vars.buttons.back().get(), global_vars.tool.cur_page_tools, global_vars.tool.max_page_tools, false);
createGrid((MAP_WIDTH - TOOLS_WIDTH) * BLOCK_SIZE,
1 - (MAP_HEIGHT + 3) * BLOCK_SIZE, TOOLS_WIDTH, 2, scene);
// mods
@ -1208,6 +1210,7 @@ int main() {
global_vars.buttons.back()->setAlignment(SDLPP::OBJ_CENTER, SDLPP::OBJ_CENTER);
global_vars.buttons.back()->setPermanent();
global_vars.buttons.back()->setButtonIndex(global_vars.buttons.size() - 1);
global_vars.tool.button_left_mods = global_vars.buttons.back();
// right mod arrow
moveStruct right_mod_input{
.scene = scene,
@ -1220,9 +1223,9 @@ int main() {
global_vars.buttons.back()->setPermanent();
global_vars.buttons.back()->setButtonIndex(global_vars.buttons.size() - 1);
left_mod_input.other_button = global_vars.buttons.back();
global_vars.tool.button_right_mods = global_vars.buttons.back();
// ensure disabled/enabled properly
toolMoveRightUpdateButtons(right_mod_input.other_button.get(), global_vars.buttons.back().get(), global_vars.tool.cur_page_mods, global_vars.tool.max_page_mods, false);
toolMoveLeftUpdateButtons(right_mod_input.other_button.get(), global_vars.buttons.back().get(), global_vars.tool.cur_page_mods, global_vars.tool.max_page_mods, false);
toolMoveUpdateButtons(right_mod_input.other_button.get(), global_vars.buttons.back().get(), global_vars.tool.cur_page_mods, global_vars.tool.max_page_mods, false);
createGrid(5 * BLOCK_SIZE, 1 - (MAP_HEIGHT + 3) * BLOCK_SIZE, MOD_WIDTH, 2,
scene);
// characters
@ -1249,6 +1252,7 @@ int main() {
global_vars.buttons.back()->setAlignment(SDLPP::OBJ_CENTER, SDLPP::OBJ_CENTER);
global_vars.buttons.back()->setPermanent();
global_vars.buttons.back()->setButtonIndex(global_vars.buttons.size() - 1);
global_vars.tool.button_left_characters = global_vars.buttons.back();
// right character arrow
moveStruct right_character_input{
.scene = scene,
@ -1260,9 +1264,9 @@ int main() {
global_vars.buttons.back()->setAlignment(SDLPP::OBJ_CENTER, SDLPP::OBJ_CENTER);
global_vars.buttons.back()->setPermanent();
global_vars.buttons.back()->setButtonIndex(global_vars.buttons.size() - 1);
global_vars.tool.button_right_characters = global_vars.buttons.back();
// ensure disabled/enabled properly
toolMoveRightUpdateButtons(right_character_input.other_button.get(), global_vars.buttons.back().get(), global_vars.tool.cur_page_characters, global_vars.tool.max_page_characters, false);
toolMoveLeftUpdateButtons(right_character_input.other_button.get(), global_vars.buttons.back().get(), global_vars.tool.cur_page_characters, global_vars.tool.max_page_characters, false);
toolMoveUpdateButtons(right_character_input.other_button.get(), global_vars.buttons.back().get(), global_vars.tool.cur_page_characters, global_vars.tool.max_page_characters, false);
left_character_input.other_button = global_vars.buttons.back();
createGrid((MOD_WIDTH + 8) * BLOCK_SIZE, 1 - (MAP_HEIGHT + 3) * BLOCK_SIZE,
CHARACTER_WIDTH, 2, scene);
@ -1356,17 +1360,17 @@ int main() {
SDL_framerateDelay(&gFPS);
SDL_PumpEvents();
std::lock_guard<std::mutex> lock(destruction_mutex);
{
std::lock_guard<std::mutex> render(render_mutex);
scene->renderScene();
renderer->presentRenderer();
}
scene->renderScene();
renderer->presentRenderer();
frames++;
if (SDL_GetTicks() - base >= 1000) {
std::cout << "FPS: " << frames << std::endl;
frames = 0;
base = SDL_GetTicks();
}
for(auto &button : global_vars.buttons) {
button->update();
}
// TODO maybe button update func instead of using mutex?
/* checkArrowsEnabled(global_vars.map.cur_page, global_vars.map.max_page,
MAP_LEFT_ENABLED_FLAG, MAP_RIGHT_ENABLED_FLAG,

View File

@ -5,9 +5,14 @@
#include "../global_vars.hpp"
#include "../objectids.hpp"
#include <functional>
#include <mutex>
#include <utility>
enum ButtonState {
NORMAL,
HIGHLIGHTED,
DISABLED,
};
struct ButtonConfig {
std::string font_color;
std::string font_outline_color;
@ -31,45 +36,50 @@ public:
: 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) {
config(config), button_text(text) {
setColor(config.bg_color);
setId(BUTTON_ID);
addCollision(SDLPP::RectColider(0, 0, 1, 1));
state = NORMAL;
}
void setFontColor(const std::string &color, std::mutex &mutex) {
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) {
updateTextColor(config.font_color, config.font_outline_color, mutex);
should_update_color = true;
}
}
void setFontColorHighlight(const std::string &color, std::mutex &mutex) {
void setFontColorHighlight(const std::string &color) {
config.font_color_highlight = color;
if(highlighted && !disabled) {
updateTextColor(config.font_color_highlight, config.font_outline_color_highlight, mutex);
should_update_color = true;
}
}
void setFontColorDisabled(const std::string &color, std::mutex &mutex) {
void setFontColorDisabled(const std::string &color) {
config.font_color_disabled = color;
if(disabled) {
updateTextColor(config.font_color_disabled, config.font_outline_color_disabled, mutex);
should_update_color = true;
}
}
void setFontOutlineColor(const std::string &color, std::mutex &mutex) {
void setFontOutlineColor(const std::string &color) {
config.font_outline_color = color;
if(!highlighted && !disabled) {
updateTextColor(config.font_color, config.font_outline_color, mutex);
should_update_color = true;
}
}
void setFontOutlineColorHighlight(const std::string &color, std::mutex &mutex) {
void setFontOutlineColorHighlight(const std::string &color) {
config.font_outline_color_highlight = color;
if(highlighted && !disabled) {
updateTextColor(config.font_color_highlight, config.font_outline_color_highlight, mutex);
should_update_color = true;
}
}
void setFontOutlineColorDisabled(const std::string &color, std::mutex &mutex) {
void setFontOutlineColorDisabled(const std::string &color) {
config.font_outline_color_disabled = color;
if(disabled) {
updateTextColor(config.font_color_disabled, config.font_outline_color_disabled, mutex);
should_update_color = true;
}
}
void setBackgroundColor(const std::string &color) {
@ -101,47 +111,71 @@ public:
void setButtonIndex(uint64_t id) {
_id = id;
}
void setHighlight(std::mutex &mutex) {
void setHighlight() {
if(!disabled) {
setColor(config.bg_color_highlight);
updateTextColor(config.font_color_highlight, config.font_outline_color_highlight, mutex);
should_update_color = true;
state = HIGHLIGHTED;
}
highlighted = true;
}
void unsetHighlight(std::mutex &mutex) {
void unsetHighlight() {
if(!disabled) {
setColor(config.bg_color);
updateTextColor(config.font_color, config.font_outline_color, mutex);
should_update_color = true;
state = NORMAL;
}
highlighted = false;
}
void disable(std::mutex &mutex) {
void disable() {
setColor(config.bg_color_disabled);
updateTextColor(config.font_color_disabled, config.font_outline_color_disabled, mutex);
should_update_color = true;
state = DISABLED;
disabled = true;
}
void enable(std::mutex &mutex) {
void enable() {
if(!highlighted) {
setColor(config.bg_color);
updateTextColor(config.font_color, config.font_outline_color, mutex);
should_update_color = true;
state = NORMAL;
} else {
setColor(config.bg_color_highlight);
updateTextColor(config.font_color_highlight, config.font_outline_color_highlight, mutex);
should_update_color = true;
state = HIGHLIGHTED;
}
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, config.outline);
}
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>