Added comments and fixed bugs

This commit is contained in:
Cooldude2606
2019-05-20 22:41:10 +01:00
parent 13b34cbd60
commit 6961614d80
11 changed files with 221 additions and 41 deletions

View File

@@ -14,7 +14,7 @@
Other functions present from expcore.gui.core
]]
local mod_gui = require 'mod-gui'
local Gui = require './core'
local Gui = require 'expcore.gui.core'
local Button = {
config={},

View File

@@ -45,7 +45,7 @@
Other functions present from expcore.gui.core
]]
local Gui = require './core'
local Gui = require 'expcore.gui.core'
local Store = require 'expcore.store'
local Game = require 'utils.game'

View File

@@ -16,7 +16,7 @@
Other functions present from expcore.gui.core
]]
local Gui = require './core'
local Gui = require 'expcore.gui.core'
local Game = require 'utils.game'
--- Event call for on_selection_state_changed and store update

View File

@@ -11,7 +11,7 @@
Other functions present from expcore.gui.core
]]
local Gui = require './core'
local Gui = require 'expcore.gui.core'
local Game = require 'utils.game'
--- Event call for on_elem_changed and store update

View File

@@ -1,9 +1,37 @@
local Gui = require './core'
local Toolbar = require './toolbar'
local Buttons = require './buttons'
--- Gui structure for the toolbar (just under top left)
--[[
>>>> Example Format
local left_gui_frame = LeftFrames.new_frame()
LeftFrames.set_open_by_default(left_gui_frame,true)
LeftFrames.on_update(left_gui_frame,function(frame,player)
frame.add('Hello, World!')
end)
>>>> Functions
LeftFrames.get_flow(player) --- Gets the left frame flow for a player
LeftFrames.get_open(player) --- Gets all open frames for a player, if non are open it will remove the close all button
LeftFrames.get_frame(player,name) --- Gets one frame from the left flow by its name
LeftFrames.toggle_frame(player,name,state) --- Toggles the visiblty of a left frame, or sets its visiblty state
LeftFrames.new_frame(name) --- Makes a new frame that can be used with on_update and adds a toggle button to the toolbar
LeftFrames.add_frame(define_name,permision_name) --- Similar to new_frame but using an already defined name (this will still add a button to the toolbar)
LeftFrames.set_open_by_default(define_name,state) --- Sets if the frame is visible when a player joins, can also be a function to return a boolean
LeftFrames.on_update(define_name,callback) --- Registeres an update function for the gui that will be used to redraw the gui (frame is cleared before call)
LeftFrames.update(define_name,player) --- Clears the gui frame for the player and calls the update callback
LeftFrames.update_all_frames(player) --- Clears all frames and then re-draws all frames
LeftFrames.update_all_players(define_name,update_offline) --- Clears and returns the gui frame for all players
LeftFrames.update_all(update_offline) --- Clears and updates all frames for all players
]]
local Gui = require 'expcore.gui.core'
local Toolbar = require 'expcore.gui.toolbar'
local Buttons = require 'expcore.gui.buttons'
local mod_gui = require 'mod-gui'
local Game = require 'utils.game'
local Events = require 'utils.events'
local Event = require 'utils.event'
local LeftFrames = {
buttons={},
@@ -11,10 +39,17 @@ local LeftFrames = {
open_by_default={}
}
--- Gets the left frame flow for a player
-- @tparam player LuaPlayer 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 all open frames for a player, if non are open it will remove the close all button
-- @tparam player LuaPlayer 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)
@@ -27,13 +62,15 @@ function LeftFrames.get_open(player)
end
end
if #open == 0 then
flow[LeftFrames.toogle_button.name].visible = false
end
flow[LeftFrames.toogle_button.name].visible = #open ~= 0
return open
end
--- Gets one frame from the left flow by its name
-- @tparam player LuaPlayer the player to get the frame of
-- @tparam name string the name of the gui frame to get
-- @treturn LuaGuiElement the frame in the left frame flow with that name
function LeftFrames.get_frame(player,name)
local flow = LeftFrames.get_flow(player)
if flow[name] and flow[name].valid then
@@ -41,29 +78,58 @@ function LeftFrames.get_frame(player,name)
end
end
function LeftFrames.toogle_frame(player,name,state)
--- Toggles the visiblty of a left frame, or sets its visiblty state
-- @tparam player LuaPlayer the player to get the frame of
-- @tparam name string the name of the gui frame to toggle
-- @tparam[opt] state boolean when given will be the state that the visiblty is set to
-- @treturn boolean the new state of the visiblity
function LeftFrames.toggle_frame(player,name,state)
local frame = LeftFrames.get_frame(player,name)
if state ~= nil then
frame.visible = state
else
Gui.toggle_visible(frame)
end
LeftFrames.get_open(player)
return frame.visible
end
--- Gets the button that was created for this left frame
-- @tparam define_name the name of the left gui frame from new_frame
-- @treturn table the define for the toggle button
function LeftFrames.get_button(define_name)
return LeftFrames.buttons[define_name]
end
--- Makes a new frame that can be used with on_update and adds a toggle button to the toolbar
-- @tparam[opt] name string when given allows an alias to the button for the permission system
-- @treturn string the name of the left frame to be used with on_update
-- @treturn table the button define that was created
function LeftFrames.new_frame(name)
local frame_name = Gui.uid_name()
LeftFrames.add_frame(frame_name,name)
return frame_name
local button = LeftFrames.add_frame(frame_name,name)
return frame_name, button
end
--- Similar to new_frame but using an already defined name (this will still add a button to the toolbar)
-- @tparam define_name string the name that is used to refrence this frame (like what is returned by new_frame)
-- @tparam[opt] name string when given allows an alias to the button for the permission system
-- @treturn table the button define that was created
function LeftFrames.add_frame(define_name,permision_name)
LeftFrames.buttons[define_name] =
Toolbar.new_button(permision_name)
:on_click(function(player,_element)
LeftFrames.toogle_frame(player,define_name)
LeftFrames.toggle_frame(player,define_name)
end)
return LeftFrames.buttons[define_name]
end
--- Sets if the frame is visible when a player joins, can also be a function to return a boolean
-- @tparam define_name the name of the left gui frame from new_frame
-- @tparam[opt=true] state ?boolean|function the default state of the visiblty, 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.set_open_by_default(define_name,state)
if not LeftFrames.buttons[define_name] then
return error('Left frame is not registered',2)
@@ -72,14 +138,35 @@ function LeftFrames.set_open_by_default(define_name,state)
LeftFrames.draw_functions[define_name] = state
end
--- Registeres an update function for the gui that will be used to redraw the gui (frame is cleared before call)
-- @tparam define_name the name of the left gui frame from new_frame
-- @tparam callback function the function which is called to update the gui frame
-- callback param - frame LuaGuiElement - the frame which has be cleared to have its elements redrawn
-- callback param - player LuaPlayer - the player who owns the frame
function LeftFrames.on_update(define_name,callback)
if not LeftFrames.buttons[define_name] then
return error('Left frame is not registered',2)
end
LeftFrames.open_by_default[define_name] = callback
LeftFrames.draw_functions[define_name] = callback
end
--- Returns a function that can be called from a factorio event to update the frame
-- @tparam define_name string the name of the left gui frame from new_frame
-- @treturn function when this function is called it will update the frame from event.player_index
function LeftFrames.update_factory(define_name)
if not LeftFrames.draw_functions[define_name] then
return error('Left frame has no update callback',2)
end
return function(event)
LeftFrames.update(define_name,event.player_index)
end
end
--- Clears the gui frame for the player and calls the update callback
-- @tparam define_name the name of the left gui frame from new_frame
-- @tparam player LuaPlayer the player to update the frame for
function LeftFrames.update(define_name,player)
player = Game.get_player_from_any(player)
local frame = LeftFrames.get_frame(player,define_name)
@@ -89,6 +176,8 @@ function LeftFrames.update(define_name,player)
end
end
--- Clears all frames and then re-draws all frames
-- @tparam player LuaPlayer the player to update the frames for
function LeftFrames.update_all_frames(player)
player = Game.get_player_from_any(player)
for define_name,draw_function in pairs(LeftFrames.draw_functions) do
@@ -98,6 +187,9 @@ function LeftFrames.update_all_frames(player)
end
end
--- Clears and returns the gui frame for all players
-- @tparam define_name the name of the left gui frame from new_frame
-- @tparam[opt=false] update_offline boolean when true will also update the frame for offline players
function LeftFrames.update_all_players(define_name,update_offline)
local players = update_offline and game.players or game.connected_players
for _,player in pairs(players) do
@@ -105,6 +197,8 @@ function LeftFrames.update_all_players(define_name,update_offline)
end
end
--- Clears and updates all frames for all players
-- @tparam[opt=false] update_offline boolean when true will also update the frame for offline players
function LeftFrames.update_all(update_offline)
local players = update_offline and game.players or game.connected_players
for _,player in pairs(players) do
@@ -114,6 +208,7 @@ end
LeftFrames.toogle_button =
Buttons.new_button()
:set_tooltip('Close Windows')
:set_caption('<')
:on_click(function(player,_element)
local flow = LeftFrames.get_flow(player)
@@ -125,13 +220,22 @@ Buttons.new_button()
end
end
end
_element.visible = false
end)
Events.add(defines.events.on_player_created,function(event)
local player = Game.get_plyaer_by_index(event.player_index)
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)
LeftFrames.toogle_button(flow)
local style = LeftFrames.toogle_button(flow).style
style.width = 18
style.height = 36
style.left_padding = 0
style.top_padding = 0
style.right_padding = 0
style.bottom_padding = 0
style.font = 'default-small-bold'
for define_name,_ in pairs(LeftFrames.buttons) do
local frame = flow.add{
@@ -142,5 +246,22 @@ Events.add(defines.events.on_player_created,function(event)
if LeftFrames.draw_functions[define_name] then
LeftFrames.draw_functions[define_name](frame,player)
end
if LeftFrames.open_by_default[define_name] == false then
frame.visible = false
elseif type(LeftFrames.open_by_default[define_name]) == 'function' then
if not LeftFrames.open_by_default[define_name](player,define_name) then
frame.visible = false
end
end
if not Toolbar.allowed(player,define_name) then
frame.visible = false
end
end
end)
LeftFrames.get_open(player)
end)
return LeftFrames

View File

@@ -12,8 +12,8 @@
Other functions present from expcore.gui.core
]]
local Gui = require './core'
local Instances = require './instances'
local Gui = require 'expcore.gui.core'
local Instances = require 'expcore.gui.instances'
local Game = require 'utils.game'
--- Event call for on_value_changed and store update

View File

@@ -4,7 +4,7 @@
local Gui = require 'expcore.gui'
local format_chat_colour,table_keys = ext_require('expcore.common','format_chat_colour','table_keys')
local Colors = require 'resources.color_presets'
local Game = require 'utils.game'
local Event = require 'utils.event'
local tests = {}
@@ -92,6 +92,35 @@ end)
end
end)
--[[
Left Frame Test
> Left frame which holds all online player names, updates when player leaves or joins
]]
local left_frame_name,left_gui_button =
Gui.new_left_frame('test-left-frame')
Gui.set_left_open_by_default(left_frame_name,true)
Gui.on_left_update(left_frame_name,function(frame,_player)
for _,player in pairs(game.connected_players) do
frame.add{
type='label',
caption=player.name
}
end
end)
left_gui_button
:set_caption('Test Left Gui')
:set_post_authenticator(function(player,button_name)
return global.show_test_gui
end)
local update_left_frame = Gui.left_update_factory(left_frame_name)
Event.add(defines.events.on_player_joined_game,update_left_frame)
Event.add(defines.events.on_player_left_game,update_left_frame)
--[[
Button Tests
> No display - Simple button which has no display

View File

@@ -14,7 +14,7 @@
Other functions present from expcore.gui.core
]]
local Gui = require './core'
local Gui = require 'expcore.gui.core'
local Game = require 'utils.game'
--- Event call for on_text_changed and store update

View File

@@ -1,12 +1,21 @@
--- Gui structure for the toolbar (top left)
--[[
>>>> 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 Buttons = require './buttons'
local Gui = require './core'
local Buttons = require 'expcore.gui.buttons'
local Gui = require 'expcore.gui.core'
local Roles = require 'expcore.roles'
local Event = require 'utils.event'
local Game = require 'utils.game'
@@ -16,7 +25,7 @@ local Toolbar = {
buttons = {}
}
local function toolbar_allow(player,define_name)
function Toolbar.allowed(player,define_name)
local permisison_name = Toolbar.permisison_names[define_name] or define_name
return Roles.player_allowed(player,permisison_name)
end
@@ -26,11 +35,11 @@ function Toolbar.permission_alias(define_name,permisison_name)
end
--- Adds a new button to the toolbar
-- @tparam[opt] name string the name of the button to be added
-- @tparam[opt] name string 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()
button:set_post_authenticator(toolbar_allow)
button:set_post_authenticator(Toolbar.allowed)
Toolbar.add_button(button)
Toolbar.permission_alias(button.name,name)
return button
@@ -69,6 +78,7 @@ function Toolbar.update(player)
element.enabled = false
end
end
log(table.inspect(Toolbar.buttons))
end
--- When there is a new player they will have the toolbar update

View File

@@ -1,7 +1,7 @@
--- This file is used to require all the different elements of the gui module
-- each module has an outline here but for more details see their seperate files in ./gui
local Gui = require('./gui/core')
local Gui = require('expcore.gui.core')
--[[
Gui._prototype_factory(tbl) --- Used internally to create new prototypes for element defines
Gui._event_factory(name) --- Used internally to create event handler adders for element defines
@@ -36,14 +36,14 @@ local Gui = require('./gui/core')
Gui.toggle_visible(element) --- Will toggle the visiblity of an element
]]
local Instances = require('./gui/instances')
local Instances = require('expcore.gui.instances')
Gui.new_instance_group = Instances.registers
Gui.get_instances = Instances.get_elements
Gui.add_instance = Instances.get_elements
Gui.update_instances = Instances.apply_to_elements
Gui.classes.instances = Instances
local Button = require('./gui/buttons')
local Button = require('expcore.gui.buttons')
Gui.new_button = Button.new_button
Gui.classes.button = Button
--[[
@@ -58,7 +58,7 @@ Gui.classes.button = Button
Button._prototype:set_key_filter(filter,...) --- Adds a control key filter to the button
]]
local Checkbox = require('./gui/checkboxs')
local Checkbox = require('expcore.gui.checkboxs')
Gui.new_checkbox = Checkbox.new_checkbox
Gui.new_radiobutton = Checkbox.new_radiobutton
Gui.new_radiobutton_option_set = Checkbox.new_option_set
@@ -80,7 +80,7 @@ Gui.classes.checkbox = Checkbox
Checkbox.reset_radiobutton(element,exclude,recursive) --- Sets all radiobutotn in a element to false (unless excluded) and can act recursivly
]]
local Dropdown = require('./gui/dropdown')
local Dropdown = require('expcore.gui.dropdown')
Gui.new_dropdown = Dropdown.new_dropdown
Gui.new_list_box = Dropdown.new_list_box
Gui.classes.dropdown = Dropdown
@@ -99,7 +99,7 @@ Gui.classes.dropdown = Dropdown
Dropdown.get_selected_value(element) --- Returns the currently selected value rather than index
]]
local Slider = require('./gui/slider')
local Slider = require('expcore.gui.slider')
Gui.new_slider = Slider.new_slider
Gui.classes.slider = Slider
--[[
@@ -113,7 +113,7 @@ Gui.classes.slider = Slider
Slider._prototype:enable_auto_draw_label(state) --- Enables auto draw of the label, the label will share the same parent element as the slider
]]
local Text = require('./gui/text')
local Text = require('expcore.gui.text')
Gui.new_text_filed = Text.new_text_field
Gui.new_text_box = Text.new_text_box
Gui.classes.text = Text
@@ -130,7 +130,7 @@ Gui.classes.text = Text
Text._prototype_box:set_read_only(state) --- Sets the text box to be read only
]]
local ElemButton = require('./gui/elem-button')
local ElemButton = require('expcore.gui.elem-button')
Gui.new_elem_button = ElemButton.new_elem_button
Gui.classes.elem_button = ElemButton
--[[
@@ -143,7 +143,7 @@ Gui.classes.elem_button = ElemButton
ElemButton._prototype:set_default(value) --- Sets the default value for the elem button, this may be a function or a string
]]
local Toolbar = require('./gui/toolbar')
local Toolbar = require('expcore.gui.toolbar')
Gui.new_toolbar_button = Toolbar.new_button
Gui.add_button_to_toolbar = Toolbar.add_button
Gui.update_toolbar = Toolbar.update
@@ -154,12 +154,32 @@ Gui.classes.toolbar = Toolbar
Toolbar.update(player) --- Updates the player's toolbar with an new buttons or expected change in auth return
]]
local LeftFrames = require('./gui/left')
local LeftFrames = require('expcore.gui.left')
Gui.new_left_frame = LeftFrames.new_frame
Gui.add_frame_to_left_frames = LeftFrames.add_frame
Gui.set_left_open_by_default = LeftFrames.set_open_by_default
Gui.on_left_frame_update = LeftFrames.on_update
Gui.on_left_update = LeftFrames.on_update
Gui.left_update_factory = LeftFrames.update_factory
Gui.update_left_frames = LeftFrames.update_all_frames
Gui.update_left_frame = LeftFrames.update
Gui.get_left_frame = LeftFrames.get_frame
Gui.classes.left_frames = LeftFrames
--[[
LeftFrames.get_flow(player) --- Gets the left frame flow for a player
LeftFrames.get_open(player) --- Gets all open frames for a player, if non are open it will remove the close all button
LeftFrames.get_frame(player,name) --- Gets one frame from the left flow by its name
LeftFrames.toggle_frame(player,name,state) --- Toggles the visiblty of a left frame, or sets its visiblty state
LeftFrames.new_frame(name) --- Makes a new frame that can be used with on_update and adds a toggle button to the toolbar
LeftFrames.add_frame(define_name,permision_name) --- Similar to new_frame but using an already defined name (this will still add a button to the toolbar)
LeftFrames.set_open_by_default(define_name,state) --- Sets if the frame is visible when a player joins, can also be a function to return a boolean
LeftFrames.on_update(define_name,callback) --- Registeres an update function for the gui that will be used to redraw the gui (frame is cleared before call)
LeftFrames.update(define_name,player) --- Clears the gui frame for the player and calls the update callback
LeftFrames.update_all_frames(player) --- Clears all frames and then re-draws all frames
LeftFrames.update_all_players(define_name,update_offline) --- Clears and returns the gui frame for all players
LeftFrames.update_all(update_offline) --- Clears and updates all frames for all players
]]
return Gui

View File

@@ -17,7 +17,7 @@ local interface_modules = {
-- loads all the modules given in the above table
for key,value in pairs(interface_modules) do
if type(value) == 'string' then
interface_modules[key] = require(value)
interface_modules[key] = Common.opt_require(value)
end
end