Added Text input and ElemButtons Tests all finished

This commit is contained in:
Cooldude2606
2019-05-16 20:18:39 +01:00
parent b7dd534f59
commit fbc07dab59
10 changed files with 640 additions and 145 deletions

View File

@@ -1,8 +1,10 @@
--- This file creates a teste gui that is used to test every input method
-- note that this does not cover every permutation only features in indepentance
-- for example store in most cases is just by player name, but other store methods are tested with checkbox
local Gui = require 'expcore.gui'
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')
local tests = {}
@@ -11,6 +13,14 @@ local function categozie_by_player(element)
return player.name
end
--[[
Toolbar Tests
> No display - Toolbar button with no display
> With caption - Toolbar button with a caption display
> With icons - Toolbar button with an icon
> Main test gui - Main test gui triggers all other tests
]]
Gui.new_toolbar_button('click-1')
:set_post_authenticator(function(player,button_name)
return global.click_one
@@ -44,246 +54,507 @@ Gui.new_toolbar_button('gui-test-open')
end)
:on_click(function(player,_element,event)
if player.gui.center.TestGui then player.gui.center.TestGui.destroy() return end
local frame = player.gui.center.add{type='frame',caption='Gui Test',name='TestGui'}
frame = frame.add{type='table',column_count=5}
for key,element in pairs(tests) do
local test_function = type(element) == 'function' and element or element.draw_to
local success,err = pcall(test_function,element,frame)
if success then
player.print('Drawing: '..key..format_chat_colour(' SUCCESS',Colors.green))
else
player.print('Drawing: '..key..format_chat_colour(' FAIL',Colors.red)..' '..clean_stack_trace(err))
local frame = player.gui.center.add{
type='frame',
caption='Gui Test',
name='TestGui'
}
for test_group_name,test_group in pairs(tests) do
player.print('Starting tests for: '..format_chat_colour(test_group_name,Colors.cyan))
local pass_count = 0
local test_count = 0
local flow = frame.add{
type='flow',
name=test_group_name,
direction='vertical'
}
for test_name,test in pairs(test_group) do
local test_function = type(test) == 'function' and test or test.draw_to
test_count = test_count+1
local success,err = pcall(test_function,test,flow)
if success then
pass_count = pass_count+1
else
player.print('Failed Test: '..format_chat_colour(test_name,Colors.red))
log('Gui Test Failed: '..test_name..' stacktrace:\n'..err)
end
end
if pass_count == test_count then
player.print('All tests '..format_chat_colour('passed',Colors.green)..' ('..test_group_name..')')
else
player.print('Passed '..format_chat_colour(pass_count..'/'..test_count,Colors.cyan)..' ('..test_group_name..')')
end
end
end)
tests['Button no display'] = Gui.new_button('test button no display')
--[[
Button Tests
> No display - Simple button which has no display
> Caption - Simple button but has a caption on it
> Icons - Button with an icon display plus two icons for hover and select
> Auth - Button which can only be passed when auth is true (press no display to toggle; needs reopen)
]]
local button_no_display =
Gui.new_button('test-button-no-display')
:set_tooltip('Button no display')
:on_click(function(player,element,event)
player.print('Button no display')
global.test_auth_button = not global.test_auth_button
player.print('Auth Button auth state: '..tostring(global.test_auth_button))
end)
tests['Button caption'] = Gui.new_button('test button caption')
local button_with_caption =
Gui.new_button('test-button-with-caption')
:set_tooltip('Button with caption')
:set_caption('Button Caption')
:on_click(function(player,element,event)
player.print('Button caption')
player.print('Button with caption')
end)
tests['Button icon'] = Gui.new_button('test button icon')
local button_with_icon =
Gui.new_button('test-button-with-icon')
:set_tooltip('Button with icons')
:set_sprites('utility/warning_icon','utility/warning','utility/warning_white')
:on_click(function(player,element,event)
player.print('Button icon')
player.print('Button with icons')
end)
tests['Button auth'] = Gui.new_button('test button auth')
local button_with_auth =
Gui.new_button('test-button-with-auth')
:set_tooltip('Button with auth')
:set_post_authenticator(function(player,button_name)
return global.test_auth_button
end)
:on_click(function(player,element,event)
player.print('Button auth')
player.print('Button with auth')
end)
tests['Checkbox local'] = Gui.new_checkbox('test checkbox local')
tests.Buttons = {
['No display']=button_no_display,
['Caption']=button_with_caption,
['Icons']=button_with_icon,
['Auth']=button_with_auth
}
--[[
Checkbox Test
> Local -- Simple checkbox that can toggle
> Game store -- Checkbox which syncs its state between all players
> Force store -- Checkbox which syncs its state with all players on the same force
> Player store -- Checkbox that stores its state between re-draws
]]
local checkbox_local =
Gui.new_checkbox('test-checkbox-local')
:set_tooltip('Checkbox local')
:set_caption('Checkbox Local')
:on_state_change(function(player,element,state)
:on_change(function(player,element,state)
player.print('Checkbox local: '..tostring(state))
end)
tests['Checkbox store game'] = Gui.new_checkbox('test checkbox store game')
local checkbox_game =
Gui.new_checkbox('test-checkbox-store-game')
:set_tooltip('Checkbox store game')
:set_caption('Checkbox Store Game')
:add_store()
:on_state_change(function(player,element,state)
:on_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,state)
player.print('Checkbox store player: '..tostring(state))
end)
tests['Checkbox store force'] = Gui.new_checkbox('test checkbox store force')
local checkbox_force =
Gui.new_checkbox('test-checkbox-store-force')
:set_tooltip('Checkboc store force')
:set_caption('Checkbox Store Force')
:add_store(function(element)
local player = Game.get_player_by_index(element.player_index)
return player.force.name
end)
:on_state_change(function(player,element,state)
:on_change(function(player,element,state)
player.print('Checkbox store force: '..tostring(state))
end)
tests['Radiobutton local'] = Gui.new_radiobutton('test radiobutton local')
local checkbox_player =
Gui.new_checkbox('test-checkbox-store-player')
:set_tooltip('Checkbox store player')
:set_caption('Checkbox Store Player')
:add_store(categozie_by_player)
:on_change(function(player,element,state)
player.print('Checkbox store player: '..tostring(state))
end)
tests.Checkboxs = {
['Local']=checkbox_local,
['Game store']=checkbox_game,
['Force store']=checkbox_force,
['Player store']=checkbox_player
}
--[[
Radiobutton Tests
> Local -- Simple radiobutton that can only be toggled true
> Player store -- Radio button that saves its state between re-draws
> Option set -- A set of radio buttons where only one can be true at a time
]]
local radiobutton_local =
Gui.new_radiobutton('test-radiobutton-local')
:set_tooltip('Radiobutton local')
:set_caption('Radiobutton Local')
:on_state_change(function(player,element,state)
:on_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')
local radiobutton_player =
Gui.new_radiobutton('test-radiobutton-store')
:set_tooltip('Radiobutton store')
:set_caption('Radiobutton Store')
:add_store(categozie_by_player)
:on_state_change(function(player,element,state)
player.print('Radiobutton store player: '..tostring(state))
:on_change(function(player,element,state)
player.print('Radiobutton store: '..tostring(state))
end)
local test_option_set = Gui.new_radiobutton_option_set('gui.test.share',function(value,category)
local radiobutton_option_set =
Gui.new_radiobutton_option_set('gui.test.share',function(value,category)
game.print('Radiobutton option set for: '..category..' is now: '..tostring(value))
end,categozie_by_player)
tests['Radiobutton option one'] = Gui.new_radiobutton('test radiobutton option one')
local radiobutton_option_one =
Gui.new_radiobutton('test-radiobutton-option-one')
:set_tooltip('Radiobutton option set')
:set_caption('Radiobutton Option One')
:add_as_option(test_option_set,'One')
:on_state_change(function(player,element,state)
:add_as_option(radiobutton_option_set,'One')
:on_change(function(player,element,state)
player.print('Radiobutton option one: '..tostring(state))
end)
tests['Radiobutton option two'] = Gui.new_radiobutton('test radiobutton option two')
local radiobutton_option_two =
Gui.new_radiobutton('test-radiobutton-option-two')
:set_tooltip('Radiobutton option set')
:set_caption('Radiobutton Option Two')
:add_as_option(test_option_set,'Two')
:on_state_change(function(player,element,state)
:add_as_option(radiobutton_option_set,'Two')
:on_change(function(player,element,state)
player.print('Radiobutton option two: '..tostring(state))
end)
tests['Radiobutton option three'] = Gui.new_radiobutton('test radiobutton option three')
local radiobutton_option_three =
Gui.new_radiobutton('test-radiobutton-option-three')
:set_tooltip('Radiobutton option set')
:set_caption('Radiobutton Option Three')
:add_as_option(test_option_set,'Three')
:on_state_change(function(player,element,state)
:add_as_option(radiobutton_option_set,'Three')
:on_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')
tests.Radiobuttons = {
['Local']=radiobutton_local,
['Player store']=radiobutton_player,
['Option set']=function(self,frame)
Gui.draw_option_set(radiobutton_option_set,frame)
end
}
--[[
Dropdown Test
> Local static general -- Simple dropdown with all static options and general handler
> Player startic general -- Dropdown with all static options and general handler and stores option between re-draws
> Local static case -- Dropdown with all static options but case handlers and a general handler
> Player static case -- Dropdown with all static options but case handlers and a general handler and stores option between re-draws
> Local dynamic -- Dropdown with one static option with the reset generated by a function
> Player dynamic -- Dropdown with one static option with the reset generated by a function and stores option between re-draws
]]
local 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)
:on_change(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')
local dropdown_player_static_general =
Gui.new_dropdown('test-dropdown-store-static-general')
:set_tooltip('Dropdown store 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))
:on_change(function(player,element,value)
player.print('Dropdown store 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')
local 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)
:on_change(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))
player.print('Dropdown store 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')
local dropdown_player_static_case =
Gui.new_dropdown('test-dropdown-store-static-case')
:set_tooltip('Dropdown store 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))
:on_change(function(player,element,value)
player.print('Dropdown store 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')
local dropdown_local_dynamic =
Gui.new_dropdown('test-dropdown-local-dynamic')
:set_tooltip('Dropdown local dynamic')
: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))
:on_change(function(player,element,value)
player.print('Dropdown local dynamic: '..tostring(value))
end)
tests['Dropdown player dynamic general'] = Gui.new_dropdown('test dropdown player dynamic general')
:set_tooltip('Dropdown Player Dynamic General')
local dropdown_player_dynamic =
Gui.new_dropdown('test-dropdown-store-dynamic')
:set_tooltip('Dropdown store dynamic')
: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))
:on_change(function(player,element,value)
player.print('Dropdown store dynamic: '..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')
tests.Dropdowns = {
['Local static general']=dropdown_local_static_general,
['Player startic general']=dropdown_player_static_general,
['Local static case']=dropdown_local_static_case,
['Player static case']=dropdown_player_static_case,
['Local dynamic general']=dropdown_local_dynamic,
['Player dynamic general']=dropdown_player_dynamic
}
--[[
List Box Tests
> Local -- A list box with all static options and general handler
> Store -- A list box with all static options and general handler and stores options between re-draws
]]
local list_box_local =
Gui.new_list_box('test-list-box-local')
:set_tooltip('List box local')
:add_options('One','Two','Three','Four')
:on_selection(function(player,element,value)
player.print('Dropdown local static general: '..tostring(value))
:on_change(function(player,element,value)
player.print('Dropdown local: '..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')
local list_box_player =
Gui.new_list_box('test-list-box-store')
:set_tooltip('List box store')
: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))
:on_change(function(player,element,value)
player.print('Dropdown store: '..tostring(value))
end)
tests['Slider local default'] = Gui.new_slider('test slider local default')
:set_tooltip('Silder Local Default')
tests["List Boxs"] = {
['Local']=list_box_local,
['Player']=list_box_player
}
--[[
Slider Tests
> Local default -- Simple slider with default range
> Store default -- Slider with default range that stores value between re-draws
> Static range -- Simple slider with a static range
> Dynamic range -- Slider with a dynamic range
> Local label -- Simple slider with default range which has a label
> Store label -- Slider with default range which has a label and stores value between re-draws
]]
local slider_local_default =
Gui.new_slider('test-slider-local-default')
:set_tooltip('Silder local default')
:on_change(function(player,element,value,percent)
player.print('Slider local default: '..tostring(math.round(value))..' '..tostring(math.round(percent,2)))
player.print('Slider local default: '..tostring(math.round(value))..' '..tostring(math.round(percent,1)))
end)
tests['Slider player default'] = Gui.new_slider('test slider player default')
:set_tooltip('Silder Player Default')
local slider_player_default =
Gui.new_slider('test-slider-store-default')
:set_tooltip('Silder store default')
:add_store(categozie_by_player)
:on_change(function(player,element,value,percent)
player.print('Slider player default: '..tostring(math.round(value))..' '..tostring(math.round(percent,2)))
player.print('Slider store default: '..tostring(math.round(value))..' '..tostring(math.round(percent,1)))
end)
tests['Slider static range'] = Gui.new_slider('test slider static range')
:set_tooltip('Silder Static Range')
local slider_static =
Gui.new_slider('test-slider-static-range')
:set_tooltip('Silder static range')
:set_range(5,50)
:on_change(function(player,element,value,percent)
player.print('Slider static range: '..tostring(math.round(value))..' '..tostring(math.round(percent,2)))
player.print('Slider static range: '..tostring(math.round(value))..' '..tostring(math.round(percent,1)))
end)
tests['Slider dynamic range'] = Gui.new_slider('test slider dynamic range')
:set_tooltip('Silder Dynamic Range')
local slider_dynamic =
Gui.new_slider('test-slider-dynamic-range')
:set_tooltip('Silder dynamic range')
:set_range(function(player,element)
return player.index - 5
end,function(player,element)
return player.index + 4
end)
:on_change(function(player,element,value,percent)
player.print('Slider static range: '..tostring(math.round(value))..' '..tostring(math.round(percent,2)))
player.print('Slider dynamic range: '..tostring(math.round(value))..' '..tostring(math.round(percent,1)))
end)
local label_slider = Gui.new_slider('test slider local lable')
:set_tooltip('Silder Local label')
local label_slider_local =
Gui.new_slider('test-slider-local-label')
:set_tooltip('Silder local label')
:enable_auto_draw_label()
:on_change(function(player,element,value,percent)
player.print('Slider local label: '..tostring(math.round(value))..' '..tostring(math.round(percent,2)))
player.print('Slider local label: '..tostring(math.round(value))..' '..tostring(math.round(percent,1)))
end)
tests['Slider local label'] = function(self,frame)
local flow = frame.add{type='flow'}
label_slider:draw_to(flow)
end
local label_slider_player = Gui.new_slider('test slider player lable')
:set_tooltip('Silder Player label')
local label_slider_player =
Gui.new_slider('test-slider-store-label')
:set_tooltip('Silder store label')
:enable_auto_draw_label()
:add_store(categozie_by_player)
:on_change(function(player,element,value,percent)
player.print('Slider player label: '..tostring(math.round(value))..' '..tostring(math.round(percent,2)))
player.print('Slider store label: '..tostring(math.round(value))..' '..tostring(math.round(percent,1)))
end)
tests['Slider player label'] = function(self,frame)
local flow = frame.add{type='flow'}
label_slider_player:draw_to(flow)
end
tests.Sliders = {
['Local default']=slider_local_default,
['Player default']=slider_player_default,
['Static range']=slider_static,
['Dynamic range']=slider_dynamic,
['Local label']=function(self,frame)
local flow = frame.add{type='flow'}
label_slider_local:draw_to(flow)
end,
['Player label']=function(self,frame)
local flow = frame.add{type='flow'}
label_slider_player:draw_to(flow)
end
}
--[[
Text Tests
> Local field -- Simple text field
> Store field -- Test field that stores text between re-draws
> Local box -- Simple text box
> Wrap box -- Text box which has word wrap and selection disabled
]]
local text_filed_local =
Gui.new_text_filed('test-text-field-local')
:set_tooltip('Text field local')
:on_change(function(player,element,value)
player.print('Text field local: '..value)
end)
local text_filed_store =
Gui.new_text_filed('test-text-field-store')
:set_tooltip('Text field store')
:add_store(categozie_by_player)
:on_change(function(player,element,value)
player.print('Text field store: '..value)
end)
local text_box_local =
Gui.new_text_box('test-text-box-local')
:set_tooltip('Text box local')
:on_change(function(player,element,value)
player.print('Text box local: '..value)
end)
local text_box_wrap =
Gui.new_text_box('test-text-box-wrap')
:set_tooltip('Text box wrap')
:set_selectable(false)
:set_word_wrap()
:on_change(function(player,element,value)
player.print('Text box wrap: '..value)
end)
tests.Texts = {
['Local field']=text_filed_local,
['Store field']=text_filed_store,
['Local box']=text_box_local,
['Wrap box']=text_box_wrap
}
--[[
Elem Button Tests
> Local -- Simple elem button
> Default -- Simple elem button which has a default value
> Function -- Elem button which has a dynamic default
> Store -- Elem button which stores its value between re-draws
]]
local elem_local =
Gui.new_elem_button('test-elem-local')
:set_tooltip('Elem')
:set_type('item')
:on_change(function(player,element,value)
player.print('Elem: '..value)
end)
local elem_default =
Gui.new_elem_button('test-elem-default')
:set_tooltip('Elem default')
:set_type('item')
:set_default('iron-plate')
:on_change(function(player,element,value)
player.print('Elem default: '..value)
end)
local elem_function =
Gui.new_elem_button('test-elem-function')
:set_tooltip('Elem function')
:set_type('item')
:set_default(function(player,element)
return 'iron-plate'
end)
:on_change(function(player,element,value)
player.print('Elem function: '..value)
end)
local elem_store =
Gui.new_elem_button('test-elem-store')
:set_tooltip('Elem store')
:set_type('item')
:add_store(categozie_by_player)
:on_change(function(player,element,value)
player.print('Elem store: '..value)
end)
tests["Elem Buttons"] = {
['Local']=elem_local,
['Default']=elem_default,
['Function']=elem_function,
['Store']=elem_store
}