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

@@ -2749,24 +2749,26 @@ static WindowDesc _build_vehicle_desc(
); );
static WindowDesc _build_template_vehicle_desc( static WindowDesc _build_template_vehicle_desc(
WDP_AUTO, "build_vehicle", 240, 268, WDP_AUTO, nullptr, 240, 268,
WC_BUILD_VIRTUAL_TRAIN, WC_CREATE_TEMPLATE, WC_BUILD_VIRTUAL_TRAIN, WC_CREATE_TEMPLATE,
WDF_CONSTRUCTION, WDF_CONSTRUCTION,
_nested_build_vehicle_widgets, lengthof(_nested_build_vehicle_widgets) _nested_build_vehicle_widgets, lengthof(_nested_build_vehicle_widgets),
nullptr, &_build_vehicle_desc
); );
static WindowDesc _build_vehicle_desc_train_advanced( static WindowDesc _build_vehicle_desc_train_advanced(
WDP_AUTO, "build_vehicle", 480, 268, WDP_AUTO, "build_vehicle_dual", 480, 268,
WC_BUILD_VEHICLE, WC_NONE, WC_BUILD_VEHICLE, WC_NONE,
WDF_CONSTRUCTION, WDF_CONSTRUCTION,
_nested_build_vehicle_widgets_train_advanced, lengthof(_nested_build_vehicle_widgets_train_advanced) _nested_build_vehicle_widgets_train_advanced, lengthof(_nested_build_vehicle_widgets_train_advanced)
); );
static WindowDesc _build_template_vehicle_desc_advanced( static WindowDesc _build_template_vehicle_desc_advanced(
WDP_AUTO, "build_vehicle", 480, 268, WDP_AUTO, nullptr, 480, 268,
WC_BUILD_VIRTUAL_TRAIN, WC_CREATE_TEMPLATE, WC_BUILD_VIRTUAL_TRAIN, WC_CREATE_TEMPLATE,
WDF_CONSTRUCTION, WDF_CONSTRUCTION,
_nested_build_vehicle_widgets_train_advanced, lengthof(_nested_build_vehicle_widgets_train_advanced) _nested_build_vehicle_widgets_train_advanced, lengthof(_nested_build_vehicle_widgets_train_advanced),
nullptr, &_build_vehicle_desc_train_advanced
); );

View File

@@ -832,7 +832,7 @@ static void IniSaveSettingList(IniFile &ini, const char *grpname, StringList &li
* Load a WindowDesc from config. * Load a WindowDesc from config.
* @param ini IniFile handle to the ini file with the source data * @param ini IniFile handle to the ini file with the source data
* @param grpname character string identifying the section-header of the ini file that will be parsed * @param grpname character string identifying the section-header of the ini file that will be parsed
* @param desc Destination WindowDesc * @param desc Destination WindowDescPreferences
*/ */
void IniLoadWindowSettings(IniFile &ini, const char *grpname, void *desc) void IniLoadWindowSettings(IniFile &ini, const char *grpname, void *desc)
{ {
@@ -843,7 +843,7 @@ void IniLoadWindowSettings(IniFile &ini, const char *grpname, void *desc)
* Save a WindowDesc to config. * Save a WindowDesc to config.
* @param ini IniFile handle to the ini file where the destination data is saved * @param ini IniFile handle to the ini file where the destination data is saved
* @param grpname character string identifying the section-header of the ini file * @param grpname character string identifying the section-header of the ini file
* @param desc Source WindowDesc * @param desc Source WindowDescPreferences
*/ */
void IniSaveWindowSettings(IniFile &ini, const char *grpname, void *desc) void IniSaveWindowSettings(IniFile &ini, const char *grpname, void *desc)
{ {

View File

@@ -13,11 +13,11 @@ static const SettingTable _window_settings{
[post-amble] [post-amble]
}; };
[templates] [templates]
SDT_BOOL = SDT_BOOL(WindowDesc, $var, $flags, $def, $str, $strhelp, $strval, $pre_cb, $post_cb, $from, $to, $extver, $cat, $guiproc, $startup, nullptr), SDT_BOOL = SDT_BOOL(WindowDescPreferences, $var, $flags, $def, $str, $strhelp, $strval, $pre_cb, $post_cb, $from, $to, $extver, $cat, $guiproc, $startup, nullptr),
SDT_VAR = SDT_VAR(WindowDesc, $var, $type, $flags, $def, $min, $max, $interval, $str, $strhelp, $strval, $pre_cb, $post_cb, $from, $to, $extver, $cat, $guiproc, $startup, nullptr), SDT_VAR = SDT_VAR(WindowDescPreferences, $var, $type, $flags, $def, $min, $max, $interval, $str, $strhelp, $strval, $pre_cb, $post_cb, $from, $to, $extver, $cat, $guiproc, $startup, nullptr),
[validation] [validation]
SDT_VAR = static_assert($max <= MAX_$type, "Maximum value for WindowDesc.$var exceeds storage size"); SDT_VAR = static_assert($max <= MAX_$type, "Maximum value for WindowDescPreferences.$var exceeds storage size");
[defaults] [defaults]
flags = SF_NOT_IN_SAVE | SF_NO_NETWORK_SYNC flags = SF_NOT_IN_SAVE | SF_NO_NETWORK_SYNC

View File

@@ -96,7 +96,7 @@ std::string _windows_file;
/** Window description constructor. */ /** Window description constructor. */
WindowDesc::WindowDesc(WindowPosition def_pos, const char *ini_key, int16 def_width_trad, int16 def_height_trad, 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, 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), default_pos(def_pos),
cls(window_class), cls(window_class),
parent_cls(parent_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_parts(nwid_parts),
nwid_length(nwid_length), nwid_length(nwid_length),
hotkeys(hotkeys), hotkeys(hotkeys),
pref_sticky(false), ini_parent(ini_parent),
pref_width(0), prefs({ false, 0, 0 }),
pref_height(0),
default_width_trad(def_width_trad), default_width_trad(def_width_trad),
default_height_trad(def_height_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)); _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. * Determine default width of window.
* This is either a stored user preferred size, or the built-in default. * This is either a stored user preferred size, or the built-in default.
@@ -127,7 +131,8 @@ WindowDesc::~WindowDesc()
*/ */
int16 WindowDesc::GetDefaultWidth() const 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 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); ini.LoadFromDisk(_windows_file, NO_DIRECTORY);
for (WindowDesc *wd : *_window_descs) { for (WindowDesc *wd : *_window_descs) {
if (wd->ini_key == nullptr) continue; 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); ini.LoadFromDisk(_windows_file, NO_DIRECTORY);
for (WindowDesc *wd : *_window_descs) { for (WindowDesc *wd : *_window_descs) {
if (wd->ini_key == nullptr) continue; if (wd->ini_key == nullptr) continue;
IniSaveWindowSettings(ini, wd->ini_key, wd); IniSaveWindowSettings(ini, wd->ini_key, &(wd->prefs));
} }
ini.SaveToDisk(_windows_file); ini.SaveToDisk(_windows_file);
} }
@@ -185,10 +191,10 @@ void WindowDesc::SaveToConfig()
void Window::ApplyDefaults() void Window::ApplyDefaults()
{ {
if (this->nested_root != nullptr && this->nested_root->GetWidgetOfType(WWT_STICKYBOX) != nullptr) { 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 { } else {
/* There is no stickybox; clear the preference in case someone tried to be funny */ /* 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: { case WWT_DEFSIZEBOX: {
if (_ctrl_pressed) { if (_ctrl_pressed) {
w->window_desc->pref_width = w->width; w->window_desc->GetPreferences().pref_width = w->width;
w->window_desc->pref_height = w->height; w->window_desc->GetPreferences().pref_height = w->height;
} else { } else {
int16 def_width = std::max<int16>(std::min<int16>(w->window_desc->GetDefaultWidth(), _screen.width), w->nested_root->smallest_x); 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); 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: case WWT_STICKYBOX:
w->flags ^= WF_STICKY; w->flags ^= WF_STICKY;
nw->SetDirty(w); 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; return;
default: default:

View File

@@ -173,6 +173,12 @@ Point GetToolbarAlignedWindowPosition(int window_width);
struct HotkeyList; struct HotkeyList;
struct WindowDescPreferences {
bool pref_sticky; ///< Preferred stickyness.
int16 pref_width; ///< User-preferred width of the window. Zero if unset.
int16 pref_height; ///< User-preferred height of the window. Zero if unset.
};
/** /**
* High level window description * High level window description
*/ */
@@ -180,7 +186,7 @@ struct WindowDesc {
WindowDesc(WindowPosition default_pos, const char *ini_key, int16 def_width_trad, int16 def_height_trad, WindowDesc(WindowPosition default_pos, const char *ini_key, int16 def_width_trad, int16 def_height_trad,
WindowClass window_class, WindowClass parent_class, uint32 flags, WindowClass window_class, WindowClass parent_class, uint32 flags,
const NWidgetPart *nwid_parts, int16 nwid_length, HotkeyList *hotkeys = nullptr); const NWidgetPart *nwid_parts, int16 nwid_length, HotkeyList *hotkeys = nullptr, WindowDesc *ini_parent = nullptr);
~WindowDesc(); ~WindowDesc();
@@ -192,10 +198,12 @@ struct WindowDesc {
const NWidgetPart *nwid_parts; ///< Nested widget parts describing the window. const NWidgetPart *nwid_parts; ///< Nested widget parts describing the window.
int16 nwid_length; ///< Length of the #nwid_parts array. int16 nwid_length; ///< Length of the #nwid_parts array.
HotkeyList *hotkeys; ///< Hotkeys for the window. HotkeyList *hotkeys; ///< Hotkeys for the window.
WindowDesc *ini_parent; ///< Other window desc to use for WindowDescPreferences.
bool pref_sticky; ///< Preferred stickyness. WindowDescPreferences prefs; ///< Preferences for this window
int16 pref_width; ///< User-preferred width of the window. Zero if unset.
int16 pref_height; ///< User-preferred height of the window. Zero if unset. const WindowDescPreferences &GetPreferences() const;
WindowDescPreferences &GetPreferences() { return const_cast<WindowDescPreferences &>(const_cast<const WindowDesc*>(this)->GetPreferences()); }
int16 GetDefaultWidth() const; int16 GetDefaultWidth() const;
int16 GetDefaultHeight() const; int16 GetDefaultHeight() const;