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
2930const Extent IngameWindow::borderSize (1 , 1 );
31+
32+ constexpr Extent IngameWindow::ButtonSize;
33+
3034IngameWindow::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-
371361void 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+ }
0 commit comments