diff --git a/game/addons/web_pages/classes/WebPage.gd b/game/addons/web_pages/classes/WebPage.gd index f3358f7..e19e9d2 100644 --- a/game/addons/web_pages/classes/WebPage.gd +++ b/game/addons/web_pages/classes/WebPage.gd @@ -486,8 +486,120 @@ func replace_entry(var entry : WebPageEntry, var data : WebPageEntry) -> void: on_entries_changed() return +func clear_entries() -> void: + for i in range(entries.size()): + var e : WebPageEntry = entries[i] + + if e && e.is_connected("changed", self, "_on_entry_changed"): + e.disconnect("changed", self, "_on_entry_changed") + + entries.clear() + +func save_data(): + _save_data() + +func _save_data(): + if !Engine.editor_hint && allow_web_interface_editing: + var data : Dictionary = Dictionary() + + data["entries_size"] = entries.size() + var entries_data : Array = Array() + + for i in range(entries.size()): + var e : WebPageEntry = entries[i] + + if e: + var ed : Dictionary = Dictionary() + + ed["index"] = i + ed["class"] = e.get_page_entry_class_name() + ed["data"] = e.to_dict() + + entries_data.push_back(ed) + + data["entries"] = entries_data + + # TODO add get_uri(), get_root_uri(), etc helpers to WebNode + var wn : WebNode = self + var wnpath : String + + while wn: + wnpath = wn.uri_segment + "_" + wnpath + wn = wn.get_parent_webnode() + + if !wnpath.empty(): + wnpath = "user://" + wnpath + ".json" + + var f : File = File.new() + f.open(wnpath, File.WRITE) + f.store_string(to_json(data)) + f.close() + +func load_data(): + _load_data() + +func _load_data(): + if !Engine.editor_hint && allow_web_interface_editing: + var data : Dictionary = Dictionary() + var wn : WebNode = self + var wnpath : String + + while wn: + wnpath = wn.uri_segment + "_" + wnpath + wn = wn.get_parent_webnode() + + if !wnpath.empty(): + wnpath = "user://" + wnpath + ".json" + + var f : File = File.new() + + if !f.file_exists(wnpath): + return + + f.open(wnpath, File.READ) + var dr : String = f.get_as_text() + f.close() + + var jp : JSONParseResult = JSON.parse(dr) + + if jp.error != OK: + print("jp.error != OK") + return + + data = jp.result + + if !data.has("entries_size") || !data.has("entries"): + return + + clear_entries() + entries.resize(data["entries_size"]) + var entries_data : Array = data["entries"] + + for i in range(entries.size()): + var ed : Dictionary = entries_data[i] + + var index : int = ed["index"] + + if index < 0 || index >= entries.size(): + print("ERROR! index < 0 || index >= entries.size()") + return + + var cls : String = ed["class"] + var edata : Dictionary = ed["data"] + + var e : WebPageEntry = create_entry(cls) + + if e: + e.from_dict(edata) + e.id = index + entries[index] = e + + e.connect("changed", self, "_on_entry_changed") + func _notification(what): if what == NOTIFICATION_READY: + load_data() + if !Engine.editor_hint: for i in range(entries.size()): var e : WebPageEntry = entries[i] @@ -510,7 +622,7 @@ func _notification(what): _pending_array_mutex.unlock() if changed: - #todo save + save_data() on_entries_changed() func _on_entry_changed(): diff --git a/game/addons/web_pages/classes/WebPageEntry.gd b/game/addons/web_pages/classes/WebPageEntry.gd index 8ba9f0e..78a1b0c 100644 --- a/game/addons/web_pages/classes/WebPageEntry.gd +++ b/game/addons/web_pages/classes/WebPageEntry.gd @@ -16,6 +16,18 @@ func render(request : WebServerRequest): func _render(request : WebServerRequest): pass +func to_dict() -> Dictionary: + return _to_dict() + +func from_dict(dict : Dictionary) -> void: + _from_dict(dict) + +func _to_dict() -> Dictionary: + return Dictionary() + +func _from_dict(dict : Dictionary) -> void: + pass + func handle_edit(request : WebServerRequest) -> WebPageEntry: return _handle_edit(request) diff --git a/game/addons/web_pages/classes/post_entries/WebPageEntryImage.gd b/game/addons/web_pages/classes/post_entries/WebPageEntryImage.gd index 4f8fda5..4cbeff4 100644 --- a/game/addons/web_pages/classes/post_entries/WebPageEntryImage.gd +++ b/game/addons/web_pages/classes/post_entries/WebPageEntryImage.gd @@ -113,6 +113,24 @@ func _handle_edit(request : WebServerRequest) -> WebPageEntry: request.compile_and_send_body() return null +func _to_dict() -> Dictionary: + var dict : Dictionary = Dictionary() + + dict["image_path"] = image_path + dict["image_url"] = image_url + dict["alt"] = alt + dict["image_size_x"] = image_size.x + dict["image_size_y"] = image_size.y + + return dict + +func _from_dict(dict : Dictionary) -> void: + image_path = dict["image_path"] + image_url = dict["image_url"] + alt = dict["alt"] + image_size.x = int(dict["image_size_x"]) + image_size.y = int(dict["image_size_y"]) + func _get_editor() -> Control: var WebPageEntryImageEditor : PackedScene = ResourceLoader.load("res://addons/web_pages/editor/post_entries/WebPageEntryImageEditor.tscn", "PackedScene") return WebPageEntryImageEditor.instance() as Control diff --git a/game/addons/web_pages/classes/post_entries/WebPageEntryText.gd b/game/addons/web_pages/classes/post_entries/WebPageEntryText.gd index 88adb99..dfe2713 100644 --- a/game/addons/web_pages/classes/post_entries/WebPageEntryText.gd +++ b/game/addons/web_pages/classes/post_entries/WebPageEntryText.gd @@ -52,6 +52,18 @@ func _handle_edit(request : WebServerRequest) -> WebPageEntry: request.compile_and_send_body() return null +func _to_dict() -> Dictionary: + var dict : Dictionary = Dictionary() + + dict["text"] = text + + return dict + +func _from_dict(dict : Dictionary) -> void: + var t : String = dict["text"] + + set_text(t) + func _get_editor() -> Control: var WebPageEntryTextEditor : PackedScene = ResourceLoader.load("res://addons/web_pages/editor/post_entries/WebPageEntryTextEditor.tscn", "PackedScene") return WebPageEntryTextEditor.instance() as Control diff --git a/game/addons/web_pages/classes/post_entries/WebPageEntryTitleText.gd b/game/addons/web_pages/classes/post_entries/WebPageEntryTitleText.gd index dd2a397..db2071d 100644 --- a/game/addons/web_pages/classes/post_entries/WebPageEntryTitleText.gd +++ b/game/addons/web_pages/classes/post_entries/WebPageEntryTitleText.gd @@ -44,6 +44,16 @@ func _handle_edit(request : WebServerRequest) -> WebPageEntry: request.compile_and_send_body() return null +func _to_dict() -> Dictionary: + var dict : Dictionary = Dictionary() + + dict["text"] = text + + return dict + +func _from_dict(dict : Dictionary) -> void: + text = dict["text"] + func _get_editor() -> Control: var WebPageEntryTitleTextEditor : PackedScene = ResourceLoader.load("res://addons/web_pages/editor/post_entries/WebPageEntryTitleTextEditor.tscn", "PackedScene") return WebPageEntryTitleTextEditor.instance() as Control