@@ -11,6 +11,9 @@ var WORKING_DIR = "res://dialogic"
1111var DIALOG_DIR = WORKING_DIR + "/dialogs"
1212var CHAR_DIR = WORKING_DIR + "/characters"
1313var working_dialog_file = ''
14+ var timer_duration = 200
15+ var timer_interval = 30
16+ var autosaving_hash
1417onready var Timeline = $ Editor/EventEditor/TimeLine
1518onready var DialogList = $ Editor/EventTools/DialogItemList
1619onready var CharacterList = $ Editor/CharacterTools/CharacterItemList
@@ -42,6 +45,12 @@ func _ready():
4245 # Making the dialog editor the default
4346 hide_editors ()
4447 _on_EventButton_pressed ()
48+
49+ func _process (delta ):
50+ timer_interval -= 1
51+ if timer_interval < 0 :
52+ timer_interval = timer_duration
53+ _on_AutoSaver_timeout ()
4554
4655func _on_piece_connect (from , from_slot , to , to_slot ):
4756 $ Editor/GraphEdit .connect_node (from , from_slot , to , to_slot )
@@ -128,8 +137,7 @@ func _on_ReloadResource_pressed():
128137func _on_ButtonSave_pressed ():
129138 save_nodes (working_dialog_file )
130139
131- func save_nodes (path ):
132- print ('Saving resource --------' )
140+ func generate_save_data ():
133141 var info_to_save = {
134142 'metadata' : {
135143 'dialogic-version' : '0.4'
@@ -138,13 +146,16 @@ func save_nodes(path):
138146 }
139147 for event in Timeline .get_children ():
140148 info_to_save ['events' ].append (event .event_data )
141-
149+ return info_to_save
150+
151+ func save_nodes (path ):
152+ print ('Saving resource --------' )
153+ var info_to_save = generate_save_data ()
142154 var file = File .new ()
143155 file .open (path , File .WRITE )
144156 file .store_line (to_json (info_to_save ))
145157 file .close ()
146-
147- print (info_to_save )
158+ autosaving_hash = info_to_save .hash ()
148159
149160func load_nodes (path ):
150161 working_dialog_file = path
@@ -176,6 +187,7 @@ func load_nodes(path):
176187 create_character_leave_node (i ['character' ], i ['action' ])
177188 print ('character-leave-block: ' , i )
178189
190+ autosaving_hash = generate_save_data ().hash ()
179191 fold_all_nodes ()
180192
181193# Conversation files
@@ -202,13 +214,30 @@ func refresh_dialog_list():
202214 DialogList .set_item_metadata (index , {'file' : c ['file' ], 'index' : index })
203215 index += 1
204216
205-
206217func _on_DialogItemList_item_selected (index ):
207218 var selected = DialogList .get_item_text (index )
208219 var file = DialogList .get_item_metadata (index )['file' ]
209220 clear_timeline ()
210221 load_nodes (DIALOG_DIR + '/' + file )
211222
223+ # Renaming dialogs
224+
225+ func _on_DialogItemList_item_rmb_selected (index , at_position ):
226+ print (index )
227+ $ RenameDialog .register_text_enter ($ RenameDialog/LineEdit )
228+ $ RenameDialog/LineEdit .text = get_filename_from_path (working_dialog_file )
229+ $ RenameDialog .set_as_minsize ()
230+ $ RenameDialog .popup_centered ()
231+
232+ func _on_RenameDialog_confirmed ():
233+ var new_name = $ RenameDialog/LineEdit .text + '.json'
234+ var dir = Directory .new ()
235+ var new_full_path = DIALOG_DIR + '/' + new_name
236+ dir .rename (working_dialog_file , new_full_path )
237+ working_dialog_file = new_full_path
238+ $ RenameDialog/LineEdit .text = ''
239+ refresh_dialog_list ()
240+
212241# Character Creation
213242func _on_Button_pressed ():
214243 var file = create_character ()
@@ -289,6 +318,18 @@ func _on_character_SaveButton_pressed():
289318 file .close ()
290319 refresh_character_list ()
291320
321+ func get_character_data (file ):
322+ var data = load_json (CHAR_DIR + '/' + file )
323+ return data
324+
325+ func get_character_color (file ):
326+ var data = load_json (CHAR_DIR + '/' + file )
327+ if is_instance_valid (data ):
328+ if data .has ('color' ):
329+ return data ['color' ]
330+ else :
331+ return "ffffff"
332+
292333func get_character_name (file ):
293334 var data = load_json (CHAR_DIR + '/' + file )
294335 if data .has ('name' ):
@@ -311,7 +352,6 @@ func _on_RemoveConfirmation_confirmed():
311352 clear_character_editor ()
312353 refresh_character_list ()
313354
314-
315355func _on_DeleteButton_pressed ():
316356 $ RemoveConfirmation .popup_centered ()
317357
@@ -342,6 +382,11 @@ func load_json(path):
342382 return
343383 return data_parse .result
344384
385+ func get_filename_from_path (path ):
386+ if OS .get_name () == "Windows" :
387+ return path .split ('/' )[- 1 ].replace ('.json' , '' )
388+ else :
389+ return path .split ('\\ ' )[- 1 ].replace ('.json' , '' )
345390
346391# Godot dialog
347392func godot_dialog ():
@@ -362,7 +407,6 @@ func fold_all_nodes():
362407func unfold_all_nodes ():
363408 for event in Timeline .get_children ():
364409 event .get_node ("VBoxContainer/Header/VisibleToggle" ).set_pressed (true )
365- print (event .get_node ("VBoxContainer/Header/VisibleToggle" ))
366410
367411func _on_ButtonFold_pressed ():
368412 fold_all_nodes ()
@@ -387,3 +431,10 @@ func _on_CharactersButton_pressed():
387431 $ Editor/CharacterTools .visible = true
388432 $ Editor/CharacterEditor .visible = true
389433 $ HBoxContainer/CharactersButton .set ('self_modulate' , Color ('#6a9dea' ))
434+
435+ # Auto saving
436+
437+ func _on_AutoSaver_timeout ():
438+ if autosaving_hash != generate_save_data ().hash ():
439+ save_nodes (working_dialog_file )
440+ print ('[!] Changes detected. Auto saving. ' , autosaving_hash )
0 commit comments