#include "guecs/sfml/components.hpp" #include "guecs/ui.hpp" #include #include #include #include #include "dbc.hpp" #include #include "slides_ui.hpp" using std::string, std::wstring, std::shared_ptr, std::make_shared; using nlohmann::json; Slide::Slide(const string& title, const string& content, json& config, json& deck_config) : $title(guecs::to_wstring(title)), $content(guecs::to_wstring(content)), $config(config) { // first config the text with any font stuff from the deck config_text($title_font, deck_config); config_text($content_font, deck_config); // then config it with the slide's config config_text($title_font, $config); config_text($content_font, $config); } void Slide::init(lel::Cell& cell) { if(!$initialized) { $initialized = true; $gui.position(cell.x, cell.y, cell.w, cell.h); $gui.layout( "[=*%(300,200)title|_|_]" "[_|_|_]" "[*%(300,600)content|_|_]" "[_|_|_]" "[_|_|_]" "[_|_|_]" "[_|_|_]" "[_|_|_]"); auto title = $gui.entity("title"); $gui.set(title, $title_font); auto content = $gui.entity("content"); $gui.set(content, $content_font); if($config.contains("image")) { $gui.set(content, {$config["image"]}); } $gui.init(); } } void Slide::config_text(guecs::Text &result, nlohmann::json& config) { if(config.contains("font_size")) result.size = config["font_size"]; if(config.contains("font_color")) { std::vector color = config["font_color"]; dbc::check(color.size() == 4, "font_color on slide must have 4 values rgba"); fmt::println("font_color is {},{},{},{}", color[0], color[1], color[2], color[3]); result.color = sf::Color{ color[0], color[1], color[2], color[3]}; } if(config.contains("font_padding")) result.padding = config["font_padding"]; if(config.contains("font_centered")) result.centered = config["font_centered"]; } void Slide::render(sf::RenderWindow& window) { $gui.render(window); // $gui.debug_layout(window); } SlidesUI::SlidesUI(shared_ptr deck) { dbc::check(deck != nullptr, "deck is null"); dbc::check(deck->slides.size() > 0, "slide deck is empy"); $deck = deck; $current = 0; } void SlidesUI::set_text(const std::string& name) { if($deck->config.contains(name)) { auto text = $gui.entity(name); std::string content = $deck->config[name]; $gui.set(text, { guecs::to_wstring(content), guecs::THEME.TEXT_SIZE-10, guecs::THEME.LIGHT_DARK, }); } } void SlidesUI::init() { $gui.position(0, 0, WINDOW_WIDTH, WINDOW_HEIGHT); $gui.layout( "[t_left|t_center|t_right]" "[*%(300,500)slide|_|_|_|_]" "[_|_|_|_|_]" "[_|_|_|_|_]" "[_|_|_|_|_]" "[_|_|_|_|_]" "[_]" "[title|*%(200,100)description|_|_|_]" ); set_text("title"); set_text("description"); guecs::Background bg{$gui.$parser}; bg.set_color(guecs::THEME.BG_COLOR_DARK); $gui.set($gui.MAIN, bg); show_slide(); $gui.init(); } Slide& SlidesUI::current() { return $deck->slides.at($current); } void SlidesUI::next_slide() { if($current < $deck->slides.size() - 1) { $current++; show_slide(); } } void SlidesUI::prev_slide() { if($current > 0) { $current--; show_slide(); } } void SlidesUI::set_slide(size_t index) { $current = index < $deck->slides.size() ? index : 0; show_slide(); } void SlidesUI::show_slide() { auto& slide = current(); // kind of have to set the background for the whole UI using the slide config auto& bg = $gui.get($gui.MAIN); sf::Color color = guecs::THEME.FILL_COLOR; if(slide.$config.contains("bg_image")) { bg.set_sprite(slide.$config["bg_image"], true); } else if($deck->config.contains("bg_image")) { bg.set_sprite($deck->config["bg_image"], true); } else { bg.sprite = nullptr; bg.texture = nullptr; } if(slide.$config.contains("bg_color")) { auto color_conf = slide.$config["bg_color"]; color = {color_conf[0], color_conf[1], color_conf[2], color_conf[3]}; } else if($deck->config.contains("bg_color")) { auto color_conf = $deck->config["bg_color"]; color = {color_conf[0], color_conf[1], color_conf[2], color_conf[3]}; } bg.set_color(color); bg.init(); auto& cell = $gui.cell_for("slide"); slide.init(cell); } void SlidesUI::render(sf::RenderWindow& window) { $gui.render(window); auto& slide = current(); slide.render(window); // $gui.debug_layout(window); } void SlidesUI::mouse(float x, float y, guecs::Modifiers mods) { $gui.mouse(x, y, mods); if(mods.test(guecs::ModBit::left)) { next_slide(); } else if(mods.test(guecs::ModBit::right)) { prev_slide(); } } void SlidesUI::handle_events(sf::RenderWindow& presenter, const sf::Event& event) { if(const auto* mev = event.getIf()) { sf::Vector2f pos = presenter.mapPixelToCoords(mev->position); if(mev->button == sf::Mouse::Button::Left) { mouse(pos.x, pos.y, {1 << guecs::ModBit::left}); } else if(mev->button == sf::Mouse::Button::Right) { mouse(pos.x, pos.y, {1 << guecs::ModBit::right}); } } }