fix: 使用 assets/cards/ 图片显示卡牌
- 移除文字显示方式(RankLabel/SuitLabel) - 添加 TextureRect 节点显示卡牌图片 - 根据卡牌 suit 和 rank 动态加载对应图片 - 图片命名映射:S=黑桃, H=红桃, C=梅花, D=方块, SJ=小王, BJ=大王 🤖 Generated with [Qoder][https://qoder.com]
This commit is contained in:
@@ -8,11 +8,11 @@ var card_data: Card = null
|
||||
var is_selected: bool = false
|
||||
|
||||
@onready var panel: Panel = $Panel
|
||||
@onready var rank_label: Label = $Panel/RankLabel
|
||||
@onready var suit_label: Label = $Panel/SuitLabel
|
||||
@onready var card_texture: TextureRect = $Panel/CardTexture
|
||||
|
||||
const SUIT_SYMBOLS := ["\u2660", "\u2665", "\u2663", "\u2666", "SJ", "BJ"]
|
||||
const RANK_NAMES := ["", "", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A", "SJ", "BJ"]
|
||||
const RANK_PREFIX := ["", "", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A", "SJ", "BJ"]
|
||||
const SUIT_LETTER := ["S", "H", "C", "D", "SJ", "BJ"]
|
||||
const CARD_TEXTURE_PATH := "res://assets/cards/"
|
||||
|
||||
func setup(card: Card) -> void:
|
||||
card_data = card
|
||||
@@ -21,20 +21,29 @@ func setup(card: Card) -> void:
|
||||
func update_display() -> void:
|
||||
if card_data == null:
|
||||
return
|
||||
var suit := card_data.suit()
|
||||
var rank := card_data.rank()
|
||||
var is_red := suit == 1 or suit == 3
|
||||
var color := Color.RED if is_red else Color.BLACK
|
||||
var suit_idx := card_data.suit()
|
||||
var rank_idx := card_data.rank()
|
||||
|
||||
if rank_label:
|
||||
rank_label.text = RANK_NAMES[rank]
|
||||
rank_label.add_theme_color_override("font_color", color)
|
||||
rank_label.add_theme_font_size_override("font_size", 14)
|
||||
# 构建图片文件名:如 "2H.png", "AS.png", "SJ.png", "BJ.png"
|
||||
var rank_str := RANK_PREFIX[rank_idx]
|
||||
var file_name: String
|
||||
if suit_idx == 4:
|
||||
# 小王
|
||||
file_name = "SJ.png"
|
||||
elif suit_idx == 5:
|
||||
# 大王
|
||||
file_name = "BJ.png"
|
||||
else:
|
||||
var suit_str := SUIT_LETTER[suit_idx]
|
||||
file_name = "%s%s.png" % [rank_str, suit_str]
|
||||
|
||||
if suit_label:
|
||||
suit_label.text = SUIT_SYMBOLS[suit]
|
||||
suit_label.add_theme_color_override("font_color", color)
|
||||
suit_label.add_theme_font_size_override("font_size", 24)
|
||||
var full_path := CARD_TEXTURE_PATH + file_name
|
||||
var texture := load(full_path) as Texture2D
|
||||
|
||||
if card_texture and texture:
|
||||
card_texture.texture = texture
|
||||
else:
|
||||
print("[CardNode] 无法加载卡牌图片: ", full_path)
|
||||
|
||||
_update_panel()
|
||||
|
||||
@@ -48,7 +57,7 @@ func _update_panel() -> void:
|
||||
sbox.border_width_right = 2
|
||||
sbox.border_width_top = 2
|
||||
sbox.border_width_bottom = 2
|
||||
sbox.border_color = Color(0.3, 0.3, 0.3)
|
||||
sbox.border_color = Color(0.3, 0.3, 0.3) if not is_selected else Color(1.0, 0.8, 0.0)
|
||||
sbox.content_margin_left = 2
|
||||
sbox.content_margin_right = 2
|
||||
sbox.content_margin_top = 2
|
||||
|
||||
@@ -1,10 +1,11 @@
|
||||
[gd_scene load_steps=2 format=3 uid="uid://card_node"]
|
||||
|
||||
[ext_resource type="Script" path="res://src/ui/components/card_node.gd" id="1_script"]
|
||||
|
||||
[node name="CardNode" type="Control"]
|
||||
custom_minimum_size = Vector2(70, 100)
|
||||
custom_minimum_size = Vector2(80, 110)
|
||||
mouse_filter = 1
|
||||
size = Vector2(70, 100)
|
||||
size = Vector2(80, 110)
|
||||
script = ExtResource("1_script")
|
||||
|
||||
[node name="Panel" type="Panel" parent="."]
|
||||
@@ -14,27 +15,10 @@ anchors_preset = 15
|
||||
anchor_right = 1.0
|
||||
anchor_bottom = 1.0
|
||||
|
||||
[node name="RankLabel" type="Label" parent="Panel"]
|
||||
[node name="CardTexture" type="TextureRect" parent="Panel"]
|
||||
layout_mode = 1
|
||||
anchors_preset = 2
|
||||
anchor_top = 1.0
|
||||
anchors_preset = 15
|
||||
anchor_right = 1.0
|
||||
anchor_bottom = 1.0
|
||||
offset_top = -18.0
|
||||
offset_right = 12.0
|
||||
offset_bottom = -2.0
|
||||
text = "A"
|
||||
horizontal_alignment = 1
|
||||
|
||||
[node name="SuitLabel" type="Label" parent="Panel"]
|
||||
layout_mode = 1
|
||||
anchors_preset = 8
|
||||
anchor_left = 0.5
|
||||
anchor_right = 0.5
|
||||
anchor_top = 0.5
|
||||
anchor_bottom = 0.5
|
||||
offset_left = -20.0
|
||||
offset_top = -20.0
|
||||
offset_right = 20.0
|
||||
offset_bottom = 20.0
|
||||
horizontal_alignment = 1
|
||||
vertical_alignment = 1
|
||||
expand_mode = 1
|
||||
stretch_mode = 5
|
||||
|
||||
Reference in New Issue
Block a user