Added sliders

This commit is contained in:
Cooldude2606
2019-09-01 17:18:19 +01:00
parent 129610679e
commit 71b0e16bd0
101 changed files with 549 additions and 161 deletions

View File

@@ -682,4 +682,22 @@ function Common.table_insert(tbl,start_index,tbl2)
return tbl
end
--[[-- Used to resolve a value that could also be a function returning that value
@tparam any value the value which you want to test is not nil and if it is a function then call the function
@treturn any the value given or returned by value if it is a function
@usage-- Default value handling
-- if default value is not a function then it is returned
-- if it is a function then it is called with the first argument being self
local value = Common.resolve_value(self.defaut_value,self)
]]
function Common.resolve_value(value,...)
if value then
if type(value) == 'function' then
return value(...)
else
return value
end
end
end
return Common

View File

@@ -73,5 +73,6 @@ Gui.require_concept('checkbox')
Gui.require_concept('dropdown')
Gui.require_concept('elem_button')
Gui.require_concept('progress_bar')
Gui.require_concept('slider')
return Gui

View File

@@ -7,33 +7,42 @@ local Gui = require 'expcore.gui.core'
--[[-- The basic checkbox element
@element checkbox
@param on_state_change fired when the state of the element is changed
@param on_state_changed fired when the state of the element is changed
@tparam ?string|Concepts.LocalisedString caption the message that is shown next to the checkbox
@tparam ?string|Concepts.LocalisedString tooltip the tooltip that shows when a player hovers over the checkbox
@tparam ?boolean|function default the default state of this checkbox, or a function which returns the default state
@tparam boolean use_radio setting to true will use radio buttons rather than checkboxs
@usage-- Making a basic checkbox
local basic_checkbox =
Gui.clone_concept('checkbox','basic_checkbox')
:set_caption('Basic Checkbox')
:set_tooltip('Basic checkbox')
:on_state_change(function(event)
:on_state_changed(function(event)
event.player.print('Basic checkbox is now: '..tostring(event.element.state))
end)
]]
Gui.new_concept('checkbox')
:new_event('on_state_change',defines.events.on_gui_checked_state_changed)
:new_event('on_state_changed',defines.events.on_gui_checked_state_changed)
:new_property('tooltip')
:new_property('caption')
:new_property('default_state',false)
:new_property('default',false)
:new_property('use_radio',false)
:define_draw(function(properties,parent,element)
local default = properties.default
local state = type(default) == 'boolean' and default
element = parent.add{
name = properties.name,
type = properties.use_radio and 'radiobutton' or 'checkbox',
caption = properties.caption,
tooltip = properties.tooltip,
state = properties.default_state
state = state
}
default = Gui.resolve_property(default,element)
if default and default ~= state then
element.state = default
end
return element
end)

View File

@@ -8,8 +8,8 @@ local array_insert = ext_require('expcore.common','array_insert')
--[[-- The basic dropdown element
@element dropdown
@param on_selection_change fired when the selected value is changed
@tparam ?string|Concepts.LocalisedString|function default_selection the option which is selected by default, or a function which returns the default
@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
@@ -17,7 +17,7 @@ local array_insert = ext_require('expcore.common','array_insert')
local static_dropdown =
Gui.clone_concept('dropdown','static_dropdown')
:set_static_items{'Option 1','Option 2','Option 3'}
:on_selection_change(function(event)
:on_selection_changed(function(event)
local value = Gui.get_dropdown_value(event.element)
event.player.print('Static dropdown is now: '..value)
end)
@@ -31,14 +31,14 @@ Gui.clone_concept('dropdown','dynamic_dropdown')
end
return items
end)
:on_selection_change(function(event)
:on_selection_changed(function(event)
local value = Gui.get_dropdown_value(event.element)
event.player.print('Dynamic dropdown is now: '..value)
end)
]]
Gui.new_concept('dropdown')
:new_event('on_selection_change',defines.events.on_gui_selection_state_changed)
:new_property('default_selection')
:new_event('on_selection_changed',defines.events.on_gui_selection_state_changed)
:new_property('default')
:new_property('use_list_box',false)
:new_property('static_items',nil,function(properties,value,start_index)
if not value then
@@ -87,12 +87,8 @@ end)
end
end
if properties.default_selection then
local default = properties.default_selection
if type(default) == 'function' then
default = default(element)
end
local default = Gui.resolve_property(properties.default,element)
if default then
Gui.set_dropdown_value(element,default)
end

View File

@@ -7,42 +7,30 @@ local Gui = require 'expcore.gui.core'
--[[-- The basic dropdown element
@element elem_button
@param on_selection_change fired when the selected value is changed
@tparam ?string|Concepts.SignalID|function default_selection the option which is selected by default, or a function which returns the default
@param on_selection_changed fired when the selected value is changed
@tparam ?string|Concepts.SignalID|function default the option which is selected by default, or a function which returns the default
@tparam string elem_type the type of elem selection that this is, default is item selection
@usage-- Making a basic elem button
local basic_elem_button =
Gui.clone_concept('elem_button','basic_elembutton')
:on_selection_change(function(event)
:on_selection_changed(function(event)
event.player.print('Basic elem button is now: '..event.element.elem_value)
end)
]]
Gui.new_concept('elem_button')
:new_event('on_selection_change',defines.events.on_gui_elem_changed)
:new_property('default_selection')
:new_event('on_selection_changed',defines.events.on_gui_elem_changed)
:new_property('default')
:new_property('elem_type','item')
:define_draw(function(properties,parent,element,selection)
:define_draw(function(properties,parent,element)
element = parent.add{
name = properties.name,
type = 'choose-elem-button',
elem_type = properties.elem_type
}
if properties.default_selection and not selection then
local default = properties.default_selection
if type(default) == 'function' then
default = default(element)
end
local default = Gui.resolve_property(properties.default,element)
if default then
element.elem_value = default
elseif selection then
if type(selection) == 'function' then
selection = selection(element)
end
element.elem_value = selection
end
return element

View File

@@ -0,0 +1,75 @@
--[[-- Core Module - Gui
@module Gui
@alias Gui
]]
local Gui = require 'expcore.gui.core'
--[[-- The basic slider element
@element slider
@param on_value_changed fired when the value of the slider is changed
@tparam number value_step the minimum amount by which the value of the slider can be changed
@tparam ?number|function default the default value of the slider or a function which returns the default value
@tparam boolean discrete_slider makes this slider a discrete slider (at time of writing unsure what this is)
@tparam ?number|function range accepts two params the minimum and the maximum for this slider, or a single function to return both
@usage-- Making a basic slider
local basic_slider =
Gui.clone_concept('slider','basic_slider')
:set_range(1,10)
:on_value_changed(function(event)
event.player.print('Basic slider is now: '..event.element.slider_value)
end)
@usage-- Making a discrete_slider
local discrete_slider =
Gui.clone_concept('slider','discrete_slider')
:set_range(1,10)
:set_value_step(1)
:set_discrete_slider(true)
:on_value_changed(function(event)
event.player.print('Interval slider is now: '..event.element.slider_value)
end)
]]
Gui.new_concept('slider')
:new_event('on_value_changed',defines.events.on_gui_value_changed)
:new_property('value_step')
:new_property('default')
:new_property('discrete_slider',false)
:new_property('range',nil,function(properties,minimum,maximum)
if type(minimum) == 'function' then
properties.range = minimum
else
properties.minimum = minimum
properties.maximum = maximum
end
end)
:define_draw(function(properties,parent,element)
local default = properties.default
local value = type(default) == 'number' and default
local value_step = properties.value_step
element = parent.add{
name = properties.name,
type = 'slider',
caption = properties.caption,
minimum_value = properties.minimum,
maximum_value = properties.maximum,
discrete_slider = properties.discrete_slider,
discrete_values = value_step ~= nil,
value_step = value_step,
value = value
}
local min, max = Gui.resolve_property(properties.range,element)
if min or max then
min = min or element.get_slider_minimum()
max = max or element.get_slider_maximum()
element.set_slider_minimum_maximum(min,max)
end
default = Gui.resolve_property(default,element)
if default and default ~= value then
element.slider_value = default
end
return element
end)

View File

@@ -4,6 +4,7 @@
]]
local Game = require 'utils.game' -- @dep utils.game
local resolve_value = ext_require('expcore.common','resolve_value') -- @dep expcore.common
local Prototype = require 'expcore.gui.prototype'
local Gui = {
@@ -186,6 +187,18 @@ function Gui.set_padding(element,up,down,left,right)
style.right_padding = right == true and style.top_padding or right or 0
end
--[[ Used to check a property exists and if it is a function then call the function
@tparam any value the value that you are testing exists and call if its a function
@tparam LuaGuiElement element the element that is passed to the function if it is a function
@treturn any the value or what it returns if it is a function
@usage-- Getting the default value
local default = Gui.resolve_property(properties.default,element)
if default then
element.value = default
end
]]
Gui.resolve_property = resolve_value
--- Store Categories.
-- Functions that are common types of categories
-- @section store-categories

View File

@@ -517,12 +517,13 @@ end)
for key,instance in pairs(instances) do
if not instance or not instance.valid then
instances[key] = nil
end
if args then
update_callback(instance,unpack(args))
else
update_callback(instance,...)
if args then
update_callback(instance,unpack(args))
else
update_callback(instance,...)
end
end
end
end
@@ -647,12 +648,12 @@ local custom_button =
Gui.get_concept('checkbox'):clone('my_checkbox')
:set_caption('My Checkbox')
:set_tooltip('Clicking this check box will change it for everyone')
:on_state_change(function(event)
:on_state_changed(function(event)
local element = event.element
event.concept.set_data(element,element.state) -- Update the stored data to trigger an update of all other instances
end)
:define_combined_store(function(element,state) -- We could add a category function here if we wanted to
element.state = state or false -- When you sync an instance this is what is called
element.state = state or false -- Note that the value passed may be nil if there is no stored value and no default set
end)
]]
function Prototype:define_combined_store(category_callback,sync_callback)
@@ -679,8 +680,11 @@ Gui.get_concept('CustomButton')
-- Used internally when first draw and automatically when the store updates
custom_button.sync_instance(element)
]]
local properties = self.properties
function self.sync_instance(element)
sync_callback(element,self.get_data(element))
local default = properties.default
local value = self.get_data(element) or type(default) == 'function' and default(element) or default
sync_callback(element,value)
end
return self

View File

@@ -157,7 +157,7 @@ local basic_checkbox =
Gui.clone_concept('checkbox',TEST 'basic_checkbox')
:set_caption('Basic Checkbox')
:set_tooltip('Basic checkbox')
:on_state_change(function(event)
:on_state_changed(function(event)
event.player.print('Basic checkbox is now: '..tostring(event.element.state))
end)
@@ -165,7 +165,7 @@ local game_checkbox =
Gui.clone_concept('checkbox',TEST 'game_checkbox')
:set_caption('Game Stored Checkbox')
:set_tooltip('Game stored checkbox')
:on_state_change(function(event)
:on_state_changed(function(event)
local element = event.element
event.concept.set_data(element,element.state) -- Update other instances
event.player.print('Game stored checkbox is now: '..tostring(element.state))
@@ -178,7 +178,7 @@ local force_checkbox =
Gui.clone_concept('checkbox',TEST 'force_checkbox')
:set_caption('Force Stored Checkbox')
:set_tooltip('Force stored checkbox')
:on_state_change(function(event)
:on_state_changed(function(event)
local element = event.element
event.concept.set_data(element,element.state) -- Update other instances
event.player.print('Force stored checkbox is now: '..tostring(element.state))
@@ -191,7 +191,7 @@ local player_checkbox =
Gui.clone_concept('checkbox',TEST 'player_checkbox')
:set_caption('Player Stored Checkbox')
:set_tooltip('Player stored checkbox')
:on_state_change(function(event)
:on_state_changed(function(event)
local element = event.element
event.concept.set_data(element,element.state) -- Update other instances
event.player.print('Player stored checkbox is now: '..tostring(element.state))
@@ -218,7 +218,7 @@ Dropdowns
local static_dropdown =
Gui.clone_concept('dropdown',TEST 'static_dropdown')
:set_static_items{'Option 1','Option 2','Option 3'}
:on_selection_change(function(event)
:on_selection_changed(function(event)
local value = Gui.get_dropdown_value(event.element)
event.player.print('Static dropdown is now: '..value)
end)
@@ -234,7 +234,7 @@ Gui.clone_concept('dropdown',TEST 'dynamic_dropdown')
end
return items
end)
:on_selection_change(function(event)
:on_selection_changed(function(event)
local value = Gui.get_dropdown_value(event.element)
event.player.print('Dynamic dropdown is now: '..value)
end)
@@ -242,7 +242,7 @@ end)
local static_player_dropdown =
Gui.clone_concept('dropdown',TEST 'static_player_dropdown')
:set_static_items{'Option 1','Option 2','Option 3'}
:on_selection_change(function(event)
:on_selection_changed(function(event)
local element = event.element
local value = Gui.get_dropdown_value(element)
event.concept.set_data(element,value)
@@ -263,7 +263,7 @@ Gui.clone_concept('dropdown',TEST 'dynamic_player_dropdown')
end
return items
end)
:on_selection_change(function(event)
:on_selection_changed(function(event)
local element = event.element
local value = Gui.get_dropdown_value(element)
event.concept.set_data(element,value)
@@ -290,7 +290,7 @@ local static_listbox =
Gui.clone_concept('dropdown',TEST 'static_listbox')
:set_use_list_box(true)
:set_static_items{'Option 1','Option 2','Option 3'}
:on_selection_change(function(event)
:on_selection_changed(function(event)
local value = Gui.get_dropdown_value(event.element)
event.player.print('Static listbox is now: '..value)
end)
@@ -299,7 +299,7 @@ local static_player_listbox =
Gui.clone_concept('dropdown',TEST 'static_player_listbox')
:set_use_list_box(true)
:set_static_items{'Option 1','Option 2','Option 3'}
:on_selection_change(function(event)
:on_selection_changed(function(event)
local element = event.element
local value = Gui.get_dropdown_value(element)
event.concept.set_data(element,value)
@@ -323,15 +323,15 @@ Elem Buttons
local basic_elem_button =
Gui.clone_concept('elem_button',TEST 'basic_elembutton')
:on_selection_change(function(event)
:on_selection_changed(function(event)
event.player.print('Basic elem button is now: '..event.element.elem_value)
end)
local default_selection_elem_button =
Gui.clone_concept('elem_button',TEST 'default_selection_elem_button')
:set_elem_type('signal')
:set_default_selection{type='virtual',name='signal-info'}
:on_selection_change(function(event)
:set_default{type='virtual',name='signal-info'}
:on_selection_changed(function(event)
local value = event.element.elem_value
event.player.print('Default selection elem button is now: '..value.type..'/'..value.name)
end)
@@ -339,7 +339,7 @@ end)
local player_elem_button =
Gui.clone_concept('elem_button',TEST 'player_elem_button')
:set_elem_type('technology')
:on_selection_change(function(event)
:on_selection_changed(function(event)
local element = event.element
local value = element.elem_value
event.concept.set_data(element,value)
@@ -446,4 +446,72 @@ tests['Progress Bars'] = {
['Game Instance Progress Bar'] = game_progress_bar,
['Force Instance Progress Bar'] = force_instance_progress_bar,
['Force Stored Progress Bar'] = force_stored_progress_bar
}
--[[
Sliders
> Basic Slider -- Just a basic slider with range 1 to 10
> Interval Slider -- Same as above but can only be intergers
> Discrete Slider -- A discrete slider
> Dynamic Slider -- A slider which has a dynamic range
> Player Stored Slider -- Slider which stores the value per player, also goes 1 to 10
]]
local basic_slider =
Gui.clone_concept('slider',TEST 'basic_slider')
:set_range(1,10)
:on_value_changed(function(event)
event.player.print('Basic slider is now: '..event.element.slider_value)
end)
local interval_slider =
Gui.clone_concept('slider',TEST 'interval_slider')
:set_range(1,10)
:set_value_step(1)
:on_value_changed(function(event)
event.player.print('Interval slider is now: '..event.element.slider_value)
end)
local discrete_slider =
Gui.clone_concept('slider',TEST 'discrete_slider')
:set_range(1,10)
:set_value_step(1)
:set_discrete_slider(true)
:on_value_changed(function(event)
event.player.print('Discrete slider is now: '..event.element.slider_value)
end)
local dynamic_slider =
Gui.clone_concept('slider',TEST 'dynamic_slider')
:set_range(function(element)
local player = Gui.get_player_from_element(element)
return 1, player.name:len()
end)
:set_value_step(1)
:set_discrete_slider(true)
:on_value_changed(function(event)
event.player.print('Dynamic slider is now: '..event.element.slider_value)
end)
local player_slider =
Gui.clone_concept('slider',TEST 'player_slider')
:set_range(1,10)
:set_value_step(1)
:set_discrete_slider(true)
:on_value_changed(function(event)
local element = event.element
local value = element.slider_value
event.concept.set_data(element,value)
event.player.print('Player stored slider is now: '..value)
end)
:define_combined_store(Gui.categorize_by_player,function(element,value)
element.slider_value = value or 0
end)
tests.Sliders = {
['Basic Slider'] = basic_slider,
['Interval Slider'] = interval_slider,
['Discrete Slider'] = discrete_slider,
['Dynamic Slider'] = dynamic_slider,
['Player Stored Slider'] = player_slider
}