Skip to content
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

Block code demo scene #223

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions addons/block_code/block_code_plugin.gd
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,9 @@ static func is_block_code_editable(block_code: BlockCode) -> bool:
if not block_code:
return false

if not Engine.is_editor_hint():
return true

# A BlockCode node can be edited if it belongs to the edited scene, or it
# is an editable instance.

Expand Down
5 changes: 4 additions & 1 deletion addons/block_code/ui/block_canvas/block_canvas.gd
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,11 @@ func set_child(n: Node):
func _on_context_changed():
clear_canvas()

var edited_node = EditorInterface.get_inspector().get_edited_object() as Node
var edited_node: Node

if Engine.is_editor_hint():
edited_node = EditorInterface.get_inspector().get_edited_object() as Node

if _context.block_script != _current_block_script:
_window.position = Vector2(0, 0)
zoom = 1
Expand Down
9 changes: 7 additions & 2 deletions addons/block_code/ui/block_canvas/block_canvas.tscn
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ layout_mode = 2

[node name="Window" type="Control" parent="WindowContainer"]
unique_name_in_owner = true
visible = false
layout_mode = 2
anchors_preset = 0
offset_right = 1152.0
Expand Down Expand Up @@ -60,10 +61,12 @@ theme_override_constants/margin_bottom = 4

[node name="ZoomButton" type="Button" parent="WindowContainer/Overlay/MarginContainer"]
unique_name_in_owner = true
visible = false
modulate = Color(1, 1, 1, 0.470588)
layout_mode = 2
focus_mode = 0
theme_override_font_sizes/font_size = 24
text = "1.0x"

[node name="MouseOverride" type="MarginContainer" parent="."]
unique_name_in_owner = true
Expand All @@ -72,7 +75,6 @@ mouse_filter = 2

[node name="EmptyBox" type="VBoxContainer" parent="."]
unique_name_in_owner = true
visible = false
layout_mode = 2
size_flags_vertical = 4

Expand All @@ -90,7 +92,7 @@ size_flags_vertical = 4
[node name="Label" type="Label" parent="SelectedNodeBox"]
custom_minimum_size = Vector2(200, 0)
layout_mode = 2
text = "Use block coding to create custom behavior and game mechanics for \"{node}\"."
text = "Use block coding to create custom behavior and game mechanics for \"BlockCanvas\"."
horizontal_alignment = 1
autowrap_mode = 2

Expand All @@ -103,6 +105,7 @@ unique_name_in_owner = true
layout_mode = 2
size_flags_horizontal = 4
theme_type_variation = &"InspectorActionButton"
disabled = true
text = "Add Block Code"
icon = ExtResource("2_710vn")

Expand All @@ -128,13 +131,15 @@ unique_name_in_owner = true
layout_mode = 2
size_flags_horizontal = 4
theme_type_variation = &"InspectorActionButton"
disabled = true
text = "Open in Editor"

[node name="ReplaceBlockCodeButton" type="Button" parent="SelectedNodeWithBlockCodeBox/ButtonsBox"]
unique_name_in_owner = true
layout_mode = 2
size_flags_horizontal = 4
theme_type_variation = &"InspectorActionButton"
disabled = true
text = "Override Block Code"
icon = ExtResource("2_710vn")

Expand Down
59 changes: 37 additions & 22 deletions addons/block_code/ui/main_panel.gd
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,12 @@ const VariableDefinition = preload("res://addons/block_code/code_generation/vari
@onready var _drag_manager: DragManager = %DragManager
@onready var _title_bar: TitleBar = %TitleBar
@onready var _delete_node_button: Button = %DeleteNodeButton
@onready var _editor_inspector: EditorInspector = EditorInterface.get_inspector()
@onready var _picker_split: HSplitContainer = %PickerSplit
@onready var _collapse_button: Button = %CollapseButton

@onready var _icon_delete := EditorInterface.get_editor_theme().get_icon("Remove", "EditorIcons")
@onready var _icon_collapse := EditorInterface.get_editor_theme().get_icon("Back", "EditorIcons")
@onready var _icon_expand := EditorInterface.get_editor_theme().get_icon("Forward", "EditorIcons")
@onready var _icon_delete := EditorInterface.get_editor_theme().get_icon("Remove", "EditorIcons") if Engine.is_editor_hint() else null
@onready var _icon_collapse := EditorInterface.get_editor_theme().get_icon("Back", "EditorIcons") if Engine.is_editor_hint() else null
@onready var _icon_expand := EditorInterface.get_editor_theme().get_icon("Forward", "EditorIcons") if Engine.is_editor_hint() else null

const Constants = preload("res://addons/block_code/ui/constants.gd")

Expand Down Expand Up @@ -61,12 +60,16 @@ func _on_undo_redo_version_changed():

func _on_show_script_button_pressed():
var script: String = _block_canvas.generate_script_from_current_window()
print("_on_show_script_button_pressed ", _block_canvas._context.block_code_node)

script_window_requested.emit(script)


func _on_delete_node_button_pressed():
var scene_root = EditorInterface.get_edited_scene_root()
var scene_root: Node

if Engine.is_editor_hint():
scene_root = EditorInterface.get_edited_scene_root()

if not scene_root:
return
Expand Down Expand Up @@ -130,7 +133,7 @@ func save_script():
print("No script loaded to save.")
return

var scene_node = EditorInterface.get_edited_scene_root()
var scene_node = EditorInterface.get_edited_scene_root() if Engine.is_editor_hint() else null

if not BlockCodePlugin.is_block_code_editable(_context.block_code_node):
print("Block code for {node} is not editable.".format({"node": _context.block_code_node}))
Expand All @@ -141,29 +144,36 @@ func save_script():
var resource_path_split = block_script.resource_path.split("::", true, 1)
var resource_scene = resource_path_split[0]

undo_redo.create_action("Modify %s's block code script" % _context.parent_node.name, UndoRedo.MERGE_DISABLE, _context.block_code_node)
if undo_redo:
undo_redo.create_action("Modify %s's block code script" % _context.parent_node.name, UndoRedo.MERGE_DISABLE, _context.block_code_node)

if resource_scene and resource_scene != scene_node.scene_file_path:
if resource_scene and scene_node and resource_scene != scene_node.scene_file_path:
# This resource is from another scene. Since the user is changing it
# here, we'll make a copy for this scene rather than changing it in the
# other scene file.
undo_redo.add_undo_property(_context.block_code_node, "block_script", _context.block_script)
if undo_redo:
undo_redo.add_undo_property(_context.block_code_node, "block_script", _context.block_script)
block_script = block_script.duplicate(true)
undo_redo.add_do_property(_context.block_code_node, "block_script", block_script)
if undo_redo:
undo_redo.add_do_property(_context.block_code_node, "block_script", block_script)

undo_redo.add_undo_property(block_script, "block_serialization_trees", block_script.block_serialization_trees)
if undo_redo:
undo_redo.add_undo_property(block_script, "block_serialization_trees", block_script.block_serialization_trees)
_block_canvas.rebuild_ast_list()
_block_canvas.rebuild_block_serialization_trees()
undo_redo.add_do_property(block_script, "block_serialization_trees", block_script.block_serialization_trees)
if undo_redo:
undo_redo.add_do_property(block_script, "block_serialization_trees", block_script.block_serialization_trees)

var generated_script = _block_canvas.generate_script_from_current_window()
if generated_script != block_script.generated_script:
undo_redo.add_undo_property(block_script, "generated_script", block_script.generated_script)
undo_redo.add_do_property(block_script, "generated_script", generated_script)

if undo_redo:
undo_redo.add_undo_property(block_script, "generated_script", block_script.generated_script)
undo_redo.add_do_property(block_script, "generated_script", generated_script)

block_script.version = Constants.CURRENT_DATA_VERSION

undo_redo.commit_action()
if undo_redo:
undo_redo.commit_action()


func _input(event):
Expand Down Expand Up @@ -273,13 +283,18 @@ func _create_variable(variable: VariableDefinition):

var block_script: BlockScriptSerialization = _context.block_script

undo_redo.create_action("Create variable %s in %s's block code script" % [variable.var_name, _context.parent_node.name])
undo_redo.add_undo_property(_context.block_script, "variables", _context.block_script.variables)
if undo_redo:
undo_redo.create_action("Create variable %s in %s's block code script" % [variable.var_name, _context.parent_node.name])
undo_redo.add_undo_property(_context.block_script, "variables", _context.block_script.variables)

var new_variables = block_script.variables.duplicate()
new_variables.append(variable)
var new_variables = block_script.variables.duplicate()
new_variables.append(variable)

undo_redo.add_do_property(_context.block_script, "variables", new_variables)
undo_redo.commit_action()
undo_redo.add_do_property(_context.block_script, "variables", new_variables)
undo_redo.commit_action()
else:
var new_variables = block_script.variables.duplicate()
new_variables.append(variable)
block_script.variables = new_variables

_picker.reload_blocks()
Loading