MyGUI 3.5.3
MyGUI_Canvas.cpp
Go to the documentation of this file.
1/*
2 * This source file is part of MyGUI. For the latest info, see http://mygui.info/
3 * Distributed under the MIT License
4 * (See accompanying file COPYING.MIT or copy at http://opensource.org/licenses/MIT)
5 */
6
7#include "MyGUI_Precompiled.h"
8#include "MyGUI_Canvas.h"
10#include "MyGUI_Gui.h"
11#include "MyGUI_RenderManager.h"
12#include "MyGUI_Bitwise.h"
13
14namespace MyGUI
15{
16
17 Canvas::Canvas()
18 {
19 mGenTexName = utility::toString((size_t)this, "_Canvas");
20 }
21
22 void Canvas::createTexture(TextureResizeMode _resizeMode, TextureUsage _usage, PixelFormat _format)
23 {
24 int width = std::max(1, getWidth());
25 int height = std::max(1, getHeight());
26
27 createTexture(width, height, _resizeMode, _usage, _format);
28 }
29
30 void Canvas::createTexture(
31 const IntSize& _size,
32 TextureResizeMode _resizeMode,
33 TextureUsage _usage,
34 PixelFormat _format)
35 {
36 int width = std::max(1, _size.width);
37 int height = std::max(1, _size.height);
38
39 createTexture(width, height, _resizeMode, _usage, _format);
40 }
41
42 void Canvas::createExactTexture(int _width, int _height, TextureUsage _usage, PixelFormat _format)
43 {
44 int width = std::max(1, _width);
45 int height = std::max(1, _height);
46
47 _destroyTexture(true);
48
49 mTexture = RenderManager::getInstance().createTexture(mGenTexName);
50 mTexture->setInvalidateListener(this);
51 mTexture->createManual(width, height, _usage, _format);
52
53 mTexManaged = true;
54
55 _setTextureName(mGenTexName);
56 correctUV();
57 requestUpdateCanvas(this, Event(true, true, mInvalidateData));
58 }
59
60 void Canvas::resize(const IntSize& _size)
61 {
62 if (_size.width <= 0 || _size.height <= 0 || !mTexManaged)
63 return;
64
65 mReqTexSize = _size;
66
67 frameAdvise(true);
68 }
69
70 void Canvas::createTexture(
71 int _width,
72 int _height,
73 TextureResizeMode _resizeMode,
74 TextureUsage _usage,
75 PixelFormat _format)
76 {
77 mTexResizeMode = _resizeMode;
78
79 int width = std::max(1, _width);
80 int height = std::max(1, _height);
81
82 if (_resizeMode == TRM_PT_CONST_SIZE)
83 {
84 mReqTexSize = IntSize(width, height);
85 }
86 else
87 {
88 mReqTexSize = IntSize(std::max(1, getWidth()), std::max(1, getHeight()));
89 }
90
91 bool create = checkCreate(width, height);
92
93 width = Bitwise::firstPO2From(width);
94 height = Bitwise::firstPO2From(height);
95
96 if (create)
97 createExactTexture(width, height, _usage, _format);
98 }
99
100 void Canvas::setSize(const IntSize& _size)
101 {
102 resize(_size);
103
104 Base::setSize(_size);
105 }
106
107 void Canvas::setCoord(const IntCoord& _coord)
108 {
109 resize(_coord.size());
110
111 Base::setCoord(_coord);
112 }
113
114 void Canvas::updateTexture()
115 {
116 mInvalidateData = true;
117 frameAdvise(true);
118 }
119
120 bool Canvas::checkCreate(int _width, int _height) const
121 {
122 if (mTexture == nullptr)
123 return true;
124
125 if (mTexture->getWidth() >= _width && mTexture->getHeight() >= _height)
126 return false;
127
128 return true;
129 }
130
131 void Canvas::validate(int& _width, int& _height, TextureUsage& _usage, PixelFormat& _format) const
132 {
133 _width = std::max(1, _width);
134 _height = std::max(1, _height);
135
136 _width = Bitwise::firstPO2From(_width);
137 _height = Bitwise::firstPO2From(_height);
138
139 // restore usage and format
140 if (mTexture != nullptr)
141 {
142 if (_usage == getDefaultTextureUsage())
143 _usage = mTexture->getUsage();
144
145 if (_format == getDefaultTextureFormat())
146 _format = mTexture->getFormat();
147 }
148 }
149
150 void Canvas::destroyTexture()
151 {
152 frameAdvise(false);
153 mInvalidateData = false;
154 _destroyTexture(true);
155 }
156
157 void Canvas::shutdownOverride()
158 {
159 _destroyTexture(false);
160 frameAdvise(false);
161 }
162
163 void Canvas::initialiseOverride()
164 {
165 }
166
167 void Canvas::_destroyTexture(bool _sendEvent)
168 {
169 if (mTexture != nullptr)
170 {
171 if (_sendEvent)
172 {
173 eventPreTextureChanges(this);
174 }
175
176 _setTextureName("");
177 RenderManager::getInstance().destroyTexture(mTexture);
178 mTexture = nullptr;
179 mTexData = nullptr;
180 }
181 }
182
183 void Canvas::correctUV()
184 {
185 if (mTexResizeMode == TRM_PT_VIEW_REQUESTED)
186 {
187 _setUVSet(FloatRect(
188 0,
189 0,
190 (float)mReqTexSize.width / (float)getTextureRealWidth(),
191 (float)mReqTexSize.height / (float)getTextureRealHeight()));
192 }
193
194 if (mTexResizeMode == TRM_PT_CONST_SIZE || mTexResizeMode == TRM_PT_VIEW_ALL)
195 {
196 _setUVSet(FloatRect(0, 0, 1, 1));
197 }
198 }
199
200 void* Canvas::lock(TextureUsage _usage)
201 {
202 void* data = mTexture->lock(_usage);
203
204 mTexData = reinterpret_cast<uint8*>(data);
205
206 return data;
207 }
208
209 void Canvas::unlock()
210 {
211 mTexture->unlock();
212 }
213
214 bool Canvas::isTextureSrcSize() const
215 {
216 return getTextureSrcSize() == getTextureRealSize();
217 }
218
219 void Canvas::frameAdvise(bool _advise)
220 {
221 if (_advise)
222 {
223 if (!mFrameAdvise)
224 {
225 MyGUI::Gui::getInstance().eventFrameStart += MyGUI::newDelegate(this, &Canvas::frameEntered);
226 mFrameAdvise = true;
227 }
228 }
229 else
230 {
231 if (mFrameAdvise)
232 {
233 MyGUI::Gui::getInstance().eventFrameStart -= MyGUI::newDelegate(this, &Canvas::frameEntered);
234 mFrameAdvise = false;
235 }
236 }
237 }
238
239 void Canvas::frameEntered(float _time)
240 {
241 int width = mReqTexSize.width;
242 int height = mReqTexSize.height;
243 TextureUsage usage = getDefaultTextureUsage();
244 PixelFormat format = getDefaultTextureFormat();
245
246 validate(width, height, usage, format);
247
248 bool create = mTexManaged && checkCreate(width, height);
249
250 if (mTexResizeMode == TRM_PT_CONST_SIZE)
251 create = false;
252
253 if (create)
254 {
255 createExactTexture(width, height, usage, format);
256 correctUV();
257 }
258 else // I thought order is important
259 {
260 correctUV();
261 requestUpdateCanvas(this, Event(false, true, mInvalidateData));
262 }
263
264 mInvalidateData = false;
265 frameAdvise(false);
266 }
267
268 void Canvas::textureInvalidate(ITexture* _texture)
269 {
270 updateTexture();
271 }
272
273 void Canvas::_setUVSet(const FloatRect& _rect)
274 {
275 if (nullptr != getSubWidgetMain())
276 getSubWidgetMain()->_setUVSet(_rect);
277 }
278
279 bool Canvas::isLocked() const
280 {
281 return mTexture->isLocked();
282 }
283
284 int Canvas::getTextureRealWidth() const
285 {
286 return (int)mTexture->getWidth();
287 }
288
289 int Canvas::getTextureRealHeight() const
290 {
291 return (int)mTexture->getHeight();
292 }
293
294 IntSize Canvas::getTextureRealSize() const
295 {
296 return {getTextureRealWidth(), getTextureRealHeight()};
297 }
298
299 int Canvas::getTextureSrcWidth() const
300 {
301 return mReqTexSize.width;
302 }
303
304 int Canvas::getTextureSrcHeight() const
305 {
306 return mReqTexSize.height;
307 }
308
309 IntSize Canvas::getTextureSrcSize() const
310 {
311 return mReqTexSize;
312 }
313
314 PixelFormat Canvas::getTextureFormat() const
315 {
316 return mTexture->getFormat();
317 }
318
319 const std::string& Canvas::getTextureName() const
320 {
321 return mTexture->getName();
322 }
323
324 Canvas::TextureResizeMode Canvas::getResizeMode() const
325 {
326 return mTexResizeMode;
327 }
328
329 void Canvas::setResizeMode(TextureResizeMode _value)
330 {
331 mTexResizeMode = _value;
332 }
333
334 bool Canvas::isTextureCreated() const
335 {
336 return mTexture != nullptr;
337 }
338
339 bool Canvas::isTextureManaged() const
340 {
341 return mTexManaged;
342 }
343
344 ITexture* Canvas::getTexture() const
345 {
346 return mTexture;
347 }
348
349 void Canvas::setTextureManaged(bool _value)
350 {
351 mTexManaged = _value;
352 }
353
354 TextureUsage Canvas::getDefaultTextureUsage()
355 {
356 return TextureUsage::Stream | TextureUsage::Write;
357 }
358
359 PixelFormat Canvas::getDefaultTextureFormat()
360 {
361 return PixelFormat::R8G8B8A8;
362 }
363
364} // namespace MyGUI
__attribute__((visibility("default"))) void trim(std std::string toString(T _value)
uint8_t uint8
Definition MyGUI_Types.h:45
types::TRect< float > FloatRect
Definition MyGUI_Types.h:33
types::TCoord< int > IntCoord
Definition MyGUI_Types.h:35
types::TSize< int > IntSize
Definition MyGUI_Types.h:29
delegates::DelegateFunction< Args... > * newDelegate(void(*_func)(Args... args))