parent
e26a937ce6
commit
8cc5973f71
@ -0,0 +1,10 @@ |
||||
#pragma once |
||||
|
||||
constexpr const int WINDOW_WIDTH=1920; |
||||
constexpr const int WINDOW_HEIGHT=1080; |
||||
constexpr const int FRAME_LIMIT=60; |
||||
constexpr const bool VSYNC=true; |
||||
constexpr const int TITLE_SIZE=124; |
||||
constexpr const int CONTENT_SIZE=94; |
||||
constexpr const int CONTROL_WIDTH=400; |
||||
constexpr const int CONTROL_HEIGHT=400; |
@ -0,0 +1,46 @@ |
||||
#include "guecs/sfml/components.hpp" |
||||
#include "guecs/ui.hpp" |
||||
#include <fmt/xchar.h> |
||||
#include <deque> |
||||
#include <iostream> |
||||
#include <nlohmann/json.hpp> |
||||
#include "dbc.hpp" |
||||
#include <memory> |
||||
#include <filesystem> |
||||
#include <fstream> |
||||
#include <iostream> |
||||
#include "constants.hpp" |
||||
#include "control.hpp" |
||||
|
||||
ControlUI::ControlUI(sf::RenderWindow& presenter) : |
||||
$presenter(presenter) |
||||
{ |
||||
$gui.position(0, 0, CONTROL_WIDTH, CONTROL_HEIGHT); |
||||
$gui.layout( |
||||
"[status]" |
||||
"[docs]" |
||||
); |
||||
} |
||||
|
||||
void ControlUI::init() { |
||||
auto status_id = $gui.entity("status"); |
||||
$gui.set<guecs::Text>(status_id, {L""}); |
||||
|
||||
auto docs_id = $gui.entity("docs"); |
||||
$gui.set<guecs::Text>(docs_id, {L"A: win left\nD: win right\nQ: quit"}); |
||||
|
||||
$status = $gui.get_if<guecs::Text>(status_id); |
||||
|
||||
$gui.init(); |
||||
} |
||||
|
||||
void ControlUI::render(sf::RenderWindow& window) { |
||||
auto pos = $presenter.getPosition(); |
||||
auto size = $presenter.getSize(); |
||||
|
||||
$status->update(fmt::format(L"pos={},{}\nsize={},{}", |
||||
pos.x, pos.y, size.x, size.y)); |
||||
|
||||
window.clear(); |
||||
$gui.render(window); |
||||
} |
@ -0,0 +1,14 @@ |
||||
#pragma once |
||||
|
||||
#include "guecs/sfml/components.hpp" |
||||
#include "guecs/ui.hpp" |
||||
|
||||
struct ControlUI { |
||||
guecs::UI $gui; |
||||
guecs::Text* $status; |
||||
sf::RenderWindow& $presenter; |
||||
|
||||
ControlUI(sf::RenderWindow& presenter); |
||||
void init(); |
||||
void render(sf::RenderWindow& window); |
||||
}; |
@ -0,0 +1,59 @@ |
||||
#include "parser.hpp" |
||||
#include "dbc.hpp" |
||||
#include <filesystem> |
||||
#include <fstream> |
||||
#include <iostream> |
||||
#include <nlohmann/json.hpp> |
||||
#include <memory> |
||||
|
||||
using std::shared_ptr, std::string, nlohmann::json; |
||||
namespace fs = std::filesystem; |
||||
|
||||
shared_ptr<SlideDeck> parse_slides(const string& md_file) { |
||||
shared_ptr<SlideDeck> slides = std::make_shared<SlideDeck>(); |
||||
|
||||
dbc::check(fs::exists(md_file), "md file missing"); |
||||
|
||||
auto size = fs::file_size(md_file); |
||||
string line(size, '\0'); |
||||
bool started = false; |
||||
json config; |
||||
|
||||
if(std::ifstream in_file{md_file, std::ios::binary}) { |
||||
while(std::getline(in_file, line)) { |
||||
if(line == "{") { |
||||
string json_data; |
||||
|
||||
do { |
||||
json_data += line; |
||||
} while (std::getline(in_file, line) && line != "}"); |
||||
|
||||
json_data += "}"; |
||||
|
||||
config = json::parse(json_data); |
||||
std::cout << "JSON: " << config << '\n'; |
||||
} else if(line == "===") { |
||||
fmt::println("START"); |
||||
started = true; |
||||
} else if(line == "---") { |
||||
fmt::println("START SLIDE"); |
||||
} else { |
||||
if(started) { |
||||
string title = line; |
||||
string content; |
||||
|
||||
while(std::getline(in_file, line) && line != "---") { |
||||
content += line + "\n"; |
||||
} |
||||
|
||||
slides->emplace_back(title, content, config); |
||||
config = json::parse("{}"); |
||||
} else { |
||||
fmt::println("JUNK: {}", line); |
||||
} |
||||
} |
||||
} |
||||
} |
||||
|
||||
return slides; |
||||
} |
@ -0,0 +1,6 @@ |
||||
#pragma once |
||||
#include <memory> |
||||
#include <string> |
||||
#include "slides.hpp" |
||||
|
||||
std::shared_ptr<SlideDeck> parse_slides(const std::string& md_file); |
@ -0,0 +1,132 @@ |
||||
#include "guecs/sfml/components.hpp" |
||||
#include "guecs/ui.hpp" |
||||
#include <fmt/xchar.h> |
||||
#include <deque> |
||||
#include <iostream> |
||||
#include <nlohmann/json.hpp> |
||||
#include "dbc.hpp" |
||||
#include <memory> |
||||
#include "constants.hpp" |
||||
#include "slides.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) : |
||||
$title(guecs::to_wstring(title)), |
||||
$content(guecs::to_wstring(content)), |
||||
$config(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,300)content|_|_]" |
||||
"[_|_|_]" |
||||
"[_|_|_]"); |
||||
|
||||
auto title = $gui.entity("title"); |
||||
$gui.set<guecs::Text>(title, {$title, TITLE_SIZE}); |
||||
|
||||
auto content = $gui.entity("content"); |
||||
$gui.set<guecs::Text>(content, { |
||||
$content, |
||||
CONTENT_SIZE, |
||||
guecs::THEME.TEXT_COLOR, 20}); |
||||
|
||||
$gui.init(); |
||||
} |
||||
} |
||||
|
||||
void Slide::render(sf::RenderWindow& window) { |
||||
$gui.render(window); |
||||
// $gui.debug_layout(window);
|
||||
} |
||||
|
||||
SlidesUI::SlidesUI(shared_ptr<SlideDeck> slides) { |
||||
dbc::check(slides != nullptr, "slides is null"); |
||||
dbc::check(slides->size() > 0, "slide deck is empy"); |
||||
$slides = slides; |
||||
$current = 0; |
||||
|
||||
$gui.position(0, 0, WINDOW_WIDTH, WINDOW_HEIGHT); |
||||
$gui.layout( |
||||
"[t_left|t_center|t_right]" |
||||
"[m_left|*%(300,400)slide|_|_|m_right]" |
||||
"[_|_|_|_|_]" |
||||
"[_|_|_|_|_]" |
||||
"[_|_|_|_|_]" |
||||
"[_|_|_|_|_]" |
||||
"[b_left|b_center|b_right]" |
||||
); |
||||
} |
||||
|
||||
void SlidesUI::init() { |
||||
guecs::Background bg{$gui.$parser, }; |
||||
bg.set_color(guecs::THEME.BG_COLOR_DARK); |
||||
$gui.set<guecs::Background>($gui.MAIN, bg); |
||||
|
||||
show_slide(); |
||||
|
||||
$gui.init(); |
||||
} |
||||
|
||||
Slide& SlidesUI::current() { |
||||
return $slides->at($current); |
||||
} |
||||
|
||||
void SlidesUI::next_slide() { |
||||
if($current < $slides->size() - 1) { |
||||
$current++; |
||||
show_slide(); |
||||
} |
||||
} |
||||
|
||||
void SlidesUI::prev_slide() { |
||||
if($current > 0) { |
||||
$current--; |
||||
show_slide(); |
||||
} |
||||
} |
||||
|
||||
void SlidesUI::show_slide() { |
||||
auto& slide = current(); |
||||
auto& bg = $gui.get<guecs::Background>($gui.MAIN); |
||||
sf::Color color = guecs::THEME.FILL_COLOR; |
||||
|
||||
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]}; |
||||
} |
||||
|
||||
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(); |
||||
} |
||||
} |
@ -0,0 +1,37 @@ |
||||
#pragma once |
||||
|
||||
#include "guecs/sfml/backend.hpp" |
||||
#include "guecs/sfml/components.hpp" |
||||
#include "guecs/ui.hpp" |
||||
#include <nlohmann/json.hpp> |
||||
#include <memory> |
||||
|
||||
struct Slide { |
||||
guecs::UI $gui; |
||||
std::wstring $title; |
||||
std::wstring $content; |
||||
nlohmann::json $config; |
||||
bool $initialized = false; |
||||
|
||||
Slide(const std::string& title, const std::string& content, nlohmann::json& config); |
||||
void init(lel::Cell& cell); |
||||
void render(sf::RenderWindow& window); |
||||
}; |
||||
|
||||
using SlideDeck = std::vector<Slide>; |
||||
|
||||
struct SlidesUI { |
||||
guecs::UI $gui; |
||||
std::shared_ptr<SlideDeck> $slides = nullptr; |
||||
size_t $current = 0; |
||||
|
||||
SlidesUI(std::shared_ptr<SlideDeck> slides); |
||||
void init(); |
||||
Slide& current(); |
||||
|
||||
void next_slide(); |
||||
void prev_slide(); |
||||
void show_slide(); |
||||
void render(sf::RenderWindow& window); |
||||
void mouse(float x, float y, guecs::Modifiers mods); |
||||
}; |
Loading…
Reference in new issue