Remember window positions as preferences

This commit is contained in:
2025-04-09 16:03:31 -05:00
parent c89ea6aa45
commit 3ba9259482
6 changed files with 43 additions and 8 deletions

View File

@@ -742,13 +742,9 @@ void cDialog::handle_one_event(const sf::Event& currentEvent, cFramerateLimiter&
case sf::Event::GainedFocus:
case sf::Event::MouseMoved:
// Did the window move, potentially dirtying the canvas below it?
auto winPosition = win.getPosition();
if (winLastX != winPosition.x || winLastY != winPosition.y) {
if(check_window_moved(win, winLastX, winLastY))
if (redraw_everything != NULL)
redraw_everything();
}
winLastX = winPosition.x;
winLastY = winPosition.y;
bool inField = false;
for(auto& ctrl : controls) {

View File

@@ -184,7 +184,9 @@ void adjust_window_mode() {
mainPtr().create(sf::VideoMode(width, winHeight, 32), "Blades of Exile", sf::Style::Titlebar | sf::Style::Close, winSettings);
// Center the small window on the desktop
mainPtr().setPosition({static_cast<int>((desktop.width - width) / 2), static_cast<int>((desktop.height - height) / 2)});
int win_x = get_int_pref("MainWindowX", static_cast<int>((desktop.width - width) / 2));
int win_y = get_int_pref("MainWindowY", static_cast<int>((desktop.height - height) / 2));
mainPtr().setPosition({win_x, win_y});
} else {
mainPtr().create(desktop, "Blades of Exile", sf::Style::None, winSettings);
mainPtr().setPosition({0,0});

View File

@@ -681,7 +681,10 @@ void init_mini_map() {
if (map_scale < 0.1) map_scale = 1.0;
if (mini_map().isOpen()) mini_map().close();
mini_map().create(sf::VideoMode(map_scale*296,map_scale*277), "Map", sf::Style::Titlebar | sf::Style::Close);
mini_map().setPosition(sf::Vector2i(52,62));
// TODO why is 52,62 the default position, anyway?
int map_x = get_int_pref("MapWindowX", 52);
int map_y = get_int_pref("MapWindowY", 62);
mini_map().setPosition(sf::Vector2i(map_x,map_y));
sf::View view;
view.reset(sf::FloatRect(0, 0, map_scale*296,map_scale*277));
view.setViewport(sf::FloatRect(0, 0, map_scale, map_scale));

View File

@@ -1299,6 +1299,9 @@ void handle_quit_event() {
All_Done = true;
}
int last_window_x = 0;
int last_window_y = 0;
void handle_one_event(const sf::Event& event, cFramerateLimiter& fps_limiter) {
// What does this do and should it be here?
@@ -1334,11 +1337,13 @@ void handle_one_event(const sf::Event& event, cFramerateLimiter& fps_limiter) {
break;
case sf::Event::GainedFocus:
check_window_moved(mainPtr(), last_window_x, last_window_y, "MainWindow");
makeFrontWindow(mainPtr());
change_cursor({event.mouseMove.x, event.mouseMove.y});
return;
case sf::Event::MouseMoved:
check_window_moved(mainPtr(), last_window_x, last_window_y, "MainWindow");
change_cursor({event.mouseMove.x, event.mouseMove.y});
return;
@@ -1360,11 +1365,17 @@ void queue_fake_event(const sf::Event& event) {
fake_event_queue.push_back(event);
}
int last_map_x = 0;
int last_map_y = 0;
void handle_one_minimap_event(const sf::Event& event) {
if(event.type == sf::Event::Closed) {
close_map(true);
} else if(event.type == sf::Event::GainedFocus) {
check_window_moved(mini_map(), last_map_x, last_map_y, "MapWindow");
makeFrontWindow(mainPtr());
}else if(event.type == sf::Event::MouseMoved){
check_window_moved(mini_map(), last_map_x, last_map_y, "MapWindow");
}else if(event.type == sf::Event::KeyPressed) {
switch(event.key.code){
case sf::Keyboard::Escape:

View File

@@ -69,3 +69,24 @@ void launchDocs(std::string relative_url) {
launchURL("http://openboe.com/docs/" + relative_url);
}
}
bool check_window_moved(sf::RenderWindow& win, int& winLastX, int& winLastY, std::string position_pref) {
auto winPosition = win.getPosition();
bool moved = false;
if(winLastX != winPosition.x || winLastY != winPosition.y){
// Save the positions of main window and map window as hidden preferences
// (clamped to keep them fully on-screen when they appear the first time)
if(!position_pref.empty()){
sf::VideoMode desktop = sf::VideoMode::getDesktopMode();
int pref_x = minmax(0, desktop.width - win.getSize().x, winPosition.x);
int pref_y = minmax(0, desktop.height - win.getSize().y, winPosition.y);
set_pref(position_pref + "X", pref_x);
set_pref(position_pref + "Y", pref_y);
}
moved = true;
}
winLastX = winPosition.x;
winLastY = winPosition.y;
return moved;
}

View File

@@ -88,6 +88,8 @@ inline double get_ui_scale_map() {
void adjust_window_for_menubar(int mode, unsigned int width, unsigned int height);
bool check_window_moved(sf::RenderWindow& win, int& winLastX, int& winLastY, std::string position_pref = "");
class ModalSession {
void* session;
sf::Window* parent;