-
Notifications
You must be signed in to change notification settings - Fork 29
/
Copy pathGuiItem.cpp
306 lines (254 loc) · 8.55 KB
/
GuiItem.cpp
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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
////////////////////////////////////////////////////////////////////////////////////////////////////
// This file is part of CosmoScout VR //
////////////////////////////////////////////////////////////////////////////////////////////////////
// SPDX-FileCopyrightText: German Aerospace Center (DLR) <[email protected]>
// SPDX-License-Identifier: MIT
#include "GuiItem.hpp"
#include "GuiArea.hpp"
#include <VistaOGLExt/VistaTexture.h>
namespace cs::gui {
////////////////////////////////////////////////////////////////////////////////////////////////////
GuiItem::GuiItem(std::string const& url, bool allowLocalFileAccess)
: WebView(url, 100, 100, allowLocalFileAccess)
, mAreaWidth(1)
, mAreaHeight(1)
, mSizeX(0)
, mSizeY(0)
, mPositionX(0)
, mPositionY(0)
, mOffsetX(0)
, mOffsetY(0)
, mRelSizeX(1.F)
, mRelSizeY(1.F)
, mRelPositionX(0.5F)
, mRelPositionY(0.5F)
, mRelOffsetX(0.F)
, mRelOffsetY(0.F)
, mIsRelSizeX(true)
, mIsRelSizeY(true)
, mIsRelPositionX(true)
, mIsRelPositionY(true)
, mIsRelOffsetX(true)
, mIsRelOffsetY(true) {
setDrawCallback([this](DrawEvent const& event) { return updateTexture(event); });
setRequestKeyboardFocusCallback(
[this](bool requested) { mIsKeyboardInputElementFocused = requested; });
glGenBuffers(1, &mTextureBuffer);
glBindBuffer(GL_TEXTURE_BUFFER, mTextureBuffer);
size_t bufferSize{4 * sizeof(uint8_t) * getWidth() * getHeight()};
GLbitfield flags = GL_MAP_WRITE_BIT | GL_MAP_PERSISTENT_BIT | GL_MAP_COHERENT_BIT;
glBufferStorage(GL_TEXTURE_BUFFER, bufferSize, nullptr, flags);
mBufferData = static_cast<uint8_t*>(glMapBufferRange(GL_TEXTURE_BUFFER, 0, bufferSize, flags));
glGenTextures(1, &mTexture);
glBindTexture(GL_TEXTURE_BUFFER, mTexture);
glTexBuffer(GL_TEXTURE_BUFFER, GL_RGBA8, mTextureBuffer);
glBindBuffer(GL_TEXTURE_BUFFER, 0);
glBindTexture(GL_TEXTURE_BUFFER, 0);
}
////////////////////////////////////////////////////////////////////////////////////////////////////
GuiItem::~GuiItem() {
glBindBuffer(GL_TEXTURE_BUFFER, mTextureBuffer);
glUnmapBuffer(GL_TEXTURE_BUFFER);
glDeleteBuffers(1, &mTextureBuffer);
glDeleteTextures(1, &mTexture);
// seems to be necessary as OnPaint can be called by some other thread even
// if this object is already deleted
setDrawCallback([](DrawEvent const& /*unused*/) { return nullptr; });
}
////////////////////////////////////////////////////////////////////////////////////////////////////
void GuiItem::setSizeX(unsigned int value) {
mSizeX = value;
mIsRelSizeX = false;
updateSizes();
}
void GuiItem::setSizeY(unsigned int value) {
mSizeY = value;
mIsRelSizeY = false;
updateSizes();
}
void GuiItem::setPositionX(int value) {
mPositionX = value;
mIsRelPositionX = false;
updateSizes();
}
void GuiItem::setPositionY(int value) {
mPositionY = value;
mIsRelPositionY = false;
updateSizes();
}
void GuiItem::setOffsetX(int value) {
mOffsetX = value;
mIsRelOffsetX = false;
updateSizes();
}
void GuiItem::setOffsetY(int value) {
mOffsetY = value;
mIsRelOffsetY = false;
updateSizes();
}
void GuiItem::setRelSizeX(float value) {
mRelSizeX = value;
mIsRelSizeX = true;
updateSizes();
}
void GuiItem::setRelSizeY(float value) {
mRelSizeY = value;
mIsRelSizeY = true;
updateSizes();
}
void GuiItem::setRelPositionX(float value) {
mRelPositionX = value;
mIsRelPositionX = true;
updateSizes();
}
void GuiItem::setRelPositionY(float value) {
mRelPositionY = value;
mIsRelPositionY = true;
updateSizes();
}
void GuiItem::setRelOffsetX(float value) {
mRelOffsetX = value;
mIsRelOffsetX = true;
updateSizes();
}
void GuiItem::setRelOffsetY(float value) {
mRelOffsetY = value;
mIsRelOffsetY = true;
updateSizes();
}
////////////////////////////////////////////////////////////////////////////////////////////////////
unsigned int GuiItem::getSizeX() const {
return mSizeX;
}
unsigned int GuiItem::getSizeY() const {
return mSizeY;
}
int GuiItem::getPositionX() const {
return mPositionX;
}
int GuiItem::getPositionY() const {
return mPositionY;
}
int GuiItem::getOffsetX() const {
return mOffsetX;
}
int GuiItem::getOffsetY() const {
return mOffsetY;
}
float GuiItem::getRelSizeX() const {
return mRelSizeX;
}
float GuiItem::getRelSizeY() const {
return mRelSizeY;
}
float GuiItem::getRelPositionX() const {
return mRelPositionX;
}
float GuiItem::getRelPositionY() const {
return mRelPositionY;
}
float GuiItem::getRelOffsetX() const {
return mRelOffsetX;
}
float GuiItem::getRelOffsetY() const {
return mRelOffsetY;
}
int GuiItem::getTextureSizeX() const {
return mTextureSizeX;
}
int GuiItem::getTextureSizeY() const {
return mTextureSizeY;
}
////////////////////////////////////////////////////////////////////////////////////////////////////
void GuiItem::setIsEnabled(bool bEnabled) {
mIsEnabled = bEnabled;
}
bool GuiItem::getIsEnabled() const {
return mIsEnabled;
}
////////////////////////////////////////////////////////////////////////////////////////////////////
bool GuiItem::getIsKeyboardInputElementFocused() const {
return mIsKeyboardInputElementFocused;
}
////////////////////////////////////////////////////////////////////////////////////////////////////
bool GuiItem::calculateMousePosition(int areaX, int areaY, int& x, int& y) {
int tmpX = areaX - mOffsetX - mPositionX + getWidth() / 2;
int tmpY = areaY - mOffsetY - mPositionY + getHeight() / 2;
x = tmpX;
y = tmpY;
if (tmpX > static_cast<int>(mSizeX) - 1 || tmpX < 0) {
return false;
}
if (tmpY > static_cast<int>(mSizeY) - 1 || tmpY < 0) {
return false;
}
return true;
}
////////////////////////////////////////////////////////////////////////////////////////////////////
uint8_t* GuiItem::updateTexture(DrawEvent const& event) {
if (event.mResized) {
glBindBuffer(GL_TEXTURE_BUFFER, mTextureBuffer);
glUnmapBuffer(GL_TEXTURE_BUFFER);
glBindBuffer(GL_TEXTURE_BUFFER, 0);
glDeleteBuffers(1, &mTextureBuffer);
glGenBuffers(1, &mTextureBuffer);
glBindBuffer(GL_TEXTURE_BUFFER, mTextureBuffer);
size_t bufferSize{4 * sizeof(uint8_t) * event.mWidth * event.mHeight};
GLbitfield flags = GL_MAP_WRITE_BIT | GL_MAP_PERSISTENT_BIT | GL_MAP_COHERENT_BIT;
glBufferStorage(GL_TEXTURE_BUFFER, bufferSize, nullptr, flags);
mBufferData = static_cast<uint8_t*>(glMapBufferRange(GL_TEXTURE_BUFFER, 0, bufferSize, flags));
glBindTexture(GL_TEXTURE_BUFFER, mTexture);
glTexBuffer(GL_TEXTURE_BUFFER, GL_RGBA8, mTextureBuffer);
glBindBuffer(GL_TEXTURE_BUFFER, 0);
glBindTexture(GL_TEXTURE_BUFFER, 0);
mTextureSizeX = event.mWidth;
mTextureSizeY = event.mHeight;
}
return mBufferData;
}
////////////////////////////////////////////////////////////////////////////////////////////////////
void GuiItem::onAreaResize(int width, int height) {
mAreaWidth = width;
mAreaHeight = height;
updateSizes();
}
////////////////////////////////////////////////////////////////////////////////////////////////////
void GuiItem::updateSizes() {
if (mIsRelSizeX) {
mSizeX = static_cast<uint32_t>(mRelSizeX * static_cast<float>(mAreaWidth));
} else {
mRelSizeX = static_cast<float>(1.0 * mSizeX / mAreaWidth);
}
if (mIsRelSizeY) {
mSizeY = static_cast<uint32_t>(mRelSizeY * static_cast<float>(mAreaHeight));
} else {
mRelSizeY = static_cast<float>(1.0 * mSizeY / mAreaHeight);
}
if (mIsRelPositionX) {
mPositionX = static_cast<uint32_t>(mRelPositionX * static_cast<float>(mAreaWidth));
} else {
mRelPositionX = static_cast<float>(1.0 * mPositionX / mAreaWidth);
}
if (mIsRelPositionY) {
mPositionY = static_cast<uint32_t>(mRelPositionY * static_cast<float>(mAreaHeight));
} else {
mRelPositionY = static_cast<float>(1.0 * mPositionY / mAreaHeight);
}
if (mIsRelOffsetX) {
mOffsetX = static_cast<uint32_t>(mRelOffsetX * static_cast<float>(mAreaWidth));
} else {
mRelOffsetX = static_cast<float>(1.0 * mOffsetX / mAreaWidth);
}
if (mIsRelOffsetY) {
mOffsetY = static_cast<uint32_t>(mRelOffsetY * static_cast<float>(mAreaHeight));
} else {
mRelOffsetY = static_cast<float>(1.0 * mOffsetY / mAreaHeight);
}
resize(static_cast<int>(mSizeX), static_cast<int>(mSizeY));
}
////////////////////////////////////////////////////////////////////////////////////////////////////
uint32_t GuiItem::getTexture() const {
return mTexture;
}
////////////////////////////////////////////////////////////////////////////////////////////////////
} // namespace cs::gui