exports card input handling to seperate node and script
This commit is contained in:
parent
933e6b715c
commit
2f5185d22c
@ -21,26 +21,8 @@ var focused: bool # Is this card currently a focus?
|
||||
var mouse_offset: Vector2
|
||||
|
||||
|
||||
func _physics_process(delta: float) -> void:
|
||||
focused = hovered or dragging
|
||||
|
||||
if focused:
|
||||
# TODO: Export handling keypresses to its own area.
|
||||
if Input.is_action_just_pressed("MAIN"):
|
||||
tapped = not tapped
|
||||
$TweenController.tap(tapped, delta)
|
||||
|
||||
if Input.is_action_just_pressed("SELECT"):
|
||||
dragging = true
|
||||
Input.set_default_cursor_shape(Input.CURSOR_DRAG)
|
||||
mouse_offset = get_global_mouse_position() - global_position
|
||||
|
||||
if Input.is_action_just_released("SELECT"):
|
||||
dragging = false
|
||||
check_hover()
|
||||
|
||||
if dragging:
|
||||
$TweenController.move_to(get_global_mouse_position() - mouse_offset, delta)
|
||||
func init(id: String) -> void:
|
||||
card_id = id
|
||||
|
||||
|
||||
# This is called when we want to apply the behaviour of the mouse being
|
||||
@ -52,6 +34,13 @@ func check_hover() -> void:
|
||||
_on_mouse_exited()
|
||||
|
||||
|
||||
func _physics_process(delta: float) -> void:
|
||||
focused = hovered or dragging
|
||||
|
||||
$InputHandler.handle_inputs(delta)
|
||||
$TweenController.handle_constant_tweens(delta)
|
||||
|
||||
|
||||
func _on_mouse_entered() -> void:
|
||||
hovered = true
|
||||
|
||||
@ -78,10 +67,6 @@ func _card_error(error_type: String) -> String:
|
||||
return "ERROR::CARD::%s::%s::%s::\n" % [card_id, card_name, error_type]
|
||||
|
||||
|
||||
func init(id: String) -> void:
|
||||
card_id = id
|
||||
|
||||
|
||||
func _ready() -> void:
|
||||
var load_status = _load_card()
|
||||
if load_status != OK:
|
||||
|
@ -1,7 +1,8 @@
|
||||
[gd_scene load_steps=4 format=3 uid="uid://cah3mvdnom1xg"]
|
||||
[gd_scene load_steps=5 format=3 uid="uid://cah3mvdnom1xg"]
|
||||
|
||||
[ext_resource type="Script" uid="uid://b3yqd1qu7dyq" path="res://scenes/card/card.gd" id="1_kikvd"]
|
||||
[ext_resource type="Script" uid="uid://bkk0pyypi1id7" path="res://scenes/card/tween.gd" id="2_imta7"]
|
||||
[ext_resource type="Script" uid="uid://dhgk6fhw8oua0" path="res://scenes/card/input.gd" id="3_vtcvk"]
|
||||
|
||||
[sub_resource type="RectangleShape2D" id="RectangleShape2D_kikvd"]
|
||||
size = Vector2(125, 175)
|
||||
@ -18,7 +19,9 @@ shape = SubResource("RectangleShape2D_kikvd")
|
||||
|
||||
[node name="TweenController" type="Node2D" parent="."]
|
||||
script = ExtResource("2_imta7")
|
||||
move_speed = 50.0
|
||||
|
||||
[node name="InputHandler" type="Node2D" parent="."]
|
||||
script = ExtResource("3_vtcvk")
|
||||
|
||||
[connection signal="mouse_entered" from="Area2D" to="." method="_on_mouse_entered"]
|
||||
[connection signal="mouse_exited" from="Area2D" to="." method="_on_mouse_exited"]
|
||||
|
@ -0,0 +1,27 @@
|
||||
extends Node2D
|
||||
|
||||
var card: Node
|
||||
var tween_controller: Node
|
||||
|
||||
|
||||
func _ready() -> void:
|
||||
card = get_parent()
|
||||
tween_controller = card.get_node("TweenController")
|
||||
|
||||
|
||||
func handle_inputs(delta: float) -> void:
|
||||
if not card.focused:
|
||||
# TODO: Global card actions, e.g. untapping everything.
|
||||
return
|
||||
|
||||
if Input.is_action_just_pressed("MAIN"):
|
||||
card.tapped = not card.tapped
|
||||
tween_controller.tap(card.tapped, delta)
|
||||
|
||||
if Input.is_action_just_pressed("SELECT"):
|
||||
card.dragging = true
|
||||
Input.set_default_cursor_shape(Input.CURSOR_DRAG)
|
||||
card.mouse_offset = get_global_mouse_position() - card.global_position
|
||||
if Input.is_action_just_released("SELECT"):
|
||||
card.dragging = false
|
||||
card.check_hover()
|
@ -4,19 +4,32 @@ extends Node
|
||||
@export var tap_speed = 5.0
|
||||
@export var scale_speed = 0.1
|
||||
|
||||
var card: Node
|
||||
|
||||
# TODO: Figure out elastic tween transitions for bounciness.
|
||||
|
||||
|
||||
func handle_constant_tweens(delta: float) -> void:
|
||||
if card.dragging:
|
||||
move_to(card.get_global_mouse_position() - card.mouse_offset, delta)
|
||||
|
||||
|
||||
func move_to(location: Vector2, delta: float) -> void:
|
||||
var tween = create_tween()
|
||||
tween.tween_property(get_parent(), "position", location, delta * move_speed)
|
||||
tween.tween_property(card, "position", location, delta * move_speed)
|
||||
|
||||
|
||||
func tap(tapped: bool, delta: float) -> void:
|
||||
var tween = create_tween()
|
||||
var rotation = 90 if tapped else 0
|
||||
tween.tween_property(get_parent(), "rotation_degrees", rotation, delta * tap_speed)
|
||||
tween.tween_property(card, "rotation_degrees", rotation, delta * tap_speed)
|
||||
|
||||
|
||||
func scale(scalar: float) -> void:
|
||||
var tween = create_tween()
|
||||
var new_scale = Vector2.ONE * scalar
|
||||
tween.tween_property(get_parent(), "scale", new_scale, scale_speed)
|
||||
tween.tween_property(card, "scale", new_scale, scale_speed)
|
||||
|
||||
|
||||
func _ready() -> void:
|
||||
card = get_parent()
|
||||
|
Loading…
x
Reference in New Issue
Block a user