diff --git a/main.cpp b/main.cpp index 724ad05..c2c0078 100644 --- a/main.cpp +++ b/main.cpp @@ -5,25 +5,80 @@ #include #include #include +#include "dbc.hpp" +#include constexpr const int WINDOW_WIDTH=1280; constexpr const int WINDOW_HEIGHT=720; constexpr const int FRAME_LIMIT=60; constexpr const bool VSYNC=true; -using std::string, std::wstring; +using std::string, std::wstring, std::shared_ptr, std::make_shared; using nlohmann::json; +struct Slide { + guecs::UI $gui; + wstring $title; + wstring $content; + bool $initialized = false; + + Slide(const string& title, const string& content) : + $title(guecs::to_wstring(title)), + $content(guecs::to_wstring(content)) + { + } + + void init(lel::Cell& cell) { + if(!$initialized) { + $initialized = true; + + $gui.position(cell.x, cell.y, cell.w, cell.h); + $gui.layout( + "[=*%(300,200)title|_|_]" + "[_|_|_]" + "[=*%(300,300)content|_|_]" + "[_|_|_]" + "[_|_|_]"); + + auto title = $gui.entity("title"); + $gui.set(title, { + $title, 124}); + + auto content = $gui.entity("content"); + $gui.set(content, { + $content, + 56, guecs::THEME.TEXT_COLOR, 20}); + + $gui.init(); + } + } + + void render(sf::RenderWindow& window) { + $gui.render(window); + // $gui.debug_layout(window); + } +}; + +using SlideDeck = std::vector; + + struct SlidesUI { guecs::UI $gui; + shared_ptr $slides = nullptr; + size_t $current = 0; + + SlidesUI(shared_ptr slides) { + dbc::check(slides != nullptr, "slides is null"); + dbc::check(slides->size() > 0, "slide deck is empy"); + $slides = slides; + $current = 0; - SlidesUI() { $gui.position(0, 0, WINDOW_WIDTH, WINDOW_HEIGHT); $gui.layout( "[t_left|t_center|t_right]" - "[m_left|*%(300,200)title|_|_|m_right]" + "[m_left|*%(300,400)slide|_|_|m_right]" + "[_|_|_|_|_]" "[_|_|_|_|_]" - "[_|*%(300,300)content|_|_|_]" "[_|_|_|_|_]" "[_|_|_|_|_]" "[b_left|b_center|b_right]" @@ -35,37 +90,66 @@ struct SlidesUI { bg.set_color(guecs::THEME.BG_COLOR_DARK); $gui.set($gui.MAIN, bg); - auto title = $gui.entity("title"); - $gui.set(title, { - L"Title Thing\n", 124}); - - auto content = $gui.entity("content"); - $gui.set(content, { - L"* Sample 2\n" - L"* Sample 3\n", - 56, guecs::THEME.TEXT_COLOR, 20}); + show_slide(); $gui.init(); } - void set_data(json& data) { - (void)data; + Slide& current() { + return $slides->at($current); + } + + void next_slide() { + if($current < $slides->size() - 1) { + $current++; + show_slide(); + } + } + + void prev_slide() { + if($current > 0) { + $current--; + show_slide(); + } + } + + void show_slide() { + auto& slide = current(); + auto& cell = $gui.cell_for("slide"); + slide.init(cell); } void render(sf::RenderWindow& window) { $gui.render(window); + + auto& slide = current(); + slide.render(window); + // $gui.debug_layout(window); } void 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(); + } } }; -json parse_slides(const std::string& md_file) { +shared_ptr parse_slides(const string& md_file) { + shared_ptr slides = make_shared(); + + for(int i = 0; i < 10; i++) { + slides->emplace_back(fmt::format("Title {}", i),"content"); + } + (void)md_file; - return {}; + + return slides; } int main() { @@ -77,9 +161,8 @@ int main() { window.setVerticalSyncEnabled(VSYNC); auto data = parse_slides("some_bullshit.md"); - SlidesUI slides; + SlidesUI slides(data); slides.init(); - slides.set_data(data); while(window.isOpen()) { while (const auto event = window.pollEvent()) { @@ -88,9 +171,12 @@ int main() { } if(const auto* mouse = event->getIf()) { + sf::Vector2f pos = window.mapPixelToCoords(mouse->position); + if(mouse->button == sf::Mouse::Button::Left) { - sf::Vector2f pos = window.mapPixelToCoords(mouse->position); - slides.mouse(pos.x, pos.y, {}); + slides.mouse(pos.x, pos.y, {1 << guecs::ModBit::left}); + } else if(mouse->button == sf::Mouse::Button::Right) { + slides.mouse(pos.x, pos.y, {1 << guecs::ModBit::right}); } } }