Skip to content

Commit 321e33a

Browse files
Refactor IngameWindow button handling
1 parent 286b61e commit 321e33a

2 files changed

Lines changed: 62 additions & 49 deletions

File tree

libs/s25main/ingameWindows/IngameWindow.cpp

Lines changed: 42 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,9 @@ const Extent IngameWindow::borderSize(1, 1);
3030
IngameWindow::IngameWindow(unsigned id, const DrawPoint& pos, const Extent& size, std::string title,
3131
glArchivItem_Bitmap* background, bool modal, CloseBehavior closeBehavior, Window* parent)
3232
: Window(parent, id, pos, size), title_(std::move(title)), background(background), lastMousePos(0, 0),
33-
isModal_(modal), closeme(false), isMinimized_(false), isMoving(false), closeBehavior_(closeBehavior)
33+
isModal_(modal), closeme(false), isMinimized_(false), isMoving(false), closeBehavior_(closeBehavior),
34+
buttons_({ID_btnClose, ID_btnMinimize})
3435
{
35-
std::fill(buttonState.begin(), buttonState.end(), ButtonState::Up);
3636
contentOffset.x = LOADER.GetImageN("resource", 38)->getWidth(); // left border
3737
contentOffset.y = LOADER.GetImageN("resource", 42)->getHeight(); // title bar
3838
contentOffsetEnd.x = LOADER.GetImageN("resource", 39)->getWidth(); // right border
@@ -146,13 +146,11 @@ void IngameWindow::MouseLeftDown(const MouseCoords& mc)
146146
lastMousePos = mc.GetPos();
147147
} else
148148
{
149-
// Check the 2 buttons
150-
const std::array<Rect, 2> rec = {GetCloseButtonBounds(), GetMinimizeButtonBounds()};
151-
152-
for(unsigned i = 0; i < 2; ++i)
149+
// Check the buttons
150+
for(auto& button : buttons_)
153151
{
154-
if(IsPointInRect(mc.GetPos(), rec[i]))
155-
buttonState[i] = ButtonState::Pressed;
152+
if(IsPointInRect(mc.GetPos(), GetButtonBounds(button.id)))
153+
button.state = ButtonState::Pressed;
156154
}
157155
}
158156
}
@@ -161,26 +159,24 @@ void IngameWindow::MouseLeftUp(const MouseCoords& mc)
161159
{
162160
isMoving = false;
163161

164-
const std::array<Rect, 2> rec = {GetCloseButtonBounds(), GetMinimizeButtonBounds()};
165-
166-
for(unsigned i = 0; i < 2; ++i)
162+
for(auto& button : buttons_)
167163
{
168-
buttonState[i] = ButtonState::Up;
164+
button.state = ButtonState::Up;
169165

170-
if((i == 0 && closeBehavior_ == CloseBehavior::Custom) // no close button
171-
|| (i == 1 && isModal_)) // modal windows cannot be minimized
172-
{
166+
if((button.id == ID_btnClose && closeBehavior_ == CloseBehavior::Custom) // no close button
167+
|| (button.id != ID_btnClose && isModal_)) // modal windows cannot be minimized
173168
continue;
174-
}
175169

176-
if(IsPointInRect(mc.GetPos(), rec[i]))
170+
if(IsPointInRect(mc.GetPos(), GetButtonBounds(button.id)))
177171
{
178-
if(i == 0)
179-
Close();
180-
else
172+
switch(button.id)
181173
{
182-
SetMinimized(!IsMinimized());
183-
LOADER.GetSoundN("sound", 113)->Play(255, false);
174+
default:
175+
case ID_btnClose: Close(); break;
176+
case ID_btnMinimize:
177+
SetMinimized(!IsMinimized());
178+
LOADER.GetSoundN("sound", 113)->Play(255, false);
179+
break;
184180
}
185181
}
186182
}
@@ -203,15 +199,13 @@ void IngameWindow::MouseMove(const MouseCoords& mc)
203199
lastMousePos = mc.GetPos();
204200
} else
205201
{
206-
// Check the 2 buttons
207-
const std::array<Rect, 2> rec = {GetCloseButtonBounds(), GetMinimizeButtonBounds()};
208-
209-
for(unsigned i = 0; i < 2; ++i)
202+
// Check the buttons
203+
for(auto& button : buttons_)
210204
{
211-
if(IsPointInRect(mc.GetPos(), rec[i]))
212-
buttonState[i] = mc.ldown ? ButtonState::Pressed : ButtonState::Hover;
205+
if(IsPointInRect(mc.GetPos(), GetButtonBounds(button.id)))
206+
button.state = mc.ldown ? ButtonState::Pressed : ButtonState::Hover;
213207
else
214-
buttonState[i] = ButtonState::Up;
208+
button.state = ButtonState::Up;
215209
}
216210
}
217211
}
@@ -250,12 +244,15 @@ void IngameWindow::Draw_()
250244
glArchivItem_Bitmap* rightUpperImg = LOADER.GetImageN("resource", 37);
251245
rightUpperImg->DrawFull(GetPos() + DrawPoint(GetSize().x - rightUpperImg->getWidth(), 0));
252246

253-
// The 2 buttons
254-
constexpr std::array<helpers::EnumArray<uint16_t, ButtonState>, 2> ids = {{{47, 55, 50}, {48, 56, 52}}};
247+
// The buttons
248+
using ButtonStateIds = helpers::EnumArray<uint16_t, ButtonState>;
249+
constexpr ButtonStateIds closeButtonIds = {47, 55, 51};
250+
constexpr ButtonStateIds minimizeButtonIds = {48, 56, 52};
255251
if(closeBehavior_ != CloseBehavior::Custom)
256-
LOADER.GetImageN("resource", ids[0][buttonState[0]])->DrawFull(GetPos());
252+
LOADER.GetImageN("resource", closeButtonIds[buttons_[ID_btnClose].state])->DrawFull(GetPos());
257253
if(!IsModal())
258-
LOADER.GetImageN("resource", ids[1][buttonState[1]])->DrawFull(GetPos() + DrawPoint(GetSize().x - 16, 0));
254+
LOADER.GetImageN("resource", minimizeButtonIds[buttons_[ID_btnMinimize].state])
255+
->DrawFull(GetPos() + DrawPoint(GetSize().x - 16, 0));
259256

260257
// The title bar
261258
unsigned titleIndex;
@@ -358,16 +355,6 @@ bool IngameWindow::IsMessageRelayAllowed() const
358355
return !isMinimized_;
359356
}
360357

361-
Rect IngameWindow::GetCloseButtonBounds() const
362-
{
363-
return Rect(GetPos(), 16, 16);
364-
}
365-
366-
Rect IngameWindow::GetMinimizeButtonBounds() const
367-
{
368-
return Rect(GetPos().x + GetSize().x - 16, GetPos().y, 16, 16);
369-
}
370-
371358
void IngameWindow::SaveOpenStatus(bool isOpen) const
372359
{
373360
auto windowSettings = SETTINGS.windows.persistentSettings.find(GetGUIID());
@@ -376,3 +363,15 @@ void IngameWindow::SaveOpenStatus(bool isOpen) const
376363
windowSettings->second.isOpen = isOpen;
377364
}
378365
}
366+
367+
Rect IngameWindow::GetButtonBounds(unsigned short id) const
368+
{
369+
auto pos = GetPos();
370+
switch(id)
371+
{
372+
default:
373+
case ID_btnClose: break;
374+
case ID_btnMinimize: pos.x += GetSize().x - ButtonSize.x; break;
375+
}
376+
return Rect(pos, ButtonSize);
377+
}

libs/s25main/ingameWindows/IngameWindow.h

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -99,22 +99,36 @@ class IngameWindow : public Window
9999
std::string title_;
100100
glArchivItem_Bitmap* background;
101101
DrawPoint lastMousePos;
102-
std::array<ButtonState, 2> buttonState;
103102

104103
/// Offset from left and top to actual content
105104
Extent contentOffset;
106105
/// Offset from content to right and bottom boundary
107106
Extent contentOffsetEnd;
108107

109-
/// Get bounds of close button (left)
110-
Rect GetCloseButtonBounds() const;
111-
/// Get bounds of minimize button (right)
112-
Rect GetMinimizeButtonBounds() const;
113-
114108
private:
109+
enum ButtonIds : unsigned short
110+
{
111+
ID_btnClose,
112+
ID_btnMinimize
113+
};
114+
115+
struct Button
116+
{
117+
Button(unsigned short id) : id(id) {}
118+
119+
unsigned short id;
120+
ButtonState state = ButtonState::Up;
121+
};
122+
123+
static constexpr auto ButtonSize = Extent(16, 16);
124+
125+
/// Get bounds of given button
126+
Rect GetButtonBounds(unsigned short id) const;
127+
115128
bool isModal_;
116129
bool closeme;
117130
bool isMinimized_;
118131
bool isMoving;
119132
CloseBehavior closeBehavior_;
133+
std::array<Button, 2> buttons_;
120134
};

0 commit comments

Comments
 (0)