Bugs Fixed

This commit is contained in:
Cooldude2606
2019-05-06 13:38:05 +01:00
parent 2119378fad
commit 6d1c907484
7 changed files with 69 additions and 35 deletions

View File

@@ -38,5 +38,5 @@ return {
'config.command_auth_runtime_disable', -- allows commands to be enabled and disabled during runtime 'config.command_auth_runtime_disable', -- allows commands to be enabled and disabled during runtime
'config.permission_groups', -- loads some predefined permission groups 'config.permission_groups', -- loads some predefined permission groups
'config.roles', -- loads some predefined roles 'config.roles', -- loads some predefined roles
'expcore.store_test' 'expcore.gui.test'
} }

View File

@@ -5,7 +5,7 @@ local Gui = require './core'
local Button = { local Button = {
config={}, config={},
clean_names={}, clean_names={},
_prototype = Gui._set_up_prototype{} _prototype = Gui._extend_prototype{}
} }
function Button.new_button(name) function Button.new_button(name)
@@ -31,15 +31,25 @@ function Button.draw_button(name,element)
button = Button.config[button] button = Button.config[button]
end end
end end
button:draw_to(element) return button:draw_to(element)
end end
function Button._prototype:draw_to(element) function Button._prototype:draw_to(element)
if element.children[self.name] then return end 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 if self.authenticator then
self_element.enabled = not not self.authenticator(element.player,self.clean_name or self.name) self_element.enabled = not not self.authenticator(element.player,self.clean_name or self.name)
end end
return self_element
end end
function Button._prototype:set_sprites(sprite,hovered_sprite,clicked_sprite) 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 if type(callback) ~= 'function' then
return error('Event callback must be a function') return error('Event callback must be a function')
end end
self.on_click = callback self._on_click = callback
self:_add_handler() self:_add_handler()
return self return self
end end
@@ -99,7 +109,7 @@ function Button._prototype:on_left_click(callback)
if type(callback) ~= 'function' then if type(callback) ~= 'function' then
return error('Event callback must be a function') return error('Event callback must be a function')
end end
self.on_left_click = callback self._on_left_click = callback
self:_add_handler() self:_add_handler()
return self return self
end end
@@ -108,7 +118,7 @@ function Button._prototype:on_right_click(callback)
if type(callback) ~= 'function' then if type(callback) ~= 'function' then
return error('Event callback must be a function') return error('Event callback must be a function')
end end
self.on_right_click = callback self._on_right_click = callback
self:_add_handler() self:_add_handler()
return self return self
end 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 if not self.authenticator(event.player,self.clean_name or self.name) then return end
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) 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) self.on_right_click(event.player,event.element,event)
end end
@@ -138,7 +148,7 @@ function Button._prototype:_add_handler()
end end
end end
self.on_click(event.player,event.element,event) self._on_click(event.player,event.element,event)
end) end)
end end

View File

@@ -9,16 +9,19 @@ function Gui._extend_prototype(tbl)
for k,v in pairs(Gui._prototype) do for k,v in pairs(Gui._prototype) do
if not tbl[k] then tbl[k] = v end if not tbl[k] then tbl[k] = v end
end end
return tbl
end end
--- Sets the caption for the element config --- Sets the caption for the element config
function Gui._prototype:set_caption(caption) function Gui._prototype:set_caption(caption)
self.caption = caption self.caption = caption
return self
end end
--- Sets the tooltip for the element config --- Sets the tooltip for the element config
function Gui._prototype:set_tooltip(tooltip) function Gui._prototype:set_tooltip(tooltip)
self.tooltip = tooltip self.tooltip = tooltip
return self
end end
function Gui.toggle_enable(element) function Gui.toggle_enable(element)

View File

@@ -1,18 +1,27 @@
local Gui = require 'expcore.gui' local Gui = require 'expcore.gui'
Gui.new_toolbar_button('click-1') Gui.new_toolbar_button('click-1')
:set_authenticator(function(player,button_name)
return global.click_one
end)
:on_click(function(player,element,event) :on_click(function(player,element,event)
player.print('CLICK 1') player.print('CLICK 1')
end) end)
Gui.new_toolbar_button('click-2') Gui.new_toolbar_button('click-2')
:set_caption('Click Two') :set_caption('Click Two')
:set_authenticator(function(player,button_name)
return global.click_two
end)
:on_click(function(player,element,event) :on_click(function(player,element,event)
player.print('CLICK 2') player.print('CLICK 2')
end) end)
Gui.new_toolbar_button('click-3') Gui.new_toolbar_button('click-3')
:set_sprites('utility/questionmark') :set_sprites('utility/questionmark')
:set_authenticator(function(player,button_name)
return global.click_three
end)
:on_click(function(player,element,event) :on_click(function(player,element,event)
player.print('CLICK 3') player.print('CLICK 3')
end) end)

View File

@@ -1,6 +1,6 @@
local Buttons = require './buttons' local Buttons = require './buttons'
local Gui = require './core' local Gui = require './core'
local Roles = require 'expre.roles' local Roles = require 'expcore.roles'
local Event = require 'utils.event' local Event = require 'utils.event'
local Game = require 'utils.game' local Game = require 'utils.game'
@@ -12,6 +12,7 @@ function Toolbar.new_button(name)
name = name or #Toolbar.buttons+1 name = name or #Toolbar.buttons+1
local button = Buttons.new_button('toolbar/'..name) local button = Buttons.new_button('toolbar/'..name)
button:set_authenticator(Roles.player_allowed) button:set_authenticator(Roles.player_allowed)
Toolbar.add_button(button)
return button return button
end end
@@ -28,30 +29,36 @@ function Toolbar.add_button(button)
end end
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 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 if button.authenticator(player,button.clean_name or button.name) then
self_button.visible = true element.visible = true
element.enabled = true
else else
self_button.visible = false element.visible = false
element.enabled = false
end end
end end
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)
Toolbar.draw(player) Toolbar.update(player)
end) end)
Event.add(Roles.player_role_assigned,function(event) Event.add(Roles.player_role_assigned,function(event)
local player = Game.get_player_by_index(event.player_index) local player = Game.get_player_by_index(event.player_index)
Toolbar.draw(player) Toolbar.update(player)
end) end)
Event.add(Roles.player_role_unassigned,function(event) Event.add(Roles.player_role_unassigned,function(event)
local player = Game.get_player_by_index(event.player_index) local player = Game.get_player_by_index(event.player_index)
Toolbar.draw(player) Toolbar.update(player)
end) end)
return Toolbar return Toolbar

View File

@@ -10,7 +10,8 @@ local interface_modules = {
['output']=Common.player_return, ['output']=Common.player_return,
['Group']='expcore.permission_groups', ['Group']='expcore.permission_groups',
['Roles']='expcore.roles', ['Roles']='expcore.roles',
['Store']='expcore.store' ['Store']='expcore.store',
['Gui']='expcore.gui'
} }
-- loads all the modules given in the above table -- loads all the modules given in the above table

View File

@@ -2,6 +2,7 @@ local Token = require 'utils.token'
local Event = require 'utils.event' local Event = require 'utils.event'
local Game = require 'utils.game' local Game = require 'utils.game'
local Global = require 'utils.global' local Global = require 'utils.global'
local mod_gui = require 'mod-gui'
local 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) Gui.on_pre_player_hide_top = custom_handler_factory(on_pre_hidden_handlers)
--- Allows the player to show / hide this element. --- 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. -- This function must be called in the control stage, i.e not inside an event.
-- @param element_name<string> This name must be globally unique. -- @param element_name<string> This name must be globally unique.
function Gui.allow_player_to_toggle_top_element_visibility(element_name) 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 top_elements[#top_elements + 1] = element_name
end 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() local toggle_button_name = Gui.uid_name()
Event.add( Event.add(
@@ -201,15 +211,16 @@ Event.add(
end end
local b = local b =
player.gui.top.add { Gui.get_top_element_flow(player).add {
type = 'button', type = 'button',
name = toggle_button_name, name = toggle_button_name,
caption = '<', caption = '<',
style=mod_gui.button_style,
tooltip = 'Shows / hides the Redmew Gui buttons.' tooltip = 'Shows / hides the Redmew Gui buttons.'
} }
local style = b.style local style = b.style
style.width = 18 style.width = 18
style.height = 38 style.height = 36
style.left_padding = 0 style.left_padding = 0
style.top_padding = 0 style.top_padding = 0
style.right_padding = 0 style.right_padding = 0
@@ -223,21 +234,16 @@ Gui.on_click(
function(event) function(event)
local button = event.element local button = event.element
local player = event.player local player = event.player
local top = player.gui.top local top = Gui.get_top_element_flow(player)
if button.caption == '<' then if button.caption == '<' then
for i = 1, #top_elements do for i = 1, #top_elements do
local name = top_elements[i] local name = top_elements[i]
local ele = top[name] local ele = top[name]
if ele and ele.valid then if ele and ele.valid then
local style = ele.style if ele.visible then
-- 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
custom_raise(on_pre_hidden_handlers, ele, player) custom_raise(on_pre_hidden_handlers, ele, player)
style.visible = false ele.visible = false
end end
end end
end end
@@ -249,17 +255,15 @@ Gui.on_click(
local name = top_elements[i] local name = top_elements[i]
local ele = top[name] local ele = top[name]
if ele and ele.valid then if ele and ele.valid then
local style = ele.style if not ele.visible then
ele.visible = true
if not style.visible then
style.visible = true
custom_raise(on_visible_handlers, ele, player) custom_raise(on_visible_handlers, ele, player)
end end
end end
end end
button.caption = '<' button.caption = '<'
button.style.height = 38 button.style.height = 36
end end
end end
) )