From 6d1c907484982c6d26dd99a8e34bb771ea4886dd Mon Sep 17 00:00:00 2001 From: Cooldude2606 Date: Mon, 6 May 2019 13:38:05 +0100 Subject: [PATCH] Bugs Fixed --- config/file_loader.lua | 2 +- expcore/Gui/buttons.lua | 28 +++++++++++++++++--------- expcore/Gui/core.lua | 3 +++ expcore/Gui/test.lua | 9 +++++++++ expcore/Gui/toolbar.lua | 23 ++++++++++++++-------- modules/commands/interface.lua | 3 ++- utils/gui.lua | 36 +++++++++++++++++++--------------- 7 files changed, 69 insertions(+), 35 deletions(-) diff --git a/config/file_loader.lua b/config/file_loader.lua index 72016f49..7f1d6b18 100644 --- a/config/file_loader.lua +++ b/config/file_loader.lua @@ -38,5 +38,5 @@ return { 'config.command_auth_runtime_disable', -- allows commands to be enabled and disabled during runtime 'config.permission_groups', -- loads some predefined permission groups 'config.roles', -- loads some predefined roles - 'expcore.store_test' + 'expcore.gui.test' } \ No newline at end of file diff --git a/expcore/Gui/buttons.lua b/expcore/Gui/buttons.lua index 4219b2eb..142eba18 100644 --- a/expcore/Gui/buttons.lua +++ b/expcore/Gui/buttons.lua @@ -5,7 +5,7 @@ local Gui = require './core' local Button = { config={}, clean_names={}, - _prototype = Gui._set_up_prototype{} + _prototype = Gui._extend_prototype{} } function Button.new_button(name) @@ -31,15 +31,25 @@ function Button.draw_button(name,element) button = Button.config[button] end end - button:draw_to(element) + return button:draw_to(element) end function Button._prototype:draw_to(element) if element.children[self.name] then return end - local self_element = element.add(self) + local draw = table.deep_copy(self) + draw.authenticator = nil + draw.clean_name = nil + draw.raw_mouse_button_filter = nil + draw.key_button_filter = nil + draw._on_click = nil + draw._on_left_click = nil + draw._on_right_click = nil + draw.has_handler = nil + local self_element = element.add(draw) if self.authenticator then self_element.enabled = not not self.authenticator(element.player,self.clean_name or self.name) end + return self_element end function Button._prototype:set_sprites(sprite,hovered_sprite,clicked_sprite) @@ -90,7 +100,7 @@ function Button._prototype:on_click(callback) if type(callback) ~= 'function' then return error('Event callback must be a function') end - self.on_click = callback + self._on_click = callback self:_add_handler() return self end @@ -99,7 +109,7 @@ function Button._prototype:on_left_click(callback) if type(callback) ~= 'function' then return error('Event callback must be a function') end - self.on_left_click = callback + self._on_left_click = callback self:_add_handler() return self end @@ -108,7 +118,7 @@ function Button._prototype:on_right_click(callback) if type(callback) ~= 'function' then return error('Event callback must be a function') end - self.on_right_click = callback + self._on_right_click = callback self:_add_handler() return self end @@ -125,9 +135,9 @@ function Button._prototype:_add_handler() if not self.authenticator(event.player,self.clean_name or self.name) then return end end - if mosue_button == defines.mouse_button_type.left and self.on_left_click then + if mosue_button == defines.mouse_button_type.left and self._on_left_click then self.on_left_click(event.player,event.element,event) - elseif mosue_button == defines.mouse_button_type.right and self.on_right_click then + elseif mosue_button == defines.mouse_button_type.right and self._on_right_click then self.on_right_click(event.player,event.element,event) end @@ -138,7 +148,7 @@ function Button._prototype:_add_handler() end end - self.on_click(event.player,event.element,event) + self._on_click(event.player,event.element,event) end) end diff --git a/expcore/Gui/core.lua b/expcore/Gui/core.lua index e2c19c6b..2ff6bca3 100644 --- a/expcore/Gui/core.lua +++ b/expcore/Gui/core.lua @@ -9,16 +9,19 @@ function Gui._extend_prototype(tbl) for k,v in pairs(Gui._prototype) do if not tbl[k] then tbl[k] = v end end + return tbl end --- Sets the caption for the element config function Gui._prototype:set_caption(caption) self.caption = caption + return self end --- Sets the tooltip for the element config function Gui._prototype:set_tooltip(tooltip) self.tooltip = tooltip + return self end function Gui.toggle_enable(element) diff --git a/expcore/Gui/test.lua b/expcore/Gui/test.lua index 9c4f05bb..b4286087 100644 --- a/expcore/Gui/test.lua +++ b/expcore/Gui/test.lua @@ -1,18 +1,27 @@ local Gui = require 'expcore.gui' Gui.new_toolbar_button('click-1') +:set_authenticator(function(player,button_name) + return global.click_one +end) :on_click(function(player,element,event) player.print('CLICK 1') end) Gui.new_toolbar_button('click-2') :set_caption('Click Two') +:set_authenticator(function(player,button_name) + return global.click_two +end) :on_click(function(player,element,event) player.print('CLICK 2') end) Gui.new_toolbar_button('click-3') :set_sprites('utility/questionmark') +:set_authenticator(function(player,button_name) + return global.click_three +end) :on_click(function(player,element,event) player.print('CLICK 3') end) \ No newline at end of file diff --git a/expcore/Gui/toolbar.lua b/expcore/Gui/toolbar.lua index ae219924..3c9e9151 100644 --- a/expcore/Gui/toolbar.lua +++ b/expcore/Gui/toolbar.lua @@ -1,6 +1,6 @@ local Buttons = require './buttons' local Gui = require './core' -local Roles = require 'expre.roles' +local Roles = require 'expcore.roles' local Event = require 'utils.event' local Game = require 'utils.game' @@ -12,6 +12,7 @@ function Toolbar.new_button(name) name = name or #Toolbar.buttons+1 local button = Buttons.new_button('toolbar/'..name) button:set_authenticator(Roles.player_allowed) + Toolbar.add_button(button) return button end @@ -28,30 +29,36 @@ function Toolbar.add_button(button) end end -function Toolbar.draw(player) +function Toolbar.update(player) + local top = Gui.get_top_element_flow(player) + if not top then return end for _,button in pairs(Toolbar.buttons) do - local self_button = button:draw_to(player.gui.top) + local element + if top[button.name] then element = top[button.name] + else element = button:draw_to(top) end if button.authenticator(player,button.clean_name or button.name) then - self_button.visible = true + element.visible = true + element.enabled = true else - self_button.visible = false + element.visible = false + element.enabled = false end end end Event.add(defines.events.on_player_created,function(event) local player = Game.get_player_by_index(event.player_index) - Toolbar.draw(player) + Toolbar.update(player) end) Event.add(Roles.player_role_assigned,function(event) local player = Game.get_player_by_index(event.player_index) - Toolbar.draw(player) + Toolbar.update(player) end) Event.add(Roles.player_role_unassigned,function(event) local player = Game.get_player_by_index(event.player_index) - Toolbar.draw(player) + Toolbar.update(player) end) return Toolbar \ No newline at end of file diff --git a/modules/commands/interface.lua b/modules/commands/interface.lua index 98452174..59138617 100644 --- a/modules/commands/interface.lua +++ b/modules/commands/interface.lua @@ -10,7 +10,8 @@ local interface_modules = { ['output']=Common.player_return, ['Group']='expcore.permission_groups', ['Roles']='expcore.roles', - ['Store']='expcore.store' + ['Store']='expcore.store', + ['Gui']='expcore.gui' } -- loads all the modules given in the above table diff --git a/utils/gui.lua b/utils/gui.lua index 7ff901aa..57f68330 100644 --- a/utils/gui.lua +++ b/utils/gui.lua @@ -2,6 +2,7 @@ local Token = require 'utils.token' local Event = require 'utils.event' local Game = require 'utils.game' local Global = require 'utils.global' +local mod_gui = require 'mod-gui' local Gui = {} @@ -179,7 +180,7 @@ Gui.on_player_show_top = custom_handler_factory(on_visible_handlers) Gui.on_pre_player_hide_top = custom_handler_factory(on_pre_hidden_handlers) --- Allows the player to show / hide this element. --- The element must be part in gui.top. +-- The element must be in Gui.get_top_element_flow(player). -- This function must be called in the control stage, i.e not inside an event. -- @param element_name This name must be globally unique. function Gui.allow_player_to_toggle_top_element_visibility(element_name) @@ -189,6 +190,15 @@ function Gui.allow_player_to_toggle_top_element_visibility(element_name) top_elements[#top_elements + 1] = element_name end +--- Returns the flow where top elements can be added and will be effected by google visibility +-- For the toggle to work it must be registed with Gui.allow_player_to_toggle_top_element_visibility(element_name) +-- @tparam player LuaPlayer pointer to the player who has the gui +-- @treturn LuaGuiEelement the top element flow +function Gui.get_top_element_flow(player) + player = Game.get_player_from_any(player) + return mod_gui.get_button_flow(player) +end + local toggle_button_name = Gui.uid_name() Event.add( @@ -201,15 +211,16 @@ Event.add( end local b = - player.gui.top.add { + Gui.get_top_element_flow(player).add { type = 'button', name = toggle_button_name, caption = '<', + style=mod_gui.button_style, tooltip = 'Shows / hides the Redmew Gui buttons.' } local style = b.style style.width = 18 - style.height = 38 + style.height = 36 style.left_padding = 0 style.top_padding = 0 style.right_padding = 0 @@ -223,21 +234,16 @@ Gui.on_click( function(event) local button = event.element local player = event.player - local top = player.gui.top + local top = Gui.get_top_element_flow(player) if button.caption == '<' then for i = 1, #top_elements do local name = top_elements[i] local ele = top[name] if ele and ele.valid then - local style = ele.style - - -- if visible is not set it has the value of nil. - -- Hence nil is treated as is visible. - local v = style.visible - if v or v == nil then + if ele.visible then custom_raise(on_pre_hidden_handlers, ele, player) - style.visible = false + ele.visible = false end end end @@ -249,17 +255,15 @@ Gui.on_click( local name = top_elements[i] local ele = top[name] if ele and ele.valid then - local style = ele.style - - if not style.visible then - style.visible = true + if not ele.visible then + ele.visible = true custom_raise(on_visible_handlers, ele, player) end end end button.caption = '<' - button.style.height = 38 + button.style.height = 36 end end )