Compare commits

..

No commits in common. "c59851b49063873fe47fbd2fbdbfa6de6a8586c5" and "194415500d24a77fdb58af0158512a6c7d7ba59b" have entirely different histories.

2 changed files with 84 additions and 122 deletions

View File

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

View File

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