Skip to content

Commit 3df993a

Browse files
Refactor IngameWindow button handling
1 parent e6d791d commit 3df993a

2 files changed

Lines changed: 59 additions & 49 deletions

File tree

libs/s25main/ingameWindows/IngameWindow.cpp

Lines changed: 44 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#include "Settings.h"
99
#include "driver/MouseCoords.h"
1010
#include "drivers/VideoDriverWrapper.h"
11+
#include "helpers/EnumRange.h"
1112
#include "helpers/MultiArray.h"
1213
#include "helpers/containerUtils.h"
1314
#include "ogl/FontStyle.h"
@@ -27,12 +28,15 @@ const DrawPoint IngameWindow::posAtMouse(std::numeric_limits<DrawPoint::ElementT
2728
std::numeric_limits<DrawPoint::ElementType>::max() - 1);
2829

2930
const Extent IngameWindow::borderSize(1, 1);
31+
32+
constexpr Extent IngameWindow::ButtonSize;
33+
3034
IngameWindow::IngameWindow(unsigned id, const DrawPoint& pos, const Extent& size, std::string title,
3135
glArchivItem_Bitmap* background, bool modal, CloseBehavior closeBehavior, Window* parent)
3236
: 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)
37+
isModal_(modal), closeme(false), isMinimized_(false), isMoving(false), closeBehavior_(closeBehavior),
38+
buttonStates_({ButtonState::Up, ButtonState::Up})
3439
{
35-
std::fill(buttonState.begin(), buttonState.end(), ButtonState::Up);
3640
contentOffset.x = LOADER.GetImageN("resource", 38)->getWidth(); // left border
3741
contentOffset.y = LOADER.GetImageN("resource", 42)->getHeight(); // title bar
3842
contentOffsetEnd.x = LOADER.GetImageN("resource", 39)->getWidth(); // right border
@@ -146,13 +150,11 @@ void IngameWindow::MouseLeftDown(const MouseCoords& mc)
146150
lastMousePos = mc.GetPos();
147151
} else
148152
{
149-
// Check the 2 buttons
150-
const std::array<Rect, 2> rec = {GetCloseButtonBounds(), GetMinimizeButtonBounds()};
151-
152-
for(unsigned i = 0; i < 2; ++i)
153+
// Check the buttons
154+
for(const auto id : helpers::enumRange<IwButtonId>())
153155
{
154-
if(IsPointInRect(mc.GetPos(), rec[i]))
155-
buttonState[i] = ButtonState::Pressed;
156+
if(IsPointInRect(mc.GetPos(), GetButtonBounds(id)))
157+
buttonStates_[id] = ButtonState::Pressed;
156158
}
157159
}
158160
}
@@ -161,26 +163,23 @@ void IngameWindow::MouseLeftUp(const MouseCoords& mc)
161163
{
162164
isMoving = false;
163165

164-
const std::array<Rect, 2> rec = {GetCloseButtonBounds(), GetMinimizeButtonBounds()};
165-
166-
for(unsigned i = 0; i < 2; ++i)
166+
for(const auto id : helpers::enumRange<IwButtonId>())
167167
{
168-
buttonState[i] = ButtonState::Up;
168+
buttonStates_[id] = ButtonState::Up;
169169

170-
if((i == 0 && closeBehavior_ == CloseBehavior::Custom) // no close button
171-
|| (i == 1 && isModal_)) // modal windows cannot be minimized
172-
{
170+
if((id == IwButtonId::Close && closeBehavior_ == CloseBehavior::Custom) // no close button
171+
|| (id != IwButtonId::Close && isModal_)) // modal windows cannot be minimized
173172
continue;
174-
}
175173

176-
if(IsPointInRect(mc.GetPos(), rec[i]))
174+
if(IsPointInRect(mc.GetPos(), GetButtonBounds(id)))
177175
{
178-
if(i == 0)
179-
Close();
180-
else
176+
switch(id)
181177
{
182-
SetMinimized(!IsMinimized());
183-
LOADER.GetSoundN("sound", 113)->Play(255, false);
178+
case IwButtonId::Close: Close(); break;
179+
case IwButtonId::Minimize:
180+
SetMinimized(!IsMinimized());
181+
LOADER.GetSoundN("sound", 113)->Play(255, false);
182+
break;
184183
}
185184
}
186185
}
@@ -203,15 +202,13 @@ void IngameWindow::MouseMove(const MouseCoords& mc)
203202
lastMousePos = mc.GetPos();
204203
} else
205204
{
206-
// Check the 2 buttons
207-
const std::array<Rect, 2> rec = {GetCloseButtonBounds(), GetMinimizeButtonBounds()};
208-
209-
for(unsigned i = 0; i < 2; ++i)
205+
// Check the buttons
206+
for(const auto id : helpers::enumRange<IwButtonId>())
210207
{
211-
if(IsPointInRect(mc.GetPos(), rec[i]))
212-
buttonState[i] = mc.ldown ? ButtonState::Pressed : ButtonState::Hover;
208+
if(IsPointInRect(mc.GetPos(), GetButtonBounds(id)))
209+
buttonStates_[id] = mc.ldown ? ButtonState::Pressed : ButtonState::Hover;
213210
else
214-
buttonState[i] = ButtonState::Up;
211+
buttonStates_[id] = ButtonState::Up;
215212
}
216213
}
217214
}
@@ -250,12 +247,15 @@ void IngameWindow::Draw_()
250247
glArchivItem_Bitmap* rightUpperImg = LOADER.GetImageN("resource", 37);
251248
rightUpperImg->DrawFull(GetPos() + DrawPoint(GetSize().x - rightUpperImg->getWidth(), 0));
252249

253-
// The 2 buttons
254-
constexpr std::array<helpers::EnumArray<uint16_t, ButtonState>, 2> ids = {{{47, 55, 50}, {48, 56, 52}}};
250+
// The buttons
251+
using ButtonStateResIds = helpers::EnumArray<uint16_t, ButtonState>;
252+
constexpr ButtonStateResIds closeButtonResIds = {47, 55, 51};
253+
constexpr ButtonStateResIds minimizeButtonResIds = {48, 56, 52};
255254
if(closeBehavior_ != CloseBehavior::Custom)
256-
LOADER.GetImageN("resource", ids[0][buttonState[0]])->DrawFull(GetPos());
255+
LOADER.GetImageN("resource", closeButtonResIds[buttonStates_[IwButtonId::Close]])->DrawFull(GetPos());
257256
if(!IsModal())
258-
LOADER.GetImageN("resource", ids[1][buttonState[1]])->DrawFull(GetPos() + DrawPoint(GetSize().x - 16, 0));
257+
LOADER.GetImageN("resource", minimizeButtonResIds[buttonStates_[IwButtonId::Minimize]])
258+
->DrawFull(GetButtonBounds(IwButtonId::Minimize));
259259

260260
// The title bar
261261
unsigned titleIndex;
@@ -358,16 +358,6 @@ bool IngameWindow::IsMessageRelayAllowed() const
358358
return !isMinimized_;
359359
}
360360

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-
371361
void IngameWindow::SaveOpenStatus(bool isOpen) const
372362
{
373363
auto windowSettings = SETTINGS.windows.persistentSettings.find(GetGUIID());
@@ -376,3 +366,14 @@ void IngameWindow::SaveOpenStatus(bool isOpen) const
376366
windowSettings->second.isOpen = isOpen;
377367
}
378368
}
369+
370+
Rect IngameWindow::GetButtonBounds(IwButtonId id) const
371+
{
372+
auto pos = GetPos();
373+
switch(id)
374+
{
375+
case IwButtonId::Close: break;
376+
case IwButtonId::Minimize: pos.x += GetSize().x - ButtonSize.x; break;
377+
}
378+
return Rect(pos, ButtonSize);
379+
}

libs/s25main/ingameWindows/IngameWindow.h

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
#include "Window.h"
88
#include "gameData/const_gui_ids.h"
9+
#include "helpers/EnumArray.h"
910
#include <array>
1011
#include <vector>
1112

@@ -24,6 +25,13 @@ enum CloseBehavior
2425
NoRightClick,
2526
};
2627

28+
enum class IwButtonId : unsigned short
29+
{
30+
Close,
31+
Minimize
32+
};
33+
constexpr auto maxEnumValue(IwButtonId) { return IwButtonId::Minimize; }
34+
2735
class IngameWindow : public Window
2836
{
2937
public:
@@ -99,22 +107,23 @@ class IngameWindow : public Window
99107
std::string title_;
100108
glArchivItem_Bitmap* background;
101109
DrawPoint lastMousePos;
102-
std::array<ButtonState, 2> buttonState;
103110

104111
/// Offset from left and top to actual content
105112
Extent contentOffset;
106113
/// Offset from content to right and bottom boundary
107114
Extent contentOffsetEnd;
108115

109-
/// Get bounds of close button (left)
110-
Rect GetCloseButtonBounds() const;
111-
/// Get bounds of minimize button (right)
112-
Rect GetMinimizeButtonBounds() const;
113-
114116
private:
117+
static constexpr auto ButtonSize = Extent(16, 16);
118+
static constexpr auto TitleMargin = 32;
119+
120+
/// Get bounds of given button
121+
Rect GetButtonBounds(IwButtonId id) const;
122+
115123
bool isModal_;
116124
bool closeme;
117125
bool isMinimized_;
118126
bool isMoving;
119127
CloseBehavior closeBehavior_;
128+
helpers::EnumArray<ButtonState, IwButtonId> buttonStates_;
120129
};

0 commit comments

Comments
 (0)