Skip to content

Commit 028fa10

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 28f9541 commit 028fa10

File tree

4 files changed

+69
-44
lines changed

4 files changed

+69
-44
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: 27 additions & 23 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 b: BlockDefinition = BlockDefinition.new()
127+
b.name = &"simplecharacter_move"
128+
b.target_node_class = _class_name
129+
b.category = "Input"
130+
b.type = Types.BlockType.STATEMENT
131+
b.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)'
134+
b.code_template = 'move_with_player_buttons("{player}", "{kind}", delta)'
132135
b.defaults = {
133136
"player": OptionData.new(["player_1", "player_2"]),
134137
"kind": OptionData.new(["top-down", "platformer", "spaceship"]),
135138
}
136-
b.category = "Input"
137139
block_list.append(b)
138140

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
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: 18 additions & 15 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]
104+
var b: BlockDefinition = BlockDefinition.new()
105+
b.name = &"simplescoring_set_score_player_%s" % player
106+
b.target_node_class = _class_name
108107
b.category = "Info | Score"
108+
b.type = Types.BlockType.STATEMENT
109+
b.display_template = "Set player %s score to {score: INT}" % player
110+
b.code_template = "score_%s = {score}" % _POSITIONS_FOR_PLAYER[player]
109111
block_list.append(b)
110112

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]
113+
b = BlockDefinition.new()
114+
b.name = &"simplescoring_change_score_player_%s" % player
115+
b.target_node_class = _class_name
116116
b.category = "Info | Score"
117+
b.type = Types.BlockType.STATEMENT
118+
b.display_template = "Change player %s score by {score: INT}" % player
119+
b.code_template = "score_%s += {score}" % _POSITIONS_FOR_PLAYER[player]
117120
block_list.append(b)
118121

119-
return block_list
122+
BlocksCatalog.add_custom_blocks(_class_name, block_list)

addons/block_code/ui/picker/picker.gd

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,20 +28,20 @@ func block_script_selected(block_script: BlockScriptSerialization):
2828
var blocks_to_add: Array[Block] = []
2929
var categories_to_add: Array[BlockCategory] = []
3030

31-
# By default, assume the class is built-in.
3231
var parent_class: String = block_script.script_inherits
32+
blocks_to_add.append_array(CategoryFactory.get_inherited_blocks(parent_class))
33+
3334
for class_dict in ProjectSettings.get_global_class_list():
3435
if class_dict.class == block_script.script_inherits:
3536
var script = load(class_dict.path)
3637
if script.has_method("get_custom_categories"):
3738
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())
39+
if script.has_method("setup_custom_blocks"):
40+
script.setup_custom_blocks()
41+
var builtin_class = script.get_instance_base_type()
42+
blocks_to_add.append_array(CategoryFactory.get_inherited_blocks(builtin_class))
4143
break
4244

43-
blocks_to_add.append_array(CategoryFactory.get_inherited_blocks(parent_class))
44-
4545
init_picker(blocks_to_add, categories_to_add)
4646
reload_variables(block_script.variables)
4747

0 commit comments

Comments
 (0)