Skip to content

Commit

Permalink
Block definition: Add constant value like definition
Browse files Browse the repository at this point in the history
Define the format "{const <parameter name>: <type>}" as constant
parameter value. And, it is a kind of in parameter value, but never
changes.

https://phabricator.endlessm.com/T35712
  • Loading branch information
starnight committed Nov 19, 2024
1 parent 1ec0336 commit 8851d72
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
5 changes: 4 additions & 1 deletion addons/block_code/code_generation/block_definition.gd
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ extends Resource

const Types = preload("res://addons/block_code/types/types.gd")

const FORMAT_STRING_PATTERN = "\\[(?<out_parameter>[^\\]]+)\\]|\\{(?<in_parameter>[^}]+)\\}|(?<label>[^\\{\\[]+)"
const FORMAT_STRING_PATTERN = "\\[(?<out_parameter>[^\\]]+)\\]|\\{const (?<const_parameter>[^}]+)\\}|\\{(?!const )(?<in_parameter>[^}]+)\\}|(?<label>[^\\{\\[]+)"

@export var name: StringName

Expand Down Expand Up @@ -102,6 +102,9 @@ static func parse_display_template(template_string: String):
elif regex_match.names.has("out_parameter"):
var parameter_string := regex_match.get_string("out_parameter")
items.append({"out_parameter": _parse_parameter_format(parameter_string)})
elif regex_match.names.has("const_parameter"):
var parameter_string := regex_match.get_string("const_parameter")
items.append({"const_parameter": _parse_parameter_format(parameter_string)})
return items


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,8 @@ func _update_from_format_string():
_append_input_parameter(item.get("in_parameter"), match_id)
elif item.has("out_parameter"):
_append_output_parameter(item.get("out_parameter"), match_id)
elif item.has("const_parameter"):
_append_const_parameter(item.get("const_parameter"), match_id)
match_id += 1


Expand All @@ -104,7 +106,7 @@ func _append_label(label_format: String):
_container.add_child(label)


func _append_input_parameter(parameter: Dictionary, id: int):
func _append_input_parameter(parameter: Dictionary, id: int) -> ParameterInput:
var default_value = parameter_defaults.get(parameter["name"])

var parameter_input: ParameterInput = ParameterInputScene.instantiate()
Expand All @@ -126,6 +128,8 @@ func _append_input_parameter(parameter: Dictionary, id: int):
_container.add_child(parameter_input)
_parameter_inputs_by_name[parameter["name"]] = parameter_input

return parameter_input


func _append_output_parameter(parameter: Dictionary, id: int):
var parameter_output: ParameterOutput
Expand All @@ -137,5 +141,12 @@ func _append_output_parameter(parameter: Dictionary, id: int):
_container.add_child(parameter_output)


func _append_const_parameter(parameter: Dictionary, id: int):
# const_parameter is a kind of in_parameter with default value, but never
# changes value.
var parameter_const := _append_input_parameter(parameter, id)
parameter_const.visible = false


func _on_parameter_input_drag_started(offset: Vector2):
drag_started.emit(offset)

0 comments on commit 8851d72

Please sign in to comment.