| 1 |
extends Panel |
| 2 |
|
| 3 |
onready var _script_list = $ScriptsList |
| 4 |
onready var _nav = { |
| 5 |
prev = $Navigation/Previous, |
| 6 |
next = $Navigation/Next, |
| 7 |
run = $Navigation/Run, |
| 8 |
current_script = $Navigation/CurrentScript, |
| 9 |
run_single = $Navigation/RunSingleScript |
| 10 |
} |
| 11 |
onready var _progress = { |
| 12 |
script = $ScriptProgress, |
| 13 |
script_xy = $ScriptProgress/xy, |
| 14 |
test = $TestProgress, |
| 15 |
test_xy = $TestProgress/xy |
| 16 |
} |
| 17 |
onready var _summary = { |
| 18 |
failing = $Summary/Failing, |
| 19 |
passing = $Summary/Passing, |
| 20 |
fail_count = 0, |
| 21 |
pass_count = 0 |
| 22 |
} |
| 23 |
|
| 24 |
onready var _extras = $ExtraOptions |
| 25 |
onready var _ignore_pauses = $ExtraOptions/IgnorePause |
| 26 |
onready var _continue_button = $Continue/Continue |
| 27 |
onready var _text_box = $TextDisplay/RichTextLabel |
| 28 |
|
| 29 |
onready var _titlebar = { |
| 30 |
bar = $TitleBar, |
| 31 |
time = $TitleBar/Time, |
| 32 |
label = $TitleBar/Title |
| 33 |
} |
| 34 |
|
| 35 |
onready var _user_files = $UserFileViewer |
| 36 |
|
| 37 |
var _mouse = { |
| 38 |
down = false, |
| 39 |
in_title = false, |
| 40 |
down_pos = null, |
| 41 |
in_handle = false |
| 42 |
} |
| 43 |
var _is_running = false |
| 44 |
var _start_time = 0.0 |
| 45 |
var _time = 0.0 |
| 46 |
|
| 47 |
const DEFAULT_TITLE = 'Gut: The Godot Unit Testing tool.' |
| 48 |
var _pre_maximize_rect = null |
| 49 |
var _font_size = 20 |
| 50 |
|
| 51 |
signal end_pause |
| 52 |
signal ignore_pause |
| 53 |
signal log_level_changed |
| 54 |
signal run_script |
| 55 |
signal run_single_script |
| 56 |
|
| 57 |
func _ready(): |
| 58 |
|
| 59 |
if(Engine.editor_hint): |
| 60 |
return |
| 61 |
|
| 62 |
_pre_maximize_rect = get_rect() |
| 63 |
_hide_scripts() |
| 64 |
_update_controls() |
| 65 |
_nav.current_script.set_text("No scripts available") |
| 66 |
set_title() |
| 67 |
clear_summary() |
| 68 |
_titlebar.time.set_text("Time 0.0") |
| 69 |
|
| 70 |
_extras.visible = false |
| 71 |
update() |
| 72 |
|
| 73 |
set_font_size(_font_size) |
| 74 |
set_font('CourierPrime') |
| 75 |
|
| 76 |
_user_files.set_position(Vector2(10, 30)) |
| 77 |
|
| 78 |
func elapsed_time_as_str(): |
| 79 |
return str("%.1f" % (_time / 1000.0), 's') |
| 80 |
|
| 81 |
func _process(_delta): |
| 82 |
if(_is_running): |
| 83 |
_time = OS.get_ticks_msec() - _start_time |
| 84 |
_titlebar.time.set_text(str('Time: ', elapsed_time_as_str())) |
| 85 |
|
| 86 |
func _draw(): # needs get_size() |
| 87 |
# Draw the lines in the corner to show where you can |
| 88 |
# drag to resize the dialog |
| 89 |
var grab_margin = 3 |
| 90 |
var line_space = 3 |
| 91 |
var grab_line_color = Color(.4, .4, .4) |
| 92 |
for i in range(1, 10): |
| 93 |
var x = rect_size - Vector2(i * line_space, grab_margin) |
| 94 |
var y = rect_size - Vector2(grab_margin, i * line_space) |
| 95 |
draw_line(x, y, grab_line_color, 1, true) |
| 96 |
|
| 97 |
func _on_Maximize_draw(): |
| 98 |
# draw the maximize square thing. |
| 99 |
var btn = $TitleBar/Maximize |
| 100 |
btn.set_text('') |
| 101 |
var w = btn.get_size().x |
| 102 |
var h = btn.get_size().y |
| 103 |
btn.draw_rect(Rect2(0, 0, w, h), Color(0, 0, 0, 1)) |
| 104 |
btn.draw_rect(Rect2(2, 4, w - 4, h - 6), Color(1,1,1,1)) |
| 105 |
|
| 106 |
func _on_ShowExtras_draw(): |
| 107 |
var btn = $Continue/ShowExtras |
| 108 |
btn.set_text('') |
| 109 |
var start_x = 20 |
| 110 |
var start_y = 15 |
| 111 |
var pad = 5 |
| 112 |
var color = Color(.1, .1, .1, 1) |
| 113 |
var width = 2 |
| 114 |
for i in range(3): |
| 115 |
var y = start_y + pad * i |
| 116 |
btn.draw_line(Vector2(start_x, y), Vector2(btn.get_size().x - start_x, y), color, width, true) |
| 117 |
|
| 118 |
# #################### |
| 119 |
# GUI Events |
| 120 |
# #################### |
| 121 |
func _on_Run_pressed(): |
| 122 |
_run_mode() |
| 123 |
emit_signal('run_script', get_selected_index()) |
| 124 |
|
| 125 |
func _on_CurrentScript_pressed(): |
| 126 |
_toggle_scripts() |
| 127 |
|
| 128 |
func _on_Previous_pressed(): |
| 129 |
_select_script(get_selected_index() - 1) |
| 130 |
|
| 131 |
func _on_Next_pressed(): |
| 132 |
_select_script(get_selected_index() + 1) |
| 133 |
|
| 134 |
func _on_LogLevelSlider_value_changed(_value): |
| 135 |
emit_signal('log_level_changed', $LogLevelSlider.value) |
| 136 |
|
| 137 |
func _on_Continue_pressed(): |
| 138 |
_continue_button.disabled = true |
| 139 |
emit_signal('end_pause') |
| 140 |
|
| 141 |
func _on_IgnorePause_pressed(): |
| 142 |
var checked = _ignore_pauses.is_pressed() |
| 143 |
emit_signal('ignore_pause', checked) |
| 144 |
if(checked): |
| 145 |
emit_signal('end_pause') |
| 146 |
_continue_button.disabled = true |
| 147 |
|
| 148 |
func _on_RunSingleScript_pressed(): |
| 149 |
_run_mode() |
| 150 |
emit_signal('run_single_script', get_selected_index()) |
| 151 |
|
| 152 |
func _on_ScriptsList_item_selected(index): |
| 153 |
var tmr = $ScriptsList/DoubleClickTimer |
| 154 |
if(!tmr.is_stopped()): |
| 155 |
_run_mode() |
| 156 |
emit_signal('run_single_script', get_selected_index()) |
| 157 |
tmr.stop() |
| 158 |
else: |
| 159 |
tmr.start() |
| 160 |
|
| 161 |
_select_script(index) |
| 162 |
|
| 163 |
func _on_TitleBar_mouse_entered(): |
| 164 |
_mouse.in_title = true |
| 165 |
|
| 166 |
func _on_TitleBar_mouse_exited(): |
| 167 |
_mouse.in_title = false |
| 168 |
|
| 169 |
func _input(event): |
| 170 |
if(event is InputEventMouseButton): |
| 171 |
if(event.button_index == 1): |
| 172 |
_mouse.down = event.pressed |
| 173 |
if(_mouse.down): |
| 174 |
_mouse.down_pos = event.position |
| 175 |
|
| 176 |
if(_mouse.in_title): |
| 177 |
if(event is InputEventMouseMotion and _mouse.down): |
| 178 |
set_position(get_position() + (event.position - _mouse.down_pos)) |
| 179 |
_mouse.down_pos = event.position |
| 180 |
_pre_maximize_rect = get_rect() |
| 181 |
|
| 182 |
if(_mouse.in_handle): |
| 183 |
if(event is InputEventMouseMotion and _mouse.down): |
| 184 |
var new_size = rect_size + event.position - _mouse.down_pos |
| 185 |
var new_mouse_down_pos = event.position |
| 186 |
rect_size = new_size |
| 187 |
_mouse.down_pos = new_mouse_down_pos |
| 188 |
_pre_maximize_rect = get_rect() |
| 189 |
|
| 190 |
func _on_ResizeHandle_mouse_entered(): |
| 191 |
_mouse.in_handle = true |
| 192 |
|
| 193 |
func _on_ResizeHandle_mouse_exited(): |
| 194 |
_mouse.in_handle = false |
| 195 |
|
| 196 |
func _on_RichTextLabel_gui_input(ev): |
| 197 |
pass |
| 198 |
# leaving this b/c it is wired up and might have to send |
| 199 |
# more signals through |
| 200 |
|
| 201 |
func _on_Copy_pressed(): |
| 202 |
OS.clipboard = _text_box.text |
| 203 |
|
| 204 |
func _on_ShowExtras_toggled(button_pressed): |
| 205 |
_extras.visible = button_pressed |
| 206 |
|
| 207 |
func _on_Maximize_pressed(): |
| 208 |
if(get_rect() == _pre_maximize_rect): |
| 209 |
maximize() |
| 210 |
else: |
| 211 |
rect_size = _pre_maximize_rect.size |
| 212 |
rect_position = _pre_maximize_rect.position |
| 213 |
# #################### |
| 214 |
# Private |
| 215 |
# #################### |
| 216 |
func _run_mode(is_running=true): |
| 217 |
if(is_running): |
| 218 |
_start_time = OS.get_ticks_msec() |
| 219 |
_time = 0.0 |
| 220 |
clear_summary() |
| 221 |
_is_running = is_running |
| 222 |
|
| 223 |
_hide_scripts() |
| 224 |
var ctrls = $Navigation.get_children() |
| 225 |
for i in range(ctrls.size()): |
| 226 |
ctrls[i].disabled = is_running |
| 227 |
|
| 228 |
func _select_script(index): |
| 229 |
var text = _script_list.get_item_text(index) |
| 230 |
var max_len = 50 |
| 231 |
if(text.length() > max_len): |
| 232 |
text = '...' + text.right(text.length() - (max_len - 5)) |
| 233 |
$Navigation/CurrentScript.set_text(text) |
| 234 |
_script_list.select(index) |
| 235 |
_update_controls() |
| 236 |
|
| 237 |
func _toggle_scripts(): |
| 238 |
if(_script_list.visible): |
| 239 |
_hide_scripts() |
| 240 |
else: |
| 241 |
_show_scripts() |
| 242 |
|
| 243 |
func _show_scripts(): |
| 244 |
_script_list.show() |
| 245 |
|
| 246 |
func _hide_scripts(): |
| 247 |
_script_list.hide() |
| 248 |
|
| 249 |
func _update_controls(): |
| 250 |
var is_empty = _script_list.get_selected_items().size() == 0 |
| 251 |
if(is_empty): |
| 252 |
_nav.next.disabled = true |
| 253 |
_nav.prev.disabled = true |
| 254 |
else: |
| 255 |
var index = get_selected_index() |
| 256 |
_nav.prev.disabled = index <= 0 |
| 257 |
_nav.next.disabled = index >= _script_list.get_item_count() - 1 |
| 258 |
|
| 259 |
_nav.run.disabled = is_empty |
| 260 |
_nav.current_script.disabled = is_empty |
| 261 |
_nav.run_single.disabled = is_empty |
| 262 |
|
| 263 |
func _update_summary(): |
| 264 |
if(!_summary): |
| 265 |
return |
| 266 |
|
| 267 |
var total = _summary.fail_count + _summary.pass_count |
| 268 |
$Summary.visible = !total == 0 |
| 269 |
$Summary/AssertCount.text = str('Failures ', _summary.fail_count, '/', total) |
| 270 |
# #################### |
| 271 |
# Public |
| 272 |
# #################### |
| 273 |
func run_mode(is_running=true): |
| 274 |
_run_mode(is_running) |
| 275 |
|
| 276 |
func set_scripts(scripts): |
| 277 |
_script_list.clear() |
| 278 |
for i in range(scripts.size()): |
| 279 |
_script_list.add_item(scripts[i]) |
| 280 |
_select_script(0) |
| 281 |
_update_controls() |
| 282 |
|
| 283 |
func select_script(index): |
| 284 |
_select_script(index) |
| 285 |
|
| 286 |
func get_selected_index(): |
| 287 |
return _script_list.get_selected_items()[0] |
| 288 |
|
| 289 |
func get_log_level(): |
| 290 |
return $LogLevelSlider.value |
| 291 |
|
| 292 |
func set_log_level(value): |
| 293 |
var new_value = value |
| 294 |
if(new_value == null): |
| 295 |
new_value = 0 |
| 296 |
$LogLevelSlider.value = new_value |
| 297 |
|
| 298 |
func set_ignore_pause(should): |
| 299 |
_ignore_pauses.pressed = should |
| 300 |
|
| 301 |
func get_ignore_pause(): |
| 302 |
return _ignore_pauses.pressed |
| 303 |
|
| 304 |
func get_text_box(): |
| 305 |
# due to some timing issue, this cannot return _text_box but can return |
| 306 |
# this. |
| 307 |
return $TextDisplay/RichTextLabel |
| 308 |
|
| 309 |
func end_run(): |
| 310 |
_run_mode(false) |
| 311 |
_update_controls() |
| 312 |
|
| 313 |
func set_progress_script_max(value): |
| 314 |
var max_val = max(value, 1) |
| 315 |
_progress.script.set_max(max_val) |
| 316 |
_progress.script_xy.set_text(str('0/', max_val)) |
| 317 |
|
| 318 |
func set_progress_script_value(value): |
| 319 |
_progress.script.set_value(value) |
| 320 |
var txt = str(value, '/', _progress.test.get_max()) |
| 321 |
_progress.script_xy.set_text(txt) |
| 322 |
|
| 323 |
func set_progress_test_max(value): |
| 324 |
var max_val = max(value, 1) |
| 325 |
_progress.test.set_max(max_val) |
| 326 |
_progress.test_xy.set_text(str('0/', max_val)) |
| 327 |
|
| 328 |
func set_progress_test_value(value): |
| 329 |
_progress.test.set_value(value) |
| 330 |
var txt = str(value, '/', _progress.test.get_max()) |
| 331 |
_progress.test_xy.set_text(txt) |
| 332 |
|
| 333 |
func clear_progress(): |
| 334 |
_progress.test.set_value(0) |
| 335 |
_progress.script.set_value(0) |
| 336 |
|
| 337 |
func pause(): |
| 338 |
_continue_button.disabled = false |
| 339 |
|
| 340 |
func set_title(title=null): |
| 341 |
if(title == null): |
| 342 |
$TitleBar/Title.set_text(DEFAULT_TITLE) |
| 343 |
else: |
| 344 |
$TitleBar/Title.set_text(title) |
| 345 |
|
| 346 |
func add_passing(amount=1): |
| 347 |
if(!_summary): |
| 348 |
return |
| 349 |
_summary.pass_count += amount |
| 350 |
_update_summary() |
| 351 |
|
| 352 |
func add_failing(amount=1): |
| 353 |
if(!_summary): |
| 354 |
return |
| 355 |
_summary.fail_count += amount |
| 356 |
_update_summary() |
| 357 |
|
| 358 |
func clear_summary(): |
| 359 |
_summary.fail_count = 0 |
| 360 |
_summary.pass_count = 0 |
| 361 |
_update_summary() |
| 362 |
|
| 363 |
func maximize(): |
| 364 |
if(is_inside_tree()): |
| 365 |
var vp_size_offset = get_viewport().size |
| 366 |
rect_size = vp_size_offset / get_scale() |
| 367 |
set_position(Vector2(0, 0)) |
| 368 |
|
| 369 |
func clear_text(): |
| 370 |
_text_box.bbcode_text = '' |
| 371 |
|
| 372 |
func scroll_to_bottom(): |
| 373 |
pass |
| 374 |
#_text_box.cursor_set_line(_gui.get_text_box().get_line_count()) |
| 375 |
|
| 376 |
func _set_font_size_for_rtl(rtl, new_size): |
| 377 |
if(rtl.get('custom_fonts/normal_font') != null): |
| 378 |
rtl.get('custom_fonts/bold_italics_font').size = new_size |
| 379 |
rtl.get('custom_fonts/bold_font').size = new_size |
| 380 |
rtl.get('custom_fonts/italics_font').size = new_size |
| 381 |
rtl.get('custom_fonts/normal_font').size = new_size |
| 382 |
|
| 383 |
|
| 384 |
func _set_fonts_for_rtl(rtl, base_font_name): |
| 385 |
pass |
| 386 |
|
| 387 |
|
| 388 |
func set_font_size(new_size): |
| 389 |
_font_size = new_size |
| 390 |
_set_font_size_for_rtl(_text_box, new_size) |
| 391 |
_set_font_size_for_rtl(_user_files.get_rich_text_label(), new_size) |
| 392 |
|
| 393 |
|
| 394 |
func _set_font(rtl, font_name, custom_name): |
| 395 |
if(font_name == null): |
| 396 |
rtl.set('custom_fonts/' + custom_name, null) |
| 397 |
else: |
| 398 |
var dyn_font = DynamicFont.new() |
| 399 |
var font_data = DynamicFontData.new() |
| 400 |
font_data.font_path = 'res://addons/gut/fonts/' + font_name + '.ttf' |
| 401 |
font_data.antialiased = true |
| 402 |
dyn_font.font_data = font_data |
| 403 |
rtl.set('custom_fonts/' + custom_name, dyn_font) |
| 404 |
|
| 405 |
func _set_all_fonts_in_ftl(ftl, base_name): |
| 406 |
if(base_name == 'Default'): |
| 407 |
_set_font(ftl, null, 'normal_font') |
| 408 |
_set_font(ftl, null, 'bold_font') |
| 409 |
_set_font(ftl, null, 'italics_font') |
| 410 |
_set_font(ftl, null, 'bold_italics_font') |
| 411 |
else: |
| 412 |
_set_font(ftl, base_name + '-Regular', 'normal_font') |
| 413 |
_set_font(ftl, base_name + '-Bold', 'bold_font') |
| 414 |
_set_font(ftl, base_name + '-Italic', 'italics_font') |
| 415 |
_set_font(ftl, base_name + '-BoldItalic', 'bold_italics_font') |
| 416 |
set_font_size(_font_size) |
| 417 |
|
| 418 |
func set_font(base_name): |
| 419 |
_set_all_fonts_in_ftl(_text_box, base_name) |
| 420 |
_set_all_fonts_in_ftl(_user_files.get_rich_text_label(), base_name) |
| 421 |
|
| 422 |
func set_default_font_color(color): |
| 423 |
_text_box.set('custom_colors/default_color', color) |
| 424 |
|
| 425 |
func set_background_color(color): |
| 426 |
$TextDisplay.color = color |
| 427 |
|
| 428 |
func _on_UserFiles_pressed(): |
| 429 |
_user_files.show_open() |
| 430 |
|
| 431 |
func get_waiting_label(): |
| 432 |
return $TextDisplay/WaitingLabel |