Skip to content

Commit

Permalink
Merge pull request #46 from ligen131/feat-20240215-cursor-manager
Browse files Browse the repository at this point in the history
添加用于管理鼠标指针动画的类 CursorManager
  • Loading branch information
cutekibry authored Feb 15, 2024
2 parents 3565ebf + 7faa392 commit 3cd0a9c
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 14 deletions.
7 changes: 1 addition & 6 deletions main.gd
Original file line number Diff line number Diff line change
@@ -1,15 +1,10 @@
extends Node


const CURSOR_ARROW := preload("res://bg/cursor/cursor_arrow.png")
const CURSOR_POINTING_HAND := preload("res://bg/cursor/cursor_pointing_hand.png")
const CURSOR_DRAG := preload("res://bg/cursor/cursor_drag.png")



func _ready():
Input.set_custom_mouse_cursor(CURSOR_ARROW)
Input.set_custom_mouse_cursor(CURSOR_POINTING_HAND, Input.CURSOR_POINTING_HAND)
Input.set_custom_mouse_cursor(CURSOR_DRAG, Input.CURSOR_DRAG)


func _on_main_menu_enter_level():
Expand Down
5 changes: 4 additions & 1 deletion main.tscn
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
[gd_scene load_steps=5 format=3 uid="uid://c17fbsiogbgo1"]
[gd_scene load_steps=6 format=3 uid="uid://c17fbsiogbgo1"]

[ext_resource type="PackedScene" uid="uid://c07co5p46apu7" path="res://objects/main_menu/main_menu.tscn" id="1_fk6j6"]
[ext_resource type="Script" path="res://main.gd" id="1_nb6uf"]
[ext_resource type="PackedScene" uid="uid://d3geq38s5fjc6" path="res://bg/dynamic_bg/dynamic_bg.tscn" id="2_8k4il"]
[ext_resource type="AudioStream" uid="uid://cr3nkhf0fejm5" path="res://levels/base_level/bgm.wav" id="3_n81it"]
[ext_resource type="PackedScene" uid="uid://prht3u5pnjls" path="res://scripts/cursor_manager/cursor_manager.tscn" id="5_pqych"]

[node name="Main" type="Node"]
script = ExtResource("1_nb6uf")
Expand All @@ -20,5 +21,7 @@ stream = ExtResource("3_n81it")
volume_db = -4.685
attenuation = 0.0001

[node name="CursorManager" parent="." instance=ExtResource("5_pqych")]

[connection signal="enter_level" from="MainMenu" to="." method="_on_main_menu_enter_level"]
[connection signal="finished" from="BGMPlayer" to="." method="_on_bgm_player_finished"]
5 changes: 0 additions & 5 deletions objects/card/card.gd
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,6 @@ func put_down():
$SFXPutDown.play()

entered_block.set_card(self)

Input.set_default_cursor_shape(Input.CURSOR_POINTING_HAND) # 设置鼠标指针形状为手形

else:
queue_free()
Expand Down Expand Up @@ -112,7 +110,6 @@ func _ready():
func _process(_delta: float) -> void:
if self.is_dragging: # 如果正在拖拽
self.global_position = get_global_mouse_position().round() # 将卡牌位置设置为鼠标位置的全局位置,四舍五入取整
Input.set_default_cursor_shape(Input.CURSOR_DRAG) # 设置鼠标指针形状为拖拽形状

var offset := 0
if self.is_shaking:
Expand Down Expand Up @@ -143,12 +140,10 @@ func _input_event(_viewport: Object, event: InputEvent, _shape_idx: int) -> void

func _on_mouse_entered():
$HighlightSprite.visible = true
Input.set_default_cursor_shape(Input.CURSOR_POINTING_HAND) # 设置鼠标指针形状为手形


func _on_mouse_exited():
$HighlightSprite.visible = false
Input.set_default_cursor_shape(Input.CURSOR_ARROW) # 设置鼠标指针形状为箭头


func _on_tree_exiting():
Expand Down
7 changes: 5 additions & 2 deletions objects/card_base/card_base.gd
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,11 @@ func get_word() -> String:
return $Word.get_word()


## 获取剩余卡牌数量。
func get_card_count() -> int:
return self.card_count


## 进行通关后的处理。
func set_victory() -> void:
# 开始淡出
Expand Down Expand Up @@ -116,14 +121,12 @@ func _on_mouse_entered():
is_mouse_on = true
if available_stat == DEFAULT:
available_stat = HIGHLIGHT
Input.set_default_cursor_shape(Input.CURSOR_POINTING_HAND)


func _on_mouse_exited():
is_mouse_on = false
if available_stat == HIGHLIGHT:
available_stat = DEFAULT
Input.set_default_cursor_shape(Input.CURSOR_ARROW)


func _on_cards_child_exiting_tree(node: Node) -> void:
Expand Down
38 changes: 38 additions & 0 deletions scripts/cursor_manager/cursor_manager.gd
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
## 用于检测与鼠标重合的、[member Area2D.z_index] 最高的 [Area2D],并根据其状态设置鼠标光标形状。
class_name CursorManager extends Area2D


const CURSOR_ARROW := preload("res://bg/cursor/cursor_arrow.png") ## 鼠标光标为指针时的素材。
const CURSOR_POINTING_HAND := preload("res://bg/cursor/cursor_pointing_hand.png") ## 鼠标光标为指向时的素材。
const CURSOR_DRAG := preload("res://bg/cursor/cursor_drag.png") ## 鼠标光标为拖拽时的素材。


func _ready():
Input.set_custom_mouse_cursor(CURSOR_ARROW)
Input.set_custom_mouse_cursor(CURSOR_POINTING_HAND, Input.CURSOR_POINTING_HAND)
Input.set_custom_mouse_cursor(CURSOR_DRAG, Input.CURSOR_DRAG)


func _process(_delta):
self.global_position = get_global_mouse_position()

# 获取 z_index 最高的 Area2D
var top_area: Area2D = null
for area in get_overlapping_areas():
if top_area == null or area.z_index > top_area.z_index:
top_area = area

if top_area == null:
Input.set_custom_mouse_cursor(CURSOR_ARROW)
elif top_area is Card:
if top_area.is_dragging:
Input.set_custom_mouse_cursor(CURSOR_DRAG)
else:
Input.set_custom_mouse_cursor(CURSOR_POINTING_HAND)
elif top_area is CardBase:
if top_area.get_card_count() > 0:
Input.set_custom_mouse_cursor(CURSOR_POINTING_HAND)
else:
Input.set_custom_mouse_cursor(CURSOR_ARROW)
else:
Input.set_custom_mouse_cursor(CURSOR_ARROW)
12 changes: 12 additions & 0 deletions scripts/cursor_manager/cursor_manager.tscn
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
[gd_scene load_steps=3 format=3 uid="uid://prht3u5pnjls"]

[ext_resource type="Script" path="res://scripts/cursor_manager/cursor_manager.gd" id="1_v3lev"]

[sub_resource type="RectangleShape2D" id="RectangleShape2D_stfy2"]
size = Vector2(0, 0)

[node name="CursorManager" type="Area2D"]
script = ExtResource("1_v3lev")

[node name="CollisionShape2D" type="CollisionShape2D" parent="."]
shape = SubResource("RectangleShape2D_stfy2")

0 comments on commit 3cd0a9c

Please sign in to comment.