Skip to content

Commit df3aa8c

Browse files
falbrechtskirchingerFlamefire
authored andcommitted
Refactor IngameWindow button handling
1 parent ebe773e commit df3aa8c

File tree

2 files changed

+59
-48
lines changed

2 files changed

+59
-48
lines changed

libs/s25main/ingameWindows/IngameWindow.cpp

Lines changed: 44 additions & 42 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"
@@ -19,6 +20,10 @@
1920
#include <algorithm>
2021
#include <utility>
2122

23+
namespace {
24+
constexpr Extent ButtonSize(16, 16);
25+
}
26+
2227
const DrawPoint IngameWindow::posLastOrCenter(std::numeric_limits<DrawPoint::ElementType>::max(),
2328
std::numeric_limits<DrawPoint::ElementType>::max());
2429
const DrawPoint IngameWindow::posCenter(std::numeric_limits<DrawPoint::ElementType>::max() - 1,
@@ -32,7 +37,7 @@ IngameWindow::IngameWindow(unsigned id, const DrawPoint& pos, const Extent& size
3237
: Window(parent, id, pos, size), title_(std::move(title)), background(background), lastMousePos(0, 0),
3338
isModal_(modal), closeme(false), isMinimized_(false), isMoving(false), closeBehavior_(closeBehavior)
3439
{
35-
std::fill(buttonState.begin(), buttonState.end(), ButtonState::Up);
40+
std::fill(buttonStates_.begin(), buttonStates_.end(), ButtonState::Up);
3641
contentOffset.x = LOADER.GetImageN("resource", 38)->getWidth(); // left border
3742
contentOffset.y = LOADER.GetImageN("resource", 42)->getHeight(); // title bar
3843
contentOffsetEnd.x = LOADER.GetImageN("resource", 39)->getWidth(); // right border
@@ -146,13 +151,11 @@ void IngameWindow::MouseLeftDown(const MouseCoords& mc)
146151
lastMousePos = mc.GetPos();
147152
} else
148153
{
149-
// Check the 2 buttons
150-
const std::array<Rect, 2> rec = {GetCloseButtonBounds(), GetMinimizeButtonBounds()};
151-
152-
for(unsigned i = 0; i < 2; ++i)
154+
// Check the buttons
155+
for(const auto btn : helpers::enumRange<IwButton>())
153156
{
154-
if(IsPointInRect(mc.GetPos(), rec[i]))
155-
buttonState[i] = ButtonState::Pressed;
157+
if(IsPointInRect(mc.GetPos(), GetButtonBounds(btn)))
158+
buttonStates_[btn] = ButtonState::Pressed;
156159
}
157160
}
158161
}
@@ -161,26 +164,23 @@ void IngameWindow::MouseLeftUp(const MouseCoords& mc)
161164
{
162165
isMoving = false;
163166

164-
const std::array<Rect, 2> rec = {GetCloseButtonBounds(), GetMinimizeButtonBounds()};
165-
166-
for(unsigned i = 0; i < 2; ++i)
167+
for(const auto btn : helpers::enumRange<IwButton>())
167168
{
168-
buttonState[i] = ButtonState::Up;
169+
buttonStates_[btn] = ButtonState::Up;
169170

170-
if((i == 0 && closeBehavior_ == CloseBehavior::Custom) // no close button
171-
|| (i == 1 && isModal_)) // modal windows cannot be minimized
172-
{
171+
if((btn == IwButton::Close && closeBehavior_ == CloseBehavior::Custom) // no close button
172+
|| (btn == IwButton::Minimize && isModal_)) // modal windows cannot be minimized
173173
continue;
174-
}
175174

176-
if(IsPointInRect(mc.GetPos(), rec[i]))
175+
if(IsPointInRect(mc.GetPos(), GetButtonBounds(btn)))
177176
{
178-
if(i == 0)
179-
Close();
180-
else
177+
switch(btn)
181178
{
182-
SetMinimized(!IsMinimized());
183-
LOADER.GetSoundN("sound", 113)->Play(255, false);
179+
case IwButton::Close: Close(); break;
180+
case IwButton::Minimize:
181+
SetMinimized(!IsMinimized());
182+
LOADER.GetSoundN("sound", 113)->Play(255, false);
183+
break;
184184
}
185185
}
186186
}
@@ -203,15 +203,13 @@ void IngameWindow::MouseMove(const MouseCoords& mc)
203203
lastMousePos = mc.GetPos();
204204
} else
205205
{
206-
// Check the 2 buttons
207-
const std::array<Rect, 2> rec = {GetCloseButtonBounds(), GetMinimizeButtonBounds()};
208-
209-
for(unsigned i = 0; i < 2; ++i)
206+
// Check the buttons
207+
for(const auto btn : helpers::enumRange<IwButton>())
210208
{
211-
if(IsPointInRect(mc.GetPos(), rec[i]))
212-
buttonState[i] = mc.ldown ? ButtonState::Pressed : ButtonState::Hover;
209+
if(IsPointInRect(mc.GetPos(), GetButtonBounds(btn)))
210+
buttonStates_[btn] = mc.ldown ? ButtonState::Pressed : ButtonState::Hover;
213211
else
214-
buttonState[i] = ButtonState::Up;
212+
buttonStates_[btn] = ButtonState::Up;
215213
}
216214
}
217215
}
@@ -250,12 +248,15 @@ void IngameWindow::Draw_()
250248
glArchivItem_Bitmap* rightUpperImg = LOADER.GetImageN("resource", 37);
251249
rightUpperImg->DrawFull(GetPos() + DrawPoint(GetSize().x - rightUpperImg->getWidth(), 0));
252250

253-
// The 2 buttons
254-
constexpr std::array<helpers::EnumArray<uint16_t, ButtonState>, 2> ids = {{{47, 55, 50}, {48, 56, 52}}};
251+
// The buttons
252+
using ButtonStateResIds = helpers::EnumArray<unsigned, ButtonState>;
253+
constexpr ButtonStateResIds closeResIds = {47, 55, 51};
254+
constexpr ButtonStateResIds minimizeResIds = {48, 56, 52};
255255
if(closeBehavior_ != CloseBehavior::Custom)
256-
LOADER.GetImageN("resource", ids[0][buttonState[0]])->DrawFull(GetPos());
256+
LOADER.GetImageN("resource", closeResIds[buttonStates_[IwButton::Close]])->DrawFull(GetPos());
257257
if(!IsModal())
258-
LOADER.GetImageN("resource", ids[1][buttonState[1]])->DrawFull(GetPos() + DrawPoint(GetSize().x - 16, 0));
258+
LOADER.GetImageN("resource", minimizeResIds[buttonStates_[IwButton::Minimize]])
259+
->DrawFull(GetButtonBounds(IwButton::Minimize));
259260

260261
// The title bar
261262
unsigned titleIndex;
@@ -358,16 +359,6 @@ bool IngameWindow::IsMessageRelayAllowed() const
358359
return !isMinimized_;
359360
}
360361

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-
371362
void IngameWindow::SaveOpenStatus(bool isOpen) const
372363
{
373364
auto windowSettings = SETTINGS.windows.persistentSettings.find(GetGUIID());
@@ -376,3 +367,14 @@ void IngameWindow::SaveOpenStatus(bool isOpen) const
376367
windowSettings->second.isOpen = isOpen;
377368
}
378369
}
370+
371+
Rect IngameWindow::GetButtonBounds(IwButton btn) const
372+
{
373+
auto pos = GetPos();
374+
switch(btn)
375+
{
376+
case IwButton::Close: break;
377+
case IwButton::Minimize: pos.x += GetSize().x - ButtonSize.x; break;
378+
}
379+
return Rect(pos, ButtonSize);
380+
}

libs/s25main/ingameWindows/IngameWindow.h

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#pragma once
66

77
#include "Window.h"
8+
#include "helpers/EnumArray.h"
89
#include "gameData/const_gui_ids.h"
910
#include <array>
1011
#include <vector>
@@ -24,6 +25,16 @@ enum CloseBehavior
2425
NoRightClick,
2526
};
2627

28+
enum class IwButton
29+
{
30+
Close,
31+
Minimize
32+
};
33+
constexpr auto maxEnumValue(IwButton)
34+
{
35+
return IwButton::Minimize;
36+
}
37+
2738
class IngameWindow : public Window
2839
{
2940
public:
@@ -99,22 +110,20 @@ class IngameWindow : public Window
99110
std::string title_;
100111
glArchivItem_Bitmap* background;
101112
DrawPoint lastMousePos;
102-
std::array<ButtonState, 2> buttonState;
103113

104114
/// Offset from left and top to actual content
105115
Extent contentOffset;
106116
/// Offset from content to right and bottom boundary
107117
Extent contentOffsetEnd;
108118

109-
/// Get bounds of close button (left)
110-
Rect GetCloseButtonBounds() const;
111-
/// Get bounds of minimize button (right)
112-
Rect GetMinimizeButtonBounds() const;
113-
114119
private:
120+
/// Get bounds of given button
121+
Rect GetButtonBounds(IwButton btn) const;
122+
115123
bool isModal_;
116124
bool closeme;
117125
bool isMinimized_;
118126
bool isMoving;
119127
CloseBehavior closeBehavior_;
128+
helpers::EnumArray<ButtonState, IwButton> buttonStates_;
120129
};

0 commit comments

Comments
 (0)