Now mostly working with GUECS but shaders are still busted. Have to find out why they stopped working.
parent
a5f6a82611
commit
ac22a11c9f
@ -0,0 +1,66 @@ |
||||
#include "backend.hpp" |
||||
#include "shaders.hpp" |
||||
#include "sound.hpp" |
||||
#include "textures.hpp" |
||||
|
||||
namespace sfml { |
||||
guecs::SpriteTexture Backend::texture_get(const string& name) { |
||||
auto sp = textures::get(name); |
||||
return {sp.sprite, sp.texture}; |
||||
} |
||||
|
||||
Backend::Backend() { |
||||
sound::init(); |
||||
shaders::init(); |
||||
textures::init(); |
||||
} |
||||
|
||||
void Backend::sound_play(const string& name) { |
||||
sound::play(name); |
||||
} |
||||
|
||||
void Backend::sound_stop(const string& name) { |
||||
sound::stop(name); |
||||
} |
||||
|
||||
std::shared_ptr<sf::Shader> Backend::shader_get(const std::string& name) { |
||||
return shaders::get(name); |
||||
} |
||||
|
||||
bool Backend::shader_updated() { |
||||
if(shaders::updated($shaders_version)) { |
||||
$shaders_version = shaders::version(); |
||||
return true; |
||||
} else { |
||||
return false; |
||||
} |
||||
} |
||||
|
||||
guecs::Theme Backend::theme() { |
||||
guecs::Theme theme { |
||||
.BLACK={1, 4, 2}, |
||||
.DARK_DARK={9, 29, 16}, |
||||
.DARK_MID={14, 50, 26}, |
||||
.DARK_LIGHT={0, 109, 44}, |
||||
.MID={63, 171, 92}, |
||||
.LIGHT_DARK={161, 217, 155}, |
||||
.LIGHT_MID={199, 233, 192}, |
||||
.LIGHT_LIGHT={229, 245, 224}, |
||||
.WHITE={255, 255, 255}, |
||||
.TRANSPARENT = sf::Color::Transparent |
||||
}; |
||||
|
||||
theme.PADDING = 3; |
||||
theme.BORDER_PX = 1; |
||||
theme.TEXT_SIZE = 40; |
||||
theme.LABEL_SIZE = 40; |
||||
theme.FILL_COLOR = theme.DARK_DARK; |
||||
theme.TEXT_COLOR = theme.LIGHT_LIGHT; |
||||
theme.BG_COLOR = theme.MID; |
||||
theme.BORDER_COLOR = theme.LIGHT_DARK; |
||||
theme.BG_COLOR_DARK = theme.BLACK; |
||||
theme.FONT_FILE_NAME = "assets/text.ttf"; |
||||
|
||||
return theme; |
||||
} |
||||
} |
@ -0,0 +1,19 @@ |
||||
#include "guecs/ui.hpp" |
||||
|
||||
namespace sfml { |
||||
using std::string; |
||||
|
||||
class Backend : public guecs::Backend { |
||||
int $shaders_version = 0; |
||||
|
||||
public: |
||||
|
||||
Backend(); |
||||
guecs::SpriteTexture texture_get(const string& name); |
||||
void sound_play(const string& name); |
||||
void sound_stop(const string& name); |
||||
std::shared_ptr<sf::Shader> shader_get(const std::string& name); |
||||
bool shader_updated(); |
||||
guecs::Theme theme(); |
||||
}; |
||||
} |
@ -1,15 +0,0 @@ |
||||
#pragma once |
||||
#include <SFML/Graphics/Color.hpp> |
||||
|
||||
namespace ColorValue { |
||||
const sf::Color BLACK{1, 4, 2}; |
||||
const sf::Color DARK_DARK{9, 29, 16}; |
||||
const sf::Color DARK_MID{14, 50, 26}; |
||||
const sf::Color DARK_LIGHT{0, 109, 44}; |
||||
const sf::Color MID{63, 171, 92}; |
||||
const sf::Color LIGHT_DARK{161, 217, 155}; |
||||
const sf::Color LIGHT_MID{199, 233, 192}; |
||||
const sf::Color LIGHT_LIGHT{229, 245, 224}; |
||||
const sf::Color WHITE{255, 255, 255}; |
||||
const sf::Color TRANSPARENT = sf::Color::Transparent; |
||||
} |
@ -1,316 +0,0 @@ |
||||
#include "guecs.hpp" |
||||
#include "shaders.hpp" |
||||
#include "sound.hpp" |
||||
#include "config.hpp" |
||||
|
||||
namespace guecs { |
||||
|
||||
void Textual::init(lel::Cell &cell, shared_ptr<sf::Font> font_ptr) { |
||||
dbc::check(font_ptr != nullptr, "you failed to initialize this WideText"); |
||||
if(font == nullptr) font = font_ptr; |
||||
if(text == nullptr) text = make_shared<sf::Text>(*font, content, size); |
||||
text->setFillColor(color); |
||||
|
||||
if(centered) { |
||||
auto bounds = text->getLocalBounds(); |
||||
auto text_cell = lel::center(bounds.size.x, bounds.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}); |
||||
} else { |
||||
text->setPosition({float(cell.x + padding * 2), float(cell.y + padding * 2)}); |
||||
} |
||||
|
||||
text->setCharacterSize(size); |
||||
} |
||||
|
||||
void Textual::update(std::wstring& new_content) { |
||||
content = new_content; |
||||
text->setString(content); |
||||
} |
||||
|
||||
void Sprite::init(lel::Cell &cell) { |
||||
auto sprite_texture = textures::get(name); |
||||
|
||||
sprite = make_shared<sf::Sprite>( |
||||
*sprite_texture.texture, |
||||
sprite_texture.sprite->getTextureRect()); |
||||
|
||||
sprite->setPosition({ |
||||
float(cell.x + padding), |
||||
float(cell.y + padding)}); |
||||
|
||||
auto bounds = sprite->getLocalBounds(); |
||||
|
||||
sprite->setScale({ |
||||
float(cell.w - padding * 2) / bounds.size.x, |
||||
float(cell.h - padding * 2) / bounds.size.y}); |
||||
} |
||||
|
||||
void Rectangle::init(lel::Cell& cell) { |
||||
sf::Vector2f size{float(cell.w) - padding * 2, float(cell.h) - padding * 2}; |
||||
if(shape == nullptr) shape = make_shared<sf::RectangleShape>(size); |
||||
shape->setPosition({float(cell.x + padding), float(cell.y + padding)}); |
||||
shape->setFillColor(color); |
||||
shape->setOutlineColor(border_color); |
||||
shape->setOutlineThickness(border_px); |
||||
} |
||||
|
||||
|
||||
void Meter::init(lel::Cell& cell) { |
||||
bar.init(cell); |
||||
} |
||||
|
||||
void Sound::play(bool hover) { |
||||
if(!hover) { |
||||
sound::play(on_click); |
||||
} |
||||
} |
||||
|
||||
void Background::init() { |
||||
sf::Vector2f size{float(w), float(h)}; |
||||
if(shape == nullptr) shape = make_shared<sf::RectangleShape>(size); |
||||
shape->setPosition({float(x), float(y)}); |
||||
shape->setFillColor(color); |
||||
} |
||||
|
||||
void Effect::init(lel::Cell &cell) { |
||||
$shader_version = shaders::version(); |
||||
$shader = shaders::get(name); |
||||
$shader->setUniform("u_resolution", sf::Vector2f({float(cell.w), float(cell.h)})); |
||||
$clock = std::make_shared<sf::Clock>(); |
||||
} |
||||
|
||||
void Effect::step() { |
||||
sf::Time cur_time = $clock->getElapsedTime(); |
||||
float u_time = cur_time.asSeconds(); |
||||
|
||||
if(u_time < $u_time_end) { |
||||
$shader->setUniform("u_duration", duration); |
||||
$shader->setUniform("u_time_end", $u_time_end); |
||||
$shader->setUniform("u_time", u_time); |
||||
} else { |
||||
$active = false; |
||||
} |
||||
} |
||||
|
||||
void Effect::run() { |
||||
$active = true; |
||||
sf::Time u_time = $clock->getElapsedTime(); |
||||
$u_time_end = u_time.asSeconds() + duration; |
||||
} |
||||
|
||||
shared_ptr<sf::Shader> Effect::checkout_ptr() { |
||||
if(shaders::updated($shader_version)) { |
||||
$shader = shaders::get(name); |
||||
$shader_version = shaders::version(); |
||||
} |
||||
|
||||
return $shader; |
||||
} |
||||
|
||||
UI::UI() { |
||||
$font = make_shared<sf::Font>(Config::path_to(FONT_FILE_NAME)); |
||||
} |
||||
|
||||
void UI::position(int x, int y, int width, int height) { |
||||
$parser.position(x, y, width, height); |
||||
} |
||||
|
||||
void UI::layout(std::string grid) { |
||||
$grid = grid; |
||||
bool good = $parser.parse($grid); |
||||
dbc::check(good, "LEL parsing failed."); |
||||
|
||||
for(auto& [name, cell] : $parser.cells) { |
||||
auto ent = init_entity(name); |
||||
$world.set<lel::Cell>(ent, cell); |
||||
} |
||||
} |
||||
|
||||
DinkyECS::Entity UI::init_entity(std::string name) { |
||||
auto entity = $world.entity(); |
||||
// this lets you look up an entity by name
|
||||
$name_ents.insert_or_assign(name, entity); |
||||
// this makes it easier to get the name during querying
|
||||
$world.set<CellName>(entity, {name}); |
||||
return entity; |
||||
} |
||||
|
||||
DinkyECS::Entity UI::entity(std::string name) { |
||||
dbc::check($name_ents.contains(name), |
||||
fmt::format("GUECS entity {} does not exist. Forgot to init_entity?", name)); |
||||
return $name_ents.at(name); |
||||
} |
||||
|
||||
void UI::init() { |
||||
if($world.has_the<Background>()) { |
||||
auto& bg = $world.get_the<Background>(); |
||||
bg.init(); |
||||
} |
||||
|
||||
$world.query<Background>([](auto, auto& bg) { |
||||
bg.init(); |
||||
}); |
||||
|
||||
$world.query<lel::Cell, Rectangle>([](auto, auto& cell, auto& rect) { |
||||
rect.init(cell); |
||||
}); |
||||
|
||||
$world.query<lel::Cell, Effect>([](auto, auto& cell, auto& effect) { |
||||
effect.init(cell); |
||||
}); |
||||
|
||||
$world.query<Rectangle, Meter>([](auto, auto& bg, auto &) { |
||||
bg.shape->setFillColor(ColorValue::BLACK); |
||||
}); |
||||
|
||||
$world.query<lel::Cell, Meter>([](auto, auto &cell, auto& meter) { |
||||
meter.init(cell); |
||||
}); |
||||
|
||||
$world.query<lel::Cell, Textual>([this](auto, auto& cell, auto& text) { |
||||
text.init(cell, $font); |
||||
}); |
||||
|
||||
$world.query<lel::Cell, Label>([this](auto, auto& cell, auto& text) { |
||||
text.init(cell, $font); |
||||
}); |
||||
|
||||
$world.query<lel::Cell, Sprite>([&](auto, auto &cell, auto &sprite) { |
||||
sprite.init(cell); |
||||
}); |
||||
} |
||||
|
||||
void UI::debug_layout(sf::RenderWindow& window) { |
||||
$world.query<lel::Cell>([&](const auto, auto &cell) { |
||||
sf::RectangleShape rect{{float(cell.w), float(cell.h)}}; |
||||
rect.setPosition({float(cell.x), float(cell.y)}); |
||||
rect.setFillColor(sf::Color::Transparent); |
||||
rect.setOutlineColor(sf::Color::Red); |
||||
rect.setOutlineThickness(2.0f); |
||||
window.draw(rect); |
||||
}); |
||||
} |
||||
|
||||
void UI::render(sf::RenderWindow& window) { |
||||
if($world.has_the<Background>()) { |
||||
auto& bg = $world.get_the<Background>(); |
||||
window.draw(*bg.shape); |
||||
} |
||||
|
||||
$world.query<Effect>([&](auto, auto& shader) { |
||||
if(shader.$active) shader.step(); |
||||
}); |
||||
|
||||
$world.query<Rectangle>([&](auto ent, auto& rect) { |
||||
render_helper(window, ent, true, rect.shape); |
||||
}); |
||||
|
||||
$world.query<lel::Cell, Meter>([&](auto ent, auto& cell, const auto &meter) { |
||||
int pad = meter.bar.padding * 2; |
||||
float level = std::clamp(meter.percent, 0.0f, 1.0f) * float(cell.w - pad); |
||||
// ZED: this 6 is a border width, make it a thing
|
||||
meter.bar.shape->setSize({std::max(level, 0.0f), float(cell.h - pad)}); |
||||
render_helper(window, ent, true, meter.bar.shape); |
||||
}); |
||||
|
||||
$world.query<Sprite>([&](auto ent, auto& sprite) { |
||||
render_helper(window, ent, false, sprite.sprite); |
||||
}); |
||||
|
||||
$world.query<Label>([&](auto ent, auto& text) { |
||||
render_helper(window, ent, false, text.text); |
||||
}); |
||||
|
||||
$world.query<Textual>([&](auto ent, auto& text) { |
||||
render_helper(window, ent, true, text.text); |
||||
}); |
||||
} |
||||
|
||||
bool UI::mouse(float x, float y, bool hover) { |
||||
int action_count = 0; |
||||
|
||||
$world.query<lel::Cell, Clickable>([&](auto ent, auto& cell, auto &clicked) { |
||||
if((x >= cell.x && x <= cell.x + cell.w) && |
||||
(y >= cell.y && y <= cell.y + cell.h)) |
||||
{ |
||||
do_if<Effect>(ent, [hover](auto& effect) { |
||||
effect.$shader->setUniform("hover", hover); |
||||
effect.run(); |
||||
}); |
||||
|
||||
do_if<Sound>(ent, [hover](auto& sound) { |
||||
// here set that it played then only play once
|
||||
sound.play(hover); |
||||
}); |
||||
|
||||
if(hover) return; // kinda gross
|
||||
|
||||
if(auto action_data = get_if<ActionData>(ent)) { |
||||
clicked.action(ent, action_data->data); |
||||
} else { |
||||
clicked.action(ent, {}); |
||||
} |
||||
|
||||
action_count++; |
||||
} else { |
||||
// via ORBLISHJ
|
||||
// just reset the hover trigger for all that aren't hit
|
||||
// then in the ^^ positive branch play it and set it played
|
||||
} |
||||
}); |
||||
|
||||
return action_count > 0; |
||||
} |
||||
|
||||
void UI::show_sprite(string region, string sprite_name) { |
||||
auto ent = entity(region); |
||||
|
||||
if(has<Sprite>(ent)) { |
||||
auto& to_show = get<Sprite>(ent); |
||||
auto sprite_texture = textures::get(sprite_name); |
||||
to_show.sprite->setTexture(*sprite_texture.texture); |
||||
} else { |
||||
Sprite to_show{sprite_name}; |
||||
auto& cell = cell_for(ent); |
||||
to_show.init(cell); |
||||
set<guecs::Sprite>(ent, to_show); |
||||
} |
||||
} |
||||
|
||||
void UI::show_text(string region, wstring content) { |
||||
auto ent = entity(region); |
||||
|
||||
if(auto text = get_if<Textual>(ent)) { |
||||
text->text->setString(content); |
||||
} else { |
||||
auto &cell = cell_for(ent); |
||||
Textual to_set{content, 20}; |
||||
to_set.init(cell, $font); |
||||
to_set.text->setFillColor(ColorValue::LIGHT_MID); |
||||
set<Textual>(ent, to_set); |
||||
} |
||||
} |
||||
|
||||
void UI::show_label(string region, wstring content) { |
||||
auto ent = entity(region); |
||||
|
||||
if(auto text = get_if<Label>(ent)) { |
||||
text->text->setString(content); |
||||
} else { |
||||
auto &cell = cell_for(ent); |
||||
Label to_set{content, 20}; |
||||
to_set.init(cell, $font); |
||||
to_set.text->setFillColor(ColorValue::LIGHT_MID); |
||||
set<Label>(ent, to_set); |
||||
} |
||||
} |
||||
|
||||
Clickable make_action(DinkyECS::World& target, Events::GUI event) { |
||||
return {[&, event](auto ent, auto data){ |
||||
// remember that ent is passed in from the UI::mouse handler
|
||||
target.send<Events::GUI>(event, ent, data); |
||||
}}; |
||||
} |
||||
|
||||
} |
@ -1,233 +0,0 @@ |
||||
#pragma once |
||||
#include "color.hpp" |
||||
#include "dinkyecs.hpp" |
||||
#include "lel.hpp" |
||||
#include <string> |
||||
#include <memory> |
||||
#include <SFML/Graphics.hpp> |
||||
#include "textures.hpp" |
||||
#include <functional> |
||||
#include "events.hpp" |
||||
#include "constants.hpp" |
||||
#include <any> |
||||
#include "shaders.hpp" |
||||
|
||||
namespace guecs { |
||||
using std::shared_ptr, std::make_shared, std::wstring, std::string; |
||||
|
||||
struct Textual { |
||||
std::wstring content; |
||||
unsigned int size = GUECS_FONT_SIZE; |
||||
sf::Color color = GUECS_TEXT_COLOR; |
||||
int padding = GUECS_PADDING; |
||||
bool centered = false; |
||||
shared_ptr<sf::Font> font = nullptr; |
||||
shared_ptr<sf::Text> text = nullptr; |
||||
|
||||
void init(lel::Cell &cell, shared_ptr<sf::Font> font_ptr); |
||||
void update(std::wstring& new_content); |
||||
}; |
||||
|
||||
struct Label : public Textual { |
||||
template<typename... Args> |
||||
Label(Args... args) : Textual(args...) |
||||
{ |
||||
centered = true; |
||||
} |
||||
|
||||
Label() { |
||||
centered = true; |
||||
}; |
||||
}; |
||||
|
||||
struct Clickable { |
||||
/* This is actually called by UI::mouse and passed the entity ID of the
|
||||
* button pressed so you can interact with it in the event handler. |
||||
*/ |
||||
std::function<void(DinkyECS::Entity ent, std::any data)> action; |
||||
}; |
||||
|
||||
struct Sprite { |
||||
std::string name; |
||||
int padding = GUECS_PADDING; |
||||
std::shared_ptr<sf::Sprite> sprite = nullptr; |
||||
std::shared_ptr<sf::Texture> texture = nullptr; |
||||
|
||||
void init(lel::Cell &cell); |
||||
}; |
||||
|
||||
struct Rectangle { |
||||
int padding = GUECS_PADDING; |
||||
sf::Color color = GUECS_FILL_COLOR; |
||||
sf::Color border_color = GUECS_BORDER_COLOR; |
||||
int border_px = GUECS_BORDER_PX; |
||||
shared_ptr<sf::RectangleShape> shape = nullptr; |
||||
|
||||
void init(lel::Cell& cell); |
||||
}; |
||||
|
||||
struct Meter { |
||||
float percent = 1.0f; |
||||
Rectangle bar{}; |
||||
|
||||
void init(lel::Cell& cell); |
||||
}; |
||||
|
||||
struct ActionData { |
||||
std::any data; |
||||
}; |
||||
|
||||
struct CellName { |
||||
std::string name; |
||||
}; |
||||
|
||||
struct Effect { |
||||
float duration = 0.1f; |
||||
std::string name{"ui_shader"}; |
||||
float $u_time_end = 0.0; |
||||
bool $active = false; |
||||
std::shared_ptr<sf::Clock> $clock = nullptr; |
||||
std::shared_ptr<sf::Shader> $shader = nullptr; |
||||
int $shader_version = 0; |
||||
|
||||
void init(lel::Cell &cell); |
||||
void run(); |
||||
void step(); |
||||
shared_ptr<sf::Shader> checkout_ptr(); |
||||
}; |
||||
|
||||
struct Sound { |
||||
std::string on_click{"ui_click"}; |
||||
void play(bool hover); |
||||
}; |
||||
|
||||
struct Background { |
||||
float x = 0.0f; |
||||
float y = 0.0f; |
||||
float w = 0.0f; |
||||
float h = 0.0f; |
||||
sf::Color color = GUECS_BG_COLOR; |
||||
shared_ptr<sf::RectangleShape> shape = nullptr; |
||||
|
||||
Background(lel::Parser& parser, sf::Color bg_color=GUECS_BG_COLOR) : |
||||
x(parser.grid_x), |
||||
y(parser.grid_y), |
||||
w(parser.grid_w), |
||||
h(parser.grid_h), |
||||
color(bg_color) |
||||
{} |
||||
|
||||
Background() {} |
||||
|
||||
void init(); |
||||
}; |
||||
|
||||
class UI { |
||||
public: |
||||
DinkyECS::World $world; |
||||
std::unordered_map<std::string, DinkyECS::Entity> $name_ents; |
||||
shared_ptr<sf::Font> $font = nullptr; |
||||
lel::Parser $parser; |
||||
std::string $grid = ""; |
||||
|
||||
UI(); |
||||
|
||||
void position(int x, int y, int width, int height); |
||||
void layout(std::string grid); |
||||
DinkyECS::Entity init_entity(std::string name); |
||||
DinkyECS::Entity entity(std::string name); |
||||
|
||||
inline lel::CellMap& cells() { |
||||
return $parser.cells; |
||||
} |
||||
|
||||
inline DinkyECS::World& world() { |
||||
return $world; |
||||
} |
||||
|
||||
void init(); |
||||
void render(sf::RenderWindow& window); |
||||
bool mouse(float x, float y, bool hover); |
||||
void debug_layout(sf::RenderWindow& window); |
||||
|
||||
template <typename Comp> |
||||
void set(DinkyECS::Entity ent, Comp val) { |
||||
$world.set<Comp>(ent, val); |
||||
} |
||||
|
||||
template <typename Comp> |
||||
void set_init(DinkyECS::Entity ent, Comp val) { |
||||
dbc::check(has<lel::Cell>(ent),"WRONG! slot is missing its cell?!"); |
||||
auto& cell = get<lel::Cell>(ent); |
||||
val.init(cell); |
||||
$world.set<Comp>(ent, val); |
||||
} |
||||
|
||||
template <typename Comp> |
||||
void do_if(DinkyECS::Entity ent, std::function<void(Comp &)> cb) { |
||||
if($world.has<Comp>(ent)) { |
||||
cb($world.get<Comp>(ent)); |
||||
} |
||||
} |
||||
|
||||
lel::Cell& cell_for(DinkyECS::Entity ent) { |
||||
return $world.get<lel::Cell>(ent); |
||||
} |
||||
|
||||
lel::Cell& cell_for(std::string name) { |
||||
DinkyECS::Entity ent = entity(name); |
||||
return $world.get<lel::Cell>(ent); |
||||
} |
||||
|
||||
template <typename Comp> |
||||
Comp& get(DinkyECS::Entity entity) { |
||||
return $world.get<Comp>(entity); |
||||
} |
||||
|
||||
template <typename Comp> |
||||
std::optional<Comp> get_if(DinkyECS::Entity entity) { |
||||
return $world.get_if<Comp>(entity); |
||||
} |
||||
|
||||
template <typename Comp> |
||||
bool has(DinkyECS::Entity entity) { |
||||
return $world.has<Comp>(entity); |
||||
} |
||||
|
||||
template <typename Comp> |
||||
void remove(DinkyECS::Entity ent) { |
||||
$world.remove<Comp>(ent); |
||||
} |
||||
|
||||
template <typename Comp> |
||||
void close(string region) { |
||||
auto ent = entity(region); |
||||
remove<Comp>(ent); |
||||
} |
||||
|
||||
template<typename T> |
||||
void render_helper(sf::RenderWindow& window, DinkyECS::Entity ent, bool is_shape, T& target) { |
||||
sf::Shader *shader_ptr = nullptr; |
||||
|
||||
if($world.has<Effect>(ent)) { |
||||
auto& shader = $world.get<Effect>(ent); |
||||
|
||||
if(shader.$active) { |
||||
auto ptr = shader.checkout_ptr(); |
||||
ptr->setUniform("is_shape", is_shape); |
||||
// NOTE: this is needed because SFML doesn't handle shared_ptr
|
||||
shader_ptr = ptr.get(); |
||||
} |
||||
} |
||||
|
||||
window.draw(*target, shader_ptr); |
||||
} |
||||
|
||||
void show_sprite(string region, string sprite_name); |
||||
void show_text(string region, wstring content); |
||||
void show_label(string region, wstring content); |
||||
}; |
||||
|
||||
Clickable make_action(DinkyECS::World& target, Events::GUI event); |
||||
|
||||
} |
@ -1,117 +0,0 @@ |
||||
#include "lel.hpp" |
||||
#include <fmt/core.h> |
||||
#include "dbc.hpp" |
||||
#include <numeric> |
||||
|
||||
#include "lel_parser.cpp" |
||||
|
||||
namespace lel { |
||||
|
||||
Parser::Parser(int x, int y, int width, int height) : |
||||
grid_x(x), |
||||
grid_y(y), |
||||
grid_w(width), |
||||
grid_h(height), |
||||
cur(0, 0) |
||||
{ |
||||
} |
||||
|
||||
Parser::Parser() : cur(0, 0) { } |
||||
|
||||
void Parser::position(int x, int y, int width, int height) { |
||||
grid_x = x; |
||||
grid_y = y; |
||||
grid_w = width; |
||||
grid_h = height; |
||||
} |
||||
|
||||
void Parser::id(std::string name) { |
||||
if(name != "_") { |
||||
dbc::check(!cells.contains(name), |
||||
fmt::format("duplicate cell name {}", name)); |
||||
cells.insert_or_assign(name, cur); |
||||
} |
||||
|
||||
cur = {cur.col + 1, cur.row}; |
||||
auto& row = grid.back(); |
||||
row.push_back(name); |
||||
} |
||||
|
||||
void Parser::finalize() { |
||||
size_t rows = grid.size(); |
||||
int cell_height = grid_h / rows; |
||||
|
||||
for(auto& row : grid) { |
||||
size_t columns = row.size(); |
||||
int cell_width = grid_w / columns; |
||||
dbc::check(cell_width > 0, "invalid cell width calc"); |
||||
dbc::check(cell_height > 0, "invalid cell height calc"); |
||||
|
||||
for(auto& name : row) { |
||||
if(name == "_") continue; |
||||
auto& cell = cells.at(name); |
||||
|
||||
// ZED: getting a bit hairy but this should work
|
||||
if(cell.percent) { |
||||
// when percent mode we have to take unset to 100%
|
||||
if(cell.max_w == 0) cell.max_w = 100; |
||||
if(cell.max_h == 0) cell.max_h = 100; |
||||
cell.max_w *= cell_width * 0.01; |
||||
cell.max_h *= cell_height * 0.01; |
||||
} else { |
||||
if(cell.max_w == 0) cell.max_w = cell_width; |
||||
if(cell.max_h == 0) cell.max_h = cell_height; |
||||
} |
||||
|
||||
cell.w = cell.expand ? std::min(cell.max_w, grid_w) : std::min(cell_width, cell.max_w); |
||||
cell.h = cell.expand ? std::min(cell.max_h, grid_h) : std::min(cell_height, cell.max_h); |
||||
|
||||
dbc::check(cell.h > 0, fmt::format("invalid height cell {}", name)); |
||||
dbc::check(cell.w > 0, fmt::format("invalid width cell {}", name)); |
||||
|
||||
cell.x = grid_x + (cell.col * cell_width); |
||||
cell.y = grid_y + (cell.row * cell_height); |
||||
|
||||
// keep the midpoint since it is used a lot
|
||||
cell.mid_x = std::midpoint(cell.x, cell.x + cell.w); |
||||
cell.mid_y = std::midpoint(cell.y, cell.y + cell.h); |
||||
|
||||
// perform alignments
|
||||
if(cell.right) cell.x += cell_width - cell.w; |
||||
if(cell.bottom) cell.y += cell_height - cell.h; |
||||
if(cell.center) { |
||||
cell.x = cell.mid_x - cell.w / 2; |
||||
cell.y = cell.mid_y - cell.h / 2; |
||||
} |
||||
} |
||||
} |
||||
} |
||||
|
||||
void Parser::reset() { |
||||
cur = {0, 0}; |
||||
grid.clear(); |
||||
cells.clear(); |
||||
} |
||||
|
||||
std::optional<std::string> Parser::hit(int x, int y) { |
||||
for(auto& [name, cell] : cells) { |
||||
if((x >= cell.x && x <= cell.x + cell.w) && |
||||
(y >= cell.y && y <= cell.y + cell.h)) { |
||||
return name; |
||||
} |
||||
} |
||||
|
||||
return std::nullopt; |
||||
} |
||||
|
||||
Cell center(int width, int height, Cell &parent) { |
||||
Cell copy = parent; |
||||
|
||||
copy.x = parent.mid_x - width / 2; |
||||
copy.y = parent.mid_y - height / 2; |
||||
copy.w = width; |
||||
copy.h = height; |
||||
|
||||
return copy; |
||||
} |
||||
} |
@ -1,57 +0,0 @@ |
||||
#pragma once |
||||
#include <string> |
||||
#include <unordered_map> |
||||
#include <functional> |
||||
#include <optional> |
||||
#include <vector> |
||||
#include "dbc.hpp" |
||||
|
||||
namespace lel { |
||||
|
||||
struct Cell { |
||||
int x = 0; |
||||
int y = 0; |
||||
int w = 0; |
||||
int h = 0; |
||||
int mid_x = 0; |
||||
int mid_y = 0; |
||||
int max_w = 0; |
||||
int max_h = 0; |
||||
int col = 0; |
||||
int row = 0; |
||||
bool right = false; |
||||
bool bottom = false; |
||||
bool expand = false; |
||||
bool center = false; |
||||
bool percent = false; |
||||
|
||||
Cell(int col, int row) : col(col), row(row) {} |
||||
Cell() {} |
||||
}; |
||||
|
||||
using Row = std::vector<std::string>; |
||||
using CellMap = std::unordered_map<std::string, Cell>; |
||||
|
||||
struct Parser { |
||||
int grid_x = 0; |
||||
int grid_y = 0; |
||||
int grid_w = 0; |
||||
int grid_h = 0; |
||||
Cell cur; |
||||
std::vector<Row> grid; |
||||
CellMap cells; |
||||
|
||||
Parser(int x, int y, int width, int height); |
||||
Parser(); |
||||
|
||||
void position(int x, int y, int width, int height); |
||||
void id(std::string name); |
||||
void reset(); |
||||
bool parse(std::string input); |
||||
void finalize(); |
||||
std::optional<std::string> hit(int x, int y); |
||||
}; |
||||
|
||||
|
||||
Cell center(int width, int height, Cell &parent); |
||||
} |
@ -1,263 +0,0 @@ |
||||
|
||||
#line 1 "lel_parser.rl" |
||||
/***** !!!!!! THIS IS INCLUDED BY lel.cpp DO NOT PUT IN BUILD!!!!!! ******/ |
||||
|
||||
#include "lel.hpp" |
||||
#include <fmt/core.h> |
||||
#include <iostream> |
||||
|
||||
namespace lel { |
||||
|
||||
|
||||
#line 42 "lel_parser.rl" |
||||
|
||||
|
||||
|
||||
#line 12 "lel_parser.cpp" |
||||
static const char _Parser_actions[] = { |
||||
0, 1, 1, 1, 2, 1, 3, 1,
|
||||
4, 1, 5, 1, 6, 1, 9, 1,
|
||||
10, 1, 11, 1, 12, 1, 13, 2,
|
||||
0, 7, 2, 0, 8, 2, 4, 1,
|
||||
2, 4, 5 |
||||
}; |
||||
|
||||
static const char _Parser_key_offsets[] = { |
||||
0, 0, 4, 20, 33, 35, 39, 41,
|
||||
44, 56, 61 |
||||
}; |
||||
|
||||
static const char _Parser_trans_keys[] = { |
||||
32, 91, 9, 13, 32, 37, 40, 42,
|
||||
46, 61, 94, 95, 9, 13, 60, 62,
|
||||
65, 90, 97, 122, 37, 40, 42, 46,
|
||||
61, 94, 95, 60, 62, 65, 90, 97,
|
||||
122, 48, 57, 41, 44, 48, 57, 48,
|
||||
57, 41, 48, 57, 32, 93, 95, 124,
|
||||
9, 13, 48, 57, 65, 90, 97, 122,
|
||||
32, 93, 124, 9, 13, 32, 91, 9,
|
||||
13, 0 |
||||
}; |
||||
|
||||
static const char _Parser_single_lengths[] = { |
||||
0, 2, 8, 7, 0, 2, 0, 1,
|
||||
4, 3, 2 |
||||
}; |
||||
|
||||
static const char _Parser_range_lengths[] = { |
||||
0, 1, 4, 3, 1, 1, 1, 1,
|
||||
4, 1, 1 |
||||
}; |
||||
|
||||
static const char _Parser_index_offsets[] = { |
||||
0, 0, 4, 17, 28, 30, 34, 36,
|
||||
39, 48, 53 |
||||
}; |
||||
|
||||
static const char _Parser_indicies[] = { |
||||
0, 2, 0, 1, 3, 4, 5, 6,
|
||||
7, 9, 7, 10, 3, 8, 10, 10,
|
||||
1, 4, 5, 6, 7, 9, 7, 10,
|
||||
8, 10, 10, 1, 11, 1, 12, 13,
|
||||
14, 1, 15, 1, 16, 17, 1, 18,
|
||||
20, 19, 21, 18, 19, 19, 19, 1,
|
||||
22, 23, 24, 22, 1, 25, 2, 25,
|
||||
1, 0 |
||||
}; |
||||
|
||||
static const char _Parser_trans_targs[] = { |
||||
1, 0, 2, 2, 3, 4, 3, 3,
|
||||
3, 3, 8, 5, 3, 6, 5, 7,
|
||||
3, 7, 9, 8, 10, 2, 9, 10,
|
||||
2, 10 |
||||
}; |
||||
|
||||
static const char _Parser_trans_actions[] = { |
||||
0, 0, 3, 0, 17, 0, 13, 5,
|
||||
11, 15, 21, 19, 23, 23, 0, 19,
|
||||
26, 0, 7, 0, 32, 29, 0, 9,
|
||||
1, 0 |
||||
}; |
||||
|
||||
static const int Parser_start = 1; |
||||
static const int Parser_first_final = 10; |
||||
static const int Parser_error = 0; |
||||
|
||||
static const int Parser_en_main = 1; |
||||
|
||||
|
||||
#line 45 "lel_parser.rl" |
||||
|
||||
bool Parser::parse(std::string input) { |
||||
reset(); |
||||
int cs = 0; |
||||
const char *start = nullptr; |
||||
const char *begin = input.data(); |
||||
const char *p = input.data(); |
||||
const char *pe = p + input.size(); |
||||
std::string tk; |
||||
|
||||
|
||||
#line 93 "lel_parser.cpp" |
||||
{ |
||||
cs = Parser_start; |
||||
} |
||||
|
||||
#line 56 "lel_parser.rl" |
||||
|
||||
#line 96 "lel_parser.cpp" |
||||
{ |
||||
int _klen; |
||||
unsigned int _trans; |
||||
const char *_acts; |
||||
unsigned int _nacts; |
||||
const char *_keys; |
||||
|
||||
if ( p == pe ) |
||||
goto _test_eof; |
||||
if ( cs == 0 ) |
||||
goto _out; |
||||
_resume: |
||||
_keys = _Parser_trans_keys + _Parser_key_offsets[cs]; |
||||
_trans = _Parser_index_offsets[cs]; |
||||
|
||||
_klen = _Parser_single_lengths[cs]; |
||||
if ( _klen > 0 ) { |
||||
const char *_lower = _keys; |
||||
const char *_mid; |
||||
const char *_upper = _keys + _klen - 1; |
||||
while (1) { |
||||
if ( _upper < _lower ) |
||||
break; |
||||
|
||||
_mid = _lower + ((_upper-_lower) >> 1); |
||||
if ( (*p) < *_mid ) |
||||
_upper = _mid - 1; |
||||
else if ( (*p) > *_mid ) |
||||
_lower = _mid + 1; |
||||
else { |
||||
_trans += (unsigned int)(_mid - _keys); |
||||
goto _match; |
||||
} |
||||
} |
||||
_keys += _klen; |
||||
_trans += _klen; |
||||
} |
||||
|
||||
_klen = _Parser_range_lengths[cs]; |
||||
if ( _klen > 0 ) { |
||||
const char *_lower = _keys; |
||||
const char *_mid; |
||||
const char *_upper = _keys + (_klen<<1) - 2; |
||||
while (1) { |
||||
if ( _upper < _lower ) |
||||
break; |
||||
|
||||
_mid = _lower + (((_upper-_lower) >> 1) & ~1); |
||||
if ( (*p) < _mid[0] ) |
||||
_upper = _mid - 2; |
||||
else if ( (*p) > _mid[1] ) |
||||
_lower = _mid + 2; |
||||
else { |
||||
_trans += (unsigned int)((_mid - _keys)>>1); |
||||
goto _match; |
||||
} |
||||
} |
||||
_trans += _klen; |
||||
} |
||||
|
||||
_match: |
||||
_trans = _Parser_indicies[_trans]; |
||||
cs = _Parser_trans_targs[_trans]; |
||||
|
||||
if ( _Parser_trans_actions[_trans] == 0 ) |
||||
goto _again; |
||||
|
||||
_acts = _Parser_actions + _Parser_trans_actions[_trans]; |
||||
_nacts = (unsigned int) *_acts++; |
||||
while ( _nacts-- > 0 ) |
||||
{ |
||||
switch ( *_acts++ ) |
||||
{ |
||||
case 0: |
||||
#line 13 "lel_parser.rl" |
||||
{tk = input.substr(start - begin, p - start); } |
||||
break; |
||||
case 1: |
||||
#line 15 "lel_parser.rl" |
||||
{} |
||||
break; |
||||
case 2: |
||||
#line 16 "lel_parser.rl" |
||||
{ grid.push_back(Row()); } |
||||
break; |
||||
case 3: |
||||
#line 17 "lel_parser.rl" |
||||
{ cur.bottom = (*p) == '.'; } |
||||
break; |
||||
case 4: |
||||
#line 18 "lel_parser.rl" |
||||
{ id(input.substr(start - begin, p - start)); } |
||||
break; |
||||
case 5: |
||||
#line 19 "lel_parser.rl" |
||||
{ cur.col = 0; cur.row++; } |
||||
break; |
||||
case 6: |
||||
#line 20 "lel_parser.rl" |
||||
{ cur.right = (*p) == '>'; } |
||||
break; |
||||
case 7: |
||||
#line 21 "lel_parser.rl" |
||||
{cur.max_w = std::stoi(tk); } |
||||
break; |
||||
case 8: |
||||
#line 22 "lel_parser.rl" |
||||
{ cur.max_h = std::stoi(tk); } |
||||
break; |
||||
case 9: |
||||
#line 23 "lel_parser.rl" |
||||
{ cur.expand = true; } |
||||
break; |
||||
case 10: |
||||
#line 24 "lel_parser.rl" |
||||
{ cur.center = true; } |
||||
break; |
||||
case 11: |
||||
#line 25 "lel_parser.rl" |
||||
{ cur.percent = true; } |
||||
break; |
||||
case 12: |
||||
#line 35 "lel_parser.rl" |
||||
{ start = p; } |
||||
break; |
||||
case 13: |
||||
#line 38 "lel_parser.rl" |
||||
{start = p;} |
||||
break; |
||||
#line 211 "lel_parser.cpp" |
||||
} |
||||
} |
||||
|
||||
_again: |
||||
if ( cs == 0 ) |
||||
goto _out; |
||||
if ( ++p != pe ) |
||||
goto _resume; |
||||
_test_eof: {} |
||||
_out: {} |
||||
} |
||||
|
||||
#line 57 "lel_parser.rl" |
||||
|
||||
bool good = pe - p == 0; |
||||
if(good) { |
||||
finalize(); |
||||
} else { |
||||
dbc::log("error at:"); |
||||
std::cout << p; |
||||
} |
||||
return good; |
||||
} |
||||
|
||||
} |
@ -1,68 +0,0 @@ |
||||
/***** !!!!!! THIS IS INCLUDED BY lel.cpp DO NOT PUT IN BUILD!!!!!! ******/ |
||||
|
||||
#include "lel.hpp" |
||||
#include <fmt/core.h> |
||||
#include <iostream> |
||||
|
||||
namespace lel { |
||||
|
||||
%%{ |
||||
machine Parser; |
||||
alphtype char; |
||||
|
||||
action token {tk = input.substr(start - begin, fpc - start); } |
||||
|
||||
action col {} |
||||
action ltab { grid.push_back(Row()); } |
||||
action valign { cur.bottom = fc == '.'; } |
||||
action id { id(input.substr(start - begin, fpc - start)); } |
||||
action row { cur.col = 0; cur.row++; } |
||||
action align { cur.right = fc == '>'; } |
||||
action setwidth {cur.max_w = std::stoi(tk); } |
||||
action setheight { cur.max_h = std::stoi(tk); } |
||||
action expand { cur.expand = true; } |
||||
action center { cur.center = true; } |
||||
action percent { cur.percent = true; } |
||||
|
||||
col = "|" $col; |
||||
ltab = "[" $ltab; |
||||
rtab = "]" $row; |
||||
valign = ("^" | ".") $valign; |
||||
expand = "*" $expand; |
||||
center = "=" $center; |
||||
percent = "%" $percent; |
||||
halign = ("<" | ">") $align; |
||||
number = digit+ >{ start = fpc; } %token; |
||||
setw = ("(" number %setwidth ("," number %setheight)? ")") ; |
||||
modifiers = (percent | center | expand | valign | halign | setw); |
||||
id = modifiers* ((alpha | '_')+ :>> (alnum | '_')*) >{start = fpc;} %id; |
||||
row = space* ltab space* id space* (col space* id space*)* space* rtab space*; |
||||
|
||||
main := row+; |
||||
}%% |
||||
|
||||
%% write data; |
||||
|
||||
bool Parser::parse(std::string input) { |
||||
reset(); |
||||
int cs = 0; |
||||
const char *start = nullptr; |
||||
const char *begin = input.data(); |
||||
const char *p = input.data(); |
||||
const char *pe = p + input.size(); |
||||
std::string tk; |
||||
|
||||
%% write init; |
||||
%% write exec; |
||||
|
||||
bool good = pe - p == 0; |
||||
if(good) { |
||||
finalize(); |
||||
} else { |
||||
dbc::log("error at:"); |
||||
std::cout << p; |
||||
} |
||||
return good; |
||||
} |
||||
|
||||
} |
@ -1,39 +0,0 @@ |
||||
#include <catch2/catch_test_macros.hpp> |
||||
#include "dbc.hpp" |
||||
|
||||
using namespace dbc; |
||||
|
||||
TEST_CASE("basic feature tests", "[utils]") { |
||||
log("Logging a message."); |
||||
|
||||
try { |
||||
sentinel("This shouldn't happen."); |
||||
} catch(SentinelError) { |
||||
log("Sentinel happened."); |
||||
} |
||||
|
||||
pre("confirm positive cases work", 1 == 1); |
||||
pre("confirm positive lambda", [&]{ return 1 == 1;}); |
||||
post("confirm positive post", 1 == 1); |
||||
post("confirm postitive post with lamdba", [&]{ return 1 == 1;}); |
||||
|
||||
check(1 == 1, "one equals 1"); |
||||
|
||||
try { |
||||
check(1 == 2, "this should fail"); |
||||
} catch(CheckError err) { |
||||
log("check fail worked"); |
||||
} |
||||
|
||||
try { |
||||
pre("failing pre", 1 == 3); |
||||
} catch(PreCondError err) { |
||||
log("pre fail worked"); |
||||
} |
||||
|
||||
try { |
||||
post("failing post", 1 == 4); |
||||
} catch(PostCondError err) { |
||||
log("post faile worked"); |
||||
} |
||||
} |
@ -0,0 +1,9 @@ |
||||
[wrap-git] |
||||
directory=lel-guecs-0.2.0 |
||||
url=https://git.learnjsthehardway.com/learn-code-the-hard-way/lel-guecs.git |
||||
revision=HEAD |
||||
depth=1 |
||||
method=meson |
||||
|
||||
[provide] |
||||
lel_guecs = lel_guecs_dep |
Loading…
Reference in new issue