-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCardView.cpp
More file actions
195 lines (173 loc) · 6.2 KB
/
Copy pathCardView.cpp
File metadata and controls
195 lines (173 loc) · 6.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
#include "CardView.h"
USING_NS_CC;
const std::string CardView::SUIT_IMAGE_PATH = "images/";
bool CardView::s_useImageSuit = true;
CardView::CardView()
: _card(Suit::SPADES, 1), _cardWidth(100), _cardHeight(140), _index(-1)
, _highlighted(false), _disabled(false), _background(nullptr)
, _cardFrame(nullptr), _rankLabel(nullptr), _suitLabel(nullptr), _suitSprite(nullptr) {}
CardView::~CardView() {}
CardView* CardView::create(const Card& card, float width, float height)
{
CardView* view = new CardView();
if (view && view->init()) {
view->setCard(card);
view->setCardSize(width, height);
view->updateDisplay();
view->autorelease();
return view;
}
CC_SAFE_DELETE(view);
return nullptr;
}
CardView* CardView::create(float width, float height)
{
CardView* view = new CardView();
if (view && view->init()) {
view->setCardSize(width, height);
view->autorelease();
return view;
}
CC_SAFE_DELETE(view);
return nullptr;
}
bool CardView::init()
{
if (!Node::init()) return false;
setupUI();
setupTouchListener();
return true;
}
void CardView::setupUI()
{
_background = Sprite::create();
_background->setTextureRect(Rect(0, 0, _cardWidth, _cardHeight));
_background->setColor(Color3B(255, 255, 255));
addChild(_background, 0);
_cardFrame = Sprite::create("images/card_frame.png");
if (_cardFrame) {
_cardFrame->setPosition(Vec2::ZERO);
Size s = _cardFrame->getContentSize();
_cardFrame->setScale(_cardWidth / s.width, _cardHeight / s.height);
addChild(_cardFrame, 1);
}
_rankLabel = Label::createWithSystemFont("", "Arial", 28);
if (_rankLabel) {
_rankLabel->setPosition(Vec2(-_cardWidth * 0.32f, _cardHeight * 0.36f));
addChild(_rankLabel, 2);
}
_suitSprite = Sprite::create();
_suitSprite->setPosition(Vec2(0, -_cardHeight * 0.1f));
addChild(_suitSprite, 2);
_suitLabel = Label::createWithSystemFont("", "Arial", 24);
if (_suitLabel) {
_suitLabel->setPosition(Vec2(-_cardWidth * 0.32f, _cardHeight * 0.2f));
addChild(_suitLabel, 2);
}
setContentSize(Size(_cardWidth, _cardHeight));
}
void CardView::setupTouchListener()
{
auto listener = EventListenerTouchOneByOne::create();
listener->setSwallowTouches(true);
listener->onTouchBegan = [this](Touch* touch, Event*) {
Vec2 pos = convertToNodeSpace(touch->getLocation());
Size s = getContentSize();
Rect rect(-s.width/2, -s.height/2, s.width, s.height);
return rect.containsPoint(pos);
};
listener->onTouchEnded = [this](Touch*, Event*) {
if (_clickCallback && !_disabled) _clickCallback(this);
};
_eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this);
}
void CardView::setCard(const Card& card) { _card = card; }
void CardView::setCardSize(float w, float h)
{
_cardWidth = w; _cardHeight = h;
if (_background) _background->setTextureRect(Rect(0, 0, w, h));
if (_rankLabel) _rankLabel->setPosition(Vec2(-w * 0.32f, h * 0.36f));
if (_suitLabel) _suitLabel->setPosition(Vec2(-w * 0.32f, h * 0.2f));
if (_suitSprite) _suitSprite->setPosition(Vec2(0, -h * 0.1f));
setContentSize(Size(w, h));
}
void CardView::updateDisplay()
{
if (_card.isJoker())
{
if (_background) {
if (_card.isRedJoker()) {
_background->setColor(Color3B(255, 200, 200));
} else {
_background->setColor(Color3B(200, 200, 255));
}
}
if (_rankLabel) {
_rankLabel->setString(_card.getDisplayText());
_rankLabel->setPosition(Vec2(0, 0));
_rankLabel->setSystemFontSize(_cardWidth * 0.35f);
_rankLabel->setColor(_card.getColor());
}
if (_suitSprite) _suitSprite->setVisible(false);
if (_suitLabel) _suitLabel->setVisible(false);
}
else
{
if (_background) {
Color3B c = _highlighted ? Color3B(200, 255, 200) : Color3B(255, 255, 255);
if (_disabled) c = Color3B(180, 180, 180);
_background->setColor(c);
}
if (_rankLabel) {
_rankLabel->setString(Card::getRankDisplayText(_card.getRank()));
_rankLabel->setPosition(Vec2(-_cardWidth * 0.32f, _cardHeight * 0.36f));
_rankLabel->setSystemFontSize(28);
_rankLabel->setColor(_disabled ? Color3B(128,128,128) : _card.getColor());
}
updateSuitDisplay();
}
}
void CardView::updateSuitDisplay()
{
if (_card.isJoker()) {
if (_suitSprite) _suitSprite->setVisible(false);
if (_suitLabel) _suitLabel->setVisible(false);
return;
}
Color3B c = _disabled ? Color3B(128,128,128) : _card.getColor();
if (s_useImageSuit) {
std::string path = getSuitImagePath(_card.getSuit());
if (!path.empty()) {
Texture2D* tex = Director::getInstance()->getTextureCache()->addImage(path);
if (tex) {
_suitSprite->setTexture(tex);
float sz = _cardWidth * 0.5f;
Size ts = tex->getContentSize();
_suitSprite->setScale(sz / std::max(ts.width, ts.height));
_suitSprite->setVisible(true);
}
}
}
if (_suitLabel) {
_suitLabel->setString(_card.getSuitSymbol());
_suitLabel->setColor(c);
_suitLabel->setVisible(true);
}
}
std::string CardView::getSuitImagePath(Suit suit) const
{
static const char* names[] = {"heart.png", "diamond.png", "club.png", "spade.png"};
int idx = static_cast<int>(suit);
std::string path = SUIT_IMAGE_PATH + names[idx];
if (FileUtils::getInstance()->isFileExist(path)) return path;
return "";
}
void CardView::setHighlighted(bool h) { if (_highlighted != h) { _highlighted = h; updateDisplay(); } }
void CardView::setDisabled(bool d) { if (_disabled != d) { _disabled = d; updateDisplay(); } }
void CardView::moveTo(const Vec2& pos, float dur, std::function<void()> cb)
{
_targetPosition = pos;
auto move = MoveTo::create(dur, pos);
if (cb) runAction(Sequence::create(move, CallFunc::create(cb), nullptr));
else runAction(move);
}