Bugbot/UI/bug_report_form.gd
Jamie Greunbaum 407e3b2a9e - Improved error handling for empty fields.
- Added extra static typing hints.
2024-05-28 13:21:54 -04:00

141 lines
6.5 KiB
GDScript

class_name BugReportForm
extends ColorRect
signal submitted(map_name:String, bug_location:Vector3, bug_rotation:Vector3)
signal cancelled
@onready var __product_name : Label = $Form/VBoxContainerLeft/GridContainer/ProductName as Label
@onready var __version_label : Label = $Form/VBoxContainerLeft/GridContainer/VersionLabel as Label
@onready var __version_button : MenuButton = $Form/VBoxContainerLeft/GridContainer/VersionButton as MenuButton
@onready var __hardware_label : Label = $Form/VBoxContainerLeft/GridContainer/HardwareLabel as Label
@onready var __hardware_button : MenuButton = $Form/VBoxContainerLeft/GridContainer/HardwareButton as MenuButton
@onready var __os_label : Label = $Form/VBoxContainerLeft/GridContainer/OSLabel as Label
@onready var __os_button : MenuButton = $Form/VBoxContainerLeft/GridContainer/OSButton as MenuButton
@onready var __component_label : Label = $Form/VBoxContainerLeft/GridContainer/ComponentLabel as Label
@onready var __component_button : MenuButton = $Form/VBoxContainerLeft/GridContainer/ComponentButton as MenuButton
@onready var __severity_label : Label = $Form/VBoxContainerLeft/GridContainer/SeverityLabel as Label
@onready var __severity_button : MenuButton = $Form/VBoxContainerLeft/GridContainer/SeverityButton as MenuButton
@onready var __summary_label : Label = $Form/VBoxContainerRight/GridContainer/SummaryLabel as Label
@onready var __summary_text : LineEdit = $Form/VBoxContainerRight/GridContainer/SummaryText as LineEdit
@onready var __description_label : Label = $Form/VBoxContainerRight/GridContainer/DescriptionLabel as Label
@onready var __description_text : TextEdit = $Form/VBoxContainerRight/GridContainer/DescriptionText as TextEdit
@onready var __submit_button : Button = $Form/VBoxContainerRight/SubmitButton as Button
var map_name : String
var bug_location : Vector3
var bug_rotation : Vector3
var __label_groups : Array
var __stored_mouse_mode : int
var __server_api : BugbotServerAPI
const __DEFAULT_PRODUCT_NAME : StringName = &"NameOfProductHere"
const __BUTTON_DISABLED_MESSAGE : StringName = &"Not available"
const __ERROR_TEXT_COLOUR : Color = Color(0.75, 0.0, 0.0)
func _enter_tree():
__stored_mouse_mode = Input.mouse_mode
Input.mouse_mode = Input.MOUSE_MODE_VISIBLE
__server_api = BugbotServerAPI._create_new_server_api()
func _exit_tree():
Input.mouse_mode = __stored_mouse_mode
func fill_tags(tag_list:Array):
__label_groups.resize(BugbotServerAPI.BugbotTagArray.MAX)
__label_groups = tag_list
__product_name.text = ProjectSettings.get_setting("application/config/name", __DEFAULT_PRODUCT_NAME)
__fill_item_list(__version_button, BugbotServerAPI.BugbotTagArray.VERSION)
__fill_item_list(__hardware_button, BugbotServerAPI.BugbotTagArray.HARDWARE)
__fill_item_list(__os_button, BugbotServerAPI.BugbotTagArray.OS)
__fill_item_list(__component_button, BugbotServerAPI.BugbotTagArray.COMPONENT)
__fill_item_list(__severity_button, BugbotServerAPI.BugbotTagArray.SEVERITY)
func __fill_item_list(menu_button:MenuButton, label_group:int):
var menu_options : Array = __label_groups[label_group]
if menu_options.is_empty():
menu_button.text = __BUTTON_DISABLED_MESSAGE
menu_button.disabled = true
else:
var menu : PopupMenu = menu_button.get_popup()
for i in menu_options.size():
menu.add_item(menu_options[i]["name"], i)
menu.id_pressed.connect(func(id:int): menu_button.text = menu_options[id]["name"])
func _on_submit_button_pressed() -> void:
__submit_button.disabled = true
var summary_error : bool = false
var summary_text : String = __summary_text.text
if summary_text.is_empty():
printerr("You must fill in a summary.")
__summary_label.add_theme_color_override("font_color", __ERROR_TEXT_COLOUR)
__summary_text.add_theme_color_override("font_color", __ERROR_TEXT_COLOUR)
summary_error = true
else:
__summary_label.remove_theme_color_override("font_color")
__summary_text.remove_theme_color_override("font_color")
var description_error : bool = false
var description_text : String = __description_text.text
if description_text.is_empty():
printerr("You must fill in a description.")
__description_label.add_theme_color_override("font_color", __ERROR_TEXT_COLOUR)
__description_text.add_theme_color_override("font_color", __ERROR_TEXT_COLOUR)
description_error = true
else:
__description_label.remove_theme_color_override("font_color")
__description_text.remove_theme_color_override("font_color")
var labels_to_be_added : Array
var version_error : bool = !__get_label_ids_for_submission_data(labels_to_be_added, BugbotServerAPI.BugbotTagArray.VERSION, __version_label, __version_button)
var hardware_error : bool = !__get_label_ids_for_submission_data(labels_to_be_added, BugbotServerAPI.BugbotTagArray.HARDWARE, __hardware_label, __hardware_button)
var os_error : bool = !__get_label_ids_for_submission_data(labels_to_be_added, BugbotServerAPI.BugbotTagArray.OS, __os_label, __os_button)
var component_error : bool = !__get_label_ids_for_submission_data(labels_to_be_added, BugbotServerAPI.BugbotTagArray.COMPONENT, __component_label, __component_button)
var severity_error : bool = !__get_label_ids_for_submission_data(labels_to_be_added, BugbotServerAPI.BugbotTagArray.SEVERITY, __severity_label, __severity_button)
if summary_error or description_error or version_error or hardware_error or os_error or component_error or severity_error:
__submit_button.disabled = false
return
var bug_report_form_data : Dictionary = {
"title": summary_text,
"body": description_text,
"labels": labels_to_be_added,
}
__server_api._send_form_data(bug_report_form_data, map_name, bug_location, bug_rotation, __submission_response)
func __get_label_ids_for_submission_data(label_array:Array, label_group_id:int, label:Label, button:MenuButton) -> bool:
var labels : Array
for applied_label in __label_groups[label_group_id]:
if applied_label["name"] == __severity_button.text:
labels.append(applied_label["id"])
break
if labels.is_empty() and not (__label_groups[label_group_id] as Array).is_empty():
printerr("You must select a label for \"%s\"" % [label.text])
label.add_theme_color_override("font_color", __ERROR_TEXT_COLOUR)
button.add_theme_color_override("font_color", __ERROR_TEXT_COLOUR)
return false
label_array.append_array(labels)
label.remove_theme_color_override("font_color")
button.remove_theme_color_override("font_color")
return true
func __submission_response(response:Variant) -> void:
submitted.emit(map_name, bug_location, bug_rotation)
queue_free()
func _on_cancel_button_pressed() -> void:
cancelled.emit()
queue_free()