Skip to content

Commit ae97597

Browse files
authored
Merge pull request #160 from endlessm/text-validation
Text validation
2 parents b5d4ab1 + ce6f0a1 commit ae97597

File tree

2 files changed

+101
-52
lines changed

2 files changed

+101
-52
lines changed

addons/block_code/ui/blocks/utilities/parameter_input/parameter_input.gd

Lines changed: 30 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,11 @@ var option: bool = false
3333
@onready var _bool_input_option := %BoolInputOption
3434

3535
# Used to submit the text when losing focus:
36-
var _last_lineedit_submitted_text: String
37-
var _last_x_lineedit_submitted_text: String
38-
var _last_y_lineedit_submitted_text: String
36+
@onready var _last_submitted_text = {
37+
_line_edit: _line_edit.text,
38+
_x_line_edit: _x_line_edit.text,
39+
_y_line_edit: _y_line_edit.text,
40+
}
3941

4042

4143
func set_raw_input(raw_input):
@@ -99,10 +101,6 @@ func _ready():
99101
snap_point.block_type = block_type
100102
snap_point.variant_type = variant_type
101103

102-
_last_lineedit_submitted_text = _line_edit.text
103-
_last_x_lineedit_submitted_text = _x_line_edit.text
104-
_last_y_lineedit_submitted_text = _y_line_edit.text
105-
106104
_update_visible_input()
107105

108106

@@ -136,34 +134,44 @@ func get_string() -> String:
136134
return "%s" % input
137135

138136

139-
func _on_line_edit_text_submitted(new_text):
140-
_last_lineedit_submitted_text = new_text
137+
func _validate_and_submit_edit_text(line_edit: Node, type: Variant.Type):
138+
if _last_submitted_text[line_edit] == line_edit.text:
139+
return
140+
match type:
141+
TYPE_FLOAT:
142+
if not line_edit.text.is_valid_float():
143+
line_edit.text = _last_submitted_text[line_edit]
144+
return
145+
TYPE_INT:
146+
if not line_edit.text.is_valid_int():
147+
line_edit.text = _last_submitted_text[line_edit]
148+
return
149+
_last_submitted_text[line_edit] = line_edit.text
141150
modified.emit()
142151

143152

153+
func _on_line_edit_text_submitted(_new_text):
154+
_validate_and_submit_edit_text(_line_edit, variant_type)
155+
156+
144157
func _on_line_edit_focus_exited():
145-
if _last_lineedit_submitted_text != _line_edit.text:
146-
modified.emit()
158+
_validate_and_submit_edit_text(_line_edit, variant_type)
147159

148160

149-
func _on_x_line_edit_text_submitted(new_text):
150-
_last_x_lineedit_submitted_text = new_text
151-
modified.emit()
161+
func _on_x_line_edit_text_submitted(_new_text):
162+
_validate_and_submit_edit_text(_x_line_edit, TYPE_FLOAT)
152163

153164

154165
func _on_x_line_edit_focus_exited():
155-
if _last_x_lineedit_submitted_text != _x_line_edit.text:
156-
modified.emit()
166+
_validate_and_submit_edit_text(_x_line_edit, TYPE_FLOAT)
157167

158168

159-
func _on_y_line_edit_text_submitted(new_text):
160-
_last_y_lineedit_submitted_text = new_text
161-
modified.emit()
169+
func _on_y_line_edit_text_submitted(_new_text):
170+
_validate_and_submit_edit_text(_y_line_edit, TYPE_FLOAT)
162171

163172

164173
func _on_y_line_edit_focus_exited():
165-
if _last_y_lineedit_submitted_text != _y_line_edit.text:
166-
modified.emit()
174+
_validate_and_submit_edit_text(_y_line_edit, TYPE_FLOAT)
167175

168176

169177
func _update_visible_input():
@@ -185,15 +193,11 @@ func _update_visible_input():
185193

186194
func _switch_input(node: Node):
187195
for c in _input_switcher.get_children():
188-
c.visible = false
189-
190-
if node:
191-
node.visible = true
196+
c.visible = c == node
192197

193198

194199
func _on_color_input_color_changed(color):
195200
_update_panel_bg_color(color)
196-
197201
modified.emit()
198202

199203

addons/block_code/ui/picker/categories/category_factory.gd

Lines changed: 71 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -362,6 +362,7 @@ static func get_general_blocks() -> Array[Block]:
362362
b.variant_type = TYPE_VECTOR2
363363
b.block_format = "Vector2 x: {x: FLOAT} y: {y: FLOAT}"
364364
b.statement = "Vector2({x}, {y})"
365+
b.defaults = {"x": "0", "y": "0"}
365366
b.category = "Variables"
366367
block_list.append(b)
367368

@@ -373,6 +374,7 @@ static func get_general_blocks() -> Array[Block]:
373374
b.variant_type = TYPE_INT
374375
b.block_format = "{a: INT} + {b: INT}"
375376
b.statement = "({a} + {b})"
377+
b.defaults = {"a": "1", "b": "1"}
376378
b.category = "Math"
377379
block_list.append(b)
378380

@@ -381,6 +383,7 @@ static func get_general_blocks() -> Array[Block]:
381383
b.variant_type = TYPE_INT
382384
b.block_format = "{a: INT} - {b: INT}"
383385
b.statement = "({a} - {b})"
386+
b.defaults = {"a": "1", "b": "1"}
384387
b.category = "Math"
385388
block_list.append(b)
386389

@@ -389,6 +392,7 @@ static func get_general_blocks() -> Array[Block]:
389392
b.variant_type = TYPE_INT
390393
b.block_format = "{a: INT} * {b: INT}"
391394
b.statement = "({a} * {b})"
395+
b.defaults = {"a": "1", "b": "1"}
392396
b.category = "Math"
393397
block_list.append(b)
394398

@@ -397,6 +401,7 @@ static func get_general_blocks() -> Array[Block]:
397401
b.variant_type = TYPE_INT
398402
b.block_format = "{a: INT} / {b: INT}"
399403
b.statement = "({a} / {b})"
404+
b.defaults = {"a": "1", "b": "1"}
400405
b.category = "Math"
401406
block_list.append(b)
402407

@@ -405,6 +410,7 @@ static func get_general_blocks() -> Array[Block]:
405410
b.variant_type = TYPE_INT
406411
b.block_format = "{base: INT} ^ {exp: INT}"
407412
b.statement = "(pow({base}, {exp}))"
413+
b.defaults = {"base": "1", "exp": "1"}
408414
b.category = "Math"
409415
block_list.append(b)
410416

@@ -413,14 +419,14 @@ static func get_general_blocks() -> Array[Block]:
413419

414420
b = BLOCKS["control_block"].instantiate()
415421
b.block_name = "if"
416-
b.block_formats = ["if {condition: BOOL}"]
422+
b.block_formats = ["if {condition: BOOL}"]
417423
b.statements = ["if {condition}:"]
418424
b.category = "Logic | Conditionals"
419425
block_list.append(b)
420426

421427
b = BLOCKS["control_block"].instantiate()
422428
b.block_name = "if_else"
423-
b.block_formats = ["if {condition: BOOL}", "else"]
429+
b.block_formats = ["if {condition: BOOL}", "else"]
424430
b.statements = ["if {condition}:", "else:"]
425431
b.category = "Logic | Conditionals"
426432
block_list.append(b)
@@ -430,7 +436,11 @@ static func get_general_blocks() -> Array[Block]:
430436
b.variant_type = TYPE_BOOL
431437
b.block_format = "{int1: INT} {op: OPTION} {int2: INT}"
432438
b.statement = "({int1} {op} {int2})"
433-
b.defaults = {"op": OptionData.new(["==", ">", "<", ">=", "<=", "!="])}
439+
b.defaults = {
440+
"op": OptionData.new(["==", ">", "<", ">=", "<=", "!="]),
441+
"int1": "1",
442+
"int2": "1",
443+
}
434444
b.category = "Logic | Comparison"
435445
block_list.append(b)
436446

@@ -529,22 +539,41 @@ static func property_to_blocklist(property: Dictionary) -> Array[Block]:
529539

530540
var variant_type = property.type
531541

542+
const FALLBACK_SET_FOR_TYPE = {
543+
TYPE_INT: "0",
544+
TYPE_FLOAT: "0",
545+
TYPE_VECTOR2: "0, 0",
546+
TYPE_COLOR: "DARK_ORANGE",
547+
}
548+
549+
const FALLBACK_CHANGE_FOR_TYPE = {
550+
TYPE_INT: "1",
551+
TYPE_FLOAT: "1",
552+
TYPE_VECTOR2: "1, 1",
553+
TYPE_COLOR: "DARK_ORANGE",
554+
}
555+
532556
if variant_type:
533557
var type_string: String = Types.VARIANT_TYPE_TO_STRING[variant_type]
534558

535559
var b = BLOCKS["statement_block"].instantiate()
536560
b.block_name = "set_prop_%s" % property.name
537561
b.block_format = "Set %s to {value: %s}" % [property.name.capitalize(), type_string]
538562
b.statement = "%s = {value}" % property.name
563+
var default_set = property.get("default_set", FALLBACK_SET_FOR_TYPE.get(variant_type, ""))
564+
b.defaults = {"value": default_set}
539565
b.category = property.category
540566
block_list.append(b)
541567

542-
b = BLOCKS["statement_block"].instantiate()
543-
b.block_name = "change_prop_%s" % property.name
544-
b.block_format = "Change %s by {value: %s}" % [property.name.capitalize(), type_string]
545-
b.statement = "%s += {value}" % property.name
546-
b.category = property.category
547-
block_list.append(b)
568+
if property.get("has_change", true):
569+
b = BLOCKS["statement_block"].instantiate()
570+
b.block_name = "change_prop_%s" % property.name
571+
b.block_format = "Change %s by {value: %s}" % [property.name.capitalize(), type_string]
572+
b.statement = "%s += {value}" % property.name
573+
var default_change = property.get("default_change", FALLBACK_CHANGE_FOR_TYPE[variant_type])
574+
b.defaults = {"value": default_change}
575+
b.category = property.category
576+
block_list.append(b)
548577

549578
b = BLOCKS["parameter_block"].instantiate()
550579
b.block_name = "get_prop_%s" % property.name
@@ -565,7 +594,7 @@ static func blocks_from_property_list(property_list: Array, selected_props: Dict
565594
for prop in property_list:
566595
if selected_property == prop.name:
567596
found_prop = prop
568-
found_prop.category = selected_props[selected_property]
597+
found_prop.merge(selected_props[selected_property])
569598
break
570599
if found_prop:
571600
block_list.append_array(property_to_blocklist(found_prop))
@@ -593,23 +622,39 @@ static func get_built_in_blocks(_class_name: String) -> Array[Block]:
593622

594623
match _class_name:
595624
"Node2D":
596-
var b = BLOCKS["statement_block"].instantiate()
597-
b.block_name = "node2d_rotation"
598-
b.block_format = "Set Rotation Degrees {angle: FLOAT}"
599-
b.statement = "rotation_degrees = {angle}"
600-
b.category = "Transform | Rotation"
601-
block_list.append(b)
602-
603625
props = {
604-
"position": "Transform | Position",
605-
"rotation": "Transform | Rotation",
606-
"scale": "Transform | Scale",
626+
"position":
627+
{
628+
"category": "Transform | Position",
629+
"default_set": "100, 100",
630+
"default_change": "1, 1",
631+
},
632+
"rotation_degrees":
633+
{
634+
"category": "Transform | Rotation",
635+
"default_set": "45",
636+
"default_change": "1",
637+
},
638+
"scale":
639+
{
640+
"category": "Transform | Scale",
641+
"default_set": "2, 2",
642+
"default_change": "0.1, 0.1",
643+
},
607644
}
608645

609646
"CanvasItem":
610647
props = {
611-
"modulate": "Graphics | Modulate",
612-
"visible": "Graphics | Visibility",
648+
"modulate":
649+
{
650+
"category": "Graphics | Modulate",
651+
"has_change": false,
652+
},
653+
"visible":
654+
{
655+
"category": "Graphics | Visibility",
656+
"has_change": false,
657+
},
613658
}
614659

615660
"RigidBody2D":
@@ -650,9 +695,9 @@ static func get_built_in_blocks(_class_name: String) -> Array[Block]:
650695
block_list.append(b)
651696

652697
props = {
653-
"mass": "Physics | Mass",
654-
"linear_velocity": "Physics | Velocity",
655-
"angular_velocity": "Physics | Velocity",
698+
"mass": {"category": "Physics | Mass"},
699+
"linear_velocity": {"category": "Physics | Velocity"},
700+
"angular_velocity": {"category": "Physics | Velocity"},
656701
}
657702

658703
"AnimationPlayer":
@@ -754,7 +799,7 @@ static func get_built_in_blocks(_class_name: String) -> Array[Block]:
754799
block_list.append(b)
755800

756801
props = {
757-
"velocity": "Physics | Velocity",
802+
"velocity": {"category": "Physics | Velocity"},
758803
}
759804

760805
var prop_list = ClassDB.class_get_property_list(_class_name, true)

0 commit comments

Comments
 (0)