adding mouse support for card picker, WIP

This commit is contained in:
betalars 2023-06-30 00:54:34 +02:00
parent b20e519b7b
commit 37eea81463
1 changed files with 23 additions and 3 deletions

View File

@ -1,5 +1,6 @@
extends CenterContainer extends CenterContainer
#fixme INI is probably redundant.
enum { enum {
INI, INI,
CARDS, CARDS,
@ -20,18 +21,19 @@ var has_focus = false:
has_focus = focus has_focus = focus
var selection_state = INI var selection_state = INI
var input_locked = true
var anim_players:Array var anim_players:Array
var curr_selection_id: int = -1: var curr_selection_id: int = -1:
set(new_id): set(new_id):
if selection_state == CARDS or selection_state == POSTS: if selection_state == CARDS or selection_state == POSTS:
if not curr_selection_id == -1: anim_players[curr_selection_id].play("deselect") if not curr_selection_id == -1: options[curr_selection_id].highlighted = false
if new_id > options.size() -1: curr_selection_id = 0 if new_id > options.size() -1: curr_selection_id = 0
elif new_id < 0: curr_selection_id = options.size() - 1 elif new_id < 0: curr_selection_id = options.size() - 1
else: curr_selection_id = new_id else: curr_selection_id = new_id
anim_players[curr_selection_id].play("select") options[curr_selection_id].highlighted = true
else: else:
curr_selection_id = new_id curr_selection_id = new_id
@ -48,11 +50,16 @@ func _ready():
anim_players.append(control.get_child(0)) anim_players.append(control.get_child(0))
fill_card_slots() fill_card_slots()
#fixme: when thisa scene is launched on it's own, state will assign focus.
State.pass_focus_to(self)
input_locked = false
func fill_card_slots(): func fill_card_slots():
for i in range($cards.get_child_count()): for i in range($cards.get_child_count()):
var card:Card = $cards.get_child(i).get_child(1) var card:Card = $cards.get_child(i).get_child(1)
card.replace_with(debug_board.get_child(0).get_child(i) as Card) card.replace_with(debug_board.get_child(0).get_child(i) as Card)
card.connect("mouse_entered", Callable(self, "get_highlight"))
func fill_post_slots(): func fill_post_slots():
var post_its: Array[PostIt] = [] var post_its: Array[PostIt] = []
@ -64,7 +71,7 @@ func fill_post_slots():
options[i].replace_with(post_its[i]) options[i].replace_with(post_its[i])
func _unhandled_input(event): func _unhandled_input(event):
if has_focus: if has_focus and not input_locked:
if event.is_action_pressed("ui_up") or event.is_action_pressed("ui_left") or event.is_action_pressed("ui_focus_next"): if event.is_action_pressed("ui_up") or event.is_action_pressed("ui_left") or event.is_action_pressed("ui_focus_next"):
curr_selection_id -= 1 curr_selection_id -= 1
elif event.is_action_pressed("ui_down") or event.is_action_pressed("ui_right") or event.is_action_pressed("ui_focus_prev"): elif event.is_action_pressed("ui_down") or event.is_action_pressed("ui_right") or event.is_action_pressed("ui_focus_prev"):
@ -76,6 +83,9 @@ func pick(id: int):
if id == -1: if id == -1:
curr_selection_id = 0 curr_selection_id = 0
return return
input_locked = true
anim_players[id].play("pick") anim_players[id].play("pick")
var yield_to = anim_players[id].animation_finished var yield_to = anim_players[id].animation_finished
output.append(options[id]) output.append(options[id])
@ -120,3 +130,13 @@ func pick(id: int):
await anim_players[0].animation_finished await anim_players[0].animation_finished
selection_state = POSTS selection_state = POSTS
input_locked = false
func handle_hover(new_highlight):
if not input_locked:
curr_selection_id = options.find(new_highlight)
func handle_mouse_button(new_selection: Node, button_event: InputEventMouseButton):
if not input_locked:
if button_event.button_index == MOUSE_BUTTON_LEFT and button_event.pressed:
pick(options.find(new_selection))