From 32f41ff72212eb5411b4f54b85b676c6a69c84c1 Mon Sep 17 00:00:00 2001 From: Cooldude2606 Date: Tue, 27 Aug 2019 19:23:25 +0100 Subject: [PATCH] First Test Success --- config/_file_loader.lua | 2 +- expcore/gui.lua | 25 +++++++++ expcore/gui/core.lua | 53 ++++++++++++++----- expcore/gui/prototype.lua | 48 +++++++++++++++++- expcore/gui/test.lua | 93 ++++++++++++++++++++++++++++++++++ modules/commands/interface.lua | 2 +- 6 files changed, 207 insertions(+), 16 deletions(-) create mode 100644 expcore/gui/test.lua diff --git a/config/_file_loader.lua b/config/_file_loader.lua index ab0c84e4..e9be5b99 100644 --- a/config/_file_loader.lua +++ b/config/_file_loader.lua @@ -51,5 +51,5 @@ return { 'config.expcore-commands.auth_runtime_disable', -- allows commands to be enabled and disabled during runtime 'config.permission_groups', -- loads some predefined permission groups 'config.roles', -- loads some predefined roles - --'expcore.gui.test' -- loads multiple gui defines to test the gui system + 'expcore.gui.test' -- loads multiple gui defines to test the gui system } \ No newline at end of file diff --git a/expcore/gui.lua b/expcore/gui.lua index 3dbe55df..7ab0bdd3 100644 --- a/expcore/gui.lua +++ b/expcore/gui.lua @@ -17,6 +17,31 @@ end) properties.caption = nil properties.type = 'sprite-button' end) +:define_draw(function(properties,parent,element) + -- Note that element might be nil if this is the first draw function + -- in this case button is a new concept so we know this is the first function and element is nil + if properties.type == 'button' then + element = parent.draw{ + type = properties.type, + name = properties.name, + caption = properties.caption, + tooltip = properties.tooltip + } + + else + element = parent.draw{ + type = properties.type, + name = properties.name, + sprite = properties.sprite, + tooltip = properties.tooltip + } + + end + + -- We must return the element or what we want to be seen as the instance, this is so other draw functions have access to it + -- for example if our custom button defined a draw function to change the font color to red + return element +end) @usage-- Makeing a alternative button based on the first local custom_button = diff --git a/expcore/gui/core.lua b/expcore/gui/core.lua index 3fa4fa93..07a69097 100644 --- a/expcore/gui/core.lua +++ b/expcore/gui/core.lua @@ -14,21 +14,53 @@ local Gui = { concepts = {} } +--[[-- Gets the gui concept with this name +@tparam string name the name of the concept that you want to get +@usage-- Getting a gui concept +local button = Gui.get_concept('Button') +]] +function Gui.get_concept(name) + return Gui.concepts[name] or error('Gui concept "'..name..'" is not defind',2) +end + +--[[-- Used internally to save concept names to the core gui module +@function Prototype:change_name +@tparam[opt=self.name] string new_name the new name of the concept +@usage-- Internal Saving +-- this is never needed to be done, internal use only! +local button = Gui.get_concept('Button') +button:change_name('Not Button') +]] +function Prototype:change_name(new_name) + if new_name then + Gui.concepts[self.name] = nil + self.name = new_name + self.properties.name = new_name + end + + Gui.concepts[self.name] = self + return self +end + +--[[-- Returns a new gui concept with no properties or events +@tparam string name the name that you want this concept to have +@usage-- Making a new concept, see module usage +local button = Gui.new_concept('Button') +]] function Gui.new_concept(name) if Gui.concepts[name] then error('Gui concept "'..name..'" is already defind',2) end - local concept = Prototype:clone(name) - Gui.concepts[name] = concept - - return concept -end - -function Gui.get_concept(name) - return Gui.concepts[name] or error('Gui concept "'..name..'" is not defind',2) + return Prototype:clone(name) end +--[[-- Making anew concept based on the properties and drawing of another +@tparam string name the name of the concept that you want as the base +@tparam string new_name the name that you want the new concept to have +@usage-- Making a new concept from another, see module usage +local custom_button = Gui.clone_concept('Button','CustomButton') +]] function Gui.clone_concept(name,new_name) local concept = Gui.concepts[name] or error('Gui concept "'..name..'" is not defind',2) @@ -36,10 +68,7 @@ function Gui.clone_concept(name,new_name) error('Gui concept "'..name..'" is already defind',2) end - local new_concept = concept:clone(new_name) - Gui.concepts[new_name] = new_concept - - return new_concept + return concept:clone(new_name) end return Gui \ No newline at end of file diff --git a/expcore/gui/prototype.lua b/expcore/gui/prototype.lua index 4c92dfa5..79128ef8 100644 --- a/expcore/gui/prototype.lua +++ b/expcore/gui/prototype.lua @@ -18,6 +18,31 @@ end) properties.caption = nil properties.type = 'sprite-button' end) +:define_draw(function(properties,parent,element) + -- Note that element might be nil if this is the first draw function + -- in this case button is a new concept so we know this is the first function and element is nil + if properties.type == 'button' then + element = parent.draw{ + type = properties.type, + name = properties.name, + caption = properties.caption, + tooltip = properties.tooltip + } + + else + element = parent.draw{ + type = properties.type, + name = properties.name, + sprite = properties.sprite, + tooltip = properties.tooltip + } + + end + + -- We must return the element or what we want to be seen as the instance, this is so other draw functions have access to it + -- for example if our custom button defined a draw function to change the font color to red + return element +end) local custom_button = button:clone('CustomButton') @@ -117,9 +142,27 @@ function Prototype:clone(concept_name) concept.set_store_from_instance = nil end + -- Sets the concept name + concept:change_name() + return concept end +--[[-- Used internally to save concept names to the core gui module +@function Prototype:change_name +@tparam[opt=self.name] string new_name the new name of the concept +@usage-- Internal Saving +-- this is never needed to be done, internal use only! +local button = Gui.get_concept('Button') +button:change_name('Not Button') +]] +function Prototype:change_name(new_name) + -- over writen in core file + self.name = new_name or self.name + self.properties.name = self.name + return self +end + --[[-- Adds a new event trigger to the concept which can be linked to a factorio event @tparam string event_name the name of the event to add, must be unique, recomented to start with "on_" @tparam[opt] defines.events factorio_event when given will fire the custom event when the factorio event is raised @@ -293,6 +336,7 @@ Gui.get_concept('Button') :define_draw(function(properties,parent,element) -- Note that element might be nil if this is the first draw function -- for this example we assume button was cloned from Prototype and so has no other draw functions defined + -- this means that there is no element yet and what we return will be the first time the element is returned -- although not shown here you also can recive any extra arguments here from the call to draw if properties.type == 'button' then element = parent.draw{ @@ -312,8 +356,8 @@ Gui.get_concept('Button') end - -- We must return the element or what we want to be seen as the instance - -- this is so other draw functions have access to it, say if our custom button defined a draw function to change the font color to red + -- We must return the element or what we want to be seen as the instance, this is so other draw functions have access to it + -- for example if our custom button defined a draw function to change the font color to red return element end) ]] diff --git a/expcore/gui/test.lua b/expcore/gui/test.lua new file mode 100644 index 00000000..fd6d6796 --- /dev/null +++ b/expcore/gui/test.lua @@ -0,0 +1,93 @@ +--[[-- Core Module - Gui + @module Gui + @alias Gui +]] + +local Gui = require 'expcore.gui' + +local tests = {} + +--[[-- Runs a set of gui tests to ensure that the system is working +@tparam LuaPlayer player the player that the guis are made for and who recives the results +@tparam[opt] string category when given only tests in this category are ran +@usage-- Run all gui tests +Gui.run_tests(Gui.test_string_return(game.print)) +]] +function Gui.run_tests(player,category) + local results = { + passed = 0, + failed = 0, + total = 0, + errors = {} + } + + if not category then + results.breakdown = {} + + for cat,_ in pairs(tests) do + local rtn = Gui.run_tests(player,cat) + results.passed = results.passed + rtn.passed + results.failed = results.failed + rtn.failed + results.total = results.total + rtn.total + + for test_name, err in pairs(rtn.errors) do + results.errors[cat..'/'..test_name] = err + end + + results.breakdown[cat] = rtn + end + + player.print(string.format('All Tests Complete. %d failed.',results.failed)) + + return results + end + + local cat_tests = tests[category] + + results.total = #cat_tests + + local output = player.print + for test_name, callback in pairs(cat_tests) do + local success, err = pcall(callback,player) + + if success then + results.passed = results.passed + 1 + else + results.erorrs[test_name] = err + output(string.format('Test "%s / %s" failed:\n%s',category,test_name,err)) + end + + end + + output(string.format('Test Complete "%s". %d failed.',category,results.failed)) + + return results +end + +--[[ +Basic frame creation +]] + +local test_frame = +Gui.new_concept('test_frame') +:define_draw(function(properties,parent,element) + element = + parent.add{ + name = properties.name, + type = 'frame', + caption = 'Gui Tests' + } + + element.add{ + type = 'label', + caption = 'Hello, World!' + } + + return element +end) + +tests.Frame = { + ['Draw Frame'] = function(player) + test_frame:draw(player.gui.center) + end +} \ No newline at end of file diff --git a/modules/commands/interface.lua b/modules/commands/interface.lua index cd6263b1..51b20ebf 100644 --- a/modules/commands/interface.lua +++ b/modules/commands/interface.lua @@ -16,7 +16,7 @@ local interface_modules = { ['Group']='expcore.permission_groups', ['Roles']='expcore.roles', ['Store']='expcore.store', - --['Gui']='expcore.gui', + ['Gui']='expcore.gui', ['Sudo']='expcore.sudo' }