MyGUI_TileRect.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_TileRect.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
{
const size_t TILERECT_COUNT_VERTEX = 16 * VertexQuad::VertexCount;
TileRect::TileRect() :
mCountVertex(TILERECT_COUNT_VERTEX)
{
mVertexFormat = RenderManager::getInstance().getVertexFormat();
}
void TileRect::setVisible(bool _visible)
{
if (mVisible == _visible)
return;
mVisible = _visible;
if (nullptr != mNode)
mNode->outOfDate(mRenderItem);
}
void TileRect::setAlpha(float _alpha)
{
uint32 alpha = ((uint8)(_alpha * 255) << 24);
mCurrentColour = (mCurrentColour & 0x00FFFFFF) | (alpha & 0xFF000000);
if (nullptr != mNode)
mNode->outOfDate(mRenderItem);
}
void TileRect::_correctView()
{
if (nullptr != mNode)
mNode->outOfDate(mRenderItem);
}
void TileRect::_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;
if (!mTileH)
mTileSize.width = mCoord.width;
if (!mTileV)
mTileSize.height = mCoord.height;
_updateView();
}
void TileRect::_updateView()
{
bool margin = _checkMargin();
mEmptyView = ((0 >= _getViewWidth()) || (0 >= _getViewHeight()));
mCurrentCoord.left = mCoord.left + mMargin.left;
mCurrentCoord.top = mCoord.top + mMargin.top;
mCurrentCoord.width = _getViewWidth();
mCurrentCoord.height = _getViewHeight();
// подсчитываем необходимое колличество тайлов
if (!mEmptyView)
{
size_t count = 0;
if (!mTileSize.empty())
{
size_t count_x = mCoord.width / mTileSize.width;
if ((mCoord.width % mTileSize.width) > 0)
count_x++;
size_t count_y = mCoord.height / mTileSize.height;
if ((mCoord.height % mTileSize.height) > 0)
count_y++;
count = count_y * count_x * VertexQuad::VertexCount;
}
// нужно больше вершин
if (count > mCountVertex)
{
mCountVertex = count + TILERECT_COUNT_VERTEX;
if (nullptr != mRenderItem)
mRenderItem->reallockDrawItem(this, mCountVertex);
}
}
// вьюпорт стал битым
if (margin)
{
// проверка на полный выход за границу
if (_checkOutside())
{
// запоминаем текущее состояние
mIsMargin = margin;
// обновить перед выходом
if (nullptr != mNode)
mNode->outOfDate(mRenderItem);
return;
}
}
// запоминаем текущее состояние
mIsMargin = margin;
if (nullptr != mNode)
mNode->outOfDate(mRenderItem);
}
void TileRect::_setUVSet(const FloatRect& _rect)
{
mCurrentTexture = _rect;
if (nullptr != mNode)
mNode->outOfDate(mRenderItem);
}
void TileRect::doRender()
{
if (!mVisible || mEmptyView || mTileSize.empty())
return;
VertexQuad* quad = reinterpret_cast<VertexQuad*>(mRenderItem->getCurrentVertexBuffer());
const RenderTargetInfo& info = mRenderItem->getRenderTarget()->getInfo();
// размер одного тайла
mRealTileWidth = info.pixScaleX * (float)(mTileSize.width) * 2;
mRealTileHeight = info.pixScaleY * (float)(mTileSize.height) * 2;
mTextureHeightOne = (mCurrentTexture.bottom - mCurrentTexture.top) / mRealTileHeight;
mTextureWidthOne = (mCurrentTexture.right - mCurrentTexture.left) / mRealTileWidth;
float vertex_z = mNode->getNodeDepth();
// абсолютный размер окна
float window_left =
((info.pixScaleX * (float)(mCoord.left + mCroppedParent->getAbsoluteLeft() - info.leftOffset) +
info.hOffset) *
2) -
1;
float window_top = -(
((info.pixScaleY * (float)(mCoord.top + mCroppedParent->getAbsoluteTop() - info.topOffset) + info.vOffset) *
2) -
1);
// размер вьюпорта
float real_left =
((info.pixScaleX * (float)(mCurrentCoord.left + mCroppedParent->getAbsoluteLeft() - info.leftOffset) +
info.hOffset) *
2) -
1;
float real_right = real_left + (info.pixScaleX * (float)mCurrentCoord.width * 2);
float real_top =
-(((info.pixScaleY * (float)(mCurrentCoord.top + mCroppedParent->getAbsoluteTop() - info.topOffset) +
info.vOffset) *
2) -
1);
float real_bottom = real_top - (info.pixScaleY * (float)mCurrentCoord.height * 2);
size_t count = 0;
float bottom = window_top;
for (int y = 0; y < mCoord.height; y += mTileSize.height)
{
float top = bottom;
bottom -= mRealTileHeight;
float right = window_left;
float vertex_top = top;
float vertex_bottom = bottom;
bool texture_crop_height = false;
if (vertex_top > real_top)
{
// проверка на полный выход
if (vertex_bottom > real_top)
{
continue;
}
// обрезаем
vertex_top = real_top;
texture_crop_height = true;
}
if (vertex_bottom < real_bottom)
{
// вообще вниз ушли
if (vertex_top < real_bottom)
{
continue;
}
// обрезаем
vertex_bottom = real_bottom;
texture_crop_height = true;
}
for (int x = 0; x < mCoord.width; x += mTileSize.width)
{
float left = right;
right += mRealTileWidth;
float vertex_left = left;
float vertex_right = right;
bool texture_crop_width = false;
if (vertex_left < real_left)
{
// проверка на полный выход
if (vertex_right < real_left)
{
continue;
}
// обрезаем
vertex_left = real_left;
texture_crop_width = true;
}
if (vertex_right > real_right)
{
// вообще строку до конца не нуна
if (vertex_left > real_right)
{
continue;
}
// обрезаем
vertex_right = real_right;
texture_crop_width = true;
}
// текущие текстурные координаты
float texture_left = mCurrentTexture.left;
float texture_right = mCurrentTexture.right;
float texture_top = mCurrentTexture.top;
float texture_bottom = mCurrentTexture.bottom;
// смещение текстуры по вертикили
if (texture_crop_height)
{
// прибавляем размер смещения в текстурных координатах
texture_top += (top - vertex_top) * mTextureHeightOne;
// отнимаем размер смещения в текстурных координатах
texture_bottom -= (vertex_bottom - bottom) * mTextureHeightOne;
}
// смещение текстуры по горизонтали
if (texture_crop_width)
{
// прибавляем размер смещения в текстурных координатах
texture_left += (vertex_left - left) * mTextureWidthOne;
// отнимаем размер смещения в текстурных координатах
texture_right -= (right - vertex_right) * mTextureWidthOne;
}
quad[count].set(
vertex_left,
vertex_top,
vertex_right,
vertex_bottom,
vertex_z,
texture_left,
texture_top,
texture_right,
texture_bottom,
mCurrentColour);
count++;
}
}
mRenderItem->setLastVertexCount(VertexQuad::VertexCount * count);
}
void TileRect::createDrawItem(ITexture* _texture, ILayerNode* _node)
{
MYGUI_ASSERT(!mRenderItem, "mRenderItem must be nullptr");
mNode = _node;
mRenderItem = mNode->addToRenderItem(_texture, true, false);
mRenderItem->addDrawItem(this, mCountVertex);
}
void TileRect::destroyDrawItem()
{
MYGUI_ASSERT(mRenderItem, "mRenderItem must be not nullptr");
mNode = nullptr;
mRenderItem->removeDrawItem(this);
mRenderItem = nullptr;
}
void TileRect::setStateData(IStateInfo* _data)
{
TileRectStateInfo* data = _data->castType<TileRectStateInfo>();
mTileSize = data->getTileSize();
mTileH = data->getTileH();
mTileV = data->getTileV();
_setUVSet(data->getRect());
}
void TileRect::_setColour(const Colour& _value)
{
uint32 colour = texture_utility::toNativeColour(_value, mVertexFormat);
mCurrentColour = (colour & 0x00FFFFFF) | (mCurrentColour & 0xFF000000);
if (nullptr != mNode)
mNode->outOfDate(mRenderItem);
}
} // 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_TextureUtility.h
MyGUI_TileRect.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::TileRect::mTextureWidthOne
float mTextureWidthOne
Definition MyGUI_TileRect.h:69
MyGUI::TileRect::_setAlign
void _setAlign(const IntSize &_oldsize) override
Definition MyGUI_TileRect.cpp:53
MyGUI::TileRect::mRealTileHeight
float mRealTileHeight
Definition MyGUI_TileRect.h:66
MyGUI::TileRect::mRenderItem
RenderItem * mRenderItem
Definition MyGUI_TileRect.h:60
MyGUI::TileRect::mTileH
bool mTileH
Definition MyGUI_TileRect.h:71
MyGUI::TileRect::mCurrentTexture
FloatRect mCurrentTexture
Definition MyGUI_TileRect.h:56
MyGUI::TileRect::mEmptyView
bool mEmptyView
Definition MyGUI_TileRect.h:51
MyGUI::TileRect::_setUVSet
void _setUVSet(const FloatRect &_rect) override
Definition MyGUI_TileRect.cpp:158
MyGUI::TileRect::mCurrentCoord
IntCoord mCurrentCoord
Definition MyGUI_TileRect.h:57
MyGUI::TileRect::mCurrentColour
uint32 mCurrentColour
Definition MyGUI_TileRect.h:54
MyGUI::TileRect::setVisible
void setVisible(bool _visible) override
Definition MyGUI_TileRect.cpp:28
MyGUI::TileRect::createDrawItem
void createDrawItem(ITexture *_texture, ILayerNode *_node) override
Definition MyGUI_TileRect.cpp:322
MyGUI::TileRect::TileRect
TileRect()
Definition MyGUI_TileRect.cpp:22
MyGUI::TileRect::_setColour
void _setColour(const Colour &_value) override
Definition MyGUI_TileRect.cpp:350
MyGUI::TileRect::mTextureHeightOne
float mTextureHeightOne
Definition MyGUI_TileRect.h:68
MyGUI::TileRect::setStateData
void setStateData(IStateInfo *_data) override
Definition MyGUI_TileRect.cpp:340
MyGUI::TileRect::mCountVertex
size_t mCountVertex
Definition MyGUI_TileRect.h:63
MyGUI::TileRect::mTileSize
IntSize mTileSize
Definition MyGUI_TileRect.h:62
MyGUI::TileRect::_correctView
void _correctView() override
Definition MyGUI_TileRect.cpp:47
MyGUI::TileRect::mRealTileWidth
float mRealTileWidth
Definition MyGUI_TileRect.h:65
MyGUI::TileRect::mNode
ILayerNode * mNode
Definition MyGUI_TileRect.h:59
MyGUI::TileRect::setAlpha
void setAlpha(float _alpha) override
Definition MyGUI_TileRect.cpp:38
MyGUI::TileRect::doRender
void doRender() override
Definition MyGUI_TileRect.cpp:165
MyGUI::TileRect::mVertexFormat
VertexColourType mVertexFormat
Definition MyGUI_TileRect.h:53
MyGUI::TileRect::destroyDrawItem
void destroyDrawItem() override
Definition MyGUI_TileRect.cpp:331
MyGUI::TileRect::_updateView
void _updateView() override
Definition MyGUI_TileRect.cpp:98
MyGUI::TileRect::mTileV
bool mTileV
Definition MyGUI_TileRect.h:72
MyGUI::TileRectStateInfo
Definition MyGUI_CommonStateInfo.h:50
MyGUI::TileRectStateInfo::getTileV
bool getTileV() const
Definition MyGUI_CommonStateInfo.h:69
MyGUI::TileRectStateInfo::getRect
const FloatRect & getRect() const
Definition MyGUI_CommonStateInfo.h:54
MyGUI::TileRectStateInfo::getTileSize
const IntSize & getTileSize() const
Definition MyGUI_CommonStateInfo.h:59
MyGUI::TileRectStateInfo::getTileH
bool getTileH() const
Definition MyGUI_CommonStateInfo.h:64
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::TILERECT_COUNT_VERTEX
const size_t TILERECT_COUNT_VERTEX
Definition MyGUI_TileRect.cpp:20
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