Cleaner Code

This commit is contained in:
Cooldude2606
2019-09-22 17:08:43 +01:00
parent 1f204c6dac
commit ce88e0a296
114 changed files with 951 additions and 583 deletions

View File

@@ -640,7 +640,7 @@ function Common.array_insert(tbl,start_index,values)
local starting_length = #tbl
local adding_length = #values
local move_to = start_index+adding_length+1
for offset = 0, starting_length-start_index do
for offset = starting_length-start_index, 0, -1 do
tbl[move_to+offset] = tbl[starting_length+offset]
end
start_index = start_index-1

View File

@@ -4,22 +4,24 @@
@usage-- Making the base button concept
local button =
Gui.new_concept('Button')
:new_event('on_click',defines.events.on_gui_click)
:new_property('tooltip')
:new_property('caption',nil,function(properties,value)
Gui.new_concept() -- Make a new empty concept
:save_as('button') -- Save it as Gui.concepts.button so it can be used in other files
:new_event('on_click',defines.events.on_gui_click) -- Add an on click event for this concept
:new_property('tooltip') -- Add a property with the default setter method called tooltip
:new_property('caption',function(properties,value) -- Add a property with a custom setter method called caption
properties.caption = value
properties.sprite = nil
properties.type = 'button'
end)
:new_property('sprite',nil,function(properties,value)
:new_property('sprite',function(properties,value) -- Add a property with a custom setter method called sprite
properties.image = value
properties.caption = nil
properties.type = 'sprite-button'
end)
:define_draw(function(properties,parent,element)
-- Note that element might be nil if this is the first draw function
-- in this case button is a new concept so we know this is the first function and element is nil
:define_draw(function(properties,parent,element) -- Add the draw function to create the element from the concept
-- Properties will include all the information that you need to draw the element
-- Parent is the parent element for the element, this may have been altered by previous draw functions
-- Element is the current element being made, this may have a nil value, if it is nil then this is the first draw function
if properties.type == 'button' then
element = parent.add{
type = properties.type,
@@ -38,26 +40,27 @@ end)
end
-- We must return the element or what we want to be seen as the instance, this is so other draw functions have access to it
-- for example if our custom button defined a draw function to change the font color to red
return element
-- If you return element or parent then their values will be updated for the next draw function in the chain
-- It is best practice to always return the values if you have made any changes to them
return element, parent
end)
@usage-- Makeing a alternative button based on the first
@usage-- Making a new button which has a custom style
local custom_button =
button:clone('CustomButton')
:new_event('on_admin_clicked',defines.events.on_gui_click,function(event)
return event.player.admin -- only raise custom event when an admin clicks the button
end)
:set_caption('Custom Button')
:set_tooltip('Only admins can press this button')
:on_click(function(event)
Gui.new_concept('button') -- We can use button here since we used save as on the concept
-- button:clone() -- If we had not used save as then this is how we would use it as a base
:set_caption('Custom Button') -- Set the caption of the concept, this is possible as we added caption as a property
:set_tooltip('Only admins can press this button') -- Set the tooltip of the concept, this is possible as we added tooltip as a property
:on_click(function(event) -- Register a handler to the click event we added with new event
if not event.player.admin then
event.player.print('You must be admin to use this button')
end
end)
:on_admin_clicked(function(event)
-- Yes i know this can just be an if else but its an example
:new_event('on_admin_clicked',defines.events.on_gui_click,function(event) -- Add a click event which has a filter function
return event.player.admin -- Check if the player is admin
end)
:on_admin_clicked(function(event) -- Register a handler to the admin click event we have just created
-- The admin click event is only an example, because of how sinmple the filter is we could have just used an if else statement
game.print(event.player.name..' pressed my admin button')
end)
@@ -65,23 +68,4 @@ end)
custom_button:draw(game.player.gui.left)
]]
local Gui = require 'expcore.gui.core'
Gui.require_concept('frame')
Gui.require_concept('flow')
Gui.require_concept('table')
Gui.require_concept('label')
Gui.require_concept('line')
Gui.require_concept('scroll')
Gui.require_concept('empty')
Gui.require_concept('button')
Gui.require_concept('checkbox')
Gui.require_concept('dropdown')
Gui.require_concept('elem_button')
Gui.require_concept('progress_bar')
Gui.require_concept('slider')
Gui.require_concept('textfield')
Gui.require_concept('textbox')
return Gui
return require 'expcore.gui.core'

View File

@@ -7,30 +7,39 @@ local Gui = require 'expcore.gui.core'
--[[-- Clickable elements that fire on_gui_click when clicked.
@element button
@param on_click fired when the player clicks the button
@param on_left_click fired when the player clicks with the left mouse button
@param on_left_click fired when the player clicks with the right mouse button
@tparam ?string|Concepts.LocalisedString caption the message that is shown on the button
@tparam ?string|Concepts.LocalisedString tooltip the tooltip that shows when a player hovers over the button
@tparam SpritePath sprite upto three sprites in the order: default, hovered, clicked
@usage-- Making a basic button
local basic_button =
Gui.clone_concept('button','basic_button')
Gui.new_concept('button')
:set_caption('Basic Button')
:set_tooltip('Basic button')
:on_click(function(event)
event.player.print('You pressed basic button!')
end)
@usage-- Making a sprite button
local sprite_button =
Gui.clone_concept('button','sprite_button')
Gui.new_concept('button')
:set_sprite('utility/warning_icon')
:set_tooltip('Sprite button')
:on_click(function(event)
event.player.print('You pressed sprite button!')
end)
]]
Gui.new_concept('button')
Gui.new_concept()
:save_as('button')
-- Events
:new_event('on_click',defines.events.on_gui_click)
:new_event('on_left_click',defines.events.on_gui_click,function(event)
return event.mouse_button == defines.mouse_button_type.left
@@ -38,19 +47,25 @@ end)
:new_event('on_right_click',defines.events.on_gui_click,function(event)
return event.mouse_button == defines.mouse_button_type.right
end)
-- Properties
:new_property('tooltip')
:new_property('caption',nil,function(properties,value)
:new_property('caption',function(properties,value)
properties.caption = value
properties.type = 'button'
end)
:new_property('sprite',nil,function(properties,value,hovered_sprite,clicked_sprite)
:new_property('sprite',function(properties,value,hovered_sprite,clicked_sprite)
properties.sprite = value
properties.hovered_sprite = hovered_sprite
properties.clicked_sprite = clicked_sprite
properties.type = 'sprite-button'
end)
-- Draw
:define_draw(function(properties,parent,element)
-- Check if it should be a sprite button
if properties.type == 'sprite-button' then
-- Draw a sprite button
element = parent.add{
name = properties.name,
type = 'sprite-button',
@@ -61,6 +76,7 @@ end)
}
else
-- Draw a button
element = parent.add{
name = properties.name,
type = 'button',

View File

@@ -7,30 +7,43 @@ local Gui = require 'expcore.gui.core'
--[[-- Clickable elements with a cross in the middle that can be turned off or on.
@element checkbox
@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')
Gui.new_concept('checkbox')
:set_caption('Basic Checkbox')
:set_tooltip('Basic checkbox')
:on_state_changed(function(event)
event.player.print('Basic checkbox is now: '..tostring(event.element.state))
end)
]]
Gui.new_concept('checkbox')
Gui.new_concept()
:save_as('checkbox')
-- Events
:new_event('on_state_changed',defines.events.on_gui_checked_state_changed)
-- Properties
:new_property('tooltip')
:new_property('caption')
:new_property('default',false)
:new_property('use_radio',false)
:new_property('default',nil,false)
:new_property('use_radio',nil,false)
-- Draw
:define_draw(function(properties,parent,element)
local default = properties.default
local state = type(default) == 'boolean' and default
-- Draw a checkbox
element = parent.add{
name = properties.name,
type = properties.use_radio and 'radiobutton' or 'checkbox',
@@ -39,6 +52,7 @@ Gui.new_concept('checkbox')
state = state
}
-- Set the default state
default = Gui.resolve_property(default,element)
if default and default ~= state then
element.state = default

View File

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

View File

@@ -7,27 +7,41 @@ local Gui = require 'expcore.gui.core'
--[[-- A button that lets the player pick one of an: item, entity, tile, or signal similar to the filter-select window.
@element elem_button
@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')
Gui.new_concept('elem_button')
:on_selection_changed(function(event)
event.player.print('Basic elem button is now: '..event.element.elem_value)
end)
]]
Gui.new_concept('elem_button')
Gui.new_concept()
:save_as('elem_button')
-- Events
:new_event('on_selection_changed',defines.events.on_gui_elem_changed)
-- Properties
:new_property('default')
:new_property('elem_type','item')
:new_property('elem_type',nil,'item')
-- Draw
:define_draw(function(properties,parent,element)
-- Draw a chose elem button
element = parent.add{
name = properties.name,
type = 'choose-elem-button',
elem_type = properties.elem_type
}
-- If there is a default, select it
local default = Gui.resolve_property(properties.default,element)
if default then
element.elem_value = default

View File

@@ -7,15 +7,25 @@ local Gui = require 'expcore.gui.core'
--[[-- A empty widget that just exists. The root GUI element screen is an empty-widget.
@element empty
@tparam string style the style that the element will have
@usage-- Making a draggable space styled widget
local draggable_space =
Gui.clone_concept('empty','draggable_space')
:set_style('draggable_space')
]]
Gui.new_concept('empty')
:set_style('draggable_space')
]]
Gui.new_concept()
:save_as('empty')
-- Properties
:new_property('style')
-- Draw
:define_draw(function(properties,parent,element)
-- Draw an empty widget
element = parent.add{
name = properties.name,
type = 'empty-widget',

View File

@@ -7,20 +7,30 @@ local Gui = require 'expcore.gui.core'
--[[-- Invisible containers that lay out children either horizontally or vertically. The root GUI elements (top, left and center; see LuaGui) are flows.
@element flow
@tparam string direction the direction that children will be added
@usage-- Making a basic flow, contains a label with hello world
local basic_flow =
Gui.clone_concept('flow','basic_flow')
Gui.new_concept('flow')
:define_draw(function(properties,parent,element)
element.add{
type = 'label',
caption = 'Hello, World!'
}
end)
]]
Gui.new_concept('flow')
Gui.new_concept()
:save_as('flow')
-- Properties
:new_property('direction')
-- Draw
:define_draw(function(properties,parent,element)
-- Draw a flow
element = parent.add{
name = properties.name,
type = 'flow',

View File

@@ -7,11 +7,13 @@ local Gui = require 'expcore.gui.core'
--[[-- Grey semi-transparent boxes that contain other elements. They have a caption, and, just like flows, they lay out children either horizontally or vertically.
@element frame
@tparam ?string|Concepts.LocalisedString title the title that will show in the frame
@tparam string direction the direction that children will be added
@usage-- Making a basic frame, contains a label with hello world
local basic_frame =
Gui.clone_concept('frame','basic_frame')
Gui.new_concept('frame')
:set_title('Basic Frame')
:define_draw(function(properties,parent,element)
element.add{
@@ -19,11 +21,19 @@ Gui.clone_concept('frame','basic_frame')
caption = 'Hello, World!'
}
end)
]]
Gui.new_concept('frame')
Gui.new_concept()
:save_as('frame')
-- Properties
:new_property('title')
:new_property('direction')
-- Draw
:define_draw(function(properties,parent,element)
-- Draw a frame
element = parent.add{
name = properties.name,
type = 'frame',

View File

@@ -7,17 +7,28 @@ local Gui = require 'expcore.gui.core'
--[[-- A piece of text.
@element frame
@tparam ?string|Concepts.LocalisedString caption the caption that will show in the label
@tparam ?string|Concepts.LocalisedString description the description that will show on the label
@tparam defines.rich_text_setting rich_text how this element handles rich text
@usage-- Making a basic label
local basic_label =
Gui.clone_concept('label','basic_label')
:set_caption('Hello, World!')
]]
Gui.new_concept('label')
:set_caption('Hello, World!')
]]
Gui.new_concept()
:save_as('label')
-- Properties
:new_property('caption')
:new_property('description')
:new_property('rich_text')
-- Draw
:define_draw(function(properties,parent,element)
-- Draw a label
element = parent.add{
name = properties.name,
type = 'label',
@@ -25,5 +36,11 @@ Gui.new_concept('label')
description = properties.description
}
-- Change rich text setting if present
local rich_text = properties.rich_text
if rich_text then
element.style.rich_text_setting = rich_text
end
return element
end)

View File

@@ -7,14 +7,24 @@ local Gui = require 'expcore.gui.core'
--[[-- A vertical or horizontal line.
@element line
@tparam string direction the direction that children will be added
@usage-- Making a basic frame, contains a label with hello world
local basic_line =
Gui.clone_concept('line','basic_line')
]]
Gui.new_concept('line')
]]
Gui.new_concept()
:save_as('line')
-- Properties
:new_property('direction')
-- Draw
:define_draw(function(properties,parent,element)
-- Draw a line
element = parent.add{
name = properties.name,
type = 'line',

View File

@@ -7,14 +7,17 @@ local Gui = require 'expcore.gui.core'
--[[-- Indicate progress by displaying a partially filled bar.
@element progress_bar
@param on_completion fired when increment reaches the maxium value set by set_maximum
@tparam ?string|Concepts.LocalisedString tooltip the tooltip that will show for this element
@tparam number maximum the maxium amount an instance can be increased, default 100
@tparam boolean delay_completion when true the progress will be completed untill after the maximum rather than at the maximum
@tparam boolean inverted although this will NOT effect how you use the functions it will make the element start full and reduce as you call increase, note issues with 0 detections
@usage-- Making a basic progress bar, will increase when pressed then will reset when full
local basic_progress_bar =
Gui.clone_concept('progress_bar','basic_progress_bar')
Gui.new_concept('progress_bar')
:set_tooltip('Basic progress bar')
:set_maximum(5)
:new_event('on_click',defines.events.on_gui_click)
@@ -25,15 +28,25 @@ end)
:on_completion(function(event)
event.concept:reset(event.element)
end)
]]
local progress_bar =
Gui.new_concept('progress_bar')
Gui.new_concept()
:save_as('progress_bar')
-- Events
:new_event('on_completion')
-- Properties
:new_property('tooltip')
:new_property('maximum',100)
:new_property('delay_completion',false)
:new_property('inverted',false)
:new_property('maximum',nil,100)
:new_property('delay_completion',nil,false)
:new_property('inverted',nil,false)
-- Draw
:define_draw(function(properties,parent,element)
-- Draw a progress bar
element = parent.add{
name = properties.name,
tooltip = properties.tooltip,

View File

@@ -7,11 +7,13 @@ local Gui = require 'expcore.gui.core'
--[[-- Similar to a flow but includes the ability to show and use scroll bars.
@element scroll
@tparam string horizontal_scroll the horizontal scroll policy for this scroll pane
@tparam string vertical_scroll the vertical scroll policy for this scroll pane
@usage-- Making a basic flow, contains a label with hello world
local basic_scroll =
Gui.clone_concept('scroll','basic_scroll')
Gui.new_concept('scroll')
:define_draw(function(properties,parent,element)
element.style.hieght = 50
for i = 1,10 do
@@ -21,11 +23,19 @@ Gui.clone_concept('scroll','basic_scroll')
}
end
end)
]]
Gui.new_concept('scroll')
Gui.new_concept()
:save_as('scroll')
-- Properties
:new_property('horizontal_scroll')
:new_property('vertical_scroll')
-- Draw
:define_draw(function(properties,parent,element)
-- Draw a scroll pane
element = parent.add{
name = properties.name,
type = 'scroll-pane',

View File

@@ -7,34 +7,45 @@ local Gui = require 'expcore.gui.core'
--[[-- A number picker.
@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, this means that the slider button will stop at the same interval as the values do
@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')
Gui.new_concept('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')
Gui.new_concept('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')
Gui.new_concept()
:save_as('slider')
-- Events
:new_event('on_value_changed',defines.events.on_gui_value_changed)
-- Properties
:new_property('value_step')
:new_property('default')
:new_property('discrete_slider',false)
:new_property('range',nil,function(properties,minimum,maximum)
:new_property('discrete_slider',nil,false)
:new_property('range',function(properties,minimum,maximum)
if type(minimum) == 'function' then
properties.range = minimum
else
@@ -42,11 +53,14 @@ Gui.new_concept('slider')
properties.maximum = maximum
end
end)
-- Draw
:define_draw(function(properties,parent,element)
local default = properties.default
local value = type(default) == 'number' and default
local value_step = properties.value_step
-- Draw a slider
element = parent.add{
name = properties.name,
type = 'slider',
@@ -59,6 +73,7 @@ end)
value = value
}
-- Find the range for the slider and set it
local min, max = Gui.resolve_property(properties.range,element)
if min or max then
min = min or element.get_slider_minimum()
@@ -66,6 +81,7 @@ end)
element.set_slider_minimum_maximum(min,max)
end
-- If there is a default, select it
default = Gui.resolve_property(default,element)
if default and default ~= value then
element.slider_value = default

View File

@@ -7,14 +7,16 @@ local Gui = require 'expcore.gui.core'
--[[-- An invisible container that lays out children in a specific number of columns. Column width is given by the largest element contained in that row.
@element table
@tparam ?number|function column_count the column count of the table or a function that returns the count being given then parent element
@tparam boolean vertical_lines when true vertical lines will be drawn on the table
@tparam boolean horizontal_lines when true horizontal lines will be drawn on the table
@tparam boolean header_lines when true horizontal lines will be drawn under the first row
@tparam boolean vertical_centering when true element will be vertically centered with in the table
@usage-- Making a basic table, contains 25 labels
local basic_table =
Gui.clone_concept('table','basic_table')
Gui.new_concept('table')
:set_column_count(5)
:define_draw(function(properties,parent,element)
for i = 1,25 do
@@ -24,16 +26,24 @@ Gui.clone_concept('table','basic_table')
}
end
end)
]]
Gui.new_concept('table')
Gui.new_concept()
:save_as('table')
-- Properties
:new_property('column_count')
:new_property('vertical_lines')
:new_property('horizontal_lines')
:new_property('header_lines')
:new_property('vertical_centering')
-- Draw
:define_draw(function(properties,parent,element)
local column_count = Gui.resolve_property(properties.column_count,parent)
-- Draw a table
element = parent.add{
name = properties.name,
type = 'table',

View File

@@ -7,38 +7,54 @@ local Gui = require 'expcore.gui.core'
--[[-- A multi-line text box that supports selection and copy-paste.
@element text_box
@param on_text_changed fired when the text within the text box is changed
@tparam ?string|Concepts.LocalisedString tooltip the tooltip that shows when a player hovers over the text box
@tparam ?string|function default the default text that will appear in the text box, or a function that returns it
@tparam defines.rich_text_setting rich_text how this element handles rich text
@tparam boolean clear_on_rmb if the text box will be cleared and forcused on a right click
@tparam boolean is_selectable when true the text inside the box can be selected
@tparam boolean has_word_wrap when true the text will wrap onto the next line if it reachs the end
@tparam boolean is_read_only when true the text inside the box can not be edited by the player
@usage-- Making a text box
local basic_text_box =
Gui.clone_concept('text_box','basic_text_box')
Gui.new_concept('text_box')
:set_default('I am the text that will show in the text box')
@usage-- Making a text box which can be edited
local editible_text_box =
Gui.clone_concept('text_box','editible_text_box')
Gui.new_concept('text_box')
:set_is_read_only(false)
:set_default('I am the text that will show in the text box')
:on_confirmation(function(event)
event.player.print('Editible text box is now: '..event.element.text)
end)
]]
Gui.new_concept('text_box')
Gui.new_concept()
:save_as('text_box')
-- Events
:new_event('on_text_changed',defines.events.on_gui_text_changed)
-- Properties
:new_property('tooltip')
:new_property('default')
:new_property('clear_on_rmb',false)
:new_property('is_selectable',true)
:new_property('has_word_wrap',true)
:new_property('is_read_only',true)
:new_property('rich_text')
:new_property('clear_on_rmb',nil,false)
:new_property('is_selectable',nil,true)
:new_property('has_word_wrap',nil,true)
:new_property('is_read_only',nil,true)
-- Draw
:define_draw(function(properties,parent,element)
local default = properties.default
local text = type(default) == 'string' and default or nil
-- Draw a text box
element = parent.add{
name = properties.name,
type = 'text-box',
@@ -47,14 +63,22 @@ Gui.new_concept('text_box')
text = text
}
-- Set options for text box
element.selectable = properties.is_selectable
element.word_wrap = properties.has_word_wrap
element.read_only = properties.is_read_only
-- If there is a default, set it
default = Gui.resolve_property(default,element)
if default and default ~= text then
element.text = default
end
-- Change rich text setting if present
local rich_text = properties.rich_text
if rich_text then
element.style.rich_text_setting = rich_text
end
return element
end)

View File

@@ -7,53 +7,70 @@ local Gui = require 'expcore.gui.core'
--[[-- Boxes of text the user can type in.
@element text_field
@param on_text_changed fired when the text within the text field is changed
@param on_confirmation fired when the player presses enter with the text field forcused
@tparam ?string|Concepts.LocalisedString tooltip the tooltip that shows when a player hovers over the text field
@tparam ?string|function default the default text that will appear in the text field, or a function that returns it
@tparam defines.rich_text_setting rich_text how this element handles rich text
@tparam boolean clear_on_rmb if the text field will be cleared and forcused on a right click
@tparam boolean lose_forcus if the text field will lose forcus after the confirmation event
@tparam boolean is_number if this text field contains a number value, can be ignored if is_decimal or is_negitive is used
@tparam boolean is_decimal if this text field contains a decimal value
@tparam boolean is_negative if this text field contains a negative value
@tparam boolean is_password if this text field contains a password value
@usage-- Making a text field
local basic_text_field =
Gui.clone_concept('text_field','basic_text_field')
Gui.new_concept('text_field')
:on_confirmation(function(event)
event.player.print('Basic text field is now: '..event.element.text)
end)
@usage-- Making a text field which will clear on right click and un forcus on confirmation
local better_text_field =
Gui.clone_concept('text_field','better_text_field')
Gui.new_concept('text_field')
:set_clear_on_rmb(true)
:set_lose_forcus(true)
:on_confirmation(function(event)
event.player.print('Better text field is now: '..event.element.text)
end)
@usage-- Making a decimal input
local decimal_text_field =
Gui.clone_concept('text_field','decimal_text_field')
Gui.new_concept('text_field')
:set_is_decimal(true)
:on_confirmation(function(event)
event.player.print('Decimal text field is now: '..event.element.text)
end)
]]
Gui.new_concept('text_field')
Gui.new_concept()
:save_as('text_field')
-- Events
:new_event('on_text_changed',defines.events.on_gui_text_changed)
:new_event('on_confirmation',defines.events.on_gui_confirmed)
-- Properties
:new_property('tooltip')
:new_property('default')
:new_property('clear_on_rmb',false)
:new_property('lose_forcus',false)
:new_property('is_number',false)
:new_property('is_decimal',false)
:new_property('is_negative',false)
:new_property('is_password',false)
:new_property('rich_text')
:new_property('clear_on_rmb',nil,false)
:new_property('lose_forcus',nil,false)
:new_property('is_number',nil,false)
:new_property('is_decimal',nil,false)
:new_property('is_negative',nil,false)
:new_property('is_password',nil,false)
-- Draw
:define_draw(function(properties,parent,element)
local default = properties.default
local text = type(default) == 'string' and default or nil
-- Draw a text field
element = parent.add{
name = properties.name,
type = 'textfield',
@@ -67,10 +84,17 @@ Gui.new_concept('text_field')
text = text
}
-- If there is a default, set it
default = Gui.resolve_property(default,element)
if default and default ~= text then
element.text = default
end
-- Change rich text setting if present
local rich_text = properties.rich_text
if rich_text then
element.style.rich_text_setting = rich_text
end
return element
end)

View File

@@ -15,82 +15,89 @@ local Gui = {
-- Functions that act as a landing point for the other funtions
-- @section concept-control
--[[-- Loads a concept from the concepts file, used internally
@tparam string concept the name of the concept to require
--[[-- Loads a concept from the concepts file
@tparam string concept_name the name of the concept to require
@usage-- Load a base concept
Gui.require_concept('frame')
Gui.require_concept('frame') --- @dep Gui.concept.frame
]]
function Gui.require_concept(concept)
require('expcore.gui.concepts.'..concept)
function Gui.require_concept(concept_name)
require('expcore.gui.concepts.'..concept_name)
end
--[[-- Gets the gui concept with this name
@tparam string name the name of the concept that you want to get
--[[-- Loads a set of concepts from the styles file
@tparam string style_name the name of the style to require
@usage-- Load a base style
Gui.require_concept('expgaming') --- @dep Gui.style.frame
]]
function Gui.require_style(style_name)
require('expcore.gui.styles.'..style_name)
end
--[[-- Gets a gui concept from name, id, or its self
@tparam ?string|number|table name the name, id, or the concept you want to get
@usage-- Getting a gui concept
local button = Gui.get_concept('Button')
]]
function Gui.get_concept(name)
return Gui.concepts[name] or error('Gui concept "'..name..'" is not defind',2)
end
if not name then return end
local vtype = type(name)
if vtype == 'string' then
return Gui.concepts[name]
elseif vtype == 'table' then
if name.draw_callbacks then
return name
end
elseif vtype == 'number' then
for _,concept in pairs(Gui.concepts) do
if concept.name == name then
return concept
end
end
--[[-- Used internally to save concept names to the core gui module
@function Prototype:change_name
@tparam[opt=self.name] string new_name the new name of the concept
@usage-- Internal Saving
-- this is never needed to be done, internal use only!
local button = Gui.get_concept('Button')
button:change_name('Not Button')
]]
function Prototype:change_name(new_name)
if new_name then
Gui.concepts[self.name] = nil
self.name = new_name
self.properties.name = new_name
end
Gui.concepts[self.name] = self
end
--[[-- Used to save the concept to the main gui module to allow access from other files
@function Prototype:save_as
@tparam string save_name the new name of the concept
@usage-- Save a concept to allow access in another file
button:save_as('button')
@usage-- Access concept after being saved
Gui.concepts.button
Gui.get_concept('button')
]]
function Prototype:save_as(save_name)
Gui.concepts[save_name] = self
return self
end
--[[-- Returns a new gui concept with no properties or events
@tparam string name the name that you want this concept to have
@usage-- Making a new concept, see module usage
--[[-- Returns a new gui concept, option to provide a base concept to copy properties and draw functions from
@tparam[opt] ?string|number|table base_concept the concept that you want to copy the details of
@usage-- Making a new button, see module usage
local button = Gui.new_concept('Button')
]]
function Gui.new_concept(name)
if Gui.concepts[name] then
error('Gui concept "'..name..'" is already defind',2)
function Gui.new_concept(base_concept)
if base_concept then
base_concept = Gui.get_concept(base_concept) or error('Invalid gui concept "'..tostring(base_concept)..'"',2)
return base_concept:clone()
else
return Prototype:clone()
end
return Prototype:clone(name)
end
--[[-- Make a new concept based on the properties and drawing of another
@tparam string name the name of the concept that you want as the base
@tparam string new_name the name that you want the new concept to have
@usage-- Making a new concept from another, see module usage
local custom_button = Gui.clone_concept('Button','CustomButton')
]]
function Gui.clone_concept(name,new_name)
local concept = Gui.concepts[name] or error('Gui concept "'..name..'" is not defind',2)
if Gui.concepts[new_name] then
error('Gui concept "'..new_name..'" is already defind',2)
end
return concept:clone(new_name)
end
--[[-- Used to draw a concept to a parent element
@tparam string name the name of the concept that you want to draw
@tparam ?string|number|table concept the name of the concept that you want to draw
@tparam LuaGuiElement parent the element that will act as a parent for the new element
@treturn LuaGuiElement the element that was created
@usage-- Drawing a new element
Gui.draw_concept('Button',element)
]]
function Gui.draw_concept(name,parent,...)
local concept = Gui.concepts[name] or error('Gui concept "'..name..'" is not defind',2)
function Gui.draw_concept(concept,parent,...)
concept = Gui.get_concept(concept) or error('Invalid gui concept "'..tostring(concept)..'"',2)
return concept:draw(parent,...)
end

View File

@@ -1,67 +1,6 @@
--[[-- Core Module - Gui
@module Gui
@alias Prototype
@usage DX note - chaning this doc string has no effect on the docs
local button =
Gui.new_concept('Button')
:new_event('on_click',defines.events.on_gui_click)
:new_property('tooltip')
:new_property('caption',nil,function(properties,value)
properties.caption = value
properties.sprite = nil
properties.type = 'button'
end)
:new_property('sprite',nil,function(properties,value)
properties.image = value
properties.caption = nil
properties.type = 'sprite-button'
end)
:define_draw(function(properties,parent,element)
-- Note that element might be nil if this is the first draw function
-- in this case button is a new concept so we know this is the first function and element is nil
if properties.type == 'button' then
element = parent.draw{
type = properties.type,
name = properties.name,
caption = properties.caption,
tooltip = properties.tooltip
}
else
element = parent.draw{
type = properties.type,
name = properties.name,
sprite = properties.sprite,
tooltip = properties.tooltip
}
end
-- We must return the element or what we want to be seen as the instance, this is so other draw functions have access to it
-- for example if our custom button defined a draw function to change the font color to red
return element
end)
local custom_button =
button:clone('CustomButton')
:new_event('on_admin_clicked',defines.events.on_gui_click,function(event)
return event.player.admin -- only raise custom event when an admin clicks the button
end)
:set_caption('Custom Button')
:set_tooltip('Only admins can press this button')
:on_click(function(event)
if not event.player.admin then
event.player.print('You must be admin to use this button')
end
end)
:on_admin_clicked(function(event)
-- Yes i know this can just be an if else but its an example
game.print(event.player.name..' pressed my admin button')
end)
custom_button:draw(game.player.gui.left)
]]
--- Concept Base.
@@ -71,6 +10,7 @@ custom_button:draw(game.player.gui.left)
local Event = require 'utils.event' -- @dep utils.event
local Store = require 'expcore.store' -- @dep expcore.store
local Game = require 'utils.game' -- @dep utils.game
local Token = require 'utils.token' -- @dep utils.token
local Factorio_Events = {}
local Prototype = {
@@ -114,15 +54,16 @@ end
@treturn GuiConcept the base for building a custom gui
@usage-- Clones the base Button concept to make a alternative button
local custom_button =
Gui.get_concept('Button'):clone('CustomButton')
Gui.get_concept(Gui.concepts.button):clone()
]]
function Prototype:clone(concept_name)
function Prototype:clone()
local concept = table.deep_copy(self)
-- Replace name of the concept
concept.name = concept_name
concept.properties.name = concept_name
concept:change_name()
local uid = tostring(Token.uid())
concept.name = uid
concept.debug_name = uid
concept.properties.name = uid
-- Remove all event handlers that were copied
concept.events = {}
@@ -164,7 +105,7 @@ function Prototype:clone(concept_name)
for _,clone_callback in pairs(concept.clone_callbacks) do
local success, rtn = pcall(clone_callback,concept)
if not success then
error('Gui clone handler error with '..concept.name..':\n\t'..rtn)
error('Gui clone handler error with '..concept.debug_name..':\n\t'..rtn)
end
end
@@ -194,18 +135,28 @@ function Prototype:define_clone(clone_callback)
return self
end
--[[-- Used internally to save concept names to the core gui module
@function Prototype:change_name
@tparam[opt=self.name] string new_name the new name of the concept
@usage-- Internal Saving
-- this is never needed to be done, internal use only!
local button = Gui.get_concept('Button')
button:change_name('Not Button')
--[[-- Used to save the concept to the main gui module to allow access from other files
@function Prototype:save_as
@tparam string save_name the new name of the concept
@usage-- Save a concept to allow access in another file
button:save_as('button')
@usage-- Access concept after being saved
Gui.concepts.button
Gui.get_concept('button')
]]
function Prototype:change_name(new_name)
function Prototype:save_as(save_name)
-- over writen in core file
self.name = new_name or self.name
self.properties.name = self.name
return self
end
--[[-- Sets a debug name that can be used with error handlers
@tparam string name the name that will be used in error messages
@treturn self to allow chaining
@usage-- Set the debug name
unsaved_concept:debug('Example button')
]]
function Prototype:debug(name)
self.debug_name = name
return self
end
@@ -312,15 +263,15 @@ function Prototype:raise_event(event_name,event,from_factorio)
for _,handler in ipairs(handlers) do
local success, err = pcall(handler,event)
if not success then
error('Gui event handler error with '..self.name..'/'..event_name..':\n\t'..err)
error('Gui event handler error with '..self.debug_name..'/'..event_name..':\n\t'..err)
end
end
end
--[[-- Adds a new property to the concept, such as caption, tooltip, or some custom property you want to control
@tparam string property_name the name of the new property, must be unique
@tparam any default the default value for this property, although not strictly required is is strongly recomented
@tparam[opt] function setter_callback this function is called when set is called, if not provided then key in concept.properties is updated to new value
@tparam[opt] any default use this as the default value, will call the setter callback if defined
@treturn GuiConcept to allow chaining of functions
@usage-- Adding caption, sprite, and tooltip to the base button concept
local button =
@@ -337,14 +288,16 @@ end)
properties.type = 'sprite-button'
end)
]]
function Prototype:new_property(property_name,default,setter_callback)
function Prototype:new_property(property_name,setter_callback,default,...)
-- Check that the property does not already exist
if self.properties[property_name] then
error('Property is already defined',2)
end
-- Set the property to its default
self.properties[property_name] = default
-- Check that setter is a function if present
if setter_callback and not type(setter_callback) == 'function' then
error('Setter callback must be a function')
end
--[[-- Sets a new value for a property, triggers setter method if provided, replace with property name
@function Prototype:set_custom_property
@@ -354,7 +307,6 @@ function Prototype:new_property(property_name,default,setter_callback)
local custom_button =
Gui.get_concept('Button')
:set_caption('Default Button')
@usage-- In our examples CustomButton is cloned from Button, this means the caption property already exists
-- note that what ever values that properties have at the time of cloning are also copied
local custom_button =
@@ -367,7 +319,7 @@ Gui.get_concept('CustomButton')
-- Call the setter method to update values if present
local success, err = pcall(setter_callback,concept.properties,value,...)
if not success then
error('Gui property handler error with '..concept.name..'/'..property_name..':\n\t'..err)
error('Gui property handler error with '..concept.debug_name..'/'..property_name..':\n\t'..err)
end
else
-- Otherwise just update the key
@@ -377,6 +329,11 @@ Gui.get_concept('CustomButton')
return concept
end
-- If a default value if given then set the default value
if default ~= nil then
self['set_'..property_name](self,default,...)
end
return self
end
@@ -387,12 +344,12 @@ end
local button =
Gui.get_concept('Button')
:define_draw(function(properties,parent,element)
-- Note that element might be nil if this is the first draw function
-- for this example we assume button was cloned from Prototype and so has no other draw functions defined
-- this means that there is no element yet and what we return will be the first time the element is returned
-- although not shown here you also can recive any extra arguments here from the call to draw
-- Properties will include all the information that you need to draw the element
-- Parent is the parent element for the element, this may have been altered by previous draw functions
-- Element is the current element being made, this may have a nil value, if it is nil then this is the first draw function
-- You can also pass any other arguments that you want to this function from the draw call
if properties.type == 'button' then
element = parent.draw{
element = parent.add{
type = properties.type,
name = properties.name,
caption = properties.caption,
@@ -400,7 +357,7 @@ Gui.get_concept('Button')
}
else
element = parent.draw{
element = parent.add{
type = properties.type,
name = properties.name,
sprite = properties.sprite,
@@ -409,9 +366,9 @@ Gui.get_concept('Button')
end
-- We must return the element or what we want to be seen as the instance, this is so other draw functions have access to it
-- for example if our custom button defined a draw function to change the font color to red
return element
-- If you return element or parent then their values will be updated for the next draw function in the chain
-- It is best practice to always return the values if you have made any changes to them
return element, parent
end)
]]
function Prototype:define_draw(draw_callback)
@@ -433,12 +390,16 @@ end
local button =
Gui.get_concept('Button')
:define_pre_draw(function(properties,parent,element)
-- Here we set the lcoal parent to a new flow, to set this as the new parent we must return it below
-- Properties will include all the information that you need to draw the element
-- Parent is the parent element for the element, this may have been altered by previous draw functions
-- Element is the current element being made, this may have a nil value, if it is nil then this is the first draw function
-- You can also pass any other arguments that you want to this function from the draw call
parent = parent.add{
type = 'flow'
}
-- We must return the element here but we can also return a new parent instance that all other draw functions will see as the parent
-- If you return element or parent then their values will be updated for the next draw function in the chain
-- It is best practice to always return the values if you have made any changes to them
return element, parent
end)
]]
@@ -475,7 +436,7 @@ function Prototype:draw(parent_element,...)
if _element then element = _element end
if _parent then parent = _parent end
elseif not success then
error('Gui draw handler error with '..self.name..':\n\t'.._element)
error('Gui draw handler error with '..self.debug_name..':\n\t'.._element)
end
end

View File

@@ -12,17 +12,16 @@ local Game = require 'utils.game'
local Event = require 'utils.event'
require 'expcore.toolbar'
local test_prefix = '__GUI_TEST_'
local tests = {}
local function TEST(str) return test_prefix..str end
--[[
The main test frame
]]
Gui.require_concept('frame')
local test_frame =
Gui.clone_concept('frame',TEST 'test_frame')
Gui.new_concept('frame')
:set_title('Gui Tests')
:define_draw(function(properties,parent,element)
for category, _ in pairs(tests) do
@@ -34,7 +33,7 @@ Gui.clone_concept('frame',TEST 'test_frame')
end
end)
Gui.clone_concept('toolbar-button',TEST 'run_test_button')
Gui.new_concept('toolbar-button')
:set_permission_alias('gui-test')
:set_caption('Element Tests')
:on_click(function(event)
@@ -45,7 +44,7 @@ Gui.clone_concept('toolbar-button',TEST 'run_test_button')
end)
local test_left_frame =
Gui.clone_concept('toolbar-frame',TEST 'player_list')
Gui.new_concept('toolbar-frame')
:set_permission_alias('gui-test')
:set_caption('Frame Test Left')
:define_draw(function(properties,parent,element)
@@ -61,6 +60,7 @@ Gui.clone_concept('toolbar-frame',TEST 'player_list')
list_area.style.horizontally_stretchable = true
list_area.style.maximal_height = 200
-- Add player names
for _,player in pairs(game.connected_players) do
list_area.add{
type='label',
@@ -72,6 +72,7 @@ end)
local list_area = event.element.scroll
list_area.clear()
-- Add player names
for _,player in pairs(game.connected_players) do
list_area.add{
type='label',
@@ -154,8 +155,11 @@ Buttons
> Admin Button -- Button which is disabled if the player is not an admin
]]
Gui.require_concept('button')
local basic_button =
Gui.clone_concept('button',TEST 'basic_button')
Gui.new_concept('button')
:debug('basic_button')
:set_caption('Basic Button')
:set_tooltip('Basic button')
:on_click(function(event)
@@ -163,7 +167,8 @@ Gui.clone_concept('button',TEST 'basic_button')
end)
local sprite_button =
Gui.clone_concept('button',TEST 'sprite_button')
Gui.new_concept('button')
:debug('sprite_button')
:set_sprite('utility/warning_icon')
:set_tooltip('Sprite button')
:on_click(function(event)
@@ -171,7 +176,8 @@ Gui.clone_concept('button',TEST 'sprite_button')
end)
local multi_sprite_button =
Gui.clone_concept('button',TEST 'multi_sprite_button')
Gui.new_concept('button')
:debug('multi_sprite_button')
:set_sprite('utility/warning_icon','utility/warning','utility/warning_white')
:set_tooltip('Multi-sprite button')
:on_click(function(event)
@@ -179,7 +185,8 @@ Gui.clone_concept('button',TEST 'multi_sprite_button')
end)
local admin_button =
Gui.clone_concept('button',TEST 'admin_button')
Gui.new_concept('button')
:debug('admin_button')
:set_caption('Admin Button')
:set_tooltip('Admin button')
:define_draw(function(properties,parent,element)
@@ -208,8 +215,11 @@ Checkboxs
> Player Stored Checkbox -- Checkbox that stores its state between re-draws
]]
Gui.require_concept('checkbox')
local basic_checkbox =
Gui.clone_concept('checkbox',TEST 'basic_checkbox')
Gui.new_concept('checkbox')
:debug('basic_checkbox')
:set_caption('Basic Checkbox')
:set_tooltip('Basic checkbox')
:on_state_changed(function(event)
@@ -217,7 +227,8 @@ Gui.clone_concept('checkbox',TEST 'basic_checkbox')
end)
local game_checkbox =
Gui.clone_concept('checkbox',TEST 'game_checkbox')
Gui.new_concept('checkbox')
:debug('game_checkbox')
:set_caption('Game Stored Checkbox')
:set_tooltip('Game stored checkbox')
:on_state_changed(function(event)
@@ -230,7 +241,8 @@ end)
end)
local force_checkbox =
Gui.clone_concept('checkbox',TEST 'force_checkbox')
Gui.new_concept('checkbox')
:debug('force_checkbox')
:set_caption('Force Stored Checkbox')
:set_tooltip('Force stored checkbox')
:on_state_changed(function(event)
@@ -243,7 +255,8 @@ end)
end)
local player_checkbox =
Gui.clone_concept('checkbox',TEST 'player_checkbox')
Gui.new_concept('checkbox')
:debug('player_checkbox')
:set_caption('Player Stored Checkbox')
:set_tooltip('Player stored checkbox')
:on_state_changed(function(event)
@@ -270,8 +283,11 @@ Dropdowns
> Dynamic Player Stored Dropdown -- Same as above but now with dynamic options
]]
Gui.require_concept('dropdown')
local static_dropdown =
Gui.clone_concept('dropdown',TEST 'static_dropdown')
Gui.new_concept('dropdown')
:debug('static_dropdown')
:set_static_items{'Option 1','Option 2','Option 3'}
:on_selection_changed(function(event)
local value = Gui.get_dropdown_value(event.element)
@@ -279,7 +295,8 @@ Gui.clone_concept('dropdown',TEST 'static_dropdown')
end)
local dynamic_dropdown =
Gui.clone_concept('dropdown',TEST 'dynamic_dropdown')
Gui.new_concept('dropdown')
:debug('dynamic_dropdown')
:set_dynamic_items(function(element)
local items = {}
for concept_name,_ in pairs(Gui.concepts) do
@@ -295,7 +312,8 @@ end)
end)
local static_player_dropdown =
Gui.clone_concept('dropdown',TEST 'static_player_dropdown')
Gui.new_concept('dropdown')
:debug('static_player_dropdown')
:set_static_items{'Option 1','Option 2','Option 3'}
:on_selection_changed(function(event)
local element = event.element
@@ -308,7 +326,8 @@ end)
end)
local dynamic_player_dropdown =
Gui.clone_concept('dropdown',TEST 'dynamic_player_dropdown')
Gui.new_concept('dropdown')
:debug('dynamic_player_dropdown')
:set_dynamic_items(function(element)
local items = {}
for concept_name,_ in pairs(Gui.concepts) do
@@ -342,7 +361,8 @@ Listboxs
]]
local static_listbox =
Gui.clone_concept('dropdown',TEST 'static_listbox')
Gui.new_concept('dropdown')
:debug('static_listbox')
:set_use_list_box(true)
:set_static_items{'Option 1','Option 2','Option 3'}
:on_selection_changed(function(event)
@@ -351,7 +371,8 @@ Gui.clone_concept('dropdown',TEST 'static_listbox')
end)
local static_player_listbox =
Gui.clone_concept('dropdown',TEST 'static_player_listbox')
Gui.new_concept('dropdown')
:debug('static_player_listbox')
:set_use_list_box(true)
:set_static_items{'Option 1','Option 2','Option 3'}
:on_selection_changed(function(event)
@@ -376,14 +397,18 @@ Elem Buttons
> Player Stored Elem Button -- Same as above but is stored per player
]]
Gui.require_concept('elem_button')
local basic_elem_button =
Gui.clone_concept('elem_button',TEST 'basic_elembutton')
Gui.new_concept('elem_button')
:debug('basic_elem_button')
: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')
Gui.new_concept('elem_button')
:debug('default_selection_elem_button')
:set_elem_type('signal')
:set_default{type='virtual',name='signal-info'}
:on_selection_changed(function(event)
@@ -392,7 +417,8 @@ Gui.clone_concept('elem_button',TEST 'default_selection_elem_button')
end)
local player_elem_button =
Gui.clone_concept('elem_button',TEST 'player_elem_button')
Gui.new_concept('elem_button')
:debug('player_elem_button')
:set_elem_type('technology')
:on_selection_changed(function(event)
local element = event.element
@@ -419,8 +445,11 @@ Progress Bars
> Force Stored Progress Bar -- will increse when pressed, unlike above all will increse at same time and will have the same value
]]
Gui.require_concept('progress_bar')
local basic_progress_bar =
Gui.clone_concept('progress_bar',TEST 'basic_progress_bar')
Gui.new_concept('progress_bar')
:debug('basic_progress_bar')
:set_tooltip('Basic progress bar')
:set_maximum(5)
:new_event('on_click',defines.events.on_gui_click)
@@ -433,7 +462,8 @@ end)
end)
local inverted_progress_bar =
Gui.clone_concept('progress_bar',TEST 'inverted_progress_bar')
Gui.new_concept('progress_bar')
:debug('inverted_progress_bar')
:set_tooltip('Inverted progress bar')
:set_inverted(true)
:set_maximum(5)
@@ -446,7 +476,8 @@ end)
end)
local game_progress_bar =
Gui.clone_concept('progress_bar',TEST 'game_progress_bar')
Gui.new_concept('progress_bar')
:debug('game_progress_bar')
:set_tooltip('Game progress bar')
:set_maximum(300)
:new_event('on_tick',defines.events.on_tick)
@@ -460,7 +491,8 @@ end)
:define_instance_store()
local force_instance_progress_bar =
Gui.clone_concept('progress_bar',TEST 'force_instance_progress_bar')
Gui.new_concept('progress_bar')
:debug('force_instance_progress_bar')
:set_tooltip('Force instance progress bar')
:set_maximum(5)
:new_event('on_click',defines.events.on_gui_click)
@@ -474,7 +506,8 @@ end)
:define_instance_store(Gui.categorize_by_force)
local force_stored_progress_bar =
Gui.clone_concept('progress_bar',TEST 'force_stored_progress_bar')
Gui.new_concept('progress_bar')
:debug('force_stored_progress_bar')
:set_tooltip('Force stored progress bar')
:set_maximum(5)
:new_event('on_click',defines.events.on_gui_click)
@@ -512,15 +545,19 @@ Sliders
> Player Stored Slider -- Slider which stores the value per player, also goes 1 to 10
]]
Gui.require_concept('slider')
local basic_slider =
Gui.clone_concept('slider',TEST 'basic_slider')
Gui.new_concept('slider')
:debug('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')
Gui.new_concept('slider')
:debug('interval_slider')
:set_range(1,10)
:set_value_step(1)
:on_value_changed(function(event)
@@ -528,7 +565,8 @@ Gui.clone_concept('slider',TEST 'interval_slider')
end)
local discrete_slider =
Gui.clone_concept('slider',TEST 'discrete_slider')
Gui.new_concept('slider')
:debug('discrete_slider')
:set_range(1,10)
:set_value_step(1)
:set_discrete_slider(true)
@@ -537,7 +575,8 @@ Gui.clone_concept('slider',TEST 'discrete_slider')
end)
local dynamic_slider =
Gui.clone_concept('slider',TEST 'dynamic_slider')
Gui.new_concept('slider')
:debug('dynamic_slider')
:set_range(function(element)
local player = Gui.get_player_from_element(element)
return 1, player.name:len()
@@ -549,7 +588,8 @@ end)
end)
local player_slider =
Gui.clone_concept('slider',TEST 'player_slider')
Gui.new_concept('slider')
:debug('player_slider')
:set_range(1,10)
:set_value_step(1)
:set_discrete_slider(true)
@@ -580,16 +620,20 @@ Text Fields
> Player Stored Text Field - Same as basic but will store value per player
]]
Gui.require_concept('text_field')
-- Making a text field
local basic_text_field =
Gui.clone_concept('text_field',TEST 'basic_text_field')
Gui.new_concept('text_field')
:debug('basic_text_field')
:set_tooltip('Basic text field')
:on_confirmation(function(event)
event.player.print('Basic text field is now: '..event.element.text)
end)
local better_text_field =
Gui.clone_concept('text_field',TEST 'better_text_field')
Gui.new_concept('text_field')
:debug('better_text_field')
:set_tooltip('Better text field')
:set_clear_on_rmb(true)
:set_lose_forcus(true)
@@ -598,7 +642,8 @@ Gui.clone_concept('text_field',TEST 'better_text_field')
end)
local decimal_text_field =
Gui.clone_concept('text_field',TEST 'decimal_text_field')
Gui.new_concept('text_field')
:debug('decimal_text_field')
:set_tooltip('Decimal text field')
:set_is_decimal(true)
:on_confirmation(function(event)
@@ -606,7 +651,8 @@ Gui.clone_concept('text_field',TEST 'decimal_text_field')
end)
local password_text_field =
Gui.clone_concept('text_field',TEST 'password_text_field')
Gui.new_concept('text_field')
:debug('password_text_field')
:set_tooltip('Password text field')
:set_is_password(true)
:on_confirmation(function(event)
@@ -614,7 +660,8 @@ Gui.clone_concept('text_field',TEST 'password_text_field')
end)
local player_text_field =
Gui.clone_concept('text_field',TEST 'player_text_field')
Gui.new_concept('text_field')
:debug('player_text_field')
:set_tooltip('Player stored text field')
:on_confirmation(function(event)
local element = event.element
@@ -640,8 +687,11 @@ Text Boxs
> Editible Text Box -- A text box that can be edited
]]
Gui.require_concept('text_box')
local basic_text_box =
Gui.clone_concept('text_box',TEST 'basic_text_box')
Gui.new_concept('text_box')
:debug('basic_text_box')
:set_tooltip('Basic text box')
:set_default('I am the text that will show in the text box')
:define_draw(function(properties,parent,element)
@@ -649,7 +699,8 @@ Gui.clone_concept('text_box',TEST 'basic_text_box')
end)
local editible_text_box =
Gui.clone_concept('text_box',TEST 'editible_text_box')
Gui.new_concept('text_box')
:debug('editible_text_box')
:set_tooltip('Editible text box')
:set_is_read_only(false)
:set_default('I am the text that will show in the text box')

View File

@@ -9,6 +9,8 @@ local Event = require 'utils.event' --- @dep utils.event
local Game = require 'utils.game' --- @dep utils.game
local mod_gui = require 'mod-gui' --- @dep mod-gui
Gui.require_concept('button') --- @dep Gui.concept.button
local toolbar_toggle_concept
local toolbar_hide_concept
local toolbar_concept
@@ -54,7 +56,7 @@ end
@tparam table concept the gui concept that you want to add to the button area
@usage-- Adding a basic button to the toolbar
local new_button =
Gui.clone_concept('button','new-button')
Gui.new_concept('button')
:set_caption('Click Me')
:on_click(function(event)
event.player.print('You Clicked Me!!')
@@ -102,17 +104,20 @@ end
@element toolbar-button
@tparam string permission_alias the alias used with Toolbar.allowed
@usage-- Adding a basic button to the toolbar, note no need to call Toolbar.add_button_concept
Gui.clone_concept('toolbar-button','new-button')
Gui.new_concept('toolbar-button')
:set_caption('Click Me')
:on_click(function(event)
event.player.print('You Clicked Me!!')
end)
]]
Toolbar.button =
Gui.clone_concept('button','toolbar-button')
Gui.new_concept('button')
:save_as('toolbar-button')
:new_property('permission_alias',nil,function(properties,value)
Toolbar.set_permission_alias(properties.name,value)
end)
:define_clone(Toolbar.add_button_concept)
:define_draw(function(properties,parent,element)
element.style = mod_gui.button_style
@@ -126,7 +131,7 @@ end)
@tparam table concept the gui concept that you want to add to the toolbar frame area
@usage-- Adding a basic frame to the frame area
local new_frame =
Gui.clone_concept('frame','new_frame')
Gui.new_concept('frame')
:set_title('Test')
Toolbar.add_frame_concept(new_frame)
@@ -177,7 +182,7 @@ end
@tparam string direction the direction that the items in the frame are added
@usage-- Adding a basic player list
local player_list =
Gui.clone_concept('toolbar-frame','player_list')
Gui.new_concept('toolbar-frame')
:set_permission_alias('player_list')
:set_caption('Player List')
:toggle_with_click()
@@ -214,17 +219,24 @@ end)
end)
]]
Toolbar.frame =
Gui.clone_concept('toolbar-button','toolbar-frame')
Gui.new_concept('toolbar-button')
:save_as('toolbar-frame')
-- Properties
:new_property('open_by_default',false)
:new_property('use_container',true)
:new_property('direction','horizontal')
:new_event('on_update')
-- Clone
:define_clone(function(concept)
Toolbar.add_frame_concept(concept)
concept:on_click(function(event)
event.concept:toggle_visible_state(event.player)
end)
end)
-- Draw
:define_draw(function(properties,parent,element)
-- Add the base frame element, the button is already drawn to parent
local player = Gui.get_player_from_element(element)
@@ -332,7 +344,8 @@ end
@param on_hide_frames fired when the frames are hidden for a player
]]
toolbar_concept =
Gui.new_concept('toolbar')
Gui.new_concept()
:debug('toolbar')
:define_draw(function(properties,player)
-- Get the main flows
local top_flow = mod_gui.get_button_flow(player)
@@ -372,6 +385,8 @@ Gui.new_concept('toolbar')
Toolbar.get_visible_frames(player)
end)
-- When the buttons are updated
:new_event('on_button_update')
:on_button_update(function(event)
-- Get the top flow
@@ -389,6 +404,8 @@ end)
end
end)
-- When frames are hidden
:new_event('on_hide_frames')
:on_hide_frames(function(event)
-- Get the left flow
@@ -419,7 +436,7 @@ end
@element toolbar-toggle
]]
toolbar_toggle_concept =
Gui.clone_concept('button','toolbar-toggle')
Gui.new_concept('button')
:set_caption('<')
:set_tooltip{'gui_util.button_tooltip'}
:define_draw(toolbar_button_draw)
@@ -435,7 +452,7 @@ end)
@element toolbar-clear
]]
toolbar_hide_concept =
Gui.clone_concept('button','toolbar-clear')
Gui.new_concept('button')
:set_caption('<')
:set_tooltip{'expcore-gui.left-button-tooltip'}
:define_draw(toolbar_button_draw)