MyGUI: MyGUI_Canvas.cpp Source File

MyGUI 3.4.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"
9#include "MyGUI_ResourceManager.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();
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 _destroyTexture(true);
153 }
154
155 void Canvas::shutdownOverride()
156 {
157 _destroyTexture(false);
158 frameAdvise(false);
159 }
160
161 void Canvas::initialiseOverride()
162 {
163 }
164
165 void Canvas::_destroyTexture(bool _sendEvent)
166 {
167 if (mTexture != nullptr)
168 {
169 if (_sendEvent)
170 {
171 eventPreTextureChanges(this);
172 }
173
174 RenderManager::getInstance().destroyTexture(mTexture);
175 mTexture = nullptr;
176 }
177 }
178
179 void Canvas::correctUV()
180 {
181 if (mTexResizeMode == TRM_PT_VIEW_REQUESTED)
182 {
183 _setUVSet(FloatRect(
184 0,
185 0,
186 (float)mReqTexSize.width / (float)getTextureRealWidth(),
187 (float)mReqTexSize.height / (float)getTextureRealHeight()));
188 }
189
190 if (mTexResizeMode == TRM_PT_CONST_SIZE || mTexResizeMode == TRM_PT_VIEW_ALL)
191 {
192 _setUVSet(FloatRect(0, 0, 1, 1));
193 }
194 }
195
196 void* Canvas::lock(TextureUsage _usage)
197 {
198 void* data = mTexture->lock(_usage);
199
200 mTexData = reinterpret_cast<uint8*>(data);
201
202 return data;
203 }
204
205 void Canvas::unlock()
206 {
207 mTexture->unlock();
208 }
209
210 bool Canvas::isTextureSrcSize() const
211 {
212 return getTextureSrcSize() == getTextureRealSize();
213 }
214
215 void Canvas::frameAdvise(bool _advise)
216 {
217 if (_advise)
218 {
219 if (!mFrameAdvise)
220 {
221 MyGUI::Gui::getInstance().eventFrameStart += MyGUI::newDelegate(this, &Canvas::frameEntered);
222 mFrameAdvise = true;
223 }
224 }
225 else
226 {
227 if (mFrameAdvise)
228 {
229 MyGUI::Gui::getInstance().eventFrameStart -= MyGUI::newDelegate(this, &Canvas::frameEntered);
230 mFrameAdvise = false;
231 }
232 }
233 }
234
235 void Canvas::frameEntered(float _time)
236 {
237 int width = mReqTexSize.width;
238 int height = mReqTexSize.height;
239 TextureUsage usage = getDefaultTextureUsage();
240 PixelFormat format = getDefaultTextureFormat();
241
242 validate(width, height, usage, format);
243
244 bool create = checkCreate(width, height);
245
246 if (mTexResizeMode == TRM_PT_CONST_SIZE)
247 create = false;
248
249 if (create)
250 {
251 createExactTexture(width, height, usage, format);
252 correctUV();
253 }
254 else // I thought order is important
255 {
256 correctUV();
257 requestUpdateCanvas(this, Event(false, true, mInvalidateData));
258 }
259
260 mInvalidateData = false;
261 frameAdvise(false);
262 }
263
264 void Canvas::textureInvalidate(ITexture* _texture)
265 {
266 updateTexture();
267 }
268
269 void Canvas::_setUVSet(const FloatRect& _rect)
270 {
271 if (nullptr != getSubWidgetMain())
272 getSubWidgetMain()->_setUVSet(_rect);
273 }
274
275 bool Canvas::isLocked() const
276 {
277 return mTexture->isLocked();
278 }
279
280 int Canvas::getTextureRealWidth() const
281 {
282 return (int)mTexture->getWidth();
283 }
284
285 int Canvas::getTextureRealHeight() const
286 {
287 return (int)mTexture->getHeight();
288 }
289
290 IntSize Canvas::getTextureRealSize() const
291 {
292 return {getTextureRealWidth(), getTextureRealHeight()};
293 }
294
295 int Canvas::getTextureSrcWidth() const
296 {
297 return mReqTexSize.width;
298 }
299
300 int Canvas::getTextureSrcHeight() const
301 {
302 return mReqTexSize.height;
303 }
304
305 IntSize Canvas::getTextureSrcSize() const
306 {
307 return mReqTexSize;
308 }
309
310 PixelFormat Canvas::getTextureFormat() const
311 {
312 return mTexture->getFormat();
313 }
314
315 const std::string& Canvas::getTextureName() const
316 {
317 return mTexture->getName();
318 }
319
320 Canvas::TextureResizeMode Canvas::getResizeMode() const
321 {
322 return mTexResizeMode;
323 }
324
325 void Canvas::setResizeMode(TextureResizeMode _value)
326 {
327 mTexResizeMode = _value;
328 }
329
330 bool Canvas::isTextureCreated() const
331 {
332 return mTexture != nullptr;
333 }
334
335 bool Canvas::isTextureManaged() const
336 {
337 return mTexManaged;
338 }
339
340 ITexture* Canvas::getTexture() const
341 {
342 return mTexture;
343 }
344
345 void Canvas::setTextureManaged(bool _value)
346 {
347 mTexManaged = _value;
348 }
349
350 TextureUsage Canvas::getDefaultTextureUsage()
351 {
352 return TextureUsage::Stream | TextureUsage::Write;
353 }
354
355 PixelFormat Canvas::getDefaultTextureFormat()
356 {
357 return PixelFormat::R8G8B8A8;
358 }
359
360} // namespace MyGUI
MyGUI_Bitwise.h
MyGUI_Canvas.h
MyGUI_Gui.h
MyGUI_Precompiled.h
MyGUI_RenderManager.h
MyGUI_ResourceManager.h
MyGUI::Canvas::mReqTexSize
IntSize mReqTexSize
Requested bu user sizes.
Definition MyGUI_Canvas.h:237
MyGUI::Canvas::frameAdvise
void frameAdvise(bool _advise)
For updating once per frame.
Definition MyGUI_Canvas.cpp:215
MyGUI::Canvas::correctUV
void correctUV()
Correct texture uv-coordinates.
Definition MyGUI_Canvas.cpp:179
MyGUI::Canvas::frameEntered
void frameEntered(float _time)
For updating once per frame.
Definition MyGUI_Canvas.cpp:235
MyGUI::Canvas::createExactTexture
void createExactTexture(int _width, int _height, TextureUsage _usage, PixelFormat _format)
Creates the texture itself.
Definition MyGUI_Canvas.cpp:42
MyGUI::Canvas::isTextureManaged
bool isTextureManaged() const
Returns true if we own the texture, otherwise false.
Definition MyGUI_Canvas.cpp:335
MyGUI::Canvas::mFrameAdvise
bool mFrameAdvise
For updating once per frame. True state means updating before next frame starts.
Definition MyGUI_Canvas.h:252
MyGUI::Canvas::isTextureCreated
bool isTextureCreated() const
Returns true if the texture was created (and exists), otherwise false.
Definition MyGUI_Canvas.cpp:330
MyGUI::Canvas::unlock
void unlock()
Unlocks hardware pixel buffer.
Definition MyGUI_Canvas.cpp:205
MyGUI::Canvas::isTextureSrcSize
bool isTextureSrcSize() const
Checks if the texture has the source (required by user) size, otherwise real texture size are bigger.
Definition MyGUI_Canvas.cpp:210
MyGUI::Canvas::requestUpdateCanvas
EventHandle_CanvasPtrEvent requestUpdateCanvas
Definition MyGUI_Canvas.h:198
MyGUI::Canvas::mTexManaged
bool mTexManaged
true if we own the texture (can delete it or replace by another instance), otherwise false
Definition MyGUI_Canvas.h:249
MyGUI::Canvas::createTexture
void createTexture(TextureResizeMode _resizeMode, TextureUsage _usage=getDefaultTextureUsage(), PixelFormat _format=getDefaultTextureFormat())
Creates texture.
Definition MyGUI_Canvas.cpp:22
MyGUI::Canvas::getTextureFormat
PixelFormat getTextureFormat() const
Returns needed sizes while creating texture.
Definition MyGUI_Canvas.cpp:310
MyGUI::Canvas::getTextureRealHeight
int getTextureRealHeight() const
Returns real height of texture.
Definition MyGUI_Canvas.cpp:285
MyGUI::Canvas::getDefaultTextureUsage
static TextureUsage getDefaultTextureUsage()
Returns default GUI texture usage.
Definition MyGUI_Canvas.cpp:350
MyGUI::Canvas::initialiseOverride
void initialiseOverride() override
Definition MyGUI_Canvas.cpp:161
MyGUI::Canvas::mTexResizeMode
TextureResizeMode mTexResizeMode
Texture resize mode.
Definition MyGUI_Canvas.h:243
MyGUI::Canvas::resize
void resize(const IntSize &_size)
Calls when resize widget.
Definition MyGUI_Canvas.cpp:60
MyGUI::Canvas::destroyTexture
void destroyTexture()
Destroys texture.
Definition MyGUI_Canvas.cpp:150
MyGUI::Canvas::_setUVSet
void _setUVSet(const FloatRect &_rect)
Definition MyGUI_Canvas.cpp:269
MyGUI::Canvas::_destroyTexture
void _destroyTexture(bool _sendEvent)
Destroys texture.
Definition MyGUI_Canvas.cpp:165
MyGUI::Canvas::Canvas
Canvas()
Definition MyGUI_Canvas.cpp:17
MyGUI::Canvas::textureInvalidate
void textureInvalidate(ITexture *_texture) override
Definition MyGUI_Canvas.cpp:264
MyGUI::Canvas::mTexData
uint8 * mTexData
Saved pointer from last calling lock.
Definition MyGUI_Canvas.h:246
MyGUI::Canvas::getTextureSrcSize
IntSize getTextureSrcSize() const
Returns needed sizes while creating texture.
Definition MyGUI_Canvas.cpp:305
MyGUI::Canvas::getTextureSrcWidth
int getTextureSrcWidth() const
Returns needed width while creating texture.
Definition MyGUI_Canvas.cpp:295
MyGUI::Canvas::updateTexture
void updateTexture()
Call user delegate update and removes old texture if it isn't original.
Definition MyGUI_Canvas.cpp:114
MyGUI::Canvas::shutdownOverride
void shutdownOverride() override
Definition MyGUI_Canvas.cpp:155
MyGUI::Canvas::mInvalidateData
bool mInvalidateData
Definition MyGUI_Canvas.h:254
MyGUI::Canvas::getResizeMode
TextureResizeMode getResizeMode() const
Returns resize mode.
Definition MyGUI_Canvas.cpp:320
MyGUI::Canvas::mTexture
ITexture * mTexture
Current texture.
Definition MyGUI_Canvas.h:234
MyGUI::Canvas::eventPreTextureChanges
EventHandle_CanvasPtr eventPreTextureChanges
Definition MyGUI_Canvas.h:191
MyGUI::Canvas::getTextureRealSize
IntSize getTextureRealSize() const
Returns real _size of texture.
Definition MyGUI_Canvas.cpp:290
MyGUI::Canvas::getTextureRealWidth
int getTextureRealWidth() const
Returns real width of texture.
Definition MyGUI_Canvas.cpp:280
MyGUI::Canvas::validate
void validate(int &_width, int &_height, TextureUsage &_usage, PixelFormat &_format) const
Update entered parameters according to current texture resize mode(size) and restore (if can) paramet...
Definition MyGUI_Canvas.cpp:131
MyGUI::Canvas::setResizeMode
void setResizeMode(TextureResizeMode _value)
Sets resize mode of texture.
Definition MyGUI_Canvas.cpp:325
MyGUI::Canvas::setSize
void setSize(const IntSize &_size) override
Definition MyGUI_Canvas.cpp:100
MyGUI::Canvas::getTextureSrcHeight
int getTextureSrcHeight() const
Returns needed height while creating texture.
Definition MyGUI_Canvas.cpp:300
MyGUI::Canvas::setCoord
void setCoord(const IntCoord &_coord) override
Definition MyGUI_Canvas.cpp:107
MyGUI::Canvas::TextureResizeMode
TextureResizeMode
Definition MyGUI_Canvas.h:52
MyGUI::Canvas::TRM_PT_VIEW_ALL
@ TRM_PT_VIEW_ALL
Definition MyGUI_Canvas.h:86
MyGUI::Canvas::TRM_PT_VIEW_REQUESTED
@ TRM_PT_VIEW_REQUESTED
Definition MyGUI_Canvas.h:73
MyGUI::Canvas::TRM_PT_CONST_SIZE
@ TRM_PT_CONST_SIZE
Definition MyGUI_Canvas.h:59
MyGUI::Canvas::mGenTexName
std::string mGenTexName
Generated texture name.
Definition MyGUI_Canvas.h:240
MyGUI::Canvas::lock
void * lock(TextureUsage _usage=TextureUsage::Write)
Locks hardware pixel buffer.
Definition MyGUI_Canvas.cpp:196
MyGUI::Canvas::setTextureManaged
void setTextureManaged(bool _value)
Sets the texture managed.
Definition MyGUI_Canvas.cpp:345
MyGUI::Canvas::getDefaultTextureFormat
static PixelFormat getDefaultTextureFormat()
Returns default GUI texture format.
Definition MyGUI_Canvas.cpp:355
MyGUI::Canvas::checkCreate
bool checkCreate(int _width, int _height) const
Checks if we need to create a texture with such sizes.
Definition MyGUI_Canvas.cpp:120
MyGUI::Canvas::getTextureName
const std::string & getTextureName() const
Returns name of the current texture.
Definition MyGUI_Canvas.cpp:315
MyGUI::Canvas::getTexture
ITexture * getTexture() const
Reurns interface texture.
Definition MyGUI_Canvas.cpp:340
MyGUI::Canvas::isLocked
bool isLocked() const
Checks lockness of hardware _pixel buffer.
Definition MyGUI_Canvas.cpp:275
MyGUI::Gui::getInstance
static Gui & getInstance()
Definition MyGUI_Gui.cpp:34
MyGUI::Gui::eventFrameStart
EventHandle_FrameEventDelegate eventFrameStart
Definition MyGUI_Gui.h:215
MyGUI::ICroppedRectangle::getWidth
int getWidth() const
Definition MyGUI_ICroppedRectangle.h:113
MyGUI::ICroppedRectangle::getHeight
int getHeight() const
Definition MyGUI_ICroppedRectangle.h:118
MyGUI::ISubWidgetRect::_setUVSet
virtual void _setUVSet(const FloatRect &)
Definition MyGUI_ISubWidgetRect.h:22
MyGUI::ITexture
Definition MyGUI_ITexture.h:28
MyGUI::RenderManager::createTexture
virtual ITexture * createTexture(const std::string &_name)=0
MyGUI::RenderManager::destroyTexture
virtual void destroyTexture(ITexture *_texture)=0
MyGUI::RenderManager::getInstance
static RenderManager & getInstance()
MyGUI::SkinItem::getSubWidgetMain
ISubWidgetRect * getSubWidgetMain() const
Definition MyGUI_SkinItem.cpp:153
MyGUI::SkinItem::_setTextureName
void _setTextureName(std::string_view _texture)
Definition MyGUI_SkinItem.cpp:126
MyGUI::Bitwise::firstPO2From
static Type firstPO2From(Type _value)
Definition MyGUI_Bitwise.h:16
MyGUI::utility::toString
std::string toString(T _value)
Definition MyGUI_StringUtility.h:26
MyGUI
Definition MyGUI_ActionController.h:15
MyGUI::uint8
uint8_t uint8
Definition MyGUI_Types.h:46
MyGUI::FloatRect
types::TRect< float > FloatRect
Definition MyGUI_Types.h:34
MyGUI::IntCoord
types::TCoord< int > IntCoord
Definition MyGUI_Types.h:36
MyGUI::IntSize
types::TSize< int > IntSize
Definition MyGUI_Types.h:30
MyGUI::newDelegate
delegates::DelegateFunction< Args... > * newDelegate(void(*_func)(Args... args))
Definition MyGUI_Delegate.h:100
MyGUI::Canvas::Event
Definition MyGUI_Canvas.h:29
MyGUI::PixelFormat
Definition MyGUI_RenderFormat.h:50
MyGUI::PixelFormat::R8G8B8A8
@ R8G8B8A8
Definition MyGUI_RenderFormat.h:57
MyGUI::TextureUsage
Definition MyGUI_RenderFormat.h:98
MyGUI::TextureUsage::Write
@ Write
Definition MyGUI_RenderFormat.h:106
MyGUI::TextureUsage::Stream
@ Stream
Definition MyGUI_RenderFormat.h:104
MyGUI::types::TCoord::size
TSize< T > size() const
Definition MyGUI_TCoord.h:165
MyGUI::types::TSize::width
T width
Definition MyGUI_TSize.h:19
MyGUI::types::TSize::height
T height
Definition MyGUI_TSize.h:20