Fix window preference save/load of build vehicle windows

This commit is contained in:
Jonathan G Rennison
2022-01-23 13:58:34 +00:00
parent 2e7f8d2e1e
commit 96d789dfbf
5 changed files with 43 additions and 27 deletions

View File

@@ -96,7 +96,7 @@ std::string _windows_file;
/** Window description constructor. */
WindowDesc::WindowDesc(WindowPosition def_pos, const char *ini_key, int16 def_width_trad, int16 def_height_trad,
WindowClass window_class, WindowClass parent_class, uint32 flags,
const NWidgetPart *nwid_parts, int16 nwid_length, HotkeyList *hotkeys) :
const NWidgetPart *nwid_parts, int16 nwid_length, HotkeyList *hotkeys, WindowDesc *ini_parent) :
default_pos(def_pos),
cls(window_class),
parent_cls(parent_class),
@@ -105,9 +105,8 @@ WindowDesc::WindowDesc(WindowPosition def_pos, const char *ini_key, int16 def_wi
nwid_parts(nwid_parts),
nwid_length(nwid_length),
hotkeys(hotkeys),
pref_sticky(false),
pref_width(0),
pref_height(0),
ini_parent(ini_parent),
prefs({ false, 0, 0 }),
default_width_trad(def_width_trad),
default_height_trad(def_height_trad)
{
@@ -120,6 +119,11 @@ WindowDesc::~WindowDesc()
_window_descs->erase(std::find(_window_descs->begin(), _window_descs->end(), this));
}
const WindowDescPreferences &WindowDesc::GetPreferences() const
{
return this->ini_parent != nullptr ? this->ini_parent->prefs : this->prefs;
}
/**
* Determine default width of window.
* This is either a stored user preferred size, or the built-in default.
@@ -127,7 +131,8 @@ WindowDesc::~WindowDesc()
*/
int16 WindowDesc::GetDefaultWidth() const
{
return this->pref_width != 0 ? this->pref_width : ScaleGUITrad(this->default_width_trad);
const WindowDescPreferences &prefs = this->GetPreferences();
return prefs.pref_width != 0 ? prefs.pref_width : ScaleGUITrad(this->default_width_trad);
}
/**
@@ -137,7 +142,8 @@ int16 WindowDesc::GetDefaultWidth() const
*/
int16 WindowDesc::GetDefaultHeight() const
{
return this->pref_height != 0 ? this->pref_height : ScaleGUITrad(this->default_height_trad);
const WindowDescPreferences &prefs = this->GetPreferences();
return prefs.pref_height != 0 ? prefs.pref_height : ScaleGUITrad(this->default_height_trad);
}
/**
@@ -149,7 +155,7 @@ void WindowDesc::LoadFromConfig()
ini.LoadFromDisk(_windows_file, NO_DIRECTORY);
for (WindowDesc *wd : *_window_descs) {
if (wd->ini_key == nullptr) continue;
IniLoadWindowSettings(ini, wd->ini_key, wd);
IniLoadWindowSettings(ini, wd->ini_key, &(wd->prefs));
}
}
@@ -174,7 +180,7 @@ void WindowDesc::SaveToConfig()
ini.LoadFromDisk(_windows_file, NO_DIRECTORY);
for (WindowDesc *wd : *_window_descs) {
if (wd->ini_key == nullptr) continue;
IniSaveWindowSettings(ini, wd->ini_key, wd);
IniSaveWindowSettings(ini, wd->ini_key, &(wd->prefs));
}
ini.SaveToDisk(_windows_file);
}
@@ -185,10 +191,10 @@ void WindowDesc::SaveToConfig()
void Window::ApplyDefaults()
{
if (this->nested_root != nullptr && this->nested_root->GetWidgetOfType(WWT_STICKYBOX) != nullptr) {
if (this->window_desc->pref_sticky) this->flags |= WF_STICKY;
if (this->window_desc->GetPreferences().pref_sticky) this->flags |= WF_STICKY;
} else {
/* There is no stickybox; clear the preference in case someone tried to be funny */
this->window_desc->pref_sticky = false;
this->window_desc->prefs.pref_sticky = false;
}
}
@@ -741,8 +747,8 @@ static void DispatchLeftClickEvent(Window *w, int x, int y, int click_count)
case WWT_DEFSIZEBOX: {
if (_ctrl_pressed) {
w->window_desc->pref_width = w->width;
w->window_desc->pref_height = w->height;
w->window_desc->GetPreferences().pref_width = w->width;
w->window_desc->GetPreferences().pref_height = w->height;
} else {
int16 def_width = std::max<int16>(std::min<int16>(w->window_desc->GetDefaultWidth(), _screen.width), w->nested_root->smallest_x);
int16 def_height = std::max<int16>(std::min<int16>(w->window_desc->GetDefaultHeight(), _screen.height - 50), w->nested_root->smallest_y);
@@ -774,7 +780,7 @@ static void DispatchLeftClickEvent(Window *w, int x, int y, int click_count)
case WWT_STICKYBOX:
w->flags ^= WF_STICKY;
nw->SetDirty(w);
if (_ctrl_pressed) w->window_desc->pref_sticky = (w->flags & WF_STICKY) != 0;
if (_ctrl_pressed) w->window_desc->GetPreferences().pref_sticky = (w->flags & WF_STICKY) != 0;
return;
default: