Skip to content

Commit 768de50

Browse files
committed
Simple nodes: Port custom blocks
Add a way in the blocks catalog to add custom blocks. Port SimpleScoring and SimpleCharacter to the new block definitions. Change the picker logic to instantiate these blocks. https://phabricator.endlessm.com/T35591
1 parent dd1e0e9 commit 768de50

File tree

4 files changed

+76
-54
lines changed

4 files changed

+76
-54
lines changed

addons/block_code/code_generation/blocks_catalog.gd

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,3 +188,21 @@ static func get_blocks_by_class(_class_name: String):
188188
return []
189189
var block_definitions = _by_class_name[_class_name] as Dictionary
190190
return block_definitions.values()
191+
192+
193+
static func add_custom_blocks(
194+
_class_name,
195+
block_definitions: Array[BlockDefinition] = [],
196+
property_list: Array[Dictionary] = [],
197+
property_settings: Dictionary = {},
198+
):
199+
setup()
200+
201+
if not _class_name in _by_class_name:
202+
_by_class_name[_class_name] = {}
203+
204+
for block_definition in block_definitions:
205+
_catalog[block_definition.name] = block_definition
206+
_by_class_name[_class_name][block_definition.name] = block_definition
207+
208+
_add_property_definitions(_class_name, property_list, property_settings)

addons/block_code/simple_nodes/simple_character/simple_character.gd

Lines changed: 30 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22
class_name SimpleCharacter
33
extends CharacterBody2D
44

5-
const CategoryFactory = preload("res://addons/block_code/ui/picker/categories/category_factory.gd")
5+
const BlockDefinition = preload("res://addons/block_code/code_generation/block_definition.gd")
6+
const BlocksCatalog = preload("res://addons/block_code/code_generation/blocks_catalog.gd")
67
const Types = preload("res://addons/block_code/types/types.gd")
78

89
@export var texture: Texture2D:
@@ -117,35 +118,38 @@ func move_with_player_buttons(player: String, kind: String, delta: float):
117118
move_and_slide()
118119

119120

120-
static func get_custom_blocks() -> Array[Block]:
121-
var b: Block
122-
var block_list: Array[Block] = []
121+
static func setup_custom_blocks():
122+
var _class_name = "SimpleCharacter"
123+
var block_list: Array[BlockDefinition] = []
123124

124125
# Movement
125-
b = CategoryFactory.BLOCKS["statement_block"].instantiate()
126-
b.block_name = "simplecharacter_move"
127-
b.block_type = Types.BlockType.STATEMENT
128-
b.block_format = "Move with {player: OPTION} buttons as {kind: OPTION}"
126+
var block_definition: BlockDefinition = BlockDefinition.new()
127+
block_definition.name = &"simplecharacter_move"
128+
block_definition.target_node_class = _class_name
129+
block_definition.category = "Input"
130+
block_definition.type = Types.BlockType.STATEMENT
131+
block_definition.display_template = "Move with {player: OPTION} buttons as {kind: OPTION}"
129132
# TODO: delta here is assumed to be the parameter name of
130133
# the _process or _physics_process method:
131-
b.statement = 'move_with_player_buttons("{player}", "{kind}", delta)'
132-
b.defaults = {
134+
block_definition.code_template = 'move_with_player_buttons("{player}", "{kind}", delta)'
135+
block_definition.defaults = {
133136
"player": OptionData.new(["player_1", "player_2"]),
134137
"kind": OptionData.new(["top-down", "platformer", "spaceship"]),
135138
}
136-
b.category = "Input"
137-
block_list.append(b)
138-
139-
var property_blocks = (
140-
CategoryFactory
141-
. property_to_blocklist(
142-
{
143-
"name": "speed",
144-
"type": TYPE_VECTOR2,
145-
"category": "Physics | Velocity",
146-
}
147-
)
148-
)
149-
block_list.append_array(property_blocks)
150-
151-
return block_list
139+
block_list.append(block_definition)
140+
141+
var property_list: Array[Dictionary] = [
142+
{
143+
"name": "speed",
144+
"type": TYPE_VECTOR2,
145+
},
146+
]
147+
148+
var property_settings = {
149+
"speed":
150+
{
151+
"category": "Physics | Velocity",
152+
},
153+
}
154+
155+
BlocksCatalog.add_custom_blocks(_class_name, block_list, property_list, property_settings)

addons/block_code/simple_nodes/simple_scoring/simple_scoring.gd

Lines changed: 24 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22
class_name SimpleScoring
33
extends CanvasLayer
44

5-
const CategoryFactory = preload("res://addons/block_code/ui/picker/categories/category_factory.gd")
5+
const BlockDefinition = preload("res://addons/block_code/code_generation/block_definition.gd")
6+
const BlocksCatalog = preload("res://addons/block_code/code_generation/blocks_catalog.gd")
67
const Types = preload("res://addons/block_code/types/types.gd")
78

89
@export var score_left: int:
@@ -95,25 +96,27 @@ func set_player_score(player: String, score: int):
9596
_score_labels[player].text = str(score)
9697

9798

98-
static func get_custom_blocks() -> Array[Block]:
99-
var b: Block
100-
var block_list: Array[Block] = []
99+
static func setup_custom_blocks():
100+
var _class_name = "SimpleScoring"
101+
var block_list: Array[BlockDefinition] = []
101102

102103
for player in _POSITIONS_FOR_PLAYER:
103-
b = CategoryFactory.BLOCKS["statement_block"].instantiate()
104-
b.block_name = "simplescoring_set_score"
105-
b.block_type = Types.BlockType.STATEMENT
106-
b.block_format = "Set player %s score to {score: INT}" % player
107-
b.statement = "score_%s = {score}" % _POSITIONS_FOR_PLAYER[player]
108-
b.category = "Info | Score"
109-
block_list.append(b)
110-
111-
b = CategoryFactory.BLOCKS["statement_block"].instantiate()
112-
b.block_name = "simplescoring_change_score"
113-
b.block_type = Types.BlockType.STATEMENT
114-
b.block_format = "Change player %s score by {score: INT}" % player
115-
b.statement = "score_%s += {score}" % _POSITIONS_FOR_PLAYER[player]
116-
b.category = "Info | Score"
117-
block_list.append(b)
118-
119-
return block_list
104+
var block_definition: BlockDefinition = BlockDefinition.new()
105+
block_definition.name = &"simplescoring_set_score_player_%s" % player
106+
block_definition.target_node_class = _class_name
107+
block_definition.category = "Info | Score"
108+
block_definition.type = Types.BlockType.STATEMENT
109+
block_definition.display_template = "Set player %s score to {score: INT}" % player
110+
block_definition.code_template = "score_%s = {score}" % _POSITIONS_FOR_PLAYER[player]
111+
block_list.append(block_definition)
112+
113+
block_definition = BlockDefinition.new()
114+
block_definition.name = &"simplescoring_change_score_player_%s" % player
115+
block_definition.target_node_class = _class_name
116+
block_definition.category = "Info | Score"
117+
block_definition.type = Types.BlockType.STATEMENT
118+
block_definition.display_template = "Change player %s score by {score: INT}" % player
119+
block_definition.code_template = "score_%s += {score}" % _POSITIONS_FOR_PLAYER[player]
120+
block_list.append(block_definition)
121+
122+
BlocksCatalog.add_custom_blocks(_class_name, block_list)

addons/block_code/ui/picker/picker.gd

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,22 +25,19 @@ func block_script_selected(block_script: BlockScriptSerialization):
2525
reset_picker()
2626
return
2727

28-
var blocks_to_add: Array[Block] = []
2928
var categories_to_add: Array[BlockCategory] = []
3029

31-
# By default, assume the class is built-in.
32-
var parent_class: String = block_script.script_inherits
3330
for class_dict in ProjectSettings.get_global_class_list():
3431
if class_dict.class == block_script.script_inherits:
3532
var script = load(class_dict.path)
3633
if script.has_method("get_custom_categories"):
3734
categories_to_add = script.get_custom_categories()
38-
if script.has_method("get_custom_blocks"):
39-
blocks_to_add = script.get_custom_blocks()
40-
parent_class = str(script.get_instance_base_type())
35+
if script.has_method("setup_custom_blocks"):
36+
script.setup_custom_blocks()
4137
break
4238

43-
blocks_to_add.append_array(CategoryFactory.get_inherited_blocks(parent_class))
39+
var blocks_to_add: Array[Block] = []
40+
blocks_to_add.append_array(CategoryFactory.get_inherited_blocks(block_script.script_inherits))
4441

4542
init_picker(blocks_to_add, categories_to_add)
4643
reload_variables(block_script.variables)

0 commit comments

Comments
 (0)