-
Notifications
You must be signed in to change notification settings - Fork 27
Add dynamic options lists for blocks #206
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
e5161f3
BlockScriptSerialization: Add block library and instantiation
dylanmccall 4118bae
BlocksCatalog: Remove by_class_name cache
dylanmccall dc99328
BlockDefinition: Support extension scripts
dylanmccall 3517792
animationplayer_play: Add an extension script to generate an option list
dylanmccall 81b6456
Add TemplateEditor to generate UI based on a block format string
dylanmccall 1d8821c
ParameterInput: Add a separate option data property
dylanmccall 2cfacea
ParameterInput: Add current value to options list
dylanmccall 41afa00
TemplateEditor: Remove the OPTION parameter type
dylanmccall 4144034
ParameterInput: Change option input to fit only the selected item
dylanmccall 018836e
BlocksCatalog: Include base blocks in get_inherited_blocks
dylanmccall 33133a6
BlockAST: Move some duplicated code to a function
dylanmccall a14eed5
BlockAST: Convert OptionData to selected value
dylanmccall e812651
Block: Connect template_editor modified signal automatically
dylanmccall File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
@tool | ||
extends BlockExtension | ||
|
||
const OptionData = preload("res://addons/block_code/code_generation/option_data.gd") | ||
|
||
|
||
func get_defaults_for_node(context_node: Node) -> Dictionary: | ||
var animation_player = context_node as AnimationPlayer | ||
|
||
if not animation_player: | ||
return {} | ||
|
||
var animation_list = animation_player.get_animation_list() | ||
|
||
return {"animation": OptionData.new(animation_list)} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -44,31 +44,15 @@ class ASTNode: | |
children = [] | ||
arguments = {} | ||
|
||
func get_code_block() -> String: | ||
var code_block: String = data.code_template # get multiline code_template from block definition | ||
|
||
# insert args | ||
|
||
# check if args match an overload in the resource | ||
|
||
for arg_name in arguments: | ||
# Use parentheses to be safe | ||
var argument = arguments[arg_name] | ||
var code_string: String | ||
if argument is ASTValueNode: | ||
code_string = argument.get_code() | ||
else: | ||
code_string = BlockAST.raw_input_to_code_string(argument) | ||
|
||
code_block = code_block.replace("{%s}" % arg_name, code_string) | ||
|
||
func _get_code_block() -> String: | ||
var code_block: String = BlockAST.format_code_template(data.code_template, arguments) | ||
return IDHandler.make_unique(code_block) | ||
|
||
func get_code(depth: int) -> String: | ||
var code: String = "" | ||
|
||
# append code block | ||
var code_block := get_code_block() | ||
var code_block := _get_code_block() | ||
code_block = code_block.indent("\t".repeat(depth)) | ||
|
||
code += code_block + "\n" | ||
|
@@ -91,21 +75,7 @@ class ASTValueNode: | |
arguments = {} | ||
|
||
func get_code() -> String: | ||
var code: String = data.code_template # get code_template from block definition | ||
|
||
# check if args match an overload in the resource | ||
|
||
for arg_name in arguments: | ||
# Use parentheses to be safe | ||
var argument = arguments[arg_name] | ||
var code_string: String | ||
if argument is ASTValueNode: | ||
code_string = argument.get_code() | ||
else: | ||
code_string = BlockAST.raw_input_to_code_string(argument) | ||
|
||
code = code.replace("{%s}" % arg_name, code_string) | ||
|
||
var code: String = BlockAST.format_code_template(data.code_template, arguments) | ||
return IDHandler.make_unique("(%s)" % code) | ||
|
||
|
||
|
@@ -127,18 +97,42 @@ func to_string_recursive(node: ASTNode, depth: int) -> String: | |
return string | ||
|
||
|
||
static func format_code_template(code_template: String, arguments: Dictionary) -> String: | ||
for argument_name in arguments: | ||
# Use parentheses to be safe | ||
var argument_value: Variant = arguments[argument_name] | ||
var code_string: String | ||
var raw_string: String | ||
|
||
if argument_value is OptionData: | ||
# Temporary hack: previously, the value was stored as an OptionData | ||
# object with a list of items and a "selected" property. If we are | ||
# using an older block script where that is the case, convert the | ||
# value to the value of its selected item. | ||
# See also, ParameterInput._update_option_input. | ||
argument_value = argument_value.items[argument_value.selected] | ||
Comment on lines
+107
to
+113
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't think that we need to maintain backwards compatibility, there has been wild refactors since the last release. It doesn't harm to have this anyways. |
||
|
||
if argument_value is ASTValueNode: | ||
code_string = argument_value.get_code() | ||
raw_string = code_string | ||
else: | ||
code_string = BlockAST.raw_input_to_code_string(argument_value) | ||
raw_string = str(argument_value) | ||
|
||
code_template = code_template.replace("{{%s}}" % argument_name, raw_string) | ||
code_template = code_template.replace("{%s}" % argument_name, code_string) | ||
|
||
return code_template | ||
|
||
|
||
static func raw_input_to_code_string(input) -> String: | ||
match typeof(input): | ||
TYPE_STRING: | ||
return "'%s'" % input.replace("\\", "\\\\").replace("'", "\\'") | ||
return "'%s'" % input.c_escape() | ||
TYPE_VECTOR2: | ||
return "Vector2%s" % str(input) | ||
TYPE_COLOR: | ||
return "Color%s" % str(input) | ||
TYPE_OBJECT: | ||
if input is OptionData: | ||
var option_data := input as OptionData | ||
return option_data.items[option_data.selected] | ||
_: | ||
return "%s" % input | ||
|
||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
@tool | ||
class_name BlockExtension | ||
extends Object | ||
|
||
|
||
func get_defaults_for_node(context_node: Node) -> Dictionary: | ||
return {} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.