mirror of
https://github.com/PHIDIAS0303/ExpCluster.git
synced 2025-12-27 11:35:22 +09:00
Added elem buttons
This commit is contained in:
@@ -71,5 +71,6 @@ Gui.require_concept('frame')
|
||||
Gui.require_concept('button')
|
||||
Gui.require_concept('checkbox')
|
||||
Gui.require_concept('dropdown')
|
||||
Gui.require_concept('elem_button')
|
||||
|
||||
return Gui
|
||||
@@ -9,7 +9,7 @@ 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|Conepts.LocalisedString default_selection the option which is selected by default
|
||||
@tparam ?string|Concepts.LocalisedString|function default_selection 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
|
||||
@@ -26,11 +26,9 @@ local dynamic_dropdown =
|
||||
Gui.clone_concept('dropdown','dynamic_dropdown')
|
||||
:set_dynamic_items(function(element)
|
||||
local items = {}
|
||||
|
||||
for _,player in pairs(game.connected_players) do
|
||||
items[#items+1] = player.name
|
||||
end
|
||||
|
||||
return items
|
||||
end)
|
||||
:on_selection_change(function(event)
|
||||
@@ -107,7 +105,7 @@ end)
|
||||
|
||||
--[[-- Selects the index of a dropdown with this value
|
||||
@tparam LuaGuiElement element the dropdown that you want to set the selection for
|
||||
@tparam ?string|Conepts.LocalisedString value the value that you want selected
|
||||
@tparam ?string|Concepts.LocalisedString value the value that you want selected
|
||||
@treturn boolean if an item with this value was found
|
||||
@usage-- Selecting the item with the value 'foo'
|
||||
Gui.set_dropdown_value(element,'foo')
|
||||
@@ -124,7 +122,7 @@ end
|
||||
|
||||
--[[-- Gets the selected item value of a dropdown
|
||||
@tparam LuaGuiElement element the dropdown that you want the selected value of
|
||||
@treturn ?string|Conepts.LocalisedString the value that is currently selected
|
||||
@treturn ?string|Concepts.LocalisedString the value that is currently selected
|
||||
@usage-- Getting the selected value
|
||||
local selected_value = Gui.get_dropdown_value(element)
|
||||
]]
|
||||
|
||||
49
expcore/gui/concepts/elem_button.lua
Normal file
49
expcore/gui/concepts/elem_button.lua
Normal file
@@ -0,0 +1,49 @@
|
||||
--[[-- Core Module - Gui
|
||||
@module Gui
|
||||
@alias Gui
|
||||
]]
|
||||
|
||||
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
|
||||
@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',TEST 'basic_elembutton')
|
||||
:on_selection_change(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_property('elem_type','item')
|
||||
:define_draw(function(properties,parent,element,selection)
|
||||
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
|
||||
|
||||
element.elem_value = default
|
||||
|
||||
elseif selection then
|
||||
if type(selection) == 'function' then
|
||||
selection = selection(element)
|
||||
end
|
||||
|
||||
element.elem_value = selection
|
||||
|
||||
end
|
||||
|
||||
return element
|
||||
end)
|
||||
@@ -278,4 +278,79 @@ tests.Dropdowns = {
|
||||
['Dynamic Dropdown'] = dynamic_dropdown,
|
||||
['Static Player Stored Dropdown'] = static_player_dropdown,
|
||||
['Dynamic Player Stored Dropdown'] = dynamic_player_dropdown
|
||||
}
|
||||
|
||||
--[[
|
||||
Listboxs
|
||||
> Static Listbox -- Simple Listbox with all options being static
|
||||
> Static Player Stored Listbox -- Listbox where the values is synced for each player
|
||||
]]
|
||||
|
||||
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)
|
||||
local value = Gui.get_dropdown_value(event.element)
|
||||
event.player.print('Static listbox is now: '..value)
|
||||
end)
|
||||
|
||||
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)
|
||||
local element = event.element
|
||||
local value = Gui.get_dropdown_value(element)
|
||||
event.concept.set_data(element,value)
|
||||
event.player.print('Static player stored listbox is now: '..value)
|
||||
end)
|
||||
:define_combined_store(Gui.categorize_by_player,function(element,value)
|
||||
Gui.set_dropdown_value(element,value)
|
||||
end)
|
||||
|
||||
tests.Listboxs = {
|
||||
['Static Listbox'] = static_listbox,
|
||||
['Static Player Stored Listbox'] = static_player_listbox
|
||||
}
|
||||
|
||||
--[[
|
||||
Elem Buttons
|
||||
> Basic Elem Button -- Basic elem button
|
||||
> Defaut Selection Elem Button -- Same as above but has a default selection
|
||||
> Player Stored Elem Button -- Same as above but is stored per player
|
||||
]]
|
||||
|
||||
local basic_elem_button =
|
||||
Gui.clone_concept('elem_button',TEST 'basic_elembutton')
|
||||
:on_selection_change(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)
|
||||
local value = event.element.elem_value
|
||||
event.player.print('Default selection elem button is now: '..value.type..'/'..value.name)
|
||||
end)
|
||||
|
||||
local player_elem_button =
|
||||
Gui.clone_concept('elem_button',TEST 'player_elem_button')
|
||||
:set_elem_type('technology')
|
||||
:on_selection_change(function(event)
|
||||
local element = event.element
|
||||
local value = element.elem_value
|
||||
event.concept.set_data(element,value)
|
||||
event.player.print('Player stored elem button is now: '..value)
|
||||
end)
|
||||
:define_combined_store(Gui.categorize_by_player,function(element,value)
|
||||
element.elem_value = value
|
||||
end)
|
||||
|
||||
tests['Elem Buttons'] = {
|
||||
['Basic Elem Button'] = basic_elem_button,
|
||||
['Defaut Selection Elem Button'] = default_selection_elem_button,
|
||||
['Player Stored Elem Button'] = player_elem_button
|
||||
}
|
||||
Reference in New Issue
Block a user