From b156d2722adb37d92486299bf841325fbafd4391 Mon Sep 17 00:00:00 2001 From: Relintai Date: Thu, 25 Aug 2022 15:09:54 +0200 Subject: [PATCH] Page entry editing setup. --- game/Main.tscn | 19 +++- game/addons/web_pages/classes/WebPage.gd | 49 +++++++++ .../post_entries/WebPageEntryTitleText.gd | 2 +- game/addons/web_pages/editor/AddEntryPopup.gd | 13 ++- game/addons/web_pages/editor/PageEditor.gd | 99 ++++++++++++++++++- game/addons/web_pages/editor/PageEditor.tscn | 23 +++-- ...geEntryEditor.gd => WebPageEntryEditor.gd} | 21 +++- ...tryEditor.tscn => WebPageEntryEditor.tscn} | 16 +-- .../WebPageEntryTitleTextEditor.gd | 28 ++++++ .../WebPageEntryTitleTextEditor.tscn | 12 +++ 10 files changed, 257 insertions(+), 25 deletions(-) rename game/addons/web_pages/editor/{PageEntryEditor.gd => WebPageEntryEditor.gd} (62%) rename game/addons/web_pages/editor/{PageEntryEditor.tscn => WebPageEntryEditor.tscn} (83%) create mode 100644 game/addons/web_pages/editor/post_entries/WebPageEntryTitleTextEditor.gd create mode 100644 game/addons/web_pages/editor/post_entries/WebPageEntryTitleTextEditor.tscn diff --git a/game/Main.tscn b/game/Main.tscn index 2cd0cd9..935dbec 100644 --- a/game/Main.tscn +++ b/game/Main.tscn @@ -1,7 +1,23 @@ -[gd_scene load_steps=3 format=2] +[gd_scene load_steps=7 format=2] [ext_resource path="res://Main.gd" type="Script" id=1] [ext_resource path="res://addons/web_pages/classes/WebPage.gd" type="Script" id=2] +[ext_resource path="res://addons/web_pages/classes/post_entries/WebPageEntryTitleText.gd" type="Script" id=3] + +[sub_resource type="Resource" id=1] +script = ExtResource( 3 ) +text = "" +hsize = 1 + +[sub_resource type="Resource" id=2] +script = ExtResource( 3 ) +text = "" +hsize = 1 + +[sub_resource type="Resource" id=3] +script = ExtResource( 3 ) +text = "" +hsize = 1 [node name="WebServer" type="WebServerSimple"] script = ExtResource( 1 ) @@ -15,3 +31,4 @@ data = "Blog" [node name="Asdasd" type="WebNode" parent="WebRoot"] uri_segment = "asda" script = ExtResource( 2 ) +entries = [ SubResource( 1 ), SubResource( 2 ), SubResource( 3 ) ] diff --git a/game/addons/web_pages/classes/WebPage.gd b/game/addons/web_pages/classes/WebPage.gd index 1303f5e..a3b48f1 100644 --- a/game/addons/web_pages/classes/WebPage.gd +++ b/game/addons/web_pages/classes/WebPage.gd @@ -3,3 +3,52 @@ extends WebNode class_name WebPage, "res://addons/web_pages/icons/icon_web_page.svg" export(Array, Resource) var entries : Array + +signal entries_changed() + +func add_entry(var entry : WebPageEntry, var after : WebPageEntry = null) -> void: + if after != null: + for i in range(entries.size()): + if entries[i] == after: + entries.insert(i + 1, entry) + break + else: + entries.push_back(entry) + + emit_signal("entries_changed") + +func remove_entry(var entry : WebPageEntry) -> void: + entries.erase(entry) + + emit_signal("entries_changed") + +func move_entry_up(var entry : WebPageEntry) -> void: + # Skips checking the first entry (1, entries.size()) + for i in range(1, entries.size()): + if entries[i] == entry: + entries[i] = entries[i - 1] + entries[i - 1] = entry + emit_signal("entries_changed") + return + +func move_entry_down(var entry : WebPageEntry) -> void: + # Skips checking the last entry (size() - 1) + for i in range(entries.size() - 1): + if entries[i] == entry: + entries[i] = entries[i + 1] + entries[i + 1] = entry + emit_signal("entries_changed") + return + +func get_entry_before(var entry : WebPageEntry) -> WebPageEntry: + if entries.size() < 2: + return null + + if entries[0] == entry: + return null + + for i in range(1, entries.size()): + if entries[i] == entry: + return entries[i - 1] + + return null diff --git a/game/addons/web_pages/classes/post_entries/WebPageEntryTitleText.gd b/game/addons/web_pages/classes/post_entries/WebPageEntryTitleText.gd index f0eb741..1fd62cf 100644 --- a/game/addons/web_pages/classes/post_entries/WebPageEntryTitleText.gd +++ b/game/addons/web_pages/classes/post_entries/WebPageEntryTitleText.gd @@ -2,5 +2,5 @@ tool extends "res://addons/web_pages/classes/WebPageEntry.gd" class_name WebPageEntryTitleText, "res://addons/web_pages/icons/icon_web_page_entry_title_text.svg" -export(String) var title_text : String +export(String) var text : String export(int) var hsize : int = 1 diff --git a/game/addons/web_pages/editor/AddEntryPopup.gd b/game/addons/web_pages/editor/AddEntryPopup.gd index 357a6c4..0416c2a 100644 --- a/game/addons/web_pages/editor/AddEntryPopup.gd +++ b/game/addons/web_pages/editor/AddEntryPopup.gd @@ -3,11 +3,16 @@ extends AcceptDialog signal on_entry_class_selected(entry_class) +var _init : bool = false + func _notification(what): - if what == NOTIFICATION_INSTANCED: - get_ok().set_text("Close") - - get_node("HFlowContainer/AddTitleTextButton").connect("pressed", self, "_add_title_text_button_pressed") + if what == NOTIFICATION_ENTER_TREE: + if !_init: + _init = true + + get_ok().set_text("Close") + get_node("VBC/AddTitleTextButton").connect("pressed", self, "_add_title_text_button_pressed") func _add_title_text_button_pressed() -> void: emit_signal("on_entry_class_selected" , "WebPageEntryTitleText") + hide() diff --git a/game/addons/web_pages/editor/PageEditor.gd b/game/addons/web_pages/editor/PageEditor.gd index cd50591..3b94351 100644 --- a/game/addons/web_pages/editor/PageEditor.gd +++ b/game/addons/web_pages/editor/PageEditor.gd @@ -1,7 +1,7 @@ tool extends PanelContainer -var PageEditor : PackedScene = null +var WebPageEntryEditor : PackedScene = null var _wne_tool_bar_button : Button = null @@ -11,11 +11,70 @@ var undo_redo : UndoRedo = null var _entry_name_line_edit : LineEdit = null var _uri_segment_line_edit : LineEdit = null var _add_entry_popup : AcceptDialog = null +var _entries_container : Control = null + +var _entry_add_after : WebPageEntry = null func set_page(page : WebPage): + if _page: + _page.disconnect("entries_changed", self, "on_page_entries_changed") + _page = page + + if _page: + _page.connect("entries_changed", self, "on_page_entries_changed") + _entry_name_line_edit.text = page.name _uri_segment_line_edit.text = page.uri_segment + + recreate() + +func recreate() -> void: + clear() + create_editors() + +func create_editors() -> void: + for i in range(_page.entries.size()): + var e : WebPageEntry = _page.entries[i] + + if e: + var ee : Control = create_editor_for_entry(e) + _entries_container.add_child(ee) + +func create_editor_for_entry(entry : WebPageEntry) -> Control: + var c : Control = WebPageEntryEditor.instance() as Control + c.set_entry(entry, undo_redo) + return c + +func clear() -> void: + for i in range(_entries_container.get_child_count()): + _entries_container.get_child(i).queue_free() + +func add_entry(entry : WebPageEntry, after : WebPageEntry = null) -> void: + undo_redo.create_action("Added web page entry") + undo_redo.add_do_method(_page, "add_entry", entry, after) + undo_redo.add_undo_method(_page, "remove_entry", entry) + undo_redo.commit_action() + +func remove_entry(entry : WebPageEntry) -> void: + var after : WebPageEntry = _page.get_entry_before(entry) + + undo_redo.create_action("Added web page entry") + undo_redo.add_do_method(_page, "remove_entry", entry) + undo_redo.add_undo_method(_page, "add_entry", entry, after) + undo_redo.commit_action() + +func move_entry_up(entry : WebPageEntry) -> void: + undo_redo.create_action("Added web page entry") + undo_redo.add_do_method(_page, "move_entry_up", entry) + undo_redo.add_undo_method(_page, "move_entry_down", entry) + undo_redo.commit_action() + +func move_entry_down(entry : WebPageEntry) -> void: + undo_redo.create_action("Added web page entry") + undo_redo.add_do_method(_page, "move_entry_down", entry) + undo_redo.add_undo_method(_page, "move_entry_up", entry) + undo_redo.commit_action() func _notification(what): if what == NOTIFICATION_INSTANCED: @@ -28,7 +87,12 @@ func _notification(what): _add_entry_popup = get_node("Popups/AddEntryPopup") _add_entry_popup.connect("on_entry_class_selected", self, "_on_add_entry_class_selected") - PageEditor = ResourceLoader.load("res://addons/web_pages/editor/PageEditor.tscn", "PackedScene") as PackedScene + _entries_container = get_node("MC/EntriesContainer/MainVB/Entries") + + var main_add_button : Button = get_node("MC/EntriesContainer/MainVB/MainAddButton") + main_add_button.connect("pressed", self, "_on_entry_add_requested") + + WebPageEntryEditor = ResourceLoader.load("res://addons/web_pages/editor/WebPageEntryEditor.tscn", "PackedScene") as PackedScene elif what == NOTIFICATION_ENTER_TREE: var wne : Control = Engine.get_global("WebNodeEditor") if wne: @@ -93,5 +157,34 @@ func _edited_node_changed(web_node : WebNode): #add method to switch off to the prev screen #wne.switch_to_main_screen_tab(self) +func on_page_entries_changed() -> void: + recreate() + func _on_add_entry_class_selected(cls_name : String) -> void: - print(cls_name) + var entry : WebPageEntry = null + + if cls_name == "WebPageEntryTitleText": + entry = WebPageEntryTitleText.new() + + if !entry: + PLogger.log_error("PageEditor: Couldn't create entry for: " + cls_name) + return + + add_entry(entry, _entry_add_after) + +func _on_entry_add_requested() -> void: + _entry_add_after = null + _add_entry_popup.popup_centered() + +func _on_entry_add_requested_after(entry) -> void: + _entry_add_after = entry + _add_entry_popup.popup_centered() + +func _on_entry_move_up_requested(entry) -> void: + move_entry_up(entry) + +func _on_entry_move_down_requested(entry) -> void: + move_entry_down(entry) + +func _on_entry_delete_requested(entry) -> void: + remove_entry(entry) diff --git a/game/addons/web_pages/editor/PageEditor.tscn b/game/addons/web_pages/editor/PageEditor.tscn index ee848e9..723f52d 100644 --- a/game/addons/web_pages/editor/PageEditor.tscn +++ b/game/addons/web_pages/editor/PageEditor.tscn @@ -61,10 +61,21 @@ margin_bottom = 586.0 size_flags_horizontal = 3 size_flags_vertical = 3 -[node name="Entries" type="VBoxContainer" parent="MC/EntriesContainer"] +[node name="MainVB" type="VBoxContainer" parent="MC/EntriesContainer"] margin_right = 1010.0 +margin_bottom = 24.0 size_flags_horizontal = 3 +[node name="MainAddButton" type="Button" parent="MC/EntriesContainer/MainVB"] +margin_right = 1010.0 +margin_bottom = 20.0 +text = "Add (Bottom)" + +[node name="Entries" type="VBoxContainer" parent="MC/EntriesContainer/MainVB"] +margin_top = 24.0 +margin_right = 1010.0 +margin_bottom = 24.0 + [node name="Popups" type="Control" parent="."] margin_left = 7.0 margin_top = 7.0 @@ -80,20 +91,20 @@ anchor_bottom = 0.5 margin_left = -100.0 margin_top = -35.0 margin_right = 100.0 -margin_bottom = 35.0 +margin_bottom = 31.0 window_title = "Add" script = ExtResource( 3 ) -[node name="HFlowContainer" type="HFlowContainer" parent="Popups/AddEntryPopup"] +[node name="VBC" type="VBoxContainer" parent="Popups/AddEntryPopup"] margin_left = 8.0 margin_top = 8.0 margin_right = 192.0 -margin_bottom = 34.0 +margin_bottom = 30.0 size_flags_horizontal = 3 size_flags_vertical = 3 -[node name="AddTitleTextButton" type="Button" parent="Popups/AddEntryPopup/HFlowContainer"] -margin_right = 89.0 +[node name="AddTitleTextButton" type="Button" parent="Popups/AddEntryPopup/VBC"] +margin_right = 184.0 margin_bottom = 22.0 text = "Title Text" icon = ExtResource( 2 ) diff --git a/game/addons/web_pages/editor/PageEntryEditor.gd b/game/addons/web_pages/editor/WebPageEntryEditor.gd similarity index 62% rename from game/addons/web_pages/editor/PageEntryEditor.gd rename to game/addons/web_pages/editor/WebPageEntryEditor.gd index bba938b..9c00f7c 100644 --- a/game/addons/web_pages/editor/PageEntryEditor.gd +++ b/game/addons/web_pages/editor/WebPageEntryEditor.gd @@ -4,16 +4,30 @@ extends VBoxContainer var _entry : WebPageEntry = null var _entry_type_label : Label = null +var _main_container : Control = null + +var _editor : Control = null + +var WebPageEntryTitleTextEditor : PackedScene = null signal entry_add_requested_after(entry) signal entry_move_up_requested(entry) signal entry_move_down_requested(entry) signal entry_delete_requested(entry) -func set_entry(entry : WebPageEntry) -> void: +func set_entry(entry : WebPageEntry, undo_redo : UndoRedo) -> void: _entry = entry - _entry_type_label.text = entry.get_class() + var cls : String = entry.get_class() + _entry_type_label.text = cls + + if cls == "WebPageEntryTitleText": + _editor = WebPageEntryTitleTextEditor.instance() + + if _editor: + _editor.undo_redo = undo_redo + _editor.set_entry(entry) + _main_container.add_child(_editor) func _on_add_button_pressed(): emit_signal("entry_add_requested_after", _entry) @@ -29,7 +43,10 @@ func _on_delete_button_pressed(): func _notification(what): if what == NOTIFICATION_INSTANCED: + WebPageEntryTitleTextEditor = ResourceLoader.load("res://addons/web_pages/editor/post_entries/WebPageEntryTitleTextEditor.tscn", "PackedScene") + _entry_type_label = get_node("PC/VBC/TopBar/EntryTypeLabel") + _main_container = get_node("PC/VBC/MainContainer") get_node("AddButton").connect("pressed", self, "_on_add_button_pressed") get_node("PC/VBC/TopBar/UpButton").connect("pressed", self, "_on_up_button_pressed") diff --git a/game/addons/web_pages/editor/PageEntryEditor.tscn b/game/addons/web_pages/editor/WebPageEntryEditor.tscn similarity index 83% rename from game/addons/web_pages/editor/PageEntryEditor.tscn rename to game/addons/web_pages/editor/WebPageEntryEditor.tscn index 51361dc..dcb1eea 100644 --- a/game/addons/web_pages/editor/PageEntryEditor.tscn +++ b/game/addons/web_pages/editor/WebPageEntryEditor.tscn @@ -1,6 +1,6 @@ [gd_scene load_steps=2 format=2] -[ext_resource path="res://addons/web_pages/editor/PageEntryEditor.gd" type="Script" id=1] +[ext_resource path="res://addons/web_pages/editor/WebPageEntryEditor.gd" type="Script" id=1] [node name="Entry" type="VBoxContainer"] margin_right = 1024.0 @@ -10,13 +10,13 @@ script = ExtResource( 1 ) [node name="PC" type="PanelContainer" parent="."] margin_right = 1024.0 -margin_bottom = 46.0 +margin_bottom = 60.0 [node name="VBC" type="VBoxContainer" parent="PC"] margin_left = 7.0 margin_top = 7.0 margin_right = 1017.0 -margin_bottom = 39.0 +margin_bottom = 53.0 [node name="TopBar" type="HBoxContainer" parent="PC/VBC"] margin_right = 1010.0 @@ -51,13 +51,13 @@ margin_top = 24.0 margin_right = 1010.0 margin_bottom = 28.0 -[node name="MainContainer" type="MarginContainer" parent="PC/VBC"] +[node name="MainContainer" type="PanelContainer" parent="PC/VBC"] margin_top = 32.0 margin_right = 1010.0 -margin_bottom = 32.0 +margin_bottom = 46.0 [node name="AddButton" type="Button" parent="."] -margin_top = 50.0 +margin_top = 64.0 margin_right = 1024.0 -margin_bottom = 70.0 -text = "Add" +margin_bottom = 84.0 +text = "Add Here" diff --git a/game/addons/web_pages/editor/post_entries/WebPageEntryTitleTextEditor.gd b/game/addons/web_pages/editor/post_entries/WebPageEntryTitleTextEditor.gd new file mode 100644 index 0000000..1389942 --- /dev/null +++ b/game/addons/web_pages/editor/post_entries/WebPageEntryTitleTextEditor.gd @@ -0,0 +1,28 @@ +tool +extends MarginContainer + +var _entry : WebPageEntryTitleText = null +var undo_redo : UndoRedo = null + +var _line_edit : LineEdit = null + +func set_entry(entry : WebPageEntryTitleText, pundo_redo : UndoRedo) -> void: + undo_redo = pundo_redo + + _entry = entry + + _line_edit.text = _entry.text + +func _notification(what): + if what == NOTIFICATION_INSTANCED: + _line_edit = get_node("LineEdit") + + _line_edit.connect("text_entered", self, "_on_line_edit") + +func _on_line_edit(text : String) -> void: + undo_redo.create_action("Page title text changed") + undo_redo.add_do_property(_entry, "text", text) + undo_redo.add_undo_property(_entry, "text", _entry.text) + undo_redo.add_do_property(_line_edit, "text", text) + undo_redo.add_undo_property(_line_edit, "text", _entry.text) + undo_redo.commit_action() diff --git a/game/addons/web_pages/editor/post_entries/WebPageEntryTitleTextEditor.tscn b/game/addons/web_pages/editor/post_entries/WebPageEntryTitleTextEditor.tscn new file mode 100644 index 0000000..a4052e4 --- /dev/null +++ b/game/addons/web_pages/editor/post_entries/WebPageEntryTitleTextEditor.tscn @@ -0,0 +1,12 @@ +[gd_scene load_steps=2 format=2] + +[ext_resource path="res://addons/web_pages/editor/post_entries/WebPageEntryTitleTextEditor.gd" type="Script" id=1] + +[node name="WebPageEntryTitleTextEditor" type="MarginContainer"] +anchor_right = 1.0 +anchor_bottom = 1.0 +script = ExtResource( 1 ) + +[node name="LineEdit" type="LineEdit" parent="."] +margin_right = 1024.0 +margin_bottom = 600.0