diff --git a/data/layout.xml b/data/layout.xml index b21d197..2dc0550 100644 --- a/data/layout.xml +++ b/data/layout.xml @@ -1,7 +1,4 @@ - - - diff --git a/engine/app.cpp b/engine/app.cpp index ffe7f5f..13d6e75 100644 --- a/engine/app.cpp +++ b/engine/app.cpp @@ -5,7 +5,7 @@ App App::I; // singleton void App::create() { - width = 500; + width = 800; height = 500; } @@ -75,7 +75,7 @@ void App::init() #endif layout.load("data/layout.xml"); - //layout.update(width, height); + layout["main"].update(width, height); sampler.create(); ShaderManager::create(kShader::Texture, shader_v, shader_f); @@ -109,11 +109,16 @@ void App::init() void App::update(float dt) { + constexpr auto main_id = const_hash("main"); + glClearColor(.1f, .1f, .1f, 1.f); glViewport(0, 0, (GLsizei)width, (GLsizei)height); glClear(GL_COLOR_BUFFER_BIT); - layout.reload(); + if (layout.reload()) + { + layout[main_id].update(width, height); + } glActiveTexture(GL_TEXTURE0); tex.bind(); @@ -122,17 +127,16 @@ void App::update(float dt) ShaderManager::u_int(kShaderUniform::Tex, 0); glPolygonMode(GL_FRONT_AND_BACK, GL_FILL); - glEnable(GL_SCISSOR_TEST); -// for (auto& n : layout) -// { -// -// if (n.m_widget) -// { -// auto box = n.m_widget->clip; -// glScissor(box.x, height - box.y - box.w, box.z, box.w); -// n.m_widget->draw(); -// } -// } + //glEnable(GL_SCISSOR_TEST); + for (auto& n : layout[main_id]) + { + if (n.m_widget) + { + auto box = n.m_widget->clip; + glScissor(box.x, height - box.y - box.w, box.z, box.w); + n.m_widget->draw(); + } + } glDisable(GL_SCISSOR_TEST); tex.unbind(); sampler.unbind(); @@ -140,9 +144,8 @@ void App::update(float dt) void App::resize(float w, float h) { - width = w; - height = h; -// layout.update(width, height); + constexpr auto main_id = const_hash("main"); + layout[main_id].update(w, h); } void App::mouse_down(int button, float x, float y) diff --git a/engine/layout.cpp b/engine/layout.cpp index 62d1e73..113deb9 100644 --- a/engine/layout.cpp +++ b/engine/layout.cpp @@ -9,7 +9,7 @@ void Node::update(float width, float height) YGNodeStyleSetWidth(y_node, width); YGNodeStyleSetHeight(y_node, height); YGNodeCalculateLayout(y_node, YGUndefined, YGUndefined, YGDirectionLTR); - glm::mat4 proj = glm::ortho(0.f, m_size.x, m_size.y, 0.f, -1.f, 1.f); + glm::mat4 proj = glm::ortho(0.f, width, height, 0.f, -1.f, 1.f); update_internal({ 0, 0 }, proj); } @@ -218,7 +218,7 @@ bool LayoutManager::load(const char* path) m_file_info = tmp_info; m_path = path; - m_layouts.clear(); + auto old = std::move(m_layouts); tinyxml2::XMLDocument xml; auto ret = xml.LoadFile(path); @@ -235,11 +235,18 @@ bool LayoutManager::load(const char* path) return false; } uint16_t id = const_hash(id_str); - auto p = m_layouts.try_emplace(id); + auto& p = m_layouts.try_emplace(id); if (p.second) { auto& node = p.first->second; - node.load_internal(current); + // try to copy the old size values + if (old.count(id)) + { + const auto& old_node = old[id]; + YGNodeCopyStyle(node.y_node, old_node.y_node); + } + if (!current->NoChildren()) + node.load_internal(current->FirstChildElement()); } else { @@ -247,14 +254,13 @@ bool LayoutManager::load(const char* path) } current = current->NextSiblingElement("layout"); } + + return true; } -void LayoutManager::reload() +bool 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); + // avoid conflict when assigning the same string from c_str + std::string path_copy = m_path; + return load(path_copy.c_str()); } diff --git a/engine/layout.h b/engine/layout.h index 0c76c13..b87cb17 100644 --- a/engine/layout.h +++ b/engine/layout.h @@ -182,6 +182,8 @@ public: class Node { + friend class LayoutManager; + const Node* parent{ nullptr }; YGNodeRef y_node{ nullptr }; @@ -293,5 +295,7 @@ class LayoutManager struct stat m_file_info { 0 }; public: bool load(const char* path); - void reload(); + bool reload(); + Node& operator[](uint16_t id) { return m_layouts[id]; } + Node& operator[](const char* ids) { return m_layouts[const_hash(ids)]; } };