Skip to content

Commit ce6f0a1

Browse files
committed
CategoryFactory: Add defaults to all blocks
All possible blocks gain default values. So they can be used directly without having to type a value. And most importantly, so the script is not generated with syntax errors. The defaults are opinionated, they have to be. Eg. set scale to double size, change position so the node2d moves to the right-bottom. Also: remove "change by" block for modulate and visible properties. They don't make sense. https://phabricator.endlessm.com/T35562
1 parent 98fa3b3 commit ce6f0a1

File tree

1 file changed

+71
-19
lines changed

1 file changed

+71
-19
lines changed

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

Lines changed: 71 additions & 19 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))
@@ -594,15 +623,38 @@ static func get_built_in_blocks(_class_name: String) -> Array[Block]:
594623
match _class_name:
595624
"Node2D":
596625
props = {
597-
"position": "Transform | Position",
598-
"rotation_degrees": "Transform | Rotation",
599-
"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+
},
600644
}
601645

602646
"CanvasItem":
603647
props = {
604-
"modulate": "Graphics | Modulate",
605-
"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+
},
606658
}
607659

608660
"RigidBody2D":
@@ -643,9 +695,9 @@ static func get_built_in_blocks(_class_name: String) -> Array[Block]:
643695
block_list.append(b)
644696

645697
props = {
646-
"mass": "Physics | Mass",
647-
"linear_velocity": "Physics | Velocity",
648-
"angular_velocity": "Physics | Velocity",
698+
"mass": {"category": "Physics | Mass"},
699+
"linear_velocity": {"category": "Physics | Velocity"},
700+
"angular_velocity": {"category": "Physics | Velocity"},
649701
}
650702

651703
"AnimationPlayer":
@@ -747,7 +799,7 @@ static func get_built_in_blocks(_class_name: String) -> Array[Block]:
747799
block_list.append(b)
748800

749801
props = {
750-
"velocity": "Physics | Velocity",
802+
"velocity": {"category": "Physics | Velocity"},
751803
}
752804

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

0 commit comments

Comments
 (0)