parent
7c90eb6da1
commit
74a8599977
@ -0,0 +1,67 @@ |
||||
#include "backend.hpp" |
||||
#include "shaders.hpp" |
||||
#include "sound.hpp" |
||||
#include "textures.hpp" |
||||
#include "config.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={0, 0, 0}, |
||||
.DARK_DARK={10, 10, 10}, |
||||
.DARK_MID={30, 30, 30}, |
||||
.DARK_LIGHT={60, 60, 60}, |
||||
.MID={100, 100, 100}, |
||||
.LIGHT_DARK={150, 150, 150}, |
||||
.LIGHT_MID={200, 200, 200}, |
||||
.LIGHT_LIGHT={230, 230, 230}, |
||||
.WHITE={255, 255, 255}, |
||||
.TRANSPARENT = sf::Color::Transparent |
||||
}; |
||||
|
||||
theme.PADDING = 3; |
||||
theme.BORDER_PX = 1; |
||||
theme.TEXT_SIZE = 20; |
||||
theme.LABEL_SIZE = 20; |
||||
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 = Config::path_to("assets/text.otf").string(); |
||||
|
||||
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,354 +0,0 @@ |
||||
#include "guecs.hpp" |
||||
#include "shaders.hpp" |
||||
#include "sound.hpp" |
||||
#include "textures.hpp" |
||||
#include <typeinfo> |
||||
|
||||
namespace guecs { |
||||
using std::make_shared; |
||||
|
||||
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(const wstring& new_content) { |
||||
content = new_content; |
||||
text->setString(content); |
||||
} |
||||
|
||||
void Sprite::update(const string& new_name) { |
||||
if(new_name != name) { |
||||
name = new_name; |
||||
auto sprite_texture = textures::get(name); |
||||
sprite->setTexture(*sprite_texture.texture); |
||||
sprite->setTextureRect(sprite_texture.sprite->getTextureRect()); |
||||
} |
||||
} |
||||
|
||||
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 Meter::render(lel::Cell& cell) { |
||||
float level = std::clamp(percent, 0.0f, 1.0f) * float(cell.w); |
||||
// ZED: this 6 is a border width, make it a thing
|
||||
bar.shape->setSize({std::max(level, 0.0f), float(cell.h - 6)}); |
||||
} |
||||
|
||||
void Sound::play(bool hover) { |
||||
if(!hover) { |
||||
sound::play(on_click); |
||||
} |
||||
} |
||||
|
||||
void Sound::stop(bool hover) { |
||||
if(!hover) { |
||||
sound::stop(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; |
||||
} |
||||
|
||||
void Effect::stop() { |
||||
$active = false; |
||||
} |
||||
|
||||
UI::UI() { |
||||
$font = make_shared<sf::Font>(FONT_FILE_NAME); |
||||
} |
||||
|
||||
void UI::position(int x, int y, int width, int height) { |
||||
$parser.position(x, y, width, height); |
||||
} |
||||
|
||||
sf::Vector2f UI::get_position() { |
||||
return {float($parser.grid_x), float($parser.grid_y)}; |
||||
} |
||||
|
||||
sf::Vector2f UI::get_size() { |
||||
return {float($parser.grid_w), float($parser.grid_h)}; |
||||
} |
||||
|
||||
void UI::layout(const 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); |
||||
set<lel::Cell>(ent, cell); |
||||
} |
||||
} |
||||
|
||||
Entity UI::init_entity(const string& name) { |
||||
auto ent = entity(); |
||||
// this lets you look up an entity by name
|
||||
$name_ents.insert_or_assign(name, ent); |
||||
// this makes it easier to get the name during querying
|
||||
set<CellName>(ent, {name}); |
||||
return ent; |
||||
} |
||||
|
||||
Entity UI::entity(const string& name) { |
||||
dbc::check($name_ents.contains(name), |
||||
fmt::format("GUECS entity {} does not exist. Mispelled cell name?", name)); |
||||
return $name_ents.at(name); |
||||
} |
||||
|
||||
Entity UI::entity(const string& name, int id) { |
||||
return entity(fmt::format("{}{}", name, id)); |
||||
} |
||||
|
||||
void UI::init() { |
||||
query<Background>([](auto, auto& bg) { |
||||
bg.init(); |
||||
}); |
||||
|
||||
query<lel::Cell, Rectangle>([](auto, auto& cell, auto& rect) { |
||||
rect.init(cell); |
||||
}); |
||||
|
||||
query<lel::Cell, Effect>([](auto, auto& cell, auto& shader) { |
||||
shader.init(cell); |
||||
}); |
||||
|
||||
query<Rectangle, Meter>([](auto, auto& bg, auto &meter) { |
||||
bg.shape->setFillColor(meter.color); |
||||
}); |
||||
|
||||
query<lel::Cell, Meter>([](auto, auto &cell, auto& meter) { |
||||
meter.init(cell); |
||||
}); |
||||
|
||||
query<lel::Cell, Textual>([this](auto, auto& cell, auto& text) { |
||||
text.init(cell, $font); |
||||
}); |
||||
|
||||
query<lel::Cell, Label>([this](auto, auto& cell, auto& text) { |
||||
text.init(cell, $font); |
||||
}); |
||||
|
||||
query<lel::Cell, Sprite>([&](auto, auto &cell, auto &sprite) { |
||||
sprite.init(cell); |
||||
}); |
||||
} |
||||
|
||||
void UI::debug_layout(sf::RenderWindow& window) { |
||||
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(auto bg = get_if<Background>(MAIN)) { |
||||
window.draw(*(bg->shape)); |
||||
} |
||||
|
||||
query<Effect>([&](auto, auto& shader) { |
||||
if(shader.$active) shader.step(); |
||||
}); |
||||
|
||||
query<Rectangle>([&](auto ent, auto& rect) { |
||||
render_helper(window, ent, true, rect.shape); |
||||
}); |
||||
|
||||
query<lel::Cell, Meter>([&](auto ent, auto& cell, auto &meter) { |
||||
meter.render(cell); |
||||
render_helper(window, ent, true, meter.bar.shape); |
||||
}); |
||||
|
||||
query<Sprite>([&](auto ent, auto& sprite) { |
||||
render_helper(window, ent, false, sprite.sprite); |
||||
}); |
||||
|
||||
query<Label>([&](auto ent, auto& text) { |
||||
render_helper(window, ent, false, text.text); |
||||
}); |
||||
|
||||
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; |
||||
|
||||
query<lel::Cell>([&](auto ent, auto& cell) { |
||||
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; |
||||
|
||||
click_on(ent); |
||||
|
||||
action_count++; |
||||
} else { |
||||
do_if<Effect>(ent, [hover](auto& effect) { |
||||
effect.stop(); |
||||
}); |
||||
|
||||
do_if<Sound>(ent, [hover](auto& sound) { |
||||
// here set that it played then only play once
|
||||
sound.stop(hover); |
||||
}); |
||||
} |
||||
}); |
||||
|
||||
return action_count > 0; |
||||
} |
||||
|
||||
void UI::show_sprite(const string& region, const string& sprite_name) { |
||||
auto ent = entity(region); |
||||
|
||||
if(auto sprite = get_if<Sprite>(ent)) { |
||||
sprite->update(sprite_name); |
||||
} else { |
||||
set_init<Sprite>(ent, {sprite_name}); |
||||
} |
||||
} |
||||
|
||||
void UI::show_text(const string& region, const wstring& content) { |
||||
auto ent = entity(region); |
||||
|
||||
if(auto tc = get_if<Textual>(ent)) { |
||||
tc->text->setString(content); |
||||
} else { |
||||
auto &cell = cell_for(ent); |
||||
Textual to_set{content, TEXT_SIZE}; |
||||
to_set.init(cell, $font); |
||||
set<Textual>(ent, to_set); |
||||
} |
||||
} |
||||
|
||||
void UI::click_on(const string& name, bool required) { |
||||
auto ent = entity(name); |
||||
|
||||
if(required) { |
||||
dbc::check(has<Clickable>(ent), |
||||
fmt::format("click_on required '{}' to exist but it doesn't", name)); |
||||
} |
||||
|
||||
click_on(ent); |
||||
} |
||||
|
||||
void UI::click_on(Entity ent) { |
||||
if(auto clicked = get_if<Clickable>(ent)) { |
||||
if(auto action_data = get_if<ActionData>(ent)) { |
||||
clicked->action(ent, action_data->data); |
||||
} else { |
||||
clicked->action(ent, {}); |
||||
} |
||||
} |
||||
} |
||||
|
||||
void UI::show_label(const string& region, const wstring& content) { |
||||
auto ent = entity(region); |
||||
|
||||
if(auto tc = get_if<Label>(ent)) { |
||||
tc->text->setString(content); |
||||
} else { |
||||
auto &cell = cell_for(ent); |
||||
Label to_set{content, LABEL_SIZE}; |
||||
to_set.init(cell, $font); |
||||
to_set.text->setFillColor(TEXT_COLOR); |
||||
set<Label>(ent, to_set); |
||||
} |
||||
} |
||||
} |
@ -1,340 +0,0 @@ |
||||
#pragma once |
||||
#include "dbc.hpp" |
||||
#include "color.hpp" |
||||
#include "lel.hpp" |
||||
#include <string> |
||||
#include <memory> |
||||
#include <SFML/Graphics.hpp> |
||||
#include <functional> |
||||
#include "events.hpp" |
||||
#include <any> |
||||
#include <queue> |
||||
#include <typeindex> |
||||
#include <unordered_map> |
||||
|
||||
namespace guecs { |
||||
constexpr const int PADDING = 3; |
||||
constexpr const int BORDER_PX = 1; |
||||
constexpr const int TEXT_SIZE = 30; |
||||
constexpr const int LABEL_SIZE = 20; |
||||
constexpr const sf::Color FILL_COLOR = ColorValue::DARK_MID; |
||||
constexpr const sf::Color TEXT_COLOR = ColorValue::LIGHT_LIGHT; |
||||
constexpr const sf::Color BG_COLOR = ColorValue::MID; |
||||
constexpr const sf::Color BORDER_COLOR = ColorValue::MID; |
||||
constexpr const char *FONT_FILE_NAME="assets/text.otf"; |
||||
|
||||
using std::shared_ptr, std::wstring, std::string; |
||||
|
||||
using Entity = unsigned long; |
||||
|
||||
using EntityMap = std::unordered_map<Entity, size_t>; |
||||
|
||||
template <typename T> |
||||
struct ComponentStorage { |
||||
std::vector<T> data; |
||||
std::queue<size_t> free_indices; |
||||
}; |
||||
|
||||
struct Textual { |
||||
std::wstring content; |
||||
unsigned int size = TEXT_SIZE; |
||||
sf::Color color = TEXT_COLOR; |
||||
int padding = 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(const std::wstring& new_content); |
||||
}; |
||||
|
||||
struct Label : public Textual { |
||||
template<typename... Args> |
||||
Label(Args... args) : Textual(args...) |
||||
{ |
||||
centered = true; |
||||
size = LABEL_SIZE; |
||||
} |
||||
|
||||
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(Entity ent, std::any data)> action; |
||||
}; |
||||
|
||||
struct Sprite { |
||||
string name; |
||||
int padding = PADDING; |
||||
std::shared_ptr<sf::Sprite> sprite = nullptr; |
||||
|
||||
void init(lel::Cell &cell); |
||||
void update(const string& new_name); |
||||
}; |
||||
|
||||
struct Rectangle { |
||||
int padding = PADDING; |
||||
sf::Color color = FILL_COLOR; |
||||
sf::Color border_color = BORDER_COLOR; |
||||
int border_px = BORDER_PX; |
||||
shared_ptr<sf::RectangleShape> shape = nullptr; |
||||
|
||||
void init(lel::Cell& cell); |
||||
}; |
||||
|
||||
struct Meter { |
||||
float percent = 1.0f; |
||||
sf::Color color = ColorValue::BLACK; |
||||
Rectangle bar; |
||||
|
||||
void init(lel::Cell& cell); |
||||
void render(lel::Cell& cell); |
||||
}; |
||||
|
||||
struct ActionData { |
||||
std::any data; |
||||
}; |
||||
|
||||
struct CellName { |
||||
string name; |
||||
}; |
||||
|
||||
struct Effect { |
||||
float duration = 0.1f; |
||||
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 stop(); |
||||
void step(); |
||||
shared_ptr<sf::Shader> checkout_ptr(); |
||||
}; |
||||
|
||||
struct Sound { |
||||
string on_click{"ui_click"}; |
||||
void play(bool hover); |
||||
void stop(bool hover); |
||||
}; |
||||
|
||||
struct Background { |
||||
float x = 0.0f; |
||||
float y = 0.0f; |
||||
float w = 0.0f; |
||||
float h = 0.0f; |
||||
sf::Color color = BG_COLOR; |
||||
shared_ptr<sf::RectangleShape> shape = nullptr; |
||||
|
||||
Background(lel::Parser& parser, sf::Color bg_color=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: |
||||
Entity MAIN = 0; |
||||
unsigned long entity_count = 1; |
||||
std::unordered_map<std::type_index, EntityMap> $components; |
||||
std::unordered_map<std::type_index, std::any> $component_storages; |
||||
std::unordered_map<string, Entity> $name_ents; |
||||
shared_ptr<sf::Font> $font = nullptr; |
||||
lel::Parser $parser; |
||||
string $grid = ""; |
||||
|
||||
UI(); |
||||
|
||||
void position(int x, int y, int width, int height); |
||||
sf::Vector2f get_position(); |
||||
sf::Vector2f get_size(); |
||||
void layout(const string& grid); |
||||
Entity init_entity(const string& name); |
||||
Entity entity(const string& name); |
||||
Entity entity(const string& name, int id); |
||||
|
||||
inline lel::CellMap& cells() { |
||||
return $parser.cells; |
||||
} |
||||
|
||||
void init(); |
||||
void render(sf::RenderWindow& window); |
||||
bool mouse(float x, float y, bool hover); |
||||
void click_on(const string& name, bool required=false); |
||||
void click_on(Entity slot_id); |
||||
void debug_layout(sf::RenderWindow& window); |
||||
|
||||
Entity entity() { return ++entity_count; } |
||||
|
||||
template <typename Comp> |
||||
size_t make_component() { |
||||
auto &storage = component_storage_for<Comp>(); |
||||
size_t index; |
||||
|
||||
if(!storage.free_indices.empty()) { |
||||
index = storage.free_indices.front(); |
||||
storage.free_indices.pop(); |
||||
} else { |
||||
storage.data.emplace_back(); |
||||
index = storage.data.size() - 1; |
||||
} |
||||
|
||||
return index; |
||||
} |
||||
|
||||
template <typename Comp> |
||||
ComponentStorage<Comp> &component_storage_for() { |
||||
auto type_index = std::type_index(typeid(Comp)); |
||||
$component_storages.try_emplace(type_index, ComponentStorage<Comp>{}); |
||||
return std::any_cast<ComponentStorage<Comp> &>( |
||||
$component_storages.at(type_index)); |
||||
} |
||||
|
||||
template <typename Comp> |
||||
EntityMap &entity_map_for() { |
||||
return $components[std::type_index(typeid(Comp))]; |
||||
} |
||||
|
||||
template <typename Comp> |
||||
void set(Entity ent, Comp val) { |
||||
EntityMap &map = entity_map_for<Comp>(); |
||||
|
||||
if(has<Comp>(ent)) { |
||||
get<Comp>(ent) = val; |
||||
return; |
||||
} |
||||
|
||||
map.insert_or_assign(ent, make_component<Comp>()); |
||||
get<Comp>(ent) = val; |
||||
} |
||||
|
||||
template <typename Comp> |
||||
Comp& get(Entity ent) { |
||||
EntityMap &map = entity_map_for<Comp>(); |
||||
auto &storage = component_storage_for<Comp>(); |
||||
auto index = map.at(ent); |
||||
return storage.data[index]; |
||||
} |
||||
|
||||
template <typename Comp> |
||||
Comp* get_if(Entity entity) { |
||||
EntityMap &map = entity_map_for<Comp>(); |
||||
auto &storage = component_storage_for<Comp>(); |
||||
if(map.contains(entity)) { |
||||
auto index = map.at(entity); |
||||
return &storage.data[index]; |
||||
} else { |
||||
return nullptr; |
||||
} |
||||
} |
||||
|
||||
template <typename Comp> |
||||
bool has(Entity ent) { |
||||
EntityMap &map = entity_map_for<Comp>(); |
||||
return map.contains(ent); |
||||
} |
||||
|
||||
template <typename Comp> |
||||
void remove(Entity ent) { |
||||
EntityMap &map = entity_map_for<Comp>(); |
||||
|
||||
if(map.contains(ent)) { |
||||
size_t index = map.at(ent); |
||||
component_storage_for<Comp>().free_indices.push(index); |
||||
} |
||||
|
||||
map.erase(ent); |
||||
} |
||||
|
||||
template <typename Comp> |
||||
void query(std::function<void(Entity, Comp &)> cb) { |
||||
EntityMap &map = entity_map_for<Comp>(); |
||||
|
||||
for(auto &[entity, index] : map) { |
||||
cb(entity, get<Comp>(entity)); |
||||
} |
||||
} |
||||
|
||||
template <typename CompA, typename CompB> |
||||
void query(std::function<void(Entity, CompA &, CompB &)> cb) { |
||||
EntityMap &map_a = entity_map_for<CompA>(); |
||||
EntityMap &map_b = entity_map_for<CompB>(); |
||||
|
||||
for(auto &[entity, index_a] : map_a) { |
||||
if(map_b.contains(entity)) { |
||||
cb(entity, get<CompA>(entity), get<CompB>(entity)); |
||||
} |
||||
} |
||||
} |
||||
|
||||
template <typename Comp> |
||||
void set_init(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); |
||||
set<Comp>(ent, val); |
||||
} |
||||
|
||||
template <typename Comp> |
||||
void do_if(Entity ent, std::function<void(Comp &)> cb) { |
||||
if(has<Comp>(ent)) { |
||||
cb(get<Comp>(ent)); |
||||
} |
||||
} |
||||
|
||||
lel::Cell& cell_for(Entity ent) { |
||||
return get<lel::Cell>(ent); |
||||
} |
||||
|
||||
lel::Cell& cell_for(const string& name) { |
||||
Entity ent = entity(name); |
||||
return get<lel::Cell>(ent); |
||||
} |
||||
|
||||
|
||||
// BUG: close could just be remove with overload
|
||||
template <typename Comp> |
||||
void close(string region) { |
||||
auto ent = entity(region); |
||||
if(has<Comp>(ent)) { |
||||
remove<Comp>(ent); |
||||
} |
||||
} |
||||
|
||||
template<typename T> |
||||
void render_helper(sf::RenderWindow& window, Entity ent, bool is_shape, T& target) { |
||||
sf::Shader *shader_ptr = nullptr; |
||||
|
||||
if(auto shader = get_if<Effect>(ent)) { |
||||
if(shader->$active && !is_shape) { |
||||
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(const string& region, const string& sprite_name); |
||||
void show_text(const string& region, const wstring& content); |
||||
void show_label(const string& region, const wstring& content); |
||||
}; |
||||
} |
@ -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,261 +0,0 @@ |
||||
|
||||
#line 1 "lel_parser.rl" |
||||
#include "lel.hpp" |
||||
#include <fmt/core.h> |
||||
#include <iostream> |
||||
|
||||
namespace lel { |
||||
|
||||
|
||||
#line 40 "lel_parser.rl" |
||||
|
||||
|
||||
|
||||
#line 10 "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 43 "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 91 "lel_parser.cpp" |
||||
{ |
||||
cs = Parser_start; |
||||
} |
||||
|
||||
#line 54 "lel_parser.rl" |
||||
|
||||
#line 94 "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 11 "lel_parser.rl" |
||||
{tk = input.substr(start - begin, p - start); } |
||||
break; |
||||
case 1: |
||||
#line 13 "lel_parser.rl" |
||||
{} |
||||
break; |
||||
case 2: |
||||
#line 14 "lel_parser.rl" |
||||
{ grid.push_back(Row()); } |
||||
break; |
||||
case 3: |
||||
#line 15 "lel_parser.rl" |
||||
{ cur.bottom = (*p) == '.'; } |
||||
break; |
||||
case 4: |
||||
#line 16 "lel_parser.rl" |
||||
{ id(input.substr(start - begin, p - start)); } |
||||
break; |
||||
case 5: |
||||
#line 17 "lel_parser.rl" |
||||
{ cur.col = 0; cur.row++; } |
||||
break; |
||||
case 6: |
||||
#line 18 "lel_parser.rl" |
||||
{ cur.right = (*p) == '>'; } |
||||
break; |
||||
case 7: |
||||
#line 19 "lel_parser.rl" |
||||
{cur.max_w = std::stoi(tk); } |
||||
break; |
||||
case 8: |
||||
#line 20 "lel_parser.rl" |
||||
{ cur.max_h = std::stoi(tk); } |
||||
break; |
||||
case 9: |
||||
#line 21 "lel_parser.rl" |
||||
{ cur.expand = true; } |
||||
break; |
||||
case 10: |
||||
#line 22 "lel_parser.rl" |
||||
{ cur.center = true; } |
||||
break; |
||||
case 11: |
||||
#line 23 "lel_parser.rl" |
||||
{ cur.percent = true; } |
||||
break; |
||||
case 12: |
||||
#line 33 "lel_parser.rl" |
||||
{ start = p; } |
||||
break; |
||||
case 13: |
||||
#line 36 "lel_parser.rl" |
||||
{start = p;} |
||||
break; |
||||
#line 209 "lel_parser.cpp" |
||||
} |
||||
} |
||||
|
||||
_again: |
||||
if ( cs == 0 ) |
||||
goto _out; |
||||
if ( ++p != pe ) |
||||
goto _resume; |
||||
_test_eof: {} |
||||
_out: {} |
||||
} |
||||
|
||||
#line 55 "lel_parser.rl" |
||||
|
||||
bool good = pe - p == 0; |
||||
if(good) { |
||||
finalize(); |
||||
} else { |
||||
dbc::log("error at:"); |
||||
std::cout << p; |
||||
} |
||||
return good; |
||||
} |
||||
|
||||
} |
@ -1,66 +0,0 @@ |
||||
#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,32 +0,0 @@ |
||||
#include <catch2/catch_test_macros.hpp> |
||||
#include <fmt/core.h> |
||||
#include "constants.hpp" |
||||
#include "guecs.hpp" |
||||
#include "textures.hpp" |
||||
#include <fmt/xchar.h> |
||||
|
||||
using namespace guecs; |
||||
|
||||
TEST_CASE("prototype one gui", "[ecs-gui]") { |
||||
guecs::UI gui; |
||||
textures::init(); |
||||
|
||||
gui.position(0, 0, 1000, 500); |
||||
gui.layout("[test1|test2|test3][test4|_|test5]"); |
||||
|
||||
for(auto& [name, cell] : gui.cells()) { |
||||
auto button = gui.entity(name); |
||||
gui.set<lel::Cell>(button, cell); |
||||
gui.set<Rectangle>(button, {}); |
||||
gui.set<Clickable>(button, {}); |
||||
gui.set<Textual>(button, {L"whatever"}); |
||||
} |
||||
|
||||
gui.init(); |
||||
|
||||
// at this point it's mostly ready but I'd need to render it to a window real quick
|
||||
sf::RenderWindow window; |
||||
window.setSize({SCREEN_WIDTH, SCREEN_HEIGHT}); |
||||
gui.render(window); |
||||
window.display(); |
||||
} |
@ -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