From d6b26eda21de95f728e20c831c5a511d1abcd359 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lera=20Elvo=C3=A9?= Date: Thu, 14 Dec 2023 23:47:42 +0300 Subject: [PATCH] add logger and logger renderer --- classes/deck/deck_holder.gd | 2 + classes/deck/logger.gd | 39 +++++++++ classes/deck/nodes/print.gd | 7 +- graph_node_renderer/deck_holder_renderer.gd | 11 +++ graph_node_renderer/deck_holder_renderer.tscn | 9 ++- graph_node_renderer/logger_renderer.gd | 43 ++++++++++ graph_node_renderer/logger_renderer.tscn | 79 +++++++++++++++++++ project.godot | 5 ++ 8 files changed, 189 insertions(+), 6 deletions(-) create mode 100644 classes/deck/logger.gd create mode 100644 graph_node_renderer/logger_renderer.gd create mode 100644 graph_node_renderer/logger_renderer.tscn diff --git a/classes/deck/deck_holder.gd b/classes/deck/deck_holder.gd index 3d97703..13c7371 100644 --- a/classes/deck/deck_holder.gd +++ b/classes/deck/deck_holder.gd @@ -6,6 +6,8 @@ class_name DeckHolder #static var decks: Array[Deck] static var decks: Dictionary # Dictionary[String -> id, (Deck|Dictionary[String -> instance_id, Deck])] +static var logger := Logger.new() + ## Returns a new empty deck and assigns a new random ID to it. static func add_empty_deck() -> Deck: diff --git a/classes/deck/logger.gd b/classes/deck/logger.gd new file mode 100644 index 0000000..5359237 --- /dev/null +++ b/classes/deck/logger.gd @@ -0,0 +1,39 @@ +class_name Logger + +enum LogType { + INFO, + WARN, + ERROR, +} + +enum LogCategory { + NODE, + DECK, + SYSTEM, + RENDERER, +} + +signal log_message(text: String, type: LogType, category: LogCategory) + + +func log_node(text: Variant, type: LogType = LogType.INFO) -> void: + self.log(str(text), type, LogCategory.NODE) + + +func log_deck(text: Variant, type: LogType = LogType.INFO) -> void: + self.log(str(text), type, LogCategory.DECK) + + +func log_system(text: Variant, type: LogType = LogType.INFO) -> void: + self.log(str(text), type, LogCategory.SYSTEM) + + +func log_renderer(text: Variant, type: LogType = LogType.INFO) -> void: + self.log(str(text), type, LogCategory.RENDERER) + + +func log(text: String, type: LogType, category: LogCategory) -> void: + log_message.emit(text, type, category) + + if OS.has_feature("editor"): + prints(LogType.keys()[type].capitalize(), LogCategory.keys()[category].capitalize(), text) diff --git a/classes/deck/nodes/print.gd b/classes/deck/nodes/print.gd index 922ac7f..cb937e8 100644 --- a/classes/deck/nodes/print.gd +++ b/classes/deck/nodes/print.gd @@ -35,12 +35,13 @@ func _receive(to_input_port: int, data: Variant, extra_data: Array = []) -> void return var data_to_print = resolve_input_port_value(0) - if data_to_print == null: + if data_to_print == null || data_to_print.is_empty(): data_to_print = str(data) times_activated += 1 # var data_to_print = input_ports[0].value_callback.call() - print(data_to_print) - print("extra data: ", extra_data) + #print(data_to_print) + #print("extra data: ", extra_data) + DeckHolder.logger.log_node(data_to_print) send(0, true) diff --git a/graph_node_renderer/deck_holder_renderer.gd b/graph_node_renderer/deck_holder_renderer.gd index 50af7ef..795ca54 100644 --- a/graph_node_renderer/deck_holder_renderer.gd +++ b/graph_node_renderer/deck_holder_renderer.gd @@ -50,6 +50,7 @@ var _deck_to_save: WeakRef @onready var obs_setup_dialog := $OBSWebsocketSetupDialog as OBSWebsocketSetupDialog @onready var twitch_setup_dialog := $Twitch_Setup_Dialog as TwitchSetupDialog +@onready var logger_renderer: LoggerRenderer = %LoggerRenderer func _ready() -> void: @@ -87,6 +88,10 @@ func _ready() -> void: file_popup_menu.set_item_shortcut(FileMenuId.SAVE, save_deck_shortcut) file_popup_menu.set_item_shortcut(FileMenuId.SAVE_AS, save_deck_as_shortcut) file_popup_menu.set_item_shortcut(FileMenuId.CLOSE, close_deck_shortcut) + + DeckHolder.logger.log_renderer("normal test") + DeckHolder.logger.log_renderer("warning test", Logger.LogType.WARN) + DeckHolder.logger.log_renderer("error test", Logger.LogType.ERROR) ## Called when the File button in the [MenuBar] is pressed with the [param id] @@ -393,3 +398,9 @@ func _on_unsaved_changes_dialog_confirmed() -> void: close_tab(i) get_tree().quit() + + +func _unhandled_input(event: InputEvent) -> void: + if event.is_action_pressed("toggle_console"): + logger_renderer.visible = !logger_renderer.visible + accept_event() diff --git a/graph_node_renderer/deck_holder_renderer.tscn b/graph_node_renderer/deck_holder_renderer.tscn index 634da7f..1e4d948 100644 --- a/graph_node_renderer/deck_holder_renderer.tscn +++ b/graph_node_renderer/deck_holder_renderer.tscn @@ -1,9 +1,10 @@ -[gd_scene load_steps=20 format=3 uid="uid://duaah5x0jhkn6"] +[gd_scene load_steps=21 format=3 uid="uid://duaah5x0jhkn6"] [ext_resource type="Script" path="res://graph_node_renderer/deck_holder_renderer.gd" id="1_67g2g"] [ext_resource type="PackedScene" uid="uid://b84f2ngtcm5b8" path="res://graph_node_renderer/tab_container_custom.tscn" id="1_s3ug2"] [ext_resource type="Theme" uid="uid://dqqdqscid2iem" path="res://graph_node_renderer/default_theme.tres" id="1_tgul2"] [ext_resource type="Script" path="res://addons/no-obs-ws/NoOBSWS.gd" id="4_nu72u"] +[ext_resource type="PackedScene" uid="uid://duvh3r740w2p5" path="res://graph_node_renderer/logger_renderer.tscn" id="4_pvexk"] [ext_resource type="Script" path="res://addons/no_twitch/twitch_connection.gd" id="5_3n36q"] [ext_resource type="PackedScene" uid="uid://eioso6jb42jy" path="res://graph_node_renderer/obs_websocket_setup_dialog.tscn" id="5_uo2gj"] [ext_resource type="PackedScene" uid="uid://bq2lxmbnic4lc" path="res://graph_node_renderer/twitch_setup_dialog.tscn" id="7_7rhap"] @@ -85,7 +86,7 @@ theme_override_constants/margin_bottom = 2 [node name="VSplitContainer" type="VSplitContainer" parent="MarginContainer"] layout_mode = 2 -split_offset = 677 +split_offset = 460 [node name="VBoxContainer" type="VBoxContainer" parent="MarginContainer/VSplitContainer"] layout_mode = 2 @@ -136,7 +137,9 @@ item_0/id = 0 unique_name_in_owner = true layout_mode = 2 -[node name="ConsoleContainer" type="PanelContainer" parent="MarginContainer/VSplitContainer"] +[node name="LoggerRenderer" parent="MarginContainer/VSplitContainer" instance=ExtResource("4_pvexk")] +unique_name_in_owner = true +visible = false layout_mode = 2 [node name="FileDialog" type="FileDialog" parent="."] diff --git a/graph_node_renderer/logger_renderer.gd b/graph_node_renderer/logger_renderer.gd new file mode 100644 index 0000000..4a1cb8e --- /dev/null +++ b/graph_node_renderer/logger_renderer.gd @@ -0,0 +1,43 @@ +extends PanelContainer +class_name LoggerRenderer + +@onready var output_label: RichTextLabel = %OutputLabel +@onready var copy_button: Button = %CopyButton +@onready var clear_button: Button = %ClearButton + +const COLORS := { + Logger.LogType.INFO: Color.WHITE, + Logger.LogType.WARN: Color("f4f486"), + Logger.LogType.ERROR: Color("f47c7c"), +} + + +func _ready() -> void: + clear_button.pressed.connect( + func(): + output_label.clear() + output_label.text = "" + output_label.push_context() + ) + DeckHolder.logger.log_message.connect(_on_logger_log_message) + output_label.push_context() + + # the copy button is disabled for now because it doesnt work + copy_button.pressed.connect( + func(): + var c = output_label.get_text() + DisplayServer.clipboard_set(output_label.get_text()) + ) + + +func _on_logger_log_message(text: String, type: Logger.LogType, category: Logger.LogCategory) -> void: + output_label.pop_context() + output_label.push_context() + var category_text: String = Logger.LogCategory.keys()[category].capitalize() + category_text = "(%s)" % category_text + output_label.push_bold() + output_label.add_text("%s: " % category_text) + output_label.pop() + output_label.push_color(COLORS[type]) + output_label.add_text(text) + output_label.newline() diff --git a/graph_node_renderer/logger_renderer.tscn b/graph_node_renderer/logger_renderer.tscn new file mode 100644 index 0000000..e2c2c6e --- /dev/null +++ b/graph_node_renderer/logger_renderer.tscn @@ -0,0 +1,79 @@ +[gd_scene load_steps=4 format=3 uid="uid://duvh3r740w2p5"] + +[ext_resource type="Script" path="res://graph_node_renderer/logger_renderer.gd" id="1_82rlk"] + +[sub_resource type="SystemFont" id="SystemFont_1dh4a"] +font_names = PackedStringArray("Monospace") + +[sub_resource type="SystemFont" id="SystemFont_0uj5d"] +font_names = PackedStringArray("Monospace") +font_weight = 700 + +[node name="LoggerRenderer" type="PanelContainer"] +anchors_preset = -1 +anchor_right = 0.42016 +anchor_bottom = 0.181704 +offset_right = -0.0240021 +offset_bottom = 0.255997 +script = ExtResource("1_82rlk") +metadata/_edit_use_anchors_ = true + +[node name="MarginContainer" type="MarginContainer" parent="."] +layout_mode = 2 +theme_override_constants/margin_left = 3 +theme_override_constants/margin_top = 3 +theme_override_constants/margin_right = 3 +theme_override_constants/margin_bottom = 3 + +[node name="VBoxContainer" type="VBoxContainer" parent="MarginContainer"] +layout_mode = 2 + +[node name="Label" type="Label" parent="MarginContainer/VBoxContainer"] +layout_mode = 2 +text = "Debug Console" + +[node name="HBoxContainer" type="HBoxContainer" parent="MarginContainer/VBoxContainer"] +layout_mode = 2 +size_flags_vertical = 3 + +[node name="PanelContainer" type="PanelContainer" parent="MarginContainer/VBoxContainer/HBoxContainer"] +layout_mode = 2 +size_flags_horizontal = 3 + +[node name="MarginContainer" type="MarginContainer" parent="MarginContainer/VBoxContainer/HBoxContainer/PanelContainer"] +layout_mode = 2 +theme_override_constants/margin_left = 3 +theme_override_constants/margin_top = 3 +theme_override_constants/margin_right = 3 +theme_override_constants/margin_bottom = 3 + +[node name="OutputLabel" type="RichTextLabel" parent="MarginContainer/VBoxContainer/HBoxContainer/PanelContainer/MarginContainer"] +unique_name_in_owner = true +layout_mode = 2 +size_flags_horizontal = 3 +focus_mode = 2 +theme_override_fonts/normal_font = SubResource("SystemFont_1dh4a") +theme_override_fonts/bold_font = SubResource("SystemFont_0uj5d") +theme_override_font_sizes/normal_font_size = 12 +theme_override_font_sizes/bold_font_size = 13 +scroll_following = true +context_menu_enabled = true +threaded = true +selection_enabled = true +deselect_on_focus_loss_enabled = false +drag_and_drop_selection_enabled = false + +[node name="VBoxContainer" type="VBoxContainer" parent="MarginContainer/VBoxContainer/HBoxContainer"] +layout_mode = 2 +alignment = 1 + +[node name="CopyButton" type="Button" parent="MarginContainer/VBoxContainer/HBoxContainer/VBoxContainer"] +unique_name_in_owner = true +visible = false +layout_mode = 2 +text = "Copy" + +[node name="ClearButton" type="Button" parent="MarginContainer/VBoxContainer/HBoxContainer/VBoxContainer"] +unique_name_in_owner = true +layout_mode = 2 +text = "Clear" diff --git a/project.godot b/project.godot index f74f7ac..6b7fb0b 100644 --- a/project.godot +++ b/project.godot @@ -43,6 +43,11 @@ rename_node={ "events": [Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":-1,"window_id":0,"alt_pressed":false,"shift_pressed":false,"ctrl_pressed":false,"meta_pressed":false,"pressed":false,"keycode":4194333,"physical_keycode":0,"key_label":0,"unicode":0,"echo":false,"script":null) ] } +toggle_console={ +"deadzone": 0.5, +"events": [Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":-1,"window_id":0,"alt_pressed":false,"shift_pressed":false,"ctrl_pressed":false,"meta_pressed":false,"pressed":false,"keycode":0,"physical_keycode":78,"key_label":0,"unicode":110,"echo":false,"script":null) +] +} [rendering]