mirror of
https://github.com/PHIDIAS0303/ExpCluster.git
synced 2025-12-27 11:35:22 +09:00
Added Buttons and Toolbar
This commit is contained in:
145
expcore/Gui/buttons.lua
Normal file
145
expcore/Gui/buttons.lua
Normal file
@@ -0,0 +1,145 @@
|
||||
--- Adds a button handler
|
||||
local mod_gui = require 'mod-gui'
|
||||
local Gui = require './core'
|
||||
|
||||
local Button = {
|
||||
config={},
|
||||
clean_names={},
|
||||
_prototype = Gui._set_up_prototype{}
|
||||
}
|
||||
|
||||
function Button.new_button(name)
|
||||
local uid = Gui.uid_name()
|
||||
Button.config[uid] = setmetatable({
|
||||
name=uid,
|
||||
clean_name=name,
|
||||
style=mod_gui.button_style,
|
||||
type='button'
|
||||
},{__index=Button._prototype})
|
||||
Button.clean_names[uid]=name
|
||||
Button.clean_names[name]=uid
|
||||
return Button.config[uid]
|
||||
end
|
||||
|
||||
function Button.draw_button(name,element)
|
||||
local button = Button.config[name]
|
||||
if not button then
|
||||
button = Button.clean_names[name]
|
||||
if not button then
|
||||
return error('Button with uid: '..name..' does not exist')
|
||||
else
|
||||
button = Button.config[button]
|
||||
end
|
||||
end
|
||||
button:draw_to(element)
|
||||
end
|
||||
|
||||
function Button._prototype:draw_to(element)
|
||||
if element.children[self.name] then return end
|
||||
local self_element = element.add(self)
|
||||
if self.authenticator then
|
||||
self_element.enabled = not not self.authenticator(element.player,self.clean_name or self.name)
|
||||
end
|
||||
end
|
||||
|
||||
function Button._prototype:set_sprites(sprite,hovered_sprite,clicked_sprite)
|
||||
self.type = 'sprite-button'
|
||||
self.sprite = sprite
|
||||
self.hovered_sprite = hovered_sprite
|
||||
self.clicked_sprite = clicked_sprite
|
||||
return self
|
||||
end
|
||||
|
||||
function Button._prototype:set_click_filter(filter,...)
|
||||
if type(filter) == 'string' then
|
||||
filter = {[filter]=true}
|
||||
for _,v in pairs({...}) do
|
||||
filter[v] = true
|
||||
end
|
||||
end
|
||||
for k,v in pairs(filter) do
|
||||
if type(v) == 'string' then
|
||||
filter[k] = defines.mouse_button_type[v]
|
||||
end
|
||||
end
|
||||
self.mouse_button_filter = filter
|
||||
self.raw_mouse_button_filter = filter
|
||||
return self
|
||||
end
|
||||
|
||||
function Button._prototype:set_key_filter(filter,...)
|
||||
if type(filter) == 'string' then
|
||||
filter = {[filter]=true}
|
||||
for _,v in pairs({...}) do
|
||||
filter[v] = true
|
||||
end
|
||||
end
|
||||
self.key_button_filter = filter
|
||||
return self
|
||||
end
|
||||
|
||||
function Button._prototype:set_authenticator(callback)
|
||||
if type(callback) ~= 'function' then
|
||||
return error('Authenicater callback must be a function')
|
||||
end
|
||||
self.authenticator = callback
|
||||
return self
|
||||
end
|
||||
|
||||
function Button._prototype:on_click(callback)
|
||||
if type(callback) ~= 'function' then
|
||||
return error('Event callback must be a function')
|
||||
end
|
||||
self.on_click = callback
|
||||
self:_add_handler()
|
||||
return self
|
||||
end
|
||||
|
||||
function Button._prototype:on_left_click(callback)
|
||||
if type(callback) ~= 'function' then
|
||||
return error('Event callback must be a function')
|
||||
end
|
||||
self.on_left_click = callback
|
||||
self:_add_handler()
|
||||
return self
|
||||
end
|
||||
|
||||
function Button._prototype:on_right_click(callback)
|
||||
if type(callback) ~= 'function' then
|
||||
return error('Event callback must be a function')
|
||||
end
|
||||
self.on_right_click = callback
|
||||
self:_add_handler()
|
||||
return self
|
||||
end
|
||||
|
||||
function Button._prototype:_add_handler()
|
||||
if self.has_handler then return end
|
||||
self.has_handler = true
|
||||
Gui.on_click(self.name,function(event)
|
||||
local mosue_button = event.button
|
||||
local keys = {alt=event.alt,control=event.control,shift=event.shift}
|
||||
event.keys = keys
|
||||
|
||||
if self.authenticator then
|
||||
if not self.authenticator(event.player,self.clean_name or self.name) then return end
|
||||
end
|
||||
|
||||
if mosue_button == defines.mouse_button_type.left and self.on_left_click then
|
||||
self.on_left_click(event.player,event.element,event)
|
||||
elseif mosue_button == defines.mouse_button_type.right and self.on_right_click then
|
||||
self.on_right_click(event.player,event.element,event)
|
||||
end
|
||||
|
||||
if self.raw_mouse_button_filter and not self.raw_mouse_button_filter[mosue_button] then return end
|
||||
if self.key_button_filter then
|
||||
for key,state in pairs(self.key_button_filter) do
|
||||
if state and not keys[key] then return end
|
||||
end
|
||||
end
|
||||
|
||||
self.on_click(event.player,event.element,event)
|
||||
end)
|
||||
end
|
||||
|
||||
return Button
|
||||
44
expcore/Gui/core.lua
Normal file
44
expcore/Gui/core.lua
Normal file
@@ -0,0 +1,44 @@
|
||||
local Gui = require 'utils.gui'
|
||||
|
||||
Gui._prototype = {}
|
||||
Gui.inputs = {}
|
||||
Gui.structure = {}
|
||||
Gui.outputs = {}
|
||||
|
||||
function Gui._extend_prototype(tbl)
|
||||
for k,v in pairs(Gui._prototype) do
|
||||
if not tbl[k] then tbl[k] = v end
|
||||
end
|
||||
end
|
||||
|
||||
--- Sets the caption for the element config
|
||||
function Gui._prototype:set_caption(caption)
|
||||
self.caption = caption
|
||||
end
|
||||
|
||||
--- Sets the tooltip for the element config
|
||||
function Gui._prototype:set_tooltip(tooltip)
|
||||
self.tooltip = tooltip
|
||||
end
|
||||
|
||||
function Gui.toggle_enable(element)
|
||||
if not element or not element.valid then return end
|
||||
if not element.enabled then
|
||||
-- this way round so if its nil it will become false
|
||||
element.enabled = true
|
||||
else
|
||||
element.enabled = false
|
||||
end
|
||||
end
|
||||
|
||||
function Gui.toggle_visible(element)
|
||||
if not element or not element.valid then return end
|
||||
if not element.visible then
|
||||
-- this way round so if its nil it will become false
|
||||
element.visible = true
|
||||
else
|
||||
element.visible = false
|
||||
end
|
||||
end
|
||||
|
||||
return Gui
|
||||
18
expcore/Gui/test.lua
Normal file
18
expcore/Gui/test.lua
Normal file
@@ -0,0 +1,18 @@
|
||||
local Gui = require 'expcore.gui'
|
||||
|
||||
Gui.new_toolbar_button('click-1')
|
||||
:on_click(function(player,element,event)
|
||||
player.print('CLICK 1')
|
||||
end)
|
||||
|
||||
Gui.new_toolbar_button('click-2')
|
||||
:set_caption('Click Two')
|
||||
:on_click(function(player,element,event)
|
||||
player.print('CLICK 2')
|
||||
end)
|
||||
|
||||
Gui.new_toolbar_button('click-3')
|
||||
:set_sprites('utility/questionmark')
|
||||
:on_click(function(player,element,event)
|
||||
player.print('CLICK 3')
|
||||
end)
|
||||
57
expcore/Gui/toolbar.lua
Normal file
57
expcore/Gui/toolbar.lua
Normal file
@@ -0,0 +1,57 @@
|
||||
local Buttons = require './buttons'
|
||||
local Gui = require './core'
|
||||
local Roles = require 'expre.roles'
|
||||
local Event = require 'utils.event'
|
||||
local Game = require 'utils.game'
|
||||
|
||||
local Toolbar = {
|
||||
buttons = {}
|
||||
}
|
||||
|
||||
function Toolbar.new_button(name)
|
||||
name = name or #Toolbar.buttons+1
|
||||
local button = Buttons.new_button('toolbar/'..name)
|
||||
button:set_authenticator(Roles.player_allowed)
|
||||
return button
|
||||
end
|
||||
|
||||
function Toolbar.add_button(button)
|
||||
table.insert(Toolbar.buttons,button)
|
||||
Gui.allow_player_to_toggle_top_element_visibility(button.name)
|
||||
Gui.on_player_show_top(button.name,function(event)
|
||||
if not button.authenticator(player,button.clean_name or button.name) then
|
||||
event.element.visible = false
|
||||
end
|
||||
end)
|
||||
if not button.authenticator then
|
||||
button:set_authenticator(function() return true end)
|
||||
end
|
||||
end
|
||||
|
||||
function Toolbar.draw(player)
|
||||
for _,button in pairs(Toolbar.buttons) do
|
||||
local self_button = button:draw_to(player.gui.top)
|
||||
if button.authenticator(player,button.clean_name or button.name) then
|
||||
self_button.visible = true
|
||||
else
|
||||
self_button.visible = false
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
Event.add(defines.events.on_player_created,function(event)
|
||||
local player = Game.get_player_by_index(event.player_index)
|
||||
Toolbar.draw(player)
|
||||
end)
|
||||
|
||||
Event.add(Roles.player_role_assigned,function(event)
|
||||
local player = Game.get_player_by_index(event.player_index)
|
||||
Toolbar.draw(player)
|
||||
end)
|
||||
|
||||
Event.add(Roles.player_role_unassigned,function(event)
|
||||
local player = Game.get_player_by_index(event.player_index)
|
||||
Toolbar.draw(player)
|
||||
end)
|
||||
|
||||
return Toolbar
|
||||
24
expcore/gui.lua
Normal file
24
expcore/gui.lua
Normal file
@@ -0,0 +1,24 @@
|
||||
-- This file is used to require all the different elements of the gui module
|
||||
local opt_require = ext_require('expcore.common','opt_require')
|
||||
|
||||
local Gui = require('./gui/core')
|
||||
|
||||
local Buttons = require('./gui/buttons')
|
||||
Gui.new_button = Buttons.new_button
|
||||
Gui.inputs.buttons = Buttons
|
||||
|
||||
local Toolbar = require('./gui/toolbar')
|
||||
Gui.new_toolbar_button = Toolbar.new_button
|
||||
Gui.add_button_to_toolbar = Toolbar.add_button
|
||||
Gui.structure.toolbar = Toolbar
|
||||
|
||||
--[[local Checkboxs = opt_require('./gui/checkboxs')
|
||||
Gui.new_checkbox = Checkboxs.new_checkbox
|
||||
Gui.new_radiobutton = Checkboxs.new_radiobutton
|
||||
Gui.inputs.checkboxs = Checkboxs
|
||||
|
||||
local TextEntry = opt_require('./gui/text')
|
||||
Gui.new_text_entry = TextEntry.new_text_entry
|
||||
Gui.inputs.text_entrys = TextEntry
|
||||
]]
|
||||
return Gui
|
||||
Reference in New Issue
Block a user