2022-06-26 13:13:06 +00:00
|
|
|
#include "editor_scenes.hpp"
|
|
|
|
#include "../../sdlpp/sdlpp.hpp"
|
|
|
|
#include "../../sdlpp/sdlpp_mouse.hpp"
|
|
|
|
#include "../global_vars.hpp"
|
|
|
|
#include "../objectids.hpp"
|
|
|
|
#include "../editor_visitor.hpp"
|
|
|
|
|
|
|
|
bool __update_scenes_main_menu = false;
|
|
|
|
bool __quit_scenes_main_menu = false;
|
|
|
|
bool __started_main_menu = false;
|
2022-09-24 18:33:15 +00:00
|
|
|
bool *__quit_flag = nullptr;
|
2022-06-26 13:13:06 +00:00
|
|
|
uint64_t __cur_button_index_main_menu = -1;
|
|
|
|
uint64_t __cur_button_index_main_menu_down = -1;
|
|
|
|
std::vector<std::shared_ptr<Button>> __buttons_main_menu{};
|
|
|
|
std::shared_ptr<SDLPP::RectangleRender> __mouse_main_menu{};
|
|
|
|
|
|
|
|
void quitMainMenu() {
|
2022-09-24 18:33:15 +00:00
|
|
|
game_scenes.pop_back();
|
|
|
|
if (__quit_flag != nullptr) {
|
|
|
|
*__quit_flag = true;
|
|
|
|
} else {
|
|
|
|
g_quit = true;
|
|
|
|
}
|
2022-06-26 13:13:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void resumeMainMenu() {
|
|
|
|
__quit_scenes_main_menu = true;
|
|
|
|
}
|
|
|
|
|
2022-07-21 18:17:24 +00:00
|
|
|
void quitMainMenuCallback(void * /*UNUSED*/, Button * /*UNUSED*/) {
|
2022-06-26 13:13:06 +00:00
|
|
|
quitMainMenu();
|
|
|
|
}
|
|
|
|
|
2022-07-21 18:17:24 +00:00
|
|
|
void resumeMainMenuCallback(void * /*UNUSED*/, Button * /*UNUSED*/) {
|
2022-06-26 13:13:06 +00:00
|
|
|
resumeMainMenu();
|
|
|
|
}
|
|
|
|
|
2022-07-24 18:22:45 +00:00
|
|
|
void loadFinished(const std::string level_name) {
|
|
|
|
__quit_scenes_main_menu = true;
|
|
|
|
loadMapDialogCallback(level_name);
|
|
|
|
}
|
|
|
|
|
2022-09-24 18:33:15 +00:00
|
|
|
void showLoadMenu(void * /*UNUSED*/, Button * /*UNUSED*/) {
|
2022-07-24 18:22:45 +00:00
|
|
|
// TODO levels
|
2022-09-24 18:33:15 +00:00
|
|
|
auto loadMenu = createLoadScene(__buttons_main_menu.back()->getRenderer(),
|
|
|
|
"levels", loadFinished);
|
2022-07-24 18:22:45 +00:00
|
|
|
game_scenes.push_back(loadMenu);
|
|
|
|
}
|
|
|
|
|
2022-07-22 11:26:23 +00:00
|
|
|
void __updateSelectedButton_MainMenu(uint64_t new_index) {
|
|
|
|
if (new_index != __cur_button_index_main_menu &&
|
|
|
|
new_index != (uint64_t)-1) {
|
|
|
|
__buttons_main_menu[new_index]->setHighlight();
|
|
|
|
|
|
|
|
if (__cur_button_index_main_menu != (uint64_t)-1) {
|
2022-09-24 18:33:15 +00:00
|
|
|
__buttons_main_menu[__cur_button_index_main_menu]->unsetHighlight();
|
2022-07-22 11:26:23 +00:00
|
|
|
}
|
|
|
|
__cur_button_index_main_menu = new_index;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-26 13:13:06 +00:00
|
|
|
void resetGlobals() {
|
|
|
|
__update_scenes_main_menu = false;
|
|
|
|
__quit_scenes_main_menu = false;
|
|
|
|
__cur_button_index_main_menu_down = -1;
|
|
|
|
__mouse_main_menu->setPos(0, 0);
|
|
|
|
}
|
|
|
|
|
2022-07-21 18:17:24 +00:00
|
|
|
void handleKeyUpMainMenu(SDL_Keycode key, SDLPP::Scene & /*UNUSED*/) {
|
2022-06-26 13:13:06 +00:00
|
|
|
switch (key) {
|
|
|
|
case SDLK_ESCAPE:
|
|
|
|
resumeMainMenu();
|
|
|
|
break;
|
2022-07-22 11:26:23 +00:00
|
|
|
case SDLK_DOWN:
|
|
|
|
case SDLK_s:
|
2022-09-24 18:33:15 +00:00
|
|
|
if (__cur_button_index_main_menu == __buttons_main_menu.size() - 1) {
|
2022-07-22 11:26:23 +00:00
|
|
|
__updateSelectedButton_MainMenu(0);
|
|
|
|
} else {
|
|
|
|
__updateSelectedButton_MainMenu(__cur_button_index_main_menu + 1);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case SDLK_UP:
|
|
|
|
case SDLK_w:
|
2022-09-24 18:33:15 +00:00
|
|
|
if (__cur_button_index_main_menu == 0) {
|
2022-07-22 11:26:23 +00:00
|
|
|
__updateSelectedButton_MainMenu(__buttons_main_menu.size() - 1);
|
|
|
|
} else {
|
|
|
|
__updateSelectedButton_MainMenu(__cur_button_index_main_menu - 1);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case SDLK_RETURN:
|
2022-09-24 18:33:15 +00:00
|
|
|
if (__cur_button_index_main_menu >= 0 &&
|
|
|
|
__cur_button_index_main_menu < __buttons_main_menu.size()) {
|
2022-07-22 11:26:23 +00:00
|
|
|
__buttons_main_menu[__cur_button_index_main_menu]
|
|
|
|
->performFunction();
|
|
|
|
}
|
|
|
|
break;
|
2022-06-26 13:13:06 +00:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-21 18:17:24 +00:00
|
|
|
std::shared_ptr<SDLPP::Scene>
|
|
|
|
createSceneMainMenu(std::shared_ptr<SDLPP::Renderer> &renderer) {
|
2022-06-26 13:13:06 +00:00
|
|
|
auto scene = std::make_shared<SDLPP::Scene>(renderer);
|
2022-07-21 18:17:24 +00:00
|
|
|
auto bg = std::make_shared<SDLPP::RectangleRender>(0, 0, 10, 10, renderer,
|
|
|
|
"#00000088", true);
|
2022-06-26 13:13:06 +00:00
|
|
|
bg->setPermanent();
|
|
|
|
bg->setId(1);
|
|
|
|
scene->addObject(bg);
|
|
|
|
__mouse_main_menu =
|
|
|
|
std::make_shared<SDLPP::RectangleRender>(0.01, 0.01, 0, 0, renderer);
|
|
|
|
__mouse_main_menu->setMinWidth(1);
|
|
|
|
__mouse_main_menu->setMinHeight(1);
|
|
|
|
__mouse_main_menu->setAlignment(SDLPP::OBJ_CENTER, SDLPP::OBJ_CENTER);
|
|
|
|
__mouse_main_menu->setId(EDITOR_MOUSE_ID);
|
|
|
|
__mouse_main_menu->setColiderColor("#00FF00");
|
|
|
|
__mouse_main_menu->addCollision(SDLPP::RectColider({ 0, 0 }, { 1, 1 }));
|
|
|
|
scene->addObject(__mouse_main_menu);
|
|
|
|
|
|
|
|
ButtonConfig default_button_theme{};
|
|
|
|
default_button_theme.bg_color = "#FFFFFF88";
|
|
|
|
default_button_theme.bg_color_highlight = "#FFFFFFBB";
|
|
|
|
default_button_theme.bg_color_disabled = "#AAAAAA88";
|
|
|
|
default_button_theme.font_color = "#000000";
|
|
|
|
default_button_theme.font_color_highlight = "#000000";
|
|
|
|
default_button_theme.font_color_disabled = "#555555";
|
|
|
|
default_button_theme.font_outline_color = "#FFFFFF88";
|
|
|
|
default_button_theme.font_outline_color_highlight = "#FFFFFFAA";
|
|
|
|
default_button_theme.font_outline_color_disabled = "#787878";
|
|
|
|
default_button_theme.outline = 0.1;
|
|
|
|
// buttons
|
|
|
|
__buttons_main_menu.emplace_back(std::make_shared<Button>(
|
2022-07-21 18:17:24 +00:00
|
|
|
0.2, 0.25, 0.6, 0.1, renderer, "SAVE", default_button_theme,
|
|
|
|
saveMapCallback, nullptr));
|
|
|
|
__buttons_main_menu.back()->setAlignment(SDLPP::OBJ_CENTER,
|
|
|
|
SDLPP::OBJ_CENTER);
|
2022-07-21 17:26:50 +00:00
|
|
|
__buttons_main_menu.back()->setPermanent();
|
|
|
|
__buttons_main_menu.back()->setButtonIndex(__buttons_main_menu.size() - 1);
|
2022-09-24 18:33:15 +00:00
|
|
|
__buttons_main_menu.emplace_back(
|
|
|
|
std::make_shared<Button>(0.2, 0.4, 0.6, 0.1, renderer, "LOAD",
|
|
|
|
default_button_theme, showLoadMenu, nullptr));
|
2022-07-21 18:17:24 +00:00
|
|
|
__buttons_main_menu.back()->setAlignment(SDLPP::OBJ_CENTER,
|
|
|
|
SDLPP::OBJ_CENTER);
|
2022-07-21 17:26:50 +00:00
|
|
|
__buttons_main_menu.back()->setPermanent();
|
|
|
|
__buttons_main_menu.back()->setButtonIndex(__buttons_main_menu.size() - 1);
|
|
|
|
__buttons_main_menu.emplace_back(std::make_shared<Button>(
|
2022-07-21 18:17:24 +00:00
|
|
|
0.2, 0.55, 0.6, 0.1, renderer, "RESUME", default_button_theme,
|
|
|
|
resumeMainMenuCallback, nullptr));
|
|
|
|
__buttons_main_menu.back()->setAlignment(SDLPP::OBJ_CENTER,
|
|
|
|
SDLPP::OBJ_CENTER);
|
2022-06-26 13:13:06 +00:00
|
|
|
__buttons_main_menu.back()->setPermanent();
|
|
|
|
__buttons_main_menu.back()->setButtonIndex(__buttons_main_menu.size() - 1);
|
|
|
|
__buttons_main_menu.emplace_back(std::make_shared<Button>(
|
2022-07-21 18:17:24 +00:00
|
|
|
0.2, 0.7, 0.6, 0.1, renderer, "QUIT", default_button_theme,
|
|
|
|
quitMainMenuCallback, nullptr));
|
|
|
|
__buttons_main_menu.back()->setAlignment(SDLPP::OBJ_CENTER,
|
|
|
|
SDLPP::OBJ_CENTER);
|
2022-06-26 13:13:06 +00:00
|
|
|
__buttons_main_menu.back()->setPermanent();
|
|
|
|
__buttons_main_menu.back()->setButtonIndex(__buttons_main_menu.size() - 1);
|
2022-07-21 18:17:24 +00:00
|
|
|
for (auto &button : __buttons_main_menu) {
|
2022-06-26 13:13:06 +00:00
|
|
|
scene->addObject(button);
|
|
|
|
}
|
|
|
|
return scene;
|
|
|
|
}
|
|
|
|
|
2022-07-21 18:17:24 +00:00
|
|
|
void additionalRenderMainMenu(std::shared_ptr<SDLPP::Scene> & /*UNUSED*/) {
|
2022-06-26 13:13:06 +00:00
|
|
|
if (__update_scenes_main_menu) {
|
2022-07-21 18:17:24 +00:00
|
|
|
for (auto &_scene : game_scenes) {
|
2022-06-26 13:13:06 +00:00
|
|
|
_scene.scene->updateSizeAndPosition();
|
|
|
|
}
|
|
|
|
if (__started_main_menu) {
|
|
|
|
__update_scenes_main_menu = false;
|
|
|
|
} else {
|
|
|
|
__started_main_menu = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (__quit_scenes_main_menu) {
|
|
|
|
game_scenes.pop_back();
|
|
|
|
resetGlobals();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void getMousePositionFlagsMainMenu(SDLPP::Scene &scene) {
|
|
|
|
auto mouse = scene.getObjects({ EDITOR_MOUSE_ID })[0];
|
|
|
|
// move mouse colider to mouse position
|
|
|
|
mouse->setPos(SDLPP::Mouse::getMousePositionDouble(
|
|
|
|
scene.getRenderer(), SDLPP::OBJ_CENTER, SDLPP::OBJ_CENTER));
|
|
|
|
|
|
|
|
MouseVisitor visitor;
|
|
|
|
scene.visitCollisions(*mouse, visitor);
|
2022-07-22 11:26:23 +00:00
|
|
|
__updateSelectedButton_MainMenu(visitor.getCurButton());
|
2022-06-26 13:13:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void pollEventsMainMenu(std::shared_ptr<SDLPP::Scene> &scene) {
|
|
|
|
SDL_Event event;
|
|
|
|
while (SDLPP::getSDLEvent(event)) {
|
|
|
|
switch (event.type) {
|
|
|
|
case SDL_QUIT:
|
|
|
|
quitMainMenu();
|
|
|
|
break;
|
|
|
|
case SDL_KEYUP:
|
|
|
|
handleKeyUpMainMenu(event.key.keysym.sym, *scene);
|
|
|
|
break;
|
|
|
|
case SDL_WINDOWEVENT:
|
|
|
|
if (event.window.event == SDL_WINDOWEVENT_RESIZED) {
|
|
|
|
__update_scenes_main_menu = true;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case SDL_MOUSEMOTION:
|
|
|
|
getMousePositionFlagsMainMenu(*scene);
|
|
|
|
break;
|
|
|
|
case SDL_MOUSEBUTTONUP:
|
2022-07-21 18:17:24 +00:00
|
|
|
if (__cur_button_index_main_menu_down ==
|
|
|
|
__cur_button_index_main_menu &&
|
2022-06-26 13:13:06 +00:00
|
|
|
__cur_button_index_main_menu != (uint64_t)-1) {
|
2022-07-21 18:17:24 +00:00
|
|
|
__buttons_main_menu[__cur_button_index_main_menu]
|
|
|
|
->performFunction();
|
2022-06-26 13:13:06 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case SDL_MOUSEBUTTONDOWN:
|
|
|
|
// store current mouse flags in previous mouse flags
|
|
|
|
__cur_button_index_main_menu_down = __cur_button_index_main_menu;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-21 18:17:24 +00:00
|
|
|
SceneStruct
|
2022-09-24 18:33:15 +00:00
|
|
|
createEditorMainMenuScene(std::shared_ptr<SDLPP::Renderer> &renderer,
|
|
|
|
bool *quit_flag) {
|
2022-06-26 13:13:06 +00:00
|
|
|
SceneStruct ret{};
|
|
|
|
ret.scene = createSceneMainMenu(renderer);
|
|
|
|
ret.additionalRender = additionalRenderMainMenu;
|
|
|
|
ret.doInput = pollEventsMainMenu;
|
|
|
|
__update_scenes_main_menu = true;
|
2022-07-22 11:26:23 +00:00
|
|
|
__updateSelectedButton_MainMenu(0);
|
2022-09-24 18:33:15 +00:00
|
|
|
__quit_flag = quit_flag;
|
2022-06-26 13:13:06 +00:00
|
|
|
return ret;
|
|
|
|
}
|