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

In block_canvas, snap EntryBlock blocks to a grid #130

Merged
merged 1 commit into from
Jul 12, 2024
Merged
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
22 changes: 17 additions & 5 deletions addons/block_code/ui/block_canvas/block_canvas.gd
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@ class_name BlockCanvas
extends MarginContainer

const EXTEND_MARGIN: float = 800
const BLOCK_AUTO_PLACE_MARGIN: Vector2 = Vector2(16, 8)
const DEFAULT_WINDOW_MARGIN: Vector2 = Vector2(24, 24)
const BLOCK_AUTO_PLACE_MARGIN: Vector2 = Vector2(25, 8)
const DEFAULT_WINDOW_MARGIN: Vector2 = Vector2(25, 25)
const SNAP_GRID: Vector2 = Vector2(25, 25)
const ZOOM_FACTOR: float = 1.1

@onready var _window: Control = %Window
Expand All @@ -27,6 +28,7 @@ const ZOOM_FACTOR: float = 1.1
@onready var _mouse_override: Control = %MouseOverride
@onready var _zoom_label: Label = %ZoomLabel

var _current_bsd: BlockScriptData
var _block_scenes_by_class = {}
var _panning := false
var zoom: float:
Expand Down Expand Up @@ -59,7 +61,11 @@ func _populate_block_scenes_by_class():


func add_block(block: Block, position: Vector2 = Vector2.ZERO) -> void:
block.position = canvas_to_window(position)
if block is EntryBlock:
block.position = canvas_to_window(position).snapped(SNAP_GRID)
else:
block.position = canvas_to_window(position)

_window.add_child(block)


Expand All @@ -74,7 +80,9 @@ func get_blocks() -> Array[Block]:

func arrange_block(block: Block, nearby_block: Block) -> void:
add_block(block)
block.global_position = (nearby_block.global_position + (nearby_block.get_size() * Vector2.RIGHT) + BLOCK_AUTO_PLACE_MARGIN)
var rect = nearby_block.get_global_rect()
rect.position += (rect.size * Vector2.RIGHT) + BLOCK_AUTO_PLACE_MARGIN
block.global_position = rect.position


func set_child(n: Node):
Expand Down Expand Up @@ -105,7 +113,9 @@ func bsd_selected(bsd: BlockScriptData):
_load_bsd(bsd)
_window.visible = true
_zoom_label.visible = true
reset_window_position()

if bsd != _current_bsd:
reset_window_position()
elif edited_node == null:
_empty_box.visible = true
elif BlockCodePlugin.node_has_block_code(edited_node):
Expand All @@ -123,6 +133,8 @@ func bsd_selected(bsd: BlockScriptData):
_selected_node_label.text = _selected_node_label_format.format({"node": edited_node.name})
_add_block_code_button.disabled = false

_current_bsd = bsd


func _load_bsd(bsd: BlockScriptData):
for tree in bsd.block_trees.array:
Expand Down
Loading