Skip to content

Commit

Permalink
Improve window positioning
Browse files Browse the repository at this point in the history
Improve window positioning in two ways:
1) Remember the window position set by a move and try to restore this position after every resize.
2) If the window is moved to a screen edge, save the width or height of the screen – depending on the edge – instead. This makes the window "stick" to the edge when resizing.
  • Loading branch information
falbrechtskirchinger committed Jul 22, 2023
1 parent 5907058 commit d96af72
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 15 deletions.
8 changes: 6 additions & 2 deletions libs/s25main/Settings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -347,8 +347,10 @@ void Settings::LoadIngame()
const auto* iniWindow = static_cast<const libsiedler2::ArchivItem_Ini*>(settingsIngame.find(window.second));
if(!iniWindow)
continue;
windows.persistentSettings[window.first].lastPos.x = iniWindow->getIntValue("pos_x");
windows.persistentSettings[window.first].lastPos.y = iniWindow->getIntValue("pos_y");
const auto x = windows.persistentSettings[window.first].lastPos.x = iniWindow->getIntValue("pos_x");
const auto y = windows.persistentSettings[window.first].lastPos.y = iniWindow->getIntValue("pos_y");
windows.persistentSettings[window.first].restorePos.x = iniWindow->getValue("restore_pos_x", x);
windows.persistentSettings[window.first].restorePos.y = iniWindow->getValue("restore_pos_y", y);
windows.persistentSettings[window.first].isOpen = iniWindow->getIntValue("is_open");
windows.persistentSettings[window.first].isMinimized = iniWindow->getValue("is_minimized", false);
}
Expand Down Expand Up @@ -502,6 +504,8 @@ void Settings::SaveIngame()
continue;
iniWindow->setValue("pos_x", windows.persistentSettings[window.first].lastPos.x);
iniWindow->setValue("pos_y", windows.persistentSettings[window.first].lastPos.y);
iniWindow->setValue("restore_pos_x", windows.persistentSettings[window.first].restorePos.x);
iniWindow->setValue("restore_pos_y", windows.persistentSettings[window.first].restorePos.y);
iniWindow->setValue("is_open", windows.persistentSettings[window.first].isOpen);
iniWindow->setValue("is_minimized", windows.persistentSettings[window.first].isMinimized);
}
Expand Down
1 change: 1 addition & 0 deletions libs/s25main/Settings.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ bool checkPort(int port);
struct PersistentWindowSettings
{
DrawPoint lastPos = DrawPoint::Invalid();
DrawPoint restorePos = DrawPoint::Invalid();
bool isOpen = false;
bool isMinimized = false;
};
Expand Down
25 changes: 18 additions & 7 deletions libs/s25main/ingameWindows/IngameWindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ IngameWindow::IngameWindow(unsigned id, const DrawPoint& pos, const Extent& size
if(pos == posLastOrCenter)
{
if(windowSettings_ && windowSettings_->lastPos.isValid())
SetPos(windowSettings_->lastPos);
SetPos(windowSettings_->lastPos, !windowSettings_->restorePos.isValid());
else
MoveToCenter();
} else if(pos == posCenter)
Expand All @@ -92,7 +92,7 @@ void IngameWindow::SetIwSize(const Extent& newSize)
Window::Resize(wndSize);

// Reset the position to check if parts of the window are out of the visible area
SetPos(GetPos());
SetPos(windowSettings_ ? windowSettings_->restorePos : GetPos(), false);
}

Extent IngameWindow::GetIwSize() const
Expand All @@ -105,24 +105,35 @@ DrawPoint IngameWindow::GetRightBottomBoundary()
return DrawPoint(GetSize() - contentOffsetEnd);
}

void IngameWindow::SetPos(DrawPoint newPos)
void IngameWindow::SetPos(DrawPoint newPos, bool saveRestorePos)
{
const Extent screenSize = VIDEODRIVER.GetRenderSize();
DrawPoint newRestorePos = newPos;
// Too far left or right?
if(newPos.x < 0)
newPos.x = 0;
else if(newPos.x + GetSize().x > screenSize.x)
newRestorePos.x = newPos.x = 0;
else if(newPos.x + GetSize().x >= screenSize.x)
{
newPos.x = screenSize.x - GetSize().x;
newRestorePos.x = screenSize.x; // make window stick to the right
}

// Too high or low?
if(newPos.y < 0)
newPos.y = 0;
else if(newPos.y + GetSize().y > screenSize.y)
newRestorePos.y = newPos.y = 0;
else if(newPos.y + GetSize().y >= screenSize.y)
{
newPos.y = screenSize.y - GetSize().y;
newRestorePos.y = screenSize.y; // make window stick to the bottom
}

// if possible save the position to settings
if(windowSettings_)
{
windowSettings_->lastPos = newPos;
if(saveRestorePos)
windowSettings_->restorePos = newRestorePos;
}

Window::SetPos(newPos);
}
Expand Down
2 changes: 1 addition & 1 deletion libs/s25main/ingameWindows/IngameWindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ class IngameWindow : public Window
DrawPoint GetRightBottomBoundary();

/// Set the position for the window after adjusting newPos so the window is in the visible area
void SetPos(DrawPoint newPos);
void SetPos(DrawPoint newPos, bool saveRestorePos = true);

/// merkt das Fenster zum Schließen vor.
virtual void Close();
Expand Down
5 changes: 0 additions & 5 deletions libs/s25main/ingameWindows/iwObservate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -143,11 +143,6 @@ void iwObservate::Msg_ButtonClick(const unsigned ctrl_id)
for(unsigned i = 1; i <= 4; ++i)
GetCtrl<ctrlImageButton>(i)->SetPos(
DrawPoint(GetCtrl<ctrlImageButton>(i)->GetPos().x - diff, GetSize().y - 50));

DrawPoint maxPos(VIDEODRIVER.GetRenderSize() - GetSize() - Extent::all(1));
DrawPoint newPos = elMin(maxPos, GetPos());
if(newPos != GetPos())
SetPos(newPos);
}
}

Expand Down

0 comments on commit d96af72

Please sign in to comment.