Added checkboxes

This commit is contained in:
Cooldude2606
2019-05-10 19:00:06 +01:00
parent e3da2f6dc9
commit 5c43c5f854
4 changed files with 264 additions and 70 deletions

View File

@@ -19,17 +19,50 @@ local function get_config(name)
end
function Button.new_button(name)
local uid = Gui.uid_name()
Button.config[uid] = setmetatable({
local self = setmetatable({
name=uid,
clean_name=name,
style=mod_gui.button_style,
type='button'
clean_name=name
},{__index=Button._prototype})
self._draw.name = uid
self._draw.style = mod_gui.button_style
self._draw.type = 'button'
Button.config[uid] = self
if name then
Button.clean_names[uid]=name
Button.clean_names[name]=uid
end
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.mouse_button_filter and not self.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
if self._on_click then
self._on_click(event.player,event.element,event)
end
end)
return Button.config[uid]
end
@@ -38,29 +71,11 @@ function Button.draw_button(name,element)
return button:draw_to(element)
end
function Button._prototype:draw_to(element)
if element.children[self.name] then return end
local draw = table.deep_copy(self)
draw.authenticator = nil
draw.clean_name = nil
draw.raw_mouse_button_filter = nil
draw.key_button_filter = nil
draw._on_click = nil
draw._on_left_click = nil
draw._on_right_click = nil
draw.has_handler = nil
local self_element = element.add(draw)
if self.authenticator then
self_element.enabled = not not self.authenticator(element.player,self.clean_name or self.name)
end
return self_element
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
self._draw.type = 'sprite-button'
self._draw.sprite = sprite
self._draw.hovered_sprite = hovered_sprite
self._draw.clicked_sprite = clicked_sprite
return self
end
@@ -77,7 +92,6 @@ function Button._prototype:set_click_filter(filter,...)
end
end
self.mouse_button_filter = filter
self.raw_mouse_button_filter = filter
return self
end
@@ -92,20 +106,11 @@ function Button._prototype:set_key_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
@@ -114,7 +119,6 @@ function Button._prototype:on_left_click(callback)
return error('Event callback must be a function')
end
self._on_left_click = callback
self:_add_handler()
return self
end
@@ -123,37 +127,7 @@ function Button._prototype:on_right_click(callback)
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