3 class_name DialogicCharacter
6 ## Resource that represents a character in dialog.
7 ## Manages/contains portraits, custom info and translation of characters.
9 @export var display_name := ""
10 @export var nicknames := []
12 @export var color := Color()
13 @export var description := ""
15 @export var scale := 1.0
16 @export var offset := Vector2()
17 @export var mirror := false
19 @export var default_portrait := ""
20 @export var portraits := {}
22 @export var custom_info := {}
24 ## All valid properties that can be accessed by their translation.
25 enum TranslatedProperties {
30 var _translation_id := ""
33 func _to_string() -> String:
34 return "[{name}:{id}]".format({"name":get_character_name(), "id":get_instance_id()})
37 ## Adds a translation ID to the character.
38 func add_translation_id() -> String:
39 _translation_id = DialogicUtil.get_next_translation_id()
40 return _translation_id
43 ## Returns the character's translation ID.
44 ## Adds a translation ID to the character if it doesn't have one.
45 func get_set_translation_id() -> String:
46 if _translation_id == null or _translation_id.is_empty():
47 return add_translation_id()
49 return _translation_id
52 ## Removes the translation ID from the character.
53 func remove_translation_id() -> void:
57 ## Checks [param property] and matches it to a translation key.
59 ## Undefined behaviour if an invalid integer is passed.
60 func get_property_translation_key(property: TranslatedProperties) -> String:
61 var property_key := ""
64 TranslatedProperties.NAME:
66 TranslatedProperties.NICKNAMES:
67 property_key = "nicknames"
69 return "Character".path_join(_translation_id).path_join(property_key)
72 ## Accesses the original text of the character.
74 ## Undefined behaviour if an invalid integer is passed.
75 func _get_property_original_text(property: TranslatedProperties) -> String:
77 TranslatedProperties.NAME:
79 TranslatedProperties.NICKNAMES:
80 return ", ".join(nicknames)
85 ## Access a property of the character and if conditions are met, attempts to
86 ## translate the property.
88 ## The translation feature must be enabled in the project settings.
89 ## The translation ID must be set.
90 ## Otherwise, returns the text property as is.
92 ## Undefined behaviour if an invalid integer is passed.
93 func _get_property_translated(property: TranslatedProperties) -> String:
94 var try_translation: bool = (_translation_id != null
95 and not _translation_id.is_empty()
96 and ProjectSettings.get_setting('dialogic/translation/enabled', false)
100 var translation_key := get_property_translation_key(property)
101 var translated_property := tr(translation_key)
103 # If no translation is found, tr() returns the ID.
104 # However, we want to fallback to the original text.
105 if translated_property == translation_key:
106 return _get_property_original_text(property)
108 return translated_property
111 return _get_property_original_text(property)
114 ## Translates the nicknames of the characters and then returns them as an array
116 func get_nicknames_translated() -> Array:
117 var translated_nicknames := _get_property_translated(TranslatedProperties.NICKNAMES)
118 return (translated_nicknames.split(", ") as Array)
121 ## Translates and returns the display name of the character.
122 func get_display_name_translated() -> String:
123 return _get_property_translated(TranslatedProperties.NAME)
126 ## Returns the best name for this character.
127 func get_character_name() -> String:
128 var unique_identifier := DialogicResourceUtil.get_unique_identifier(resource_path)
129 if not unique_identifier.is_empty():
130 return unique_identifier
131 if not resource_path.is_empty():
132 return resource_path.get_file().trim_suffix('.dch')
133 elif not display_name.is_empty():
134 return display_name.validate_node_name()
136 return "UnnamedCharacter"
139 ## Returns the info of the given portrait.
140 ## Uses the default portrait if the given portrait doesn't exist.
141 func get_portrait_info(portrait_name:String) -> Dictionary:
142 return portraits.get(portrait_name, portraits.get(default_portrait, {}))