]> Untitled Git - wolf-seeking-sheep.git/blob - addons/dialogic/Resources/character.gd
Squashed commit of the following:
[wolf-seeking-sheep.git] / addons / dialogic / Resources / character.gd
1 @tool
2 extends Resource
3 class_name DialogicCharacter
4
5
6 ## Resource that represents a character in dialog.
7 ## Manages/contains portraits, custom info and translation of characters.
8
9 @export var display_name := ""
10 @export var nicknames := []
11
12 @export var color := Color()
13 @export var description := ""
14
15 @export var scale  := 1.0
16 @export var offset := Vector2()
17 @export var mirror := false
18
19 @export var default_portrait := ""
20 @export var portraits := {}
21
22 @export var custom_info := {}
23
24 ## All valid properties that can be accessed by their translation.
25 enum TranslatedProperties {
26         NAME,
27         NICKNAMES,
28 }
29
30 var _translation_id := ""
31
32
33 func _to_string() -> String:
34         return "[{name}:{id}]".format({"name":get_character_name(), "id":get_instance_id()})
35
36
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
41
42
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()
48         else:
49                 return _translation_id
50
51
52 ## Removes the translation ID from the character.
53 func remove_translation_id() -> void:
54         _translation_id = ""
55
56
57 ## Checks [param property] and matches it to a translation key.
58 ##
59 ## Undefined behaviour if an invalid integer is passed.
60 func get_property_translation_key(property: TranslatedProperties) -> String:
61         var property_key := ""
62
63         match property:
64                 TranslatedProperties.NAME:
65                         property_key = "name"
66                 TranslatedProperties.NICKNAMES:
67                         property_key = "nicknames"
68
69         return "Character".path_join(_translation_id).path_join(property_key)
70
71
72 ## Accesses the original text of the character.
73 ##
74 ## Undefined behaviour if an invalid integer is passed.
75 func _get_property_original_text(property: TranslatedProperties) -> String:
76         match property:
77                 TranslatedProperties.NAME:
78                         return display_name
79                 TranslatedProperties.NICKNAMES:
80                         return ", ".join(nicknames)
81
82         return ""
83
84
85 ## Access a property of the character and if conditions are met, attempts to
86 ## translate the property.
87 ##
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.
91 ##
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)
97         )
98
99         if try_translation:
100                 var translation_key := get_property_translation_key(property)
101                 var translated_property := tr(translation_key)
102
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)
107
108                 return translated_property
109
110         else:
111                 return _get_property_original_text(property)
112
113
114 ## Translates the nicknames of the characters and then returns them as an array
115 ## of strings.
116 func get_nicknames_translated() -> Array:
117         var translated_nicknames := _get_property_translated(TranslatedProperties.NICKNAMES)
118         return (translated_nicknames.split(", ") as Array)
119
120
121 ## Translates and returns the display name of the character.
122 func get_display_name_translated() -> String:
123         return _get_property_translated(TranslatedProperties.NAME)
124
125
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()
135         else:
136                 return "UnnamedCharacter"
137
138
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, {}))