start implementing shape tag

This commit is contained in:
2017-02-01 00:59:56 +00:00
parent 39f44eca5e
commit ed02e38805
9 changed files with 120 additions and 6 deletions

View File

@@ -34,7 +34,9 @@
</plane> </plane>
<!-- content panel --> <!-- content panel -->
<plane width="1" grow="1" height="100%" pad="30" wrap="1"> <plane width="1" grow="1" height="100%" pad="30" wrap="1">
<border thickness="5" border-color="1" color=".4" height="100%"></border> <border thickness="5" border-color="1" color=".4" height="100%">
<shape type="round-rect" width="100%" height="100%"></shape>
</border>
</plane> </plane>
</plane> </plane>
<!-- status bar --> <!-- status bar -->

View File

@@ -151,6 +151,7 @@
</ItemDefinitionGroup> </ItemDefinitionGroup>
<ItemGroup> <ItemGroup>
<ClCompile Include="engine\app.cpp" /> <ClCompile Include="engine\app.cpp" />
<ClCompile Include="engine\font.cpp" />
<ClCompile Include="engine\image.cpp" /> <ClCompile Include="engine\image.cpp" />
<ClCompile Include="engine\layout.cpp" /> <ClCompile Include="engine\layout.cpp" />
<ClCompile Include="engine\main.cpp" /> <ClCompile Include="engine\main.cpp" />
@@ -184,6 +185,7 @@
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ClInclude Include="engine\app.h" /> <ClInclude Include="engine\app.h" />
<ClInclude Include="engine\font.h" />
<ClInclude Include="engine\image.h" /> <ClInclude Include="engine\image.h" />
<ClInclude Include="engine\layout.h" /> <ClInclude Include="engine\layout.h" />
<ClInclude Include="engine\pch.h" /> <ClInclude Include="engine\pch.h" />

View File

@@ -48,6 +48,9 @@
<ClCompile Include="engine\layout.cpp"> <ClCompile Include="engine\layout.cpp">
<Filter>Source Files</Filter> <Filter>Source Files</Filter>
</ClCompile> </ClCompile>
<ClCompile Include="engine\font.cpp">
<Filter>Source Files</Filter>
</ClCompile>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ClInclude Include="engine\app.h"> <ClInclude Include="engine\app.h">
@@ -71,5 +74,8 @@
<ClInclude Include="engine\layout.h"> <ClInclude Include="engine\layout.h">
<Filter>Header Files</Filter> <Filter>Header Files</Filter>
</ClInclude> </ClInclude>
<ClInclude Include="engine\font.h">
<Filter>Header Files</Filter>
</ClInclude>
</ItemGroup> </ItemGroup>
</Project> </Project>

View File

@@ -1,3 +1,4 @@
#include "pch.h"
#include "font.h" #include "font.h"

View File

@@ -1,7 +1,6 @@
#include "pch.h" #include "pch.h"
#include "image.h" #include "image.h"
#define STB_IMAGE_IMPLEMENTATION
#include <stb/stb_image.h> #include <stb/stb_image.h>
bool Image::load(std::string filename) bool Image::load(std::string filename)

View File

@@ -182,6 +182,9 @@ void Node::load_internal(const tinyxml2::XMLElement* x_node)
case kWidget::Border: case kWidget::Border:
m_widget = std::make_unique<WidgetBorder>(); m_widget = std::make_unique<WidgetBorder>();
break; break;
case kWidget::Shape:
m_widget = std::make_unique<WidgetShape>();
break;
} }
while (attr) while (attr)
@@ -190,6 +193,9 @@ void Node::load_internal(const tinyxml2::XMLElement* x_node)
attr = attr->Next(); attr = attr->Next();
} }
if (m_widget)
m_widget->create();
auto x_child = x_node->FirstChildElement(); auto x_child = x_node->FirstChildElement();
while (x_child) while (x_child)
{ {

View File

@@ -5,7 +5,8 @@
enum class kAttribute : uint16_t enum class kAttribute : uint16_t
{ {
Width = const_hash("width"), id = const_hash("id"),
Width = const_hash("width"),
MinWidth = const_hash("max-width"), MinWidth = const_hash("max-width"),
MaxWidth = const_hash("min-width"), MaxWidth = const_hash("min-width"),
Height = const_hash("height"), Height = const_hash("height"),
@@ -23,11 +24,22 @@ enum class kAttribute : uint16_t
Color = const_hash("color"), Color = const_hash("color"),
Thickness = const_hash("thickness"), Thickness = const_hash("thickness"),
BorderColor = const_hash("border-color"), BorderColor = const_hash("border-color"),
Type = const_hash("type"),
}; };
enum class kWidget : uint16_t enum class kWidget : uint16_t
{ {
Border = const_hash("border"), Border = const_hash("border"),
Shape = const_hash("shape"),
};
enum class kShapeType : uint16_t
{
Quad = const_hash("rect"),
Poly = const_hash("poly"),
RoundRect = const_hash("round-rect"),
Slice9 = const_hash("slice9"),
}; };
class Widget class Widget
@@ -35,8 +47,9 @@ class Widget
public: public:
glm::mat4 mvp; glm::mat4 mvp;
glm::vec4 clip; glm::vec4 clip;
virtual void draw() { }; virtual void create() { }
virtual void parse_attributes(kAttribute ka, const tinyxml2::XMLAttribute* attr) { }; virtual void draw() { }
virtual void parse_attributes(kAttribute ka, const tinyxml2::XMLAttribute* attr) { }
}; };
class WidgetBorder : public Widget class WidgetBorder : public Widget
@@ -89,6 +102,85 @@ public:
} }
}; };
class WidgetShape : public Widget
{
public:
std::unique_ptr<Shape> m_shape;
kShapeType m_type;
glm::vec4 m_color;
glm::vec4 m_border_color;
float m_thinkness{ 1 };
float m_radius{ .5f };
virtual void create() override
{
switch (m_type)
{
case kShapeType::Quad:
m_shape = std::make_unique<Plane>();
((Plane*)m_shape.get())->create<1>(1, 1);
break;
case kShapeType::Poly:
m_shape = std::make_unique<Circle>();
((Circle*)m_shape.get())->create<5>(m_radius);
break;
case kShapeType::RoundRect:
m_shape = std::make_unique<Rounded>();
((Rounded*)m_shape.get())->create<3>(1, 1, .1f);
break;
case kShapeType::Slice9:
m_shape = std::make_unique<Slice9>();
((Slice9*)m_shape.get())->create(1, 1, .1f, .1f);
break;
default:
break;
}
}
virtual void draw() override
{
ShaderManager::use(kShader::Color);
ShaderManager::u_mat4(kShaderUniform::MVP, mvp);
ShaderManager::u_vec4(kShaderUniform::Col, m_color);
m_shape->draw_fill();
glLineWidth(m_thinkness);
ShaderManager::u_vec4(kShaderUniform::Col, m_border_color);
m_shape->draw_stroke();
}
virtual void parse_attributes(kAttribute id, const tinyxml2::XMLAttribute* attr)
{
switch (id)
{
case kAttribute::Color:
{
glm::vec4 pad;
int n = sscanf(attr->Value(), "%f %f %f %f", &pad.x, &pad.y, &pad.z, &pad.w);
if (n == 1)
m_color = glm::vec4(pad.x);
else
m_color = pad;
break;
}
case kAttribute::BorderColor:
{
glm::vec4 pad;
int n = sscanf(attr->Value(), "%f %f %f %f", &pad.x, &pad.y, &pad.z, &pad.w);
if (n == 1)
m_border_color = glm::vec4(pad.x);
else
m_border_color = pad;
break;
}
case kAttribute::Thickness:
m_thinkness = attr->FloatValue();
break;
case kAttribute::Type:
m_type = (kShapeType)const_hash(attr->Value());
break;
}
}
};
class Node class Node
{ {
const Node* parent{ nullptr }; const Node* parent{ nullptr };

View File

@@ -1 +1,7 @@
#include "pch.h" #include "pch.h"
#define STB_TRUETYPE_IMPLEMENTATION
#include <stb/stb_truetype.h>
#define STB_IMAGE_IMPLEMENTATION
#include <stb/stb_image.h>

View File

@@ -33,4 +33,4 @@
#include <tinyxml2.h> #include <tinyxml2.h>
#include <yoga/Yoga.h> #include <yoga/Yoga.h>
#endif #endif