finishing up card refactor

This commit is contained in:
betalars 2023-11-01 23:19:47 +01:00
parent cf37714035
commit c5f0774b3a
5 changed files with 150 additions and 95 deletions

View File

@ -8,7 +8,16 @@ extends PanelContainer
#} #}
enum {NAVIGATE, ASSIGN, DRAG} enum {NAVIGATE, ASSIGN, DRAG}
var focus_stickies:bool = true var focus_stickies:bool = true:
set(stickies):
if stickies and sticky_note_container.get_child_count() == 0: return
if not current_context == ASSIGN and not focus_stickies == stickies:
if stickies:
current_sticky_note_id = current_sticky_note_id
else:
current_dropzone_id = current_dropzone_id
focus_stickies = stickies
var has_stage = false: var has_stage = false:
set(focus): set(focus):
@ -38,7 +47,7 @@ var base_sticky_note_panel: Panel
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:
NAVIGATE: NAVIGATE:
pass _return_sticky_notes_to_panels()
DRAG: DRAG:
pass pass
ASSIGN: ASSIGN:
@ -78,9 +87,14 @@ var mementos_collected: int = 0:
@onready var current_sticky_note_id: int = 0: @onready var current_sticky_note_id: int = 0:
set(new_id): set(new_id):
if new_id > sticky_note_container.get_child_count() - 1: current_sticky_note_id = 0 if sticky_note_container.get_child_count() <= 1: return
elif 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
if current_context == ASSIGN:
_return_sticky_notes_to_panels()
currently_active_node.preview_sticky_note(sticky_note_container.get_child(current_sticky_note_id).attached_sticky_note)
else:
currently_active_node = sticky_note_container.get_child(current_sticky_note_id).get_child(1) currently_active_node = sticky_note_container.get_child(current_sticky_note_id).get_child(1)
var cache: Array = [] var cache: Array = []
@ -116,19 +130,26 @@ func populate_board(card_names: Array):
# spawning the cards and adding them to the dictionary # spawning the cards and adding them to the dictionary
for new_card in all_new["cards"]: for new_card in all_new["cards"]:
new_card.position = Vector2(randi_range(dropzone_padding, dropzone_size.x), randi_range(dropzone_padding, dropzone_size.y)) add_card(new_card)
insert_area(dropzone, new_card)
new_card.set_owner(self)
new_card.is_dragable = true
for new_sticky_note in all_new["sticky_notes"]: # spawning a sticky note for new_sticky_note in all_new["sticky_notes"]: # spawning a sticky note
var new_panel = base_sticky_note_panel.duplicate() add_sticky_note(new_sticky_note)
sticky_note_container.add_child(new_panel)
new_panel.set_owner(self)
new_panel.attatch_sticky_note(new_sticky_note, false)
#currently_active_node = area_dict["dropzone_content"][0] # set first Card as currently selected node by default #currently_active_node = area_dict["dropzone_content"][0] # set first Card as currently selected node by default
currently_active_node = dropzone.get_child(0) currently_active_node = dropzone.get_child(0)
func add_card(card: Card):
card.reparent(self)
card.position = Vector2(randi_range(dropzone_padding, dropzone_size.x), randi_range(dropzone_padding, dropzone_size.y))
insert_area(dropzone, card)
card.set_owner(self)
card.is_dragable = true
func add_sticky_note(sticky: StickyNote):
var new_panel = base_sticky_note_panel.duplicate()
sticky_note_container.add_child(new_panel)
new_panel.set_owner(self)
new_panel.attatch_sticky_note(sticky, false)
# 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:
return dropzone.get_rect().has_point(to_check.global_position) return dropzone.get_rect().has_point(to_check.global_position)
@ -144,47 +165,48 @@ func handle_mouse_button(to_handle: Area2D, input: InputEvent):
currently_active_node = to_handle # update currently selected currently_active_node = to_handle # update currently selected
if input.is_action_pressed("mouse_left"): if input.is_action_pressed("mouse_left"):
to_handle.is_dragged = true to_handle.is_dragged = true
if to_handle is StickyNote:
if not to_handle.on_board:
to_handle.reparent(dropzone)
to_handle.owner = self
current_context = DRAG current_context = DRAG
if to_handle is StickyNote: # when Drag stops ...
# 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 input.is_action_released("mouse_left"):
to_handle.is_dragged = false
if to_handle is StickyNote:
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:
focus_stickies = false
if area.has_sticky_note_attached(): if area.has_sticky_note_attached():
area.exchange_sticky_note_with(to_handle).reparent(dropzone) to_handle = area.exchange_sticky_note_with(to_handle)
current_context = ASSIGN to_handle.reparent(dropzone)
sticky_note_container.get_child(current_sticky_note_id).attached_sticky_note = to_handle
current_context = NAVIGATE
return return
else: else:
area.attach_sticky_note(to_handle) area.attach_sticky_note(to_handle)
if not sticky_note_container.get_child_count() == 0:
sticky_note_container.get_child(current_sticky_note_id).clear_if_empty()
current_context = NAVIGATE current_context = NAVIGATE
return return
# when user stops dragging else:
if !is_in_dropzone(to_handle) or input.is_action_pressed("mouse_right"): _return_sticky_notes_to_panels()
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 return
# Handeling any cards and all not yet handled Sticky Notes ## Dropping Cards and Sticky Notes not causing a return condition above.
if input.is_action_released("mouse_left"):
insert_area(dropzone, to_handle) insert_area(dropzone, to_handle)
current_context = NAVIGATE current_context = NAVIGATE
focus_stickies = false focus_stickies = false
current_dropzone_id = dropzone.get_children().find(to_handle) current_dropzone_id = dropzone.get_children().find(to_handle)
if to_handle is StickyNote:
to_handle.rotation = to_handle.base_rotation to_handle.rotation = to_handle.base_rotation
to_handle.scale = to_handle.base_scale to_handle.scale = to_handle.base_scale
current_context = NAVIGATE
if input.is_action_pressed("mouse_right") and current_context == DRAG:
to_handle.reset_drag()
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():
@ -207,27 +229,29 @@ func is_board_lore() -> bool:
# 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):
if to_handle != currently_active_node:
currently_active_node.highlighted = false
currently_active_node = to_handle currently_active_node = to_handle
if is_in_dropzone(to_handle): if is_in_dropzone(to_handle) or to_handle is Card:
if to_handle is Card or (to_handle is StickyNote and to_handle.on_board): if not (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)
focus_stickies = false 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.attached_to)
focus_stickies = true 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):
var children = parent.get_children() var children:Array = parent.get_children()
var i = 0 var i = 0
if not node in get_children(): node.reparent(parent) if not node in parent.get_children():
node.reparent(parent)
node.owner = self
if children.size() > 0: if children.size() > 0:
while children[i].global_position.y > node.global_position.y and i+1 < children.size(): i+=1 children.erase(node)
while children[i].global_position.y < node.global_position.y and i+1 < children.size():
i+=1
parent.move_child(node, i) parent.move_child(node, i)
# Takes the inputs for control inputs # Takes the inputs for control inputs
@ -252,31 +276,46 @@ func _input(event):
else: else:
current_dropzone_id += 1 current_dropzone_id += 1
elif event.is_action_pressed("ui_left"): # left to switch context to the left elif event.is_action_pressed("ui_right"): # left to switch context to the left
if not focus_stickies: if not focus_stickies:
if current_context == NAVIGATE:
focus_stickies = true focus_stickies = true
elif current_context == ASSIGN:
current_context = NAVIGATE
elif event.is_action_pressed("ui_right"): # right to switch context to the right elif event.is_action_pressed("ui_left"): # right to switch context to the right
if focus_stickies: if focus_stickies:
if current_context == NAVIGATE:
focus_stickies = false focus_stickies = false
elif current_context == ASSIGN:
current_context = NAVIGATE
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
if dropzone.get_child(current_dropzone_id) is Card:
card = dropzone.get_child(current_dropzone_id)
if current_context == ASSIGN: # 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() current_dropzone_id = find_first_free_card()
else: else:
card.attach_sticky_note(currently_active_node) card.attach_sticky_note(sticky_note_container.get_child(current_sticky_note_id).attached_sticky_note)
current_context == NAVIGATE current_context = NAVIGATE
focus_stickies = false focus_stickies = false
else: else:
if focus_stickies: if !focus_stickies and card.has_sticky_note_attached():
current_context = ASSIGN
current_dropzone_id = find_first_free_card()
else:
if card.has_sticky_note_attached():
currently_active_node = card.remove_sticky_note() currently_active_node = card.remove_sticky_note()
add_sticky_note(currently_active_node)
current_dropzone_id = -1
else: current_dropzone_id = find_first_free_card()
current_context = ASSIGN
focus_stickies = !focus_stickies
if focus_stickies:
current_sticky_note_id = current_sticky_note_id
else:
current_dropzone_id = current_dropzone_id
# 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):
@ -294,3 +333,7 @@ func find_first_free_card() -> int:
if !dropzone.get_child((i+current_dropzone_id)%dropzone.get_child_count()).has_sticky_note_attached(): 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 (i+current_dropzone_id)%dropzone.get_child_count()
return -1 return -1
func on_sticky_panel_cleared():
if current_sticky_note_id == sticky_note_container.get_child_count() - 1:
current_sticky_note_id -= 1

View File

@ -155,22 +155,22 @@ func _move_card():
position += (get_viewport().get_mouse_position() - position) - mouse_offset position += (get_viewport().get_mouse_position() - position) - mouse_offset
func has_sticky_note_attached() -> bool: func has_sticky_note_attached() -> bool:
return is_instance_valid(current_sticky_note) return get_child(-1) is Card
func preview_sticky_note(sticky_note: StickyNote): func preview_sticky_note(sticky_note: StickyNote):
if has_sticky_note_attached(): sticky_note.reparent(self.get_parent())
sticky_note.tween_transform_to(sticky_note_anchor.global_transform + sticky_note.diameter) sticky_note.owner = owner
else: sticky_note.tween_transform_to(Transform2D(0, sticky_note_anchor.global_position + 0 * Vector2(sticky_note.diameter, sticky_note.diameter)))
sticky_note.tween_transform_to(sticky_note_anchor.global_transform)
func attach_sticky_note(sticky_note: StickyNote) -> bool: func attach_sticky_note(sticky_note: StickyNote) -> bool:
if is_instance_valid(current_sticky_note): if has_sticky_note_attached():
return false return false
sticky_note.reparent(get_child(3, true)) sticky_note.reparent(self)
sticky_note.position = Vector2(0,0) sticky_note.owner = self.owner
sticky_note.position = sticky_note_anchor.position
sticky_note.on_board = false sticky_note.on_board = false
current_sticky_note = sticky_note current_sticky_note = sticky_note
sticky_note.attatched_to = self sticky_note.attached_to = self
return true return true
func remove_sticky_note() -> StickyNote: func remove_sticky_note() -> StickyNote:
@ -178,8 +178,9 @@ func remove_sticky_note() -> StickyNote:
var former_child:StickyNote = current_sticky_note var former_child:StickyNote = current_sticky_note
current_sticky_note = null current_sticky_note = null
former_child.reparent(get_parent()) former_child.reparent(get_parent())
former_child.owner = self.owner
former_child.on_board = true former_child.on_board = true
former_child.attatched_to = null former_child.attached_to = null
return former_child return former_child
func exchange_sticky_note_with(new_note: StickyNote) -> StickyNote: func exchange_sticky_note_with(new_note: StickyNote) -> StickyNote:
@ -191,3 +192,10 @@ func exchange_sticky_note_with(new_note: StickyNote) -> StickyNote:
func check_hover(): func check_hover():
if is_mouse_entered: if is_mouse_entered:
_on_mouse_entered() _on_mouse_entered()
func reclaim_sticky_note():
current_sticky_note.on_board = false
current_sticky_note.tween_transform_to(sticky_note_anchor.global_transform)
await current_sticky_note.transform_tween_finished
current_sticky_note.reparent(self)
current_sticky_note.owner = self.owner

View File

@ -2,7 +2,7 @@ class_name PostItPanel
extends Panel extends Panel
@export var minimum_size:Vector2 = Vector2(400, 100) @export var minimum_size:Vector2 = Vector2(400, 100)
var attatched_sticky_note: StickyNote var attached_sticky_note: StickyNote
@onready var ancor = $"sticky-note_anchor" @onready var ancor = $"sticky-note_anchor"
func _ready(): func _ready():
@ -21,30 +21,31 @@ func attatch_sticky_note(attatchment: StickyNote, tween:bool = true):
custom_minimum_size = minimum_size custom_minimum_size = minimum_size
ancor.position = Vector2(ancor.position.x, minimum_size.y/2) ancor.position = Vector2(ancor.position.x, minimum_size.y/2)
attatchment.reparent(self) attatchment.reparent(self)
attatched_sticky_note = attatchment attached_sticky_note = attatchment
attatchment.owner = self.owner attatchment.owner = self.owner
attatchment.attatched_to = self attatchment.attached_to = self
attatchment.transform = ancor.transform attatchment.transform = ancor.transform
func reclaim_sticky_note(): func reclaim_sticky_note():
if is_empty(): if is_empty() and attached_sticky_note.on_board == true:
attatched_sticky_note.on_board = false attached_sticky_note.on_board = false
attatched_sticky_note.tween_transform_to(ancor.global_position) attached_sticky_note.tween_transform_to(ancor.global_transform)
await attatched_sticky_note.transform_tween_finished await attached_sticky_note.transform_tween_finished
attatched_sticky_note.reparent(self) attached_sticky_note.reparent(self)
attatched_sticky_note.owner = self.owner attached_sticky_note.owner = self.owner
func clear_if_empty(): func clear_if_empty():
if !is_empty(): return if !is_empty(): return
if attatched_sticky_note.attatched_to == self: attatched_sticky_note.attatched_to = null if attached_sticky_note.attached_to == self: attached_sticky_note.attached_to = null
var height_tween: Tween = create_tween() var height_tween: Tween = create_tween()
height_tween.tween_property(self, "custom_minimum_size", Vector2.ZERO, 0.3) height_tween.tween_property(self, "custom_minimum_size", Vector2.ZERO, 0.3)
await height_tween.finished await height_tween.finished
owner.on_sticky_panel_cleared()
self.free() self.free()
func replace_sticky_note_with(new_sticky_note: StickyNote): func replace_sticky_note_with(new_sticky_note: StickyNote):
if is_empty(): if is_empty():
attatched_sticky_note = new_sticky_note attached_sticky_note = new_sticky_note
func is_empty() -> bool: func is_empty() -> bool:
return ancor.get_child_count() == 0 return get_child_count() == 1

View File

@ -51,12 +51,13 @@ var is_dragged: bool = false:
is_dragged = dragged is_dragged = dragged
z_index = int(dragged) z_index = int(dragged)
var mouse_offset: Vector2 var initial_drag_position: Vector2
var mouse_diff: Vector2
@onready var diameter = $CollisionShape2D.shape.height @onready var diameter = $CollisionShape2D.shape.height
@export_range(1.0, 10.0) var bounce_speed: float = 8 @export_range(1.0, 10.0) var bounce_speed: float = 8
var on_board: bool = false var on_board: bool = false
var attatched_to: Node = null var attached_to: Node = null
func _ready() -> void: func _ready() -> void:
@ -86,7 +87,6 @@ func _process(delta: float) -> void:
_move_sticky_note() _move_sticky_note()
func _on_focus_entered(): func _on_focus_entered():
print(self, "is focused") print(self, "is focused")
@ -105,18 +105,17 @@ func _on_mouse_exited():
get_parent().check_hover() get_parent().check_hover()
func _on_input_event(viewport, event, shape_idx): func _on_input_event(viewport, event, shape_idx):
if event is InputEventMouseMotion:
_move_sticky_note()
if event is InputEventMouseButton: if event is InputEventMouseButton:
if event.button_index == MOUSE_BUTTON_LEFT or event.button_index == MOUSE_BUTTON_RIGHT: if event.button_index == MOUSE_BUTTON_LEFT or event.button_index == MOUSE_BUTTON_RIGHT:
if "handle_mouse_button" in owner: if "handle_mouse_button" in owner:
mouse_offset = (get_viewport().get_mouse_position() - global_position) mouse_diff = get_viewport().get_mouse_position()
initial_drag_position = global_position
owner.handle_mouse_button(self, event) owner.handle_mouse_button(self, event)
func _move_sticky_note(): func _move_sticky_note():
if is_dragged: if is_dragged:
position += (get_viewport().get_mouse_position() - position) - mouse_offset position = initial_drag_position + get_viewport().get_mouse_position() - mouse_diff
func is_sticky_note_attached() -> bool: func is_sticky_note_attached() -> bool:
# there is probably a nicer way to do this # there is probably a nicer way to do this
@ -127,3 +126,7 @@ func tween_transform_to(target: Transform2D):
transform_tween.tween_property(self, "transform", target, 0.25) transform_tween.tween_property(self, "transform", target, 0.25)
await transform_tween.finished await transform_tween.finished
emit_signal("transform_tween_finished") emit_signal("transform_tween_finished")
func reset_drag():
if attached_to != null:
attached_to.reclaim_sticky_note()

View File

@ -184,7 +184,7 @@ theme = ExtResource("3_mi4ah")
theme_type_variation = &"card_text" theme_type_variation = &"card_text"
autowrap_mode = 3 autowrap_mode = 3
[node name="sticky_note anchor" type="Node2D" parent="."] [node name="sticky note anchor" type="Node2D" parent="."]
position = Vector2(-65.6478, 60.3852) position = Vector2(-65.6478, 60.3852)
[connection signal="input_event" from="." to="." method="_on_input_event"] [connection signal="input_event" from="." to="." method="_on_input_event"]