MyGUI_SubSkin.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_SubSkin.h"
#include "MyGUI_RenderItem.h"
#include "MyGUI_SkinManager.h"
#include "MyGUI_LanguageManager.h"
#include "MyGUI_LayerNode.h"
#include "MyGUI_CommonStateInfo.h"
#include "MyGUI_RenderManager.h"
#include "MyGUI_TextureUtility.h"
namespace MyGUI
{
SubSkin::SubSkin()
{
mVertexFormat = RenderManager::getInstance().getVertexFormat();
}
void SubSkin::setVisible(bool _visible)
{
if (mVisible == _visible)
return;
mVisible = _visible;
if (nullptr != mNode)
mNode->outOfDate(mRenderItem);
}
void SubSkin::setAlpha(float _alpha)
{
uint32 alpha = ((uint8)(_alpha * 255) << 24);
mCurrentColour = (mCurrentColour & 0x00FFFFFF) | (alpha & 0xFF000000);
if (nullptr != mNode)
mNode->outOfDate(mRenderItem);
}
void SubSkin::_correctView()
{
if (nullptr != mNode)
mNode->outOfDate(mRenderItem);
}
void SubSkin::_setAlign(const IntSize& _oldsize)
{
// первоначальное выравнивание
if (mAlign.isHStretch())
{
// растягиваем
mCoord.width = mCoord.width + (mCroppedParent->getWidth() - _oldsize.width);
mIsMargin = true; // при изменении размеров все пересчитывать
}
else if (mAlign.isRight())
{
// двигаем по правому краю
mCoord.left = mCoord.left + (mCroppedParent->getWidth() - _oldsize.width);
}
else if (mAlign.isHCenter())
{
// выравнивание по горизонтали без растяжения
mCoord.left = (mCroppedParent->getWidth() - mCoord.width) / 2;
}
if (mAlign.isVStretch())
{
// растягиваем
mCoord.height = mCoord.height + (mCroppedParent->getHeight() - _oldsize.height);
mIsMargin = true; // при изменении размеров все пересчитывать
}
else if (mAlign.isBottom())
{
// двигаем по нижнему краю
mCoord.top = mCoord.top + (mCroppedParent->getHeight() - _oldsize.height);
}
else if (mAlign.isVCenter())
{
// выравнивание по вертикали без растяжения
mCoord.top = (mCroppedParent->getHeight() - mCoord.height) / 2;
}
mCurrentCoord = mCoord;
_updateView();
}
void SubSkin::_updateView()
{
//mAbsolutePosition = mCroppedParent->getAbsolutePosition() + mCoord.point();
bool margin = _checkMargin();
mEmptyView = ((0 >= _getViewWidth()) || (0 >= _getViewHeight()));
mCurrentCoord.left = mCoord.left + mMargin.left;
mCurrentCoord.top = mCoord.top + mMargin.top;
// вьюпорт стал битым
if (margin)
{
// проверка на полный выход за границу
if (_checkOutside())
{
// запоминаем текущее состояние
mIsMargin = margin;
// обновить перед выходом
if (nullptr != mNode)
mNode->outOfDate(mRenderItem);
return;
}
}
// мы обрезаны или были обрезаны
if (mIsMargin || margin)
{
mCurrentCoord.width = _getViewWidth();
mCurrentCoord.height = _getViewHeight();
if ((mCurrentCoord.width > 0) && (mCurrentCoord.height > 0))
{
// теперь смещаем текстуру
float UV_lft = mMargin.left / (float)mCoord.width;
float UV_top = mMargin.top / (float)mCoord.height;
float UV_rgt = (mCoord.width - mMargin.right) / (float)mCoord.width;
float UV_btm = (mCoord.height - mMargin.bottom) / (float)mCoord.height;
float UV_sizeX = mRectTexture.right - mRectTexture.left;
float UV_sizeY = mRectTexture.bottom - mRectTexture.top;
float UV_lft_total = mRectTexture.left + UV_lft * UV_sizeX;
float UV_top_total = mRectTexture.top + UV_top * UV_sizeY;
float UV_rgt_total = mRectTexture.right - (1 - UV_rgt) * UV_sizeX;
float UV_btm_total = mRectTexture.bottom - (1 - UV_btm) * UV_sizeY;
mCurrentTexture.set(UV_lft_total, UV_top_total, UV_rgt_total, UV_btm_total);
}
}
if (mIsMargin && !margin)
{
// мы не обрезаны, но были, ставим базовые координаты
mCurrentTexture = mRectTexture;
}
// запоминаем текущее состояние
mIsMargin = margin;
if (nullptr != mNode)
mNode->outOfDate(mRenderItem);
}
void SubSkin::createDrawItem(ITexture* _texture, ILayerNode* _node)
{
MYGUI_ASSERT(!mRenderItem, "mRenderItem must be nullptr");
mNode = _node;
mRenderItem = mNode->addToRenderItem(_texture, true, mSeparate);
mRenderItem->addDrawItem(this, VertexQuad::VertexCount);
}
void SubSkin::destroyDrawItem()
{
MYGUI_ASSERT(mRenderItem, "mRenderItem must be not nullptr");
mNode = nullptr;
mRenderItem->removeDrawItem(this);
mRenderItem = nullptr;
}
void SubSkin::_setUVSet(const FloatRect& _rect)
{
if (mRectTexture == _rect)
return;
mRectTexture = _rect;
// если обрезаны, то просчитываем с учето обрезки
if (mIsMargin)
{
float UV_lft = mMargin.left / (float)mCoord.width;
float UV_top = mMargin.top / (float)mCoord.height;
float UV_rgt = (mCoord.width - mMargin.right) / (float)mCoord.width;
float UV_btm = (mCoord.height - mMargin.bottom) / (float)mCoord.height;
float UV_sizeX = mRectTexture.right - mRectTexture.left;
float UV_sizeY = mRectTexture.bottom - mRectTexture.top;
float UV_lft_total = mRectTexture.left + UV_lft * UV_sizeX;
float UV_top_total = mRectTexture.top + UV_top * UV_sizeY;
float UV_rgt_total = mRectTexture.right - (1 - UV_rgt) * UV_sizeX;
float UV_btm_total = mRectTexture.bottom - (1 - UV_btm) * UV_sizeY;
mCurrentTexture.set(UV_lft_total, UV_top_total, UV_rgt_total, UV_btm_total);
}
// мы не обрезаны, базовые координаты
else
{
mCurrentTexture = mRectTexture;
}
if (nullptr != mNode)
mNode->outOfDate(mRenderItem);
}
void SubSkin::doRender()
{
if (!mVisible || mEmptyView)
return;
VertexQuad* quad = reinterpret_cast<VertexQuad*>(mRenderItem->getCurrentVertexBuffer());
const RenderTargetInfo& info = mRenderItem->getRenderTarget()->getInfo();
float vertex_z = mNode->getNodeDepth();
float vertex_left =
((info.pixScaleX * (float)(mCurrentCoord.left + mCroppedParent->getAbsoluteLeft() - info.leftOffset) +
info.hOffset) *
2) -
1;
float vertex_right = vertex_left + (info.pixScaleX * (float)mCurrentCoord.width * 2);
float vertex_top =
-(((info.pixScaleY * (float)(mCurrentCoord.top + mCroppedParent->getAbsoluteTop() - info.topOffset) +
info.vOffset) *
2) -
1);
float vertex_bottom = vertex_top - (info.pixScaleY * (float)mCurrentCoord.height * 2);
quad->set(
vertex_left,
vertex_top,
vertex_right,
vertex_bottom,
vertex_z,
mCurrentTexture.left,
mCurrentTexture.top,
mCurrentTexture.right,
mCurrentTexture.bottom,
mCurrentColour);
mRenderItem->setLastVertexCount(VertexQuad::VertexCount);
}
void SubSkin::_setColour(const Colour& _value)
{
uint32 colour = texture_utility::toNativeColour(_value, mVertexFormat);
mCurrentColour = (colour & 0x00FFFFFF) | (mCurrentColour & 0xFF000000);
if (nullptr != mNode)
mNode->outOfDate(mRenderItem);
}
void SubSkin::setStateData(IStateInfo* _data)
{
_setUVSet(_data->castType<SubSkinStateInfo>()->getRect());
}
} // namespace MyGUI
MyGUI_CommonStateInfo.h
MYGUI_ASSERT
#define MYGUI_ASSERT(exp, dest)
Definition MyGUI_Diagnostic.h:31
MyGUI_LanguageManager.h
MyGUI_LayerNode.h
MyGUI_Precompiled.h
MyGUI_RenderItem.h
MyGUI_RenderManager.h
MyGUI_SkinManager.h
MyGUI_SubSkin.h
MyGUI_TextureUtility.h
MyGUI::ICroppedRectangle::mIsMargin
bool mIsMargin
Definition MyGUI_ICroppedRectangle.h:248
MyGUI::ICroppedRectangle::mCoord
IntCoord mCoord
Definition MyGUI_ICroppedRectangle.h:245
MyGUI::ICroppedRectangle::mMargin
IntRect mMargin
Definition MyGUI_ICroppedRectangle.h:244
MyGUI::ICroppedRectangle::_checkMargin
bool _checkMargin()
Definition MyGUI_ICroppedRectangle.h:184
MyGUI::ICroppedRectangle::_checkOutside
bool _checkOutside() const
Definition MyGUI_ICroppedRectangle.h:234
MyGUI::ICroppedRectangle::mCroppedParent
ICroppedRectangle * mCroppedParent
Definition MyGUI_ICroppedRectangle.h:249
MyGUI::ICroppedRectangle::_getViewWidth
int _getViewWidth() const
Definition MyGUI_ICroppedRectangle.h:148
MyGUI::ICroppedRectangle::_getViewHeight
int _getViewHeight() const
Definition MyGUI_ICroppedRectangle.h:152
MyGUI::ILayerNode
Definition MyGUI_ILayerNode.h:29
MyGUI::IObject::castType
Type * castType(bool _throw=true)
Definition MyGUI_IObject.h:18
MyGUI::IStateInfo
Definition MyGUI_IStateInfo.h:17
MyGUI::ISubWidget::mAlign
Align mAlign
Definition MyGUI_ISubWidget.h:67
MyGUI::ISubWidget::mVisible
bool mVisible
Definition MyGUI_ISubWidget.h:68
MyGUI::ITexture
Definition MyGUI_ITexture.h:28
MyGUI::RenderManager::getVertexFormat
virtual VertexColourType getVertexFormat() const =0
MyGUI::RenderManager::getInstance
static RenderManager & getInstance()
MyGUI::SubSkin::destroyDrawItem
void destroyDrawItem() override
Definition MyGUI_SubSkin.cpp:165
MyGUI::SubSkin::mSeparate
bool mSeparate
Definition MyGUI_SubSkin.h:64
MyGUI::SubSkin::setVisible
void setVisible(bool _visible) override
Definition MyGUI_SubSkin.cpp:25
MyGUI::SubSkin::mCurrentTexture
FloatRect mCurrentTexture
Definition MyGUI_SubSkin.h:58
MyGUI::SubSkin::mEmptyView
bool mEmptyView
Definition MyGUI_SubSkin.h:53
MyGUI::SubSkin::mCurrentCoord
IntCoord mCurrentCoord
Definition MyGUI_SubSkin.h:59
MyGUI::SubSkin::mNode
ILayerNode * mNode
Definition MyGUI_SubSkin.h:61
MyGUI::SubSkin::setStateData
void setStateData(IStateInfo *_data) override
Definition MyGUI_SubSkin.cpp:256
MyGUI::SubSkin::_updateView
void _updateView() override
Definition MyGUI_SubSkin.cpp:91
MyGUI::SubSkin::mVertexFormat
VertexColourType mVertexFormat
Definition MyGUI_SubSkin.h:55
MyGUI::SubSkin::doRender
void doRender() override
Definition MyGUI_SubSkin.cpp:208
MyGUI::SubSkin::_setUVSet
void _setUVSet(const FloatRect &_rect) override
Definition MyGUI_SubSkin.cpp:174
MyGUI::SubSkin::SubSkin
SubSkin()
Definition MyGUI_SubSkin.cpp:20
MyGUI::SubSkin::_setColour
void _setColour(const Colour &_value) override
Definition MyGUI_SubSkin.cpp:247
MyGUI::SubSkin::_correctView
void _correctView() override
Definition MyGUI_SubSkin.cpp:44
MyGUI::SubSkin::mRenderItem
RenderItem * mRenderItem
Definition MyGUI_SubSkin.h:62
MyGUI::SubSkin::mRectTexture
FloatRect mRectTexture
Definition MyGUI_SubSkin.h:52
MyGUI::SubSkin::setAlpha
void setAlpha(float _alpha) override
Definition MyGUI_SubSkin.cpp:35
MyGUI::SubSkin::mCurrentColour
uint32 mCurrentColour
Definition MyGUI_SubSkin.h:56
MyGUI::SubSkin::createDrawItem
void createDrawItem(ITexture *_texture, ILayerNode *_node) override
Definition MyGUI_SubSkin.cpp:156
MyGUI::SubSkin::_setAlign
void _setAlign(const IntSize &_oldsize) override
Definition MyGUI_SubSkin.cpp:50
MyGUI::SubSkinStateInfo
Definition MyGUI_CommonStateInfo.h:20
MyGUI::SubSkinStateInfo::getRect
const FloatRect & getRect() const
Definition MyGUI_CommonStateInfo.h:24
MyGUI::texture_utility::toNativeColour
uint32 toNativeColour(const Colour &_colour, VertexColourType _format)
Convert Colour to 32-bit representation.
Definition MyGUI_TextureUtility.cpp:63
MyGUI
Definition MyGUI_ActionController.h:15
MyGUI::uint32
uint32_t uint32
Definition MyGUI_Types.h:48
MyGUI::uint8
uint8_t uint8
Definition MyGUI_Types.h:46
MyGUI::FloatRect
types::TRect< float > FloatRect
Definition MyGUI_Types.h:34
MyGUI::IntSize
types::TSize< int > IntSize
Definition MyGUI_Types.h:30
MyGUI::Colour
Definition MyGUI_Colour.h:17
MyGUI::RenderTargetInfo
Definition MyGUI_RenderTargetInfo.h:16
MyGUI::RenderTargetInfo::vOffset
float vOffset
Definition MyGUI_RenderTargetInfo.h:29
MyGUI::RenderTargetInfo::topOffset
int topOffset
Definition MyGUI_RenderTargetInfo.h:33
MyGUI::RenderTargetInfo::hOffset
float hOffset
Definition MyGUI_RenderTargetInfo.h:28
MyGUI::RenderTargetInfo::pixScaleX
float pixScaleX
Definition MyGUI_RenderTargetInfo.h:26
MyGUI::RenderTargetInfo::leftOffset
int leftOffset
Definition MyGUI_RenderTargetInfo.h:32
MyGUI::RenderTargetInfo::pixScaleY
float pixScaleY
Definition MyGUI_RenderTargetInfo.h:27
MyGUI::VertexQuad
Definition MyGUI_VertexData.h:34
MyGUI::VertexQuad::VertexCount
@ VertexCount
Definition MyGUI_VertexData.h:43
MyGUI::VertexQuad::set
void set(float _l, float _t, float _r, float _b, float _z, float _u1, float _v1, float _u2, float _v2, uint32 _colour)
Definition MyGUI_VertexData.h:46
MyGUI::types::TSize::width
T width
Definition MyGUI_TSize.h:19
MyGUI::types::TSize::height
T height
Definition MyGUI_TSize.h:20