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

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