From c9b704b9a094e7efb5bca2b06ac92a2c2cd4983b Mon Sep 17 00:00:00 2001 From: Cooldude2606 Date: Mon, 23 Mar 2020 18:03:29 +0000 Subject: [PATCH] Working center gui --- config/_file_loader.lua | 1 + config/roles.lua | 3 +- expcore/gui/left_flow.lua | 41 +++---------------------- expcore/gui/prototype.lua | 4 +-- expcore/gui/top_flow.lua | 63 +++++++++++++++++++++++++++++++++++++++ modules/gui/readme.lua | 49 ++++++++++++++++++++++++++++++ 6 files changed, 121 insertions(+), 40 deletions(-) create mode 100644 modules/gui/readme.lua diff --git a/config/_file_loader.lua b/config/_file_loader.lua index 35a07946..0a5ed1df 100644 --- a/config/_file_loader.lua +++ b/config/_file_loader.lua @@ -43,6 +43,7 @@ return { 'modules.addons.chat-reply', 'modules.addons.tree-decon', -- GUI + 'modules.gui.readme', 'modules.gui.rocket-info', 'modules.gui.science-info', 'modules.gui.warp-list', diff --git a/config/roles.lua b/config/roles.lua index ff40131f..800d4cf1 100644 --- a/config/roles.lua +++ b/config/roles.lua @@ -220,7 +220,8 @@ local default = Roles.new_role('Guest','') 'gui/rocket-info', 'gui/science-info', 'gui/task-list', - 'gui/warp-list' + 'gui/warp-list', + 'gui/readme' } --- Jail role diff --git a/expcore/gui/left_flow.lua b/expcore/gui/left_flow.lua index f41d8419..03fba9ab 100644 --- a/expcore/gui/left_flow.lua +++ b/expcore/gui/left_flow.lua @@ -42,28 +42,6 @@ function Gui._prototype_element:add_to_left_flow(open_on_join) return self end ---[[-- Styles a top flow button depending on the state given -@tparam LuaGuiElement the button element to style -@tparam boolean state The state the button is in - -@usage-- Sets the button to the visible style -Gui.left_toolbar_button_style(button, true) - -@usage-- Sets the button to the hidden style -Gui.left_toolbar_button_style(button, false) - -]] -function Gui.left_toolbar_button_style(button, state) - if state then - button.style = Gui.top_flow_button_visible_style - else - button.style = Gui.top_flow_button_style - end - button.style.minimal_width = 36 - button.style.height = 36 - button.style.padding = -2 -end - --[[-- Creates a button on the top flow which will toggle the given element define, the define must exist in the left flow @tparam string sprite the sprite that you want to use on the button @tparam ?string|Concepts.LocalizedString tooltip the tooltip that you want the button to have @@ -78,18 +56,7 @@ end) ]] function Gui.left_toolbar_button(sprite,tooltip,element_define,authenticator) - local button = Gui.element{ - type = 'sprite-button', - sprite = sprite, - tooltip = tooltip, - style = Gui.top_flow_button_style - } - :style{ - minimal_width = 36, - height = 36, - padding = -2 - } - :add_to_top_flow(authenticator) + local button = Gui.toolbar_button(sprite,tooltip,authenticator) -- Add on_click handler to handle click events comming from the player button:on_click(function(player,_,_) @@ -152,7 +119,7 @@ function Gui.draw_left_flow(player) local button = top_flow[element_define.toolbar_button] if button then -- Style the button - Gui.left_toolbar_button_style(button, visible) + Gui.toolbar_button_style(button, visible) end end end @@ -206,7 +173,7 @@ function Gui.hide_left_flow(player) local button = top_flow[element_define.toolbar_button] if button then -- Style the button - Gui.left_toolbar_button_style(button, false) + Gui.toolbar_button_style(button, false) -- Get the button define from the reverse lookup on the element local button_define = Gui.defines[element_define.toolbar_button] -- Raise the custom event if all of the top checks have passed @@ -263,7 +230,7 @@ function Gui.toggle_left_element(player,element_define,state) local button = top_flow[element_define.toolbar_button] if button then -- Style the button - Gui.left_toolbar_button_style(button, state) + Gui.toolbar_button_style(button, state) end end return state diff --git a/expcore/gui/prototype.lua b/expcore/gui/prototype.lua index 21ee907c..526d27b0 100644 --- a/expcore/gui/prototype.lua +++ b/expcore/gui/prototype.lua @@ -365,11 +365,11 @@ end --- Called when the player opens a GUI. -- @tparam function handler the event handler which will be called -Gui._prototype_element.on_opened = event_handler_factory(defines.events.on_gui_opened) +Gui._prototype_element.on_open = event_handler_factory(defines.events.on_gui_opened) --- Called when the player closes the GUI they have open. -- @tparam function handler the event handler which will be called -Gui._prototype_element.on_closed = event_handler_factory(defines.events.on_gui_closed) +Gui._prototype_element.on_close = event_handler_factory(defines.events.on_gui_closed) --- Called when LuaGuiElement is clicked. -- @tparam function handler the event handler which will be called diff --git a/expcore/gui/top_flow.lua b/expcore/gui/top_flow.lua index d9291d0f..f06dea2e 100644 --- a/expcore/gui/top_flow.lua +++ b/expcore/gui/top_flow.lua @@ -101,4 +101,67 @@ function Gui.toggle_top_flow(player,state) top_flow.visible = state return state +end + +--[[-- Get the element define that is in the top flow, use in events without an element refrence +@tparam LuaPlayer player the player that you want to get the element for +@tparam table element_define the element that you want to get +@treturn LuaGuiElement the gui element linked to this define for this player + +@usage-- Get your top element +local button = Gui.get_top_element(game.player, example_button) + +]] +function Gui.get_top_element(player, element_define) + local top_flow = Gui.get_top_flow(player) + return top_flow[element_define.name] +end + +--[[-- Creates a button on the top flow with consistent styling +@tparam string sprite the sprite that you want to use on the button +@tparam ?string|Concepts.LocalizedString tooltip the tooltip that you want the button to have +@tparam[opt] function authenticator used to decide if the button should be visible to a player + +@usage-- Add a button to the toolbar +local toolbar_button = +Gui.left_toolbar_button('entity/inserter', 'Nothing to see here', function(player) + return player.admin +end) + +]] +function Gui.toolbar_button(sprite,tooltip,authenticator) + return Gui.element{ + type = 'sprite-button', + sprite = sprite, + tooltip = tooltip, + style = Gui.top_flow_button_style + } + :style{ + minimal_width = 36, + height = 36, + padding = -2 + } + :add_to_top_flow(authenticator) +end + +--[[-- Styles a top flow button depending on the state given +@tparam LuaGuiElement the button element to style +@tparam boolean state The state the button is in + +@usage-- Sets the button to the visible style +Gui.toolbar_button_style(button, true) + +@usage-- Sets the button to the hidden style +Gui.toolbar_button_style(button, false) + +]] +function Gui.toolbar_button_style(button, state) + if state then + button.style = Gui.top_flow_button_visible_style + else + button.style = Gui.top_flow_button_style + end + button.style.minimal_width = 36 + button.style.height = 36 + button.style.padding = -2 end \ No newline at end of file diff --git a/modules/gui/readme.lua b/modules/gui/readme.lua new file mode 100644 index 00000000..c66bee57 --- /dev/null +++ b/modules/gui/readme.lua @@ -0,0 +1,49 @@ +--[[-- Gui Module - Readme + - Adds a main gui that contains lots of important information about our server + @gui Readme + @alias readme +]] + +local Gui = require 'expcore.gui' --- @dep expcore.gui +local Roles = require 'expcore.roles' --- @dep expcore.roles +local Event = require 'utils.event' --- @dep utils.event +local Game = require 'utils.game' --- @dep utils.game + +--- Main readme container for the center flow +-- @element readme +local readme_toggle +local readme = +Gui.element(function(event_trigger,parent) + local container = Gui.container(parent,event_trigger,200) + return container.parent +end) +:on_open(function(player) + local toggle_button = Gui.get_top_element(player, readme_toggle) + Gui.toolbar_button_style(toggle_button, true) +end) +:on_close(function(player,element) + local toggle_button = Gui.get_top_element(player, readme_toggle) + Gui.toolbar_button_style(toggle_button, false) + Gui.destroy_if_valid(element) +end) + +--- Toggle button for the readme gui +-- @element readme_toggle +readme_toggle = +Gui.toolbar_button('virtual-signal/signal-info','Readme',function(player) + return Roles.player_allowed(player,'gui/readme') +end) +:on_click(function(player,element) + local center = player.gui.center + if center[readme.name] then + player.opened = nil + else + player.opened = readme(center) + end +end) + +--- When a player joins the game for the first time show this gui +Event.add(defines.events.on_player_created,function(event) + local player = Game.get_player_by_index(event.player_index) + player.opened = readme(player.gui.center) +end) \ No newline at end of file