diff --git a/Makefile b/Makefile index 6c6b11f..60be31a 100644 --- a/Makefile +++ b/Makefile @@ -38,7 +38,7 @@ debug: build gdb --nx -x .gdbinit --ex run --args builddir/bezos_tests debug_run: build - gdb --nx -x .gdbinit --batch --ex run --ex bt --ex q --args builddir/bezos + gdb --nx -x .gdbinit --batch --ex run --ex bt --ex q --args builddir/bezos -d lua/builder_test.lua clean: meson compile --clean -C builddir diff --git a/assets/layouts.json b/assets/layouts.json new file mode 100644 index 0000000..9a1b76d --- /dev/null +++ b/assets/layouts.json @@ -0,0 +1,45 @@ +{ + "default_ui": [ + "[*%(100,700)slide]", + "[_]", + "[_]", + "[_]", + "[_]", + "[_]", + "[_]", + "[=title|=*%(200,100)description|_|_|_]" + ], + "default_slide": [ + "[*%(300,200)title|_|_|_|_]", + "[_|_|_]", + "[*%(300,600)content|_|_|*%(200,600)image|_]", + "[_|_|_|_|_]", + "[_|_|_|_|_]", + "[_|_|_|_|_]", + "[_|_|_|_|_]", + "[_|_|_|_|_]" + ], + "image_left": [ + "[=title]", + "[_|_|_]", + "[*%(200,600)image|_|*%(300,600)content|_|_]", + "[_|_|_|_|_]", + "[_|_|_|_|_]", + "[_|_|_|_|_]", + "[_|_|_|_|_]", + "[_|_|_|_|_]" + ], + "no_title": [ + "[*%(300,600)content]" + ], + "centered": [ + "[=*%(300,400)title|_|_|_|_]", + "[_|_|_]", + "[_|_|_]", + "[_|_|_]", + "[=*%(300,400)content]", + "[_|_|_]", + "[_|_|_]", + "[_|_|_]" + ] +} diff --git a/lua/builder_test.lua b/lua/builder_test.lua index 743a5a7..79a1259 100644 --- a/lua/builder_test.lua +++ b/lua/builder_test.lua @@ -11,7 +11,7 @@ slide{ image{ src="assets/test.jpg" }, - h1 "Image Before Test", + h1 "Image Before café", } slide{ diff --git a/src/bezos.cpp b/src/bezos.cpp index c32d593..86bde55 100644 --- a/src/bezos.cpp +++ b/src/bezos.cpp @@ -1,4 +1,5 @@ #include "builder.hpp" +#include namespace Bezos { Deck load(const std::string& file) { @@ -50,7 +51,7 @@ namespace Bezos { for(auto& slide : deck.slides) { fmt::println("-----"); for(auto& node : slide.content) { - fmt::println("node: {},{}", (int)node.type, node.content); + fmt::println(L"node: {},{}", (int)node.type, node.content); } } } diff --git a/src/builder.cpp b/src/builder.cpp index 6124cff..7768942 100644 --- a/src/builder.cpp +++ b/src/builder.cpp @@ -1,7 +1,7 @@ #include "builder.hpp" void Builder::config_set(const std::string& key, const sol::object& obj) { - deck.config.try_emplace(key, to_any(obj)); + deck.config.try_emplace(key, to_any(obj, true)); } size_t Builder::slide_start() { @@ -15,7 +15,7 @@ size_t Builder::node_add(size_t slide_id, NodeType nt, const sol::object& obj) { if(obj.is()) { node.type = nt; - node.content = obj.as(); + node.content = obj.as(); } else if(obj.is()) { node.type = nt; const auto& as_table = obj.as(); @@ -23,9 +23,9 @@ size_t Builder::node_add(size_t slide_id, NodeType nt, const sol::object& obj) { // configured node content for(const auto& [k, v] : as_table) { if(k.is()) { - node.config.try_emplace(k.as(), to_any(v)); + node.config.try_emplace(k.as(), to_any(v, true)); } else if(k.is()) { - node.content += v.as(); + node.content += v.as(); } else { dbc::sentinel("Invalid key type for node."); } @@ -36,7 +36,7 @@ size_t Builder::node_add(size_t slide_id, NodeType nt, const sol::object& obj) { // some nodes won't have content (images) but if it's ERROR then add error message if(node.type == NodeType::ERROR) { - node.content = "ERROR: This node not configured properly."; + node.content = L"ERROR: This node not configured properly."; } slide.content.push_back(node); @@ -44,13 +44,18 @@ size_t Builder::node_add(size_t slide_id, NodeType nt, const sol::object& obj) { return slide.content.size() - 1; } -std::any Builder::to_any(const sol::object& value) { +std::any Builder::to_any(const sol::object& value, bool is_keyval) { if(value.is()) { return value.as(); } else if(value.is()) { return value.as(); } else if(value.is()) { - return value.as(); + // BUG: this may not be necessary, or key=value maybe should be unicode values? + if(is_keyval) { + return value.as(); + } else { + return value.as(); + } } else if(value.is()) { return value.as(); } else if(value.is()) { @@ -63,5 +68,5 @@ std::any Builder::to_any(const sol::object& value) { void Builder::slide_config(size_t slide_id, const std::string& key, const sol::object& value) { auto& slide = deck.slides.at(slide_id); - slide.config.try_emplace(key, to_any(value)); + slide.config.try_emplace(key, to_any(value, true)); } diff --git a/src/builder.hpp b/src/builder.hpp index 37596ff..3291a4d 100644 --- a/src/builder.hpp +++ b/src/builder.hpp @@ -14,7 +14,7 @@ struct Builder { size_t node_add(size_t slide_id, NodeType nt, const sol::object& obj); - std::any to_any(const sol::object& value); + std::any to_any(const sol::object& value, bool is_keyval); void slide_config(size_t slide_id, const std::string& key, const sol::object& value); }; diff --git a/src/document.hpp b/src/document.hpp index 79f406a..ddfe777 100644 --- a/src/document.hpp +++ b/src/document.hpp @@ -24,7 +24,7 @@ struct Color { struct Node { NodeType type = NodeType::ERROR; - std::string content = ""; + std::wstring content = L""; std::unordered_map config{}; std::shared_ptr image = nullptr; }; diff --git a/src/engine.cpp b/src/engine.cpp index fd4d8ce..da2499e 100644 --- a/src/engine.cpp +++ b/src/engine.cpp @@ -16,6 +16,7 @@ void framebuffer_size_callback(GLFWwindow *, int width, int height) { void mouse_callback(GLFWwindow *window, double xpos, double ypos) { Engine* engine = static_cast(glfwGetWindowUserPointer(window)); engine->mouse(xpos, ypos); + engine->ui.mouse(xpos, ypos, guecs::NO_MODS); } void key_callback(GLFWwindow* window, int key, int scancode, int action, int mods) @@ -77,6 +78,38 @@ void setup() { glDepthFunc(GL_LESS); } +void Engine::init_ui() { + ui.position(0, 0, WINDOW_WIDTH, WINDOW_HEIGHT); + ui.layout( + "[*%(100,700)slide]" + "[_]" + "[_]" + "[_]" + "[_]" + "[_]" + "[_]" + "[=title|=*%(200,100)description|_|_|_]"); + ui.init(); + + auto click = ui.entity("slide"); + ui.set(click, { + [](auto mods){ + fmt::println("HOVER on slide!"); + } + }); + + auto title = ui.entity("title"); + ui.set(title, {L"The Text Test!" }); + ui.set(title, { + [&](auto mods){ + auto& at_title = ui.get(ui.entity("title")); + fmt::println("HOVER on title at x={}, y={}", at_title.position.x, at_title.position.y); + } + }); + + ui.init(); +} + void Engine::init() { init_glfw(); window = create_window(); @@ -94,6 +127,8 @@ void Engine::init() { framebuffer->init(); glfwSetWindowUserPointer(window, this); + + init_ui(); } void Engine::render() { @@ -110,7 +145,7 @@ void Engine::render() { float y = (text->font_size + 20) * line; if(node.type == NodeType::TEXT || node.type == NodeType::H1 || node.type == NodeType::UL) { - text->render(node.content, 25.0f, WINDOW_HEIGHT - y, 1.0f, glm::vec3(1.0f, 1.0f, 1.0f)); + text->render(node.content, 25.0f, y, 1.0f, glm::vec3(1.0f, 1.0f, 1.0f)); } else if(node.type == NodeType::IMAGE) { dbc::check(node.image != nullptr, "Node's image not configured."); node.image->render(); @@ -119,6 +154,8 @@ void Engine::render() { } } + ui.render(); + framebuffer->commit(); framebuffer->draw(*screen); @@ -132,6 +169,7 @@ void Engine::change_slide(int dir) { } void Engine::update() { + ui.update(); glfwPollEvents(); } diff --git a/src/engine.hpp b/src/engine.hpp index d16f5e1..8dc93b6 100644 --- a/src/engine.hpp +++ b/src/engine.hpp @@ -5,9 +5,11 @@ #include "document.hpp" #include #include +#include "guecs/ui.hpp" struct Engine { Deck& deck; + guecs::UI ui{}; std::shared_ptr framebuffer = nullptr; std::shared_ptr screen = nullptr; std::shared_ptr text = nullptr; @@ -28,4 +30,5 @@ struct Engine { void quit(); void init(); bool active(); + void init_ui(); }; diff --git a/src/guecs/opengl/components.cpp b/src/guecs/opengl/components.cpp index 3c88540..886d31c 100644 --- a/src/guecs/opengl/components.cpp +++ b/src/guecs/opengl/components.cpp @@ -13,21 +13,30 @@ namespace guecs { } void Text::init(lel::Cell &cell, const std::string& font_file) { + assert(text == nullptr && "Double init on text."); + // make the text here + text = make_shared<::Text>(); + text->font_size = size; + text->load_font(); if(centered || cell.center) { - // get the text bounds and use lel::center - // auto bounds = text->getLocalBounds(); - // auto text_cell = lel::center(bounds.size.x, bounds.size.y, cell); + // WRONG! Need the scale here... + auto& ch = text->characters.at('H'); + + auto text_cell = lel::center(ch.size.x, ch.size.y, cell); + // this stupid / 2 is because SFML renders from baseline rather than from the claimed bounding box - // text->setPosition({float(text_cell.x), float(text_cell.y) - text_cell.h / 2}); + position = {float(text_cell.x), float(text_cell.y) - text_cell.h / 2}; } else { - // CHANGE ME - // text->setPosition({float(cell.x + padding * 2), float(cell.y + padding * 2)}); + position = {float(cell.x + padding * 2), float(cell.y + padding * 2)}; } } void Text::render() { + if(text != nullptr) { + text->render(content, position.x, position.y, 1.0f, {1.0f, 1.0f, 1.0f}); + } } void Text::update(const wstring& new_content) { diff --git a/src/guecs/opengl/components.hpp b/src/guecs/opengl/components.hpp index fcb0c1d..5d3e30b 100644 --- a/src/guecs/opengl/components.hpp +++ b/src/guecs/opengl/components.hpp @@ -18,7 +18,8 @@ namespace guecs { Color color = THEME.TEXT_COLOR; int padding = THEME.PADDING; bool centered = false; - shared_ptr text = nullptr; + shared_ptr<::Text> text = nullptr; + glm::vec2 position{0.0, 0.0}; void init(lel::Cell &cell, const std::string& font_file); void update(const std::wstring& new_content); diff --git a/src/options.cpp b/src/options.cpp index 0f698ee..9ba3b26 100644 --- a/src/options.cpp +++ b/src/options.cpp @@ -42,9 +42,8 @@ Options parse_options(int argc, char* argv[]) { } if(!result.deck_given) { - result.deck_file = "builder_test.lua"; - // print_usage(); - // result.error = true; + print_usage(); + result.error = true; } return result; diff --git a/src/text.cpp b/src/text.cpp index 71595fa..00727f4 100644 --- a/src/text.cpp +++ b/src/text.cpp @@ -8,9 +8,12 @@ #include "config.hpp" #include FT_FREETYPE_H -void Text::render(const std::string& text, float x, float y, float scale, glm::vec3 color) { +void Text::render(const std::wstring& text, float x, float y, float scale, glm::vec3 color) { text_shader.use(); + // BUG: find a better way to adapt the y coord + y = WINDOW_HEIGHT - y; + // BUG: might be better as a getter/setter so it only needs to run once glUniform3f(glGetUniformLocation(text_shader.ID, "textColor"), color.x, color.y, color.z); glActiveTexture(GL_TEXTURE0 + texture_unit); diff --git a/src/text.hpp b/src/text.hpp index 0309049..8f76c1c 100644 --- a/src/text.hpp +++ b/src/text.hpp @@ -20,7 +20,7 @@ struct Text { std::unordered_map characters{}; unsigned int texture_unit = 5; - void render(const std::string& text, float x, float y, float scale, glm::vec3 color); + void render(const std::wstring& text, float x, float y, float scale, glm::vec3 color); void load_font(); Text(); };