diff --git a/expcore/Gui/center.lua b/expcore/Gui/center.lua new file mode 100644 index 00000000..9e836409 --- /dev/null +++ b/expcore/Gui/center.lua @@ -0,0 +1,190 @@ +--- Gui structure define for center gui frames +--[[ +>>>> Functions + CenterFrames.get_flow(player) --- Gets the center flow for a player + CenterFrames.clear_flow(player) --- Clears the center flow for a player + CenterFrames.draw_frame(player,name) --- Draws the center frame for a player, if already open then will do nothing + CenterFrames.redraw_frame(player,name) --- Draws the center frame for a player, if already open then will destroy it and redraw + CenterFrames.toggle_frame(player,name,state) --- Toggles if the frame is currently open or not, will open if closed and close if open + + CenterFrames.new_frame(permision_name) --- Sets the frame to be the current active gui when opened and closes all other frames + CenterFrames._prototype:set_auto_focus(state) --- Sets the frame to be the current active gui when opened and closes all other frames + CenterFrames._prototype:draw_frame(player) --- Draws this frame to the player, if already open does nothing (will call on_draw to draw to the frame) + CenterFrames._prototype:redraw_frame(player) --- Draws this frame to the player, if already open it will remove it and redraw it (will call on_draw to draw to the frame) + CenterFrames._prototype:toggle_frame(player) --- Toggles if the frame is open, if open it will close it and if closed it will open it + CenterFrames._prototype:event_handler(action) --- Creates an event handler that will trigger one of its functions, use with Event.add +]] +local Gui = require 'expcore.gui.core' +local Toolbar = require 'expcore.gui.toolbar' +local Game = require 'utils.game' + +local CenterFrames = { + _prototype = Gui._prototype_factory{ + on_draw = Gui._event_factory('on_draw') + } +} + +--- Gets the center flow for a player +-- @tparam player LuapPlayer the player to get the flow for +-- @treturn LuaGuiElement the center flow +function CenterFrames.get_flow(player) + player = Game.get_player_from_any(player) + return player.gui.center +end + +--- Clears the center flow for a player +-- @tparam player LuapPlayer the player to clear the flow for +function CenterFrames.clear_flow(player) + local flow = CenterFrames.get_flow(player) + flow.clear() +end + +--- Draws the center frame for a player, if already open then will do nothing +-- @tparam player LuapPlayer the player that will have the frame drawn +-- @tparam name string the name of the hui that will drawn +-- @treturn LuaGuiElement the new frame that was made +function CenterFrames.draw_frame(player,name) + local define = Gui.get_define(name,true) + if define then + return define:draw_frame(player) + end +end + +--- Draws the center frame for a player, if already open then will destroy it and redraw +-- @tparam player LuapPlayer the player that will have the frame drawn +-- @tparam name string the name of the hui that will drawn +-- @treturn LuaGuiElement the new frame that was made +function CenterFrames.redraw_frame(player,name) + local define = Gui.get_define(name,true) + if define then + return define:draw_frame(player) + end +end + +--- Toggles if the frame is currently open or not, will open if closed and close if open +-- @tparam player LuapPlayer the player that will have the frame toggled +-- @tparam name string the name of the hui that will be toggled +-- @tparam[opt] state boolean when set will force a state for the frame +-- @treturn boolean if the frame if no open or closed +function CenterFrames.toggle_frame(player,name,state) + local define = Gui.get_define(name,true) + if define then + if state == true then + define:draw_frame(player) + return true + elseif state == false then + local flow = CenterFrames.get_flow(player) + if flow[define.name] then + flow[define.name].destroy() + end + return false + else + return define:toggle_frame(player) + end + end +end + +--- Creates a new center frame define +-- @tparam permision_name string the name that can be used with the permision system +-- @treturn table the new center frame define +function CenterFrames.new_frame(permision_name) + local self = Toolbar.new_button(permision_name) + + self:on_click(function(player,element) + self:toggle_frame(player) + end) + + local mt = getmetatable(self) + mt.__index = CenterFrames._prototype + mt.__call = self.event_handler + + Gui.on_custom_close(self.name,function(event) + local element = event.element + if element and element.valid then element.destroy() end + end) + + return self +end + +--- Sets the frame to be the current active gui when opened and closes all other frames +-- @tparam[opt=true] state boolean when true will auto close other frames and set this frame as player.opened +function CenterFrames._prototype:set_auto_focus(state) + if state == false then + self.auto_focus = false + else + self.auto_focus = true + end +end + +--- Draws this frame to the player, if already open does nothing (will call on_draw to draw to the frame) +-- @tparam player LuaPlayer the player to draw the frame for +-- @treturn LuaGuiElement the new frame that was drawn +function CenterFrames._prototype:draw_frame(player) + player = Game.get_player_from_any(player) + local flow = CenterFrames.get_flow(player) + + if flow[self.name] then + return flow[self.name] + end + + if self.auto_focus then + flow.clear() + end + + local frame = flow.add{ + type='frame', + name=self.name + } + + if self.auto_focus then + player.opened = frame + end + + if self.events.on_draw then + self.events.on_draw(player,frame) + end + + return frame +end + +--- Draws this frame to the player, if already open it will remove it and redraw it (will call on_draw to draw to the frame) +-- @tparam player LuaPlayer the player to draw the frame for +-- @treturn LuaGuiElement the new frame that was drawn +function CenterFrames._prototype:redraw_frame(player) + player = Game.get_player_from_any(player) + local flow = CenterFrames.get_flow(player) + + if flow[self.name] then + flow[self.name].destroy() + end + + return self:draw_frame(player) +end + +--- Toggles if the frame is open, if open it will close it and if closed it will open it +-- @tparam player LuaPlayer the player to draw the frame for +-- @treturn boolean with the gui frame is now open +function CenterFrames._prototype:toggle_frame(player) + player = Game.get_player_from_any(player) + local flow = CenterFrames.get_flow(player) + + if flow[self.name] then + flow[self.name].destroy() + return false + else + self:draw_frame(player) + return true + end +end + +--- 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 CenterFrames._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 + +return CenterFrames \ No newline at end of file diff --git a/expcore/Gui/core.lua b/expcore/Gui/core.lua index 011bf283..16c9fe5a 100644 --- a/expcore/Gui/core.lua +++ b/expcore/Gui/core.lua @@ -415,7 +415,7 @@ function Gui.get_define(name,internal) return Gui.defines[Gui.names[name]] elseif not define then - return error('Invalid name for checkbox, name not found.',internal and 3 or 2) or nil + return error('Invalid name for element define, name not found.',internal and 3 or 2) or nil end diff --git a/expcore/Gui/test.lua b/expcore/Gui/test.lua index 3a1a013c..31b289f2 100644 --- a/expcore/Gui/test.lua +++ b/expcore/Gui/test.lua @@ -13,7 +13,6 @@ local tests = {} > No display - Toolbar button with no display > With caption - Toolbar button with a caption display > With icons - Toolbar button with an icon - > Main test gui - Main test gui triggers all other tests ]] Gui.new_toolbar_button('click-1') @@ -42,20 +41,19 @@ end) player.print('CLICK 3') end) -Gui.new_toolbar_button('gui-test-open') +--[[ + Center Frame Tests + > Main test gui - Main test gui triggers all other tests +]] + +local test_gui = +Gui.new_center_frame('gui-test-open') :set_caption('Open Test Gui') :set_post_authenticator(function(player,button_name) return global.show_test_gui end) -:on_click(function(player,_element) - if player.gui.center.TestGui then player.gui.center.TestGui.destroy() return end - - local frame = player.gui.center.add{ - type='frame', - caption='Gui Test', - name='TestGui' - } +:on_draw(function(player,frame) for test_group_name,test_group in pairs(tests) do player.print('Starting tests for: '..format_chat_colour(test_group_name,Colors.cyan)) diff --git a/expcore/gui.lua b/expcore/gui.lua index 321c1def..6568e744 100644 --- a/expcore/gui.lua +++ b/expcore/gui.lua @@ -1,7 +1,7 @@ --- This file is used to require all the different elements of the gui module -- each module has an outline here but for more details see their seperate files in ./gui -local Gui = require('expcore.gui.core') +local Gui = require 'expcore.gui.core' --[[ Gui._prototype_factory(tbl) --- Used internally to create new prototypes for element defines Gui._event_factory(name) --- Used internally to create event handler adders for element defines @@ -36,14 +36,14 @@ local Gui = require('expcore.gui.core') Gui.toggle_visible(element) --- Will toggle the visiblity of an element ]] -local Instances = require('expcore.gui.instances') +local Instances = require 'expcore.gui.instances' Gui.new_instance_group = Instances.registers Gui.get_instances = Instances.get_elements Gui.add_instance = Instances.get_elements Gui.update_instances = Instances.apply_to_elements Gui.classes.instances = Instances -local Button = require('expcore.gui.buttons') +local Button = require 'expcore.gui.buttons' Gui.new_button = Button.new_button Gui.classes.button = Button --[[ @@ -58,7 +58,7 @@ Gui.classes.button = Button Button._prototype:set_key_filter(filter,...) --- Adds a control key filter to the button ]] -local Checkbox = require('expcore.gui.checkboxs') +local Checkbox = require 'expcore.gui.checkboxs' Gui.new_checkbox = Checkbox.new_checkbox Gui.new_radiobutton = Checkbox.new_radiobutton Gui.new_radiobutton_option_set = Checkbox.new_option_set @@ -80,7 +80,7 @@ Gui.classes.checkbox = Checkbox Checkbox.reset_radiobutton(element,exclude,recursive) --- Sets all radiobutotn in a element to false (unless excluded) and can act recursivly ]] -local Dropdown = require('expcore.gui.dropdown') +local Dropdown = require 'expcore.gui.dropdown' Gui.new_dropdown = Dropdown.new_dropdown Gui.new_list_box = Dropdown.new_list_box Gui.classes.dropdown = Dropdown @@ -99,7 +99,7 @@ Gui.classes.dropdown = Dropdown Dropdown.get_selected_value(element) --- Returns the currently selected value rather than index ]] -local Slider = require('expcore.gui.slider') +local Slider = require 'expcore.gui.slider' Gui.new_slider = Slider.new_slider Gui.classes.slider = Slider --[[ @@ -113,7 +113,7 @@ Gui.classes.slider = Slider Slider._prototype:enable_auto_draw_label(state) --- Enables auto draw of the label, the label will share the same parent element as the slider ]] -local Text = require('expcore.gui.text') +local Text = require 'expcore.gui.text' Gui.new_text_filed = Text.new_text_field Gui.new_text_box = Text.new_text_box Gui.classes.text = Text @@ -130,7 +130,7 @@ Gui.classes.text = Text Text._prototype_box:set_read_only(state) --- Sets the text box to be read only ]] -local ElemButton = require('expcore.gui.elem-button') +local ElemButton = require 'expcore.gui.elem-button' Gui.new_elem_button = ElemButton.new_elem_button Gui.classes.elem_button = ElemButton --[[ @@ -143,7 +143,7 @@ Gui.classes.elem_button = ElemButton ElemButton._prototype:set_default(value) --- Sets the default value for the elem button, this may be a function or a string ]] -local Toolbar = require('expcore.gui.toolbar') +local Toolbar = require 'expcore.gui.toolbar' Gui.new_toolbar_button = Toolbar.new_button Gui.add_button_to_toolbar = Toolbar.add_button Gui.update_toolbar = Toolbar.update @@ -154,7 +154,7 @@ Gui.classes.toolbar = Toolbar Toolbar.update(player) --- Updates the player's toolbar with an new buttons or expected change in auth return ]] -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 @@ -179,4 +179,26 @@ Gui.classes.left_frames = LeftFrames LeftFrames._prototype:event_handler(action) --- Creates an event handler that will trigger one of its functions, use with Event.add ]] +local CenterFrames = require 'expcore.gui.center' +Gui.get_center_flow = CenterFrames.get_flow +Gui.toggle_left_frame = CenterFrames.toggle_frame +Gui.draw_center_frame = CenterFrames.draw_frame +Gui.redraw_center_frame = CenterFrames.redraw_frames +Gui.new_center_frame = CenterFrames.new_frame +Gui.classes.center_frames = CenterFrames +--[[ + CenterFrames.get_flow(player) --- Gets the center flow for a player + CenterFrames.clear_flow(player) --- Clears the center flow for a player + CenterFrames.draw_frame(player,name) --- Draws the center frame for a player, if already open then will do nothing + CenterFrames.redraw_frame(player,name) --- Draws the center frame for a player, if already open then will destory it and redraw + CenterFrames.toggle_frame(player,name,state) --- Toggles if the frame is currently open or not, will open if closed and close if open + + CenterFrames.new_frame(permision_name) --- Sets the frame to be the current active gui when opened and closes all other frames + CenterFrames._prototype:set_auto_focus(state) --- Sets the frame to be the current active gui when opened and closes all other frames + CenterFrames._prototype:draw_frame(player) --- Draws this frame to the player, if already open does nothing (will call on_draw to draw to the frame) + CenterFrames._prototype:redraw_frame(player) --- Draws this frame to the player, if already open it will remove it and redraw it (will call on_draw to draw to the frame) + CenterFrames._prototype:toggle_frame(player) --- Toggles if the frame is open, if open it will close it and if closed it will open it + CenterFrames._prototype:event_handler(action) --- Creates an event handler that will trigger one of its functions, use with Event.add +]] + return Gui \ No newline at end of file