Compare commits
38 Commits
version-0.
...
master
After Width: | Height: | Size: 51 KiB |
After Width: | Height: | Size: 33 KiB |
After Width: | Height: | Size: 10 KiB |
After Width: | Height: | Size: 4.2 KiB |
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
@ -0,0 +1,86 @@ |
|||||||
|
#include "backend.hpp" |
||||||
|
#include "shaders.hpp" |
||||||
|
#include "sound.hpp" |
||||||
|
#include "textures.hpp" |
||||||
|
#include "config.hpp" |
||||||
|
|
||||||
|
namespace sfml { |
||||||
|
using namespace nlohmann; |
||||||
|
|
||||||
|
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; |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
inline sf::Color to_color(json& config, const std::string& name) { |
||||||
|
json& val = config[name]; |
||||||
|
if(val.type() == json::value_t::array) { |
||||||
|
return sf::Color{val[0], val[1], val[2], val[3]}; |
||||||
|
} else if(val.type() == json::value_t::string) { |
||||||
|
json& array = config[val]; |
||||||
|
return sf::Color{array[0], array[1], array[2], array[3]}; |
||||||
|
} else { |
||||||
|
dbc::sentinel(fmt::format( |
||||||
|
"theme config {} has invalid color setting," |
||||||
|
"either use an array of 4 ints or a string" |
||||||
|
"referencing another config with 4 ints.", name)); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
guecs::Theme Backend::theme() { |
||||||
|
auto config = Config("assets/config.json")["theme"]; |
||||||
|
|
||||||
|
guecs::Theme theme { |
||||||
|
.BLACK=to_color(config, "black"), |
||||||
|
.DARK_DARK=to_color(config, "dark_dark"), |
||||||
|
.DARK_MID=to_color(config, "dark_mid"), |
||||||
|
.DARK_LIGHT=to_color(config, "dark_light"), |
||||||
|
.MID=to_color(config, "mid"), |
||||||
|
.LIGHT_DARK=to_color(config, "light_dark"), |
||||||
|
.LIGHT_MID=to_color(config, "light_mid"), |
||||||
|
.LIGHT_LIGHT=to_color(config, "light_light"), |
||||||
|
.WHITE=to_color(config, "white"), |
||||||
|
.TRANSPARENT = sf::Color::Transparent |
||||||
|
}; |
||||||
|
|
||||||
|
theme.PADDING = config["padding"]; |
||||||
|
theme.BORDER_PX = config["border_px"]; |
||||||
|
theme.TEXT_SIZE = config["text_size"]; |
||||||
|
theme.LABEL_SIZE = config["label_size"]; |
||||||
|
theme.FILL_COLOR = to_color(config, "fill_color"); |
||||||
|
theme.TEXT_COLOR = to_color(config, "text_color"); |
||||||
|
theme.BG_COLOR = to_color(config, "bg_color"); |
||||||
|
theme.BORDER_COLOR = to_color(config, "border_color"); |
||||||
|
theme.BG_COLOR_DARK = to_color(config, "bg_color_dark"); |
||||||
|
theme.FONT_FILE_NAME = Config::path_to(config["font_file_name"]).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,15 +0,0 @@ |
|||||||
#pragma once |
|
||||||
#include <SFML/Graphics/Color.hpp> |
|
||||||
|
|
||||||
namespace ColorValue { |
|
||||||
const sf::Color BLACK{0, 0, 0}; |
|
||||||
const sf::Color DARK_DARK{10, 10, 10}; |
|
||||||
const sf::Color DARK_MID{30, 30, 30}; |
|
||||||
const sf::Color DARK_LIGHT{60, 60, 60}; |
|
||||||
const sf::Color MID{100, 100, 100}; |
|
||||||
const sf::Color LIGHT_DARK{150, 150, 150}; |
|
||||||
const sf::Color LIGHT_MID{200, 200, 200}; |
|
||||||
const sf::Color LIGHT_LIGHT{230, 230, 230}; |
|
||||||
const sf::Color WHITE{255, 255, 255}; |
|
||||||
const sf::Color TRANSPARENT = sf::Color::Transparent; |
|
||||||
} |
|
@ -1,317 +0,0 @@ |
|||||||
#include "guecs.hpp" |
|
||||||
#include "shaders.hpp" |
|
||||||
#include "sound.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>(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& shader) { |
|
||||||
shader.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) { |
|
||||||
float level = std::clamp(meter.percent, 0.0f, 1.0f) * float(cell.w); |
|
||||||
// ZED: this 6 is a border width, make it a thing
|
|
||||||
meter.bar.shape->setSize({std::max(level, 0.0f), float(cell.h - 6)}); |
|
||||||
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)) { |
|
||||||
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); |
|
||||||
}}; |
|
||||||
} |
|
||||||
|
|
||||||
Clickable make_action(DinkyECS::World& target, Events::GUI event, std::any data) { |
|
||||||
return {[&, event, data](auto ent, auto){ |
|
||||||
// remember that ent is passed in from the UI::mouse handler
|
|
||||||
target.send<Events::GUI>(event, ent, data); |
|
||||||
}}; |
|
||||||
} |
|
||||||
|
|
||||||
} |
|
@ -1,237 +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 "components.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 && !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(string region, string sprite_name); |
|
||||||
void show_text(string region, wstring content); |
|
||||||
void update_text(string region, wstring content); |
|
||||||
void update_label(string region, wstring content); |
|
||||||
void show_label(string region, wstring content); |
|
||||||
}; |
|
||||||
|
|
||||||
Clickable make_action(DinkyECS::World& target, Events::GUI event); |
|
||||||
Clickable make_action(DinkyECS::World& target, Events::GUI event, std::any data); |
|
||||||
|
|
||||||
} |
|
@ -1,7 +1,7 @@ |
|||||||
#pragma once |
#pragma once |
||||||
#include <SFML/Graphics/RenderWindow.hpp> |
#include <SFML/Graphics/RenderWindow.hpp> |
||||||
#include <SFML/Graphics/Font.hpp> |
#include <SFML/Graphics/Font.hpp> |
||||||
#include "guecs.hpp" |
#include <guecs/ui.hpp> |
||||||
#include "textures.hpp" |
#include "textures.hpp" |
||||||
#include "components.hpp" |
#include "components.hpp" |
||||||
#include <SFML/System/Clock.hpp> |
#include <SFML/System/Clock.hpp> |
@ -0,0 +1,18 @@ |
|||||||
|
#include "gui/guecstra.hpp" |
||||||
|
|
||||||
|
namespace guecs { |
||||||
|
|
||||||
|
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); |
||||||
|
}}; |
||||||
|
} |
||||||
|
|
||||||
|
Clickable make_action(DinkyECS::World& target, Events::GUI event, std::any data) { |
||||||
|
return {[&, event, data](auto ent, auto){ |
||||||
|
// remember that ent is passed in from the UI::mouse handler
|
||||||
|
target.send<Events::GUI>(event, ent, data); |
||||||
|
}}; |
||||||
|
} |
||||||
|
} |
@ -0,0 +1,8 @@ |
|||||||
|
#include "components.hpp" |
||||||
|
#include "events.hpp" |
||||||
|
#include <guecs/ui.hpp> |
||||||
|
|
||||||
|
namespace guecs { |
||||||
|
Clickable make_action(DinkyECS::World& target, Events::GUI event); |
||||||
|
Clickable make_action(DinkyECS::World& target, Events::GUI event, std::any data); |
||||||
|
} |
@ -0,0 +1,65 @@ |
|||||||
|
#include "gui/loot_ui.hpp" |
||||||
|
#include "constants.hpp" |
||||||
|
#include <fmt/xchar.h> |
||||||
|
#include "gui/guecstra.hpp" |
||||||
|
|
||||||
|
namespace gui { |
||||||
|
using namespace guecs; |
||||||
|
|
||||||
|
LootUI::LootUI(GameLevel level) : |
||||||
|
$level(level) |
||||||
|
{ |
||||||
|
$gui.position(RAY_VIEW_X+RAY_VIEW_WIDTH/2-200, |
||||||
|
RAY_VIEW_Y+RAY_VIEW_HEIGHT/2-200, 400, 400); |
||||||
|
|
||||||
|
$gui.layout( |
||||||
|
"[item_0 | item_1 |item_2 | item_3 ]" |
||||||
|
"[item_4 | item_5 |item_6 | item_7 ]" |
||||||
|
"[item_8 | item_9 |item_10| item_11]" |
||||||
|
"[item_12| item_13|item_14|item_15 ]" |
||||||
|
"[_ | %(100,50)close| _]" |
||||||
|
); |
||||||
|
} |
||||||
|
|
||||||
|
void LootUI::init() { |
||||||
|
using guecs::THEME; |
||||||
|
auto bg_color = THEME.DARK_LIGHT; |
||||||
|
bg_color.a = 140; |
||||||
|
$gui.set<Background>($gui.MAIN, {$gui.$parser, bg_color}); |
||||||
|
|
||||||
|
// fill in 4 slots for prototype
|
||||||
|
for(int i = 0; i < 4; i++) { |
||||||
|
auto id = $gui.entity("item_", i); |
||||||
|
|
||||||
|
$gui.set<guecs::Rectangle>(id, {THEME.PADDING, |
||||||
|
THEME.TRANSPARENT, THEME.LIGHT_MID }); |
||||||
|
|
||||||
|
$gui.set<guecs::Effect>(id, {0.4f, "ui_shader"}); |
||||||
|
$gui.set<guecs::Clickable>(id, { |
||||||
|
[=](auto, auto) { fmt::println("clicked button_{}", i); } |
||||||
|
}); |
||||||
|
$gui.set<guecs::Sprite>(id, {"broken_yoyo-64"}); |
||||||
|
} |
||||||
|
|
||||||
|
auto close = $gui.entity("close"); |
||||||
|
$gui.set<guecs::Rectangle>(close, {}); |
||||||
|
$gui.set<guecs::Label>(close, {L"CLOSE"}); |
||||||
|
$gui.set<guecs::Clickable>(close, |
||||||
|
guecs::make_action(*$level.world, Events::GUI::LOOT_CLOSE)); |
||||||
|
|
||||||
|
$gui.init(); |
||||||
|
} |
||||||
|
|
||||||
|
void LootUI::render(sf::RenderWindow& window) { |
||||||
|
$gui.render(window); |
||||||
|
} |
||||||
|
|
||||||
|
void LootUI::update_level(GameLevel &level) { |
||||||
|
$level = level; |
||||||
|
init(); |
||||||
|
} |
||||||
|
|
||||||
|
bool LootUI::mouse(float x, float y, bool hover) { |
||||||
|
return $gui.mouse(x, y, hover); |
||||||
|
} |
||||||
|
} |
@ -0,0 +1,22 @@ |
|||||||
|
#pragma once |
||||||
|
#include "levelmanager.hpp" |
||||||
|
#include <SFML/Graphics/RenderWindow.hpp> |
||||||
|
#include <SFML/Graphics/Font.hpp> |
||||||
|
#include <guecs/ui.hpp> |
||||||
|
#include "events.hpp" |
||||||
|
|
||||||
|
namespace gui { |
||||||
|
class LootUI { |
||||||
|
public: |
||||||
|
bool active = false; |
||||||
|
guecs::UI $gui; |
||||||
|
GameLevel $level; |
||||||
|
|
||||||
|
LootUI(GameLevel level); |
||||||
|
|
||||||
|
void init(); |
||||||
|
void render(sf::RenderWindow& window); |
||||||
|
void update_level(GameLevel &level); |
||||||
|
bool mouse(float x, float y, bool hover); |
||||||
|
}; |
||||||
|
} |
@ -1,19 +1,24 @@ |
|||||||
#pragma once |
#pragma once |
||||||
#include "levelmanager.hpp" |
#include "levelmanager.hpp" |
||||||
#include "textures.hpp" |
#include "textures.hpp" |
||||||
#include "guecs.hpp" |
#include <guecs/ui.hpp> |
||||||
#include "tilemap.hpp" |
#include "tilemap.hpp" |
||||||
|
#include <string> |
||||||
|
|
||||||
namespace gui { |
namespace gui { |
||||||
class MapViewUI { |
class MapViewUI { |
||||||
public: |
public: |
||||||
guecs::UI $gui; |
guecs::UI $gui; |
||||||
GameLevel $level; |
GameLevel $level; |
||||||
|
DinkyECS::Entity $log_to; |
||||||
textures::SpriteTexture $paper; |
textures::SpriteTexture $paper; |
||||||
|
std::deque<std::wstring> $messages; |
||||||
|
|
||||||
MapViewUI(GameLevel &level); |
MapViewUI(GameLevel &level); |
||||||
void init(); |
void init(); |
||||||
void render(sf::RenderWindow &window, int compass_dir); |
void render(sf::RenderWindow &window, int compass_dir); |
||||||
void update_level(GameLevel &level); |
void update_level(GameLevel &level); |
||||||
|
void log(std::wstring msg); |
||||||
|
void update(); |
||||||
}; |
}; |
||||||
} |
} |
@ -1,7 +1,7 @@ |
|||||||
#pragma once |
#pragma once |
||||||
#include "levelmanager.hpp" |
#include "levelmanager.hpp" |
||||||
#include "textures.hpp" |
#include "textures.hpp" |
||||||
#include "guecs.hpp" |
#include <guecs/ui.hpp> |
||||||
#include "tilemap.hpp" |
#include "tilemap.hpp" |
||||||
|
|
||||||
namespace gui { |
namespace gui { |
@ -1,6 +1,5 @@ |
|||||||
#include "overlay_ui.hpp" |
#include "gui/overlay_ui.hpp" |
||||||
#include "constants.hpp" |
#include "constants.hpp" |
||||||
#include "color.hpp" |
|
||||||
#include "events.hpp" |
#include "events.hpp" |
||||||
#include <optional> |
#include <optional> |
||||||
|
|
@ -1,9 +1,11 @@ |
|||||||
#pragma once |
#pragma once |
||||||
#include <SFML/Graphics/RenderWindow.hpp> |
#include <SFML/Graphics/RenderWindow.hpp> |
||||||
#include <SFML/Graphics/Font.hpp> |
#include <SFML/Graphics/Font.hpp> |
||||||
#include "guecs.hpp" |
#include <guecs/ui.hpp> |
||||||
|
|
||||||
namespace gui { |
namespace gui { |
||||||
|
using std::string; |
||||||
|
|
||||||
class OverlayUI { |
class OverlayUI { |
||||||
public: |
public: |
||||||
guecs::UI $gui; |
guecs::UI $gui; |
@ -0,0 +1,252 @@ |
|||||||
|
#include "gui/ritual_ui.hpp" |
||||||
|
#include "components.hpp" |
||||||
|
#include <guecs/ui.hpp> |
||||||
|
#include "rand.hpp" |
||||||
|
#include "animation.hpp" |
||||||
|
#include "rand.hpp" |
||||||
|
#include "sound.hpp" |
||||||
|
#include "events.hpp" |
||||||
|
|
||||||
|
namespace gui { |
||||||
|
namespace ritual { |
||||||
|
using namespace guecs; |
||||||
|
using std::any, std::any_cast, std::string, std::make_any; |
||||||
|
|
||||||
|
UI::UI(GameLevel level) : |
||||||
|
$level(level), |
||||||
|
$blanket($level.world->get_the<::ritual::Blanket>()) |
||||||
|
{ |
||||||
|
$gui.position(STATUS_UI_X, STATUS_UI_Y, STATUS_UI_WIDTH, STATUS_UI_HEIGHT); |
||||||
|
$gui.layout( |
||||||
|
"[_]" |
||||||
|
"[inv_slot0 | inv_slot1 | inv_slot2| inv_slot3]" |
||||||
|
"[inv_slot4 | inv_slot5 | inv_slot6| inv_slot7]" |
||||||
|
"[inv_slot8 | inv_slot9 | inv_slot10| inv_slot11]" |
||||||
|
"[inv_slot12 | inv_slot13 | inv_slot14| inv_slot15]" |
||||||
|
"[inv_slot16 | inv_slot17 | inv_slot18| inv_slot19]" |
||||||
|
"[_ |*%(200,400)result_text|_]" |
||||||
|
"[*%(100,200)result_image|_ |_]" |
||||||
|
"[_|_|_]" |
||||||
|
"[_|_|_]" |
||||||
|
"[_]" |
||||||
|
"[ ritual_ui ]"); |
||||||
|
} |
||||||
|
|
||||||
|
void UI::event(Event ev, std::any data) { |
||||||
|
switch($state) { |
||||||
|
FSM_STATE(State, START, ev); |
||||||
|
FSM_STATE(State, OPENED, ev, data); |
||||||
|
FSM_STATE(State, CRAFTING, ev, data); |
||||||
|
FSM_STATE(State, CLOSED, ev); |
||||||
|
FSM_STATE(State, OPENING, ev); |
||||||
|
FSM_STATE(State, CLOSING, ev); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
void UI::START(Event) { |
||||||
|
$ritual_ui = textures::get("ritual_crafting_area"); |
||||||
|
$ritual_ui.sprite->setPosition($gui.get_position()); |
||||||
|
$ritual_ui.sprite->setTextureRect($ritual_closed_rect); |
||||||
|
state(State::CLOSED); |
||||||
|
$ritual_anim = animation::load("ritual_blanket"); |
||||||
|
|
||||||
|
auto open_close_toggle = $gui.entity("ritual_ui"); |
||||||
|
$gui.set<Clickable>(open_close_toggle, { |
||||||
|
[&](auto, auto){ event(Event::TOGGLE); } |
||||||
|
}); |
||||||
|
|
||||||
|
|
||||||
|
$craft_state = $ritual_engine.start(); |
||||||
|
$gui.init(); |
||||||
|
|
||||||
|
state(State::CLOSED); |
||||||
|
} |
||||||
|
|
||||||
|
void UI::OPENED(Event ev, std::any data) { |
||||||
|
if(ev == Event::TOGGLE) { |
||||||
|
clear_blanket(); |
||||||
|
state(State::CLOSING); |
||||||
|
} else if(ev == Event::SELECT) { |
||||||
|
// do this before transitioning
|
||||||
|
state(State::CRAFTING); |
||||||
|
UI::CRAFTING(ev, data); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
void UI::CRAFTING(Event ev, std::any data) { |
||||||
|
if(ev == Event::TOGGLE) { |
||||||
|
clear_blanket(); |
||||||
|
state(State::CLOSING); |
||||||
|
} else if(ev == Event::COMBINE) { |
||||||
|
complete_combine(); |
||||||
|
} else if(ev == Event::SELECT) { |
||||||
|
dbc::check(data.has_value(), "OPENED state given SELECT with no data"); |
||||||
|
auto pair = std::any_cast<SelectedItem>(data); |
||||||
|
select_item(pair); |
||||||
|
update_selection_state(); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
|
||||||
|
void UI::CLOSED(Event ev) { |
||||||
|
if(ev == Event::TOGGLE) { |
||||||
|
$ritual_anim.play(); |
||||||
|
load_blanket(); |
||||||
|
state(State::OPENING); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
void UI::OPENING(Event ev) { |
||||||
|
if(ev == Event::TICK) { |
||||||
|
if(!animation::apply($ritual_anim, $ritual_ui)) { |
||||||
|
state(State::OPENED); |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
void UI::CLOSING(Event ev) { |
||||||
|
if(ev == Event::TICK) { |
||||||
|
$ritual_ui.sprite->setTextureRect($ritual_closed_rect); |
||||||
|
state(State::CLOSED); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
bool UI::mouse(float x, float y, bool hover) { |
||||||
|
return $gui.mouse(x, y, hover); |
||||||
|
} |
||||||
|
|
||||||
|
bool UI::is_open() { |
||||||
|
return !in_state(State::CLOSED); |
||||||
|
} |
||||||
|
|
||||||
|
void UI::render(sf::RenderWindow &window) { |
||||||
|
event(Event::TICK); |
||||||
|
|
||||||
|
window.draw(*$ritual_ui.sprite); |
||||||
|
|
||||||
|
if(in_state(State::OPENED) || in_state(State::CRAFTING)) { |
||||||
|
$gui.render(window); |
||||||
|
// $gui.debug_layout(window);
|
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
void UI::clear_blanket() { |
||||||
|
for(int i = 0; i < INV_SLOTS; i++) { |
||||||
|
auto slot_id = $gui.entity("inv_slot", i); |
||||||
|
|
||||||
|
if($gui.has<Sprite>(slot_id)) { |
||||||
|
$gui.remove<Sprite>(slot_id); |
||||||
|
$gui.remove<Clickable>(slot_id); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
$blanket.reset(); |
||||||
|
} |
||||||
|
|
||||||
|
void UI::select_item(SelectedItem pair) { |
||||||
|
auto& sprite = $gui.get<Sprite>(pair.slot_id); |
||||||
|
|
||||||
|
if($blanket.is_selected(pair.item_id)) { |
||||||
|
$blanket.deselect(pair.item_id); |
||||||
|
sprite.sprite->setColor({255, 255, 255, 255}); |
||||||
|
} else { |
||||||
|
$blanket.select(pair.item_id); |
||||||
|
sprite.sprite->setColor({255, 200, 200, 200}); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
void UI::update_selection_state() { |
||||||
|
if($blanket.no_selections()) { |
||||||
|
clear_craft_result(); |
||||||
|
state(State::OPENED); |
||||||
|
} else { |
||||||
|
run_crafting_engine(); |
||||||
|
show_craft_result(); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
void UI::load_blanket() { |
||||||
|
// update the list of available items
|
||||||
|
int i = 0; |
||||||
|
for(auto& [item_id, item] : $blanket.contents) { |
||||||
|
auto slot_id = $gui.entity("inv_slot", i++); |
||||||
|
auto icon_name = fmt::format("{}-64", item); |
||||||
|
|
||||||
|
$gui.set_init<Sprite>(slot_id, {icon_name}); |
||||||
|
$gui.set<Clickable>(slot_id, { |
||||||
|
[&, slot_id, item_id](auto, auto) { |
||||||
|
auto data = std::make_any<SelectedItem>(slot_id, item_id); |
||||||
|
event(Event::SELECT, data); |
||||||
|
} |
||||||
|
}); |
||||||
|
} |
||||||
|
|
||||||
|
for(; i < INV_SLOTS; i++) { |
||||||
|
auto slot_id = $gui.entity("inv_slot", i); |
||||||
|
$gui.remove<Sprite>(slot_id); |
||||||
|
$gui.remove<Clickable>(slot_id); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
void UI::complete_combine() { |
||||||
|
if($craft_state.is_combined()) { |
||||||
|
auto ritual = $ritual_engine.finalize($craft_state); |
||||||
|
auto& belt = $level.world->get_the<::ritual::Belt>(); |
||||||
|
belt.equip(belt.next(), ritual); |
||||||
|
$level.world->send<Events::GUI>(Events::GUI::NEW_RITUAL, $level.player, {}); |
||||||
|
$blanket.consume_crafting(); |
||||||
|
clear_craft_result(); |
||||||
|
|
||||||
|
load_blanket(); |
||||||
|
state(State::OPENED); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
void UI::run_crafting_engine() { |
||||||
|
$craft_state.reset(); |
||||||
|
|
||||||
|
for(auto [item_id, setting] : $blanket.selected) { |
||||||
|
auto& item = $blanket.get(item_id); |
||||||
|
$ritual_engine.load_junk($craft_state, item); |
||||||
|
} |
||||||
|
|
||||||
|
$ritual_engine.plan($craft_state); |
||||||
|
} |
||||||
|
|
||||||
|
void UI::show_craft_result() { |
||||||
|
using enum ::ritual::Element; |
||||||
|
auto ritual = $ritual_engine.finalize($craft_state); |
||||||
|
auto combine = $gui.entity("result_image"); |
||||||
|
|
||||||
|
if($craft_state.is_combined()) { |
||||||
|
$gui.show_label("result_text", L"This might work..."); |
||||||
|
|
||||||
|
switch(ritual.element) { |
||||||
|
case FIRE: |
||||||
|
$gui.show_sprite("result_image", "broken_yoyo-64"); |
||||||
|
break; |
||||||
|
case LIGHTNING: |
||||||
|
$gui.show_sprite("result_image", "pocket_watch-64"); |
||||||
|
break; |
||||||
|
default: |
||||||
|
$gui.show_sprite("result_image", "severed_finger-64"); |
||||||
|
} |
||||||
|
|
||||||
|
$gui.set<Clickable>(combine, { |
||||||
|
[&](auto, auto){ event(Event::COMBINE); } |
||||||
|
}); |
||||||
|
} else { |
||||||
|
$gui.show_label("result_text", L"That won't work."); |
||||||
|
$gui.show_sprite("result_image", "dubious_combination-128"); |
||||||
|
$gui.remove<Clickable>(combine); |
||||||
|
return; |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
void UI::clear_craft_result() { |
||||||
|
$blanket.reset(); |
||||||
|
$gui.close<Label>("result_text"); |
||||||
|
$gui.close<Sprite>("result_image"); |
||||||
|
} |
||||||
|
} |
||||||
|
} |
@ -0,0 +1,69 @@ |
|||||||
|
#pragma once |
||||||
|
#include "levelmanager.hpp" |
||||||
|
#include "constants.hpp" |
||||||
|
#include <deque> |
||||||
|
#include "textures.hpp" |
||||||
|
#include <guecs/ui.hpp> |
||||||
|
#include "rituals.hpp" |
||||||
|
#include "simplefsm.hpp" |
||||||
|
|
||||||
|
namespace gui { |
||||||
|
namespace ritual { |
||||||
|
enum class State { |
||||||
|
START=0, |
||||||
|
OPENED=1, |
||||||
|
CLOSED=2, |
||||||
|
OPENING=3, |
||||||
|
CLOSING=4, |
||||||
|
CRAFTING=5 |
||||||
|
}; |
||||||
|
|
||||||
|
enum class Event { |
||||||
|
STARTED=0, |
||||||
|
TOGGLE=1, |
||||||
|
TICK=2, |
||||||
|
SELECT=3, |
||||||
|
COMBINE=4 |
||||||
|
}; |
||||||
|
|
||||||
|
struct SelectedItem { |
||||||
|
DinkyECS::Entity slot_id; |
||||||
|
DinkyECS::Entity item_id; |
||||||
|
}; |
||||||
|
|
||||||
|
class UI : public DeadSimpleFSM<State, Event> { |
||||||
|
public: |
||||||
|
sf::IntRect $ritual_closed_rect{{0,0},{380,720}}; |
||||||
|
sf::IntRect $ritual_open_rect{{380 * 2,0},{380,720}}; |
||||||
|
components::Animation $ritual_anim; |
||||||
|
guecs::UI $gui; |
||||||
|
GameLevel $level; |
||||||
|
textures::SpriteTexture $ritual_ui; |
||||||
|
::ritual::Blanket& $blanket; |
||||||
|
::ritual::Engine $ritual_engine; |
||||||
|
::ritual::CraftingState $craft_state; |
||||||
|
|
||||||
|
UI(GameLevel level); |
||||||
|
|
||||||
|
void event(Event ev, std::any data={}); |
||||||
|
void START(Event); |
||||||
|
void OPENED(Event, std::any data={}); |
||||||
|
void CRAFTING(Event, std::any data={}); |
||||||
|
void CLOSED(Event); |
||||||
|
void OPENING(Event); |
||||||
|
void CLOSING(Event); |
||||||
|
|
||||||
|
bool mouse(float x, float y, bool hover); |
||||||
|
void render(sf::RenderWindow &window); |
||||||
|
bool is_open(); |
||||||
|
void load_blanket(); |
||||||
|
void clear_blanket(); |
||||||
|
void select_item(SelectedItem pair); |
||||||
|
void show_craft_result(); |
||||||
|
void clear_craft_result(); |
||||||
|
void run_crafting_engine(); |
||||||
|
void complete_combine(); |
||||||
|
void update_selection_state(); |
||||||
|
}; |
||||||
|
} |
||||||
|
} |
@ -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,55 +0,0 @@ |
|||||||
#pragma once |
|
||||||
#include <string> |
|
||||||
#include <unordered_map> |
|
||||||
#include <functional> |
|
||||||
#include <optional> |
|
||||||
#include <vector> |
|
||||||
|
|
||||||
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,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,156 +0,0 @@ |
|||||||
#include "ritual_ui.hpp" |
|
||||||
#include "components.hpp" |
|
||||||
#include "guecs.hpp" |
|
||||||
#include "rand.hpp" |
|
||||||
#include "animation.hpp" |
|
||||||
#include "rand.hpp" |
|
||||||
|
|
||||||
namespace gui { |
|
||||||
using namespace guecs; |
|
||||||
using std::any, std::any_cast, std::string, std::make_any; |
|
||||||
|
|
||||||
RitualUI::RitualUI(GameLevel level) : |
|
||||||
$level(level) |
|
||||||
{ |
|
||||||
$gui.position(STATUS_UI_X, STATUS_UI_Y, STATUS_UI_WIDTH, STATUS_UI_HEIGHT); |
|
||||||
$gui.layout( |
|
||||||
"[_]" |
|
||||||
"[inv_slot9 | inv_slot10 | inv_slot11| inv_slot12]" |
|
||||||
"[inv_slot13 | inv_slot14 | inv_slot15| inv_slot16]" |
|
||||||
"[inv_slot17 | inv_slot18 | inv_slot19| inv_slot20]" |
|
||||||
"[inv_slot21 | inv_slot22 | inv_slot23| inv_slot24]" |
|
||||||
"[*%(100,600)circle_area]" |
|
||||||
"[_]" |
|
||||||
"[_]" |
|
||||||
"[_]" |
|
||||||
"[_]" |
|
||||||
"[_]" |
|
||||||
"[ ritual_ui ]"); |
|
||||||
} |
|
||||||
|
|
||||||
void RitualUI::init() { |
|
||||||
std::vector<std::string> junk_list{ |
|
||||||
{"chess_pawn"}, |
|
||||||
{"dirty_kerchief"}, |
|
||||||
{"mushroom"}, |
|
||||||
{"pocket_watch"}, |
|
||||||
{"rusty_nails"}, |
|
||||||
{"severed_finger"} |
|
||||||
}; |
|
||||||
|
|
||||||
for(auto& [name, cell] : $gui.cells()) { |
|
||||||
auto button = $gui.entity(name); |
|
||||||
|
|
||||||
if(name == "circle_area") { |
|
||||||
$gui.set<Effect>(button, {0.4f}); |
|
||||||
$gui.set<Sprite>(button, {"the_ritual_circle"}); |
|
||||||
$gui.set<Clickable>(button, { |
|
||||||
[&](auto ent, auto){ ritual_circle_clicked(ent); } |
|
||||||
}); |
|
||||||
} else if(name.starts_with("inv_slot")) { |
|
||||||
$gui.set<Sprite>(button, { |
|
||||||
fmt::format("{}-64", junk_list[button % junk_list.size()])}); |
|
||||||
$gui.set<Effect>(button, {0.4f}); |
|
||||||
$gui.set<Sound>(button, {"ui_click"}); |
|
||||||
$gui.set<Clickable>(button, { |
|
||||||
[&](auto ent, auto){ inv_slot_clicked(ent); } |
|
||||||
}); |
|
||||||
} else if(name == "ritual_ui") { |
|
||||||
$gui.set<Clickable>(button, { |
|
||||||
[&](auto, auto){ toggle(); } |
|
||||||
}); |
|
||||||
$gui.set<Sound>(button, {"pickup"}); |
|
||||||
} |
|
||||||
} |
|
||||||
|
|
||||||
$ritual_ui = textures::get("ritual_crafting_area"); |
|
||||||
$ritual_ui.sprite->setPosition({0,0}); |
|
||||||
$ritual_ui.sprite->setTextureRect($ritual_closed_rect); |
|
||||||
$ritual_state = RitualUIState::CLOSED; |
|
||||||
$ritual_anim = animation::load("ritual_blanket"); |
|
||||||
|
|
||||||
$gui.init(); |
|
||||||
|
|
||||||
} |
|
||||||
|
|
||||||
bool RitualUI::is_open() { |
|
||||||
return $ritual_state != RitualUIState::CLOSED; |
|
||||||
} |
|
||||||
|
|
||||||
void RitualUI::inv_slot_clicked(DinkyECS::Entity ent) { |
|
||||||
if($gui.has<Sprite>(ent)) { |
|
||||||
auto& bs = $gui.get<Sprite>(ent); |
|
||||||
auto ritual_circle = $gui.entity("circle_area"); |
|
||||||
auto& ritual_cell = $gui.cell_for(ritual_circle); |
|
||||||
dbc::log(fmt::format("inv_slot clicked {}", bs.name)); |
|
||||||
|
|
||||||
int inner_x = ritual_cell.x + ritual_cell.x / 2; |
|
||||||
int inner_y = ritual_cell.y + ritual_cell.y / 2; |
|
||||||
|
|
||||||
float x = Random::uniform(inner_x, inner_x + ritual_cell.w / 2); |
|
||||||
float y = Random::uniform(inner_y, inner_y + ritual_cell.h / 2); |
|
||||||
bs.sprite->setPosition({float(x), float(y)}); |
|
||||||
} |
|
||||||
} |
|
||||||
|
|
||||||
void RitualUI::reset_inv_positions() { |
|
||||||
auto ritual_circle = $gui.entity("circle_area"); |
|
||||||
|
|
||||||
$gui.world().query<lel::Cell, Sprite>( |
|
||||||
[&](const auto ent, auto &cell, auto &bs) { |
|
||||||
if(ent == ritual_circle) { |
|
||||||
bs.sprite->setColor({255,255,255,255}); |
|
||||||
bs.sprite->setRotation(sf::degrees(0.0)); |
|
||||||
} else { |
|
||||||
bs.sprite->setPosition({(float)cell.x, (float)cell.y}); |
|
||||||
} |
|
||||||
}); |
|
||||||
} |
|
||||||
|
|
||||||
void RitualUI::ritual_circle_clicked(DinkyECS::Entity ent) { |
|
||||||
auto cell = $gui.cell_for(ent); |
|
||||||
auto& bs = $gui.get<Sprite>(ent); |
|
||||||
bs.sprite->setColor({200, 0, 0}); |
|
||||||
animation::center(*bs.sprite, {(float)cell.x, (float)cell.y}); |
|
||||||
animation::rotate(*bs.sprite, 20.0); |
|
||||||
} |
|
||||||
|
|
||||||
bool RitualUI::mouse(float x, float y, bool hover) { |
|
||||||
return $gui.mouse(x, y, hover); |
|
||||||
} |
|
||||||
|
|
||||||
void RitualUI::toggle() { |
|
||||||
using enum RitualUIState; |
|
||||||
|
|
||||||
if($ritual_state == OPEN) { |
|
||||||
$ritual_state = CLOSING; |
|
||||||
} else if($ritual_state == CLOSED) { |
|
||||||
$ritual_state = OPENING; |
|
||||||
$ritual_anim.play(); |
|
||||||
} |
|
||||||
} |
|
||||||
|
|
||||||
/* WARNING: This is really not the greatest way to do this.
|
|
||||||
* look in status_ui.update_level() |
|
||||||
* */ |
|
||||||
void RitualUI::update() { |
|
||||||
dbc::log("RITUAL UPDATE NOT IMPLEMENTED"); |
|
||||||
} |
|
||||||
|
|
||||||
void RitualUI::render(sf::RenderWindow &window) { |
|
||||||
using enum RitualUIState; |
|
||||||
|
|
||||||
if($ritual_state == OPENING) { |
|
||||||
if(!animation::apply($ritual_anim, $ritual_ui)) { |
|
||||||
$ritual_state = OPEN; |
|
||||||
} |
|
||||||
} else if($ritual_state == CLOSING) { |
|
||||||
reset_inv_positions(); |
|
||||||
$ritual_ui.sprite->setTextureRect($ritual_closed_rect); |
|
||||||
$ritual_state = CLOSED; |
|
||||||
} |
|
||||||
|
|
||||||
window.draw(*$ritual_ui.sprite); |
|
||||||
if($ritual_state == OPEN) $gui.render(window); |
|
||||||
} |
|
||||||
} |
|
@ -1,38 +0,0 @@ |
|||||||
#pragma once |
|
||||||
#include "levelmanager.hpp" |
|
||||||
#include "constants.hpp" |
|
||||||
#include <deque> |
|
||||||
#include "textures.hpp" |
|
||||||
#include "guecs.hpp" |
|
||||||
|
|
||||||
namespace gui { |
|
||||||
enum class RitualUIState { |
|
||||||
OPEN=0, |
|
||||||
CLOSED=1, |
|
||||||
OPENING=2, |
|
||||||
CLOSING=3 |
|
||||||
}; |
|
||||||
|
|
||||||
class RitualUI { |
|
||||||
public: |
|
||||||
sf::IntRect $ritual_closed_rect{{0,0},{380,720}}; |
|
||||||
sf::IntRect $ritual_open_rect{{380 * 2,0},{380,720}}; |
|
||||||
RitualUIState $ritual_state = RitualUIState::CLOSED; |
|
||||||
textures::SpriteTexture $ritual_ui; |
|
||||||
components::Animation $ritual_anim; |
|
||||||
guecs::UI $gui; |
|
||||||
GameLevel $level; |
|
||||||
|
|
||||||
RitualUI(GameLevel level); |
|
||||||
bool mouse(float x, float y, bool hover); |
|
||||||
void toggle(); |
|
||||||
bool is_open(); |
|
||||||
void init(); |
|
||||||
void render(sf::RenderWindow &window); |
|
||||||
void update(); |
|
||||||
|
|
||||||
void ritual_circle_clicked(DinkyECS::Entity ent); |
|
||||||
void inv_slot_clicked(DinkyECS::Entity ent); |
|
||||||
void reset_inv_positions(); |
|
||||||
}; |
|
||||||
} |
|
@ -1,33 +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& world = gui.world(); |
|
||||||
auto button = gui.entity(name); |
|
||||||
world.set<lel::Cell>(button, cell); |
|
||||||
world.set<Rectangle>(button, {}); |
|
||||||
world.set<Clickable>(button, {}); |
|
||||||
world.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(); |
|
||||||
} |
|
@ -1,52 +0,0 @@ |
|||||||
#include "lel.hpp" |
|
||||||
#include <catch2/catch_test_macros.hpp> |
|
||||||
#include <fmt/core.h> |
|
||||||
#include <string> |
|
||||||
|
|
||||||
TEST_CASE("test basic ops", "[lel]") { |
|
||||||
lel::Parser parser(0, 0, 500, 500); |
|
||||||
|
|
||||||
bool good = parser.parse( |
|
||||||
"[ label_1 | label3 | test1]" |
|
||||||
"[ *(300,300)text1 | %(150)people | ^test2]" |
|
||||||
"[ >label2 | _ | .test3]" |
|
||||||
"[ =message | buttons | test4]"); |
|
||||||
|
|
||||||
REQUIRE(good); |
|
||||||
|
|
||||||
for(size_t rowcount = 0; rowcount < parser.grid.size(); rowcount++) { |
|
||||||
auto& row = parser.grid[rowcount]; |
|
||||||
|
|
||||||
for(size_t colcount = 0; colcount < row.size(); colcount++) { |
|
||||||
auto &name = row[colcount]; |
|
||||||
if(name == "_") { |
|
||||||
REQUIRE(!parser.cells.contains(name)); |
|
||||||
} else { |
|
||||||
auto &cell = parser.cells.at(name); |
|
||||||
REQUIRE(cell.row == int(rowcount)); |
|
||||||
REQUIRE(cell.col == int(colcount)); |
|
||||||
} |
|
||||||
} |
|
||||||
} |
|
||||||
|
|
||||||
REQUIRE(parser.cells.size() == 11); |
|
||||||
REQUIRE(parser.cells.at("label2").right == true); |
|
||||||
REQUIRE(parser.cells.at("text1").expand == true); |
|
||||||
REQUIRE(parser.cells.at("text1").w == 300); |
|
||||||
REQUIRE(parser.cells.at("text1").h == 300); |
|
||||||
REQUIRE(parser.cells.at("people").expand == false); |
|
||||||
REQUIRE(parser.cells.at("message").expand == false); |
|
||||||
REQUIRE(parser.cells.at("message").center == true); |
|
||||||
|
|
||||||
for(auto& [name, cell] : parser.cells) { |
|
||||||
REQUIRE(cell.w > 0); |
|
||||||
REQUIRE(cell.h > 0); |
|
||||||
} |
|
||||||
|
|
||||||
auto hit = parser.hit(10, 10); |
|
||||||
REQUIRE(*hit == "label_1"); |
|
||||||
|
|
||||||
auto nohit = parser.hit(1000, 1000); |
|
||||||
REQUIRE(!nohit); |
|
||||||
REQUIRE(nohit == std::nullopt); |
|
||||||
} |
|
@ -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