mirror of
https://github.com/PHIDIAS0303/ExpCluster.git
synced 2025-12-30 20:41:41 +09:00
Added toolbar
This commit is contained in:
@@ -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'}
|
||||
|
||||
48
locale/ExpCore/GuiParts/toolbar.lua
Normal file
48
locale/ExpCore/GuiParts/toolbar.lua
Normal file
@@ -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
|
||||
Reference in New Issue
Block a user