mirror of
https://github.com/PHIDIAS0303/ExpCluster.git
synced 2025-12-30 04:21:41 +09:00
Revert
This commit is contained in:
@@ -1,90 +0,0 @@
|
||||
--[[-- Core Module - Gui
|
||||
@module Gui
|
||||
@alias Gui
|
||||
]]
|
||||
|
||||
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 Concepts.SpritePath sprite upto three sprites in the order: default, hovered, clicked
|
||||
|
||||
@usage-- Making a basic button
|
||||
local 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.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()
|
||||
: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
|
||||
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',function(properties,value)
|
||||
properties.caption = value
|
||||
properties.type = 'button'
|
||||
end)
|
||||
: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',
|
||||
sprite = properties.sprite,
|
||||
hovered_sprite = properties.hovered_sprite,
|
||||
clicked_sprite = properties.clicked_sprite,
|
||||
tooltip = properties.tooltip
|
||||
}
|
||||
|
||||
else
|
||||
-- Draw a button
|
||||
element = parent.add{
|
||||
name = properties.name,
|
||||
type = 'button',
|
||||
caption = properties.caption,
|
||||
tooltip = properties.tooltip
|
||||
}
|
||||
|
||||
end
|
||||
|
||||
return element
|
||||
end)
|
||||
198
expcore/gui/concepts/center.lua
Normal file
198
expcore/gui/concepts/center.lua
Normal file
@@ -0,0 +1,198 @@
|
||||
--[[-- Core Module - Gui
|
||||
@module Gui
|
||||
@alias Prototype
|
||||
]]
|
||||
|
||||
--- Center Guis.
|
||||
-- Gui structure define for center gui frames
|
||||
-- @section center
|
||||
|
||||
--[[
|
||||
>>>> Functions
|
||||
CenterFrames.get_flow(player) --- Gets the center flow for a player
|
||||
CenterFrames.clear_flow(player) --- Clears the center flow for a player
|
||||
CenterFrames.draw_frame(player,name) --- Draws the center frame for a player, if already open then will do nothing
|
||||
CenterFrames.redraw_frame(player,name) --- Draws the center frame for a player, if already open then will destroy it and redraw
|
||||
CenterFrames.toggle_frame(player,name,state) --- Toggles if the frame is currently open or not, will open if closed and close if open
|
||||
|
||||
CenterFrames.new_frame(permission_name) --- Sets the frame to be the current active gui when opened and closes all other frames
|
||||
CenterFrames._prototype:on_draw(player,frame) --- Use to draw your elements onto the new frame
|
||||
CenterFrames._prototype:set_auto_focus(state) --- Sets the frame to be the current active gui when opened and closes all other frames
|
||||
CenterFrames._prototype:draw_frame(player) --- Draws this frame to the player, if already open does nothing (will call on_draw to draw to the frame)
|
||||
CenterFrames._prototype:redraw_frame(player) --- Draws this frame to the player, if already open it will remove it and redraw it (will call on_draw to draw to the frame)
|
||||
CenterFrames._prototype:toggle_frame(player) --- Toggles if the frame is open, if open it will close it and if closed it will open it
|
||||
CenterFrames._prototype:event_handler(action) --- Creates an event handler that will trigger one of its functions, use with Event.add
|
||||
]]
|
||||
local Gui = require 'expcore.gui.core' --- @dep expcore.gui.core
|
||||
local Prototype = require 'expcore.gui.prototype' --- @dep expcore.gui.prototype
|
||||
local Toolbar = require 'expcore.gui.concepts.toolbar' --- @dep expcore.gui.concepts.toolbar
|
||||
local Game = require 'utils.game' --- @dep utils.game
|
||||
|
||||
local CenterFrames = {
|
||||
_prototype = Prototype.extend{
|
||||
on_creation = Prototype.event
|
||||
}
|
||||
}
|
||||
|
||||
--- Gets the center flow for a player
|
||||
-- @tparam LuaPlayer player the player to get the flow for
|
||||
-- @treturn LuaGuiElement the center flow
|
||||
function CenterFrames.get_flow(player)
|
||||
player = Game.get_player_from_any(player)
|
||||
return player.gui.center
|
||||
end
|
||||
|
||||
--- Clears the center flow for a player
|
||||
-- @tparam LuaPlayer player the player to clear the flow for
|
||||
function CenterFrames.clear_flow(player)
|
||||
local flow = CenterFrames.get_flow(player)
|
||||
flow.clear()
|
||||
end
|
||||
|
||||
--- Draws the center frame for a player, if already open then will do nothing
|
||||
-- @tparam LuaPlayer player the player that will have the frame drawn
|
||||
-- @tparam string name the name of the hui that will drawn
|
||||
-- @treturn LuaGuiElement the new frame that was made
|
||||
function CenterFrames.draw_frame(player,name)
|
||||
local define = Gui.get_define(name,true)
|
||||
if define then
|
||||
return define:draw_frame(player)
|
||||
end
|
||||
end
|
||||
|
||||
--- Draws the center frame for a player, if already open then will destroy it and redraw
|
||||
-- @tparam LuaPlayer player the player that will have the frame drawn
|
||||
-- @tparam string name the name of the hui that will drawn
|
||||
-- @treturn LuaGuiElement the new frame that was made
|
||||
function CenterFrames.redraw_frame(player,name)
|
||||
local define = Gui.get_define(name,true)
|
||||
if define then
|
||||
return define:draw_frame(player)
|
||||
end
|
||||
end
|
||||
|
||||
--- Toggles if the frame is currently open or not, will open if closed and close if open
|
||||
-- @tparam LuaPlayer player the player that will have the frame toggled
|
||||
-- @tparam string name the name of the hui that will be toggled
|
||||
-- @tparam[opt] boolean state when set will force a state for the frame
|
||||
-- @treturn boolean if the frame if no open or closed
|
||||
function CenterFrames.toggle_frame(player,name,state)
|
||||
local define = Gui.get_define(name,true)
|
||||
if define then
|
||||
if state == true then
|
||||
define:draw_frame(player)
|
||||
return true
|
||||
elseif state == false then
|
||||
local flow = CenterFrames.get_flow(player)
|
||||
if flow[define.name..'-frame'] then
|
||||
flow[define.name..'-frame'].destroy()
|
||||
end
|
||||
return false
|
||||
else
|
||||
return define:toggle_frame(player)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
--- Creates a new center frame define
|
||||
-- @tparam string permission_name the name that can be used with the permission system
|
||||
-- @treturn table the new center frame define
|
||||
function CenterFrames.new_frame(permission_name)
|
||||
local self = Toolbar.new_button(permission_name)
|
||||
|
||||
self:on_click(function(player,element)
|
||||
self:toggle_frame(player)
|
||||
end)
|
||||
|
||||
local mt = getmetatable(self)
|
||||
mt.__index = CenterFrames._prototype
|
||||
mt.__call = self.event_handler
|
||||
|
||||
Gui.on_custom_close(self.name..'-frame',function(event)
|
||||
local element = event.element
|
||||
if element and element.valid then element.destroy() end
|
||||
end)
|
||||
|
||||
return self
|
||||
end
|
||||
|
||||
--- Sets the frame to be the current active gui when opened and closes all other frames
|
||||
-- @tparam[opt=true] boolean state when true will auto close other frames and set this frame as player.opened
|
||||
function CenterFrames._prototype:set_auto_focus(state)
|
||||
if state == false then
|
||||
self.auto_focus = false
|
||||
else
|
||||
self.auto_focus = true
|
||||
end
|
||||
end
|
||||
|
||||
--- Draws this frame to the player, if already open does nothing (will call on_draw to draw to the frame)
|
||||
-- @tparam LuaPlayer player the player to draw the frame for
|
||||
-- @treturn LuaGuiElement the new frame that was drawn
|
||||
function CenterFrames._prototype:draw_frame(player)
|
||||
player = Game.get_player_from_any(player)
|
||||
local flow = CenterFrames.get_flow(player)
|
||||
|
||||
if flow[self.name..'-frame'] then
|
||||
return flow[self.name..'-frame']
|
||||
end
|
||||
|
||||
if self.auto_focus then
|
||||
flow.clear()
|
||||
end
|
||||
|
||||
local frame = flow.add{
|
||||
type='frame',
|
||||
name=self.name..'-frame'
|
||||
}
|
||||
|
||||
if self.auto_focus then
|
||||
player.opened = frame
|
||||
end
|
||||
|
||||
self:raise_event('on_creation',player,frame)
|
||||
|
||||
return frame
|
||||
end
|
||||
|
||||
--- Draws this frame to the player, if already open it will remove it and redraw it (will call on_draw to draw to the frame)
|
||||
-- @tparam LuaPlayer player the player to draw the frame for
|
||||
-- @treturn LuaGuiElement the new frame that was drawn
|
||||
function CenterFrames._prototype:redraw_frame(player)
|
||||
player = Game.get_player_from_any(player)
|
||||
local flow = CenterFrames.get_flow(player)
|
||||
|
||||
if flow[self.name..'-frame'] then
|
||||
flow[self.name..'-frame'].destroy()
|
||||
end
|
||||
|
||||
return self:draw_frame(player)
|
||||
end
|
||||
|
||||
--- Toggles if the frame is open, if open it will close it and if closed it will open it
|
||||
-- @tparam LuaPlayer player the player to draw the frame for
|
||||
-- @treturn boolean with the gui frame is now open
|
||||
function CenterFrames._prototype:toggle_frame(player)
|
||||
player = Game.get_player_from_any(player)
|
||||
local flow = CenterFrames.get_flow(player)
|
||||
|
||||
if flow[self.name..'-frame'] then
|
||||
flow[self.name..'-frame'].destroy()
|
||||
return false
|
||||
else
|
||||
self:draw_frame(player)
|
||||
return true
|
||||
end
|
||||
end
|
||||
|
||||
--- Creates an event handler that will trigger one of its functions, use with Event.add
|
||||
-- @tparam[opt=update] string action the action to take on this event
|
||||
function CenterFrames._prototype:event_handler(action)
|
||||
action = action or 'update'
|
||||
return function(event)
|
||||
local player = Game.get_player_by_index(event.player_index)
|
||||
self[action](self,player)
|
||||
end
|
||||
end
|
||||
|
||||
return CenterFrames
|
||||
@@ -1,62 +0,0 @@
|
||||
--[[-- Core Module - Gui
|
||||
@module Gui
|
||||
@alias Gui
|
||||
]]
|
||||
|
||||
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.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()
|
||||
: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',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',
|
||||
caption = properties.caption,
|
||||
tooltip = properties.tooltip,
|
||||
state = state
|
||||
}
|
||||
|
||||
-- Set the default state
|
||||
default = Gui.resolve_property(default,element)
|
||||
if default and default ~= state then
|
||||
element.state = default
|
||||
end
|
||||
|
||||
return element
|
||||
end)
|
||||
@@ -1,169 +0,0 @@
|
||||
--[[-- Core Module - Gui
|
||||
@module Gui
|
||||
@alias Gui
|
||||
]]
|
||||
|
||||
local Gui = require 'expcore.gui.core'
|
||||
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
|
||||
local 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.new_concept('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_changed(function(event)
|
||||
local value = Gui.get_dropdown_value(event.element)
|
||||
event.player.print('Dynamic dropdown is now: '..value)
|
||||
end)
|
||||
|
||||
]]
|
||||
|
||||
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',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',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)
|
||||
|
||||
-- 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 = 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)
|
||||
Gui.add_dropdown_items(element,start_index,dynamic_items)
|
||||
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)
|
||||
end
|
||||
|
||||
return element
|
||||
end)
|
||||
|
||||
--- Dropdowns.
|
||||
-- functions used with dropdowns
|
||||
-- @section dropdowns
|
||||
|
||||
--[[-- Selects the index of a dropdown with this value
|
||||
@tparam LuaGuiElement element the dropdown that you want to set the selection for
|
||||
@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')
|
||||
]]
|
||||
function Gui.set_dropdown_value(element,value)
|
||||
for index, item in pairs(element.items) do
|
||||
if item == value then
|
||||
element.selected_index = index
|
||||
return true
|
||||
end
|
||||
end
|
||||
return false
|
||||
end
|
||||
|
||||
--[[-- Gets the selected item value of a dropdown
|
||||
@tparam LuaGuiElement element the dropdown that you want the selected value of
|
||||
@treturn ?string|Concepts.LocalisedString the value that is currently selected
|
||||
@usage-- Getting the selected value
|
||||
local selected_value = Gui.get_dropdown_value(element)
|
||||
]]
|
||||
function Gui.get_dropdown_value(element)
|
||||
return element.items[element.selected_index]
|
||||
end
|
||||
|
||||
--[[-- Adds the given items into the list of items for this dropdown
|
||||
@tparam LuaGuiElement element the dropdown that you want to add the items to
|
||||
@tparam[opt] number start_index the index at which the items will be added, if not given appened to the end
|
||||
@tparam table new_items the list of new items that you want to add
|
||||
@treturn table the list of items that the element now has
|
||||
@usage-- Add the items 'foo' and 'bar' to the end
|
||||
Gui.add_dropdown_items(element,{'foo','bar'})
|
||||
@usage-- Add the items 'foo' and 'bar' to the start
|
||||
Gui.add_dropdown_items(element,1,{'foo','bar'})
|
||||
]]
|
||||
function Gui.add_dropdown_items(element,start_index,new_items)
|
||||
if not new_items then
|
||||
new_items = start_index
|
||||
start_index = nil
|
||||
end
|
||||
|
||||
local items = element.items
|
||||
element.items = array_insert(items,start_index,new_items)
|
||||
|
||||
return items
|
||||
end
|
||||
@@ -1,51 +0,0 @@
|
||||
--[[-- Core Module - Gui
|
||||
@module Gui
|
||||
@alias Gui
|
||||
]]
|
||||
|
||||
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.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()
|
||||
:save_as('elem_button')
|
||||
|
||||
-- Events
|
||||
:new_event('on_selection_changed',defines.events.on_gui_elem_changed)
|
||||
|
||||
-- Properties
|
||||
:new_property('default')
|
||||
: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
|
||||
end
|
||||
|
||||
return element
|
||||
end)
|
||||
@@ -1,36 +0,0 @@
|
||||
--[[-- Core Module - Gui
|
||||
@module Gui
|
||||
@alias Gui
|
||||
]]
|
||||
|
||||
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.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',
|
||||
style = properties.style
|
||||
}
|
||||
|
||||
return element
|
||||
end)
|
||||
@@ -1,42 +0,0 @@
|
||||
--[[-- Core Module - Gui
|
||||
@module Gui
|
||||
@alias Gui
|
||||
]]
|
||||
|
||||
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.new_concept('flow')
|
||||
:define_draw(function(properties,parent,element)
|
||||
element.add{
|
||||
type = 'label',
|
||||
caption = 'Hello, World!'
|
||||
}
|
||||
end)
|
||||
|
||||
]]
|
||||
|
||||
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',
|
||||
caption = properties.title,
|
||||
direction = properties.direction
|
||||
}
|
||||
|
||||
return element
|
||||
end)
|
||||
@@ -1,45 +0,0 @@
|
||||
--[[-- Core Module - Gui
|
||||
@module Gui
|
||||
@alias Gui
|
||||
]]
|
||||
|
||||
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.new_concept('frame')
|
||||
:set_title('Basic Frame')
|
||||
:define_draw(function(properties,parent,element)
|
||||
element.add{
|
||||
type = 'label',
|
||||
caption = 'Hello, World!'
|
||||
}
|
||||
end)
|
||||
|
||||
]]
|
||||
|
||||
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',
|
||||
caption = properties.title,
|
||||
direction = properties.direction
|
||||
}
|
||||
|
||||
return element
|
||||
end)
|
||||
@@ -1,46 +0,0 @@
|
||||
--[[-- Core Module - Gui
|
||||
@module Gui
|
||||
@alias Gui
|
||||
]]
|
||||
|
||||
local Gui = require 'expcore.gui.core'
|
||||
|
||||
--[[-- A piece of text.
|
||||
@element label
|
||||
|
||||
@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.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',
|
||||
caption = properties.caption,
|
||||
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)
|
||||
322
expcore/gui/concepts/left.lua
Normal file
322
expcore/gui/concepts/left.lua
Normal file
@@ -0,0 +1,322 @@
|
||||
--[[-- Core Module - Gui
|
||||
@module Gui
|
||||
@alias Prototype
|
||||
]]
|
||||
|
||||
--- Left Guis.
|
||||
-- Gui structure define for left frames
|
||||
-- @section left
|
||||
|
||||
--[[
|
||||
>>>> Example formating
|
||||
|
||||
-- first we add config that relates to the button on the toolbar, all normal button functions are present
|
||||
local left_frame =
|
||||
Gui.new_left_frame('test-left-frame')
|
||||
:set_caption('Test Left Gui')
|
||||
:set_post_authenticator(function(player,button_name)
|
||||
return global.show_test_gui
|
||||
end)
|
||||
|
||||
-- then we add the config for the left frame, on_draw should draw the gui from an empty frame, on_update should take a frame from on_draw on edit it
|
||||
:set_open_by_default()
|
||||
:on_draw(function(_player,frame)
|
||||
for _,player in pairs(game.connected_players) do
|
||||
frame.add{
|
||||
type='label',
|
||||
caption=player.name
|
||||
}
|
||||
end
|
||||
end)
|
||||
|
||||
-- now we can use the action factory to call events on the gui, actions are: 'update', 'update_all', 'redraw', 'redraw_all'
|
||||
Event.add(defines.events.on_player_joined_game,left_frame 'update_all')
|
||||
Event.add(defines.events.on_player_left_game,left_frame 'update_all')
|
||||
|
||||
>>>> Functions
|
||||
LeftFrames.get_flow(player) --- Gets the left frame flow for a player
|
||||
LeftFrames.get_frame(name,player) --- Gets one frame from the left flow by its name
|
||||
LeftFrames.get_open(player) --- Gets all open frames for a player, if non are open it will remove the close all button
|
||||
LeftFrames.toggle_frame(name,player,state) --- Toggles the visibility of a left frame, or sets its visibility state
|
||||
|
||||
LeftFrames.new_frame(permission_name) --- Creates a new left frame define
|
||||
LeftFrames._prototype:set_open_by_default(state) --- Sets if the frame is visible when a player joins, can also be a function to return a boolean
|
||||
LeftFrames._prototype:set_direction(direction) --- Sets the direction of the frame, either vertical or horizontal
|
||||
LeftFrames._prototype:get_frame(player) --- Gets the frame for this define from the left frame flow
|
||||
LeftFrames._prototype:is_open(player) --- Returns if the player currently has this define visible
|
||||
LeftFrames._prototype:toggle(player) --- Toggles the visibility of the left frame
|
||||
|
||||
LeftFrames._prototype:update(player) --- Updates the contents of the left frame, first tries update callback, other wise will clear and redraw
|
||||
LeftFrames._prototype:update_all(update_offline) --- Updates the frame for all players, see update
|
||||
LeftFrames._prototype:redraw(player) --- Redraws the frame by calling on_draw, will always clear the frame
|
||||
LeftFrames._prototype:redraw_all(update_offline) --- Redraws the frame for all players, see redraw
|
||||
|
||||
LeftFrames._prototype:on_draw(player,frame) --- Use to draw your elements to the new frame
|
||||
LeftFrames._prototype:on_update(player,frame) --- Use to edit your frame when there is no need to redraw it
|
||||
LeftFrames._prototype:on_player_toggle(player,frame) --- Is triggered when the player presses the toggle button
|
||||
LeftFrames._prototype:event_handler(action) --- Creates an event handler that will trigger one of its functions, use with Event.add
|
||||
]]
|
||||
local Gui = require 'expcore.gui.core' --- @dep expcore.gui.core
|
||||
local Prototype = require 'expcore.gui.prototype' --- @dep expcore.gui.prototype
|
||||
local Toolbar = require 'expcore.gui.concepts.toolbar' --- @dep expcore.gui.concepts.toolbar
|
||||
local Buttons = require 'expcore.gui.elements.buttons' --- @dep expcore.gui.elements.buttons
|
||||
local mod_gui = require 'mod-gui' --- @dep mod-gui
|
||||
local Game = require 'utils.game' --- @dep utils.game
|
||||
local Event = require 'utils.event' --- @dep utils.event
|
||||
|
||||
local LeftFrames = {
|
||||
frames={},
|
||||
_prototype=Prototype.extend{
|
||||
on_creation = Prototype.event,
|
||||
on_update = Prototype.event,
|
||||
on_player_toggle = Prototype.event
|
||||
}
|
||||
}
|
||||
setmetatable(LeftFrames._prototype, {
|
||||
__index = Buttons._prototype
|
||||
})
|
||||
|
||||
--- Gets the left frame flow for a player
|
||||
-- @tparam LuaPlayer player the player to get the flow of
|
||||
-- @treturn LuaGuiElement the left frame flow for the player
|
||||
function LeftFrames.get_flow(player)
|
||||
player = Game.get_player_from_any(player)
|
||||
return mod_gui.get_frame_flow(player)
|
||||
end
|
||||
|
||||
--- Gets one frame from the left flow by its name
|
||||
-- @tparam string name the name of the gui frame to get
|
||||
-- @tparam LuaPlayer player the player to get the frame of
|
||||
-- @treturn LuaGuiElement the frame in the left frame flow with that name
|
||||
function LeftFrames.get_frame(name,player)
|
||||
local define = LeftFrames.frames[name]
|
||||
if not define then
|
||||
return error('Left Frame '..name..' is not defined.',2)
|
||||
end
|
||||
return define:get_frame(player)
|
||||
end
|
||||
|
||||
--- Gets all open frames for a player, if non are open it will remove the close all button
|
||||
-- @tparam LuaPlayer player the player to get the flow of
|
||||
-- @treturn table contains all the open (and registered) frames for the player
|
||||
function LeftFrames.get_open(player)
|
||||
local open = {}
|
||||
local flow = LeftFrames.get_flow(player)
|
||||
|
||||
for _,define in pairs(LeftFrames.frames) do
|
||||
if define:is_open(player) then
|
||||
table.insert(open,define)
|
||||
end
|
||||
end
|
||||
|
||||
flow[LeftFrames.toggle_button.name].visible = #open ~= 0
|
||||
|
||||
return open
|
||||
end
|
||||
|
||||
--- Toggles the visibility of a left frame, or sets its visibility state
|
||||
-- @tparam string name the name of the gui frame to toggle
|
||||
-- @tparam LuaPlayer player the player to get the frame of
|
||||
-- @tparam[opt] boolean state when given will be the state that the visibility is set to
|
||||
-- @treturn boolean the new state of the visibility
|
||||
function LeftFrames.toggle_frame(name,player,state)
|
||||
local define = LeftFrames.frames[name]
|
||||
if not define then
|
||||
return error('Left Frame '..name..' is not defined.',2)
|
||||
end
|
||||
|
||||
local frame = LeftFrames.get_frame(name,player)
|
||||
if state ~= nil then
|
||||
frame.visible = state
|
||||
else
|
||||
Gui.toggle_visible(frame)
|
||||
end
|
||||
|
||||
LeftFrames.get_open(player)
|
||||
|
||||
return frame.visible
|
||||
end
|
||||
|
||||
--- Creates a new left frame define
|
||||
-- @tparam string permission_name the name that can be used with the permission system
|
||||
-- @treturn table the new left frame define
|
||||
function LeftFrames.new_frame(permission_name)
|
||||
local self = Toolbar.new_button(permission_name)
|
||||
|
||||
local mt = getmetatable(self)
|
||||
mt.__index = LeftFrames._prototype
|
||||
mt.__call = self.event_handler
|
||||
|
||||
self:on_click(function(player,_element)
|
||||
local visible = self:toggle(player)
|
||||
local frame = self:get_frame(player)
|
||||
self:raise_event('on_player_toggle',player,frame,visible)
|
||||
end)
|
||||
|
||||
LeftFrames.frames[self.name] = self
|
||||
|
||||
return self
|
||||
end
|
||||
|
||||
--- Sets if the frame is visible when a player joins, can also be a function to return a boolean
|
||||
-- @tparam[opt=true] ?boolean|function state the default state of the visibility, can be a function
|
||||
-- state param - player LuaPlayer - the player that has joined the game
|
||||
-- state param - define_name string - the define name for the frame
|
||||
-- state return - boolean - false will hide the frame
|
||||
function LeftFrames._prototype:set_open_by_default(state)
|
||||
if state == false then
|
||||
self.open_by_default = false
|
||||
elseif state == nil then
|
||||
self.open_by_default = true
|
||||
else
|
||||
self.open_by_default = state
|
||||
end
|
||||
return self
|
||||
end
|
||||
|
||||
--- Sets the direction of the frame, either vertical or horizontal
|
||||
-- @tparam string direction the direction to have the elements be added to the frame
|
||||
function LeftFrames._prototype:set_direction(direction)
|
||||
self.direction = direction
|
||||
return self
|
||||
end
|
||||
|
||||
--- Creates the gui for the first time, used internally
|
||||
-- @tparam LuaPlayer player the player to draw the frame to
|
||||
-- @treturn LuaGuiElement the frame that was made
|
||||
function LeftFrames._prototype:_internal_draw(player)
|
||||
local flow = LeftFrames.get_flow(player)
|
||||
local frame = flow.add{
|
||||
type='frame',
|
||||
name=self.name..'-frame',
|
||||
direction=self.direction
|
||||
}
|
||||
|
||||
self:raise_event('on_creation',player,frame)
|
||||
|
||||
if not self.open_by_default then
|
||||
frame.visible = false
|
||||
elseif type(self.open_by_default) == 'function' then
|
||||
if not self.open_by_default(player,self.name) then
|
||||
frame.visible = false
|
||||
end
|
||||
end
|
||||
|
||||
if not Toolbar.allowed(player,self.name) then
|
||||
frame.visible = false
|
||||
end
|
||||
|
||||
return frame
|
||||
end
|
||||
|
||||
--- Gets the frame for this define from the left frame flow
|
||||
-- @tparam LuaPlayer player the player to get the frame of
|
||||
-- @treturn LuaGuiElement the frame in the left frame flow for this define
|
||||
function LeftFrames._prototype:get_frame(player)
|
||||
local flow = LeftFrames.get_flow(player)
|
||||
if flow[self.name..'-frame'] and flow[self.name..'-frame'].valid then
|
||||
return flow[self.name..'-frame']
|
||||
else
|
||||
return self:_internal_draw(player)
|
||||
end
|
||||
end
|
||||
|
||||
--- Returns if the player currently has this define visible
|
||||
-- @tparam LuaPlayer player the player to get the frame of
|
||||
-- @treturn boolean true if it is open/visible
|
||||
function LeftFrames._prototype:is_open(player)
|
||||
local frame = self:get_frame(player)
|
||||
return frame and frame.visible or false
|
||||
end
|
||||
|
||||
--- Toggles the visibility of the left frame
|
||||
-- @tparam LuaPlayer player the player to toggle the frame of
|
||||
-- @treturn boolean the new state of the visibility
|
||||
function LeftFrames._prototype:toggle(player)
|
||||
local frame = self:get_frame(player)
|
||||
Gui.toggle_visible(frame)
|
||||
LeftFrames.get_open(player)
|
||||
return frame.visible
|
||||
end
|
||||
|
||||
--- Updates the contents of the left frame, first tries update callback, other wise will clear and redraw
|
||||
-- @tparam LuaPlayer player the player to update the frame of
|
||||
function LeftFrames._prototype:update(player)
|
||||
local frame = self:get_frame(player)
|
||||
if self:raise_event('on_update',player,frame) == 0 then
|
||||
frame.clear()
|
||||
self:raise_event('on_creation',player,frame)
|
||||
end
|
||||
end
|
||||
|
||||
--- Updates the frame for all players, see update
|
||||
-- @tparam[opt=false] boolean update_offline when true will update the frame for offline players
|
||||
function LeftFrames._prototype:update_all(update_offline)
|
||||
local players = update_offline == true and game.players or game.connected_players
|
||||
for _,player in pairs(players) do
|
||||
self:update(player)
|
||||
end
|
||||
end
|
||||
|
||||
--- Redraws the frame by calling on_draw, will always clear the frame
|
||||
-- @tparam LuaPlayer player the player to update the frame of
|
||||
function LeftFrames._prototype:redraw(player)
|
||||
local frame = self:get_frame(player)
|
||||
frame.clear()
|
||||
self:raise_event('on_creation',player,frame)
|
||||
end
|
||||
|
||||
--- Redraws the frame for all players, see redraw
|
||||
-- @tparam[opt=false] boolean update_offline when true will update the frame for offline players
|
||||
function LeftFrames._prototype:redraw_all(update_offline)
|
||||
local players = update_offline == true and game.players or game.connected_players
|
||||
for _,player in pairs(players) do
|
||||
self:redraw(player)
|
||||
end
|
||||
end
|
||||
|
||||
--- Creates an event handler that will trigger one of its functions, use with Event.add
|
||||
-- @tparam[opt=update] string action the action to take on this event
|
||||
function LeftFrames._prototype:event_handler(action)
|
||||
action = action or 'update'
|
||||
return function(event)
|
||||
local player
|
||||
if event and event.player_index then
|
||||
player = Game.get_player_by_index(event.player_index)
|
||||
end
|
||||
self[action](self,player)
|
||||
end
|
||||
end
|
||||
|
||||
LeftFrames.toggle_button =
|
||||
Buttons.new_button()
|
||||
:set_tooltip{'expcore-gui.left-button-tooltip'}
|
||||
:set_caption('<')
|
||||
:on_click(function(player,element)
|
||||
for _,define in pairs(LeftFrames.frames) do
|
||||
local frame = LeftFrames.get_frame(define.name,player)
|
||||
frame.visible = false
|
||||
define:raise_event('on_player_toggle',player,frame,false)
|
||||
end
|
||||
element.visible = false
|
||||
end)
|
||||
|
||||
Event.add(defines.events.on_player_created,function(event)
|
||||
local player = Game.get_player_by_index(event.player_index)
|
||||
local flow = LeftFrames.get_flow(player)
|
||||
|
||||
local close_button = LeftFrames.toggle_button(flow)
|
||||
Gui.set_padding(close_button)
|
||||
local style = close_button.style
|
||||
style.width = 18
|
||||
style.height = 36
|
||||
style.font = 'default-small-bold'
|
||||
|
||||
for _,define in pairs(LeftFrames.frames) do
|
||||
define:_internal_draw(player)
|
||||
end
|
||||
|
||||
LeftFrames.get_open(player)
|
||||
end)
|
||||
|
||||
return LeftFrames
|
||||
@@ -1,35 +0,0 @@
|
||||
--[[-- Core Module - Gui
|
||||
@module Gui
|
||||
@alias Gui
|
||||
]]
|
||||
|
||||
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.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',
|
||||
direction = properties.direction
|
||||
}
|
||||
|
||||
return element
|
||||
end)
|
||||
230
expcore/gui/concepts/popups.lua
Normal file
230
expcore/gui/concepts/popups.lua
Normal file
@@ -0,0 +1,230 @@
|
||||
--[[-- Core Module - Gui
|
||||
@module Gui
|
||||
@alias Prototype
|
||||
]]
|
||||
|
||||
--- Popups.
|
||||
-- Gui structure define for popup gui
|
||||
-- @section popups
|
||||
|
||||
--[[
|
||||
>>>> Functions
|
||||
PopupFrames.get_flow(player) --- Gets the left flow that contains the popup frames
|
||||
PopupFrames.open(define_name,player,open_time,...) --- Opens a popup for the player, can give the amount of time it is open as well as params for the draw function
|
||||
|
||||
PopupFrames.close_progress --- Progress bar which when depleted will close the popup frame
|
||||
PopupFrames.close_button --- A button which can be used to close the gui before the timer runs out
|
||||
|
||||
PopupFrames.new_popup(name) --- Creates a new popup frame define
|
||||
PopupFrames._prototype:set_default_open_time(amount) --- Sets the default open time for the popup, will be used if non is provided with open
|
||||
PopupFrames._prototype:open(player,open_time,...) --- Opens this define for a player, can be given open time and any other params for the draw function
|
||||
]]
|
||||
local Gui = require 'expcore.gui.core' --- @dep expcore.gui.core
|
||||
local Prototype = require 'expcore.gui.prototype' --- @dep expcore.gui.prototype
|
||||
local Game = require 'utils.game' --- @dep utils.game
|
||||
local Event = require 'utils.event' --- @dep utils.event
|
||||
local ProgressBar = require 'expcore.gui.elements.progress-bar' --- @dep expcore.gui.elements.progress-bar
|
||||
local Button = require 'expcore.gui.elements.buttons' --- @dep expcore.gui.elements.buttons
|
||||
local mod_gui = require 'mod-gui' --- @dep mod-gui
|
||||
local Color = require 'resources.color_presets' --- @dep resources.color_presets
|
||||
local Global = require 'utils.global' --- @dep utils.global
|
||||
|
||||
local PopupFrames = {
|
||||
paused_popups={},
|
||||
popup_flow_name = Gui.uid_name(),
|
||||
main_frame_name = Gui.uid_name(),
|
||||
close_frame_name = Gui.uid_name(),
|
||||
_prototype = Prototype.extend{
|
||||
on_creation = Prototype.event
|
||||
}
|
||||
}
|
||||
Global.register(PopupFrames.paused_popups,function(tbl)
|
||||
PopupFrames.paused_popups = tbl
|
||||
end)
|
||||
|
||||
--- Sets the state of the element in the paused list, nil or true
|
||||
-- @tparam LuaGuiElement element the element to set the state of
|
||||
-- @tparam[opt] boolean state the state to set it to, true will pause the the progress bar
|
||||
local function set_paused_state(element,state)
|
||||
local name = element.player_index..':'..element.index
|
||||
PopupFrames.paused_popups[name] = state
|
||||
end
|
||||
|
||||
--- Gets the state of the element in the paused list, nil or true
|
||||
-- @tparam LuaGuiElement element the element to get the state of
|
||||
local function get_paused_state(element)
|
||||
local name = element.player_index..':'..element.index
|
||||
return PopupFrames.paused_popups[name]
|
||||
end
|
||||
|
||||
--- Gets the left flow that contains the popup frames
|
||||
-- @tparam LuaPlayer player the player to get the flow for
|
||||
-- @treturn LuaGuiElement the left flow that contains the popup frames
|
||||
function PopupFrames.get_flow(player)
|
||||
player = Game.get_player_from_any(player)
|
||||
local flow = mod_gui.get_frame_flow(player)
|
||||
return flow[PopupFrames.popup_flow_name]
|
||||
end
|
||||
|
||||
--- Opens a popup for the player, can give the amount of time it is open as well as params for the draw function
|
||||
-- @tparam string define_name the name of the define that you want to open for the player
|
||||
-- @tparam LuaPlayer player the player to open the popup for
|
||||
-- @tparam[opt] number open_time the minimum number of ticks you want the popup open for, 0 means no limit, nil will take default
|
||||
-- @tparam any ... the other params that you want to pass to your on_draw event
|
||||
-- @treturn LuaGuiElement the frame that was drawn, the inner gui flow which contains the content
|
||||
function PopupFrames.open(define_name,player,open_time,...)
|
||||
local define = Gui.get_define(define_name,true)
|
||||
player = Game.get_player_from_any(player)
|
||||
return define:open(player,open_time,...)
|
||||
end
|
||||
|
||||
--- Closes the popup, is called by progress bar and close button
|
||||
-- @tparam LuaGuiElement element either the progress bar or the close button
|
||||
local function close_popup(element)
|
||||
local frame = element.parent.parent.parent
|
||||
if not frame or not frame.valid then return end
|
||||
set_paused_state(element.parent[PopupFrames.close_progress:uid()])
|
||||
frame.destroy()
|
||||
end
|
||||
|
||||
--- Progress bar which when depleted will close the popup frame
|
||||
PopupFrames.close_progress =
|
||||
ProgressBar.new_progressbar()
|
||||
:use_count_down()
|
||||
:set_tooltip('Pause/Resume Auto-close')
|
||||
:on_complete(function(player,element)
|
||||
close_popup(element)
|
||||
end)
|
||||
|
||||
--- A button which can be used to close the gui before the timer runs out
|
||||
PopupFrames.close_button =
|
||||
Button.new_button()
|
||||
:set_sprites('utility/close_white')
|
||||
:set_tooltip('Close Popup')
|
||||
:on_click(function(player,element)
|
||||
close_popup(element)
|
||||
end)
|
||||
|
||||
--- When the progress bar is clicked it will pause its progress, or resume if previously paused
|
||||
Gui.on_click(PopupFrames.close_progress:uid(),function(event)
|
||||
local element = event.element
|
||||
if get_paused_state(element) then
|
||||
set_paused_state(element)
|
||||
else
|
||||
set_paused_state(element,true)
|
||||
end
|
||||
end)
|
||||
|
||||
--- When the parent flow of the progress bar is clicked it will pause its progress, or resume if previously paused
|
||||
Gui.on_click(PopupFrames.close_frame_name,function(event)
|
||||
local element = event.element[PopupFrames.close_progress:uid()]
|
||||
if get_paused_state(element) then
|
||||
set_paused_state(element)
|
||||
else
|
||||
set_paused_state(element,true)
|
||||
end
|
||||
end)
|
||||
|
||||
--- Creates a new popup frame define
|
||||
-- @tparam[opt] string name the optional debug name that can be added
|
||||
-- @treturn table the new popup frame define
|
||||
function PopupFrames.new_popup(name)
|
||||
local self = Gui.new_define(PopupFrames._prototype,name)
|
||||
self.draw_data.type = 'flow'
|
||||
self.draw_data.direction = 'vertical'
|
||||
|
||||
local mt = getmetatable(self)
|
||||
mt.__call = function(tbl,player,open_time,...)
|
||||
return tbl:open(player,open_time,...)
|
||||
end
|
||||
|
||||
self:on_draw(function(player,element,maximum,...)
|
||||
-- main content frame
|
||||
local frame = element.add{
|
||||
type='flow',
|
||||
name=PopupFrames.main_frame_name
|
||||
}
|
||||
frame.style.horizontally_stretchable = true
|
||||
|
||||
-- flow for progress bar and close button
|
||||
local close_flow = element.add{
|
||||
type='flow',
|
||||
name=PopupFrames.close_frame_name
|
||||
}
|
||||
close_flow.style.horizontally_stretchable = true
|
||||
|
||||
-- progress bar, when 0 then a static full one is drawn
|
||||
local progress_style
|
||||
if maximum == 0 then
|
||||
progress_style = close_flow.add{
|
||||
type='progressbar',
|
||||
tooltip='No Auto-close',
|
||||
value=1
|
||||
}.style
|
||||
else
|
||||
progress_style = PopupFrames.close_progress(close_flow,maximum).style
|
||||
end
|
||||
progress_style.top_padding = 6
|
||||
progress_style.bottom_padding = 3
|
||||
progress_style.height = 11
|
||||
progress_style.color = Color.grey
|
||||
|
||||
-- close button, will close the popup when clicked
|
||||
local close_button = PopupFrames.close_button(close_flow)
|
||||
Gui.set_padding(close_button)
|
||||
local close_button_style = close_button.style
|
||||
close_button_style.width = 20
|
||||
close_button_style.height = 20
|
||||
|
||||
-- event trigger to draw the gui content
|
||||
self:raise_event('on_creation',player,frame,...)
|
||||
end)
|
||||
|
||||
return self
|
||||
end
|
||||
|
||||
--- Sets the default open time for the popup, will be used if non is provided with open
|
||||
-- @tparam number amount the number of ticks, by default, the popup will be open for
|
||||
-- @treturn table the define to allow for chaining
|
||||
function PopupFrames._prototype:set_default_open_time(amount)
|
||||
self.default_open_time = amount
|
||||
return self
|
||||
end
|
||||
|
||||
--- Opens this define for a player, can be given open time and any other params for the draw function
|
||||
-- @tparam LuaPlayer player the player to open the popup for
|
||||
-- @tparam[opt] number open_time the minimum number of ticks you want the popup open for, 0 means no limit, nil will take default
|
||||
-- @tparam any ... the other params that you want to pass to your on_draw event
|
||||
-- @treturn LuaGuiElement the frame that was drawn, the inner gui flow which contains the content
|
||||
function PopupFrames._prototype:open(player,open_time,...)
|
||||
open_time = open_time or self.default_open_time or 0
|
||||
player = Game.get_player_from_any(player)
|
||||
|
||||
local flow = PopupFrames.get_flow(player)
|
||||
local frame = flow.add{
|
||||
type='frame',
|
||||
style='blurry_frame'
|
||||
}
|
||||
|
||||
Gui.set_padding(frame,3,3,4,4)
|
||||
return self:draw_to(frame,open_time,...)[PopupFrames.main_frame_name]
|
||||
end
|
||||
|
||||
--- When player is first created the popup flow is added to they left flow
|
||||
Event.add(defines.events.on_player_created,function(event)
|
||||
local player = Game.get_player_by_index(event.player_index)
|
||||
local flow = mod_gui.get_frame_flow(player)
|
||||
|
||||
flow.add{
|
||||
type='flow',
|
||||
direction='vertical',
|
||||
name=PopupFrames.popup_flow_name
|
||||
}
|
||||
end)
|
||||
|
||||
--- Every tick any, not paused, progress bars will go down by one tick
|
||||
Event.add(defines.events.on_tick,PopupFrames.close_progress:event_countdown(function(element)
|
||||
return not get_paused_state(element)
|
||||
end))
|
||||
|
||||
return PopupFrames
|
||||
@@ -1,168 +0,0 @@
|
||||
--[[-- Core Module - Gui
|
||||
@module Gui
|
||||
@alias Gui
|
||||
]]
|
||||
|
||||
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.new_concept('progress_bar')
|
||||
:set_tooltip('Basic progress bar')
|
||||
:set_maximum(5)
|
||||
:new_event('on_click',defines.events.on_gui_click)
|
||||
:on_click(function(event)
|
||||
event.concept:increment(event.element)
|
||||
end)
|
||||
:set_delay_completion(true)
|
||||
:on_completion(function(event)
|
||||
event.concept:reset(event.element)
|
||||
end)
|
||||
|
||||
]]
|
||||
|
||||
local progress_bar =
|
||||
Gui.new_concept()
|
||||
:save_as('progress_bar')
|
||||
|
||||
-- Events
|
||||
:new_event('on_completion')
|
||||
|
||||
-- Properties
|
||||
:new_property('tooltip')
|
||||
: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,
|
||||
type = 'progressbar',
|
||||
value = properties.inverted and 1 or 0
|
||||
}
|
||||
|
||||
return element
|
||||
end)
|
||||
|
||||
--- Progress Bars.
|
||||
-- functions used with progress bars
|
||||
-- @section progress_bars
|
||||
|
||||
-- logic for changing the value of a progress bar
|
||||
local function raw_update(concept,element,amount)
|
||||
local old_value = element.value
|
||||
local new_value = old_value + amount
|
||||
element.value = new_value
|
||||
|
||||
local check_value = concept.properties.delay_completion and old_value or new_value
|
||||
if amount < 0 and check_value <= 0 or amount > 0 and check_value >= 1 then
|
||||
concept:raise_event('on_completion',{
|
||||
element = element
|
||||
})
|
||||
else
|
||||
return new_value
|
||||
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
--[[-- Will increase the progress of a progress bar based on this concept, if the concept has an instance store then element acts as the category, if you have a combined store it will NOT update all instances
|
||||
@tparam ?LuaGuiElement|string element either the element that is changed or the category that is being changed (only if an instance store is defined)
|
||||
@tparam[opt=1] number amount the amount that will bar will increase, note that this amount must be less than the max
|
||||
@treturn ?number|nil the new value of the element, use this to sync a data store, if the return is nil then either a instance store was used or the new value may have changed
|
||||
@usage-- Incrementing progress bar with no instance store
|
||||
local new_value = progress_bar:increment(element)
|
||||
@usage-- Incrementing progress bar with an instance store
|
||||
progress_bar:increment(category)
|
||||
]]
|
||||
function progress_bar:increment(element,amount)
|
||||
local properties = self.properties
|
||||
local inverted = properties.inverted
|
||||
local maximum = properties.maximum
|
||||
amount = amount and amount/maximum or 1/maximum
|
||||
amount = inverted and -amount or amount
|
||||
|
||||
if self.instance_store and not self.sync_instance then
|
||||
self.update_instances(element,function(next_element)
|
||||
raw_update(self,next_element,amount)
|
||||
end)
|
||||
|
||||
else
|
||||
return raw_update(self,element,amount)
|
||||
|
||||
end
|
||||
end
|
||||
|
||||
--[[-- Will decrease the progress of a progress bar based on this concept, if the concept has an instance store then element acts as the category, if you have a combined store it will NOT update all instances
|
||||
@tparam ?LuaGuiElement|string element either the element that is changed or the category that is being changed (only if an instance store is defined)
|
||||
@tparam[opt=1] number amount the amount that will bar will decrease, note that this amount must be less than the max
|
||||
@treturn number the new value of the element, use this to sync a data store, if the return is nil then either a instance store was used or the new value may have changed
|
||||
@usage-- Decrementing progress bar with no instance store
|
||||
local new_value = progress_bar:decrement(element)
|
||||
@usage-- Decrementing progress bar with an instance store
|
||||
progress_bar:decrement(category)
|
||||
]]
|
||||
function progress_bar:decrement(element,amount)
|
||||
self:increment(element,-amount)
|
||||
end
|
||||
|
||||
--[[-- Resets the progress back to 0% for this element, if the concept has an instance store then element acts as the category, if you have a combined store it will NOT update all instances
|
||||
@tparam ?LuaGuiElement|string element either the element that is changed or the category that is being changed (only if an instance store is defined)
|
||||
@treturn ?number|nil the new value of the element, use this to sync a data store, if the return is nil then either a instance store was used or the new value may have changed
|
||||
@usage-- Reseting a progress bar with no instance store
|
||||
local new_value = progress_bar:reset(element)
|
||||
@usage-- Reseting a progress bar with an instance store
|
||||
progress_bar:reset(category)
|
||||
]]
|
||||
function progress_bar:reset(element)
|
||||
local new_value = self.properties.inverted and 1 or 0
|
||||
if self.instacne_store and not self.sync_instance then
|
||||
self.update_instances(element,function(next_element)
|
||||
next_element.value = new_value
|
||||
end)
|
||||
|
||||
else
|
||||
element.value = new_value
|
||||
return new_value
|
||||
|
||||
end
|
||||
end
|
||||
|
||||
--[[-- Increment any progress bar by the given percentage
|
||||
@tparam LuaGuiElement element the progress bar that you want to update
|
||||
@tparam[opt=0.01] number amount the percentage that you want to increment the progress bar by
|
||||
@treturn boolean true if the bar is now full
|
||||
@usage-- Increment any progress bar by 10%
|
||||
Gui.increment_progress_bar(element,0.1)
|
||||
]]
|
||||
function Gui.increment_progress_bar(element,amount)
|
||||
amount = amount or 0.01
|
||||
element.value = element.value + amount
|
||||
return element.value >= 1
|
||||
end
|
||||
|
||||
--[[-- Decrement any progress bar by the given percentage
|
||||
@tparam LuaGuiElement element the progress bar that you want to update
|
||||
@tparam[opt=0.01] number amount the percentage that you want to decrement the progress bar by
|
||||
@treturn boolean true if the bar is now empty
|
||||
@usage-- Decrement any progress bar by 10%
|
||||
Gui.decrement_progress_bar(element,0.1)
|
||||
]]
|
||||
function Gui.decrement_progress_bar(element,amount)
|
||||
amount = amount or 0.01
|
||||
element.value = element.value - amount
|
||||
return element.value <= 0
|
||||
end
|
||||
@@ -1,47 +0,0 @@
|
||||
--[[-- Core Module - Gui
|
||||
@module Gui
|
||||
@alias Gui
|
||||
]]
|
||||
|
||||
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.new_concept('scroll')
|
||||
:define_draw(function(properties,parent,element)
|
||||
element.style.hieght = 50
|
||||
for i = 1,10 do
|
||||
element.add{
|
||||
type = 'label',
|
||||
caption = i
|
||||
}
|
||||
end
|
||||
end)
|
||||
|
||||
]]
|
||||
|
||||
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',
|
||||
horizontal_scroll_policy = properties.horizontal_scroll,
|
||||
vertical_scroll_policy = properties.vertical_scroll
|
||||
}
|
||||
|
||||
return element
|
||||
end)
|
||||
@@ -1,91 +0,0 @@
|
||||
--[[-- Core Module - Gui
|
||||
@module Gui
|
||||
@alias Gui
|
||||
]]
|
||||
|
||||
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.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.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()
|
||||
: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',nil,false)
|
||||
:new_property('range',function(properties,minimum,maximum)
|
||||
if type(minimum) == 'function' then
|
||||
properties.range = minimum
|
||||
else
|
||||
properties.minimum = minimum
|
||||
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',
|
||||
caption = properties.caption,
|
||||
minimum_value = properties.minimum,
|
||||
maximum_value = properties.maximum,
|
||||
discrete_slider = properties.discrete_slider,
|
||||
discrete_values = value_step ~= nil,
|
||||
value_step = value_step,
|
||||
value = value
|
||||
}
|
||||
|
||||
-- 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()
|
||||
max = max or element.get_slider_maximum()
|
||||
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
|
||||
end
|
||||
|
||||
return element
|
||||
end)
|
||||
@@ -1,58 +0,0 @@
|
||||
--[[-- Core Module - Gui
|
||||
@module Gui
|
||||
@alias Gui
|
||||
]]
|
||||
|
||||
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.new_concept('table')
|
||||
:set_column_count(5)
|
||||
:define_draw(function(properties,parent,element)
|
||||
for i = 1,25 do
|
||||
element.add{
|
||||
type = 'lable',
|
||||
caption = i
|
||||
}
|
||||
end
|
||||
end)
|
||||
|
||||
]]
|
||||
|
||||
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',
|
||||
column_count = column_count,
|
||||
draw_vertical_lines = properties.vertical_lines,
|
||||
draw_horizontal_lines = properties.horizontal_lines,
|
||||
draw_horizontal_line_after_headers = properties.header_lines,
|
||||
vertical_centering = properties.vertical_centering
|
||||
}
|
||||
|
||||
return element
|
||||
end)
|
||||
@@ -1,84 +0,0 @@
|
||||
--[[-- Core Module - Gui
|
||||
@module Gui
|
||||
@alias Gui
|
||||
]]
|
||||
|
||||
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.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.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()
|
||||
: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('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',
|
||||
tooltip = properties.tooltip,
|
||||
clear_and_focus_on_right_click = properties.clear_on_rmb,
|
||||
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)
|
||||
@@ -1,100 +0,0 @@
|
||||
--[[-- Core Module - Gui
|
||||
@module Gui
|
||||
@alias Gui
|
||||
]]
|
||||
|
||||
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.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.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.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()
|
||||
: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('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',
|
||||
tooltip = properties.tooltip,
|
||||
clear_and_focus_on_right_click = properties.clear_on_rmb,
|
||||
lose_focus_on_confirm = properties.lose_forcus,
|
||||
numeric = properties.is_number or properties.is_decimal or properties.is_negative,
|
||||
allow_decimal = properties.is_decimal,
|
||||
allow_negative = properties.is_negative,
|
||||
is_password = properties.is_password,
|
||||
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)
|
||||
114
expcore/gui/concepts/toolbar.lua
Normal file
114
expcore/gui/concepts/toolbar.lua
Normal file
@@ -0,0 +1,114 @@
|
||||
--[[-- Core Module - Gui
|
||||
@module Gui
|
||||
@alias Prototype
|
||||
]]
|
||||
|
||||
--- Toolbar.
|
||||
-- Gui structure for the toolbar (top left)
|
||||
-- @section toolbar
|
||||
|
||||
--[[
|
||||
>>>> Example format
|
||||
-- this is the same as any other button define, this just automatically draws it
|
||||
-- you can use add_button if you already defined the button
|
||||
local toolbar_button =
|
||||
Toolbar.new_button('print-click')
|
||||
:on_click(function(player,_element)
|
||||
player.print('You clicked a button!')
|
||||
end)
|
||||
|
||||
>>>> Functions
|
||||
Toolbar.new_button(name) --- Adds a new button to the toolbar
|
||||
Toolbar.add_button(button) --- Adds an existing buttton to the toolbar
|
||||
Toolbar.update(player) --- Updates the player's toolbar with an new buttons or expected change in auth return
|
||||
]]
|
||||
local Gui = require 'expcore.gui.core' --- @dep expcore.gui.core
|
||||
local Buttons = require 'expcore.gui.elements.buttons' --- @dep expcore.gui.elements.buttons
|
||||
local Roles = require 'expcore.roles' --- @dep expcore.roles
|
||||
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
|
||||
|
||||
local Toolbar = {
|
||||
permission_names = {},
|
||||
buttons = {}
|
||||
}
|
||||
|
||||
function Toolbar.allowed(player,define_name)
|
||||
local permission_name = Toolbar.permission_names[define_name] or define_name
|
||||
return Roles.player_allowed(player,permission_name)
|
||||
end
|
||||
|
||||
function Toolbar.permission_alias(define_name,permission_name)
|
||||
Toolbar.permission_names[define_name] = permission_name
|
||||
end
|
||||
|
||||
--- Adds a new button to the toolbar
|
||||
-- @tparam[opt] string name when given allows an alias to the button for the permission system
|
||||
-- @treturn table the button define
|
||||
function Toolbar.new_button(name)
|
||||
local button =
|
||||
Buttons.new_button()
|
||||
:set_post_authenticator(Toolbar.allowed)
|
||||
:set_style(mod_gui.button_style,function(style)
|
||||
Gui.set_padding_style(style,-2,-2,-2,-2)
|
||||
end)
|
||||
Toolbar.add_button(button)
|
||||
Toolbar.permission_alias(button.name,name)
|
||||
return button
|
||||
end
|
||||
|
||||
--- Adds an existing buttton to the toolbar
|
||||
-- @tparam table button the button define for the button to be added
|
||||
function Toolbar.add_button(button)
|
||||
table.insert(Toolbar.buttons,button)
|
||||
Gui.allow_player_to_toggle_top_element_visibility(button.name)
|
||||
Gui.on_player_show_top(button.name,function(event)
|
||||
if not button.post_authenticator(event.player,button.name) then
|
||||
event.element.visible = false
|
||||
end
|
||||
end)
|
||||
if not button.post_authenticator then
|
||||
button:set_post_authenticator(function() return true end)
|
||||
end
|
||||
end
|
||||
|
||||
--- Updates the player's toolbar with an new buttons or expected change in auth return
|
||||
-- @tparam LuaPlayer player the player to update the toolbar for
|
||||
function Toolbar.update(player)
|
||||
local top = Gui.get_top_element_flow(player)
|
||||
if not top then return end
|
||||
local visible = top[Gui.top_toggle_button_name].caption == '<'
|
||||
for _,button in pairs(Toolbar.buttons) do
|
||||
local element
|
||||
if top[button.name] then element = top[button.name]
|
||||
else element = button:draw_to(top) end
|
||||
if button.post_authenticator(player,button.name) then
|
||||
element.visible = visible
|
||||
element.enabled = true
|
||||
else
|
||||
element.visible = false
|
||||
element.enabled = false
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
--- When there is a new player they will have the toolbar update
|
||||
Event.add(defines.events.on_player_created,function(event)
|
||||
local player = Game.get_player_by_index(event.player_index)
|
||||
Toolbar.update(player)
|
||||
end)
|
||||
|
||||
--- When a player gets a new role they will have the toolbar updated
|
||||
Event.add(Roles.events.on_role_assigned,function(event)
|
||||
local player = Game.get_player_by_index(event.player_index)
|
||||
Toolbar.update(player)
|
||||
end)
|
||||
|
||||
--- When a player loses a role they will have the toolbar updated
|
||||
Event.add(Roles.events.on_role_unassigned,function(event)
|
||||
local player = Game.get_player_by_index(event.player_index)
|
||||
Toolbar.update(player)
|
||||
end)
|
||||
|
||||
return Toolbar
|
||||
Reference in New Issue
Block a user