Repository navigation
Expand file tree
/
Copy pathHand.cpp
More file actions
129 lines (104 loc) · 2.86 KB
/
Copy pathHand.cpp
File metadata and controls
129 lines (104 loc) · 2.86 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
//
// Created by Aram Paparian on 5/18/26.
//
#include "Hand.h"
Hand::Hand() : Hand(true)
{
}
Hand::Hand(bool player, Seat seat) : _player(player), _seat(seat)
{
onUpdate([this]()
{
update();
});
}
void Hand::update()
{
if (!_player || _hand.empty()) return;
sf::Vector2f mousePos = sf::Vector2f(sf::Mouse::getPosition(ml::WindowManager::getWindow()));
bool clicked = sf::Mouse::isButtonPressed(sf::Mouse::Button::Left);
int hoveredIndex = -1;
for (int i = _hand.size() - 1; i >= 0; i--)
{
if (_hand[i]->getGlobalBounds().contains(mousePos))
{
hoveredIndex = i;
break;
}
}
for (int i = 0; i < (int)_hand.size(); i++)
_hand[i]->setHovered(i == hoveredIndex);
if (clicked && !_wasClicked && hoveredIndex != -1 && _onCardClicked)
_onCardClicked(*_hand[hoveredIndex]);
_wasClicked = clicked;
}
void Hand::addCard(Card& card)
{
if (!_player)
card.setFaceDown(true);
_hand.emplace_back(&card);
updateLayout();
}
void Hand::removeCard(Card& card)
{
for (int i = 0; i < static_cast<int>(_hand.size()); i++)
{
if (_hand[i]->getRank() == card.getRank() && _hand[i]->getSuit() == card.getSuit())
{
_hand.erase(_hand.begin() + i);
break;
}
}
updateLayout();
}
void Hand::removeCardAt(int i)
{
if (i >= 0 && i < static_cast<int>(_hand.size()))
{
_hand.erase(_hand.begin() + i);
updateLayout();
}
}
void Hand::clear()
{
_hand.clear();
_wasClicked = false;
}
void Hand::updateLayout()
{
if (_hand.empty()) return;
sf::Vector2u winSize = ml::WindowManager::getWindow().getSize();
sf::Vector2f cardSize = _hand[0]->getSize();
float spacing = 20.f;
if (_seat == Seat::WEST || _seat == Seat::EAST)
{
float totalHeight = (_hand.size() - 1) * (spacing + cardSize.y) + cardSize.y;
float startY = (winSize.y / 2.f) - (totalHeight / 2.f);
float x = (_seat == Seat::WEST)
? spacing
: winSize.x - cardSize.x - spacing;
for (int i = 0; i < static_cast<int>(_hand.size()); i++)
_hand[i]->setPosition({x, startY + i * (cardSize.y + spacing)});
return;
}
float totalWidth = (_hand.size() - 1) * (spacing + cardSize.x) + cardSize.x;
float startX = (winSize.x / 2.f) - (totalWidth / 2.f);
float y = spacing;
if (_seat == Seat::SOUTH)
y = winSize.y - cardSize.y - spacing;
for (int i = 0; i < static_cast<int>(_hand.size()); i++)
_hand[i]->setPosition({startX + i * (cardSize.x + spacing), y});
}
const std::vector<Card*>& Hand::cards() const
{
return _hand;
}
int Hand::size() const
{
return _hand.size();
}
void Hand::draw(sf::RenderTarget& target, sf::RenderStates states) const
{
for (auto* card : _hand)
target.draw(*card);
}