frame-of-mind/src/logic-scenes/board/card-board.gd

106 lines
4.7 KiB
GDScript3
Raw Normal View History

extends PanelContainer
var area_dict = {}
@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.
func _ready():
# TODO: Currently populating the dictionary with the Nodes currently in the scene
# When opening the scene we need to pass some kind of collection to the Board
# Then it can display the Card/PostIts and populate its dictionary at the same time
var cards = dropzone.get_children()
var post_it_panels = get_child(0).get_child(1).get_child(0).get_children()
var post_its = Array()
for panel in post_it_panels:
post_its.push_back(panel.get_child(1))
area_dict["dropzone_content"] = cards # will be selected on the left side
area_dict["post_it_panels"] = post_it_panels # to remember panel positions
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.
func _process(delta):
# Reset information about Areas being dragged, if the mouse is not longer pressed.
# Needed because otherwise it can happen that the areas don't register if you stop clicking on them.
if !Input.is_mouse_button_pressed(MOUSE_BUTTON_LEFT) and is_area_dragged:
currently_dragged_area.is_dragged = false
is_area_dragged = false
currently_dragged_area = null
# Will be used later to spawn Cards and Post-Its and remember them in the dictionary
func populate_board():
pass
# Checks if a Node is currently inside the dropzone
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
2023-07-01 14:03:22 +00:00
func handle_mouse_button(to_handle: Area2D, input: InputEvent, dragableType: int):
# No two areas can be dragged at the same time.
# Make sure that only the same area is dragged.
# Otherwise overlapping areas are dragged at the same time.
if currently_dragged_area != null and to_handle != currently_dragged_area:
return
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.
# Alternative might be to check for specific values within the script ("is_card" f.e))
match dragableType:
1: # 1 = Card
if input.is_pressed():
# TODO: Add function to rearrange the array based on positions in the dropzone
print_debug(":)")
else:
currently_dragged_area = null
2: # 2 = PostIt
if input.is_pressed():
to_handle.reparent(dropzone)
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 array based on positions in the dropzone
else:
2023-07-01 14:03:22 +00:00
if is_in_dropzone(to_handle):
if to_handle.has_overlapping_areas():
var overlaps = to_handle.get_overlapping_areas()
for area in overlaps:
if area.get_meta("type") == "card":
to_handle.reparent(area)
to_handle.set_owner(self)
to_handle.position = area.get_child(3).position
else:
for panel in area_dict["post_it_panels"]:
if panel.get_child_count() == 1:
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.get_child(0).position
break
currently_dragged_area = null
2023-07-01 14:03:22 +00:00