22## Label for input controls
33class_name FormLabel extends Label
44
5-
65## Input control to label
76@export var input : Control :
87 set (new_val ):
9- if new_val == null || Form .is_input (new_val ):
8+ if new_val == null || Form .is_input (new_val ):
109 if input != null :
1110 input .gui_input .disconnect (_on_gui_input )
1211 input = new_val
1312 mode = mode # run setter
1413 indicate_required ()
15- if validate_on_input && input != null :
14+ if validate_on_input && input != null :
1615 input .gui_input .connect (_on_gui_input )
1716 else :
18- printerr (get_class (),": input must be a input button or input field" )
17+ printerr (get_class (), ": input must be a input button or input field" )
1918## "Input value must not be empty"
2019@export var input_required := false :
2120 set (new_val ):
@@ -33,8 +32,8 @@ class_name FormLabel extends Label
3332## The style to apply to the input when valid
3433var valid_style : StyleBox :
3534 get :
36- if input && _valid_style == null :
37- _valid_style = input .get_theme_stylebox ("normal" )
35+ if input && _valid_style == null :
36+ _valid_style = input .get_theme_stylebox ("normal" )
3837 return _valid_style
3938## Internal storage for valid_style
4039var _valid_style
@@ -58,7 +57,7 @@ enum Mode {
5857 if new_val == null :
5958 new_val = Mode .SEPARATE
6059 mode = new_val
61-
60+
6261 if mode == Mode .SEPARATE :
6362 visible = true
6463 else :
@@ -73,15 +72,14 @@ enum Mode {
7372 input [prop ] = ""
7473 found = true
7574 break
76- if ! found && mode == Mode .IN_INPUT :
75+ if ! found && mode == Mode .IN_INPUT :
7776 push_error ("Input " , input .get_instance_id (), " has no placeholder_text or text property" )
7877 mode = Mode .SEPARATE
7978 visible = true
8079
81-
8280## Sets the label text to the input's name if it is empty and runs necessary setters
8381func _enter_tree ():
84- if input != null && text in [null , "" ]:
82+ if input != null && text in [null , "" ]:
8583 text = input .name
8684 if ! visibility_changed .is_connected (update_display_mode ):
8785 visibility_changed .connect (update_display_mode )
@@ -91,9 +89,9 @@ func _enter_tree():
9189## If the label is not visible and the mode is Mode.SEPARATE, the mode is set to Mode.HIDDEN.
9290## Else the mode setter is run (mode = mode).
9391func update_display_mode ():
94- if visible && (mode == Mode .IN_INPUT || mode == Mode .HIDDEN ):
92+ if visible && (mode == Mode .IN_INPUT || mode == Mode .HIDDEN ):
9593 mode = Mode .SEPARATE
96- elif ! visible && mode == Mode .SEPARATE :
94+ elif ! visible && mode == Mode .SEPARATE :
9795 mode = Mode .HIDDEN
9896 else :
9997 # run setter
@@ -102,7 +100,7 @@ func update_display_mode():
102100## Add or remove the required_hint if input_required
103101func indicate_required ():
104102 # if * needed but not present
105- if input_required && required_hint not in ["" , null ] && ! text .ends_with (required_hint ):
103+ if input_required && required_hint not in ["" , null ]&& ! text .ends_with (required_hint ):
106104 # add
107105 text += required_hint
108106 # if * present but not needed
@@ -114,16 +112,16 @@ func indicate_required():
114112## Change style based on validity and return validity or default if input is not validatable
115113func indicate_validity (
116114 ## the default value to return if input is not validatable
117- default : = true
115+ default : = true
118116) -> bool :
119117 var valid = default
120118 # no input = not validatable -> valid = default
121119 if input :
122120 var broken_rules := {}
123121 var value = Protocol .new ().get_value (input )
124- var input_has_not_null_validator = has_property (input , "validator" ) && input .validator != null
125-
126- if input_required && (value == null || ((value is String || value is StringName ) && value == "" )):
122+ var input_has_not_null_validator = has_property (input , "validator" )&& input .validator != null
123+
124+ if input_required && (value == null || ((value is String || value is StringName )&& value == "" )):
127125 # input is required but empty -> valid = false
128126 broken_rules ["required" ] = true
129127 valid = false
@@ -135,7 +133,7 @@ func indicate_validity(
135133 broken_rules = input .validator .broken_rules
136134 else : # Has a text value or doesn't have to be true, has no validation rules. -> valid = true
137135 valid = true
138-
136+
139137 if ! valid :
140138 print ("Input " , input .get_instance_id (), " breaks the following rule(s):" )
141139 print (broken_rules )
@@ -155,18 +153,18 @@ func indicate_validity(
155153 return valid
156154
157155## Return validity of "Subject has property_name and it is not a method"
158- func has_property (subject :Object , property_name : StringName ) -> bool :
159- return property_name in subject && ! subject .has_method (property_name )
156+ func has_property (subject : Object , property_name : StringName ) -> bool :
157+ return property_name in subject && ! subject .has_method (property_name )
160158
161159## Indicate validity on GUI input if event is relevant and validate_on_input
162160func _on_gui_input (event : InputEvent ):
163- if validate_on_input && ! (event is InputEventMouseMotion ) && Form .is_input (input ) && (
164- event is InputEventMouseButton && event .button_index == MOUSE_BUTTON_LEFT && ! event .pressed && ( # left click release
161+ if validate_on_input && ! (event is InputEventMouseMotion )&& Form .is_input (input )&& (
162+ event is InputEventMouseButton && event .button_index == MOUSE_BUTTON_LEFT && ! event .pressed && ( # left click release
165163 input is BaseButton
166- || input is Slider
167- || input is SpinBox
168- || input is GraphEdit
164+ || input is Slider
165+ || input is SpinBox
166+ || input is GraphEdit
169167 )
170- || event is InputEventKey
168+ || event is InputEventKey
171169 ):
172170 indicate_validity .call_deferred () # ensure value is updated before validation
0 commit comments