@@ -2,8 +2,15 @@ extends DialogicSubsystem
22
33## Subsystem that manages portraits and portrait positions.
44
5+ signal character_joined (info :Dictionary )
6+ signal character_left (info :Dictionary )
7+ signal character_portrait_changed (info :Dictionary )
8+ signal character_moved (info :Dictionary )
9+ signal position_changed (info :Dictionary )
10+
11+
512## The default portrait scene.
6- var default_portrait_scene = load (get_script ().resource_path .get_base_dir ().path_join ('default_portrait.tscn' ))
13+ var default_portrait_scene : PackedScene = load (get_script ().resource_path .get_base_dir ().path_join ('default_portrait.tscn' ))
714
815## A reference to the current [DialogicNode_PortraitHolder].
916var _portrait_holder_reference : Node = null
@@ -44,7 +51,6 @@ func resume() -> void:
4451
4552## Joins a character and then calls change_portrait().
4653func add_portrait (character :DialogicCharacter , portrait :String , position_idx :int , mirrored : bool = false , z_index : int = 0 , extra_data :String = "" ) -> Node :
47- var character_node = null
4854
4955 if portrait .is_empty ():
5056 portrait = character .default_portrait
@@ -59,7 +65,7 @@ func add_portrait(character:DialogicCharacter, portrait:String, position_idx:in
5965 printerr ("[DialogicError] Character " ,character .display_name , " has no default portrait to use." )
6066 return null
6167
62- character_node = Node2D .new ()
68+ var character_node : = Node2D .new ()
6369 character_node .name = character .get_character_name ()
6470 character_node .set_meta ('character' , character )
6571 character_node .z_index = z_index
@@ -81,6 +87,10 @@ func add_portrait(character:DialogicCharacter, portrait:String, position_idx:in
8187 change_portrait_extradata (character , extra_data )
8288 change_portrait_z_index (character , z_index )
8389
90+ var info := {'character' :character }
91+ info .merge (dialogic .current_state_info ['portraits' ][character .resource_path ])
92+ character_joined .emit (info )
93+
8494 return character_node
8595
8696
@@ -97,19 +107,23 @@ func change_portrait(character:DialogicCharacter, portrait:String, update_transf
97107 print_debug ('[Dialogic] Change to not-existing portrait will be ignored!' )
98108 return
99109
110+ var info := {'character' :character , 'portrait' :portrait , 'same_scene' :false }
111+
100112 var char_node :Node = dialogic .current_state_info .portraits [character .resource_path ].node
101113
102114 # path to the scene to use
103115 var scene_path :String = character .portraits [portrait ].get ('scene' , '' )
104116
105- var portrait_node = null
117+ var portrait_node : Node = null
106118
107119 # check if the scene is the same as the currently loaded scene
108120 if (char_node .get_child_count () and
109121 character .portraits [dialogic .current_state_info ['portraits' ][character .resource_path ]['portrait' ]].get ('scene' , '' ) == scene_path and
110122 # also check if the scene supports changing to the given portrait
111123 (! char_node .get_child (0 ).has_method ('_should_do_portrait_update' ) or char_node .get_child (0 )._should_do_portrait_update (character , portrait ))):
112124 portrait_node = char_node .get_child (0 )
125+ info ['same_scene' ] = true
126+
113127 else :
114128 # remove previous portrait
115129 if char_node .get_child_count ():
@@ -118,13 +132,14 @@ func change_portrait(character:DialogicCharacter, portrait:String, update_transf
118132 if scene_path .is_empty ():
119133 portrait_node = default_portrait_scene .instantiate ()
120134 else :
121- var p = load (scene_path )
135+ var p : PackedScene = load (scene_path )
122136 if p :
123137 portrait_node = p .instantiate ()
124138 else :
125139 push_error ('Dialogic: Portrait node "' + str (scene_path ) + '" for character [' + character .display_name + '] could not be loaded. Your portrait might not show up on the screen.' )
126140
127141 dialogic .current_state_info ['portraits' ][character .resource_path ]['portrait' ] = portrait
142+ character_portrait_changed .emit (info )
128143
129144 if portrait_node :
130145 for property in character .portraits [portrait ].get ('export_overrides' , {}).keys ():
@@ -194,13 +209,13 @@ func animate_portrait(character:DialogicCharacter, animation_path:String, length
194209 print_debug ('[DialogicError] Cannot animate portrait of null/not joined character.' )
195210 return null
196211
197- var char_node = dialogic .current_state_info ['portraits' ][character .resource_path ].node
212+ var char_node : Node = dialogic .current_state_info ['portraits' ][character .resource_path ].node
198213
199214 if dialogic .current_state_info ['portraits' ][character .resource_path ].get ('animation_node' , null ):
200215 if is_instance_valid (dialogic .current_state_info ['portraits' ][character .resource_path ].animation_node ):
201216 dialogic .current_state_info ['portraits' ][character .resource_path ].animation_node .queue_free ()
202- var anim_script = load (animation_path )
203- var anim_node = Node .new ()
217+ var anim_script : Script = load (animation_path )
218+ var anim_node : = Node .new ()
204219 anim_node .set_script (anim_script )
205220 anim_node = (anim_node as DialogicAnimation )
206221 anim_node .node = char_node
@@ -235,11 +250,11 @@ func move_portrait(character:DialogicCharacter, position_idx:int, time:float = 0
235250 remove_portrait (character )
236251 return
237252
238-
239253 char_node .position = global_pos - char_node .get_parent ().global_position
240254 update_portrait_transform (character , time )
241255
242256 dialogic .current_state_info .portraits [character .resource_path ].position_index = position_idx
257+ character_moved .emit ({'character' :character , 'position_index' :position_idx , 'time' :time })
243258
244259
245260func change_portrait_z_index (character :DialogicCharacter , z_index :int , update_zindex := true ) -> void :
@@ -249,6 +264,7 @@ func change_portrait_z_index(character:DialogicCharacter, z_index:int, update_zi
249264
250265
251266func remove_portrait (character :DialogicCharacter ) -> void :
267+ character_left .emit ({'character' :character })
252268 dialogic .current_state_info ['portraits' ][character .resource_path ].node .queue_free ()
253269 dialogic .current_state_info ['portraits' ].erase (character .resource_path )
254270
@@ -266,18 +282,20 @@ func add_portrait_position(position_index: int, position:Vector2) -> void:
266282 new_position .origin_anchor = example_position .origin_anchor
267283 new_position .position_index = position_index
268284 new_position .position = position - new_position ._get_origin_position ()
285+ position_changed .emit ({'change' :'added' , 'container_node' :new_position , 'position_index' :position_index })
269286
270287
271288func move_portrait_position (position_index : int , vector :Vector2 , relative :bool = false , time :float = 0.0 ) -> void :
272- for portrait_position in get_tree ().get_nodes_in_group ('dialogic_portrait_container' ):
273- if portrait_position .is_visible_in_tree () and portrait_position .position_index == position_index :
274- if ! portrait_position .has_meta ('default_position' ):
275- portrait_position .set_meta ('default_position' , portrait_position .position )
276- var tween := portrait_position .create_tween ()
289+ for portrait_container in get_tree ().get_nodes_in_group ('dialogic_portrait_container' ):
290+ if portrait_container .is_visible_in_tree () and portrait_container .position_index == position_index :
291+ if ! portrait_container .has_meta ('default_position' ):
292+ portrait_container .set_meta ('default_position' , portrait_container .position )
293+ var tween := portrait_container .create_tween ()
277294 if ! relative :
278- tween .tween_property (portrait_position , 'position' , vector , time )
295+ tween .tween_property (portrait_container , 'position' , vector , time )
279296 else :
280- tween .tween_property (portrait_position , 'position' , vector , time ).as_relative ()
297+ tween .tween_property (portrait_container , 'position' , vector , time ).as_relative ()
298+ position_changed .emit ({'change' :'moved' , 'container_node' :portrait_container , 'position_index' :position_index })
281299 return
282300
283301 # If this is reached, no position could be found. If the position is absolute, we will add it.
0 commit comments