Skip to content

Commit d3a1599

Browse files
committed
Move property setter, changer and getter to BlockDefinition
Create three constructor functions and use them from the catalog.
1 parent fdf00e3 commit d3a1599

File tree

2 files changed

+48
-41
lines changed

2 files changed

+48
-41
lines changed

addons/block_code/code_generation/block_definition.gd

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,3 +194,46 @@ static func _parse_parameter_format(parameter_format: String) -> Dictionary:
194194

195195
static func has_category(block_definition, category: String) -> bool:
196196
return block_definition.category == category
197+
198+
199+
static func new_property_setter(_class_name: String, property: Dictionary, category: String, default_value: Variant) -> Resource:
200+
var type_string: String = Types.VARIANT_TYPE_TO_STRING[property.type]
201+
return new(
202+
&"%s_set_%s" % [_class_name, property.name],
203+
_class_name,
204+
"Set the %s property" % property.name,
205+
category,
206+
Types.BlockType.STATEMENT,
207+
TYPE_NIL,
208+
"set %s to {value: %s}" % [property.name.capitalize().to_lower(), type_string],
209+
"%s = {value}" % property.name,
210+
{"value": default_value},
211+
)
212+
213+
214+
static func new_property_changer(_class_name: String, property: Dictionary, category: String, default_value: Variant) -> Resource:
215+
var type_string: String = Types.VARIANT_TYPE_TO_STRING[property.type]
216+
return new(
217+
&"%s_change_%s" % [_class_name, property.name],
218+
_class_name,
219+
"Change the %s property" % property.name,
220+
category,
221+
Types.BlockType.STATEMENT,
222+
TYPE_NIL,
223+
"change %s by {value: %s}" % [property.name.capitalize().to_lower(), type_string],
224+
"%s += {value}" % property.name,
225+
{"value": default_value},
226+
)
227+
228+
229+
static func new_property_getter(_class_name: String, property: Dictionary, category: String) -> Resource:
230+
return new(
231+
&"%s_get_%s" % [_class_name, property.name],
232+
_class_name,
233+
"The %s property" % property.name,
234+
category,
235+
Types.BlockType.VALUE,
236+
property.type,
237+
"%s" % property.name.capitalize().to_lower(),
238+
"%s" % property.name,
239+
)

addons/block_code/code_generation/blocks_catalog.gd

Lines changed: 5 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -132,54 +132,18 @@ static func _add_property_definitions(_class_name: String, property_list: Array[
132132
# Setter
133133
var block_definition: BlockDefinition
134134
if block_settings.get("has_setter", true):
135-
block_definition = (
136-
BlockDefinition
137-
. new(
138-
&"%s_set_%s" % [_class_name, property.name],
139-
_class_name,
140-
"Set the %s property" % property.name,
141-
block_settings.category,
142-
Types.BlockType.STATEMENT,
143-
TYPE_NIL,
144-
"set %s to {value: %s}" % [property.name.capitalize().to_lower(), type_string],
145-
"%s = {value}" % property.name,
146-
{"value": block_settings.get("default_set", _FALLBACK_SET_FOR_TYPE[property.type])},
147-
)
148-
)
135+
var default_value: Variant = block_settings.get("default_set", _FALLBACK_SET_FOR_TYPE[property.type])
136+
block_definition = BlockDefinition.new_property_setter(_class_name, property, block_settings.category, default_value)
149137
_catalog[block_definition.name] = block_definition
150138

151139
# Changer
152140
if block_settings.get("has_change", true):
153-
block_definition = (
154-
BlockDefinition
155-
. new(
156-
&"%s_change_%s" % [_class_name, property.name],
157-
_class_name,
158-
"Change the %s property" % property.name,
159-
block_settings.category,
160-
Types.BlockType.STATEMENT,
161-
TYPE_NIL,
162-
"change %s by {value: %s}" % [property.name.capitalize().to_lower(), type_string],
163-
"%s += {value}" % property.name,
164-
{"value": block_settings.get("default_change", _FALLBACK_CHANGE_FOR_TYPE[property.type])},
165-
)
166-
)
141+
var default_value: Variant = block_settings.get("default_change", _FALLBACK_CHANGE_FOR_TYPE[property.type])
142+
block_definition = BlockDefinition.new_property_changer(_class_name, property, block_settings.category, default_value)
167143
_catalog[block_definition.name] = block_definition
168144

169145
# Getter
170-
block_definition = (
171-
BlockDefinition
172-
. new(
173-
&"%s_get_%s" % [_class_name, property.name],
174-
_class_name,
175-
"The %s property" % property.name,
176-
block_settings.category,
177-
Types.BlockType.VALUE,
178-
property.type,
179-
"%s" % property.name.capitalize().to_lower(),
180-
"%s" % property.name,
181-
)
182-
)
146+
block_definition = BlockDefinition.new_property_getter(_class_name, property, block_settings.category)
183147
_catalog[block_definition.name] = block_definition
184148

185149

0 commit comments

Comments
 (0)