Skip to content

Commit

Permalink
Refactor IngameWindow button handling
Browse files Browse the repository at this point in the history
  • Loading branch information
falbrechtskirchinger committed Jul 18, 2023
1 parent 286b61e commit 321e33a
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 49 deletions.
85 changes: 42 additions & 43 deletions libs/s25main/ingameWindows/IngameWindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@ const Extent IngameWindow::borderSize(1, 1);
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),
buttons_({ID_btnClose, ID_btnMinimize})
{
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
Expand Down Expand Up @@ -146,13 +146,11 @@ void IngameWindow::MouseLeftDown(const MouseCoords& mc)
lastMousePos = mc.GetPos();
} else
{
// Check the 2 buttons
const std::array<Rect, 2> rec = {GetCloseButtonBounds(), GetMinimizeButtonBounds()};

for(unsigned i = 0; i < 2; ++i)
// Check the buttons
for(auto& button : buttons_)
{
if(IsPointInRect(mc.GetPos(), rec[i]))
buttonState[i] = ButtonState::Pressed;
if(IsPointInRect(mc.GetPos(), GetButtonBounds(button.id)))
button.state = ButtonState::Pressed;
}
}
}
Expand All @@ -161,26 +159,24 @@ void IngameWindow::MouseLeftUp(const MouseCoords& mc)
{
isMoving = false;

const std::array<Rect, 2> rec = {GetCloseButtonBounds(), GetMinimizeButtonBounds()};

for(unsigned i = 0; i < 2; ++i)
for(auto& button : buttons_)
{
buttonState[i] = ButtonState::Up;
button.state = ButtonState::Up;

if((i == 0 && closeBehavior_ == CloseBehavior::Custom) // no close button
|| (i == 1 && isModal_)) // modal windows cannot be minimized
{
if((button.id == ID_btnClose && closeBehavior_ == CloseBehavior::Custom) // no close button
|| (button.id != ID_btnClose && isModal_)) // modal windows cannot be minimized
continue;
}

if(IsPointInRect(mc.GetPos(), rec[i]))
if(IsPointInRect(mc.GetPos(), GetButtonBounds(button.id)))
{
if(i == 0)
Close();
else
switch(button.id)
{
SetMinimized(!IsMinimized());
LOADER.GetSoundN("sound", 113)->Play(255, false);
default:
case ID_btnClose: Close(); break;
case ID_btnMinimize:
SetMinimized(!IsMinimized());
LOADER.GetSoundN("sound", 113)->Play(255, false);
break;
}
}
}
Expand All @@ -203,15 +199,13 @@ void IngameWindow::MouseMove(const MouseCoords& mc)
lastMousePos = mc.GetPos();
} else
{
// Check the 2 buttons
const std::array<Rect, 2> rec = {GetCloseButtonBounds(), GetMinimizeButtonBounds()};

for(unsigned i = 0; i < 2; ++i)
// Check the buttons
for(auto& button : buttons_)
{
if(IsPointInRect(mc.GetPos(), rec[i]))
buttonState[i] = mc.ldown ? ButtonState::Pressed : ButtonState::Hover;
if(IsPointInRect(mc.GetPos(), GetButtonBounds(button.id)))
button.state = mc.ldown ? ButtonState::Pressed : ButtonState::Hover;
else
buttonState[i] = ButtonState::Up;
button.state = ButtonState::Up;
}
}
}
Expand Down Expand Up @@ -250,12 +244,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<helpers::EnumArray<uint16_t, ButtonState>, 2> ids = {{{47, 55, 50}, {48, 56, 52}}};
// The buttons
using ButtonStateIds = helpers::EnumArray<uint16_t, ButtonState>;
constexpr ButtonStateIds closeButtonIds = {47, 55, 51};
constexpr ButtonStateIds minimizeButtonIds = {48, 56, 52};
if(closeBehavior_ != CloseBehavior::Custom)
LOADER.GetImageN("resource", ids[0][buttonState[0]])->DrawFull(GetPos());
LOADER.GetImageN("resource", closeButtonIds[buttons_[ID_btnClose].state])->DrawFull(GetPos());
if(!IsModal())
LOADER.GetImageN("resource", ids[1][buttonState[1]])->DrawFull(GetPos() + DrawPoint(GetSize().x - 16, 0));
LOADER.GetImageN("resource", minimizeButtonIds[buttons_[ID_btnMinimize].state])
->DrawFull(GetPos() + DrawPoint(GetSize().x - 16, 0));

// The title bar
unsigned titleIndex;
Expand Down Expand Up @@ -358,16 +355,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());
Expand All @@ -376,3 +363,15 @@ void IngameWindow::SaveOpenStatus(bool isOpen) const
windowSettings->second.isOpen = isOpen;
}
}

Rect IngameWindow::GetButtonBounds(unsigned short id) const
{
auto pos = GetPos();
switch(id)
{
default:
case ID_btnClose: break;
case ID_btnMinimize: pos.x += GetSize().x - ButtonSize.x; break;
}
return Rect(pos, ButtonSize);
}
26 changes: 20 additions & 6 deletions libs/s25main/ingameWindows/IngameWindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -99,22 +99,36 @@ class IngameWindow : public Window
std::string title_;
glArchivItem_Bitmap* background;
DrawPoint lastMousePos;
std::array<ButtonState, 2> 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:
enum ButtonIds : unsigned short
{
ID_btnClose,
ID_btnMinimize
};

struct Button
{
Button(unsigned short id) : id(id) {}

unsigned short id;
ButtonState state = ButtonState::Up;
};

static constexpr auto ButtonSize = Extent(16, 16);

/// Get bounds of given button
Rect GetButtonBounds(unsigned short id) const;

bool isModal_;
bool closeme;
bool isMinimized_;
bool isMoving;
CloseBehavior closeBehavior_;
std::array<Button, 2> buttons_;
};

0 comments on commit 321e33a

Please sign in to comment.