2020-09-21 17:17:34 +00:00
|
|
|
// SPDX-License-Identifier: MIT
|
2020-06-28 16:39:18 +00:00
|
|
|
//
|
2020-09-21 17:17:34 +00:00
|
|
|
// EmulationStation Desktop Edition
|
2020-06-28 16:39:18 +00:00
|
|
|
// NinePatchComponent.cpp
|
|
|
|
//
|
|
|
|
// Breaks up an image into 3x3 patches to accomodate resizing without distortions.
|
|
|
|
//
|
|
|
|
|
2014-06-20 01:30:09 +00:00
|
|
|
#include "components/NinePatchComponent.h"
|
2017-11-01 22:21:10 +00:00
|
|
|
|
2014-06-20 01:30:09 +00:00
|
|
|
#include "Log.h"
|
|
|
|
#include "ThemeData.h"
|
2021-11-11 18:47:59 +00:00
|
|
|
#include "resources/Font.h"
|
2021-07-07 18:31:46 +00:00
|
|
|
#include "resources/TextureResource.h"
|
2013-08-23 02:41:40 +00:00
|
|
|
|
2023-05-07 20:56:24 +00:00
|
|
|
NinePatchComponent::NinePatchComponent(const std::string& path)
|
2022-03-14 18:51:48 +00:00
|
|
|
: mRenderer {Renderer::getInstance()}
|
2022-01-16 11:09:55 +00:00
|
|
|
, mPath {path}
|
|
|
|
, mCornerSize {16.0f, 16.0f}
|
|
|
|
, mSharpCorners {false}
|
2023-05-07 20:56:24 +00:00
|
|
|
, mFrameColor {mMenuColorFrame}
|
2013-08-23 02:41:40 +00:00
|
|
|
{
|
2020-06-28 16:39:18 +00:00
|
|
|
if (!mPath.empty())
|
|
|
|
buildVertices();
|
2013-08-23 02:41:40 +00:00
|
|
|
}
|
|
|
|
|
2013-09-14 15:58:34 +00:00
|
|
|
void NinePatchComponent::updateColors()
|
|
|
|
{
|
2023-05-07 20:56:24 +00:00
|
|
|
if (mVertices == nullptr)
|
|
|
|
return;
|
2019-08-08 20:16:11 +00:00
|
|
|
|
2023-05-07 20:56:24 +00:00
|
|
|
for (int i {0}; i < 6 * 9; ++i)
|
|
|
|
(*mVertices)[i].color = mFrameColor;
|
2013-09-14 15:58:34 +00:00
|
|
|
}
|
|
|
|
|
2013-08-23 02:41:40 +00:00
|
|
|
void NinePatchComponent::buildVertices()
|
|
|
|
{
|
2021-10-29 17:38:45 +00:00
|
|
|
if (mSize.x == 0.0f || mSize.y == 0.0f)
|
|
|
|
return;
|
|
|
|
|
2023-05-07 20:56:24 +00:00
|
|
|
mVertices.reset();
|
2022-08-23 20:24:24 +00:00
|
|
|
glm::vec2 relCornerSize {0.0f, 0.0f};
|
2021-10-30 11:07:07 +00:00
|
|
|
|
|
|
|
// Don't scale the rasterized version of the frame as it would look bad.
|
|
|
|
if (mPath.substr(mPath.size() - 4, std::string::npos) == ".png") {
|
|
|
|
relCornerSize = mCornerSize;
|
|
|
|
}
|
|
|
|
else {
|
2021-11-11 18:47:59 +00:00
|
|
|
// Scale the corner size relative to the screen resolution (using the medium sized
|
|
|
|
// default font as size reference).
|
2023-02-10 00:00:10 +00:00
|
|
|
relCornerSize = mCornerSize * (Font::get(FONT_SIZE_MEDIUM_FIXED)->getLetterHeight() *
|
2022-10-25 18:39:12 +00:00
|
|
|
(mSharpCorners == true ? 0.0568f : 0.09f) / 2.0f);
|
2021-10-30 11:07:07 +00:00
|
|
|
}
|
2021-07-02 15:36:05 +00:00
|
|
|
|
2022-08-23 20:24:24 +00:00
|
|
|
glm::vec2 texSize {relCornerSize * 3.0f};
|
2022-09-05 21:36:49 +00:00
|
|
|
mTexture = TextureResource::get(mPath, false, false, false, false, false,
|
2022-08-23 20:24:24 +00:00
|
|
|
static_cast<size_t>(texSize.x), static_cast<size_t>(texSize.y));
|
2021-10-29 17:38:45 +00:00
|
|
|
|
|
|
|
mTexture->rasterizeAt(texSize.x, texSize.y);
|
2020-06-28 16:39:18 +00:00
|
|
|
|
2023-05-07 20:56:24 +00:00
|
|
|
if (mTexture->getSize() == glm::ivec2 {0, 0}) {
|
|
|
|
LOG(LogError) << "NinePatchComponent has no texture";
|
2020-06-28 16:39:18 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-05-07 20:56:24 +00:00
|
|
|
mVertices = std::make_unique<std::vector<Renderer::Vertex>>(6 * 9);
|
2020-06-28 16:39:18 +00:00
|
|
|
|
2022-01-16 11:09:55 +00:00
|
|
|
const float imgSizeX[3] {relCornerSize.x, mSize.x - relCornerSize.x * 2.0f, relCornerSize.x};
|
|
|
|
const float imgSizeY[3] {relCornerSize.y, mSize.y - relCornerSize.y * 2.0f, relCornerSize.y};
|
|
|
|
const float imgPosX[3] {0, imgSizeX[0], imgSizeX[0] + imgSizeX[1]};
|
|
|
|
const float imgPosY[3] {0, imgSizeY[0], imgSizeY[0] + imgSizeY[1]};
|
2020-06-28 16:39:18 +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.
|
2021-08-17 18:55:29 +00:00
|
|
|
// clang-format off
|
2022-08-23 20:24:24 +00:00
|
|
|
const float texSizeX[3] {relCornerSize.x / texSize.x, (texSize.x - relCornerSize.x * 2.0f) / texSize.x, relCornerSize.x / texSize.x};
|
|
|
|
const float texSizeY[3] {-relCornerSize.y / texSize.y, -(texSize.y - relCornerSize.y * 2.0f) / texSize.y, -relCornerSize.y / texSize.y};
|
2021-08-17 16:41:45 +00:00
|
|
|
|
2022-08-23 20:24:24 +00:00
|
|
|
const float texPosX[3] {0.0f, texSizeX[0], texSizeX[0] + texSizeX[1]};
|
|
|
|
const float texPosY[3] {1.0f, 1.0f + texSizeY[0], 1.0f + texSizeY[0] + texSizeY[1]};
|
2021-07-07 18:31:46 +00:00
|
|
|
// clang-format on
|
2020-06-28 16:39:18 +00:00
|
|
|
|
2023-05-07 20:56:24 +00:00
|
|
|
int v {0};
|
2020-06-28 16:39:18 +00:00
|
|
|
|
2023-05-07 20:56:24 +00:00
|
|
|
for (int slice {0}; slice < 9; ++slice) {
|
2022-01-16 11:09:55 +00:00
|
|
|
const int sliceX {slice % 3};
|
|
|
|
const int sliceY {slice / 3};
|
|
|
|
const glm::vec2 imgPos {imgPosX[sliceX], imgPosY[sliceY]};
|
|
|
|
const glm::vec2 imgSize {imgSizeX[sliceX], imgSizeY[sliceY]};
|
|
|
|
const glm::vec2 texPos {texPosX[sliceX], texPosY[sliceY]};
|
|
|
|
const glm::vec2 texSizeSlice {texSizeX[sliceX], texSizeY[sliceY]};
|
2020-06-28 16:39:18 +00:00
|
|
|
|
2021-07-07 18:31:46 +00:00
|
|
|
// clang-format off
|
2023-05-07 20:56:24 +00:00
|
|
|
(*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 + texSizeSlice.y}, 0};
|
|
|
|
(*mVertices)[v + 3] = {{imgPos.x + imgSize.x, imgPos.y }, {texPos.x + texSizeSlice.x, texPos.y }, 0};
|
|
|
|
(*mVertices)[v + 4] = {{imgPos.x + imgSize.x, imgPos.y + imgSize.y}, {texPos.x + texSizeSlice.x, texPos.y + texSizeSlice.y}, 0};
|
2021-07-07 18:31:46 +00:00
|
|
|
// clang-format on
|
2020-06-28 16:39:18 +00:00
|
|
|
|
|
|
|
// Round vertices.
|
2023-05-07 20:56:24 +00:00
|
|
|
for (int i {1}; i < 5; ++i)
|
|
|
|
(*mVertices)[v + i].position = glm::round((*mVertices)[v + i].position);
|
2020-06-28 16:39:18 +00:00
|
|
|
|
|
|
|
// Make duplicates of first and last vertex so this can be rendered as a triangle strip.
|
2023-05-07 20:56:24 +00:00
|
|
|
(*mVertices)[v + 0] = (*mVertices)[v + 1];
|
|
|
|
(*mVertices)[v + 5] = (*mVertices)[v + 4];
|
2020-06-28 16:39:18 +00:00
|
|
|
|
|
|
|
v += 6;
|
|
|
|
}
|
|
|
|
|
|
|
|
updateColors();
|
2013-08-23 02:41:40 +00:00
|
|
|
}
|
|
|
|
|
2021-08-15 17:30:31 +00:00
|
|
|
void NinePatchComponent::render(const glm::mat4& parentTrans)
|
2013-08-23 02:41:40 +00:00
|
|
|
{
|
2020-06-28 16:39:18 +00:00
|
|
|
if (!isVisible())
|
|
|
|
return;
|
2019-07-22 03:13:48 +00:00
|
|
|
|
2022-01-16 11:09:55 +00:00
|
|
|
glm::mat4 trans {parentTrans * getTransform()};
|
2019-08-25 15:23:02 +00:00
|
|
|
|
2020-06-28 16:39:18 +00:00
|
|
|
if (mTexture && mVertices != nullptr) {
|
2022-03-14 18:51:48 +00:00
|
|
|
mRenderer->setMatrix(trans);
|
2023-05-07 20:56:24 +00:00
|
|
|
(*mVertices)[0].opacity = mOpacity;
|
|
|
|
(*mVertices)[0].shaderFlags = Renderer::ShaderFlags::PREMULTIPLIED;
|
2020-06-28 16:39:18 +00:00
|
|
|
mTexture->bind();
|
2023-05-07 20:56:24 +00:00
|
|
|
mRenderer->drawTriangleStrips(&mVertices->at(0), 6 * 9);
|
2020-06-28 16:39:18 +00:00
|
|
|
}
|
2013-08-23 02:41:40 +00:00
|
|
|
|
2020-06-28 16:39:18 +00:00
|
|
|
renderChildren(trans);
|
2013-08-23 02:41:40 +00:00
|
|
|
}
|
|
|
|
|
2021-08-16 16:25:01 +00:00
|
|
|
void NinePatchComponent::fitTo(glm::vec2 size, glm::vec3 position, glm::vec2 padding)
|
2013-08-23 02:41:40 +00:00
|
|
|
{
|
2020-06-28 16:39:18 +00:00
|
|
|
size += padding;
|
2021-08-16 16:25:01 +00:00
|
|
|
position[0] -= padding.x / 2.0f;
|
|
|
|
position[1] -= padding.y / 2.0f;
|
2013-09-14 16:14:21 +00:00
|
|
|
|
2021-03-23 21:01:47 +00:00
|
|
|
setSize(size + mCornerSize * 2.0f);
|
2021-08-17 18:55:29 +00:00
|
|
|
setPosition(position.x + glm::mix(-mCornerSize.x, mCornerSize.x, mOrigin.x),
|
|
|
|
position.y + glm::mix(-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)
|
|
|
|
{
|
2020-06-28 16:39:18 +00:00
|
|
|
mPath = path;
|
|
|
|
buildVertices();
|
2013-09-14 15:58:34 +00:00
|
|
|
}
|
|
|
|
|
2023-05-07 20:56:24 +00:00
|
|
|
void NinePatchComponent::setFrameColor(unsigned int frameColor)
|
2013-09-14 15:58:34 +00:00
|
|
|
{
|
2023-05-07 20:56:24 +00:00
|
|
|
mFrameColor = frameColor;
|
2020-06-28 16:39:18 +00:00
|
|
|
updateColors();
|
2013-09-14 15:58:34 +00:00
|
|
|
}
|