2013-08-23 02:41:40 +00:00
|
|
|
#pragma once
|
|
|
|
|
2014-06-20 01:30:09 +00:00
|
|
|
#include "GuiComponent.h"
|
|
|
|
#include "resources/TextureResource.h"
|
2013-08-23 02:41:40 +00:00
|
|
|
|
2014-01-24 22:21:10 +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.
|
|
|
|
// This is accomplished by splitting an image into 9 pieces:
|
|
|
|
// ___________
|
|
|
|
// |_1_|_2_|_3_|
|
|
|
|
// |_4_|_5_|_6_|
|
|
|
|
// |_7_|_8_|_9_|
|
|
|
|
|
|
|
|
// 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:
|
2013-11-23 20:04:11 +00:00
|
|
|
NinePatchComponent(Window* window, const std::string& path = "", unsigned int edgeColor = 0xFFFFFFFF, unsigned int centerColor = 0xFFFFFFFF);
|
2013-08-23 02:41:40 +00:00
|
|
|
|
|
|
|
void render(const Eigen::Affine3f& parentTrans) override;
|
|
|
|
|
|
|
|
void onSizeChanged() override;
|
|
|
|
|
2013-09-14 16:14:21 +00:00
|
|
|
void fitTo(Eigen::Vector2f size, Eigen::Vector3f position = Eigen::Vector3f::Zero(), Eigen::Vector2f padding = Eigen::Vector2f::Zero());
|
2013-08-23 02:41:40 +00:00
|
|
|
|
2013-09-14 15:58:34 +00:00
|
|
|
void setImagePath(const std::string& path);
|
2014-01-24 22:21:10 +00:00
|
|
|
void setEdgeColor(unsigned int edgeColor); // Apply a color shift to the "edge" parts of the ninepatch.
|
|
|
|
void setCenterColor(unsigned int centerColor); // Apply a color shift to the "center" part of the ninepatch.
|
2013-09-14 15:58:34 +00:00
|
|
|
|
2014-01-01 05:39:22 +00:00
|
|
|
virtual void applyTheme(const std::shared_ptr<ThemeData>& theme, const std::string& view, const std::string& element, unsigned int properties) override;
|
|
|
|
|
2013-08-23 02:41:40 +00:00
|
|
|
private:
|
|
|
|
Eigen::Vector2f getCornerSize() const;
|
|
|
|
|
|
|
|
void buildVertices();
|
2013-09-14 15:58:34 +00:00
|
|
|
void updateColors();
|
2013-08-23 02:41:40 +00:00
|
|
|
|
|
|
|
struct Vertex
|
|
|
|
{
|
|
|
|
Eigen::Vector2f pos;
|
|
|
|
Eigen::Vector2f tex;
|
|
|
|
};
|
|
|
|
|
|
|
|
Vertex* mVertices;
|
|
|
|
GLubyte* mColors;
|
|
|
|
|
2013-09-14 15:58:34 +00:00
|
|
|
std::string mPath;
|
2013-08-23 02:41:40 +00:00
|
|
|
unsigned int mEdgeColor;
|
|
|
|
unsigned int mCenterColor;
|
|
|
|
std::shared_ptr<TextureResource> mTexture;
|
|
|
|
};
|