@@ -8,8 +8,7 @@ func _init() -> void:
88 theme_type_variation = "OutlinedPanel"
99
1010
11- static func create_shortcut_button (action : String , disabled := false ,
12- custom_text := "" , custom_icon : Texture2D = null ) -> Button :
11+ static func create_shortcut_button (action : String , disabled := false , custom_text := "" , custom_icon : Texture2D = null ) -> Button :
1312 if not InputMap .has_action (action ):
1413 push_error ("Non-existent shortcut was passed." )
1514 return
@@ -26,11 +25,11 @@ custom_text := "", custom_icon: Texture2D = null) -> Button:
2625 var shortcut_obj := Shortcut .new ()
2726 shortcut_obj .events = shortcut_events
2827 btn .shortcut = shortcut_obj
28+ btn .shortcut_in_tooltip = false
2929 btn .shortcut_feedback = false
3030 return btn
3131
32- static func create_shortcut_button_without_icon (action : String , disabled := false ,
33- custom_text := "" ) -> Button :
32+ static func create_shortcut_button_without_icon (action : String , disabled := false , custom_text := "" ) -> Button :
3433 if not InputMap .has_action (action ):
3534 push_error ("Non-existent shortcut was passed." )
3635 return
@@ -45,147 +44,92 @@ custom_text := "") -> Button:
4544 var shortcut_obj := Shortcut .new ()
4645 shortcut_obj .events = shortcut_events
4746 btn .shortcut = shortcut_obj
47+ btn .shortcut_in_tooltip = false
4848 btn .shortcut_feedback = false
4949
5050 return btn
5151
52- static func create_button (text : String , press_callback : Callable , disabled := false ,
53- icon : Texture2D = null , dim_text := "" ) -> Button :
52+ static func create_button (text : String , press_callback : Callable , disabled := false , icon : Texture2D = null , dim_text := "" ) -> Button :
5453 # Create main button.
5554 var main_button := Button .new ()
55+ main_button .theme_type_variation = "ContextButton"
56+ main_button .focus_mode = Control .FOCUS_NONE
57+ if disabled :
58+ main_button .disabled = true
59+ else :
60+ main_button .mouse_default_cursor_shape = Control .CURSOR_POINTING_HAND
61+
5662 main_button .text = text
5763 if is_instance_valid (icon ):
5864 main_button .add_theme_constant_override ("icon_max_width" , 16 )
5965 main_button .icon = icon
60-
66+
67+ if press_callback .is_valid ():
68+ main_button .pressed .connect (press_callback )
69+ main_button .pressed .connect (HandlerGUI .remove_popup )
70+
6171 if not dim_text .is_empty ():
62- # Add button with dim text.
63- var ret_button := Button .new ()
64- ret_button .theme_type_variation = "ContextButton"
65- ret_button .focus_mode = Control .FOCUS_NONE
66- ret_button .shortcut_in_tooltip = false
67- if disabled :
68- main_button .disabled = true
69- ret_button .disabled = true
70- else :
71- ret_button .mouse_default_cursor_shape = Control .CURSOR_POINTING_HAND
72-
73- const CONST_ARR : PackedStringArray = ["normal" , "hover" , "pressed" , "disabled" ]
74- main_button .begin_bulk_theme_override ()
75- for theme_type in CONST_ARR :
76- main_button .add_theme_stylebox_override (theme_type ,
77- main_button .get_theme_stylebox ("normal" , "ContextButton" ))
78- main_button .end_bulk_theme_override ()
72+ var font := main_button .get_theme_font ("font" )
73+ var font_size := main_button .get_theme_font_size ("font_size" )
74+ var dim_text_width := font .get_string_size (dim_text , HORIZONTAL_ALIGNMENT_RIGHT , - 1 , font_size ).x
7975
80- var internal_hbox := HBoxContainer .new ()
81- main_button .mouse_filter = Control .MOUSE_FILTER_IGNORE # Unpressable.
82- internal_hbox .add_theme_constant_override ("separation" , 12 )
83- main_button .add_theme_color_override ("icon_normal_color" ,
84- ret_button .get_theme_color ("icon_normal_color" , "ContextButton" ))
85- var label_margin := MarginContainer .new ()
86- label_margin .add_theme_constant_override ("margin_right" ,
87- int (ret_button .get_theme_stylebox ("normal" ).content_margin_right ))
88- var label := Label .new ()
89- label .text = dim_text
90- label .horizontal_alignment = HORIZONTAL_ALIGNMENT_RIGHT
9176 var shortcut_text_color := ThemeUtils .subtle_text_color
9277 if disabled :
9378 shortcut_text_color .a *= 0.75
94- label .add_theme_color_override ("font_color" , shortcut_text_color )
95- label .add_theme_font_size_override ("font_size" ,
96- main_button .get_theme_font_size ("font_size" ))
9779
98- ret_button .size_flags_horizontal = Control .SIZE_EXPAND_FILL
99- internal_hbox .set_anchors_and_offsets_preset (Control .PRESET_TOP_WIDE )
100- label_margin .size_flags_horizontal = Control .SIZE_EXPAND_FILL
101- label .size_flags_horizontal = Control .SIZE_FILL
102- internal_hbox .add_child (main_button )
103- label_margin .add_child (label )
104- internal_hbox .add_child (label_margin )
105- ret_button .add_child (internal_hbox )
106- ret_button .pressed .connect (press_callback )
107- ret_button .pressed .connect (HandlerGUI .remove_popup )
108- return ret_button
109- # Finish setting up the main button and return it if there's no shortcut.
110- main_button .theme_type_variation = "ContextButton"
111- main_button .focus_mode = Control .FOCUS_NONE
112- if disabled :
113- main_button .disabled = true
114- else :
115- main_button .mouse_default_cursor_shape = Control .CURSOR_POINTING_HAND
116- if press_callback .is_valid ():
117- main_button .pressed .connect (press_callback )
118- main_button .pressed .connect (HandlerGUI .remove_popup )
80+ var CONST_ARR : PackedStringArray = ["normal" , "hover" , "pressed" ]
81+ for theme_style in CONST_ARR :
82+ var sb := main_button .get_theme_stylebox (theme_style , "ContextButton" ).duplicate ()
83+ sb .content_margin_right += dim_text_width + 8
84+ main_button .add_theme_stylebox_override (theme_style , sb )
85+
86+ var on_main_button_draw := func () -> void :
87+ var sb := ThemeDB .get_default_theme ().get_stylebox ("normal" , "ContextButton" )
88+ font .draw_string (main_button .get_canvas_item (), Vector2 (0 , sb .content_margin_top + font .get_ascent (font_size )), dim_text ,
89+ HORIZONTAL_ALIGNMENT_RIGHT , main_button .size .x - sb .content_margin_right , font_size , shortcut_text_color )
90+ main_button .draw .connect (on_main_button_draw )
91+
11992 return main_button
12093
12194
122- static func create_shortcut_checkbox (action : String , start_pressed : bool ,
123- custom_text := "" ) -> CheckBox :
95+ static func create_shortcut_checkbox (action : String , start_pressed : bool ) -> CheckBox :
12496 if not InputMap .has_action (action ):
12597 push_error ("Non-existent shortcut was passed." )
12698 return
12799
128- if custom_text .is_empty ():
129- custom_text = TranslationUtils .get_action_description (action , true )
130-
131- return create_checkbox (custom_text , HandlerGUI .throw_action_event .bind (action ),
100+ return create_checkbox (TranslationUtils .get_action_description (action , true ),
101+ HandlerGUI .throw_action_event .bind (action ),
132102 start_pressed , ShortcutUtils .get_action_showcase_text (action ))
133103
134104static func create_checkbox (text : String , toggle_action : Callable ,
135105start_pressed : bool , dim_text := "" ) -> CheckBox :
136106 # Create main checkbox.
137107 var checkbox := CheckBox .new ()
108+ checkbox .focus_mode = Control .FOCUS_NONE
109+ checkbox .mouse_default_cursor_shape = Control .CURSOR_POINTING_HAND
110+
138111 checkbox .text = text
139112 checkbox .button_pressed = start_pressed
140113 checkbox .toggled .connect (toggle_action .unbind (1 ))
141114
142115 if not dim_text .is_empty ():
143- # Add button with dim text.
144- var ret_button := Button .new ()
145- ret_button .theme_type_variation = "ContextButton"
146- ret_button .focus_mode = Control .FOCUS_NONE
147- ret_button .mouse_default_cursor_shape = Control .CURSOR_POINTING_HAND
148- ret_button .shortcut_in_tooltip = false
149-
150- checkbox .begin_bulk_theme_override ()
151- const CONST_ARR : PackedStringArray = ["normal" , "pressed" ]
152- for theme_type in CONST_ARR :
153- checkbox .add_theme_stylebox_override (theme_type ,
154- checkbox .get_theme_stylebox ("normal" , "ContextButton" ))
155- checkbox .end_bulk_theme_override ()
156-
157- var internal_hbox := HBoxContainer .new ()
158- checkbox .mouse_filter = Control .MOUSE_FILTER_IGNORE # Unpressable.
159- internal_hbox .add_theme_constant_override ("separation" , 12 )
160- checkbox .add_theme_color_override ("icon_normal_color" ,
161- ret_button .get_theme_color ("icon_normal_color" , "ContextButton" ))
162- var label_margin := MarginContainer .new ()
163- label_margin .add_theme_constant_override ("margin_right" ,
164- int (ret_button .get_theme_stylebox ("normal" ).content_margin_right ))
165- var label := Label .new ()
166- label .text = dim_text
167- label .horizontal_alignment = HORIZONTAL_ALIGNMENT_RIGHT
116+ var font := checkbox .get_theme_font ("font" )
117+ var font_size := checkbox .get_theme_font_size ("font_size" )
118+ var dim_text_width := font .get_string_size (dim_text , HORIZONTAL_ALIGNMENT_RIGHT , - 1 , font_size ).x
168119 var shortcut_text_color := ThemeUtils .subtle_text_color
169- # if disabled:
170- # shortcut_text_color.a *= 0.75
171- label .add_theme_color_override ("font_color" , shortcut_text_color )
172- label .add_theme_font_size_override ("font_size" ,
173- checkbox .get_theme_font_size ("font_size" ))
174120
175- ret_button .size_flags_horizontal = Control .SIZE_EXPAND_FILL
176- internal_hbox .set_anchors_and_offsets_preset (Control .PRESET_TOP_WIDE )
177- label_margin .size_flags_horizontal = Control .SIZE_EXPAND_FILL
178- label .size_flags_horizontal = Control .SIZE_FILL
179- internal_hbox .add_child (checkbox )
180- label_margin .add_child (label )
181- internal_hbox .add_child (label_margin )
182- ret_button .add_child (internal_hbox )
183- ret_button .pressed .connect (
184- func () -> void : checkbox .button_pressed = not checkbox .button_pressed )
185- return ret_button
186- # Finish setting up the checkbox and return it if there's no shortcut.
187- checkbox .mouse_default_cursor_shape = Control .CURSOR_POINTING_HAND
188- checkbox .focus_mode = Control .FOCUS_NONE
121+ var CONST_ARR : PackedStringArray = ["normal" , "hover" , "pressed" ]
122+ for theme_style in CONST_ARR :
123+ var sb := checkbox .get_theme_stylebox (theme_style , "CheckBox" ).duplicate ()
124+ sb .content_margin_right += dim_text_width + 8
125+ checkbox .add_theme_stylebox_override (theme_style , sb )
126+
127+ var on_shortcut_draw := func () -> void :
128+ var sb := ThemeDB .get_default_theme ().get_stylebox ("normal" , "CheckBox" )
129+ font .draw_string (checkbox .get_canvas_item (), Vector2 (0 , sb .content_margin_top + font .get_ascent (font_size )), dim_text ,
130+ HORIZONTAL_ALIGNMENT_RIGHT , checkbox .size .x - sb .content_margin_right , font_size , shortcut_text_color )
131+ checkbox .draw .connect (on_shortcut_draw )
132+
189133 return checkbox
190134
191135func _setup_button (btn : Button , align_left : bool ) -> Button :
0 commit comments