-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathboardwidget.cpp
More file actions
198 lines (174 loc) · 5.99 KB
/
Copy pathboardwidget.cpp
File metadata and controls
198 lines (174 loc) · 5.99 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
196
197
198
#include "boardwidget.h"
#include <QMouseEvent>
#include <QPainter>
#include <QPolygonF>
BoardWidget::BoardWidget(QWidget* parent)
: QWidget(parent)
{
setMinimumSize(m_margin * 2 + 4 * m_cellSize, m_margin * 2 + 4 * m_cellSize);
}
void BoardWidget::setSnapshot(const BoardSnapshot& s)
{
m_snap = s;
updateGeometry();
update();
}
void BoardWidget::setSelectedShip(int shipIdx)
{
m_selectedShip = shipIdx;
update();
}
void BoardWidget::setHighlightCells(const QVector<QPoint>& cells)
{
m_highlights = cells;
update();
}
void BoardWidget::setRangeCells(const QVector<QPoint>& cells)
{
m_rangeCells = cells;
update();
}
QSize BoardWidget::sizeHint() const
{
if (m_snap.rows <= 0 || m_snap.cols <= 0)
return QSize(480, 480);
return QSize(m_margin * 2 + m_snap.cols * m_cellSize,
m_margin * 2 + m_snap.rows * m_cellSize);
}
QRect BoardWidget::cellRect(int r, int c) const
{
return QRect(m_margin + c * m_cellSize,
m_margin + r * m_cellSize,
m_cellSize,
m_cellSize);
}
void BoardWidget::paintEvent(QPaintEvent*)
{
QPainter p(this);
p.setRenderHint(QPainter::Antialiasing, true);
p.fillRect(rect(), QColor(14, 20, 30));
if (m_snap.rows <= 0 || m_snap.cols <= 0)
return;
const int rows = m_snap.rows;
const int cols = m_snap.cols;
QFont hintFont = font();
hintFont.setPointSize(10);
hintFont.setBold(true);
p.setFont(hintFont);
for (int c = 0; c < cols; c++) {
QRect r(m_margin + c * m_cellSize, 0, m_cellSize, m_margin);
p.setPen(QColor(205, 218, 230));
p.drawText(r, Qt::AlignCenter, QString::number(c < m_snap.colHints.size() ? m_snap.colHints[c] : 0));
}
for (int r = 0; r < rows; r++) {
QRect rc(0, m_margin + r * m_cellSize, m_margin, m_cellSize);
p.setPen(QColor(205, 218, 230));
p.drawText(rc, Qt::AlignCenter, QString::number(r < m_snap.rowHints.size() ? m_snap.rowHints[r] : 0));
}
// 每格底色:己方舰 / 敌舰 / 水 / 迷雾
for (int r = 0; r < rows; r++) {
for (int c = 0; c < cols; c++) {
QRect cr = cellRect(r, c).adjusted(1, 1, -1, -1);
const int v = m_snap.view[r][c];
if (v == 2)
p.fillRect(cr, QColor(58, 84, 120)); // 己方
else if (v == 3)
p.fillRect(cr, QColor(112, 54, 56)); // 敌方
else if (v == 1)
p.fillRect(cr, QColor(40, 96, 142)); // 水
else
p.fillRect(cr, QColor(78, 88, 104)); // 迷雾
}
}
// 攻击范围淡色高亮
if (!m_rangeCells.isEmpty()) {
const QColor rangeColor(255, 235, 120, 42);
for (const QPoint& c : m_rangeCells)
p.fillRect(cellRect(c.x(), c.y()).adjusted(3, 3, -3, -3), rangeColor);
}
// 绘制舰船
for (const ShipView& s : m_snap.ships)
drawShip(p, s, s.mine && s.index == m_selectedShip);
// 攻击目标高亮
if (!m_highlights.isEmpty()) {
for (const QPoint& c : m_highlights) {
p.setBrush(Qt::NoBrush);
p.setPen(QPen(QColor(110, 230, 130), 2));
p.drawRect(cellRect(c.x(), c.y()).adjusted(2, 2, -2, -2));
}
}
// 网格线
p.setPen(QColor(8, 12, 18));
for (int c = 0; c <= cols; c++) {
int x = m_margin + c * m_cellSize;
p.drawLine(x, m_margin, x, m_margin + rows * m_cellSize);
}
for (int r = 0; r <= rows; r++) {
int y = m_margin + r * m_cellSize;
p.drawLine(m_margin, y, m_margin + cols * m_cellSize, y);
}
}
void BoardWidget::drawShip(QPainter& p, const ShipView& s, bool selected) const
{
QRect bbox;
for (const QPoint& c : s.cells) {
QRect cr = cellRect(c.x(), c.y());
bbox = bbox.isNull() ? cr : bbox.united(cr);
}
QRect bar = bbox.adjusted(4, 4, -4, -4);
QColor color = s.mine ? QColor(72, 140, 220) : QColor(224, 92, 72);
p.setBrush(color);
p.setPen(QPen(QColor(18, 24, 32), 2));
p.drawRoundedRect(bar, 6, 6);
// 船头方向箭头
const QPoint bow = s.cells.first();
const QPointF center = cellRect(bow.x(), bow.y()).center();
double dx = 0, dy = 0;
switch (s.dir) {
case GameModel::North: dy = -1; break;
case GameModel::East: dx = 1; break;
case GameModel::South: dy = 1; break;
case GameModel::West: dx = -1; break;
}
const double len = m_cellSize * 0.38;
QPolygonF tri;
tri << (center + QPointF(dx * len, dy * len))
<< (center + QPointF(-dy * len * 0.5, dx * len * 0.5))
<< (center + QPointF(dy * len * 0.5, -dx * len * 0.5));
p.setPen(Qt::NoPen);
p.setBrush(QColor(255, 232, 150));
p.drawPolygon(tri);
// 血量格点:沿舰身中线均匀分布 maxHp 个圆,前 hp 个实心,其余空心
const bool horiz = (s.dir == GameModel::East || s.dir == GameModel::West);
const double pipR = m_cellSize * 0.13;
for (int i = 0; i < s.maxHp; i++) {
const qreal t = (s.maxHp == 1) ? 0.5 : (i + 0.5) / s.maxHp;
const QPointF cc = horiz
? QPointF(bar.left() + t * bar.width(), bar.center().y())
: QPointF(bar.center().x(), bar.top() + t * bar.height());
if (i < s.hp) {
p.setBrush(Qt::white);
p.setPen(Qt::NoPen);
} else {
p.setBrush(Qt::NoBrush);
p.setPen(QPen(QColor(255, 255, 255, 150), 1));
}
p.drawEllipse(cc, pipR, pipR);
}
// 选中高亮
if (selected) {
p.setBrush(Qt::NoBrush);
p.setPen(QPen(QColor(255, 228, 90), 3));
p.drawRoundedRect(bbox.adjusted(1, 1, -1, -1), 7, 7);
}
}
void BoardWidget::mousePressEvent(QMouseEvent* event)
{
if (m_snap.rows <= 0 || m_snap.cols <= 0)
return;
const int c = (event->position().x() - m_margin) / m_cellSize;
const int r = (event->position().y() - m_margin) / m_cellSize;
if (r < 0 || r >= m_snap.rows || c < 0 || c >= m_snap.cols)
return;
emit cellClicked(r, c);
}