Skip to content

First import fixes #155

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 5 commits into from
Jul 23, 2024
Merged
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
23 changes: 4 additions & 19 deletions addons/block_code/block_code_plugin.gd
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
@tool
class_name BlockCodePlugin
extends EditorPlugin
const MainPanelScene := preload("res://addons/block_code/ui/main_panel.tscn")
const MainPanel = preload("res://addons/block_code/ui/main_panel.gd")
const Types = preload("res://addons/block_code/types/types.gd")

const MainPanel := preload("res://addons/block_code/ui/main_panel.tscn")
static var main_panel: MainPanel
static var block_code_button: Button

Expand All @@ -18,31 +19,15 @@ var old_feature_profile: String = ""

const DISABLED_CLASSES := [
"BlockScriptData",
"DragManager",
"InstructionTree",
"Types",
"Block",
"ControlBlock",
"ParameterBlock",
"StatementBlock",
"DragDropArea",
"SnapPoint",
"SerializedBlockTreeNodeArray",
"SerializedBlockTreeNode",
"SerializedBlock",
"PackedSceneTreeNodeArray",
"PackedSceneTreeNode",
"BlockCanvas",
"CategoryFactory",
"BlockCategoryDisplay",
"BlockCategory",
"Picker",
"TitleBar",
"MainPanel",
"BlockCodePlugin",
"BlockCategoryButton",
"CreateVariableButton",
"VariableCategoryDisplay"
]


Expand All @@ -52,7 +37,7 @@ func _enter_tree():
editor_inspector = EditorInterface.get_inspector()
editor_selection = EditorInterface.get_selection()

main_panel = MainPanel.instantiate()
main_panel = MainPanelScene.instantiate()
main_panel.undo_redo = get_undo_redo()
block_code_button = add_control_to_bottom_panel(main_panel, _get_plugin_name())
block_inspector_plugin = BlockInspectorPlugin.new()
Expand Down
200 changes: 200 additions & 0 deletions addons/block_code/drag_manager/drag.gd
Original file line number Diff line number Diff line change
@@ -0,0 +1,200 @@
@tool
extends Control
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this not want:

@tool
class_name Drag

?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh good catch!

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK based on the comment below, we want @tool but not class_name?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Exactly, I just added a fixup.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would assume that since Drag is only loaded from DragManager, which is itself a @tool script, then it's not strictly necessary here. But I don't really understand the intricacies of @tool.


const BlockCanvas = preload("res://addons/block_code/ui/block_canvas/block_canvas.gd")
const Constants = preload("res://addons/block_code/ui/constants.gd")
const InstructionTree = preload("res://addons/block_code/instruction_tree/instruction_tree.gd")
const Types = preload("res://addons/block_code/types/types.gd")

enum DragAction { NONE, PLACE, REMOVE }

var _block: Block
var _block_scope: String
var _block_canvas: BlockCanvas
var _preview_block: Control
var _snap_points: Array[Node]
var _delete_areas: Array[Rect2]
var action: DragAction:
get:
return action
set(value):
if action != value:
action = value
_update_action_hint()

var target_snap_point: SnapPoint:
get:
return target_snap_point
set(value):
if target_snap_point != value:
target_snap_point = value
_update_preview()

var snap_block: Block:
get:
return target_snap_point.get_parent_block() if target_snap_point else null


func _init(block: Block, block_scope: String, offset: Vector2, block_canvas: BlockCanvas):
assert(block.get_parent() == null)

add_child(block)
block.position = -offset

_block = block
_block_scope = block_scope
_block_canvas = block_canvas


func set_snap_points(snap_points: Array[Node]):
_snap_points = snap_points.filter(_snaps_to)


func add_delete_area(delete_area: Rect2):
_delete_areas.append(delete_area)


func update_drag_state():
global_position = get_global_mouse_position()

if _block_canvas.is_mouse_over():
scale = Vector2(_block_canvas.zoom, _block_canvas.zoom)
else:
scale = Vector2(1, 1)

for rect in _delete_areas:
if rect.has_point(get_global_mouse_position()):
action = DragAction.REMOVE
target_snap_point = null
return

action = DragAction.PLACE

target_snap_point = _find_closest_snap_point()


func apply_drag() -> Block:
update_drag_state()

if action == DragAction.PLACE:
_place_block()
return _block
elif action == DragAction.REMOVE:
_remove_block()
return null
else:
return null


func _remove_block():
target_snap_point = null
_block.queue_free()


func _place_block():
var canvas_rect: Rect2 = _block_canvas.get_global_rect()

var position = _block.global_position - canvas_rect.position

remove_child(_block)

if target_snap_point:
# Snap the block to the point
var orphaned_block = target_snap_point.insert_snapped_block(_block)
if orphaned_block:
# Place the orphan block somewhere outside the snap point
_block_canvas.arrange_block(orphaned_block, snap_block)
else:
# Block goes on screen somewhere
_block_canvas.add_block(_block, position)

target_snap_point = null


func _snaps_to(node: Node) -> bool:
var _snap_point: SnapPoint = node as SnapPoint

if not _snap_point:
push_error("Warning: node %s is not of class SnapPoint." % node)
return false

if not _block_canvas.is_ancestor_of(_snap_point):
# We only snap to blocks on the canvas:
return false

if _block.block_type != _snap_point.block_type:
# We only snap to the same block type:
return false

if _block.block_type == Types.BlockType.VALUE and not Types.can_cast(_block.variant_type, _snap_point.variant_type):
# We only snap Value blocks to snaps that can cast to same variant:
return false

# Check if any parent node is this node
if _snap_point.is_ancestor_of(_block):
return false

var top_block = _get_top_block_for_node(_snap_point)

# Check if scope is valid
if _block_scope != "":
if top_block is EntryBlock:
if _block_scope != top_block.get_entry_statement():
return false
elif top_block:
var tree_scope := InstructionTree.get_tree_scope(top_block)
if tree_scope != "" and _block_scope != tree_scope:
return false

return true


func _find_closest_snap_point() -> Node:
var closest_snap_point: SnapPoint = null
var closest_distance: int
for snap_point in _snap_points:
var distance = _get_distance_to_snap_point(snap_point)
if distance > Constants.MINIMUM_SNAP_DISTANCE * _block_canvas.zoom:
continue
elif closest_snap_point == null or distance < closest_distance:
closest_snap_point = snap_point
closest_distance = distance
return closest_snap_point


func _get_top_block_for_node(node: Node) -> Block:
for top_block in _block_canvas.get_blocks():
if top_block.is_ancestor_of(node):
return top_block
return null


func _get_distance_to_snap_point(snap_point: SnapPoint) -> float:
var from_global: Vector2 = _block.global_position
return from_global.distance_to(snap_point.global_position)


func _update_action_hint():
match action:
DragAction.REMOVE:
_block.modulate = Color(1.0, 1.0, 1.0, 0.5)
_:
_block.modulate = Color.WHITE


func _update_preview():
if _preview_block:
_preview_block.queue_free()
_preview_block = null

if target_snap_point:
# Make preview block
_preview_block = Control.new()
_preview_block.set_script(preload("res://addons/block_code/ui/blocks/utilities/background/background.gd"))

_preview_block.color = Color(1, 1, 1, 0.5)
_preview_block.custom_minimum_size = _block.get_global_rect().size
_preview_block.size_flags_horizontal = Control.SIZE_SHRINK_BEGIN
_preview_block.size_flags_vertical = Control.SIZE_SHRINK_BEGIN

target_snap_point.add_child(_preview_block)
Loading