mirror of
https://github.com/PHIDIAS0303/ExpCluster.git
synced 2025-12-29 20:16:38 +09:00
Cleaner Code
This commit is contained in:
@@ -8,22 +8,26 @@ local array_insert = ext_require('expcore.common','array_insert')
|
||||
|
||||
--[[-- A drop down list of other elements.
|
||||
@element dropdown
|
||||
|
||||
@param on_selection_changed fired when the selected value is changed
|
||||
|
||||
@tparam ?string|Concepts.LocalisedString|function default the option which is selected by default, or a function which returns the default
|
||||
@tparam boolean use_list_box when true a list box will be used rather than a dropdown menu
|
||||
@tparam ?nil|table static_items when called with a table the values will be added as items for the dropdown, if called with nil then all items are cleared
|
||||
@tparam function dynamic_items the given function will be called to return a list of items and optional start index to add items to the dropdown when it is first drawn
|
||||
@usage-- Making a basic dropdown
|
||||
|
||||
@usage-- Making a basic dropdown
|
||||
local static_dropdown =
|
||||
Gui.clone_concept('dropdown','static_dropdown')
|
||||
Gui.new_concept('dropdown')
|
||||
:set_static_items{'Option 1','Option 2','Option 3'}
|
||||
:on_selection_changed(function(event)
|
||||
local value = Gui.get_dropdown_value(event.element)
|
||||
event.player.print('Static dropdown is now: '..value)
|
||||
end)
|
||||
|
||||
@usage-- Making a dropdown with dynamic items, example is name of online players
|
||||
local dynamic_dropdown =
|
||||
Gui.clone_concept('dropdown','dynamic_dropdown')
|
||||
Gui.new_concept('dropdown')
|
||||
:set_dynamic_items(function(element)
|
||||
local items = {}
|
||||
for _,player in pairs(game.connected_players) do
|
||||
@@ -35,51 +39,66 @@ end)
|
||||
local value = Gui.get_dropdown_value(event.element)
|
||||
event.player.print('Dynamic dropdown is now: '..value)
|
||||
end)
|
||||
|
||||
]]
|
||||
Gui.new_concept('dropdown')
|
||||
|
||||
Gui.new_concept()
|
||||
:save_as('dropdown')
|
||||
|
||||
-- Events
|
||||
:new_event('on_selection_changed',defines.events.on_gui_selection_state_changed)
|
||||
|
||||
-- Properties
|
||||
:new_property('default')
|
||||
:new_property('use_list_box',false)
|
||||
:new_property('static_items',nil,function(properties,value,start_index)
|
||||
:new_property('use_list_box',nil,false)
|
||||
:new_property('static_items',function(properties,value,start_index)
|
||||
-- Clear all items if value is nil
|
||||
if not value then
|
||||
properties.items = {}
|
||||
end
|
||||
|
||||
-- Convert value to a table
|
||||
if type(value) ~= 'table' then
|
||||
value = {value}
|
||||
end
|
||||
|
||||
-- If there are no items then set and return
|
||||
local items = properties.items
|
||||
if not items then
|
||||
properties.items = value
|
||||
return
|
||||
end
|
||||
|
||||
-- Otherwise insert into the array
|
||||
array_insert(items,start_index,value)
|
||||
end)
|
||||
:new_property('dynamic_items',nil,function(properties,value)
|
||||
:new_property('dynamic_items',function(properties,value)
|
||||
-- Check that a function value was given
|
||||
if type(value) ~= 'function' then
|
||||
error('Dynamic items must be a function')
|
||||
end
|
||||
|
||||
-- If no dynamic items then set and return
|
||||
local items = properties.dynamic_items
|
||||
if not items then
|
||||
properties.dynamic_items = {value}
|
||||
return
|
||||
end
|
||||
|
||||
-- Otherwise append to the end
|
||||
items[#items+1] = value
|
||||
end)
|
||||
:define_draw(function(properties,parent,element,new_items)
|
||||
local items = new_items or {}
|
||||
array_insert(items,1,properties.items or {})
|
||||
|
||||
-- Draw
|
||||
:define_draw(function(properties,parent,element)
|
||||
-- Draw a dropdown
|
||||
element = parent.add{
|
||||
name = properties.name,
|
||||
type = properties.use_list_box and 'list-box' or 'drop-down',
|
||||
items = items
|
||||
items = properties.items
|
||||
}
|
||||
|
||||
-- If there are dynamic items then add them
|
||||
if properties.dynamic_items then
|
||||
for _,callback in pairs(properties.dynamic_items) do
|
||||
local dynamic_items, start_index = callback(element)
|
||||
@@ -87,6 +106,7 @@ end)
|
||||
end
|
||||
end
|
||||
|
||||
-- If there is a default, select it
|
||||
local default = Gui.resolve_property(properties.default,element)
|
||||
if default then
|
||||
Gui.set_dropdown_value(element,default)
|
||||
|
||||
Reference in New Issue
Block a user