From 2119378fadf6040eb264c23cc699866b0fcf62c2 Mon Sep 17 00:00:00 2001 From: Cooldude2606 Date: Mon, 6 May 2019 12:54:41 +0100 Subject: [PATCH] Added Buttons and Toolbar --- expcore/Gui/buttons.lua | 145 ++++++++++++++++++++++++++++++++++++++++ expcore/Gui/core.lua | 44 ++++++++++++ expcore/Gui/test.lua | 18 +++++ expcore/Gui/toolbar.lua | 57 ++++++++++++++++ expcore/gui.lua | 24 +++++++ 5 files changed, 288 insertions(+) create mode 100644 expcore/Gui/buttons.lua create mode 100644 expcore/Gui/core.lua create mode 100644 expcore/Gui/test.lua create mode 100644 expcore/Gui/toolbar.lua create mode 100644 expcore/gui.lua diff --git a/expcore/Gui/buttons.lua b/expcore/Gui/buttons.lua new file mode 100644 index 00000000..4219b2eb --- /dev/null +++ b/expcore/Gui/buttons.lua @@ -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 \ No newline at end of file diff --git a/expcore/Gui/core.lua b/expcore/Gui/core.lua new file mode 100644 index 00000000..e2c19c6b --- /dev/null +++ b/expcore/Gui/core.lua @@ -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 \ No newline at end of file diff --git a/expcore/Gui/test.lua b/expcore/Gui/test.lua new file mode 100644 index 00000000..9c4f05bb --- /dev/null +++ b/expcore/Gui/test.lua @@ -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) \ No newline at end of file diff --git a/expcore/Gui/toolbar.lua b/expcore/Gui/toolbar.lua new file mode 100644 index 00000000..ae219924 --- /dev/null +++ b/expcore/Gui/toolbar.lua @@ -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 \ No newline at end of file diff --git a/expcore/gui.lua b/expcore/gui.lua new file mode 100644 index 00000000..f7945442 --- /dev/null +++ b/expcore/gui.lua @@ -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 \ No newline at end of file