11@tool
22extends Control
33
4-
54# -------------------------------------------------------------------------------
65# A button with multiple children buttons corresponding to various possible interactions
76# It's main purpose is to display a thumbnail and respond to UI inputs
87# -------------------------------------------------------------------------------
98
10-
11-
12-
139# These flags define what sort of signals and broadcast
14- enum InteractionFlags {DELETE , SET_DIALOG , SET_DRAG , PRESS , CHECK , CLEAR , SHOW_COUNT , EDIT_LABEL }
15- const PRESET_ALL :Array = [ InteractionFlags .DELETE , InteractionFlags .SET_DIALOG , InteractionFlags .SET_DRAG , InteractionFlags .PRESS ,
16- InteractionFlags .CHECK , InteractionFlags .CLEAR , InteractionFlags .SHOW_COUNT , InteractionFlags .EDIT_LABEL ]
10+ enum InteractionFlags { DELETE , SET_DIALOG , SET_DRAG , PRESS , CHECK , CLEAR , SHOW_COUNT , EDIT_LABEL }
11+ const PRESET_ALL : Array = [
12+ InteractionFlags .DELETE ,
13+ InteractionFlags .SET_DIALOG ,
14+ InteractionFlags .SET_DRAG ,
15+ InteractionFlags .PRESS ,
16+ InteractionFlags .CHECK ,
17+ InteractionFlags .CLEAR ,
18+ InteractionFlags .SHOW_COUNT ,
19+ InteractionFlags .EDIT_LABEL
20+ ]
1721
1822const ThemeAdapter = preload ("../../../controls/theme_adapter.gd" )
1923const FunLib = preload ("../../../utility/fun_lib.gd" )
2024
21- var active_interaction_flags :Array = [] : set = set_active_interaction_flags
22- @export var thumb_size :int = 100 : set = set_thumb_size
23-
24- var root_button_nd :Control = null
25- var texture_rect_nd :Control = null
26- var selection_panel_nd :Control = null
27- var check_box_nd :Control = null
28- var counter_label_nd :Control = null
29- var label_line_container_nd :Control = null
30- var label_line_edit_nd :Control = null
31- var menu_button_nd :Control = null
32- var alt_text_label_nd :Control = null
25+ var active_interaction_flags : Array = []:
26+ set = set_active_interaction_flags
27+ @export var thumb_size : int = 100 :
28+ set = set_thumb_size
29+
30+ var root_button_nd : Control = null
31+ var texture_rect_nd : Control = null
32+ var selection_panel_nd : Control = null
33+ var check_box_nd : Control = null
34+ var counter_label_nd : Control = null
35+ var label_line_container_nd : Control = null
36+ var label_line_edit_nd : Control = null
37+ var menu_button_nd : Control = null
38+ var alt_text_label_nd : Control = null
3339
3440var default_button_sizes : Dictionary = {}
3541
@@ -38,10 +44,9 @@ var default_button_sizes: Dictionary = {}
3844@export var new_texture : Texture2D = null
3945@export var options_texture : Texture2D = null
4046
41- var def_rect_size :Vector2 = Vector2 (100.0 , 100.0 )
42- var def_button_size :Vector2 = Vector2 (24.0 , 24.0 )
43- var def_max_title_chars :int = 8
44-
47+ var def_rect_size : Vector2 = Vector2 (100.0 , 100.0 )
48+ var def_button_size : Vector2 = Vector2 (24.0 , 24.0 )
49+ var def_max_title_chars : int = 8
4550
4651signal requested_delete
4752signal requested_set_dialog
@@ -51,15 +56,12 @@ signal requested_check
5156signal requested_label_edit
5257signal requested_clear
5358
54-
55-
56-
5759# -------------------------------------------------------------------------------
5860# Lifecycle
5961# -------------------------------------------------------------------------------
6062
6163
62- func init (_thumb_size :int , _button_size :int , _active_interaction_flags :Array ):
64+ func init (_thumb_size : int , _button_size : int , _active_interaction_flags : Array ):
6365 set_meta ("class" , "UI_ActionThumbnail" )
6466 thumb_size = _thumb_size
6567 active_interaction_flags = _active_interaction_flags .duplicate ()
@@ -70,7 +72,7 @@ func init(_thumb_size:int, _button_size:int, _active_interaction_flags:Array):
7072func _ready ():
7173 var Label_font_size = get_theme_font_size ("font_size" , "Label" )
7274 _set_default_textures ()
73-
75+
7476 if has_node ("% RootButton" ):
7577 root_button_nd = % RootButton
7678 if root_button_nd .has_signal ("dropped" ):
@@ -96,26 +98,27 @@ func _ready():
9698 if has_node ("% AltTextLabel" ):
9799 alt_text_label_nd = % AltTextLabel
98100 alt_text_label_nd .visible = false
99- if has_node (' % LabelLineEdit' ):
101+ if has_node (" % LabelLineEdit" ):
100102 label_line_container_nd = % LabelLineContainer
101103 label_line_edit_nd = % LabelLineEdit
102104 label_line_edit_nd .theme_type_variation = "PlantTitleLineEdit"
103105 label_line_edit_nd .text_changed .connect (on_label_edit )
104106 label_line_container_nd .visible = false
105- if has_node (' % MenuButton' ):
107+ if has_node (" % MenuButton" ):
106108 menu_button_nd = % MenuButton
107109 menu_button_nd .theme_type_variation = "MenuButton"
108110 menu_button_nd .get_popup ().id_pressed .connect (on_popup_menu_press )
109111 menu_button_nd .visible = true
110112 default_button_sizes [menu_button_nd ] = menu_button_nd .size
111-
113+
112114 if counter_label_nd :
113- counter_label_nd .add_theme_font_size_override (' font_size' , Label_font_size )
115+ counter_label_nd .add_theme_font_size_override (" font_size" , Label_font_size / 1.5 )
114116 if label_line_edit_nd :
115- label_line_edit_nd .add_theme_font_size_override ('font_size' , Label_font_size )
117+ # Prevent overdraw when the rect size is small
118+ label_line_edit_nd .add_theme_font_size_override ("font_size" , Label_font_size / 2 )
116119 if alt_text_label_nd :
117- alt_text_label_nd .add_theme_font_size_override (' font_size' , Label_font_size )
118-
120+ alt_text_label_nd .add_theme_font_size_override (" font_size" , Label_font_size / 2 )
121+
119122 update_size ()
120123 set_active_interaction_flags (active_interaction_flags )
121124
@@ -124,58 +127,59 @@ func _set_default_textures():
124127 if ! clear_texture || ! delete_texture || ! new_texture || ! options_texture :
125128 var editor_theme = ThemeAdapter .editor_theme
126129 clear_texture = editor_theme .get_theme_item (Theme .DATA_TYPE_ICON , "Clear" , "EditorIcons" )
127- delete_texture = editor_theme .get_theme_item (Theme .DATA_TYPE_ICON , "ImportFail" , "EditorIcons" )
130+ delete_texture = editor_theme .get_theme_item (
131+ Theme .DATA_TYPE_ICON , "ImportFail" , "EditorIcons"
132+ )
128133 new_texture = editor_theme .get_theme_item (Theme .DATA_TYPE_ICON , "Add" , "EditorIcons" )
129- options_texture = editor_theme .get_theme_item (Theme .DATA_TYPE_ICON , "CodeFoldDownArrow" , "EditorIcons" )
134+ options_texture = editor_theme .get_theme_item (
135+ Theme .DATA_TYPE_ICON , "CodeFoldDownArrow" , "EditorIcons"
136+ )
130137 if has_node ("% MenuButton" ):
131138 % MenuButton .icon = options_texture
132139
133140
134-
135-
136141# -------------------------------------------------------------------------------
137142# Resizing
138143# -------------------------------------------------------------------------------
139144
140145
141- func set_thumb_size (val :int ):
146+ func set_thumb_size (val : int ):
142147 thumb_size = val
143148 update_size ()
144149
145150
146151func update_size ():
147- if ! is_node_ready (): return
148-
152+ if ! is_node_ready ():
153+ return
154+
149155 var thumb_rect = Vector2 (thumb_size , thumb_size )
150156 custom_minimum_size = thumb_rect
151- size = thumb_rect
152157
153158
154- func set_counter_val (val :int ):
159+ func set_counter_val (val : int ):
155160 if ! is_node_ready ():
156161 await ready
157- if ! counter_label_nd : return
162+ if ! counter_label_nd :
163+ return
158164 counter_label_nd .text = str (val )
159165
160166
161-
162-
163167# -------------------------------------------------------------------------------
164168# Interaction flags
165169# -------------------------------------------------------------------------------
166170
167171
168- func set_active_interaction_flags (flags :Array ):
172+ func set_active_interaction_flags (flags : Array ):
169173 var ownFlagsCopy = active_interaction_flags .duplicate ()
170174 var flagsCopy = flags .duplicate ()
171-
175+
172176 for flag in ownFlagsCopy :
173177 set_interaction_flag (flag , false )
174178 for flag in flagsCopy :
175179 set_interaction_flag (flag , true )
176180
177181
178- func set_interaction_flag (flag :int , state :bool ):
182+ func set_interaction_flag (flag : int , state : bool ):
179183 if state :
180184 if ! active_interaction_flags .has (flag ):
181185 active_interaction_flags .append (flag )
@@ -185,26 +189,30 @@ func set_interaction_flag(flag:int, state:bool):
185189 enable_features_to_flag (flag , state )
186190
187191
188- func enable_features_to_flag (flag :int , state :bool ):
192+ func enable_features_to_flag (flag : int , state : bool ):
189193 if is_node_ready ():
190194 match flag :
191195 InteractionFlags .CHECK :
192196 check_box_nd .visible = state
193197 InteractionFlags .CLEAR :
194198 if state :
195- menu_button_nd .get_popup ().remove_item (menu_button_nd .get_popup ().get_item_index (0 ))
196- menu_button_nd .get_popup ().add_icon_item (clear_texture , 'Clear' , 0 )
199+ menu_button_nd .get_popup ().remove_item (
200+ menu_button_nd .get_popup ().get_item_index (0 )
201+ )
202+ menu_button_nd .get_popup ().add_icon_item (clear_texture , "Clear" , 0 )
197203 InteractionFlags .DELETE :
198204 if state :
199- menu_button_nd .get_popup ().remove_item (menu_button_nd .get_popup ().get_item_index (1 ))
200- menu_button_nd .get_popup ().add_icon_item (delete_texture , 'Delete' , 1 )
205+ menu_button_nd .get_popup ().remove_item (
206+ menu_button_nd .get_popup ().get_item_index (1 )
207+ )
208+ menu_button_nd .get_popup ().add_icon_item (delete_texture , "Delete" , 1 )
201209 InteractionFlags .SHOW_COUNT :
202210 counter_label_nd .visible = state
203211 InteractionFlags .EDIT_LABEL :
204212 label_line_container_nd .visible = state
205213
206214
207- func set_features_val_to_flag (flag :int , val ):
215+ func set_features_val_to_flag (flag : int , val ):
208216 if is_node_ready ():
209217 match flag :
210218 InteractionFlags .PRESS :
@@ -220,57 +228,71 @@ func on_set_dialog():
220228 if active_interaction_flags .has (InteractionFlags .SET_DIALOG ):
221229 requested_set_dialog .emit ()
222230
231+
223232func on_set_drag (path ):
224233 if active_interaction_flags .has (InteractionFlags .SET_DRAG ):
225234 requested_set_drag .emit (path )
226235
236+
227237func on_press ():
228238 if active_interaction_flags .has (InteractionFlags .PRESS ):
229239 requested_press .emit ()
230240
241+
231242func on_check ():
232243 if active_interaction_flags .has (InteractionFlags .CHECK ):
233244 requested_check .emit (check_box_nd .button_pressed )
234245
246+
235247func on_label_edit (label_text : String ):
236248 if active_interaction_flags .has (InteractionFlags .EDIT_LABEL ):
237249 requested_label_edit .emit (label_text )
238250
251+
239252func on_popup_menu_press (id : int ):
240253 match id :
241254 0 :
242255 call_deferred ("on_clear" )
243256 1 :
244257 call_deferred ("on_delete" )
245258
259+
246260func on_clear ():
247261 if active_interaction_flags .has (InteractionFlags .CLEAR ):
248262 requested_clear .emit ()
249263
264+
250265func on_delete ():
251266 if active_interaction_flags .has (InteractionFlags .DELETE ):
252267 requested_delete .emit ()
253268
254269
255-
256-
257270# -------------------------------------------------------------------------------
258271# Thumbnail itself and other visuals
259272# -------------------------------------------------------------------------------
260273
261274
262- func set_thumbnail (texture :Texture2D ):
275+ func trim_text (text : String , length : int = 8 ) -> String :
276+ if text .length () <= length :
277+ return text
278+
279+ return text .substr (0 , length - 3 ) + "..."
280+
281+
282+ func set_thumbnail (texture : Texture2D ):
263283 texture_rect_nd .visible = true
264284 alt_text_label_nd .visible = false
265-
285+
266286 texture_rect_nd .texture = texture
267287 alt_text_label_nd .text = ""
268288
269289
270- func set_alt_text (alt_text :String ):
271- if ! is_instance_valid (alt_text_label_nd ) || ! is_instance_valid (texture_rect_nd ): return
290+ func set_alt_text (alt_text : String ):
291+ if ! is_instance_valid (alt_text_label_nd ) || ! is_instance_valid (texture_rect_nd ):
292+ return
293+
272294 alt_text_label_nd .visible = true
273295 texture_rect_nd .visible = false
274-
275- alt_text_label_nd .text = alt_text
296+
297+ alt_text_label_nd .text = trim_text ( alt_text )
276298 texture_rect_nd .texture = null
0 commit comments