diff --git a/control.lua b/control.lua index ec8f0068..48a4fbbc 100644 --- a/control.lua +++ b/control.lua @@ -4,7 +4,10 @@ -- all files which are loaded (including the config files) are present in ./config/file_loader.lua -- this file is the landing point for all scenarios please DO NOT edit directly, further comments are to aid development +log('[START] -----| Explosive Gaming Scenario Loader |-----') + -- Info on the data lifecycle and how we use it: https://github.com/Refactorio/RedMew/wiki/The-data-lifecycle +log('[INFO] Setting up lua environment') require 'resources.data_stages' _LIFECYCLE = _STAGE.control -- Control stage @@ -22,6 +25,7 @@ require 'resources.version' ext_require = require('expcore.common').ext_require -- Please go to config/file_loader.lua to edit the files that are loaded +log('[INFO] Getting file loader config') local files = require 'config.file_loader' -- Loads all files from the config and logs that they are loaded @@ -30,7 +34,7 @@ local errors = {} for index,path in pairs(files) do -- Loads the next file in the list - log(string.format('[INFO] Loading files %3d/%s (%s)',index,total_file_count,path)) + log(string.format('[INFO] Loading file %3d/%s (%s)',index,total_file_count,path)) local success,file = pcall(require,path) -- Error Checking @@ -49,4 +53,5 @@ end -- Logs all errors again to make it make it easy to find log('[INFO] All files loaded with '..#errors..' errors:') -for _,error in pairs(errors) do log(error) end \ No newline at end of file +for _,error in pairs(errors) do log(error) end +log('[END] -----| Explosive Gaming Scenario Loader |-----') \ No newline at end of file diff --git a/expcore/Gui/checkboxs.lua b/expcore/Gui/checkboxs.lua index 0f45e48c..da24ca07 100644 --- a/expcore/Gui/checkboxs.lua +++ b/expcore/Gui/checkboxs.lua @@ -6,7 +6,7 @@ local function store_state(self,element,value) element.state = value if self.events.on_state_change then local player = Game.get_player_by_index(element.player_index) - self.events.on_state_change(player,element) + self.events.on_state_change(player,element,value) end end @@ -56,7 +56,7 @@ function Checkbox.new_checkbox(name) self:set_store(category,value) elseif self.events.on_state_change then - self.events.on_state_change(event.player,element) + self.events.on_state_change(event.player,element,element.state) end end) diff --git a/expcore/Gui/dropdown.lua b/expcore/Gui/dropdown.lua new file mode 100644 index 00000000..764af040 --- /dev/null +++ b/expcore/Gui/dropdown.lua @@ -0,0 +1,124 @@ +local Gui = require './core' +local Game = require 'utils.game' + +local _select_value +local Dropdown = { + _prototype=Gui._extend_prototype{ + on_selection = Gui._new_event_adder('on_selection'), + add_store = Gui._new_store_adder(function(self,element,value) + _select_value(element,value) + local player = Game.get_player_by_index(element.player_index) + if self.events.on_selection then + self.events.on_selection(player,element,value) + end + if self.option_callbacks and self.option_callbacks[value] then + self.option_callbacks[value](player,element,value) + end + end) + } +} + +function Dropdown.new_dropdown(name) + + local self = Gui._new_define(Dropdown._prototype) + self.draw_data.type = 'drop-down' + + if name then + self:debug_name(name) + end + + self.post_draw = function(element) + if self.dynamic_options then + local player = Game.get_player_by_index(element.player_index) + local dynamic_options = self.dynamic_options(player,element) + local items = element.items + for _,v in pairs(dynamic_options) do + table.insert(items,v) + end + element.items = items + end + if self.store then + local category = self.categorize and self.categorize(element) or nil + local value = self:get_store(category) + if value then Dropdown.select_value(element,value) end + end + end + + Gui.on_selection_state_changed(self.name,function(event) + local element = event.element + local value = Dropdown.get_selected_value(element) + + if self.store then + local category = self.categorize and self.categorize(element) or value + self:set_store(category,value) + return + end + + if self.events.on_selection then + self.events.on_selection(event.player,element,value) + end + + if self.option_callbacks and self.option_callbacks[value] then + self.option_callbacks[value](event.player,element,value) + end + + end) + + return self +end + +function Dropdown._prototype:new_static_options(options,...) + if type(options) == 'string' then + options = {options} + for _,v in pairs({...}) do + table.insert(options,v) + end + end + self.options = options + self.draw_data.items = options + return self +end +Dropdown._prototype.add_options = Dropdown._prototype.new_static_options + +function Dropdown._prototype:new_dynamic_options(callback) + if type(callback) ~= 'function' then + return error('Dynamic options callback must be a function',2) + end + self.dynamic_options = callback + return self +end +Dropdown._prototype.add_dynamic = Dropdown._prototype.new_dynamic_options + +function Dropdown._prototype:add_option_callback(option,callback) + if not self.option_callbacks then self.option_callbacks = {} end + if not self.options then self.options = {} end + self.option_callbacks[option] = callback + if not table.contains(self.options,option) then + table.insert(self.options,option) + end + return self +end + +function Dropdown.select_value(element,value) + for k,item in pairs(element.items) do + if item == value then + element.selected_index = k + return k + end + end +end +_select_value = Dropdown.select_value + +function Dropdown.get_selected_value(element) + local index = element.selected_index + return element.items[index] +end + +function Dropdown.new_list_box(name) + local self = Dropdown.new_dropdown(name) + self.draw_data.type = 'list-box' + + return self +end + +return Dropdown \ No newline at end of file diff --git a/expcore/Gui/test.lua b/expcore/Gui/test.lua index 06988e7f..40562c3a 100644 --- a/expcore/Gui/test.lua +++ b/expcore/Gui/test.lua @@ -1,5 +1,5 @@ local Gui = require 'expcore.gui' -local format_chat_colour = ext_require('expcore.common','format_chat_colour') +local format_chat_colour,table_keys = ext_require('expcore.common','format_chat_colour','table_keys') local Colors = require 'resources.color_presets' local Game = require 'utils.game' local clean_stack_trace = ext_require('modules.commands.interface','clean_stack_trace') @@ -86,22 +86,22 @@ end) tests['Checkbox local'] = Gui.new_checkbox('test checkbox local') :set_caption('Checkbox Local') -:on_state_change(function(player,element) - player.print('Checkbox local: '..tostring(element.state)) +:on_state_change(function(player,element,state) + player.print('Checkbox local: '..tostring(state)) end) tests['Checkbox store game'] = Gui.new_checkbox('test checkbox store game') :set_caption('Checkbox Store Game') :add_store() -:on_state_change(function(player,element) - player.print('Checkbox store game: '..tostring(element.state)) +:on_state_change(function(player,element,state) + player.print('Checkbox store game: '..tostring(state)) end) tests['Checkbox store player'] = Gui.new_checkbox('test checkbox store player') :set_caption('Checkbox Store Player') :add_store(categozie_by_player) -:on_state_change(function(player,element) - player.print('Checkbox store player: '..tostring(element.state)) +:on_state_change(function(player,element,state) + player.print('Checkbox store player: '..tostring(state)) end) tests['Checkbox store force'] = Gui.new_checkbox('test checkbox store force') @@ -110,21 +110,21 @@ tests['Checkbox store force'] = Gui.new_checkbox('test checkbox store force') local player = Game.get_player_by_index(element.player_index) return player.force.name end) -:on_state_change(function(player,element) - player.print('Checkbox store force: '..tostring(element.state)) +:on_state_change(function(player,element,state) + player.print('Checkbox store force: '..tostring(state)) end) tests['Radiobutton local'] = Gui.new_radiobutton('test radiobutton local') :set_caption('Radiobutton Local') -:on_state_change(function(player,element) - player.print('Radiobutton local: '..tostring(element.state)) +:on_state_change(function(player,element,state) + player.print('Radiobutton local: '..tostring(state)) end) tests['Radiobutton store player'] = Gui.new_radiobutton('test radiobutton store player') :set_caption('Radiobutton Store Player') :add_store(categozie_by_player) -:on_state_change(function(player,element) - player.print('Radiobutton store player: '..tostring(element.state)) +:on_state_change(function(player,element,state) + player.print('Radiobutton store player: '..tostring(state)) end) local test_option_set = Gui.new_radiobutton_option_set('gui.test.share',function(value,category) @@ -134,20 +134,100 @@ end,categozie_by_player) tests['Radiobutton option one'] = Gui.new_radiobutton('test radiobutton option one') :set_caption('Radiobutton Option One') :add_as_option(test_option_set,'One') -:on_state_change(function(player,element) - player.print('Radiobutton option one: '..tostring(element.state)) +:on_state_change(function(player,element,state) + player.print('Radiobutton option one: '..tostring(state)) end) tests['Radiobutton option two'] = Gui.new_radiobutton('test radiobutton option two') :set_caption('Radiobutton Option Two') :add_as_option(test_option_set,'Two') -:on_state_change(function(player,element) - player.print('Radiobutton option two: '..tostring(element.state)) +:on_state_change(function(player,element,state) + player.print('Radiobutton option two: '..tostring(state)) end) tests['Radiobutton option three'] = Gui.new_radiobutton('test radiobutton option three') :set_caption('Radiobutton Option Three') :add_as_option(test_option_set,'Three') -:on_state_change(function(player,element) - player.print('Radiobutton option three: '..tostring(element.state)) +:on_state_change(function(player,element,state) + player.print('Radiobutton option three: '..tostring(state)) +end) + +tests['Dropdown local static general'] = Gui.new_dropdown('test dropdown local static general') +:set_tooltip('Dropdown Local Static General') +:add_options('One','Two','Three','Four') +:on_selection(function(player,element,value) + player.print('Dropdown local static general: '..tostring(value)) +end) + +tests['Dropdown player static general'] = Gui.new_dropdown('test dropdown player static general') +:set_tooltip('Dropdown Player Static General') +:add_options('One','Two','Three','Four') +:add_store(categozie_by_player) +:on_selection(function(player,element,value) + player.print('Dropdown player static general: '..tostring(value)) +end) + +local function print_option_selected_1(player,element,value) + player.print('Dropdown local static case (case): '..tostring(value)) +end +tests['Dropdown local static case'] = Gui.new_dropdown('test dropdown local static case') +:set_tooltip('Dropdown Local Static Case') +:add_options('One','Two') +:add_option_callback('One',print_option_selected_1) +:add_option_callback('Two',print_option_selected_1) +:add_option_callback('Three',print_option_selected_1) +:add_option_callback('Four',print_option_selected_1) +:on_selection(function(player,element,value) + player.print('Dropdown local static case (general): '..tostring(value)) +end) + +local function print_option_selected_2(player,element,value) + player.print('Dropdown player static case (case): '..tostring(value)) +end +tests['Dropdown player static case'] = Gui.new_dropdown('test dropdown player static case') +:set_tooltip('Dropdown Player Static Case') +:add_store(categozie_by_player) +:add_options('One','Two') +:add_option_callback('One',print_option_selected_2) +:add_option_callback('Two',print_option_selected_2) +:add_option_callback('Three',print_option_selected_2) +:add_option_callback('Four',print_option_selected_2) +:on_selection(function(player,element,value) + player.print('Dropdown player static case (general): '..tostring(value)) +end) + +tests['Dropdown local dynamic general'] = Gui.new_dropdown('test dropdown local dynamic general') +:set_tooltip('Dropdown Local Dynamic General') +:add_options('Static') +:add_dynamic(function(player,element) + return table_keys(Colors) +end) +:on_selection(function(player,element,value) + player.print('Dropdown local dynamic general: '..tostring(value)) +end) + +tests['Dropdown player dynamic general'] = Gui.new_dropdown('test dropdown player dynamic general') +:set_tooltip('Dropdown Player Dynamic General') +:add_options('Static') +:add_dynamic(function(player,element) + return table_keys(Colors) +end) +:add_store(categozie_by_player) +:on_selection(function(player,element,value) + player.print('Dropdown player dynamic general: '..tostring(value)) +end) + +tests['List box local static general'] = Gui.new_list_box('test list box local static general') +:set_tooltip('List Box Local Static General') +:add_options('One','Two','Three','Four') +:on_selection(function(player,element,value) + player.print('Dropdown local static general: '..tostring(value)) +end) + +tests['List box player static general'] = Gui.new_list_box('test list box player static general') +:set_tooltip('List Box Player Static General') +:add_options('One','Two','Three','Four') +:add_store(categozie_by_player) +:on_selection(function(player,element,value) + player.print('Dropdown player static general: '..tostring(value)) end) \ No newline at end of file diff --git a/expcore/gui.lua b/expcore/gui.lua index b9a50b1a..d1343106 100644 --- a/expcore/gui.lua +++ b/expcore/gui.lua @@ -17,4 +17,9 @@ Gui.new_radiobutton = Checkbox.new_radiobutton Gui.new_radiobutton_option_set = Checkbox.new_option_set Gui.classes.checkbox = Checkbox +local Dropdown = require('./gui/dropdown') +Gui.new_dropdown = Dropdown.new_dropdown +Gui.new_list_box = Dropdown.new_list_box +Gui.classes.dropdown = Dropdown + return Gui \ No newline at end of file diff --git a/utils/event_core.lua b/utils/event_core.lua index 6ea0dac0..364e3134 100644 --- a/utils/event_core.lua +++ b/utils/event_core.lua @@ -40,6 +40,7 @@ end local function on_init() _LIFECYCLE = 5 -- on_init + log('[INFO] Entering on_init') local handlers = event_handlers[init_event_name] call_handlers(handlers) @@ -47,10 +48,12 @@ local function on_init() event_handlers[load_event_name] = nil _LIFECYCLE = 8 -- Runtime + log('[INFO] Entering runtime') end local function on_load() _LIFECYCLE = 6 -- on_load + log('[INFO] Entering on_load') local handlers = event_handlers[load_event_name] call_handlers(handlers) @@ -58,6 +61,7 @@ local function on_load() event_handlers[load_event_name] = nil _LIFECYCLE = 8 -- Runtime + log('[INFO] Entering runtime') end local function on_nth_tick_event(event)