mirror of
https://github.com/PHIDIAS0303/ExpCluster.git
synced 2025-12-29 20:16:38 +09:00
Added toolbar
This commit is contained in:
@@ -50,21 +50,21 @@ end)
|
||||
properties.type = 'sprite-button'
|
||||
end)
|
||||
:define_draw(function(properties,parent,element)
|
||||
if properties.type == 'button' then
|
||||
if properties.type == 'sprite-button' then
|
||||
element = parent.add{
|
||||
name = properties.name,
|
||||
type = properties.type,
|
||||
caption = properties.caption,
|
||||
type = 'sprite-button',
|
||||
sprite = properties.sprite,
|
||||
hovered_sprite = properties.hovered_sprite,
|
||||
clicked_sprite = properties.clicked_sprite,
|
||||
tooltip = properties.tooltip
|
||||
}
|
||||
|
||||
else
|
||||
element = parent.add{
|
||||
name = properties.name,
|
||||
type = properties.type,
|
||||
sprite = properties.sprite,
|
||||
hovered_sprite = properties.hovered_sprite,
|
||||
clicked_sprite = properties.clicked_sprite,
|
||||
type = 'button',
|
||||
caption = properties.caption,
|
||||
tooltip = properties.tooltip
|
||||
}
|
||||
|
||||
|
||||
@@ -188,6 +188,7 @@ function Gui.set_padding(element,up,down,left,right)
|
||||
end
|
||||
|
||||
--[[ Used to check a property exists and if it is a function then call the function
|
||||
@function Gui.resolve_property
|
||||
@tparam any value the value that you are testing exists and call if its a function
|
||||
@tparam LuaGuiElement element the element that is passed to the function if it is a function
|
||||
@treturn any the value or what it returns if it is a function
|
||||
|
||||
@@ -75,6 +75,7 @@ local Game = require 'utils.game' -- @dep utils.game
|
||||
local Factorio_Events = {}
|
||||
local Prototype = {
|
||||
draw_callbacks = {},
|
||||
clone_callbacks = {},
|
||||
properties = {},
|
||||
factorio_events = {},
|
||||
events = {}
|
||||
@@ -159,9 +160,40 @@ function Prototype:clone(concept_name)
|
||||
concept.set_store_from_instance = nil
|
||||
end
|
||||
|
||||
-- Loop over all the clone defines, element is updated when a value is returned
|
||||
for _,clone_callback in pairs(concept.clone_callbacks) do
|
||||
local success, rtn = pcall(clone_callback,concept)
|
||||
if not success then
|
||||
error('Gui clone handler error with '..concept.name..':\n\t'..rtn)
|
||||
end
|
||||
end
|
||||
|
||||
return concept
|
||||
end
|
||||
|
||||
--[[-- Use to add your own callbacks to the clone function, for example adding to a local table
|
||||
@tparam function clone_callback the function which is called with the concept to have something done to it
|
||||
@treturn table self to allow chaining
|
||||
@usage-- Adding concept to a local table
|
||||
local buttons = {}
|
||||
local button =
|
||||
Gui.get_concept('Button')
|
||||
:define_clone(function(concept)
|
||||
buttons[concept.name] = concept
|
||||
end)
|
||||
]]
|
||||
function Prototype:define_clone(clone_callback)
|
||||
-- Check that it is a function that is being added
|
||||
if type(clone_callback) ~= 'function' then
|
||||
error('Draw define must be a function',2)
|
||||
end
|
||||
|
||||
-- Add the draw function
|
||||
self.clone_callbacks[#self.clone_callbacks+1] = clone_callback
|
||||
|
||||
return self
|
||||
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
|
||||
@@ -280,7 +312,7 @@ function Prototype:raise_event(event_name,event,from_factorio)
|
||||
for _,handler in ipairs(handlers) do
|
||||
local success, err = pcall(handler,event)
|
||||
if not success then
|
||||
error('Gui event handler error with '..self.name..'/'..event_name..': '..err)
|
||||
error('Gui event handler error with '..self.name..'/'..event_name..':\n\t'..err)
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -335,7 +367,7 @@ Gui.get_concept('CustomButton')
|
||||
-- Call the setter method to update values if present
|
||||
local success, err = pcall(setter_callback,concept.properties,value,...)
|
||||
if not success then
|
||||
error('Gui property handler error with '..concept.name..'/'..property_name..': '..err)
|
||||
error('Gui property handler error with '..concept.name..'/'..property_name..':\n\t'..err)
|
||||
end
|
||||
else
|
||||
-- Otherwise just update the key
|
||||
@@ -413,7 +445,7 @@ function Prototype:draw(parent_element,...)
|
||||
if success and rtn then
|
||||
element = rtn
|
||||
elseif not success then
|
||||
error('Gui draw handler error with '..self.name..': '..rtn)
|
||||
error('Gui draw handler error with '..self.name..':\n\t'..rtn)
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
@@ -8,7 +8,9 @@
|
||||
-- @section tests
|
||||
|
||||
local Gui = require 'expcore.gui'
|
||||
local Game = require 'utils.game' -- @dep utils.game
|
||||
local Game = require 'utils.game'
|
||||
local Event = require 'utils.event'
|
||||
require 'expcore.toolbar'
|
||||
|
||||
local test_prefix = '__GUI_TEST_'
|
||||
local tests = {}
|
||||
@@ -32,6 +34,59 @@ Gui.clone_concept('frame',TEST 'test_frame')
|
||||
end
|
||||
end)
|
||||
|
||||
Gui.clone_concept('toolbar-button',TEST 'run_test_button')
|
||||
:set_permission_alias('gui-test')
|
||||
:set_caption('Element Tests')
|
||||
:on_click(function(event)
|
||||
local player = event.player
|
||||
if not Gui.destroy(player.gui.center[test_frame.name]) then
|
||||
Gui.run_tests(event.player)
|
||||
end
|
||||
end)
|
||||
|
||||
local test_left_frame =
|
||||
Gui.clone_concept('toolbar-frame',TEST 'player_list')
|
||||
:set_permission_alias('gui-test')
|
||||
:set_caption('Frame Test Left')
|
||||
:define_draw(function(properties,parent,element)
|
||||
local list_area =
|
||||
element.add{
|
||||
name = 'scroll',
|
||||
type = 'scroll-pane',
|
||||
direction = 'vertical',
|
||||
horizontal_scroll_policy = 'never',
|
||||
vertical_scroll_policy = 'auto-and-reserve-space'
|
||||
}
|
||||
Gui.set_padding(list_area,1,1,2,2)
|
||||
list_area.style.horizontally_stretchable = true
|
||||
list_area.style.maximal_height = 200
|
||||
|
||||
for _,player in pairs(game.connected_players) do
|
||||
list_area.add{
|
||||
type='label',
|
||||
caption=player.name
|
||||
}
|
||||
end
|
||||
end)
|
||||
:on_update(function(event)
|
||||
local list_area = event.element.scroll
|
||||
list_area.clear()
|
||||
|
||||
for _,player in pairs(game.connected_players) do
|
||||
list_area.add{
|
||||
type='label',
|
||||
caption=player.name
|
||||
}
|
||||
end
|
||||
end)
|
||||
|
||||
Event.add(defines.events.on_player_joined_game,function(event)
|
||||
test_left_frame:update_all(event)
|
||||
end)
|
||||
Event.add(defines.events.on_player_left_game,function(event)
|
||||
test_left_frame:update_all(event)
|
||||
end)
|
||||
|
||||
--[[-- 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
|
||||
|
||||
Reference in New Issue
Block a user