From 2d1de044dd15af1060c423da0a27516824fdef01 Mon Sep 17 00:00:00 2001 From: Cooldude2606 Date: Mon, 31 Dec 2018 13:54:06 +0000 Subject: [PATCH] Fixed Admin gui not closing; Gui draw more consistent --- modules/ExpGamingAdmin/Gui/control.lua | 23 ++------- modules/ExpGamingCore/Gui/center/control.lua | 49 ++++++++++--------- modules/ExpGamingCore/Gui/left/control.lua | 2 +- modules/ExpGamingCore/Gui/popup/control.lua | 4 +- modules/ExpGamingCore/Gui/src/test.lua | 6 +-- modules/ExpGamingInfo/Rockets/control.lua | 2 +- modules/ExpGamingInfo/Science/control.lua | 2 +- modules/ExpGamingInfo/Tasklist/control.lua | 2 +- .../ExpGamingPlayer/playerInfo/control.lua | 2 +- .../ExpGamingPlayer/playerList/control.lua | 4 +- modules/ExpGamingPlayer/polls/control.lua | 4 +- modules/GuiAnnouncements/control.lua | 4 +- modules/WarpPoints/control.lua | 2 +- 13 files changed, 48 insertions(+), 58 deletions(-) diff --git a/modules/ExpGamingAdmin/Gui/control.lua b/modules/ExpGamingAdmin/Gui/control.lua index 1d86d808..d26b7910 100644 --- a/modules/ExpGamingAdmin/Gui/control.lua +++ b/modules/ExpGamingAdmin/Gui/control.lua @@ -16,7 +16,7 @@ local mod_gui = require('mod-gui') local module_verbose = false local AdminGui = { on_init=function() - if loaded_modules['ExpGamingPlayer.playerInfo'] then playerInfo = require('ExpGamingPlayer') + if loaded_modules['ExpGamingPlayer.playerInfo'] then playerInfo = require('ExpGamingPlayer.playerInfo') else playerInfo = function(player,frame) frame.add{ type='label', @@ -30,7 +30,7 @@ local AdminGui = { function Admin.open(player,pre_select_player,pre_select_action) Gui.center.clear(player) - Admin.center.open(player,pre_select_player,pre_select_action) + Admin.center(player,pre_select_player,pre_select_action) end -- Function Define @@ -140,24 +140,7 @@ Admin.center = Gui.center{ name='admin-commands', caption='utility/danger_icon', tooltip={'ExpGamingAdmin.tooltip'}, - open=function(event,pre_select_player,pre_select_action) - local _player = Game.get_player(pre_select_player) - local player = Game.get_player(event) - local _center = Gui.data.center['admin-commands'] - local center_flow = Gui.center.get_flow(player) - if center_flow[_center.name] then Gui.center.clear(player) return end - local center_frame = center_flow.add{ - name=_center.name, - type='frame', - direction='vertical', - style=mod_gui.frame_style - } - -- only edit i made was passing diffrent arguments to the draw function, try to avoid this - local success, err = pcall(_center.draw,center_frame,_player,pre_select_action) - if not success then error(err) end - player.opened=center_frame - end, - draw=function(frame,pre_select_player,pre_select_action) + draw=function(self,frame,pre_select_player,pre_select_action) frame.caption={'ExpGamingAdmin.name'} local frame = frame.add{ type='flow', diff --git a/modules/ExpGamingCore/Gui/center/control.lua b/modules/ExpGamingCore/Gui/center/control.lua index a1f56b83..f3040cee 100644 --- a/modules/ExpGamingCore/Gui/center/control.lua +++ b/modules/ExpGamingCore/Gui/center/control.lua @@ -21,11 +21,11 @@ function center.add(obj) if not is_type(obj,'table') then return end if not is_type(obj.name,'string') then return end verbose('Created Center Gui: '..obj.name) - setmetatable(obj,{__index=center._prototype,__call=function(self,player) return center.open(player,self.name) end}) + setmetatable(obj,{__index=center._prototype,__call=function(self,player,...) return center.open(player,self.name,...) end}) obj.tabs = {} obj._tabs = {} Gui.data('center',obj.name,obj) - if Gui.toolbar then Gui.toolbar(obj.name,obj.caption,obj.tooltip,obj.open) end + if Gui.toolbar then Gui.toolbar(obj.name,obj.caption,obj.tooltip,function(event) return obj:open(event.player_index) end) end return obj end @@ -39,23 +39,31 @@ function center.get_flow(player) return player.gui.center.exp_center or player.gui.center.add{name='exp_center',type='flow'} end ---- Used to open a center frame for a player +--- Used to open a center frame for a player, extra params are sent to open event -- @usage Gui.center.open(player,'server-info') -- return true -- @param player a player indifier to get the flow for -- @tparam string center the name of the center frame to open -- @treturn boelon based on if it successed or not -function center.open(player,center) +function center.open(player,center,...) local player = Game.get_player(player) if not player then error('Invalid player',2) return false end Gui.center.clear(player) if not Gui.data.center[center] then return false end - Gui.data.center[center].open{ - element={name=center}, - player_index=player.index - } + local self = Gui.data.center[center] + -- this function is the draw function passed to the open event + self:open(player,function(...) Gui.center._draw(self,...) end,...) return true end +-- used as a pice of middle ware for the open event +function center._draw(self,frame,...) + game.players[frame.player_index].opened=frame + if is_type(self.draw,'function') then + local success, err = pcall(self.draw,self,frame,...) + if not success then error(err) end + else error('No Callback on center frame '..self.name) end +end + --- Used to open a center frame for a player -- @usage Gui.center.open_tab(player,'readme','rules') -- return true -- @param player a player indifier to get the flow for @@ -82,27 +90,24 @@ function center.clear(player) center.get_flow(player).clear() end --- used on the button press when the toolbar button is press, can be overriden --- not recomented for direct use see Gui.center.open -function center._prototype.open(event) - local player = Game.get_player(event) - local _center = Gui.data.center[event.element.name] +-- opens this gui for this player, draw is the draw function when event is called from center.open +-- this is the default function it can be overriden when the gui is defined, simply call draw on the frame you create +-- extra values passed to draw will also be passed to the draw event +-- extra values from center.draw and passed to the open event +function center._prototype:open(player,draw,...) + local player = Game.get_player(player) + local draw = draw or function(...) center._draw(self,...) end local center_flow = center.get_flow(player) - if center_flow[_center.name] then Gui.center.clear(player) return end + if center_flow[self.name] then Gui.center.clear(player) return end local center_frame = center_flow.add{ - name=_center.name, + name=self.name, type='frame', - caption=_center.caption, + caption=self.caption, direction='vertical', style=mod_gui.frame_style } if is_type(center_frame.caption,'string') and player.gui.is_valid_sprite_path(center_frame.caption) then center_frame.caption = '' end - if is_type(_center.draw,'function') then - local success, err = pcall(_center.draw,_center,center_frame) - if not success then error(err) end - else error('No Callback on center frame '.._center.name) - end - player.opened=center_frame + draw(center_frame,...) end -- this is the default draw function if one is not provided, can be overriden diff --git a/modules/ExpGamingCore/Gui/left/control.lua b/modules/ExpGamingCore/Gui/left/control.lua index ed89957f..b3c3510d 100644 --- a/modules/ExpGamingCore/Gui/left/control.lua +++ b/modules/ExpGamingCore/Gui/left/control.lua @@ -168,7 +168,7 @@ function left._prototype:first_open(player) frame.style.visible = false if is_type(self.open_on_join,'boolean') then frame.style.visible = self.open_on_join left_flow['gui-left-hide'].style.visible = true end end - if is_type(self.draw,'function') then self.draw(frame) else frame.style.visible = false error('No Callback On '..self.name) end + if is_type(self.draw,'function') then self:draw(frame) else frame.style.visible = false error('No Callback On '..self.name) end return frame end diff --git a/modules/ExpGamingCore/Gui/popup/control.lua b/modules/ExpGamingCore/Gui/popup/control.lua index 9f74c92b..cbb7682b 100644 --- a/modules/ExpGamingCore/Gui/popup/control.lua +++ b/modules/ExpGamingCore/Gui/popup/control.lua @@ -68,7 +68,7 @@ function popup.open(style,data,players) } _popup.close(_frame) if is_type(_popup.draw,'function') then - local success, err = pcall(_popup.draw,frame,data) + local success, err = pcall(_popup.draw,_popup,frame,data) if not success then error(err) end else error('No Draw On Popup '.._popup.name) end end @@ -94,7 +94,7 @@ function popup.open(style,data,players) } self.data.popup.close(_frame) if is_type(self.data.popup.draw,'function') then - local success, err = pcall(self.data.popup.draw,frame,self.data.data) + local success, err = pcall(self.data.popup.draw,self.data.popup,frame,self.data.data) if not success then error(err) end else error('No Draw On Popup '..self.data.popup.name) end end):open() diff --git a/modules/ExpGamingCore/Gui/src/test.lua b/modules/ExpGamingCore/Gui/src/test.lua index 21b4ff42..ab3e8d27 100644 --- a/modules/ExpGamingCore/Gui/src/test.lua +++ b/modules/ExpGamingCore/Gui/src/test.lua @@ -143,7 +143,7 @@ Gui.left{ name='test-left', caption='Gui Left', tooltip='just testing', - draw=function(frame) + draw=function(self,frame) for _,player in pairs(game.connected_players) do frame.add{type='label',caption=player.name} end @@ -166,14 +166,14 @@ end) Gui.popup{ name='test-popup', caption='Gui Popup', - draw=function(frame,data) + draw=function(self,frame,data) frame.add{type='label',caption='Opened by: '..data.player} frame.add{type='label',caption='Message: '..data.message} end }:add_left{ caption='Gui Left w/ Popup', tooltip='Send a message', - draw=function(frame) + function(self,frame) text_popup:draw(frame) send_popup:draw(frame) end diff --git a/modules/ExpGamingInfo/Rockets/control.lua b/modules/ExpGamingInfo/Rockets/control.lua index 0af0b354..243514e8 100644 --- a/modules/ExpGamingInfo/Rockets/control.lua +++ b/modules/ExpGamingInfo/Rockets/control.lua @@ -31,7 +31,7 @@ ThisModule.Gui = Gui.left{ name='rockets', caption='item/rocket-silo', tooltip={'ExpGamingInfo-Rockets.tooltip'}, - draw=function(frame) + draw=function(self,frame) frame.caption = {'ExpGamingInfo-Rockets.name'} local player = Game.get_player(frame.player_index) local satellites = player.force.get_item_launched('satellite') diff --git a/modules/ExpGamingInfo/Science/control.lua b/modules/ExpGamingInfo/Science/control.lua index 309b7b4a..dd7f4880 100644 --- a/modules/ExpGamingInfo/Science/control.lua +++ b/modules/ExpGamingInfo/Science/control.lua @@ -42,7 +42,7 @@ ThisModule.Gui = Gui.left{ name='science', caption='item/lab', tooltip={'ExpGamingInfo-Science.tooltip'}, - draw=function(frame) + draw=function(self,frame) local player = Game.get_player(frame.player_index) if not global[player.force.name] then verbose('Added Science Global for: '..player.force.name) diff --git a/modules/ExpGamingInfo/Tasklist/control.lua b/modules/ExpGamingInfo/Tasklist/control.lua index 401567a7..49be90ca 100644 --- a/modules/ExpGamingInfo/Tasklist/control.lua +++ b/modules/ExpGamingInfo/Tasklist/control.lua @@ -130,7 +130,7 @@ ThisModule.Gui = Gui.left{ name='tasklist', caption='utility/not_enough_repair_packs_icon', tooltip={'ExpGamingInfo-Tasklist.tooltip'}, - draw=function(frame) + draw=function(self,frame) frame.caption = '' local title = frame.add{ type='flow', diff --git a/modules/ExpGamingPlayer/playerInfo/control.lua b/modules/ExpGamingPlayer/playerInfo/control.lua index 7a4a56cc..b4a4a4d4 100644 --- a/modules/ExpGamingPlayer/playerInfo/control.lua +++ b/modules/ExpGamingPlayer/playerInfo/control.lua @@ -54,5 +54,5 @@ return setmetatable({ if loaded_modules['ExpGamingCore.Group'] then Group = require('ExpGamingCore.Group') end end },{ - __call=function(self,...) self.get_player_info(...) end + __call=function(self,...) return self.get_player_info(...) end }) \ No newline at end of file diff --git a/modules/ExpGamingPlayer/playerList/control.lua b/modules/ExpGamingPlayer/playerList/control.lua index 7d32f081..5af46bfb 100644 --- a/modules/ExpGamingPlayer/playerList/control.lua +++ b/modules/ExpGamingPlayer/playerList/control.lua @@ -63,7 +63,7 @@ ThisModule.Gui = Gui.left{ name='player-list', caption='entity/player', tooltip={'ExpGamingPlayer-playerList.tooltip'}, - draw=function(frame) + draw=function(self,frame) frame.caption = '' local player_list = frame.add{ name='scroll', @@ -125,6 +125,8 @@ script.on_event(defines.events.on_gui_click,function(event) -- must be a right click if event.button == defines.mouse_button_type.right then else return end local player_list = event.element.parent.parent.parent + -- must be a valid player which is clicked + if not Game.get_player(event.element.name) then return end -- hides the player list to show the info player_list.scroll.style.visible = false local flow = player_list.add{type='flow',direction='vertical'} diff --git a/modules/ExpGamingPlayer/polls/control.lua b/modules/ExpGamingPlayer/polls/control.lua index 172d68ff..3e6b8301 100644 --- a/modules/ExpGamingPlayer/polls/control.lua +++ b/modules/ExpGamingPlayer/polls/control.lua @@ -206,7 +206,7 @@ end) ThisModule.Gui = Gui.popup{ name='polls', caption={'ExpGamingPlayer-polls.name'}, - draw=function(frame,data) + draw=function(self,frame,data) frame.style.right_padding = 5 frame.style.bottom_padding = 5 local uuid = data.uuid @@ -225,7 +225,7 @@ ThisModule.Gui = Gui.popup{ }:add_left{ caption='utility/item_editor_icon', tooltip={'ExpGamingPlayer-polls.tooltip'}, - draw=function(frame) + draw=function(self,frame) frame.caption={'ExpGamingPlayer-polls.name'} frame.add{ type='label', diff --git a/modules/GuiAnnouncements/control.lua b/modules/GuiAnnouncements/control.lua index 8ee154d0..144eed14 100644 --- a/modules/GuiAnnouncements/control.lua +++ b/modules/GuiAnnouncements/control.lua @@ -57,7 +57,7 @@ end) ThisModule.Gui = Gui.popup{ name='announcements', caption={'GuiAnnouncements.name'}, - draw=function(frame,data) + draw=function(self,frame,data) frame.style.right_padding = 5 frame.style.bottom_padding = 5 frame.add{type='label',caption=data.sent_by,style='caption_label'} @@ -72,7 +72,7 @@ ThisModule.Gui = Gui.popup{ }:add_left{ caption='item/programmable-speaker', tooltip={'GuiAnnouncements.tooltip'}, - draw=function(frame) + draw=function(self,frame) frame.caption = {'GuiAnnouncements.name'} local frame = frame.add{ type='flow', diff --git a/modules/WarpPoints/control.lua b/modules/WarpPoints/control.lua index 8c6c028b..42b449bc 100644 --- a/modules/WarpPoints/control.lua +++ b/modules/WarpPoints/control.lua @@ -145,7 +145,7 @@ ThisModule.Gui = Gui.left{ name='warp-list', caption='item/'..warp_item, tooltip={'WarpPoints.tooltip'}, - draw=function(frame) + draw=function(self,frame) local player = Game.get_player(frame.player_index) frame.caption={'WarpPoints.name'} local warp_list = frame.add{