Settings: little style changes

This commit is contained in:
bi 2018-06-23 15:51:23 +03:00
parent 603f78b0c5
commit 49e4d427d4

View file

@ -1,8 +1,8 @@
--[[ --[[
This is the settings manager. Settings manager.
* It operates settings within the mod namespace (you can define settings with the same name for different mods) * Operates settings within the mod namespace (you can define settings with the same name for different mods)
* Settings location: "%AppData%\Roaming\Fatshark\Warhammer End Times Vermintide\user_settings.config" * Settings location: "%AppData%\Roaming\Fatshark\Warhammer End Times Vermintide\user_settings.config"
* All settings are being saved to the settings-file when game state changes, when options menu is closed and on reload * All settings are saved to the settings-file when game state changes, when options menu is closed, and on reload
--]] --]]
local vmf = get_mod("VMF") local vmf = get_mod("VMF")
@ -10,33 +10,30 @@ local _mods_settings = Application.user_setting("mods_settings") or {}
local _there_are_unsaved_changes = false local _there_are_unsaved_changes = false
-- #################################################################################################################### -- #####################################################################################################################
-- ##### Local functions ############################################################################################## -- ##### Local functions ###############################################################################################
-- #################################################################################################################### -- #####################################################################################################################
local function save_all_settings() local function save_all_settings()
if _there_are_unsaved_changes then if _there_are_unsaved_changes then
Application.set_user_setting("mods_settings", _mods_settings) Application.set_user_setting("mods_settings", _mods_settings)
Application.save_user_settings() Application.save_user_settings()
_there_are_unsaved_changes = false _there_are_unsaved_changes = false
end end
end end
-- #################################################################################################################### -- #####################################################################################################################
-- ##### VMFMod ####################################################################################################### -- ##### VMFMod ########################################################################################################
-- #################################################################################################################### -- #####################################################################################################################
--[[ --[[
* setting_name [string] : setting name, can contain any characters lua-string can Sets mod's setting to a given value. If setting is used in some option widget, make sure given
* setting_value [anything]: setting value, will be serialized to SJSON format, so you can save whole tables value matches one of the predefined values in this widget.
* setting_name [string] : setting name (can contain any characters lua-string can)
* call_setting_changed_event [bool]: * setting_value [anything]: setting value (can be any SJSON serializable format)
if 'true', when some setting will be changed, 'setting_changed' event will be called (if mod defined one) * notify_mod [bool] : if 'true', calls 'mod.on_setting_changed' event
--]] --]]
VMFMod.set = function (self, setting_name, setting_value, call_setting_changed_event) function VMFMod:set(setting_name, setting_value, notify_mod)
local mod_name = self:get_name() local mod_name = self:get_name()
if not _mods_settings[mod_name] then if not _mods_settings[mod_name] then
@ -44,40 +41,33 @@ VMFMod.set = function (self, setting_name, setting_value, call_setting_changed_e
end end
local mod_settings = _mods_settings[mod_name] local mod_settings = _mods_settings[mod_name]
mod_settings[setting_name] = type(setting_value) == "table" and table.clone(setting_value) or setting_value mod_settings[setting_name] = type(setting_value) == "table" and table.clone(setting_value) or setting_value
_there_are_unsaved_changes = true _there_are_unsaved_changes = true
if call_setting_changed_event then if notify_mod then
vmf.mod_setting_changed_event(self, setting_name) vmf.mod_setting_changed_event(self, setting_name)
end end
end end
--[[ --[[
* setting_name [string]: setting name, can contain any characters lua-string can Returns a mod's setting. Don't call this method for table settings very frequently, because tables are cloned on every
call.
* setting_name [string]: setting name (can contain any characters lua-string can)
--]] --]]
VMFMod.get = function (self, setting_name) function VMFMod:get(setting_name)
local mod_name = self:get_name() local mod_name = self:get_name()
local mod_settings = _mods_settings[mod_name] local mod_settings = _mods_settings[mod_name]
local setting_value = mod_settings and mod_settings[setting_name]
local setting_value
if mod_settings then
setting_value = mod_settings[setting_name]
else
return nil
end
return type(setting_value) == "table" and table.clone(setting_value) or setting_value return type(setting_value) == "table" and table.clone(setting_value) or setting_value
end end
-- #################################################################################################################### -- #####################################################################################################################
-- ##### VMF internal functions and variables ######################################################################### -- ##### VMF internal functions and variables ##########################################################################
-- #################################################################################################################### -- #####################################################################################################################
vmf.save_unsaved_settings_to_file = function() function vmf.save_unsaved_settings_to_file()
save_all_settings() save_all_settings()
end end