mirror of
https://github.com/PHIDIAS0303/ExpCluster.git
synced 2025-12-28 12:05:21 +09:00
Button docs
This commit is contained in:
@@ -3,15 +3,40 @@
|
||||
@alias Gui
|
||||
]]
|
||||
|
||||
local Gui = require 'expcore.gui'
|
||||
--- Tests.
|
||||
-- functions used to test
|
||||
-- @section tests
|
||||
|
||||
local Gui = require 'expcore.gui'
|
||||
local Game = require 'utils.game' -- @dep utils.game
|
||||
|
||||
local test_prefix = '__GUI_TEST_'
|
||||
local tests = {}
|
||||
|
||||
local function TEST(str) return test_prefix..str end
|
||||
|
||||
--[[
|
||||
The main test frame
|
||||
]]
|
||||
|
||||
local test_frame =
|
||||
Gui.clone_concept('frame',TEST 'test_frame')
|
||||
:set_title('Gui Tests')
|
||||
:define_draw(function(properties,parent,element)
|
||||
for category, _ in pairs(tests) do
|
||||
element.add{
|
||||
type = 'flow',
|
||||
name = category,
|
||||
direction = 'vertical'
|
||||
}
|
||||
end
|
||||
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
|
||||
@usage-- Run all gui tests
|
||||
Gui.run_tests(Gui.test_string_return(game.print))
|
||||
Gui.run_tests(game.player)
|
||||
]]
|
||||
function Gui.run_tests(player,category)
|
||||
local results = {
|
||||
@@ -42,18 +67,20 @@ function Gui.run_tests(player,category)
|
||||
return results
|
||||
end
|
||||
|
||||
local frame = player.gui.center[test_frame.name] or test_frame:draw(player.gui.center)
|
||||
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)
|
||||
for test_name, concept in pairs(cat_tests) do
|
||||
local success, err = pcall(concept.draw,concept,frame[category])
|
||||
|
||||
if success then
|
||||
results.passed = results.passed + 1
|
||||
else
|
||||
results.erorrs[test_name] = err
|
||||
results.errors[test_name] = err
|
||||
results.failed = results.failed + 1
|
||||
output(string.format('Test "%s / %s" failed:\n%s',category,test_name,err))
|
||||
end
|
||||
|
||||
@@ -65,29 +92,51 @@ function Gui.run_tests(player,category)
|
||||
end
|
||||
|
||||
--[[
|
||||
Basic frame creation
|
||||
Buttons
|
||||
]]
|
||||
|
||||
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
|
||||
local basic_button =
|
||||
Gui.clone_concept('button',TEST 'basic_button')
|
||||
:set_caption('Basic Button')
|
||||
:set_tooltip('Basic button')
|
||||
:on_click(function(event)
|
||||
event.player.print('You pressed basic button!')
|
||||
end)
|
||||
|
||||
tests.Frame = {
|
||||
['Draw Frame'] = function(player)
|
||||
test_frame:draw(player.gui.center)
|
||||
local sprite_button =
|
||||
Gui.clone_concept('button',TEST 'sprite_button')
|
||||
:set_sprite('utility/warning_icon')
|
||||
:set_tooltip('Sprite button')
|
||||
:on_click(function(event)
|
||||
event.player.print('You pressed sprite button!')
|
||||
end)
|
||||
|
||||
local multi_sprite_button =
|
||||
Gui.clone_concept('button',TEST 'multi_sprite_button')
|
||||
:set_sprite('utility/warning_icon','utility/warning','utility/warning_white')
|
||||
:set_tooltip('Multi-sprite button')
|
||||
:on_click(function(event)
|
||||
event.player.print('You pressed multi sprite button!')
|
||||
end)
|
||||
|
||||
local admin_button =
|
||||
Gui.clone_concept('button',TEST 'admin_button')
|
||||
:set_caption('Admin Button')
|
||||
:set_tooltip('Admin button')
|
||||
:define_draw(function(properties,parent,element)
|
||||
local player = Game.get_player_by_index(element.player_index)
|
||||
if not player.admin then
|
||||
element.enabled = false
|
||||
element.tooltip = 'You must be admin to press this button'
|
||||
end
|
||||
end)
|
||||
:on_click(function(event)
|
||||
event.player.print('You pressed admin button!')
|
||||
end)
|
||||
|
||||
tests.Buttons = {
|
||||
['Basic Button'] = basic_button,
|
||||
['Sprite Button'] = sprite_button,
|
||||
['Multi Sprite Button'] = multi_sprite_button,
|
||||
['Admin Button'] = admin_button,
|
||||
}
|
||||
Reference in New Issue
Block a user