From 5a20e13f1a18d9c9be6c26a1fcb2bac5df53327b Mon Sep 17 00:00:00 2001 From: Cooldude2606 Date: Mon, 11 Dec 2017 21:46:09 +0000 Subject: [PATCH] Added toolbar --- ExpLib.lua | 6 ++-- control.lua | 19 ++++++++++++ locale/ExpCore/GuiParts/inputs.lua | 19 +++++++++--- locale/ExpCore/GuiParts/toolbar.lua | 48 +++++++++++++++++++++++++++++ locale/ExpCore/load.lua | 2 +- locale/ExpCore/ranking.lua | 8 ++--- 6 files changed, 91 insertions(+), 11 deletions(-) create mode 100644 locale/ExpCore/GuiParts/toolbar.lua diff --git a/ExpLib.lua b/ExpLib.lua index 67a63e50..591908fd 100644 --- a/ExpLib.lua +++ b/ExpLib.lua @@ -43,7 +43,8 @@ function ExpLib.player_return(rtn,player) local player = Game.get_player(player) if is_type(rtn,'table') then -- test if its a localised string - if is_type(rtn[1],'string') and string.find(rtn[1],'.+[.].+') and not string.find(rtn[1],'%s') then pcall(player.print,rtn) + if is_type(rtn.__self,'userdata') then player.print('Cant Display Userdata') + elseif is_type(rtn[1],'string') and string.find(rtn[1],'.+[.].+') and not string.find(rtn[1],'%s') then pcall(player.print,rtn) else player.print(table.to_string(rtn)) end elseif is_type(rtn,'function') then player.print('Cant Display Functions') @@ -52,7 +53,8 @@ function ExpLib.player_return(rtn,player) elseif game.player then if is_type(rtn,'table') then -- test if its a localised string - if is_type(rtn[1],'string') and string.find(rtn[1],'.+[.].+') and not string.find(rtn[1],'%s') then pcall(game.player.print,rtn) + if is_type(rtn.__self,'userdata') then player.print('Cant Display Userdata') + elseif is_type(rtn[1],'string') and string.find(rtn[1],'.+[.].+') and not string.find(rtn[1],'%s') then pcall(game.player.print,rtn) else game.player.print(table.to_string(rtn)) end elseif is_type(rtn,'function') then game.player.print('Cant Display Functions') diff --git a/control.lua b/control.lua index 22097248..2ffce6e8 100644 --- a/control.lua +++ b/control.lua @@ -23,6 +23,7 @@ error = function(err) end end +require("mod-gui") -- loads the stdlib and allows Core Game and Event Color, Game, Event = require('/locale/StdLib/load'){'Color','Game','Event'} @@ -40,3 +41,21 @@ pcall(require,'/locale/Addons/playerRanks') -- this makes sure that all the little details are cleaned up Ranking._auto_edit_ranks() +-- button testing - there are global for a reason +text = Gui.inputs.add{ + name='text-button', + type='button', + caption='Test' +} +text:on_event(Gui.inputs.events.click,function(event) game.print('test') end) + +sprite = Gui.inputs.add{ + name='sprite-button', + type='button', + caption='item/lab' +} +sprite:on_event(Gui.inputs.events.click,function(event) game.print('test') end) + +Gui.toolbar.add('button1','btn1','test btn1',function(event) game.print('test') end) +Gui.toolbar.add('button2','btn2','test btn2',function(event) game.print('test') end) +Gui.toolbar.add('button3','item/lab','test btn3',function(event) game.print('test') end) \ No newline at end of file diff --git a/locale/ExpCore/GuiParts/inputs.lua b/locale/ExpCore/GuiParts/inputs.lua index 7ed360e3..faeb4b52 100644 --- a/locale/ExpCore/GuiParts/inputs.lua +++ b/locale/ExpCore/GuiParts/inputs.lua @@ -35,12 +35,18 @@ end -- @param root the element you want to add the input to -- @return returns the element that was added function inputs._input:draw(root) - return root.add(self.draw_data) + if is_type(self.draw_data.caption,'string') and game.player.gui.is_valid_sprite_path(self.draw_data.caption) then + return root.add{type='sprite-button',name=self.draw_data.name,sprite=self.draw_data.caption,tooltip=self.draw_data.tooltip,style=mod_gui.button_style} + elseif is_type(self.draw_data.sprite,'string') and game.player.gui.is_valid_sprite_path(self.draw_data.sprite) then + return root.add{type='sprite-button',name=self.draw_data.name,sprite=self.draw_data.sprite,tooltip=self.draw_data.tooltip,style=mod_gui.button_style} + else + return root.add(self.draw_data) + end end --- Add a new input, this is the same as doing frame.add{} but returns a diffrent object -- @usage inputs.add{type='button',name='test',caption='Test'} --- @tparam table obj the new element to add +-- @tparam table obj the new element to add if caption is a sprite path then sprite is used -- @treturn table the custom input object function inputs.add(obj) if not is_type(obj,'table') then return end @@ -54,6 +60,7 @@ function inputs.add(obj) type == 'textfield' or type == 'text-box' then else return end + if obj.type == 'button' or obj.type == 'sprite-button' then obj.style = mod_gui.button_style end obj.draw_data = table.deepcopy(obj) obj.events = {} setmetatable(obj,{__index=inputs._input}) @@ -63,8 +70,12 @@ end -- this just runs the events given to inputs function inputs._event_handler(event) - local elements = Gui._get_data('inputs_'..event.element.type) + local elements = Gui._get_data('inputs_'..event.element.type) or {} local element = elements[event.element.name] + if not element and event.element.type == 'sprite-button' then + elements = Gui._get_data('inputs_button') or {} + element = elements[event.element.name] + end if element then local success, err = pcall(element.events[event.name],event) if not success then @@ -91,7 +102,7 @@ local test = Gui.inputs.add{ type='button', caption='Test' } -test:on_event(inputs.events.click,function(event) game.print('test') end) +test:on_event(Gui.inputs.events.click,function(event) game.print('test') end) -- then later in code local frame = player.gui.top.add{name='test',type='frame'} diff --git a/locale/ExpCore/GuiParts/toolbar.lua b/locale/ExpCore/GuiParts/toolbar.lua new file mode 100644 index 00000000..a9654991 --- /dev/null +++ b/locale/ExpCore/GuiParts/toolbar.lua @@ -0,0 +1,48 @@ +--[[ +Explosive Gaming + +This file can be used with permission but this and the credit below must remain in the file. +Contact a member of management on our discord to seek permission to use our code. +Any changes that you may make to the code are yours but that does not make the script yours. +Discord: https://discord.gg/r6dC2uK +]] + +local toolbar = {} + +--- Add a button to the toolbar, ranks need to be allowed to use these buttons if ranks is preset +-- @usage toolbar.add('foo','Foo','Test',function() game.print('test') end) +-- @tparam string name the name of the button +-- @tparam string caption can be a sprite path or text to show +-- @tparma string tooltip the help to show for the button +-- @tparam function callback the function which is called on_click +-- @treturn table the button object that was made +function toolbar.add(name,caption,tooltip,callback) + local button = Gui.inputs.add{type='button',name=name,caption=caption,tooltip=tooltip} + button:on_event(Gui.inputs.events.click,callback) + Gui._add_data('toolbar',name,button) + return button +end + +--- Draws the toolbar for a certain player +-- @usage toolbar.draw(1) +-- @param player the player to draw the tool bar of +function toolbar.draw(player) + local player = Game.get_player(player) + if not player then return end + local toolbar_frame = mod_gui.get_button_flow(player) + toolbar_frame.clear() + for name,button in pairs(Gui._get_data('toolbar')) do + if is_type(Ranking,'table') and Ranking._presets and Ranking._presets().meta.rank_count > 0 then + local rank = Ranking.get_rank(player) + if rank:allowed(name) then + button:draw(toolbar_frame) + end + else button:draw(toolbar_frame) end + end +end + +if defines.events.rank_change then + Event.register(defines.events.rank_change,toolbar.draw) +end + +return toolbar \ No newline at end of file diff --git a/locale/ExpCore/load.lua b/locale/ExpCore/load.lua index 0e698e0c..1154b3e6 100644 --- a/locale/ExpCore/load.lua +++ b/locale/ExpCore/load.lua @@ -23,7 +23,7 @@ StdExpCoreLib.Ranking = require '/ranking' StdExpCoreLib.Gui = require '/gui' StdExpCoreLib.Gui:_load_parts{ 'inputs', - --'toolbar', + 'toolbar', --'center', --'left', --'popup' diff --git a/locale/ExpCore/ranking.lua b/locale/ExpCore/ranking.lua index 502fa114..76e6dd2b 100644 --- a/locale/ExpCore/ranking.lua +++ b/locale/ExpCore/ranking.lua @@ -8,7 +8,7 @@ Discord: https://discord.gg/r6dC2uK ]] --Please Only Edit Below This Line----------------------------------------------------------- local Ranking = {} -Ranking.event = script.generate_event_name() +defines.events.rank_change = script.generate_event_name() Ranking._rank = {} Ranking._group = {} -- this function is to avoid errors - see /ranks.lua @@ -120,10 +120,10 @@ function Ranking.give_rank(player,rank,by_player,tick) player.permission_group = game.permissions.get_group(rank.name) player.tag = rank.tag if not old_rank.group.name == 'Jail' then Ranking._presets().old[player.index] = rank.name end - if Ranking.event then - script.raise_event(Ranking.event,{ + if defines.events.rank_change then + script.raise_event(defines.events.rank_change,{ tick=tick, - player=player, + player_index=player.index, by_player_name=by_player_name, new_rank=rank, old_rank=old_rank