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

@@ -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)