|
| 1 | +@tool |
| 2 | +extends Control |
| 3 | + |
| 4 | +const BlockCanvas = preload("res://addons/block_code/ui/block_canvas/block_canvas.gd") |
| 5 | +const Constants = preload("res://addons/block_code/ui/constants.gd") |
| 6 | +const InstructionTree = preload("res://addons/block_code/instruction_tree/instruction_tree.gd") |
| 7 | +const Types = preload("res://addons/block_code/types/types.gd") |
| 8 | + |
| 9 | +enum DragAction { NONE, PLACE, REMOVE } |
| 10 | + |
| 11 | +var _block: Block |
| 12 | +var _block_scope: String |
| 13 | +var _block_canvas: BlockCanvas |
| 14 | +var _preview_block: Control |
| 15 | +var _snap_points: Array[Node] |
| 16 | +var _delete_areas: Array[Rect2] |
| 17 | +var action: DragAction: |
| 18 | + get: |
| 19 | + return action |
| 20 | + set(value): |
| 21 | + if action != value: |
| 22 | + action = value |
| 23 | + _update_action_hint() |
| 24 | + |
| 25 | +var target_snap_point: SnapPoint: |
| 26 | + get: |
| 27 | + return target_snap_point |
| 28 | + set(value): |
| 29 | + if target_snap_point != value: |
| 30 | + target_snap_point = value |
| 31 | + _update_preview() |
| 32 | + |
| 33 | +var snap_block: Block: |
| 34 | + get: |
| 35 | + return target_snap_point.get_parent_block() if target_snap_point else null |
| 36 | + |
| 37 | + |
| 38 | +func _init(block: Block, block_scope: String, offset: Vector2, block_canvas: BlockCanvas): |
| 39 | + assert(block.get_parent() == null) |
| 40 | + |
| 41 | + add_child(block) |
| 42 | + block.position = -offset |
| 43 | + |
| 44 | + _block = block |
| 45 | + _block_scope = block_scope |
| 46 | + _block_canvas = block_canvas |
| 47 | + |
| 48 | + |
| 49 | +func set_snap_points(snap_points: Array[Node]): |
| 50 | + _snap_points = snap_points.filter(_snaps_to) |
| 51 | + |
| 52 | + |
| 53 | +func add_delete_area(delete_area: Rect2): |
| 54 | + _delete_areas.append(delete_area) |
| 55 | + |
| 56 | + |
| 57 | +func update_drag_state(): |
| 58 | + global_position = get_global_mouse_position() |
| 59 | + |
| 60 | + if _block_canvas.is_mouse_over(): |
| 61 | + scale = Vector2(_block_canvas.zoom, _block_canvas.zoom) |
| 62 | + else: |
| 63 | + scale = Vector2(1, 1) |
| 64 | + |
| 65 | + for rect in _delete_areas: |
| 66 | + if rect.has_point(get_global_mouse_position()): |
| 67 | + action = DragAction.REMOVE |
| 68 | + target_snap_point = null |
| 69 | + return |
| 70 | + |
| 71 | + action = DragAction.PLACE |
| 72 | + |
| 73 | + target_snap_point = _find_closest_snap_point() |
| 74 | + |
| 75 | + |
| 76 | +func apply_drag() -> Block: |
| 77 | + update_drag_state() |
| 78 | + |
| 79 | + if action == DragAction.PLACE: |
| 80 | + _place_block() |
| 81 | + return _block |
| 82 | + elif action == DragAction.REMOVE: |
| 83 | + _remove_block() |
| 84 | + return null |
| 85 | + else: |
| 86 | + return null |
| 87 | + |
| 88 | + |
| 89 | +func _remove_block(): |
| 90 | + target_snap_point = null |
| 91 | + _block.queue_free() |
| 92 | + |
| 93 | + |
| 94 | +func _place_block(): |
| 95 | + var canvas_rect: Rect2 = _block_canvas.get_global_rect() |
| 96 | + |
| 97 | + var position = _block.global_position - canvas_rect.position |
| 98 | + |
| 99 | + remove_child(_block) |
| 100 | + |
| 101 | + if target_snap_point: |
| 102 | + # Snap the block to the point |
| 103 | + var orphaned_block = target_snap_point.insert_snapped_block(_block) |
| 104 | + if orphaned_block: |
| 105 | + # Place the orphan block somewhere outside the snap point |
| 106 | + _block_canvas.arrange_block(orphaned_block, snap_block) |
| 107 | + else: |
| 108 | + # Block goes on screen somewhere |
| 109 | + _block_canvas.add_block(_block, position) |
| 110 | + |
| 111 | + target_snap_point = null |
| 112 | + |
| 113 | + |
| 114 | +func _snaps_to(node: Node) -> bool: |
| 115 | + var _snap_point: SnapPoint = node as SnapPoint |
| 116 | + |
| 117 | + if not _snap_point: |
| 118 | + push_error("Warning: node %s is not of class SnapPoint." % node) |
| 119 | + return false |
| 120 | + |
| 121 | + if not _block_canvas.is_ancestor_of(_snap_point): |
| 122 | + # We only snap to blocks on the canvas: |
| 123 | + return false |
| 124 | + |
| 125 | + if _block.block_type != _snap_point.block_type: |
| 126 | + # We only snap to the same block type: |
| 127 | + return false |
| 128 | + |
| 129 | + if _block.block_type == Types.BlockType.VALUE and not Types.can_cast(_block.variant_type, _snap_point.variant_type): |
| 130 | + # We only snap Value blocks to snaps that can cast to same variant: |
| 131 | + return false |
| 132 | + |
| 133 | + # Check if any parent node is this node |
| 134 | + if _snap_point.is_ancestor_of(_block): |
| 135 | + return false |
| 136 | + |
| 137 | + var top_block = _get_top_block_for_node(_snap_point) |
| 138 | + |
| 139 | + # Check if scope is valid |
| 140 | + if _block_scope != "": |
| 141 | + if top_block is EntryBlock: |
| 142 | + if _block_scope != top_block.get_entry_statement(): |
| 143 | + return false |
| 144 | + elif top_block: |
| 145 | + var tree_scope := InstructionTree.get_tree_scope(top_block) |
| 146 | + if tree_scope != "" and _block_scope != tree_scope: |
| 147 | + return false |
| 148 | + |
| 149 | + return true |
| 150 | + |
| 151 | + |
| 152 | +func _find_closest_snap_point() -> Node: |
| 153 | + var closest_snap_point: SnapPoint = null |
| 154 | + var closest_distance: int |
| 155 | + for snap_point in _snap_points: |
| 156 | + var distance = _get_distance_to_snap_point(snap_point) |
| 157 | + if distance > Constants.MINIMUM_SNAP_DISTANCE * _block_canvas.zoom: |
| 158 | + continue |
| 159 | + elif closest_snap_point == null or distance < closest_distance: |
| 160 | + closest_snap_point = snap_point |
| 161 | + closest_distance = distance |
| 162 | + return closest_snap_point |
| 163 | + |
| 164 | + |
| 165 | +func _get_top_block_for_node(node: Node) -> Block: |
| 166 | + for top_block in _block_canvas.get_blocks(): |
| 167 | + if top_block.is_ancestor_of(node): |
| 168 | + return top_block |
| 169 | + return null |
| 170 | + |
| 171 | + |
| 172 | +func _get_distance_to_snap_point(snap_point: SnapPoint) -> float: |
| 173 | + var from_global: Vector2 = _block.global_position |
| 174 | + return from_global.distance_to(snap_point.global_position) |
| 175 | + |
| 176 | + |
| 177 | +func _update_action_hint(): |
| 178 | + match action: |
| 179 | + DragAction.REMOVE: |
| 180 | + _block.modulate = Color(1.0, 1.0, 1.0, 0.5) |
| 181 | + _: |
| 182 | + _block.modulate = Color.WHITE |
| 183 | + |
| 184 | + |
| 185 | +func _update_preview(): |
| 186 | + if _preview_block: |
| 187 | + _preview_block.queue_free() |
| 188 | + _preview_block = null |
| 189 | + |
| 190 | + if target_snap_point: |
| 191 | + # Make preview block |
| 192 | + _preview_block = Control.new() |
| 193 | + _preview_block.set_script(preload("res://addons/block_code/ui/blocks/utilities/background/background.gd")) |
| 194 | + |
| 195 | + _preview_block.color = Color(1, 1, 1, 0.5) |
| 196 | + _preview_block.custom_minimum_size = _block.get_global_rect().size |
| 197 | + _preview_block.size_flags_horizontal = Control.SIZE_SHRINK_BEGIN |
| 198 | + _preview_block.size_flags_vertical = Control.SIZE_SHRINK_BEGIN |
| 199 | + |
| 200 | + target_snap_point.add_child(_preview_block) |
0 commit comments