Buttons added

This commit is contained in:
Cooldude2606
2017-12-11 20:07:06 +00:00
parent 7e0caa0922
commit a4eeef1d1a
4 changed files with 83 additions and 6 deletions

View File

@@ -39,3 +39,4 @@ require('/locale/ExpCore/ranks')
pcall(require,'/locale/Addons/playerRanks') pcall(require,'/locale/Addons/playerRanks')
-- this makes sure that all the little details are cleaned up -- this makes sure that all the little details are cleaned up
Ranking._auto_edit_ranks() Ranking._auto_edit_ranks()

View File

@@ -8,5 +8,82 @@ Discord: https://discord.gg/r6dC2uK
]] ]]
local inputs = {} local inputs = {}
inputs._input = {}
return inputs --- Sets the input to trigger on an certain event
-- @usage button:on_event(defines.events.on_gui_click,player_return)
-- @tparam number event the event to raise callback on | can also be 'error'
-- @tparam function callback the function you want to run on the event
function inputs._input:on_event(event,callback)
if not is_type(callback,'function') then return end
if event == 'error' then self._error = callback return end
self.events[event] = callback
end
--- Draw the input into the root element
-- @usage button:draw(frame)
-- @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)
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
-- @treturn table the custom input object
function inputs.add(obj)
if not is_type(obj,'table') then return end
if not is_type(obj.type,'string') then return end
local type = obj.type
if type == 'button' or
type == 'sprite-button' or
type == 'choose-elem-button' or
type == 'checkbox' or
type == 'radiobutton' or
type == 'textfield' or
type == 'text-box'
then else return end
obj.draw_data = table.deepcopy(obj)
obj.events = {}
setmetatable(obj,{__index=inputs._input})
Gui._add_data('inputs_'..type,obj.name,obj)
return obj
end
-- this just runs the events given to inputs
function inputs._event_handler(event)
local elements = Gui._get_data('inputs_'..event.element.type)
local element = elements[event.element.name]
if element then
local success, err = pcall(element.events[event.name],event)
if not success then
if is_type(element._error,'function') then pcall(element._error)
else error(err) end
end
end
end
Event.register(defines.events.on_gui_checked_state_changed,inputs._event_handler)
Event.register(defines.events.on_gui_click,inputs._event_handler)
Event.register(defines.events.on_gui_elem_changed,inputs._event_handler)
Event.register(defines.events.on_gui_selection_state_changed,inputs._event_handler)
Event.register(defines.events.on_gui_text_changed,inputs._event_handler)
return inputs
--[[
Input Example
-- button test
local test = Gui.inputs.add{
name='test-button',
type='button',
caption='Test'
}
test:on_event(defines.events.on_gui_click,function(event) game.print('test') end)
-- then later in code
local frame = player.gui.top.add{name='test',type='frame'}
test:draw(frame)
]]

View File

@@ -11,11 +11,10 @@ local Gui = {}
local Gui_data = {} local Gui_data = {}
-- this is to enforce the read only propetry of the gui -- this is to enforce the read only propetry of the gui
function Gui._add_data(key,value) function Gui._add_data(key,value_key,value)
if game then return end if game then return end
if not Gui_data[key] then Gui_data[key] = {value} if not Gui_data[key] then Gui_data[key] = {} end
else table.insert(Gui_data[key],value) Gui_data[key][value_key] = value
end
end end
function Gui._get_data(key) return Gui_data[key] end function Gui._get_data(key) return Gui_data[key] end

View File

@@ -21,7 +21,7 @@ require '/commands'
StdExpCoreLib.Server = require '/server' StdExpCoreLib.Server = require '/server'
StdExpCoreLib.Ranking = require '/ranking' StdExpCoreLib.Ranking = require '/ranking'
StdExpCoreLib.Gui = require '/gui' StdExpCoreLib.Gui = require '/gui'
Gui:_load_parts{ StdExpCoreLib.Gui:_load_parts{
'inputs', 'inputs',
--'toolbar', --'toolbar',
--'center', --'center',