populate_board() to spawn cards/postits

This commit is contained in:
Adrian Schmid 2023-07-03 20:27:09 +02:00
parent bfd6649ce9
commit daf073c6b4
2 changed files with 61 additions and 28 deletions

View File

@ -4,6 +4,8 @@ var area_dict = {}
enum ui_context {DROPZONE, POST_IT_LIST, ASSIGN_POST_IT} enum ui_context {DROPZONE, POST_IT_LIST, ASSIGN_POST_IT}
@onready var dropzone = $HBoxContainer/dropzone @onready var dropzone = $HBoxContainer/dropzone
@onready var postit_container = $HBoxContainer/ScrollContainer/VBoxContainer
@onready var board_of_devs = $"board of devs"
@onready var active_context = ui_context.DROPZONE # 0 = dropzone, 1 = post it list @onready var active_context = ui_context.DROPZONE # 0 = dropzone, 1 = post it list
var currently_selected_node: Area2D = null var currently_selected_node: Area2D = null
@ -17,9 +19,11 @@ var selected_card_for_assignment
# 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():
populate_board()
reorder_areas("dropzone_content")
var test_arr = ["c_Joy","p_effort","c_backlash","c_body","c_hit","p_slut","p_worried_mother","p_cross_friend"]
populate_board(test_arr)
reorder_areas("dropzone_content")
active_context = ui_context.DROPZONE active_context = ui_context.DROPZONE
@ -32,28 +36,59 @@ func _process(delta):
is_area_dragged = false is_area_dragged = false
currently_dragged_area = null currently_dragged_area = null
#if active_context == ui_context.ASSIGN_POST_IT:
# Will be used later to spawn Cards and Post-Its and remember them in the dictionary # Will be used later to spawn Cards and Post-Its and remember them in the dictionary
func populate_board(): func populate_board(card_names: Array):
# TODO: Currently populating the dictionary with the Nodes currently in the scene var all_cards = Array()
# When opening the scene we need to pass some kind of collection to the Board var all_postits = Array()
# 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: # define entries in dictionary
post_its.push_back(panel.get_child(1)) area_dict["dropzone_content"] = Array()
area_dict["post_its_in_list"] = Array()
area_dict["dropzone_content"] = cards # will be selected on the left side # to remember panel positions
area_dict["post_it_panels"] = post_it_panels # to remember panel positions area_dict["post_it_panels"] = get_child(0).get_child(1).get_child(0).get_children()
area_dict["post_its_in_list"] = post_its # will be selected on the right side
# get all the cards and post-its from the board of devs
for child in board_of_devs.get_children():
for child_2 in child.get_children(): # put all cards in all_cards array
all_cards.push_back(child_2)
for child_3 in child_2.get_children(): # put all post-its in all_postits array
if "p_" in child_3.name: # post-its are currently children of cards.
all_postits.push_back(child_3) # If this changes, this logic needs to be adjusted.
# spawning the cards and adding them to the dictionary
for card_name in card_names:
if "c_" in card_name: # spawning a card
var new_card = _find_area_by_string(all_cards, card_name).duplicate()
for child in new_card.get_children(): # We need to clear all the post-its from the cards on the dev-board
if "p_" in child.name:
new_card.remove_child(child) # dev-board has post-its attached to the cards, which need to be removed
dropzone.add_child(new_card)
new_card.set_owner(self)
area_dict["dropzone_content"].push_back(new_card)
new_card.global_position = Vector2(300, 200) # using hard-coded position because I'm lazy
new_card.is_dragable = true
if "p_" in card_name: # spawning a post-it
var new_postit = _find_area_by_string(all_postits, card_name).duplicate()
for panel in area_dict["post_it_panels"]: # still mad I can't use the return_postit-func for this...
if panel.get_child_count() == 1:
panel.add_child(new_postit)
new_postit.set_owner(self)
area_dict["post_its_in_list"].push_back(new_postit)
new_postit.position = panel.get_child(0).position
new_postit.is_dragable = true
break
currently_selected_node = area_dict["dropzone_content"][0] # set first Card as currently selected node by default currently_selected_node = area_dict["dropzone_content"][0] # set first Card as currently selected node by default
# Handy function to filter an array of areas by the name of a card
func _find_area_by_string(area_arr: Array, name: String) -> Area2D:
for area in area_arr:
if area.name == name:
return area
return null
# Checks if a Node is currently inside the dropzone # Checks if a Node is currently inside the dropzone
func is_in_dropzone(to_check: Node) -> bool: func is_in_dropzone(to_check: Node) -> bool:
@ -64,7 +99,7 @@ func is_in_dropzone(to_check: Node) -> bool:
else: else:
return true return true
# called if a mouse button is pressed
func handle_mouse_button(to_handle: Area2D, input: InputEvent): func handle_mouse_button(to_handle: Area2D, input: InputEvent):
# No two areas can be dragged at the same time. # No two areas can be dragged at the same time.
# Make sure that only the same area is dragged. # Make sure that only the same area is dragged.
@ -126,7 +161,8 @@ func attach_postit_to_card(postit: Area2D, card: Area2D, update_dict = false):
# Mark area that was hovered over as currently selected # Mark area that was hovered over as currently selected
func handle_hover(to_handle: Area2D): func handle_hover(to_handle: Area2D):
currently_selected_node.highlighted = false if to_handle != currently_selected_node:
currently_selected_node.highlighted = false
currently_selected_node = to_handle currently_selected_node = to_handle
if is_in_dropzone(to_handle): if is_in_dropzone(to_handle):
@ -138,7 +174,7 @@ func handle_hover(to_handle: Area2D):
# Reorders the areas in any of the dictionaries entries # Reorders the areas in any of the dictionaries entries
# Pass the entries key in order to reorder it # Pass the entry's key in order to reorder it
func reorder_areas(reorder: String): func reorder_areas(reorder: String):
var old_order = area_dict[reorder] var old_order = area_dict[reorder]
var new_order = Array() var new_order = Array()
@ -193,6 +229,7 @@ func _input(event):
else: else:
_enter_assignment_context() _enter_assignment_context()
# TODO: I forgor the HECKING RIGHT-CLICK!!!!111 AAAAAAAAAAAAAAAAAAAA
# do some adjustments to loop elements (after last element, select first one etc.) # do some adjustments to loop elements (after last element, select first one etc.)
if selected_dropzone_element < 0: if selected_dropzone_element < 0:
@ -248,14 +285,12 @@ func _enter_assignment_context():
currently_selected_card_for_assigning = area_dict["dropzone_content"][0] currently_selected_card_for_assigning = area_dict["dropzone_content"][0]
currently_selected_card_for_assigning.highlighted = true currently_selected_card_for_assigning.highlighted = true
# leaves the context for assigning postit via button controls # leaves the context for assigning postit via button controls
func _leave_assignment_context(): func _leave_assignment_context():
currently_selected_node.highlighted = false currently_selected_node.highlighted = false
active_context = ui_context.DROPZONE active_context = ui_context.DROPZONE
currently_selected_node = currently_selected_card_for_assigning currently_selected_node = currently_selected_card_for_assigning
# handles everything to return a post it to the panels # handles everything to return a post it to the panels
func _return_postit_to_panels(postit: Area2D): func _return_postit_to_panels(postit: Area2D):
for panel in area_dict["post_it_panels"]: for panel in area_dict["post_it_panels"]:

View File

@ -46,15 +46,13 @@ var modulate_tween
@export var voice_line: AudioStream = null @export var voice_line: AudioStream = null
@export var is_dragable: bool = false @export var is_dragable: bool = false
var base_rotation = null @onready var base_rotation = rotation
var base_scale = null @onready var base_scale = scale
var is_dragged = false var is_dragged = false
func _ready() -> void: func _ready() -> void:
self.set_meta("type", "post-it") # set type information to find out if this node is a post-it self.set_meta("type", "post-it") # set type information to find out if this node is a post-it
base_rotation = rotation
base_scale = scale
$Content/Label.text = self.text $Content/Label.text = self.text
$Content/BackgroundSprite.frame = text.hash() % $Content/BackgroundSprite.sprite_frames.get_frame_count($Content/BackgroundSprite.animation) $Content/BackgroundSprite.frame = text.hash() % $Content/BackgroundSprite.sprite_frames.get_frame_count($Content/BackgroundSprite.animation)