diff --git a/CIO/CSelectBox.cpp b/CIO/CSelectBox.cpp index 8265285..04da9b4 100644 --- a/CIO/CSelectBox.cpp +++ b/CIO/CSelectBox.cpp @@ -203,6 +203,24 @@ void CSelectBox::setMouseData(SDL_MouseButtonEvent button) needRender = true; } +void CSelectBox::setSize(Extent size) +{ + if(size_ != size) + { + size_ = size; + Surf_SelectBox.reset(); + needRender = true; + // update scroll down button position + ScrollDownButton->setY(size_.y - 1 - 20); + } +} + +void CSelectBox::setPos(Position pos) +{ + pos_ = pos; + needRender = true; +} + bool CSelectBox::render() { // position in the Surface 'Surf_SelectBox' diff --git a/CIO/CSelectBox.h b/CIO/CSelectBox.h index fc56640..8a62d13 100644 --- a/CIO/CSelectBox.h +++ b/CIO/CSelectBox.h @@ -55,4 +55,6 @@ class CSelectBox needRender = true; } void addOption(const std::string& string, std::function callback = nullptr, int param = 0); + void setSize(Extent size); + void setPos(Position pos); }; diff --git a/CIO/CWindow.cpp b/CIO/CWindow.cpp index b3133af..66146a5 100644 --- a/CIO/CWindow.cpp +++ b/CIO/CWindow.cpp @@ -142,6 +142,9 @@ void CWindow::setMouseData(SDL_MouseMotionEvent motion) // the window has resized, so we need a new surface surface.reset(); + + // notify the callback that the window has been resized + callback_(WINDOW_RESIZED_CALL); } } } diff --git a/callbacks.cpp b/callbacks.cpp index 68f41de..7c69194 100644 --- a/callbacks.cpp +++ b/callbacks.cpp @@ -18,8 +18,10 @@ #include "CSurface.h" #include "globals.h" #include "helpers/format.hpp" +#include "s25util/strAlgos.h" #include #include +#include namespace bfs = boost::filesystem; @@ -715,9 +717,9 @@ void callback::EditorMainMenu(int Param) case INITIALIZING_CALL: if(WNDMain) break; - WNDMain = global::s2->RegisterWindow(std::make_unique(EditorMainMenu, WINDOWQUIT, - WindowPos::Center, Extent(220, 320), - "Main menu", WINDOW_GREEN1, WINDOW_CLOSE)); + WNDMain = global::s2->RegisterWindow( + std::make_unique(EditorMainMenu, WINDOWQUIT, WindowPos::Center, Extent(220, 320), "Main menu", + WINDOW_GREEN1, WINDOW_CLOSE | WINDOW_MOVE)); WNDMain->addButton(EditorMainMenu, LOADMENU, Position(8, 100), Extent(190, 20), BUTTON_GREEN2, "Load map"); WNDMain->addButton(EditorMainMenu, SAVEMENU, Position(8, 125), Extent(190, 20), BUTTON_GREEN2, "Save map"); @@ -747,6 +749,9 @@ void callback::EditorMainMenu(int Param) void callback::EditorLoadMenu(int Param) { static CWindow* WNDLoad = nullptr; + static CSelectBox* CB_Filename = nullptr; + static CButton* BtnLoad = nullptr; + static CButton* BtnAbort = nullptr; static std::string curFilename; enum @@ -761,20 +766,28 @@ void callback::EditorLoadMenu(int Param) { if(WNDLoad) break; - WNDLoad = global::s2->RegisterWindow(std::make_unique( - EditorLoadMenu, WINDOWQUIT, WindowPos::Center, Extent(280, 320), "Load", WINDOW_GREEN1, WINDOW_CLOSE)); - auto* CB_Filename = WNDLoad->addSelectBox(Position(10, 5), Extent(160, 280), FontSize::Medium); + WNDLoad = global::s2->RegisterWindow( + std::make_unique(EditorLoadMenu, WINDOWQUIT, WindowPos::Center, Extent(280, 320), "Load", + WINDOW_GREEN1, WINDOW_CLOSE | WINDOW_MOVE | WINDOW_RESIZE)); + CB_Filename = WNDLoad->addSelectBox(Position(10, 5), Extent(160, 280), FontSize::Medium); curFilename.clear(); for(const auto& itFile : bfs::directory_iterator(global::userMapsPath)) { if(is_regular_file(itFile.status())) { + // filter to supported map file extensions + const std::string ext = s25util::toLower(itFile.path().extension().string()); + if(ext != ".swd" && ext != ".wld") + continue; + const std::string filename = itFile.path().filename().string(); CB_Filename->addOption(filename, [filename](int) { curFilename = filename; }); } } - WNDLoad->addButton(EditorLoadMenu, LOADMAP, Position(175, 140), Extent(90, 20), BUTTON_GREY, "Load"); - WNDLoad->addButton(EditorLoadMenu, WINDOWQUIT, Position(175, 165), Extent(90, 20), BUTTON_RED1, "Abort"); + BtnLoad = + WNDLoad->addButton(EditorLoadMenu, LOADMAP, Position(175, 140), Extent(90, 20), BUTTON_GREY, "Load"); + BtnAbort = + WNDLoad->addButton(EditorLoadMenu, WINDOWQUIT, Position(175, 165), Extent(90, 20), BUTTON_RED1, "Abort"); break; } case WINDOWQUIT: @@ -783,9 +796,61 @@ void callback::EditorLoadMenu(int Param) { WNDLoad->setWaste(); WNDLoad = nullptr; + CB_Filename = nullptr; + BtnLoad = nullptr; + BtnAbort = nullptr; } break; + case WINDOW_RESIZED_CALL: + { + if(!WNDLoad || !CB_Filename || !BtnLoad || !BtnAbort) + break; + + int borderL = global::bmpArray[WINDOW_LEFT_FRAME].w; + int borderR = global::bmpArray[WINDOW_RIGHT_FRAME].w; + int borderT = global::bmpArray[WINDOW_UPPER_FRAME].h; + int borderB = global::bmpArray[WINDOW_LOWER_FRAME].h; + + int window_w = WNDLoad->getW(); + int window_h = WNDLoad->getH(); + int client_w = window_w - borderL - borderR; + int client_h = window_h - borderT - borderB; + + // minimum size + if(client_w < 200) + client_w = 200; + if(client_h < 150) + client_h = 150; + + // selectbox: fills most of the client area + const int btn_w = 90; + const int margin = 10; + const int gap = 5; + + int sb_x = borderL + margin; + int sb_y = borderT + gap; + int sb_w = client_w - margin - gap - btn_w - margin; // left margin + gap to buttons + button + right margin + int sb_h = client_h - gap - gap; // top gap + bottom gap + if(sb_w < 50) + sb_w = 50; + if(sb_h < 50) + sb_h = 50; + + CB_Filename->setPos(Position(sb_x, sb_y)); + CB_Filename->setSize(Extent(sb_w, sb_h)); + + // buttons: same Y as original (140/165 + borderT), + // X just right of the selectbox with a gap + int btn_x = sb_x + sb_w + gap; + + BtnLoad->setX(btn_x); + BtnAbort->setX(btn_x); + + WNDLoad->setDirty(); + break; + } + case LOADMAP: { if(curFilename.empty()) @@ -853,9 +918,9 @@ void callback::EditorSaveMenu(int Param) if(WNDSave) break; { - WNDSave = global::s2->RegisterWindow(std::make_unique(EditorSaveMenu, WINDOWQUIT, - WindowPos::Center, Extent(280, 200), - "Save", WINDOW_GREEN1, WINDOW_CLOSE)); + WNDSave = global::s2->RegisterWindow( + std::make_unique(EditorSaveMenu, WINDOWQUIT, WindowPos::Center, Extent(280, 200), "Save", + WINDOW_GREEN1, WINDOW_CLOSE | WINDOW_MOVE)); MapObj = global::s2->getMapObj(); WNDSave->addText("Filename", Position(100, 2), FontSize::Small); @@ -863,10 +928,10 @@ void callback::EditorSaveMenu(int Param) const bfs::path filePath = MapObj->getFilepath().empty() ? "MyMap" : MapObj->getFilepath(); TXTF_Filename->setText(filePath.filename().string()); WNDSave->addText("Mapname", Position(98, 38), FontSize::Small); - TXTF_Mapname = WNDSave->addTextfield(Position(10, 50), 19, 1); + TXTF_Mapname = WNDSave->addTextfield(Position(10, 50), 21, 1); TXTF_Mapname->setText(MapObj->getMapname()); WNDSave->addText("Author", Position(110, 75), FontSize::Medium); - TXTF_Author = WNDSave->addTextfield(Position(10, 87), 19, 1); + TXTF_Author = WNDSave->addTextfield(Position(10, 87), 21, 1); TXTF_Author->setText(MapObj->getAuthor()); WNDSave->addButton(EditorSaveMenu, SAVEMAP, Position(170, 120), Extent(90, 20), BUTTON_GREY, "Save"); WNDSave->addButton(EditorSaveMenu, WINDOWQUIT, Position(170, 145), Extent(90, 20), BUTTON_RED1, diff --git a/include/defines.h b/include/defines.h index 9b7b039..5adf11c 100644 --- a/include/defines.h +++ b/include/defines.h @@ -44,7 +44,9 @@ enum // this will happen every time the user clicks anywhere on the window WINDOW_CLICKED_CALL = -5, // this window quit message is ONLY usable to call a callback function explicit with this value - WINDOW_QUIT_MESSAGE = -6 + WINDOW_QUIT_MESSAGE = -6, + // this will happen every time the window is resized + WINDOW_RESIZED_CALL = -7 }; // BOBTYPES