From cf37714035d23ae2ea938d822fa2ae2f82fa216f Mon Sep 17 00:00:00 2001 From: TilCreator Date: Sun, 15 Oct 2023 13:27:02 +0200 Subject: [PATCH] reworking state management in card board WIP --- src/logic-scenes/board/card-board.gd | 152 ++++++++++-------- .../board/empty_sticky_note_panel.gd | 10 +- 2 files changed, 94 insertions(+), 68 deletions(-) diff --git a/src/logic-scenes/board/card-board.gd b/src/logic-scenes/board/card-board.gd index 2b18d17..f542728 100644 --- a/src/logic-scenes/board/card-board.gd +++ b/src/logic-scenes/board/card-board.gd @@ -6,7 +6,9 @@ extends PanelContainer # "sticky_notes_in_list": [], # "sticky_note_panels": [] #} -enum {DROPZONE, POST_IT_LIST, DRAGGING, ASSIGN_POST_IT} +enum {NAVIGATE, ASSIGN, DRAG} + +var focus_stickies:bool = true var has_stage = false: set(focus): @@ -30,18 +32,16 @@ var dropzone_size: Vector2 @onready var sticky_note_container = $HBoxContainer/ScrollContainer/VBoxContainer @onready var board_of_devs = $"board of devs" var base_sticky_note_panel: Panel -@onready var current_context:int = POST_IT_LIST: +@onready var current_context:int = NAVIGATE: set(context): - if current_context == ASSIGN_POST_IT and !context == ASSIGN_POST_IT: + if current_context == ASSIGN and !context == ASSIGN: sticky_note_container.get_child(current_sticky_note_id).clear_if_empty() match context: - DROPZONE: + NAVIGATE: pass - POST_IT_LIST: + DRAG: pass - DRAGGING: - pass - ASSIGN_POST_IT: + ASSIGN: pass current_context = context @onready var instructions = $instructions_panel/HBoxContainer/cards_remaining @@ -71,9 +71,9 @@ var mementos_collected: int = 0: if new_id > dropzone.get_child_count() - 1: current_dropzone_id = 0 elif new_id < 0: current_dropzone_id = dropzone.get_child_count() - 1 else: current_dropzone_id = new_id - if current_context == ASSIGN_POST_IT: + if current_context == ASSIGN: dropzone.get_child(current_dropzone_id).preview_sticky_note(currently_active_node) - elif current_context == DROPZONE: + elif not focus_stickies: currently_active_node = dropzone.get_child(current_dropzone_id) @onready var current_sticky_note_id: int = 0: @@ -81,7 +81,7 @@ var mementos_collected: int = 0: if new_id > sticky_note_container.get_child_count() - 1: current_sticky_note_id = 0 elif new_id < 0: current_sticky_note_id = sticky_note_container.get_child_count() - 1 else: current_sticky_note_id = new_id - currently_active_node = sticky_note_container.get_child(current_sticky_note_id).get_child(0) + currently_active_node = sticky_note_container.get_child(current_sticky_note_id).get_child(1) var cache: Array = [] @@ -100,10 +100,10 @@ func _ready(): has_stage = has_stage -func _process(delta): - # drops dragged area when Mouse is no longer pressed. - if has_stage and !Input.is_mouse_button_pressed(MOUSE_BUTTON_LEFT) and current_context == DRAGGING: - currently_active_node.is_dragged = false +#func _process(delta): +# # drops dragged area when Mouse is no longer pressed. +# if has_stage and !Input.is_mouse_button_pressed(MOUSE_BUTTON_LEFT) and current_context == DRAGGING: +# currently_active_node.is_dragged = false # Will be used later to spawn Cards and Post-Its and remember them in the dictionary func populate_board(card_names: Array): @@ -135,44 +135,56 @@ func is_in_dropzone(to_check: Node) -> bool: # called if a mouse button is pressed func handle_mouse_button(to_handle: Area2D, input: InputEvent): - # 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 current_context == DRAGGING: + if current_context == DRAG and to_handle != currently_active_node: return currently_active_node = to_handle # update currently selected - to_handle.is_dragged = input.pressed - if input.pressed: - current_context = DRAGGING + if input.is_action_pressed("mouse_left"): + to_handle.is_dragged = true + current_context = DRAG - # Check what is being dragged - if to_handle is Card: - current_context = DROPZONE - if !input.is_pressed(): - insert_area(dropzone, to_handle) - current_context = DROPZONE - elif to_handle is StickyNote: - if input.is_action_pressed("mouse_left"): - to_handle.reparent(dropzone) - to_handle.on_board = true - to_handle.set_owner(self) # needs to be here otherwise the owner disappears - if input.is_action_pressed("mouse_right"): - _return_sticky_notes_to_panels() - else: + if to_handle is StickyNote: + # when user stops dragging + if input.is_action_pressed("mouse_right") and current_context == DRAG: + focus_stickies = true + current_context = NAVIGATE + _return_sticky_notes_to_panels() + return + # when user stops dragging + if input.is_action_released("mouse_left"): if is_in_dropzone(to_handle): if to_handle.has_overlapping_areas(): for area in to_handle.get_overlapping_areas(): if area is Card: if area.has_sticky_note_attached(): area.exchange_sticky_note_with(to_handle).reparent(dropzone) - else: - to_handle.rotation = to_handle.base_rotation - to_handle.scale = to_handle.base_scale - else: - current_context = POST_IT_LIST - _return_sticky_notes_to_panels() + current_context = ASSIGN + + return + else: + area.attach_sticky_note(to_handle) + current_context = NAVIGATE + return + # when user stops dragging + if !is_in_dropzone(to_handle) or input.is_action_pressed("mouse_right"): + to_handle.reparent(dropzone) + to_handle.on_board = true + to_handle.set_owner(self) # needs to be here otherwise the owner disappears + current_context = NAVIGATE + return + + # Handeling any cards and all not yet handled Sticky Notes + if input.is_action_released("mouse_left"): + insert_area(dropzone, to_handle) + current_context = NAVIGATE + focus_stickies = false + current_dropzone_id = dropzone.get_children().find(to_handle) + to_handle.rotation = to_handle.base_rotation + to_handle.scale = to_handle.base_scale + current_context = NAVIGATE func _return_sticky_notes_to_panels(): for panel in sticky_note_container.get_children(): @@ -202,10 +214,10 @@ func handle_hover(to_handle: Area2D): if is_in_dropzone(to_handle): if to_handle is Card or (to_handle is StickyNote and to_handle.on_board): current_dropzone_id = dropzone.get_children().find(to_handle) - current_context = DROPZONE + focus_stickies = false else: current_sticky_note_id = sticky_note_container.get_children().find(to_handle.attatched_to) - current_context = POST_IT_LIST + focus_stickies = true # Adds a child at the correct child indext in an area func insert_area(parent: Control, node: Area2D): @@ -227,37 +239,44 @@ func _input(event): # Return, if the input is a mouse event (mouse events are handled separately) if event is InputEventMouse or !has_stage or not is_instance_valid(currently_active_node): return - if event.is_action_pressed("ui_up"): # up to select an element above - if current_context == POST_IT_LIST: - current_sticky_note_id -= 1 - else: - current_dropzone_id -= 1 + if current_context != DRAG: + if event.is_action_pressed("ui_up"): + if focus_stickies: + current_sticky_note_id -= 1 + else: + current_dropzone_id -= 1 + + elif event.is_action_pressed("ui_down"): # down to select an element beneath + if focus_stickies: + current_sticky_note_id += 1 + else: + current_dropzone_id += 1 - elif event.is_action_pressed("ui_down"): # down to select an element beneath - if current_context == POST_IT_LIST: - current_sticky_note_id += 1 - else: - current_dropzone_id += 1 - - elif event.is_action_pressed("ui_left"): # left to switch context to the left - if current_context == POST_IT_LIST: - current_context = DROPZONE + elif event.is_action_pressed("ui_left"): # left to switch context to the left + if not focus_stickies: + focus_stickies = true - elif event.is_action_pressed("ui_right"): # right to switch context to the right - current_context = POST_IT_LIST + elif event.is_action_pressed("ui_right"): # right to switch context to the right + if focus_stickies: + focus_stickies = false elif event.is_action_pressed("ui_accept"): # select the selected note it var card:Card = dropzone.get_child(current_dropzone_id) - if current_context == ASSIGN_POST_IT: # to assign it to a card + if current_context == ASSIGN: # to assign it to a card if card.has_sticky_note_attached(): currently_active_node = card.exchange_sticky_note_with(currently_active_node) + current_dropzone_id = find_first_free_card() else: card.attach_sticky_note(currently_active_node) - current_context = DROPZONE + current_context == NAVIGATE + focus_stickies = false else: - if card.has_sticky_note_attached(): - currently_active_node = card.remove_sticky_note() - current_context == ASSIGN_POST_IT + if focus_stickies: + current_context = ASSIGN + current_dropzone_id = find_first_free_card() + else: + if card.has_sticky_note_attached(): + currently_active_node = card.remove_sticky_note() # move the note it so it floats next to the card where it should be attached func _select_card_for_assigning(sticky_note: Area2D, card: Area2D): @@ -268,3 +287,10 @@ func on_scene_skipped(i: int): func claim_focus(): State.pass_stage_to(self) + +func find_first_free_card() -> int: + for i in range(dropzone.get_child_count()): + # start searching at the current location, use modulo to avoid getting out of array bounds + if !dropzone.get_child((i+current_dropzone_id)%dropzone.get_child_count()).has_sticky_note_attached(): + return (i+current_dropzone_id)%dropzone.get_child_count() + return -1 diff --git a/src/logic-scenes/board/empty_sticky_note_panel.gd b/src/logic-scenes/board/empty_sticky_note_panel.gd index 231e5d4..2f34d69 100644 --- a/src/logic-scenes/board/empty_sticky_note_panel.gd +++ b/src/logic-scenes/board/empty_sticky_note_panel.gd @@ -1,12 +1,11 @@ class_name PostItPanel extends Panel -var stored_costum_minimum_size:Vector2 +@export var minimum_size:Vector2 = Vector2(400, 100) var attatched_sticky_note: StickyNote @onready var ancor = $"sticky-note_anchor" func _ready(): - stored_costum_minimum_size = custom_minimum_size ancor.position = Vector2(ancor.position.x, 0) custom_minimum_size = Vector2(custom_minimum_size.x, 0) @@ -14,12 +13,13 @@ func attatch_sticky_note(attatchment: StickyNote, tween:bool = true): attatchment.on_board = false if tween: var height_tween: Tween = create_tween() - height_tween.tween_property(self, "custom_minimum_size", stored_costum_minimum_size, 0.3) - height_tween.tween_property(ancor, "position", Vector2(ancor.position.x, stored_costum_minimum_size.y/2), 0.3) + height_tween.tween_property(self, "custom_minimum_size", minimum_size, 0.3) + height_tween.tween_property(ancor, "position", Vector2(ancor.position.x, minimum_size.y/2), 0.3) attatchment.tween_transform_to(ancor.global_position) await attatchment.transform_tween_finished else: - custom_minimum_size = stored_costum_minimum_size + custom_minimum_size = minimum_size + ancor.position = Vector2(ancor.position.x, minimum_size.y/2) attatchment.reparent(self) attatched_sticky_note = attatchment attatchment.owner = self.owner