moved drag handling to board to avoid issues

This commit is contained in:
Adrian Schmid 2023-07-01 15:19:54 +02:00
parent 00ca4abc1f
commit ea67614da2
2 changed files with 54 additions and 15 deletions

View File

@ -1,8 +1,12 @@
extends PanelContainer extends PanelContainer
var obj_dict = {} var area_dict = {}
@onready var dropzone = $HBoxContainer/dropzone @onready var dropzone = $HBoxContainer/dropzone
var currently_selected_node: Area2D = null
var is_area_dragged: bool = false
var currently_dragged_area: Area2D
# Called when the node enters the scene tree for the first time. # Called when the node enters the scene tree for the first time.
func _ready(): func _ready():
@ -16,29 +20,66 @@ func _ready():
for panel in post_it_panels: for panel in post_it_panels:
post_its.push_back(panel.get_child(0)) post_its.push_back(panel.get_child(0))
obj_dict["Cards"] = cards area_dict["dropzone_content"] = cards # will be selected on the left side
obj_dict["Post_it_panels"] = post_it_panels area_dict["post_it_panels"] = post_it_panels # to remember panel positions
obj_dict["Post_its"] = post_its area_dict["post_its_in_list"] = post_its # will be selected on the right side
currently_selected_node = area_dict["dropzone_content"][0] # set first Card as currently selected node by default
# Called every frame. 'delta' is the elapsed time since the previous frame. # Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(delta): func _process(delta):
pass
if !Input.is_mouse_button_pressed(MOUSE_BUTTON_LEFT) and is_area_dragged:
currently_dragged_area.is_dragged = false
is_area_dragged = Input.is_mouse_button_pressed(MOUSE_BUTTON_LEFT)
currently_dragged_area = null
# Will be used later to spawn Cards and Post-Its and remember them in the dictionary
func populate_board(): func populate_board():
pass pass
func handle_mouse_button(to_handle: Node, input: InputEvent, dragableType: int): # Checks if a Node is currently inside the dropzone
print_debug(input) func is_in_dropzone(to_check: Node) -> bool:
if (dropzone.size.x < to_check.position.x or dropzone.size.y < to_check.position.y):
return false
elif (to_check.position.x < 0 or to_check.position.y < 0):
return false
else:
return true
# TODO: We need a better way to recognize
# whether to_handle is a Card or a Post-It func handle_mouse_button(to_handle: Node, input: InputEvent, dragableType: int):
currently_selected_node = to_handle # update currently selected
currently_dragged_area = to_handle
to_handle.is_dragged = input.pressed
is_area_dragged = input.pressed
# TODO: We need a better way to recognize whether "to_handle" is a Card or a Post-It.
# (Tried checking for a script, didn't work, because script has no name attached. # (Tried checking for a script, didn't work, because script has no name attached.
# Alternative might be to check for specific values within the script ("is_card" f.e)) # Alternative might be to check for specific values within the script ("is_card" f.e))
match dragableType: match dragableType:
1: # 1 = Card 1: # 1 = Card
print_debug("I'm a card") print_debug("I'm a card")
2: # 2 = PostIt 2: # 2 = PostIt
print_debug("I'm a Post-It") if input.is_pressed():
to_handle.reparent(dropzone) to_handle.reparent(dropzone)
to_handle.set_owner(self) # needs to be here otherwise the owner disappears to_handle.set_owner(self) # needs to be here otherwise the owner disappears
area_dict["post_its_in_list"].erase(to_handle)
area_dict["dropzone_content"].push_back(to_handle)
# TODO: Add function to rearrange the arrays based on positions in the dropzone
else:
if !is_in_dropzone(to_handle):
for panel in area_dict["post_it_panels"]:
if panel.get_child(0) == null:
to_handle.reparent(panel)
to_handle.set_owner(self)
area_dict["dropzone_content"].erase(to_handle)
area_dict["post_its_in_list"].push_back(to_handle)
# TODO: Add function to rearrange the array based on taken panel spot
to_handle.position = panel.position
break

View File

@ -82,8 +82,6 @@ func _on_input_event(viewport, event, shape_idx):
if event is InputEventMouseButton: if event is InputEventMouseButton:
if event.button_index == MOUSE_BUTTON_LEFT: if event.button_index == MOUSE_BUTTON_LEFT:
if is_dragable: if is_dragable and "handle_mouse_button" in owner:
is_dragged = event.pressed
if "handle_mouse_button" in owner:
owner.handle_mouse_button(self, event, 2) owner.handle_mouse_button(self, event, 2)