MyGUI_Canvas.cpp
Go to the documentation of this file.
/*
* This source file is part of MyGUI. For the latest info, see http://mygui.info/
* Distributed under the MIT License
* (See accompanying file COPYING.MIT or copy at http://opensource.org/licenses/MIT)
*/
#include "MyGUI_Precompiled.h"
#include "MyGUI_Canvas.h"
#include "MyGUI_ResourceManager.h"
#include "MyGUI_Gui.h"
#include "MyGUI_RenderManager.h"
#include "MyGUI_Bitwise.h"
namespace MyGUI
{
Canvas::Canvas()
{
mGenTexName = utility::toString((size_t)this, "_Canvas");
}
void Canvas::createTexture(TextureResizeMode _resizeMode, TextureUsage _usage, PixelFormat _format)
{
int width = std::max(1, getWidth());
int height = std::max(1, getHeight());
createTexture(width, height, _resizeMode, _usage, _format);
}
void Canvas::createTexture(
const IntSize& _size,
TextureResizeMode _resizeMode,
TextureUsage _usage,
PixelFormat _format)
{
int width = std::max(1, _size.width);
int height = std::max(1, _size.height);
createTexture(width, height, _resizeMode, _usage, _format);
}
void Canvas::createExactTexture(int _width, int _height, TextureUsage _usage, PixelFormat _format)
{
int width = std::max(1, _width);
int height = std::max(1, _height);
destroyTexture();
mTexture = RenderManager::getInstance().createTexture(mGenTexName);
mTexture->setInvalidateListener(this);
mTexture->createManual(width, height, _usage, _format);
mTexManaged = true;
_setTextureName(mGenTexName);
correctUV();
requestUpdateCanvas(this, Event(true, true, mInvalidateData));
}
void Canvas::resize(const IntSize& _size)
{
if (_size.width <= 0 || _size.height <= 0 || !mTexManaged)
return;
mReqTexSize = _size;
frameAdvise(true);
}
void Canvas::createTexture(
int _width,
int _height,
TextureResizeMode _resizeMode,
TextureUsage _usage,
PixelFormat _format)
{
mTexResizeMode = _resizeMode;
int width = std::max(1, _width);
int height = std::max(1, _height);
if (_resizeMode == TRM_PT_CONST_SIZE)
{
mReqTexSize = IntSize(width, height);
}
else
{
mReqTexSize = IntSize(std::max(1, getWidth()), std::max(1, getHeight()));
}
bool create = checkCreate(width, height);
width = Bitwise::firstPO2From(width);
height = Bitwise::firstPO2From(height);
if (create)
createExactTexture(width, height, _usage, _format);
}
void Canvas::setSize(const IntSize& _size)
{
resize(_size);
Base::setSize(_size);
}
void Canvas::setCoord(const IntCoord& _coord)
{
resize(_coord.size());
Base::setCoord(_coord);
}
void Canvas::updateTexture()
{
mInvalidateData = true;
frameAdvise(true);
}
bool Canvas::checkCreate(int _width, int _height) const
{
if (mTexture == nullptr)
return true;
if (mTexture->getWidth() >= _width && mTexture->getHeight() >= _height)
return false;
return true;
}
void Canvas::validate(int& _width, int& _height, TextureUsage& _usage, PixelFormat& _format) const
{
_width = std::max(1, _width);
_height = std::max(1, _height);
_width = Bitwise::firstPO2From(_width);
_height = Bitwise::firstPO2From(_height);
// restore usage and format
if (mTexture != nullptr)
{
if (_usage == getDefaultTextureUsage())
_usage = mTexture->getUsage();
if (_format == getDefaultTextureFormat())
_format = mTexture->getFormat();
}
}
void Canvas::destroyTexture()
{
_destroyTexture(true);
}
void Canvas::shutdownOverride()
{
_destroyTexture(false);
frameAdvise(false);
}
void Canvas::initialiseOverride()
{
}
void Canvas::_destroyTexture(bool _sendEvent)
{
if (mTexture != nullptr)
{
if (_sendEvent)
{
eventPreTextureChanges(this);
}
RenderManager::getInstance().destroyTexture(mTexture);
mTexture = nullptr;
}
}
void Canvas::correctUV()
{
if (mTexResizeMode == TRM_PT_VIEW_REQUESTED)
{
_setUVSet(FloatRect(
0,
0,
(float)mReqTexSize.width / (float)getTextureRealWidth(),
(float)mReqTexSize.height / (float)getTextureRealHeight()));
}
if (mTexResizeMode == TRM_PT_CONST_SIZE || mTexResizeMode == TRM_PT_VIEW_ALL)
{
_setUVSet(FloatRect(0, 0, 1, 1));
}
}
void* Canvas::lock(TextureUsage _usage)
{
void* data = mTexture->lock(_usage);
mTexData = reinterpret_cast<uint8*>(data);
return data;
}
void Canvas::unlock()
{
mTexture->unlock();
}
bool Canvas::isTextureSrcSize() const
{
return getTextureSrcSize() == getTextureRealSize();
}
void Canvas::frameAdvise(bool _advise)
{
if (_advise)
{
if (!mFrameAdvise)
{
MyGUI::Gui::getInstance().eventFrameStart += MyGUI::newDelegate(this, &Canvas::frameEntered);
mFrameAdvise = true;
}
}
else
{
if (mFrameAdvise)
{
MyGUI::Gui::getInstance().eventFrameStart -= MyGUI::newDelegate(this, &Canvas::frameEntered);
mFrameAdvise = false;
}
}
}
void Canvas::frameEntered(float _time)
{
int width = mReqTexSize.width;
int height = mReqTexSize.height;
TextureUsage usage = getDefaultTextureUsage();
PixelFormat format = getDefaultTextureFormat();
validate(width, height, usage, format);
bool create = checkCreate(width, height);
if (mTexResizeMode == TRM_PT_CONST_SIZE)
create = false;
if (create)
{
createExactTexture(width, height, usage, format);
correctUV();
}
else // I thought order is important
{
correctUV();
requestUpdateCanvas(this, Event(false, true, mInvalidateData));
}
mInvalidateData = false;
frameAdvise(false);
}
void Canvas::textureInvalidate(ITexture* _texture)
{
updateTexture();
}
void Canvas::_setUVSet(const FloatRect& _rect)
{
if (nullptr != getSubWidgetMain())
getSubWidgetMain()->_setUVSet(_rect);
}
bool Canvas::isLocked() const
{
return mTexture->isLocked();
}
int Canvas::getTextureRealWidth() const
{
return (int)mTexture->getWidth();
}
int Canvas::getTextureRealHeight() const
{
return (int)mTexture->getHeight();
}
IntSize Canvas::getTextureRealSize() const
{
return {getTextureRealWidth(), getTextureRealHeight()};
}
int Canvas::getTextureSrcWidth() const
{
return mReqTexSize.width;
}
int Canvas::getTextureSrcHeight() const
{
return mReqTexSize.height;
}
IntSize Canvas::getTextureSrcSize() const
{
return mReqTexSize;
}
PixelFormat Canvas::getTextureFormat() const
{
return mTexture->getFormat();
}
const std::string& Canvas::getTextureName() const
{
return mTexture->getName();
}
Canvas::TextureResizeMode Canvas::getResizeMode() const
{
return mTexResizeMode;
}
void Canvas::setResizeMode(TextureResizeMode _value)
{
mTexResizeMode = _value;
}
bool Canvas::isTextureCreated() const
{
return mTexture != nullptr;
}
bool Canvas::isTextureManaged() const
{
return mTexManaged;
}
ITexture* Canvas::getTexture() const
{
return mTexture;
}
void Canvas::setTextureManaged(bool _value)
{
mTexManaged = _value;
}
TextureUsage Canvas::getDefaultTextureUsage()
{
return TextureUsage::Stream | TextureUsage::Write;
}
PixelFormat Canvas::getDefaultTextureFormat()
{
return PixelFormat::R8G8B8A8;
}
} // 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