Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions CIO/CSelectBox.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
2 changes: 2 additions & 0 deletions CIO/CSelectBox.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,4 +55,6 @@ class CSelectBox
needRender = true;
}
void addOption(const std::string& string, std::function<void(int)> callback = nullptr, int param = 0);
void setSize(Extent size);
void setPos(Position pos);
};
3 changes: 3 additions & 0 deletions CIO/CWindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
}
Expand Down
91 changes: 78 additions & 13 deletions callbacks.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,10 @@
#include "CSurface.h"
#include "globals.h"
#include "helpers/format.hpp"
#include "s25util/strAlgos.h"
#include <boost/filesystem.hpp>
#include <algorithm>
#include <cctype>

namespace bfs = boost::filesystem;

Expand Down Expand Up @@ -715,9 +717,9 @@ void callback::EditorMainMenu(int Param)
case INITIALIZING_CALL:
if(WNDMain)
break;
WNDMain = global::s2->RegisterWindow(std::make_unique<CWindow>(EditorMainMenu, WINDOWQUIT,
WindowPos::Center, Extent(220, 320),
"Main menu", WINDOW_GREEN1, WINDOW_CLOSE));
WNDMain = global::s2->RegisterWindow(
std::make_unique<CWindow>(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");

Expand Down Expand Up @@ -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
Expand All @@ -761,20 +766,28 @@ void callback::EditorLoadMenu(int Param)
{
if(WNDLoad)
break;
WNDLoad = global::s2->RegisterWindow(std::make_unique<CWindow>(
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<CWindow>(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:
Expand All @@ -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())
Expand Down Expand Up @@ -853,20 +918,20 @@ void callback::EditorSaveMenu(int Param)
if(WNDSave)
break;
{
WNDSave = global::s2->RegisterWindow(std::make_unique<CWindow>(EditorSaveMenu, WINDOWQUIT,
WindowPos::Center, Extent(280, 200),
"Save", WINDOW_GREEN1, WINDOW_CLOSE));
WNDSave = global::s2->RegisterWindow(
std::make_unique<CWindow>(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);
TXTF_Filename = WNDSave->addTextfield(Position(10, 13), 21, 1);
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,
Expand Down
4 changes: 3 additions & 1 deletion include/defines.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down