refactor layout loading to add multiple layout, templates and references

This commit is contained in:
2017-02-01 20:35:57 +00:00
parent ed02e38805
commit 70792669e7
5 changed files with 97 additions and 51 deletions

View File

@@ -21,7 +21,26 @@ void Node::update_internal(const glm::vec2& origin, const glm::mat4& proj)
float h = YGNodeLayoutGetHeight(y_node);
m_pos = origin + glm::vec2(x, y);
m_size = glm::vec2(w, h);
m_clip = !parent ? glm::vec4(m_pos, m_size) : rect_intersection(glm::vec4(m_pos, m_size), parent->m_clip);
if (parent)
{
// correct the padding clip
// should not clip the padded area
// useful to draw decorations
float pt = YGNodeLayoutGetPadding(parent->y_node, YGEdgeTop);
float pr = YGNodeLayoutGetPadding(parent->y_node, YGEdgeRight);
float pb = YGNodeLayoutGetPadding(parent->y_node, YGEdgeBottom);
float pl = YGNodeLayoutGetPadding(parent->y_node, YGEdgeLeft);
glm::vec2 off_p(pl, pt);
glm::vec2 off_s(pr, pb);
m_clip = glm::vec4(m_pos - off_p, m_size + off_p + off_s);
m_clip = rect_intersection(m_clip, parent->m_clip);
}
else
{
m_clip = glm::vec4(m_pos, m_size);
}
if (m_widget)
{
glm::mat4 pivot = glm::translate(glm::vec3(.5f, .5f, 0.f));
@@ -151,26 +170,6 @@ void Node::parse_attributes(kAttribute ka, const tinyxml2::XMLAttribute* attr)
}
}
void Node::load(const char* path)
{
struct stat tmp_info;
if (stat(path, &tmp_info) != 0)
return;
if (tmp_info.st_mtime <= m_file_info.st_mtime)
return;
m_file_info = tmp_info;
m_path = path;
m_children.clear();
YGNodeReset(y_node);
tinyxml2::XMLDocument xml;
auto ret = xml.LoadFile(path);
if (ret != tinyxml2::XMLError::XML_SUCCESS)
return;
load_internal(xml.RootElement());
}
void Node::load_internal(const tinyxml2::XMLElement* x_node)
{
m_name = x_node->Name();
@@ -209,12 +208,53 @@ void Node::load_internal(const tinyxml2::XMLElement* x_node)
}
}
void Node::reload()
bool LayoutManager::load(const char* path)
{
float w = YGNodeLayoutGetWidth(y_node);
float h = YGNodeLayoutGetHeight(y_node);
// avoid conflict when assigning the same string from c_str
std::string path_copy = m_path;
load(path_copy.c_str());
update(w, h);
struct stat tmp_info;
if (stat(path, &tmp_info) != 0)
return false;
if (tmp_info.st_mtime <= m_file_info.st_mtime)
return false;
m_file_info = tmp_info;
m_path = path;
m_layouts.clear();
tinyxml2::XMLDocument xml;
auto ret = xml.LoadFile(path);
if (ret != tinyxml2::XMLError::XML_SUCCESS)
return false;
tinyxml2::XMLElement* current = xml.RootElement();
while (current)
{
auto id_str = current->Attribute("id");
if (!id_str)
{
printf("Layout node without id\n");
return false;
}
uint16_t id = const_hash(id_str);
auto p = m_layouts.try_emplace(id);
if (p.second)
{
auto& node = p.first->second;
node.load_internal(current);
}
else
{
printf("Layout id \"%s\" duplicated\n", id_str);
}
current = current->NextSiblingElement("layout");
}
}
void LayoutManager::reload()
{
// float w = YGNodeLayoutGetWidth(y_node);
// float h = YGNodeLayoutGetHeight(y_node);
// // avoid conflict when assigning the same string from c_str
// std::string path_copy = m_path;
// load(path_copy.c_str());
// update(w, h);
}