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.h
|
|
|
|
//
|
|
|
|
// Breaks up an image into 3x3 patches to accomodate resizing without distortions.
|
|
|
|
//
|
|
|
|
|
2017-10-31 17:12:50 +00:00
|
|
|
#ifndef ES_CORE_COMPONENTS_NINE_PATCH_COMPONENT_H
|
|
|
|
#define ES_CORE_COMPONENTS_NINE_PATCH_COMPONENT_H
|
2013-08-23 02:41:40 +00:00
|
|
|
|
2014-06-20 01:30:09 +00:00
|
|
|
#include "GuiComponent.h"
|
2021-07-07 18:31:46 +00:00
|
|
|
#include "renderers/Renderer.h"
|
2017-11-01 22:21:10 +00:00
|
|
|
|
|
|
|
class TextureResource;
|
2013-08-23 02:41:40 +00:00
|
|
|
|
2020-06-28 16:39:18 +00:00
|
|
|
// Display an image in a way so that edges don't get too distorted no matter the final size.
|
|
|
|
// Useful for UI elements like backgrounds, buttons, etc.
|
2014-01-24 22:21:10 +00:00
|
|
|
// This is accomplished by splitting an image into 9 pieces:
|
|
|
|
// ___________
|
|
|
|
// |_1_|_2_|_3_|
|
|
|
|
// |_4_|_5_|_6_|
|
|
|
|
// |_7_|_8_|_9_|
|
2020-06-28 16:39:18 +00:00
|
|
|
//
|
2014-01-24 22:21:10 +00:00
|
|
|
// Corners (1, 3, 7, 9) will not be stretched at all.
|
|
|
|
// Borders (2, 4, 6, 8) will be stretched along one axis (2 and 8 horizontally, 4 and 6 vertically).
|
|
|
|
// The center (5) will be stretched.
|
|
|
|
|
2013-08-23 02:41:40 +00:00
|
|
|
class NinePatchComponent : public GuiComponent
|
|
|
|
{
|
|
|
|
public:
|
2021-07-07 18:31:46 +00:00
|
|
|
NinePatchComponent(Window* window,
|
|
|
|
const std::string& path = "",
|
|
|
|
unsigned int edgeColor = 0xFFFFFFFF,
|
|
|
|
unsigned int centerColor = 0xFFFFFFFF);
|
2020-06-28 16:39:18 +00:00
|
|
|
virtual ~NinePatchComponent();
|
2013-08-23 02:41:40 +00:00
|
|
|
|
2021-08-15 17:30:31 +00:00
|
|
|
void render(const glm::mat4& parentTrans) override;
|
2013-08-23 02:41:40 +00:00
|
|
|
|
2021-08-17 16:41:45 +00:00
|
|
|
void onSizeChanged() override { buildVertices(); }
|
2021-08-16 16:25:01 +00:00
|
|
|
void 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
|
|
|
void setImagePath(const std::string& path);
|
|
|
|
// Apply a color shift to the "edge" parts of the ninepatch.
|
|
|
|
void setEdgeColor(unsigned int edgeColor);
|
|
|
|
// Apply a color shift to the "center" part of the ninepatch.
|
|
|
|
void setCenterColor(unsigned int centerColor);
|
2013-09-14 15:58:34 +00:00
|
|
|
|
2021-07-07 18:31:46 +00:00
|
|
|
virtual void applyTheme(const std::shared_ptr<ThemeData>& theme,
|
|
|
|
const std::string& view,
|
|
|
|
const std::string& element,
|
|
|
|
unsigned int properties) override;
|
2014-01-01 05:39:22 +00:00
|
|
|
|
2021-08-16 16:25:01 +00:00
|
|
|
const glm::vec2& getCornerSize() const { return mCornerSize; }
|
|
|
|
void setCornerSize(const glm::vec2& size)
|
2020-12-29 11:54:24 +00:00
|
|
|
{
|
|
|
|
mCornerSize = size;
|
|
|
|
buildVertices();
|
2021-07-07 18:31:46 +00:00
|
|
|
}
|
2013-08-23 02:41:40 +00:00
|
|
|
|
2018-04-21 13:23:10 +00:00
|
|
|
private:
|
2020-06-28 16:39:18 +00:00
|
|
|
void buildVertices();
|
|
|
|
void updateColors();
|
2013-08-23 02:41:40 +00:00
|
|
|
|
2020-06-28 16:39:18 +00:00
|
|
|
Renderer::Vertex* mVertices;
|
2013-08-23 02:41:40 +00:00
|
|
|
|
2020-06-28 16:39:18 +00:00
|
|
|
std::string mPath;
|
2021-08-16 16:25:01 +00:00
|
|
|
glm::vec2 mCornerSize;
|
2020-06-28 16:39:18 +00:00
|
|
|
unsigned int mEdgeColor;
|
|
|
|
unsigned int mCenterColor;
|
|
|
|
std::shared_ptr<TextureResource> mTexture;
|
2013-08-23 02:41:40 +00:00
|
|
|
};
|
2017-10-31 17:12:50 +00:00
|
|
|
|
|
|
|
#endif // ES_CORE_COMPONENTS_NINE_PATCH_COMPONENT_H
|