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"
1920#include < algorithm>
2021#include < utility>
2122
23+ namespace {
24+ constexpr Extent ButtonSize (16 , 16 );
25+ }
26+
2227const DrawPoint IngameWindow::posLastOrCenter (std::numeric_limits<DrawPoint::ElementType>::max(),
2328 std::numeric_limits<DrawPoint::ElementType>::max());
2429const 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-
371362void 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+ }
0 commit comments