From 9358be6dbf4d6a0976815b4a073801d5fbc62263 Mon Sep 17 00:00:00 2001 From: Florian Albrechtskirchinger Date: Tue, 18 Jul 2023 10:23:00 +0200 Subject: [PATCH] Refactor IngameWindow button handling --- libs/s25main/ingameWindows/IngameWindow.cpp | 87 +++++++++++---------- libs/s25main/ingameWindows/IngameWindow.h | 24 ++++-- 2 files changed, 62 insertions(+), 49 deletions(-) diff --git a/libs/s25main/ingameWindows/IngameWindow.cpp b/libs/s25main/ingameWindows/IngameWindow.cpp index b86b6ca60..745d5e0b9 100644 --- a/libs/s25main/ingameWindows/IngameWindow.cpp +++ b/libs/s25main/ingameWindows/IngameWindow.cpp @@ -8,6 +8,7 @@ #include "Settings.h" #include "driver/MouseCoords.h" #include "drivers/VideoDriverWrapper.h" +#include "helpers/EnumRange.h" #include "helpers/MultiArray.h" #include "helpers/containerUtils.h" #include "ogl/FontStyle.h" @@ -27,12 +28,15 @@ const DrawPoint IngameWindow::posAtMouse(std::numeric_limits::max() - 1); const Extent IngameWindow::borderSize(1, 1); + +constexpr Extent IngameWindow::ButtonSize; + IngameWindow::IngameWindow(unsigned id, const DrawPoint& pos, const Extent& size, std::string title, glArchivItem_Bitmap* background, bool modal, CloseBehavior closeBehavior, Window* parent) : Window(parent, id, pos, size), title_(std::move(title)), background(background), lastMousePos(0, 0), - isModal_(modal), closeme(false), isMinimized_(false), isMoving(false), closeBehavior_(closeBehavior) + isModal_(modal), closeme(false), isMinimized_(false), isMoving(false), closeBehavior_(closeBehavior), + buttonStates_({ButtonState::Up, ButtonState::Up}) { - std::fill(buttonState.begin(), buttonState.end(), ButtonState::Up); contentOffset.x = LOADER.GetImageN("resource", 38)->getWidth(); // left border contentOffset.y = LOADER.GetImageN("resource", 42)->getHeight(); // title bar contentOffsetEnd.x = LOADER.GetImageN("resource", 39)->getWidth(); // right border @@ -146,13 +150,11 @@ void IngameWindow::MouseLeftDown(const MouseCoords& mc) lastMousePos = mc.GetPos(); } else { - // Check the 2 buttons - const std::array rec = {GetCloseButtonBounds(), GetMinimizeButtonBounds()}; - - for(unsigned i = 0; i < 2; ++i) + // Check the buttons + for(const auto id : helpers::enumRange()) { - if(IsPointInRect(mc.GetPos(), rec[i])) - buttonState[i] = ButtonState::Pressed; + if(IsPointInRect(mc.GetPos(), GetButtonBounds(id))) + buttonStates_[id] = ButtonState::Pressed; } } } @@ -161,26 +163,23 @@ void IngameWindow::MouseLeftUp(const MouseCoords& mc) { isMoving = false; - const std::array rec = {GetCloseButtonBounds(), GetMinimizeButtonBounds()}; - - for(unsigned i = 0; i < 2; ++i) + for(const auto id : helpers::enumRange()) { - buttonState[i] = ButtonState::Up; + buttonStates_[id] = ButtonState::Up; - if((i == 0 && closeBehavior_ == CloseBehavior::Custom) // no close button - || (i == 1 && isModal_)) // modal windows cannot be minimized - { + if((id == IwButtonId::Close && closeBehavior_ == CloseBehavior::Custom) // no close button + || (id != IwButtonId::Close && isModal_)) // modal windows cannot be minimized continue; - } - if(IsPointInRect(mc.GetPos(), rec[i])) + if(IsPointInRect(mc.GetPos(), GetButtonBounds(id))) { - if(i == 0) - Close(); - else + switch(id) { - SetMinimized(!IsMinimized()); - LOADER.GetSoundN("sound", 113)->Play(255, false); + case IwButtonId::Close: Close(); break; + case IwButtonId::Minimize: + SetMinimized(!IsMinimized()); + LOADER.GetSoundN("sound", 113)->Play(255, false); + break; } } } @@ -203,15 +202,13 @@ void IngameWindow::MouseMove(const MouseCoords& mc) lastMousePos = mc.GetPos(); } else { - // Check the 2 buttons - const std::array rec = {GetCloseButtonBounds(), GetMinimizeButtonBounds()}; - - for(unsigned i = 0; i < 2; ++i) + // Check the buttons + for(const auto id : helpers::enumRange()) { - if(IsPointInRect(mc.GetPos(), rec[i])) - buttonState[i] = mc.ldown ? ButtonState::Pressed : ButtonState::Hover; + if(IsPointInRect(mc.GetPos(), GetButtonBounds(id))) + buttonStates_[id] = mc.ldown ? ButtonState::Pressed : ButtonState::Hover; else - buttonState[i] = ButtonState::Up; + buttonStates_[id] = ButtonState::Up; } } } @@ -250,12 +247,15 @@ void IngameWindow::Draw_() glArchivItem_Bitmap* rightUpperImg = LOADER.GetImageN("resource", 37); rightUpperImg->DrawFull(GetPos() + DrawPoint(GetSize().x - rightUpperImg->getWidth(), 0)); - // The 2 buttons - constexpr std::array, 2> ids = {{{47, 55, 50}, {48, 56, 52}}}; + // The buttons + using ButtonStateResIds = helpers::EnumArray; + constexpr ButtonStateResIds closeButtonResIds = {47, 55, 51}; + constexpr ButtonStateResIds minimizeButtonResIds = {48, 56, 52}; if(closeBehavior_ != CloseBehavior::Custom) - LOADER.GetImageN("resource", ids[0][buttonState[0]])->DrawFull(GetPos()); + LOADER.GetImageN("resource", closeButtonResIds[buttonStates_[IwButtonId::Close]])->DrawFull(GetPos()); if(!IsModal()) - LOADER.GetImageN("resource", ids[1][buttonState[1]])->DrawFull(GetPos() + DrawPoint(GetSize().x - 16, 0)); + LOADER.GetImageN("resource", minimizeButtonResIds[buttonStates_[IwButtonId::Minimize]]) + ->DrawFull(GetButtonBounds(IwButtonId::Minimize)); // The title bar unsigned titleIndex; @@ -358,16 +358,6 @@ bool IngameWindow::IsMessageRelayAllowed() const return !isMinimized_; } -Rect IngameWindow::GetCloseButtonBounds() const -{ - return Rect(GetPos(), 16, 16); -} - -Rect IngameWindow::GetMinimizeButtonBounds() const -{ - return Rect(GetPos().x + GetSize().x - 16, GetPos().y, 16, 16); -} - void IngameWindow::SaveOpenStatus(bool isOpen) const { auto windowSettings = SETTINGS.windows.persistentSettings.find(GetGUIID()); @@ -376,3 +366,14 @@ void IngameWindow::SaveOpenStatus(bool isOpen) const windowSettings->second.isOpen = isOpen; } } + +Rect IngameWindow::GetButtonBounds(IwButtonId id) const +{ + auto pos = GetPos(); + switch(id) + { + case IwButtonId::Close: break; + case IwButtonId::Minimize: pos.x += GetSize().x - ButtonSize.x; break; + } + return Rect(pos, ButtonSize); +} diff --git a/libs/s25main/ingameWindows/IngameWindow.h b/libs/s25main/ingameWindows/IngameWindow.h index 96d1c45e2..ba8e26098 100644 --- a/libs/s25main/ingameWindows/IngameWindow.h +++ b/libs/s25main/ingameWindows/IngameWindow.h @@ -5,6 +5,7 @@ #pragma once #include "Window.h" +#include "helpers/EnumArray.h" #include "gameData/const_gui_ids.h" #include #include @@ -24,6 +25,16 @@ enum CloseBehavior NoRightClick, }; +enum class IwButtonId : unsigned short +{ + Close, + Minimize +}; +constexpr auto maxEnumValue(IwButtonId) +{ + return IwButtonId::Minimize; +} + class IngameWindow : public Window { public: @@ -99,22 +110,23 @@ class IngameWindow : public Window std::string title_; glArchivItem_Bitmap* background; DrawPoint lastMousePos; - std::array buttonState; /// Offset from left and top to actual content Extent contentOffset; /// Offset from content to right and bottom boundary Extent contentOffsetEnd; - /// Get bounds of close button (left) - Rect GetCloseButtonBounds() const; - /// Get bounds of minimize button (right) - Rect GetMinimizeButtonBounds() const; - private: + static constexpr auto ButtonSize = Extent(16, 16); + static constexpr auto TitleMargin = 32; + + /// Get bounds of given button + Rect GetButtonBounds(IwButtonId id) const; + bool isModal_; bool closeme; bool isMinimized_; bool isMoving; CloseBehavior closeBehavior_; + helpers::EnumArray buttonStates_; };