[Options] Move legacy code to separate file

This commit is contained in:
Azumgi 2018-09-03 14:41:13 +03:00
parent a0f61e9295
commit adf3b56e31
5 changed files with 186 additions and 195 deletions

View file

@ -0,0 +1,3 @@
local vmf = get_mod("VMF")
vmf.options_widgets_definition = {}

View file

@ -0,0 +1,175 @@
local vmf = get_mod("VMF")
-- @TODO: Copypasted it from vmf_options_view for now. Decide what to do with this
local function build_keybind_string(keys)
local keybind_string = ""
for i, key in ipairs(keys) do
if i == 1 then
keybind_string = keybind_string .. vmf.readable_key_names[key]
else
keybind_string = keybind_string .. " + " .. vmf.readable_key_names[key]
end
end
return keybind_string
end
-- ####################################################################################################################
-- ##### VMFMod #######################################################################################################
-- ####################################################################################################################
vmf.initialize_options_legacy = function (mod, widgets_definition)
local mod_settings_list_widgets_definitions = {}
local new_widget_definition
local new_widget_index
local options_menu_favorite_mods = vmf:get("options_menu_favorite_mods")
local options_menu_collapsed_widgets = vmf:get("options_menu_collapsed_widgets")
local mod_collapsed_widgets = nil
if options_menu_collapsed_widgets then
mod_collapsed_widgets = options_menu_collapsed_widgets[mod:get_name()]
end
-- defining header widget
new_widget_index = 1
new_widget_definition = {}
new_widget_definition.widget_type = "header"
new_widget_definition.widget_index = new_widget_index
new_widget_definition.mod_name = mod:get_name()
new_widget_definition.readable_mod_name = mod:get_readable_name()
new_widget_definition.tooltip = mod:get_description()
new_widget_definition.default = true
new_widget_definition.is_mod_toggable = mod:get_internal_data("is_togglable") and
not mod:get_internal_data("is_mutator")
if mod_collapsed_widgets then
new_widget_definition.is_widget_collapsed = mod_collapsed_widgets[mod:get_name()]
end
if options_menu_favorite_mods then
for _, current_mod_name in pairs(options_menu_favorite_mods) do
if current_mod_name == mod:get_name() then
new_widget_definition.is_favorited = true
break
end
end
end
table.insert(mod_settings_list_widgets_definitions, new_widget_definition)
-- defining its subwidgets
if widgets_definition then
local level = 1
local parent_number = new_widget_index
local parent_widget = {["widget_type"] = "header", ["sub_widgets"] = widgets_definition}
local current_widget = widgets_definition[1]
local current_widget_index = 1
local parent_number_stack = {}
local parent_widget_stack = {}
local current_widget_index_stack = {}
while new_widget_index <= 256 do
-- if 'nil', we reached the end of the current level widgets list and need to go up
if current_widget then
new_widget_index = new_widget_index + 1
new_widget_definition = {}
new_widget_definition.widget_type = current_widget.widget_type -- all
new_widget_definition.widget_index = new_widget_index -- all [gen]
new_widget_definition.widget_level = level -- all [gen]
new_widget_definition.mod_name = mod:get_name() -- all [gen]
new_widget_definition.setting_name = current_widget.setting_name -- all
new_widget_definition.text = current_widget.text -- all
new_widget_definition.tooltip = current_widget.tooltip and (current_widget.text .. "\n" ..
current_widget.tooltip) -- all [optional]
new_widget_definition.unit_text = current_widget.unit_text -- numeric [optional]
new_widget_definition.range = current_widget.range -- numeric
new_widget_definition.decimals_number = current_widget.decimals_number -- numeric [optional]
new_widget_definition.options = current_widget.options -- dropdown
new_widget_definition.default_value = current_widget.default_value -- all
new_widget_definition.action = current_widget.action -- keybind [optional?]
new_widget_definition.show_widget_condition = current_widget.show_widget_condition -- all
new_widget_definition.parent_widget_number = parent_number -- all [gen]
if mod_collapsed_widgets then
new_widget_definition.is_widget_collapsed = mod_collapsed_widgets[current_widget.setting_name]
end
if type(mod:get(current_widget.setting_name)) == "nil" then
mod:set(current_widget.setting_name, current_widget.default_value)
end
if current_widget.widget_type == "keybind" then
local keybind = mod:get(current_widget.setting_name)
new_widget_definition.keybind_text = build_keybind_string(keybind)
if current_widget.action then
mod:keybind(current_widget.setting_name, current_widget.action, keybind)
end
end
table.insert(mod_settings_list_widgets_definitions, new_widget_definition)
end
if current_widget and (
current_widget.widget_type == "header" or
current_widget.widget_type == "group" or
current_widget.widget_type == "checkbox" or
current_widget.widget_type == "dropdown"
) and current_widget.sub_widgets then
-- going down to the first subwidget
level = level + 1
table.insert(parent_number_stack, parent_number)
parent_number = new_widget_index
table.insert(parent_widget_stack, parent_widget)
parent_widget = current_widget
table.insert(current_widget_index_stack, current_widget_index)
current_widget_index = 1
current_widget = current_widget.sub_widgets[1]
else
current_widget_index = current_widget_index + 1
if parent_widget.sub_widgets[current_widget_index] then
-- going to the next widget
current_widget = parent_widget.sub_widgets[current_widget_index]
else
-- going up to the widget next to the parent one
level = level - 1
parent_number = table.remove(parent_number_stack)
parent_widget = table.remove(parent_widget_stack)
current_widget_index = table.remove(current_widget_index_stack)
if not current_widget_index then
break
end
current_widget_index = current_widget_index + 1
-- widget next to parent one, or 'nil', if there are no more widgets on this level
current_widget = parent_widget.sub_widgets[current_widget_index]
end
end
end
if new_widget_index == 257 then
mod:error("(vmf_options_view) The limit of 256 options widgets was reached. You can't add any more widgets.")
end
end
table.insert(vmf.options_widgets_definition, mod_settings_list_widgets_definitions)
end

View file

@ -2754,8 +2754,6 @@ end
--╚██████╗███████╗██║ ██║███████║███████║
-- ╚═════╝╚══════╝╚═╝ ╚═╝╚══════╝╚══════╝
local SETTINGS_LIST_WIDGETS_DEFINITIONS = {} -- numerical sorting [ipairs]
local _DEFAULT_SCROLL_STEP = 40
local _SCROLL_STEP
@ -2785,7 +2783,7 @@ VMFOptionsView.init = function (self, ingame_ui_context)
self.definitions.scenegraph = scenegraph_definition
self.definitions.scenegraph_2nd_layer = {}
self.definitions.menu_widgets = menu_widgets_definition
self.definitions.settings_list_widgets = SETTINGS_LIST_WIDGETS_DEFINITIONS
self.definitions.settings_list_widgets = vmf.options_widgets_definition
-- get necessary things for the rendering
self.ui_renderer = ingame_ui_context.ui_renderer
@ -4269,196 +4267,6 @@ end
vmf.load_vmf_options_view_settings()
-- ####################################################################################################################
-- ##### VMFMod #######################################################################################################
-- ####################################################################################################################
vmf.create_options = function (mod, widgets_definition)
local mod_settings_list_widgets_definitions = {}
local new_widget_definition
local new_widget_index
local options_menu_favorite_mods = vmf:get("options_menu_favorite_mods")
local options_menu_collapsed_widgets = vmf:get("options_menu_collapsed_widgets")
local mod_collapsed_widgets = nil
if options_menu_collapsed_widgets then
mod_collapsed_widgets = options_menu_collapsed_widgets[mod:get_name()]
end
-- defining header widget
new_widget_index = 1
new_widget_definition = {}
new_widget_definition.widget_type = "header"
new_widget_definition.widget_index = new_widget_index
new_widget_definition.mod_name = mod:get_name()
new_widget_definition.readable_mod_name = mod:get_readable_name()
new_widget_definition.tooltip = mod:get_description()
new_widget_definition.default = true
new_widget_definition.is_mod_toggable = mod:get_internal_data("is_togglable") and not mod:get_internal_data("is_mutator")
if mod_collapsed_widgets then
new_widget_definition.is_widget_collapsed = mod_collapsed_widgets[mod:get_name()]
end
if options_menu_favorite_mods then
for _, current_mod_name in pairs(options_menu_favorite_mods) do
if current_mod_name == mod:get_name() then
new_widget_definition.is_favorited = true
break
end
end
end
table.insert(mod_settings_list_widgets_definitions, new_widget_definition)
-- defining its subwidgets
if widgets_definition then
local level = 1
local parent_number = new_widget_index
local parent_widget = {["widget_type"] = "header", ["sub_widgets"] = widgets_definition}
local current_widget = widgets_definition[1]
local current_widget_index = 1
local parent_number_stack = {}
local parent_widget_stack = {}
local current_widget_index_stack = {}
while new_widget_index <= 256 do
-- if 'nil', we reached the end of the current level widgets list and need to go up
if current_widget then
new_widget_index = new_widget_index + 1
new_widget_definition = {}
new_widget_definition.widget_type = current_widget.widget_type -- all
new_widget_definition.widget_index = new_widget_index -- all [gen]
new_widget_definition.widget_level = level -- all [gen]
new_widget_definition.mod_name = mod:get_name() -- all [gen]
new_widget_definition.setting_name = current_widget.setting_name -- all
new_widget_definition.text = current_widget.text -- all
new_widget_definition.tooltip = current_widget.tooltip and (current_widget.text .. "\n" ..
current_widget.tooltip) -- all [optional]
new_widget_definition.unit_text = current_widget.unit_text -- numeric [optional]
new_widget_definition.range = current_widget.range -- numeric
new_widget_definition.decimals_number = current_widget.decimals_number -- numeric [optional]
new_widget_definition.options = current_widget.options -- dropdown
new_widget_definition.default_value = current_widget.default_value -- all
new_widget_definition.action = current_widget.action -- keybind [optional?]
new_widget_definition.show_widget_condition = current_widget.show_widget_condition -- all
new_widget_definition.parent_widget_number = parent_number -- all [gen]
if mod_collapsed_widgets then
new_widget_definition.is_widget_collapsed = mod_collapsed_widgets[current_widget.setting_name]
end
if type(mod:get(current_widget.setting_name)) == "nil" then
mod:set(current_widget.setting_name, current_widget.default_value)
end
if current_widget.widget_type == "keybind" then
local keybind = mod:get(current_widget.setting_name)
new_widget_definition.keybind_text = build_keybind_string(keybind)
if current_widget.action then
mod:keybind(current_widget.setting_name, current_widget.action, keybind)
end
end
table.insert(mod_settings_list_widgets_definitions, new_widget_definition)
end
if current_widget and (
current_widget.widget_type == "header" or
current_widget.widget_type == "group" or
current_widget.widget_type == "checkbox" or
current_widget.widget_type == "dropdown"
) and current_widget.sub_widgets then
-- going down to the first subwidget
level = level + 1
table.insert(parent_number_stack, parent_number)
parent_number = new_widget_index
table.insert(parent_widget_stack, parent_widget)
parent_widget = current_widget
table.insert(current_widget_index_stack, current_widget_index)
current_widget_index = 1
current_widget = current_widget.sub_widgets[1]
else
current_widget_index = current_widget_index + 1
if parent_widget.sub_widgets[current_widget_index] then
-- going to the next widget
current_widget = parent_widget.sub_widgets[current_widget_index]
else
-- going up to the widget next to the parent one
level = level - 1
parent_number = table.remove(parent_number_stack)
parent_widget = table.remove(parent_widget_stack)
current_widget_index = table.remove(current_widget_index_stack)
if not current_widget_index then
break
end
current_widget_index = current_widget_index + 1
-- widget next to parent one, or 'nil', if there are no more widgets on this level
current_widget = parent_widget.sub_widgets[current_widget_index]
end
end
end
if new_widget_index == 257 then
mod:error("(vmf_options_view) The limit of 256 options widgets was reached. You can't add any more widgets.")
end
end
table.insert(SETTINGS_LIST_WIDGETS_DEFINITIONS, mod_settings_list_widgets_definitions)
end
if type(vmf:get("options_menu_favorite_mods")) ~= "table" then
vmf:set("options_menu_favorite_mods", {})
end

View file

@ -120,8 +120,11 @@ function vmf.initialize_mod_data(mod, mod_data)
vmf.register_mod_as_mutator(mod, mod_data.mutator_settings)
end
if mod_data.options_widgets or (mod_data.is_togglable and not mod_data.is_mutator) then
vmf.create_options(mod, mod_data.options_widgets)
if mod_data.options then
vmf.initialize_options(mod, mod_data.options_widgets)
-- @TODO: move the 2nd block to the upper statement
elseif mod_data.options_widgets or (mod_data.is_togglable and not mod_data.is_mutator) then
vmf.initialize_options_legacy(mod, mod_data.options_widgets)
end
if type(mod_data.custom_gui_textures) == "table" then

View file

@ -26,6 +26,8 @@ function vmf_mod_object:init()
dofile("scripts/mods/vmf/modules/core/keybindings")
dofile("scripts/mods/vmf/modules/core/chat")
dofile("scripts/mods/vmf/modules/core/localization")
dofile("scripts/mods/vmf/modules/core/options")
dofile("scripts/mods/vmf/modules/legacy/options")
dofile("scripts/mods/vmf/modules/core/network")
dofile("scripts/mods/vmf/modules/core/commands")
dofile("scripts/mods/vmf/modules/gui/custom_textures")