Refactor of left frames

This commit is contained in:
Cooldude2606
2019-05-21 19:04:46 +01:00
parent 6961614d80
commit 3a5599fcd2
4 changed files with 189 additions and 174 deletions

View File

@@ -1,30 +1,48 @@
--- Gui structure for the toolbar (just under top left) --- Gui structure define for left frames
--[[ --[[
>>>> Example Format >>>> Example formating
local left_gui_frame = LeftFrames.new_frame()
LeftFrames.set_open_by_default(left_gui_frame,true) -- first we add config that relates to the button on the toolbar, all normal button functions are present
local left_frame =
LeftFrames.on_update(left_gui_frame,function(frame,player) Gui.new_left_frame('test-left-frame')
frame.add('Hello, World!') :set_caption('Test Left Gui')
:set_post_authenticator(function(player,button_name)
return global.show_test_gui
end) 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 >>>> Functions
LeftFrames.get_flow(player) --- Gets the left frame flow for a player 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.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(name,player,state) --- Toggles the visiblty of a left frame, or sets its visiblty state
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.new_frame(permision_name) --- Creates a new left frame define
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._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: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 visiblty of the left frame
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._prototype:update(player) --- Updates the contents of the left frame, first tries update callback, oter wise will clear and redraw
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._prototype:update_all(update_offline) --- Updates the frame for all players, see update
LeftFrames.update(define_name,player) --- Clears the gui frame for the player and calls the update callback 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.update_all_frames(player) --- Clears all frames and then re-draws all frames LeftFrames._prototype:event_handler(action) --- Creates an event handler that will trigger one of its functions, use with Event.add
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 Gui = require 'expcore.gui.core'
local Toolbar = require 'expcore.gui.toolbar' local Toolbar = require 'expcore.gui.toolbar'
@@ -34,10 +52,15 @@ local Game = require 'utils.game'
local Event = require 'utils.event' local Event = require 'utils.event'
local LeftFrames = { local LeftFrames = {
buttons={}, frames={},
draw_functions={}, _prototype=Gui._prototype_factory{
open_by_default={} on_draw = Gui._event_factory('on_draw'),
on_update = Gui._event_factory('on_update')
}
} }
setmetatable(LeftFrames._prototype, {
__index = Buttons._prototype
})
--- Gets the left frame flow for a player --- Gets the left frame flow for a player
-- @tparam player LuaPlayer the player to get the flow of -- @tparam player LuaPlayer the player to get the flow of
@@ -47,6 +70,18 @@ function LeftFrames.get_flow(player)
return mod_gui.get_frame_flow(player) return mod_gui.get_frame_flow(player)
end end
--- Gets one frame from the left flow by its name
-- @tparam name string the name of the gui frame to get
-- @tparam player LuaPlayer 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 --- 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 -- @tparam player LuaPlayer the player to get the flow of
-- @treturn table contains all the open (and registered) frames for the player -- @treturn table contains all the open (and registered) frames for the player
@@ -54,181 +89,169 @@ function LeftFrames.get_open(player)
local open = {} local open = {}
local flow = LeftFrames.get_flow(player) local flow = LeftFrames.get_flow(player)
for _,child in pairs(flow.children) do for _,define in pairs(LeftFrames.frames) do
if LeftFrames.buttons[child.name] then if define:is_open(player) then
if child.valid and child.visible then table.insert(open,define)
table.insert(open,child)
end
end end
end end
flow[LeftFrames.toogle_button.name].visible = #open ~= 0 flow[LeftFrames.toggle_button.name].visible = #open ~= 0
return open return open
end 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
return flow[name]
end
end
--- Toggles the visiblty of a left frame, or sets its visiblty 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 name string the name of the gui frame to toggle
-- @tparam player LuaPlayer the player to get the frame of
-- @tparam[opt] state boolean when given will be the state that the visiblty is set to -- @tparam[opt] state boolean when given will be the state that the visiblty is set to
-- @treturn boolean the new state of the visiblity -- @treturn boolean the new state of the visiblity
function LeftFrames.toggle_frame(player,name,state) function LeftFrames.toggle_frame(name,player,state)
local frame = LeftFrames.get_frame(player,name) 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 if state ~= nil then
frame.visible = state frame.visible = state
else else
Gui.toggle_visible(frame) Gui.toggle_visible(frame)
end end
LeftFrames.get_open(player) LeftFrames.get_open(player)
return frame.visible return frame.visible
end end
--- Gets the button that was created for this left frame --- Creates a new left frame define
-- @tparam define_name the name of the left gui frame from new_frame -- @tparam permision_name string the name that can be used with the permision system
-- @treturn table the define for the toggle button -- @treturn table the new left frame define
function LeftFrames.get_button(define_name) function LeftFrames.new_frame(permision_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 local self = Toolbar.new_button(permision_name)
-- @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()
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) local mt = getmetatable(self)
-- @tparam define_name string the name that is used to refrence this frame (like what is returned by new_frame) mt.__index = LeftFrames._prototype
-- @tparam[opt] name string when given allows an alias to the button for the permission system mt.__call = self.event_handler
-- @treturn table the button define that was created
function LeftFrames.add_frame(define_name,permision_name) self:on_click(function(player,_element)
LeftFrames.buttons[define_name] = self:toggle(player)
Toolbar.new_button(permision_name)
:on_click(function(player,_element)
LeftFrames.toggle_frame(player,define_name)
end) end)
return LeftFrames.buttons[define_name]
LeftFrames.frames[self.name] = self
return self
end end
--- Sets if the frame is visible when a player joins, can also be a function to return a boolean --- 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 -- @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 - player LuaPlayer - the player that has joined the game
-- state param - define_name string - the define name for the frame -- state param - define_name string - the define name for the frame
-- state return - boolean - false will hide the frame -- state return - boolean - false will hide the frame
function LeftFrames.set_open_by_default(define_name,state) function LeftFrames._prototype:set_open_by_default(state)
if not LeftFrames.buttons[define_name] then if state == false then
return error('Left frame is not registered',2) self.open_by_default = false
else
self.open_by_default = state
end end
return self
LeftFrames.draw_functions[define_name] = state
end end
--- Registeres an update function for the gui that will be used to redraw the gui (frame is cleared before call) --- Gets the frame for this define from the left frame flow
-- @tparam define_name the name of the left gui frame from new_frame -- @tparam player LuaPlayer the player to get the frame of
-- @tparam callback function the function which is called to update the gui frame -- @treturn LuaGuiElement the frame in the left frame flow for this define
-- callback param - frame LuaGuiElement - the frame which has be cleared to have its elements redrawn function LeftFrames._prototype:get_frame(player)
-- callback param - player LuaPlayer - the player who owns the frame local flow = LeftFrames.get_flow(player)
function LeftFrames.on_update(define_name,callback) if flow[self.name] and flow[self.name].valid then
if not LeftFrames.buttons[define_name] then return flow[self.name]
return error('Left frame is not registered',2)
end
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
end end
--- Clears the gui frame for the player and calls the update callback --- Returns if the player currently has this define visible
-- @tparam define_name the name of the left gui frame from new_frame -- @tparam player LuaPlayer the player to get the frame of
-- @tparam player LuaPlayer the player to update the frame for -- @treturn boolean true if it is open/visible
function LeftFrames.update(define_name,player) function LeftFrames._prototype:is_open(player)
player = Game.get_player_from_any(player) local frame = self:get_frame(player)
local frame = LeftFrames.get_frame(player,define_name) return frame and frame.visible or false
frame.clear()
if LeftFrames.draw_functions[define_name] then
LeftFrames.draw_functions[define_name](frame,player)
end
end end
--- Clears all frames and then re-draws all frames --- Toggles the visiblty of the left frame
-- @tparam player LuaPlayer the player to update the frames for -- @tparam player LuaPlayer the player to toggle the frame of
function LeftFrames.update_all_frames(player) -- @treturn boolean the new state of the visiblity
player = Game.get_player_from_any(player) function LeftFrames._prototype:toggle(player)
for define_name,draw_function in pairs(LeftFrames.draw_functions) do local frame = self:get_frame(player)
local frame = LeftFrames.get_frame(player,define_name) Gui.toggle_visible(frame)
LeftFrames.get_open(player)
return frame.visible
end
--- Updates the contents of the left frame, first tries update callback, oter wise will clear and redraw
-- @tparam player LuaPlayer the player to update the frame of
function LeftFrames._prototype:update(player)
local frame = self:get_frame(player)
if self.events.on_update then
self.events.on_update(player,frame)
elseif self.events.on_draw then
frame.clear() frame.clear()
draw_function(frame,player) self.events.on_draw(player,frame)
end end
end end
--- Clears and returns the gui frame for all players --- Updates the frame for all players, see update
-- @tparam define_name the name of the left gui frame from new_frame -- @tparam[opt=false] update_offline boolean when true will update the frame for offline players
-- @tparam[opt=false] update_offline boolean when true will also update the frame for offline players function LeftFrames._prototype:update_all(update_offline)
function LeftFrames.update_all_players(define_name,update_offline) local players = update_offline == true and game.players or game.connected_players
local players = update_offline and game.players or game.connected_players
for _,player in pairs(players) do for _,player in pairs(players) do
LeftFrames.update(define_name,player) self:update(player)
end end
end end
--- Clears and updates all frames for all players --- Redraws the frame by calling on_draw, will always clear the frame
-- @tparam[opt=false] update_offline boolean when true will also update the frame for offline players -- @tparam player LuaPlayer the player to update the frame of
function LeftFrames.update_all(update_offline) function LeftFrames._prototype:redraw(player)
local players = update_offline and game.players or game.connected_players local frame = self:get_frame(player)
frame.claer()
if self.events.on_draw then
self.events.on_draw(player,frame)
end
end
--- Redraws the frame for all players, see redraw
-- @tparam[opt=false] update_offline boolean 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 for _,player in pairs(players) do
LeftFrames.update_all_frames(player) self:redraw(player)
end end
end end
LeftFrames.toogle_button = --- Creates an event handler that will trigger one of its functions, use with Event.add
-- @tparam[opt=update] action string the action to take on this event
function LeftFrames._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
LeftFrames.toggle_button =
Buttons.new_button() Buttons.new_button()
:set_tooltip('Close Windows') :set_tooltip('Close Windows')
:set_caption('<') :set_caption('<')
:on_click(function(player,_element) :on_click(function(player,element)
local flow = LeftFrames.get_flow(player) for _,define in pairs(LeftFrames.frames) do
local frame = LeftFrames.get_frame(define.name,player)
for _,child in pairs(flow.children) do frame.visible = false
if LeftFrames.buttons[child.name] then
if child.valid and child.visible then
child.visible = false
end
end
end end
element.visible = false
_element.visible = false
end) end)
Event.add(defines.events.on_player_created,function(event) Event.add(defines.events.on_player_created,function(event)
local player = Game.get_player_by_index(event.player_index) local player = Game.get_player_by_index(event.player_index)
local flow = LeftFrames.get_flow(player) local flow = LeftFrames.get_flow(player)
local style = LeftFrames.toogle_button(flow).style local style = LeftFrames.toggle_button(flow).style
style.width = 18 style.width = 18
style.height = 36 style.height = 36
style.left_padding = 0 style.left_padding = 0
@@ -237,25 +260,25 @@ Event.add(defines.events.on_player_created,function(event)
style.bottom_padding = 0 style.bottom_padding = 0
style.font = 'default-small-bold' style.font = 'default-small-bold'
for define_name,_ in pairs(LeftFrames.buttons) do for _,define in pairs(LeftFrames.frames) do
local frame = flow.add{ local frame = flow.add{
type='frame', type='frame',
name=define_name name=define.name
} }
if LeftFrames.draw_functions[define_name] then if define.events.on_draw then
LeftFrames.draw_functions[define_name](frame,player) define.events.on_draw(player,frame)
end end
if LeftFrames.open_by_default[define_name] == false then if define.open_by_default == false then
frame.visible = false frame.visible = false
elseif type(LeftFrames.open_by_default[define_name]) == 'function' then elseif type(define.open_by_default) == 'function' then
if not LeftFrames.open_by_default[define_name](player,define_name) then if not define.open_by_default(player,define.name) then
frame.visible = false frame.visible = false
end end
end end
if not Toolbar.allowed(player,define_name) then if not Toolbar.allowed(player,define.name) then
frame.visible = false frame.visible = false
end end

View File

@@ -97,12 +97,15 @@ end)
> Left frame which holds all online player names, updates when player leaves or joins > Left frame which holds all online player names, updates when player leaves or joins
]] ]]
local left_frame_name,left_gui_button = local left_frame =
Gui.new_left_frame('test-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)
Gui.set_left_open_by_default(left_frame_name,true) :set_open_by_default()
:on_draw(function(_player,frame)
Gui.on_left_update(left_frame_name,function(frame,_player)
for _,player in pairs(game.connected_players) do for _,player in pairs(game.connected_players) do
frame.add{ frame.add{
type='label', type='label',
@@ -111,15 +114,8 @@ Gui.on_left_update(left_frame_name,function(frame,_player)
end end
end) end)
left_gui_button Event.add(defines.events.on_player_joined_game,left_frame 'update_all')
:set_caption('Test Left Gui') Event.add(defines.events.on_player_left_game,left_frame 'update_all')
: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 Button Tests

View File

@@ -78,7 +78,6 @@ function Toolbar.update(player)
element.enabled = false element.enabled = false
end end
end end
log(table.inspect(Toolbar.buttons))
end end
--- When there is a new player they will have the toolbar update --- When there is a new player they will have the toolbar update

View File

@@ -155,31 +155,28 @@ Gui.classes.toolbar = Toolbar
]] ]]
local LeftFrames = require('expcore.gui.left') local LeftFrames = require('expcore.gui.left')
Gui.get_left_frame_flow = LeftFrames.get_flow
Gui.toggle_left_frame = LeftFrames.toggle_frame
Gui.new_left_frame = LeftFrames.new_frame 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_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 Gui.classes.left_frames = LeftFrames
--[[ --[[
LeftFrames.get_flow(player) --- Gets the left frame flow for a player 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.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(name,player,state) --- Toggles the visiblty of a left frame, or sets its visiblty state
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.new_frame(permision_name) --- Creates a new left frame define
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._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: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 visiblty of the left frame
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._prototype:update(player) --- Updates the contents of the left frame, first tries update callback, oter wise will clear and redraw
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._prototype:update_all(update_offline) --- Updates the frame for all players, see update
LeftFrames.update(define_name,player) --- Clears the gui frame for the player and calls the update callback 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.update_all_frames(player) --- Clears all frames and then re-draws all frames LeftFrames._prototype:event_handler(action) --- Creates an event handler that will trigger one of its functions, use with Event.add
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 return Gui