diff --git a/card.gd b/card.gd index 3313497..09a0251 100644 --- a/card.gd +++ b/card.gd @@ -1,24 +1,25 @@ -extends TextureRect -#extends Sprite2D +extends Node2D ## The card class ## ## Represents an instance of a card to be displayed on the tabletop. ## Contains helper text for the text, the cards ID, and the image path. -enum pivot { ROTATE_0, ROTATE_90, ROTATE_180, ROTATE_270 } +enum pivot { ROTATE_0 = 0, ROTATE_90 = 90, ROTATE_180 = 180, ROTATE_270 = 270 } var current_pivot = pivot.ROTATE_0 +# Card information. var card_id: String var card_name: String var card_type: String var oracle_text: String -# Tween trackers for distinguishing which state the card is in. +# Card properties. +var tapped = false + +# Card input state. var is_focused = false # Is the card a focus? var is_dragging = false # Is the card currently being dragged? -var is_travelling = false # Is the card moving to a location? -var is_pivot = false # Is the card currently waiting to be tapped/untapped? var delay = 5.0 var mouse_offset: Vector2 @@ -39,36 +40,40 @@ func _pivot() -> int: func _physics_process(delta: float) -> void: + # TODO: Export handling keypresses to its own area. + if is_focused and Input.is_action_just_pressed("MAIN"): + tapped = not tapped + current_pivot = pivot.ROTATE_90 if tapped else pivot.ROTATE_0 + var tween = create_tween() + tween.tween_property(self, "rotation_degrees", current_pivot, delta * delay) + _tweening(delta) func _tweening(delta: float) -> void: - var tween = get_tree().create_tween() - - if is_focused: - tween.tween_property(self, "scale", Vector2(1.05, 1.05), delta * delay) - else: - tween.tween_property(self, "scale", Vector2(1.0, 1.0), delta * delay) + # NOTE: Constant tweens include: + # - When dragging, new tween is created each physics_process frame to move the card to the new mouse location. if is_dragging: + var tween = create_tween() tween.tween_property( self, "position", get_global_mouse_position() - mouse_offset, delay * delta ) - if is_pivot: - tween.tween_property(self, "rotation_degrees", _pivot(), delta * delay) - is_pivot = false - -func _gui_input(event: InputEvent) -> void: +func _on_input_event(viewport: Node, event: InputEvent, shape_idx: int) -> void: if event is not InputEventMouseButton: return + match event.button_index: + # MOUSE BUTTONS MOUSE_BUTTON_LEFT: if event.pressed: - is_dragging = true + Input.set_default_cursor_shape(Input.CURSOR_DRAG) mouse_offset = get_global_mouse_position() - global_position + is_dragging = true else: + Input.set_default_cursor_shape(Input.CURSOR_POINTING_HAND) is_dragging = false MOUSE_BUTTON_RIGHT: # TODO: Tooltip menu for right-button clicking on cards. @@ -76,30 +81,41 @@ func _gui_input(event: InputEvent) -> void: func _on_mouse_entered() -> void: + # Do not care about mouse entering if we're dragging the card. + if is_dragging: + return + + Input.set_default_cursor_shape(Input.CURSOR_POINTING_HAND) + + var tween = create_tween() + var focus_scale = Vector2(1.05, 1.05) + tween.tween_property(self, "scale", focus_scale, 0.1) + is_focused = true func _on_mouse_exited() -> void: - if not is_dragging: - is_focused = false + # Do not care about mouse exiting if we're dragging the card. + if is_dragging: + return + + Input.set_default_cursor_shape(Input.CURSOR_ARROW) + var tween = create_tween() + tween.tween_property(self, "scale", Vector2.ONE, 0.1) + + is_focused = false func travel_to(position: Vector2) -> Error: # TODO: Check whether position to travel to is within the confines of where this card can go. - return OK -func _unhandled_key_input(event: InputEvent) -> void: - if not event.is_action_pressed("default_action"): - return - if current_pivot == pivot.ROTATE_0: - current_pivot = pivot.ROTATE_90 - is_pivot = true - else: - current_pivot = pivot.ROTATE_0 - is_pivot = true - set_pivot_offset(size / 2) +# func _unhandled_key_input(event: InputEvent) -> void: +# if not event.is_action_pressed("default_action"): +# return + +# set_pivot_offset(size / 2) func _card_error(error_type: String) -> String: @@ -118,7 +134,7 @@ func _ready() -> void: # Setting that up can be put here later... push_error("Failed to load card.") - set_pivot_offset(size / 2) + # set_pivot_offset(size / 2) func _load_card() -> Error: @@ -166,11 +182,12 @@ func _load_image() -> Error: push_error("%sCard on-board image failed to load correctly" % _card_error("IMAGE")) return FAILED + var size = $Area2D/CollisionShape2D.shape.size image.resize(int(size.x), int(size.y), Image.INTERPOLATE_LANCZOS) var image_texture = ImageTexture.new() image_texture.set_image(image) - texture = image_texture + $Sprite2D.texture = image_texture return OK diff --git a/card.tscn b/card.tscn index 698e340..7e0d2c6 100644 --- a/card.tscn +++ b/card.tscn @@ -3,19 +3,18 @@ [ext_resource type="Script" uid="uid://b3yqd1qu7dyq" path="res://card.gd" id="1_kikvd"] [sub_resource type="RectangleShape2D" id="RectangleShape2D_kikvd"] +size = Vector2(125, 175) -[node name="Card" type="TextureRect"] -offset_left = 794.0 -offset_top = 79.0 -offset_right = 919.0 -offset_bottom = 254.0 -mouse_default_cursor_shape = 2 +[node name="Card" type="Node2D"] script = ExtResource("1_kikvd") +[node name="Sprite2D" type="Sprite2D" parent="."] + [node name="Area2D" type="Area2D" parent="."] [node name="CollisionShape2D" type="CollisionShape2D" parent="Area2D"] shape = SubResource("RectangleShape2D_kikvd") -[connection signal="mouse_entered" from="." to="." method="_on_mouse_entered"] -[connection signal="mouse_exited" from="." to="." method="_on_mouse_exited"] +[connection signal="input_event" from="Area2D" to="." method="_on_input_event"] +[connection signal="mouse_entered" from="Area2D" to="." method="_on_mouse_entered"] +[connection signal="mouse_exited" from="Area2D" to="." method="_on_mouse_exited"] diff --git a/project.godot b/project.godot index d83b7a6..7d1eaaf 100644 --- a/project.godot +++ b/project.godot @@ -27,7 +27,7 @@ enabled=PackedStringArray() [input] -default_action={ +MAIN={ "deadzone": 0.2, "events": [Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":-1,"window_id":0,"alt_pressed":false,"shift_pressed":false,"ctrl_pressed":false,"meta_pressed":false,"pressed":false,"keycode":0,"physical_keycode":32,"key_label":0,"unicode":32,"location":0,"echo":false,"script":null) ]