The UI system can now layout a grid and put text int it with clickable regions.

master
Zed A. Shaw 2 weeks ago
parent 055ba6dfd5
commit d79d891691
  1. 2
      Makefile
  2. 45
      assets/layouts.json
  3. 2
      lua/builder_test.lua
  4. 3
      src/bezos.cpp
  5. 21
      src/builder.cpp
  6. 2
      src/builder.hpp
  7. 2
      src/document.hpp
  8. 40
      src/engine.cpp
  9. 3
      src/engine.hpp
  10. 21
      src/guecs/opengl/components.cpp
  11. 3
      src/guecs/opengl/components.hpp
  12. 5
      src/options.cpp
  13. 5
      src/text.cpp
  14. 2
      src/text.hpp

@ -38,7 +38,7 @@ debug: build
gdb --nx -x .gdbinit --ex run --args builddir/bezos_tests gdb --nx -x .gdbinit --ex run --args builddir/bezos_tests
debug_run: build 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: clean:
meson compile --clean -C builddir meson compile --clean -C builddir

@ -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]",
"[_|_|_]",
"[_|_|_]",
"[_|_|_]"
]
}

@ -11,7 +11,7 @@ slide{
image{ image{
src="assets/test.jpg" src="assets/test.jpg"
}, },
h1 "Image Before Test", h1 "Image Before café",
} }
slide{ slide{

@ -1,4 +1,5 @@
#include "builder.hpp" #include "builder.hpp"
#include <fmt/xchar.h>
namespace Bezos { namespace Bezos {
Deck load(const std::string& file) { Deck load(const std::string& file) {
@ -50,7 +51,7 @@ namespace Bezos {
for(auto& slide : deck.slides) { for(auto& slide : deck.slides) {
fmt::println("-----"); fmt::println("-----");
for(auto& node : slide.content) { for(auto& node : slide.content) {
fmt::println("node: {},{}", (int)node.type, node.content); fmt::println(L"node: {},{}", (int)node.type, node.content);
} }
} }
} }

@ -1,7 +1,7 @@
#include "builder.hpp" #include "builder.hpp"
void Builder::config_set(const std::string& key, const sol::object& obj) { 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() { 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<std::string>()) { if(obj.is<std::string>()) {
node.type = nt; node.type = nt;
node.content = obj.as<std::string>(); node.content = obj.as<std::wstring>();
} else if(obj.is<sol::table>()) { } else if(obj.is<sol::table>()) {
node.type = nt; node.type = nt;
const auto& as_table = obj.as<sol::table>(); const auto& as_table = obj.as<sol::table>();
@ -23,9 +23,9 @@ size_t Builder::node_add(size_t slide_id, NodeType nt, const sol::object& obj) {
// configured node content // configured node content
for(const auto& [k, v] : as_table) { for(const auto& [k, v] : as_table) {
if(k.is<std::string>()) { if(k.is<std::string>()) {
node.config.try_emplace(k.as<std::string>(), to_any(v)); node.config.try_emplace(k.as<std::string>(), to_any(v, true));
} else if(k.is<int>()) { } else if(k.is<int>()) {
node.content += v.as<std::string>(); node.content += v.as<std::wstring>();
} else { } else {
dbc::sentinel("Invalid key type for node."); 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 // some nodes won't have content (images) but if it's ERROR then add error message
if(node.type == NodeType::ERROR) { 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); 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; 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<int>()) { if(value.is<int>()) {
return value.as<int>(); return value.as<int>();
} else if(value.is<float>()) { } else if(value.is<float>()) {
return value.as<float>(); return value.as<float>();
} else if(value.is<std::string>()) { } else if(value.is<std::string>()) {
return value.as<std::string>(); // BUG: this may not be necessary, or key=value maybe should be unicode values?
if(is_keyval) {
return value.as<std::string>();
} else {
return value.as<std::wstring>();
}
} else if(value.is<Color>()) { } else if(value.is<Color>()) {
return value.as<Color>(); return value.as<Color>();
} else if(value.is<sol::table>()) { } else if(value.is<sol::table>()) {
@ -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) { void Builder::slide_config(size_t slide_id, const std::string& key, const sol::object& value) {
auto& slide = deck.slides.at(slide_id); auto& slide = deck.slides.at(slide_id);
slide.config.try_emplace(key, to_any(value)); slide.config.try_emplace(key, to_any(value, true));
} }

@ -14,7 +14,7 @@ struct Builder {
size_t node_add(size_t slide_id, NodeType nt, const sol::object& obj); 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); void slide_config(size_t slide_id, const std::string& key, const sol::object& value);
}; };

@ -24,7 +24,7 @@ struct Color {
struct Node { struct Node {
NodeType type = NodeType::ERROR; NodeType type = NodeType::ERROR;
std::string content = ""; std::wstring content = L"";
std::unordered_map<std::string, std::any> config{}; std::unordered_map<std::string, std::any> config{};
std::shared_ptr<Image> image = nullptr; std::shared_ptr<Image> image = nullptr;
}; };

@ -16,6 +16,7 @@ void framebuffer_size_callback(GLFWwindow *, int width, int height) {
void mouse_callback(GLFWwindow *window, double xpos, double ypos) { void mouse_callback(GLFWwindow *window, double xpos, double ypos) {
Engine* engine = static_cast<Engine*>(glfwGetWindowUserPointer(window)); Engine* engine = static_cast<Engine*>(glfwGetWindowUserPointer(window));
engine->mouse(xpos, ypos); 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) void key_callback(GLFWwindow* window, int key, int scancode, int action, int mods)
@ -77,6 +78,38 @@ void setup() {
glDepthFunc(GL_LESS); 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<guecs::Clickable>(click, {
[](auto mods){
fmt::println("HOVER on slide!");
}
});
auto title = ui.entity("title");
ui.set<guecs::Text>(title, {L"The Text Test!" });
ui.set<guecs::Clickable>(title, {
[&](auto mods){
auto& at_title = ui.get<guecs::Text>(ui.entity("title"));
fmt::println("HOVER on title at x={}, y={}", at_title.position.x, at_title.position.y);
}
});
ui.init();
}
void Engine::init() { void Engine::init() {
init_glfw(); init_glfw();
window = create_window(); window = create_window();
@ -94,6 +127,8 @@ void Engine::init() {
framebuffer->init(); framebuffer->init();
glfwSetWindowUserPointer(window, this); glfwSetWindowUserPointer(window, this);
init_ui();
} }
void Engine::render() { void Engine::render() {
@ -110,7 +145,7 @@ void Engine::render() {
float y = (text->font_size + 20) * line; float y = (text->font_size + 20) * line;
if(node.type == NodeType::TEXT || node.type == NodeType::H1 || node.type == NodeType::UL) { 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) { } else if(node.type == NodeType::IMAGE) {
dbc::check(node.image != nullptr, "Node's image not configured."); dbc::check(node.image != nullptr, "Node's image not configured.");
node.image->render(); node.image->render();
@ -119,6 +154,8 @@ void Engine::render() {
} }
} }
ui.render();
framebuffer->commit(); framebuffer->commit();
framebuffer->draw(*screen); framebuffer->draw(*screen);
@ -132,6 +169,7 @@ void Engine::change_slide(int dir) {
} }
void Engine::update() { void Engine::update() {
ui.update();
glfwPollEvents(); glfwPollEvents();
} }

@ -5,9 +5,11 @@
#include "document.hpp" #include "document.hpp"
#include <GLFW/glfw3.h> #include <GLFW/glfw3.h>
#include <memory> #include <memory>
#include "guecs/ui.hpp"
struct Engine { struct Engine {
Deck& deck; Deck& deck;
guecs::UI ui{};
std::shared_ptr<FrameBuffer> framebuffer = nullptr; std::shared_ptr<FrameBuffer> framebuffer = nullptr;
std::shared_ptr<Screen> screen = nullptr; std::shared_ptr<Screen> screen = nullptr;
std::shared_ptr<Text> text = nullptr; std::shared_ptr<Text> text = nullptr;
@ -28,4 +30,5 @@ struct Engine {
void quit(); void quit();
void init(); void init();
bool active(); bool active();
void init_ui();
}; };

@ -13,21 +13,30 @@ namespace guecs {
} }
void Text::init(lel::Cell &cell, const std::string& font_file) { void Text::init(lel::Cell &cell, const std::string& font_file) {
assert(text == nullptr && "Double init on text.");
// make the text here // make the text here
text = make_shared<::Text>();
text->font_size = size;
text->load_font();
if(centered || cell.center) { if(centered || cell.center) {
// get the text bounds and use lel::center // WRONG! Need the scale here...
// auto bounds = text->getLocalBounds(); auto& ch = text->characters.at('H');
// auto text_cell = lel::center(bounds.size.x, bounds.size.y, cell);
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 // 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 { } else {
// CHANGE ME position = {float(cell.x + padding * 2), float(cell.y + padding * 2)};
// text->setPosition({float(cell.x + padding * 2), float(cell.y + padding * 2)});
} }
} }
void Text::render() { 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) { void Text::update(const wstring& new_content) {

@ -18,7 +18,8 @@ namespace guecs {
Color color = THEME.TEXT_COLOR; Color color = THEME.TEXT_COLOR;
int padding = THEME.PADDING; int padding = THEME.PADDING;
bool centered = false; bool centered = false;
shared_ptr<Text> 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 init(lel::Cell &cell, const std::string& font_file);
void update(const std::wstring& new_content); void update(const std::wstring& new_content);

@ -42,9 +42,8 @@ Options parse_options(int argc, char* argv[]) {
} }
if(!result.deck_given) { if(!result.deck_given) {
result.deck_file = "builder_test.lua"; print_usage();
// print_usage(); result.error = true;
// result.error = true;
} }
return result; return result;

@ -8,9 +8,12 @@
#include "config.hpp" #include "config.hpp"
#include FT_FREETYPE_H #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(); 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 // 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); glUniform3f(glGetUniformLocation(text_shader.ID, "textColor"), color.x, color.y, color.z);
glActiveTexture(GL_TEXTURE0 + texture_unit); glActiveTexture(GL_TEXTURE0 + texture_unit);

@ -20,7 +20,7 @@ struct Text {
std::unordered_map<unsigned char, Character> characters{}; std::unordered_map<unsigned char, Character> characters{};
unsigned int texture_unit = 5; 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(); void load_font();
Text(); Text();
}; };

Loading…
Cancel
Save