added image widget with atlas support and global texture manager
This commit is contained in:
@@ -31,6 +31,8 @@ enum class kAttribute : uint16_t
|
||||
FontSize = const_hash("font-size"),
|
||||
Justify = const_hash("justify"),
|
||||
Align = const_hash("align"),
|
||||
Path = const_hash("path"),
|
||||
Region = const_hash("region"),
|
||||
};
|
||||
|
||||
enum class kWidget : uint16_t
|
||||
@@ -38,6 +40,7 @@ enum class kWidget : uint16_t
|
||||
Border = const_hash("border"),
|
||||
Shape = const_hash("shape"),
|
||||
Text = const_hash("text"),
|
||||
Image = const_hash("image"),
|
||||
Ref = const_hash("ref"),
|
||||
};
|
||||
|
||||
@@ -80,6 +83,10 @@ public:
|
||||
glm::vec4 m_color;
|
||||
glm::vec4 m_border_color;
|
||||
float m_thinkness{ 1 };
|
||||
static void init()
|
||||
{
|
||||
m_plane.create<1>(1, 1);
|
||||
}
|
||||
virtual std::unique_ptr<Widget> clone() override
|
||||
{
|
||||
auto ret = std::make_unique<WidgetBorder>();
|
||||
@@ -281,6 +288,82 @@ public:
|
||||
}
|
||||
};
|
||||
|
||||
class WidgetImage : public Widget
|
||||
{
|
||||
public:
|
||||
static Plane m_plane;
|
||||
static Sampler m_sampler;
|
||||
bool m_use_atlas;
|
||||
glm::vec4 m_region;
|
||||
glm::vec2 m_off;
|
||||
glm::vec2 m_sz;
|
||||
std::string m_path;
|
||||
uint16_t m_id;
|
||||
static void init()
|
||||
{
|
||||
m_plane.create<1>(1, 1);
|
||||
m_sampler.create();
|
||||
}
|
||||
virtual std::unique_ptr<Widget> clone() override
|
||||
{
|
||||
auto ret = std::make_unique<WidgetImage>();
|
||||
*ret = *this;
|
||||
return std::move(ret);
|
||||
}
|
||||
virtual void create() override
|
||||
{
|
||||
if (TextureManager::load(m_path.c_str()))
|
||||
{
|
||||
auto tex_sz = TextureManager::get(m_id).size();
|
||||
m_off = m_region.xy / tex_sz;
|
||||
m_sz = (m_region.zw - m_region.xy) / tex_sz;
|
||||
}
|
||||
}
|
||||
virtual void draw() override
|
||||
{
|
||||
TextureManager::get(m_id).bind();
|
||||
m_sampler.bind(0);
|
||||
if (m_use_atlas)
|
||||
{
|
||||
ShaderManager::use(kShader::Atlas);
|
||||
ShaderManager::u_vec2(kShaderUniform::Tof, m_off);
|
||||
ShaderManager::u_vec2(kShaderUniform::Tsz, m_sz);
|
||||
}
|
||||
else
|
||||
{
|
||||
ShaderManager::use(kShader::Texture);
|
||||
}
|
||||
ShaderManager::u_int(kShaderUniform::Tex, 0);
|
||||
ShaderManager::u_mat4(kShaderUniform::MVP, mvp);
|
||||
m_plane.draw_fill();
|
||||
m_sampler.unbind();
|
||||
TextureManager::get(m_id).unbind();
|
||||
}
|
||||
virtual void parse_attributes(kAttribute id, const tinyxml2::XMLAttribute* attr) override
|
||||
{
|
||||
switch (id)
|
||||
{
|
||||
case kAttribute::Path:
|
||||
m_path = attr->Value();
|
||||
m_id = const_hash(attr->Value());
|
||||
break;
|
||||
case kAttribute::Region:
|
||||
{
|
||||
glm::vec4 v;
|
||||
int n = sscanf(attr->Value(), "%f %f %f %f", &v.x, &v.y, &v.z, &v.w);
|
||||
if (n == 4)
|
||||
{
|
||||
m_region = v;
|
||||
m_use_atlas = true;
|
||||
}
|
||||
break;
|
||||
}
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
class Node
|
||||
{
|
||||
friend class LayoutManager;
|
||||
|
||||
Reference in New Issue
Block a user