Skip to content
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
18 changes: 12 additions & 6 deletions addons/block_code/drag_manager/drag_manager.gd
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ signal block_modified
@export var block_canvas_path: NodePath

const Constants = preload("res://addons/block_code/ui/constants.gd")
const BLOCK_AUTO_PLACE_MARGIN: Vector2 = Vector2(16, 8)

var drag_offset: Vector2
var dragging: Block = null
Expand All @@ -33,10 +34,6 @@ func _process(_delta):

var dragging_global_pos: Vector2 = dragging.get_global_rect().position

# TODO: check if dropped snap point is occupied
# if so, replace with this node and attach the previous one
# to this node's bottom snap

# Find closest snap point not child of current node
var closest_snap_point: SnapPoint = null
var closest_dist: float = INF
Expand Down Expand Up @@ -92,6 +89,7 @@ func _update_preview(snap_point: SnapPoint):
preview_block.color = Color(1, 1, 1, 0.5)
preview_block.custom_minimum_size = dragging.get_global_rect().size
preview_block.size_flags_horizontal = Control.SIZE_SHRINK_BEGIN
preview_block.size_flags_vertical = Control.SIZE_SHRINK_BEGIN

Copy link
Contributor

Choose a reason for hiding this comment

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

Nice!

previewing_snap_point.add_child(preview_block)

Expand All @@ -105,7 +103,9 @@ func drag_block(block: Block, copied_from: Block = null):
new_pos += block.get_global_rect().position

var parent = block.get_parent()
if parent:
if parent is SnapPoint:
parent.remove_snapped_block(block)
elif parent:
parent.remove_child(block)

block.position = new_pos
Expand Down Expand Up @@ -142,7 +142,13 @@ func drag_ended():
# Can snap block
preview_block.free()
preview_block = null
previewing_snap_point.add_child(dragging)
var orphaned_block = previewing_snap_point.set_snapped_block(dragging)
if orphaned_block:
# Place the orphan block somewhere outside the snap point
orphaned_block.position = (
(previewing_snap_point.block.global_position - block_canvas_rect.position) + (previewing_snap_point.block.get_size() * Vector2.RIGHT) + BLOCK_AUTO_PLACE_MARGIN
)
_block_canvas.add_block(orphaned_block)
else:
# Block goes on screen somewhere
dragging.position = (get_global_mouse_position() - block_canvas_rect.position - drag_offset)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,18 +100,7 @@ func _ready():
snap_point.block_type = block_type
snap_point.variant_type = variant_type

match variant_type:
TYPE_COLOR:
switch_input(_color_input)
TYPE_VECTOR2:
switch_input(_vector2_input)
TYPE_BOOL:
switch_input(_bool_input)
_:
switch_input(_text_input)

if option:
switch_input(_option_input)
_update_visible_input()


func get_snapped_block() -> Block:
Expand Down Expand Up @@ -148,11 +137,29 @@ func _on_line_edit_text_changed(new_text):
modified.emit()


func switch_input(node: Node):
func _update_visible_input():
if snap_point.has_snapped_block():
_switch_input(null)
Copy link
Contributor

Choose a reason for hiding this comment

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

Good, hide any input if we snap something!

elif option:
_switch_input(_option_input)
else:
match variant_type:
TYPE_COLOR:
_switch_input(_color_input)
TYPE_VECTOR2:
_switch_input(_vector2_input)
TYPE_BOOL:
_switch_input(_bool_input)
_:
_switch_input(_text_input)


func _switch_input(node: Node):
for c in _input_switcher.get_children():
c.visible = false

node.visible = true
if node:
node.visible = true


func _on_color_input_color_changed(color):
Expand All @@ -169,3 +176,7 @@ func _update_panel_bg_color(new_color):

func _on_option_input_item_selected(index):
modified.emit()


func _on_snap_point_snapped_block_changed(block):
_update_visible_input()
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,7 @@ size_flags_horizontal = 3

[node name="BoolInput" type="MarginContainer" parent="InputSwitcher"]
unique_name_in_owner = true
visible = false
layout_mode = 2
theme_override_constants/margin_left = 8

Expand Down Expand Up @@ -187,3 +188,4 @@ variant_type = 4
[connection signal="text_changed" from="InputSwitcher/Vector2Input/HBoxContainer/XLineEdit" to="." method="_on_line_edit_text_changed"]
[connection signal="text_changed" from="InputSwitcher/Vector2Input/HBoxContainer/YLineEdit" to="." method="_on_line_edit_text_changed"]
[connection signal="item_selected" from="InputSwitcher/BoolInput/BoolInputOption" to="." method="_on_option_input_item_selected"]
[connection signal="snapped_block_changed" from="SnapPoint" to="." method="_on_snap_point_snapped_block_changed"]
49 changes: 46 additions & 3 deletions addons/block_code/ui/blocks/utilities/snap_point/snap_point.gd
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ extends MarginContainer
## When block_type is [enum Types.BlockType.VALUE], the type of the value that can be used at this snap point.
@export var variant_type: Variant.Type

signal snapped_block_changed(block: Block)

var block: Block


Expand All @@ -18,7 +20,48 @@ func _ready():


func get_snapped_block() -> Block:
if get_child_count() == 0:
return null
for node in get_children():
if node is Block:
return node
return null


func has_snapped_block() -> bool:
return get_snapped_block() != null


func set_snapped_block(snapped_block: Block) -> Block:
var orphaned_block: Block = _pop_snapped_block()

if snapped_block:
add_child(snapped_block)

if snapped_block and orphaned_block:
var last_snap = _get_last_snap(snapped_block)
if last_snap:
last_snap.set_snapped_block(orphaned_block)
orphaned_block = null

snapped_block_changed.emit(snapped_block)

return orphaned_block


func remove_snapped_block(snapped_block: Block):
assert(snapped_block == get_snapped_block())
set_snapped_block(null)


func _pop_snapped_block() -> Block:
var snapped_block = get_snapped_block()
if snapped_block:
remove_child(snapped_block)
return snapped_block


return get_child(0) as Block
func _get_last_snap(block: Block) -> SnapPoint:
var last_snap: SnapPoint
while block:
last_snap = block.bottom_snap
block = last_snap.get_snapped_block() if last_snap else null
return last_snap