mirror of
https://github.com/PHIDIAS0303/ExpCluster.git
synced 2025-12-27 11:35:22 +09:00
Added Helper Function For Buttons
This commit is contained in:
35
control.lua
35
control.lua
@@ -46,16 +46,41 @@ text = Gui.inputs.add{
|
||||
name='text-button',
|
||||
type='button',
|
||||
caption='Test'
|
||||
}
|
||||
text:on_event(Gui.inputs.events.click,function(event) game.print('test') end)
|
||||
}: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)
|
||||
}: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)
|
||||
Gui.toolbar.add('button3','item/lab','test btn3',function(event) game.print('test') end)
|
||||
|
||||
local input_test = Gui.inputs.add_button('test-inputs','Try RMB','alt,ctrl,shift and mouse buttons',{
|
||||
{
|
||||
function(player,mouse,keys) return mouse == defines.mouse_button_type.left and keys.alt end,
|
||||
function(player,element) player_return('Left: Alt',nil,player) end
|
||||
},
|
||||
{
|
||||
function(player,mouse,keys) return mouse == defines.mouse_button_type.left and keys.ctrl end,
|
||||
function(player,element) player_return('Left: Ctrl',nil,player) end
|
||||
},
|
||||
{
|
||||
function(player,mouse,keys) return mouse == defines.mouse_button_type.left and keys.shift end,
|
||||
function(player,element) player_return('Left: Shift',nil,player) end
|
||||
},
|
||||
{
|
||||
function(player,mouse,keys) return mouse == defines.mouse_button_type.right and keys.alt end,
|
||||
function(player,element) player_return('Right: Alt',nil,player) end
|
||||
},
|
||||
{
|
||||
function(player,mouse,keys) return mouse == defines.mouse_button_type.right and keys.ctrl end,
|
||||
function(player,element) player_return('Right: Ctrl',nil,player) end
|
||||
},
|
||||
{
|
||||
function(player,mouse,keys) return mouse == defines.mouse_button_type.right and keys.shift end,
|
||||
function(player,element) player_return('Right: Shift',nil,player) end
|
||||
}
|
||||
}):on_event('error',function(err) game.print('this is error handliling') end)
|
||||
@@ -24,11 +24,13 @@ inputs.events = {
|
||||
-- @usage button:on_event(defines.events.on_gui_click,player_return)
|
||||
-- @param event the event to raise callback on | can be number of the event | can be a key of inputs.events
|
||||
-- @tparam function callback the function you want to run on the event
|
||||
-- @treturn table returns self so you can chain together
|
||||
function inputs._input:on_event(event,callback)
|
||||
if not is_type(callback,'function') then return end
|
||||
if inputs.events[event] then event = inputs.events[event] end
|
||||
if event == 'error' then self._error = callback return end
|
||||
self.events[event] = callback
|
||||
return self
|
||||
end
|
||||
|
||||
--- Draw the input into the root element
|
||||
@@ -46,7 +48,7 @@ function inputs._input:draw(root)
|
||||
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'}
|
||||
-- @usage Gui.inputs.add{type='button',name='test',caption='Test'}
|
||||
-- @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)
|
||||
@@ -64,6 +66,7 @@ function inputs.add(obj)
|
||||
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.data = {}
|
||||
obj.events = {}
|
||||
setmetatable(obj,{__index=inputs._input})
|
||||
Gui._add_data('inputs_'..type,obj.name,obj)
|
||||
@@ -93,12 +96,48 @@ Event.register(inputs.events.elem,inputs._event_handler)
|
||||
Event.register(inputs.events.state,inputs._event_handler)
|
||||
Event.register(inputs.events.text,inputs._event_handler)
|
||||
|
||||
-- the folwing functions are just to make inputs easier but if what you want is not include use inputs.add(obj)
|
||||
--- Used to define a button, can have many function
|
||||
-- @usage Gui.inputs.add_button('test','Test','Just for testing',{{condition,callback},...})
|
||||
-- @tparam string name the name of this button
|
||||
-- @tparam string the display for this button, either text or sprite path
|
||||
-- @tparam string tooltip the tooltip to show on the button
|
||||
-- @param callbacks can either be a single function or a list of function pairs see exaplmes at bottom
|
||||
-- @treturn table the button object that was made, to allow a custom error event if wanted
|
||||
function inputs.add_button(name,display,tooltip,callbacks)
|
||||
local button = inputs.add{
|
||||
type='button'
|
||||
name=name,
|
||||
caption=display,
|
||||
tooltip=tooltip
|
||||
}
|
||||
button.data._callbacks = callbacks
|
||||
button:on_event('click',function(event)
|
||||
local mouse = event.button
|
||||
local keys = {alt=event.alt,ctrl=event.control,shift=event.shift}
|
||||
local player = Game.get_player(event)
|
||||
local element = event.element
|
||||
local callbacks = button.data._callback = callbacks
|
||||
if is_type(callbacks,'function') then callbacks = {function(...) return true end,callbacks}
|
||||
for _,data in pairs(callbacks) do
|
||||
if is_type(data[1],'function') and is_type(data[2],'function') then
|
||||
local success, err = pcall(data[1],player,mouse,keys,event)
|
||||
if success and err == true then
|
||||
local success, err = pcall(data[2],player,element,event)
|
||||
if not success then error(err) end
|
||||
elseif not success then error(err) end
|
||||
else error('Invalid Callback Condition Format') end
|
||||
end
|
||||
end)
|
||||
return button
|
||||
end
|
||||
|
||||
return inputs
|
||||
--[[
|
||||
|
||||
Input Example
|
||||
|
||||
-- button test
|
||||
-- Basic Button Using Gui.inputs.add
|
||||
local test = Gui.inputs.add{
|
||||
name='test-button',
|
||||
type='button',
|
||||
@@ -109,4 +148,32 @@ 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'}
|
||||
test:draw(frame)
|
||||
]]
|
||||
|
||||
-- Mutly Function Button Using Gui.inputs.add_button
|
||||
Gui.inputs.add_button('test-inputs','Try RMB','alt,ctrl,shift and mouse buttons',{
|
||||
{
|
||||
function(player,mouse,keys) return mouse == defines.mouse_button_type.left and keys.alt end,
|
||||
function(player,element) player_return('Left: Alt',nil,player) end
|
||||
},
|
||||
{
|
||||
function(player,mouse,keys) return mouse == defines.mouse_button_type.left and keys.ctrl end,
|
||||
function(player,element) player_return('Left: Ctrl',nil,player) end
|
||||
},
|
||||
{
|
||||
function(player,mouse,keys) return mouse == defines.mouse_button_type.left and keys.shift end,
|
||||
function(player,element) player_return('Left: Shift',nil,player) end
|
||||
},
|
||||
{
|
||||
function(player,mouse,keys) return mouse == defines.mouse_button_type.right and keys.alt end,
|
||||
function(player,element) player_return('Right: Alt',nil,player) end
|
||||
},
|
||||
{
|
||||
function(player,mouse,keys) return mouse == defines.mouse_button_type.right and keys.ctrl end,
|
||||
function(player,element) player_return('Right: Ctrl',nil,player) end
|
||||
},
|
||||
{
|
||||
function(player,mouse,keys) return mouse == defines.mouse_button_type.right and keys.shift end,
|
||||
function(player,element) player_return('Right: Shift',nil,player) end
|
||||
}
|
||||
}):on_event('error',function(err) game.print('this is error handliling') end)
|
||||
]]
|
||||
|
||||
Reference in New Issue
Block a user