2014-06-20 01:30:09 +00:00
|
|
|
#include "components/NinePatchComponent.h"
|
2017-11-01 22:21:10 +00:00
|
|
|
|
|
|
|
#include "resources/TextureResource.h"
|
2014-06-20 01:30:09 +00:00
|
|
|
#include "Log.h"
|
|
|
|
#include "ThemeData.h"
|
2013-08-23 02:41:40 +00:00
|
|
|
|
|
|
|
NinePatchComponent::NinePatchComponent(Window* window, const std::string& path, unsigned int edgeColor, unsigned int centerColor) : GuiComponent(window),
|
2018-04-21 13:23:10 +00:00
|
|
|
mCornerSize(16, 16),
|
|
|
|
mEdgeColor(edgeColor), mCenterColor(centerColor),
|
2013-09-14 15:58:34 +00:00
|
|
|
mPath(path),
|
2019-08-08 20:16:11 +00:00
|
|
|
mVertices(NULL)
|
2013-08-23 02:41:40 +00:00
|
|
|
{
|
2013-09-14 17:51:13 +00:00
|
|
|
if(!mPath.empty())
|
|
|
|
buildVertices();
|
2013-08-23 02:41:40 +00:00
|
|
|
}
|
|
|
|
|
2015-02-09 22:23:36 +00:00
|
|
|
NinePatchComponent::~NinePatchComponent()
|
|
|
|
{
|
|
|
|
if (mVertices != NULL)
|
|
|
|
delete[] mVertices;
|
|
|
|
}
|
|
|
|
|
2013-09-14 15:58:34 +00:00
|
|
|
void NinePatchComponent::updateColors()
|
|
|
|
{
|
2019-08-08 20:16:11 +00:00
|
|
|
const unsigned int edgeColor = Renderer::convertColor(mEdgeColor);
|
|
|
|
const unsigned int centerColor = Renderer::convertColor(mCenterColor);
|
|
|
|
|
|
|
|
for(int i = 0; i < 6*9; ++i)
|
|
|
|
mVertices[i].col = edgeColor;
|
|
|
|
|
|
|
|
for(int i = 6*4; i < 6; ++i)
|
|
|
|
mVertices[(6*4)+i].col = mCenterColor;
|
2013-09-14 15:58:34 +00:00
|
|
|
}
|
|
|
|
|
2013-08-23 02:41:40 +00:00
|
|
|
void NinePatchComponent::buildVertices()
|
|
|
|
{
|
|
|
|
if(mVertices != NULL)
|
|
|
|
delete[] mVertices;
|
|
|
|
|
2013-10-04 23:09:54 +00:00
|
|
|
mTexture = TextureResource::get(mPath);
|
2013-09-14 15:58:34 +00:00
|
|
|
|
2017-10-28 20:24:35 +00:00
|
|
|
if(mTexture->getSize() == Vector2i::Zero())
|
2013-08-23 02:41:40 +00:00
|
|
|
{
|
|
|
|
mVertices = NULL;
|
|
|
|
LOG(LogWarning) << "NinePatchComponent missing texture!";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-08-08 20:16:11 +00:00
|
|
|
mVertices = new Renderer::Vertex[6 * 9];
|
2013-08-23 02:41:40 +00:00
|
|
|
|
2018-04-21 13:23:10 +00:00
|
|
|
const Vector2f texSize = Vector2f((float)mTexture->getSize().x(), (float)mTexture->getSize().y());
|
2013-08-23 02:41:40 +00:00
|
|
|
|
2019-08-08 20:16:11 +00:00
|
|
|
const float imgSizeX[3] = { mCornerSize.x(), mSize.x() - mCornerSize.x() * 2, mCornerSize.x()};
|
|
|
|
const float imgSizeY[3] = { mCornerSize.y(), mSize.y() - mCornerSize.y() * 2, mCornerSize.y()};
|
|
|
|
const float imgPosX[3] = { 0, imgSizeX[0], imgSizeX[0] + imgSizeX[1]};
|
|
|
|
const float imgPosY[3] = { 0, imgSizeY[0], imgSizeY[0] + imgSizeY[1]};
|
2013-08-23 02:41:40 +00:00
|
|
|
|
2018-04-21 13:23:10 +00:00
|
|
|
//the "1 +" in posY and "-" in sizeY is to deal with texture coordinates having a bottom left corner origin vs. verticies having a top left origin
|
2019-08-08 20:16:11 +00:00
|
|
|
const float texSizeX[3] = { mCornerSize.x() / texSize.x(), (texSize.x() - mCornerSize.x() * 2) / texSize.x(), mCornerSize.x() / texSize.x() };
|
|
|
|
const float texSizeY[3] = { -mCornerSize.y() / texSize.y(), -(texSize.y() - mCornerSize.y() * 2) / texSize.y(), -mCornerSize.y() / texSize.y() };
|
|
|
|
const float texPosX[3] = { 0, texSizeX[0], texSizeX[0] + texSizeX[1] };
|
|
|
|
const float texPosY[3] = { 1, 1 + texSizeY[0], 1 + texSizeY[0] + texSizeY[1] };
|
2013-08-23 02:41:40 +00:00
|
|
|
|
|
|
|
int v = 0;
|
|
|
|
for(int slice = 0; slice < 9; slice++)
|
|
|
|
{
|
2019-08-08 20:16:11 +00:00
|
|
|
const int sliceX = slice % 3;
|
|
|
|
const int sliceY = slice / 3;
|
|
|
|
const Vector2f imgPos = Vector2f(imgPosX[sliceX], imgPosY[sliceY]);
|
|
|
|
const Vector2f imgSize = Vector2f(imgSizeX[sliceX], imgSizeY[sliceY]);
|
|
|
|
const Vector2f texPos = Vector2f(texPosX[sliceX], texPosY[sliceY]);
|
|
|
|
const Vector2f texSize = Vector2f(texSizeX[sliceX], texSizeY[sliceY]);
|
|
|
|
|
|
|
|
mVertices[v + 1] = { { imgPos.x() , imgPos.y() }, { texPos.x(), texPos.y() }, 0 };
|
|
|
|
mVertices[v + 2] = { { imgPos.x() , imgPos.y() + imgSize.y() }, { texPos.x(), texPos.y() + texSize.y() }, 0 };
|
|
|
|
mVertices[v + 3] = { { imgPos.x() + imgSize.x(), imgPos.y() }, { texPos.x() + texSize.x(), texPos.y() }, 0 };
|
|
|
|
mVertices[v + 4] = { { imgPos.x() + imgSize.x(), imgPos.y() + imgSize.y() }, { texPos.x() + texSize.x(), texPos.y() + texSize.y() }, 0 };
|
|
|
|
|
|
|
|
// make duplicates of first and last vertex so this can be rendered as a triangle strip
|
|
|
|
mVertices[v + 0] = mVertices[v + 1];
|
|
|
|
mVertices[v + 5] = mVertices[v + 4];
|
2013-08-23 02:41:40 +00:00
|
|
|
|
|
|
|
v += 6;
|
|
|
|
}
|
2014-03-19 20:03:23 +00:00
|
|
|
|
|
|
|
// round vertices
|
|
|
|
for(int i = 0; i < 6*9; i++)
|
|
|
|
{
|
2017-10-28 20:24:35 +00:00
|
|
|
mVertices[i].pos.round();
|
2014-03-19 20:03:23 +00:00
|
|
|
}
|
2019-08-08 20:16:11 +00:00
|
|
|
|
|
|
|
updateColors();
|
2013-08-23 02:41:40 +00:00
|
|
|
}
|
|
|
|
|
2017-10-28 20:24:35 +00:00
|
|
|
void NinePatchComponent::render(const Transform4x4f& parentTrans)
|
2013-08-23 02:41:40 +00:00
|
|
|
{
|
2017-10-28 20:24:35 +00:00
|
|
|
Transform4x4f trans = parentTrans * getTransform();
|
|
|
|
trans.round();
|
2014-03-19 20:03:23 +00:00
|
|
|
|
2013-09-14 17:32:21 +00:00
|
|
|
if(mTexture && mVertices != NULL)
|
2013-08-23 02:41:40 +00:00
|
|
|
{
|
|
|
|
Renderer::setMatrix(trans);
|
|
|
|
|
|
|
|
mTexture->bind();
|
2019-08-08 20:16:11 +00:00
|
|
|
Renderer::drawTriangleStrips(&mVertices[0], 6*9);
|
2013-08-23 02:41:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
renderChildren(trans);
|
|
|
|
}
|
|
|
|
|
|
|
|
void NinePatchComponent::onSizeChanged()
|
|
|
|
{
|
|
|
|
buildVertices();
|
|
|
|
}
|
|
|
|
|
2018-04-21 13:23:10 +00:00
|
|
|
const Vector2f& NinePatchComponent::getCornerSize() const
|
|
|
|
{
|
|
|
|
return mCornerSize;
|
|
|
|
}
|
|
|
|
|
|
|
|
void NinePatchComponent::setCornerSize(int sizeX, int sizeY)
|
2013-08-23 02:41:40 +00:00
|
|
|
{
|
2018-04-21 13:23:10 +00:00
|
|
|
mCornerSize = Vector2f(sizeX, sizeY);
|
|
|
|
buildVertices();
|
2013-08-23 02:41:40 +00:00
|
|
|
}
|
|
|
|
|
2017-10-28 20:24:35 +00:00
|
|
|
void NinePatchComponent::fitTo(Vector2f size, Vector3f position, Vector2f padding)
|
2013-08-23 02:41:40 +00:00
|
|
|
{
|
2013-09-14 16:14:21 +00:00
|
|
|
size += padding;
|
|
|
|
position[0] -= padding.x() / 2;
|
|
|
|
position[1] -= padding.y() / 2;
|
|
|
|
|
2018-04-21 13:23:10 +00:00
|
|
|
setSize(size + mCornerSize * 2);
|
2018-05-10 21:43:08 +00:00
|
|
|
setPosition(position.x() + Math::lerp(-mCornerSize.x(), mCornerSize.x(), mOrigin.x()),
|
|
|
|
position.y() + Math::lerp(-mCornerSize.y(), mCornerSize.y(), mOrigin.y()));
|
2013-08-23 02:41:40 +00:00
|
|
|
}
|
2013-09-14 15:58:34 +00:00
|
|
|
|
|
|
|
void NinePatchComponent::setImagePath(const std::string& path)
|
|
|
|
{
|
|
|
|
mPath = path;
|
|
|
|
buildVertices();
|
|
|
|
}
|
|
|
|
|
|
|
|
void NinePatchComponent::setEdgeColor(unsigned int edgeColor)
|
|
|
|
{
|
|
|
|
mEdgeColor = edgeColor;
|
|
|
|
updateColors();
|
|
|
|
}
|
|
|
|
|
|
|
|
void NinePatchComponent::setCenterColor(unsigned int centerColor)
|
|
|
|
{
|
|
|
|
mCenterColor = centerColor;
|
|
|
|
updateColors();
|
|
|
|
}
|
2014-01-01 05:39:22 +00:00
|
|
|
|
|
|
|
void NinePatchComponent::applyTheme(const std::shared_ptr<ThemeData>& theme, const std::string& view, const std::string& element, unsigned int properties)
|
|
|
|
{
|
|
|
|
GuiComponent::applyTheme(theme, view, element, properties);
|
|
|
|
|
|
|
|
using namespace ThemeFlags;
|
|
|
|
|
|
|
|
const ThemeData::ThemeElement* elem = theme->getElement(view, element, "ninepatch");
|
|
|
|
if(!elem)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if(properties & PATH && elem->has("path"))
|
|
|
|
setImagePath(elem->get<std::string>("path"));
|
|
|
|
}
|