diff --git a/control.lua b/control.lua index bbca9a88..22097248 100644 --- a/control.lua +++ b/control.lua @@ -39,3 +39,4 @@ require('/locale/ExpCore/ranks') pcall(require,'/locale/Addons/playerRanks') -- this makes sure that all the little details are cleaned up Ranking._auto_edit_ranks() + diff --git a/locale/ExpCore/GuiParts/inputs.lua b/locale/ExpCore/GuiParts/inputs.lua index 97e56848..1f7989c3 100644 --- a/locale/ExpCore/GuiParts/inputs.lua +++ b/locale/ExpCore/GuiParts/inputs.lua @@ -8,5 +8,82 @@ Discord: https://discord.gg/r6dC2uK ]] local inputs = {} +inputs._input = {} -return inputs \ No newline at end of file +--- 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) +]] \ No newline at end of file diff --git a/locale/ExpCore/gui.lua b/locale/ExpCore/gui.lua index 94a362a8..5f909ba7 100644 --- a/locale/ExpCore/gui.lua +++ b/locale/ExpCore/gui.lua @@ -11,11 +11,10 @@ local Gui = {} local Gui_data = {} -- 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 not Gui_data[key] then Gui_data[key] = {value} - else table.insert(Gui_data[key],value) - end + if not Gui_data[key] then Gui_data[key] = {} end + Gui_data[key][value_key] = value end function Gui._get_data(key) return Gui_data[key] end diff --git a/locale/ExpCore/load.lua b/locale/ExpCore/load.lua index 3a8e0644..0e698e0c 100644 --- a/locale/ExpCore/load.lua +++ b/locale/ExpCore/load.lua @@ -21,7 +21,7 @@ require '/commands' StdExpCoreLib.Server = require '/server' StdExpCoreLib.Ranking = require '/ranking' StdExpCoreLib.Gui = require '/gui' -Gui:_load_parts{ +StdExpCoreLib.Gui:_load_parts{ 'inputs', --'toolbar', --'center',