forked from mavlink/qgroundcontrol
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathCheckerboardTextureData.cc
More file actions
78 lines (68 loc) · 1.95 KB
/
Copy pathCheckerboardTextureData.cc
File metadata and controls
78 lines (68 loc) · 1.95 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
#include "CheckerboardTextureData.h"
#include <QtCore/QSize>
#include <algorithm>
CheckerboardTextureData::CheckerboardTextureData(QQuick3DObject* parent) : QQuick3DTextureData(parent)
{
_rebuild();
}
void CheckerboardTextureData::setSizePixels(int sizePixels)
{
const int clamped = std::clamp(sizePixels, 2, 2048);
if (clamped == _sizePixels) {
return;
}
_sizePixels = clamped;
emit sizePixelsChanged();
_rebuild();
}
void CheckerboardTextureData::setCells(int cells)
{
// Fixed range (no cross-property dependence: QML property init order is unspecified);
// _rebuild() degrades cells > sizePixels to 1-pixel cells
const int clamped = std::clamp(cells, 1, 2048);
if (clamped == _cells) {
return;
}
_cells = clamped;
emit cellsChanged();
_rebuild();
}
void CheckerboardTextureData::setColor1(const QColor& color)
{
if (color == _color1) {
return;
}
_color1 = color;
emit color1Changed();
_rebuild();
}
void CheckerboardTextureData::setColor2(const QColor& color)
{
if (color == _color2) {
return;
}
_color2 = color;
emit color2Changed();
_rebuild();
}
void CheckerboardTextureData::_rebuild()
{
const int size = _sizePixels;
const int cellPixels = std::max(1, size / _cells);
QByteArray data(qsizetype(size) * size * 4, Qt::Uninitialized);
uchar* p = reinterpret_cast<uchar*>(data.data());
for (int y = 0; y < size; y++) {
for (int x = 0; x < size; x++) {
const bool odd = (((x / cellPixels) + (y / cellPixels)) % 2) != 0;
const QColor& color = odd ? _color2 : _color1;
*p++ = static_cast<uchar>(color.red());
*p++ = static_cast<uchar>(color.green());
*p++ = static_cast<uchar>(color.blue());
*p++ = 255;
}
}
setSize(QSize(size, size));
setFormat(QQuick3DTextureData::RGBA8);
setHasTransparency(false);
setTextureData(data);
}