reworking state management in card board WIP

This commit is contained in:
TilCreator 2023-10-15 13:27:02 +02:00
parent 0b8ed363ee
commit cf37714035
2 changed files with 94 additions and 68 deletions

View File

@ -6,7 +6,9 @@ extends PanelContainer
# "sticky_notes_in_list": [], # "sticky_notes_in_list": [],
# "sticky_note_panels": [] # "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: var has_stage = false:
set(focus): set(focus):
@ -30,18 +32,16 @@ var dropzone_size: Vector2
@onready var sticky_note_container = $HBoxContainer/ScrollContainer/VBoxContainer @onready var sticky_note_container = $HBoxContainer/ScrollContainer/VBoxContainer
@onready var board_of_devs = $"board of devs" @onready var board_of_devs = $"board of devs"
var base_sticky_note_panel: Panel var base_sticky_note_panel: Panel
@onready var current_context:int = POST_IT_LIST: @onready var current_context:int = NAVIGATE:
set(context): 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() sticky_note_container.get_child(current_sticky_note_id).clear_if_empty()
match context: match context:
DROPZONE: NAVIGATE:
pass pass
POST_IT_LIST: DRAG:
pass pass
DRAGGING: ASSIGN:
pass
ASSIGN_POST_IT:
pass pass
current_context = context current_context = context
@onready var instructions = $instructions_panel/HBoxContainer/cards_remaining @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 if new_id > dropzone.get_child_count() - 1: current_dropzone_id = 0
elif new_id < 0: current_dropzone_id = dropzone.get_child_count() - 1 elif new_id < 0: current_dropzone_id = dropzone.get_child_count() - 1
else: current_dropzone_id = new_id 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) 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) currently_active_node = dropzone.get_child(current_dropzone_id)
@onready var current_sticky_note_id: int = 0: @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 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 elif new_id < 0: current_sticky_note_id = sticky_note_container.get_child_count() - 1
else: current_sticky_note_id = new_id 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 = [] var cache: Array = []
@ -100,10 +100,10 @@ func _ready():
has_stage = has_stage has_stage = has_stage
func _process(delta): #func _process(delta):
# drops dragged area when Mouse is no longer pressed. # # 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: # if has_stage and !Input.is_mouse_button_pressed(MOUSE_BUTTON_LEFT) and current_context == DRAGGING:
currently_active_node.is_dragged = false # currently_active_node.is_dragged = false
# 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(card_names: Array): 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 # 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.
# Otherwise overlapping areas are dragged at the same time. # Otherwise overlapping areas are dragged at the same time.
if current_context == DRAGGING: if current_context == DRAG and to_handle != currently_active_node:
return return
currently_active_node = to_handle # update currently selected currently_active_node = to_handle # update currently selected
to_handle.is_dragged = input.pressed if input.is_action_pressed("mouse_left"):
if input.pressed: to_handle.is_dragged = true
current_context = DRAGGING current_context = DRAG
# Check what is being dragged if to_handle is StickyNote:
if to_handle is Card: # when user stops dragging
current_context = DROPZONE if input.is_action_pressed("mouse_right") and current_context == DRAG:
if !input.is_pressed(): focus_stickies = true
insert_area(dropzone, to_handle) current_context = NAVIGATE
current_context = DROPZONE _return_sticky_notes_to_panels()
elif to_handle is StickyNote: return
if input.is_action_pressed("mouse_left"): # when user stops dragging
to_handle.reparent(dropzone) if input.is_action_released("mouse_left"):
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 is_in_dropzone(to_handle): if is_in_dropzone(to_handle):
if to_handle.has_overlapping_areas(): if to_handle.has_overlapping_areas():
for area in to_handle.get_overlapping_areas(): for area in to_handle.get_overlapping_areas():
if area is Card: if area is Card:
if area.has_sticky_note_attached(): if area.has_sticky_note_attached():
area.exchange_sticky_note_with(to_handle).reparent(dropzone) area.exchange_sticky_note_with(to_handle).reparent(dropzone)
else: current_context = ASSIGN
to_handle.rotation = to_handle.base_rotation
to_handle.scale = to_handle.base_scale return
else: else:
current_context = POST_IT_LIST area.attach_sticky_note(to_handle)
_return_sticky_notes_to_panels() 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(): func _return_sticky_notes_to_panels():
for panel in sticky_note_container.get_children(): 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 is_in_dropzone(to_handle):
if to_handle is Card or (to_handle is StickyNote and to_handle.on_board): 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_dropzone_id = dropzone.get_children().find(to_handle)
current_context = DROPZONE focus_stickies = false
else: else:
current_sticky_note_id = sticky_note_container.get_children().find(to_handle.attatched_to) 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 # Adds a child at the correct child indext in an area
func insert_area(parent: Control, node: Area2D): 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) # 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 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 != DRAG:
if current_context == POST_IT_LIST: if event.is_action_pressed("ui_up"):
current_sticky_note_id -= 1 if focus_stickies:
else: current_sticky_note_id -= 1
current_dropzone_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 elif event.is_action_pressed("ui_left"): # left to switch context to the left
if current_context == POST_IT_LIST: if not focus_stickies:
current_sticky_note_id += 1 focus_stickies = true
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_right"): # right to switch context to the right elif event.is_action_pressed("ui_right"): # right to switch context to the right
current_context = POST_IT_LIST if focus_stickies:
focus_stickies = false
elif event.is_action_pressed("ui_accept"): # select the selected note it elif event.is_action_pressed("ui_accept"): # select the selected note it
var card:Card = dropzone.get_child(current_dropzone_id) 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(): if card.has_sticky_note_attached():
currently_active_node = card.exchange_sticky_note_with(currently_active_node) currently_active_node = card.exchange_sticky_note_with(currently_active_node)
current_dropzone_id = find_first_free_card()
else: else:
card.attach_sticky_note(currently_active_node) card.attach_sticky_note(currently_active_node)
current_context = DROPZONE current_context == NAVIGATE
focus_stickies = false
else: else:
if card.has_sticky_note_attached(): if focus_stickies:
currently_active_node = card.remove_sticky_note() current_context = ASSIGN
current_context == ASSIGN_POST_IT 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 # 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): func _select_card_for_assigning(sticky_note: Area2D, card: Area2D):
@ -268,3 +287,10 @@ func on_scene_skipped(i: int):
func claim_focus(): func claim_focus():
State.pass_stage_to(self) 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

View File

@ -1,12 +1,11 @@
class_name PostItPanel class_name PostItPanel
extends Panel extends Panel
var stored_costum_minimum_size:Vector2 @export var minimum_size:Vector2 = Vector2(400, 100)
var attatched_sticky_note: StickyNote var attatched_sticky_note: StickyNote
@onready var ancor = $"sticky-note_anchor" @onready var ancor = $"sticky-note_anchor"
func _ready(): func _ready():
stored_costum_minimum_size = custom_minimum_size
ancor.position = Vector2(ancor.position.x, 0) ancor.position = Vector2(ancor.position.x, 0)
custom_minimum_size = Vector2(custom_minimum_size.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 attatchment.on_board = false
if tween: if tween:
var height_tween: Tween = create_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(self, "custom_minimum_size", 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(ancor, "position", Vector2(ancor.position.x, minimum_size.y/2), 0.3)
attatchment.tween_transform_to(ancor.global_position) attatchment.tween_transform_to(ancor.global_position)
await attatchment.transform_tween_finished await attatchment.transform_tween_finished
else: 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) attatchment.reparent(self)
attatched_sticky_note = attatchment attatched_sticky_note = attatchment
attatchment.owner = self.owner attatchment.owner = self.owner