CardBase class with hover functionality (up and down card);
CardInputComponent class which registers the cursor hover and sends a EventBus signal about it with an indication of the card;
PlayerCardsController class for handle signals about cursor entering on card.
Current PlayerCardsController code:
## Is the cursor currently hovering over card
var _is_card_hovered: bool = false
## When the cursor is placed on card
func _on_card_cursor_entered(card: CardBase) -> void:
if _is_card_hovered:
return
if card.state != Global.CardState.IN_HAND:
return
EventBus.card_hover_started.emit(card)
_is_card_hovered = true
## When the cursor left card
func _on_card_cursor_exited(card: CardBase) -> void:
if not _is_card_hovered:
return
if card.state != Global.CardState.IN_HAND_HOVERED:
return
EventBus.card_hover_stopped.emit(card)
_is_card_hovered = false
## Connect methods to EventBus signals
func _connect_to_signals() -> void:
EventBus.card_cursor_entered.connect(_on_card_cursor_entered)
EventBus.card_cursor_exited.connect(_on_card_cursor_exited)
2 questions:
Maybe you see incorrect hover of cards by order? How to correct handle cursor enter in that situation?
What I can use for detect another card and hover it when cursor left from current card?
If you need to more info, please let me know. Thanks for any help!
What’s catching the entered event? Areas? If yes, they are not sorted according to scene tree order and, by default, their order may change between frames. Either use control nodes instead or enable main viewport’s physics_object_picking_sort and if needed physics_object_picking_first_only
Yes, Area2D node catching that. At my previous variant of that project I tried to manipulating with physics for fix that, but it’s hard code. I try way with Control nodes, that look’s like simple.
You shouldn’t do this by querying physics state. What’s the benefit of doing it this way? Just let the mouse_entered and mouse_exited signals do the job. This will obey the sorting order.
Ok, I try @normalized method and it’s fix only my first question.
Before I try to redo everything on Control, I want to ask: can I use gui_input for hanlde player mouse clicks on Card?
You need to use area’s mouse_entered and mouse_exited signals. The logic will be exactly the same as with control nodes. I’d prefer using controls over areas though, but both should work equally well given that those viewport flags are set for areas.