Compare commits
13 commits
Author | SHA1 | Date | |
---|---|---|---|
![]() |
070244269e | ||
![]() |
41d568ab25 | ||
![]() |
4b4992185f | ||
![]() |
5d0d8b16fc | ||
![]() |
4c019048d0 | ||
![]() |
7e64879f78 | ||
![]() |
0d91c554c6 | ||
![]() |
6837192583 | ||
![]() |
9e9b9620c9 | ||
![]() |
2c22da022f | ||
![]() |
3752522597 | ||
![]() |
a22342cd7e | ||
![]() |
b7cfa4db2d |
8 changed files with 128 additions and 148 deletions
|
@ -141,12 +141,12 @@ struct CalculatorUI {
|
|||
$gui.layout(
|
||||
"[*%(400)stack |_|_|_]"
|
||||
"[*%(400)readout|_|_|_]"
|
||||
"[push|pop|clear|eq]"
|
||||
"[add |sub|mul |div]"
|
||||
"[btn7|btn8|btn9]"
|
||||
"[btn4|btn5|btn6]"
|
||||
"[btn1|btn2|btn3]"
|
||||
"[neg |btn0|del]");
|
||||
"[=push|=pop|=clear|=eq]"
|
||||
"[=add |=sub|=mul |=div]"
|
||||
"[=btn7|=btn8|=btn9]"
|
||||
"[=btn4|=btn5|=btn6]"
|
||||
"[=btn1|=btn2|=btn3]"
|
||||
"[=neg |=btn0|=del]");
|
||||
}
|
||||
|
||||
void init() {
|
||||
|
@ -161,15 +161,15 @@ struct CalculatorUI {
|
|||
$gui.set<guecs::Rectangle>(id, {});
|
||||
|
||||
if(name == "readout") {
|
||||
$gui.set<guecs::Textual>(id, {L"", 40});
|
||||
$gui.set<guecs::Text>(id, {L"", 40});
|
||||
} else if(name == "stack") {
|
||||
$gui.set<guecs::Textual>(id, {L"", 20});
|
||||
$gui.set<guecs::Text>(id, {L"", 20});
|
||||
} else {
|
||||
$gui.set<guecs::Effect>(id, {});
|
||||
$gui.set<guecs::Label>(id, { label });
|
||||
$gui.set<guecs::Text>(id, { label });
|
||||
wchar_t op = label[0];
|
||||
$gui.set<guecs::Clickable>(id, {
|
||||
[&, op](auto, auto) { handle_button(op); }
|
||||
[&, op](auto) { handle_button(op); }
|
||||
});
|
||||
}
|
||||
}
|
||||
|
@ -182,8 +182,8 @@ struct CalculatorUI {
|
|||
// $gui.debug_layout(window);
|
||||
}
|
||||
|
||||
void mouse(float x, float y, bool hover) {
|
||||
$gui.mouse(x, y, hover);
|
||||
void mouse(float x, float y, guecs::Modifiers mods) {
|
||||
$gui.mouse(x, y, mods);
|
||||
}
|
||||
|
||||
void handle_button(wchar_t op) {
|
||||
|
|
|
@ -73,7 +73,7 @@ struct ClickerUI {
|
|||
ClickerUI() {
|
||||
$gui.position(0, 0, WINDOW_WIDTH, WINDOW_HEIGHT);
|
||||
$gui.layout(
|
||||
"[_|*%(300,400)clicker|_|_|_]"
|
||||
"[_|=*%(300,400)clicker|_|_|_]"
|
||||
"[_|_ |_|_|_]"
|
||||
"[_|_ |_|_|_]"
|
||||
"[_|_ |_|_|_]"
|
||||
|
@ -91,11 +91,10 @@ struct ClickerUI {
|
|||
for(auto& [name, cell] : $gui.cells()) {
|
||||
auto id = $gui.entity(name);
|
||||
if(name != "clicker") {
|
||||
$gui.set<guecs::Rectangle>(id, {});
|
||||
$gui.set<guecs::Effect>(id, {});
|
||||
$gui.set<guecs::Icon>(id, { "clicker_treat_bone" });
|
||||
$gui.set<guecs::Clickable>(id, {
|
||||
[&](auto, auto) { handle_button(Event::A_BUTTON); }
|
||||
[&](auto) { handle_button(Event::A_BUTTON); }
|
||||
});
|
||||
}
|
||||
}
|
||||
|
@ -104,7 +103,7 @@ struct ClickerUI {
|
|||
$gui.set<guecs::Sprite>($clicker, {"clicker_the_dog"});
|
||||
$gui.set<guecs::Sound>($clicker, {"clicker_bark"});
|
||||
$gui.set<guecs::Clickable>($clicker, {
|
||||
[&](auto, auto) { handle_button(Event::CLICKER); }
|
||||
[&](auto) { handle_button(Event::CLICKER); }
|
||||
});
|
||||
|
||||
// custom components need to be initialized manually
|
||||
|
@ -127,8 +126,8 @@ struct ClickerUI {
|
|||
// $gui.debug_layout(window);
|
||||
}
|
||||
|
||||
void mouse(float x, float y, bool hover) {
|
||||
$gui.mouse(x, y, hover);
|
||||
void mouse(float x, float y, guecs::Modifiers mods) {
|
||||
$gui.mouse(x, y, mods);
|
||||
}
|
||||
|
||||
void handle_button(Event ev) {
|
||||
|
@ -143,7 +142,6 @@ struct ClickerUI {
|
|||
case A_BUTTON:
|
||||
fmt::println("a button clicked");
|
||||
break;
|
||||
|
||||
default:
|
||||
assert(false && "invalid event");
|
||||
}
|
||||
|
|
|
@ -10,7 +10,7 @@
|
|||
namespace guecs {
|
||||
using std::shared_ptr, std::wstring, std::string;
|
||||
|
||||
struct Textual {
|
||||
struct Text {
|
||||
std::wstring content;
|
||||
unsigned int size = THEME.TEXT_SIZE;
|
||||
sf::Color color = THEME.TEXT_COLOR;
|
||||
|
@ -24,22 +24,11 @@ namespace guecs {
|
|||
void render(sf::RenderWindow& window, sf::Shader *shader_ptr);
|
||||
};
|
||||
|
||||
struct Label : public Textual {
|
||||
template<typename... Args>
|
||||
Label(Args... args) : Textual(args...)
|
||||
{
|
||||
centered = true;
|
||||
size = THEME.LABEL_SIZE;
|
||||
}
|
||||
|
||||
Label() {
|
||||
centered = true;
|
||||
};
|
||||
};
|
||||
|
||||
struct Sprite {
|
||||
string name;
|
||||
int padding = THEME.PADDING;
|
||||
bool stretch = true;
|
||||
bool is_icon = false;
|
||||
std::shared_ptr<sf::Sprite> sprite = nullptr;
|
||||
|
||||
void init(lel::Cell &cell);
|
||||
|
@ -47,14 +36,18 @@ namespace guecs {
|
|||
void render(sf::RenderWindow& window, sf::Shader *shader_ptr);
|
||||
};
|
||||
|
||||
struct Icon {
|
||||
string name;
|
||||
int padding = THEME.PADDING;
|
||||
std::shared_ptr<sf::Sprite> sprite = nullptr;
|
||||
struct Icon : public Sprite {
|
||||
template<typename... Args>
|
||||
Icon(Args... args) : Sprite(args...)
|
||||
{
|
||||
stretch = false;
|
||||
is_icon = true;
|
||||
}
|
||||
|
||||
void init(lel::Cell &cell);
|
||||
void update(const string& new_name);
|
||||
void render(sf::RenderWindow& window, sf::Shader *shader_ptr);
|
||||
Icon() {
|
||||
stretch = false;
|
||||
is_icon = true;
|
||||
};
|
||||
};
|
||||
|
||||
struct Rectangle {
|
||||
|
|
|
@ -10,6 +10,7 @@
|
|||
#include "guecs/theme.hpp"
|
||||
#include "guecs/sfml/components.hpp"
|
||||
#include <cassert>
|
||||
#include <bitset>
|
||||
|
||||
namespace guecs {
|
||||
using std::shared_ptr, std::wstring, std::string;
|
||||
|
@ -24,22 +25,30 @@ namespace guecs {
|
|||
std::queue<size_t> free_indices;
|
||||
};
|
||||
|
||||
namespace ModBit {
|
||||
enum {
|
||||
NONE=0,
|
||||
hover=1,
|
||||
left=2,
|
||||
right=3
|
||||
};
|
||||
}
|
||||
|
||||
using Modifiers = std::bitset<16>;
|
||||
|
||||
constexpr const Modifiers NO_MODS{0};
|
||||
|
||||
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 ActionData {
|
||||
std::any data;
|
||||
std::function<void(Modifiers mods)> action;
|
||||
};
|
||||
|
||||
struct CellName {
|
||||
string name;
|
||||
};
|
||||
|
||||
|
||||
class UI {
|
||||
public:
|
||||
Entity MAIN = 0;
|
||||
|
@ -69,9 +78,9 @@ namespace guecs {
|
|||
|
||||
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);
|
||||
bool mouse(float x, float y, Modifiers mods);
|
||||
void click_on(const std::string& name, Modifiers mods=NO_MODS);
|
||||
void click_on(Entity slot_id, Modifiers mods=NO_MODS);
|
||||
void debug_layout(sf::RenderWindow& window);
|
||||
|
||||
Entity entity() { return ++entity_count; }
|
||||
|
@ -218,6 +227,7 @@ namespace guecs {
|
|||
sf::Shader* find_shader(Entity ent, bool is_shape);
|
||||
|
||||
void show_sprite(const string& region, const string& sprite_name);
|
||||
void show_icon(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);
|
||||
};
|
||||
|
|
|
@ -3,7 +3,7 @@
|
|||
# HEY BUG: when you have a . spec in a LEL it doesn't work on text
|
||||
|
||||
project('lel-guecs', 'cpp',
|
||||
version: '0.4.0',
|
||||
version: '0.6.0',
|
||||
default_options: [
|
||||
'cpp_std=c++20',
|
||||
'cpp_args=-D_GLIBCXX_DEBUG=1 -D_GLIBCXX_DEBUG_PEDANTIC=1',
|
||||
|
|
|
@ -50,6 +50,7 @@ namespace lel {
|
|||
int cell_height = grid_h / rows;
|
||||
|
||||
for(auto& row : grid) {
|
||||
// BUG: see issue #16
|
||||
size_t columns = row.size();
|
||||
int cell_width = grid_w / columns;
|
||||
assert(cell_width > 0 && "invalid cell width calc");
|
||||
|
|
|
@ -7,13 +7,34 @@
|
|||
namespace guecs {
|
||||
using std::make_shared;
|
||||
|
||||
void Textual::init(lel::Cell &cell, shared_ptr<sf::Font> font_ptr) {
|
||||
template<typename T>
|
||||
void sfml_center_helper(T& obj, lel::Cell& cell, int padding) {
|
||||
sf::Vector2f position{float(cell.x + padding), float(cell.y + padding)};
|
||||
|
||||
if(cell.center) {
|
||||
auto bounds = obj->getLocalBounds();
|
||||
position = {float(cell.mid_x), float(cell.mid_y)};
|
||||
obj->setOrigin({bounds.size.x/2, bounds.size.y/2});
|
||||
}
|
||||
|
||||
obj->setPosition(position);
|
||||
}
|
||||
|
||||
inline SpriteTexture load_texture(const string& name, bool is_icon) {
|
||||
if(is_icon) {
|
||||
return BACKEND->get_icon(name);
|
||||
} else {
|
||||
return BACKEND->get_sprite(name);
|
||||
}
|
||||
}
|
||||
|
||||
void Text::init(lel::Cell &cell, shared_ptr<sf::Font> font_ptr) {
|
||||
assert(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) {
|
||||
if(centered || cell.center) {
|
||||
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
|
||||
|
@ -25,11 +46,11 @@ namespace guecs {
|
|||
text->setCharacterSize(size);
|
||||
}
|
||||
|
||||
void Textual::render(sf::RenderWindow& window, sf::Shader *shader_ptr) {
|
||||
void Text::render(sf::RenderWindow& window, sf::Shader *shader_ptr) {
|
||||
window.draw(*text, shader_ptr);
|
||||
}
|
||||
|
||||
void Textual::update(const wstring& new_content) {
|
||||
void Text::update(const wstring& new_content) {
|
||||
content = new_content;
|
||||
text->setString(content);
|
||||
}
|
||||
|
@ -44,59 +65,40 @@ namespace guecs {
|
|||
}
|
||||
|
||||
void Sprite::init(lel::Cell &cell) {
|
||||
auto sprite_texture = BACKEND->get_sprite(name);
|
||||
if(cell.center) stretch = false;
|
||||
|
||||
sf::IntRect rect{{0,0},sprite_texture.frame_size};
|
||||
auto sprite_texture = load_texture(name, is_icon);
|
||||
auto bounds = sprite_texture.frame_size;
|
||||
|
||||
sf::IntRect rect{{0,0}, bounds};
|
||||
sprite = make_shared<sf::Sprite>(*sprite_texture.texture, rect);
|
||||
|
||||
sprite->setPosition({
|
||||
float(cell.x + padding),
|
||||
float(cell.y + padding)});
|
||||
|
||||
auto bounds = sprite->getLocalBounds();
|
||||
|
||||
if(stretch) {
|
||||
sprite->setScale({
|
||||
float(cell.w - padding * 2) / bounds.size.x,
|
||||
float(cell.h - padding * 2) / bounds.size.y});
|
||||
float(cell.w - padding * 2) / float(bounds.x),
|
||||
float(cell.h - padding * 2) / float(bounds.y)});
|
||||
} else {
|
||||
float box_width = float(cell.w - padding * 2);
|
||||
float box_height = float(cell.h - padding * 2);
|
||||
float scale = std::min(box_width / float(bounds.x), box_height / float(bounds.y));
|
||||
sprite->setScale({scale, scale});
|
||||
}
|
||||
|
||||
sfml_center_helper(sprite, cell, padding);
|
||||
}
|
||||
|
||||
void Sprite::render(sf::RenderWindow& window, sf::Shader *shader_ptr) {
|
||||
window.draw(*sprite, shader_ptr);
|
||||
}
|
||||
|
||||
void Icon::update(const string& new_name) {
|
||||
if(new_name != name) {
|
||||
name = new_name;
|
||||
auto sprite_texture = BACKEND->get_icon(name);
|
||||
sprite->setTexture(*sprite_texture.texture);
|
||||
sprite->setTextureRect({{0,0},sprite_texture.frame_size});
|
||||
}
|
||||
}
|
||||
|
||||
void Icon::init(lel::Cell &cell) {
|
||||
auto sprite_texture = BACKEND->get_icon(name);
|
||||
|
||||
sf::IntRect rect{{0,0},sprite_texture.frame_size};
|
||||
fmt::println("ICON SIZE: {},{}; {},{}",
|
||||
rect.position.x, rect.position.y,
|
||||
rect.size.x, rect.size.y);
|
||||
sprite = make_shared<sf::Sprite>(*sprite_texture.texture, rect);
|
||||
|
||||
sprite->setPosition({ float(cell.x + padding), float(cell.y + padding)});
|
||||
}
|
||||
|
||||
void Icon::render(sf::RenderWindow& window, sf::Shader *shader_ptr) {
|
||||
window.draw(*sprite, shader_ptr);
|
||||
}
|
||||
|
||||
|
||||
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);
|
||||
|
||||
sfml_center_helper(shape, cell, padding);
|
||||
}
|
||||
|
||||
void Rectangle::render(sf::RenderWindow& window, sf::Shader *shader_ptr) {
|
||||
|
|
|
@ -82,11 +82,7 @@ namespace guecs {
|
|||
}
|
||||
});
|
||||
|
||||
query<lel::Cell, Textual>([this](auto, auto& cell, auto& text) {
|
||||
text.init(cell, $font);
|
||||
});
|
||||
|
||||
query<lel::Cell, Label>([this](auto, auto& cell, auto& text) {
|
||||
query<lel::Cell, Text>([this](auto, auto& cell, auto& text) {
|
||||
text.init(cell, $font);
|
||||
});
|
||||
|
||||
|
@ -147,20 +143,15 @@ namespace guecs {
|
|||
icon.render(window, shader_ptr);
|
||||
});
|
||||
|
||||
query<Label>([&](auto ent, auto& text) {
|
||||
auto shader_ptr = find_shader(ent, false);
|
||||
text.render(window, shader_ptr);
|
||||
});
|
||||
|
||||
query<Textual>([&](auto ent, auto& text) {
|
||||
query<Text>([&](auto ent, auto& text) {
|
||||
auto shader_ptr = find_shader(ent, false);
|
||||
text.render(window, shader_ptr);
|
||||
});
|
||||
}
|
||||
|
||||
bool UI::mouse(float x, float y, bool hover) {
|
||||
bool UI::mouse(float x, float y, Modifiers mods) {
|
||||
int action_count = 0;
|
||||
// BUG: Is Parser::hit useful?
|
||||
bool hover = mods.test(size_t(ModBit::hover));
|
||||
|
||||
query<lel::Cell>([&](auto ent, auto& cell) {
|
||||
if((x >= cell.x && x <= cell.x + cell.w) &&
|
||||
|
@ -178,11 +169,11 @@ namespace guecs {
|
|||
|
||||
if(hover) return;
|
||||
|
||||
click_on(ent);
|
||||
click_on(ent, mods);
|
||||
|
||||
action_count++;
|
||||
} else {
|
||||
do_if<Effect>(ent, [hover](auto& effect) {
|
||||
do_if<Effect>(ent, [](auto& effect) {
|
||||
effect.stop();
|
||||
});
|
||||
|
||||
|
@ -206,52 +197,37 @@ namespace guecs {
|
|||
}
|
||||
}
|
||||
|
||||
void UI::show_icon(const string& region, const string& sprite_name) {
|
||||
auto ent = entity(region);
|
||||
|
||||
if(auto sprite = get_if<Icon>(ent)) {
|
||||
sprite->update(sprite_name);
|
||||
} else {
|
||||
set_init<Icon>(ent, {sprite_name});
|
||||
}
|
||||
}
|
||||
|
||||
void UI::show_text(const string& region, const wstring& content) {
|
||||
auto ent = entity(region);
|
||||
|
||||
if(auto tc = get_if<Textual>(ent)) {
|
||||
if(auto tc = get_if<Text>(ent)) {
|
||||
tc->text->setString(content);
|
||||
} else {
|
||||
auto &cell = cell_for(ent);
|
||||
Textual to_set{content, THEME.TEXT_SIZE};
|
||||
Text to_set{content, THEME.TEXT_SIZE};
|
||||
to_set.init(cell, $font);
|
||||
set<Textual>(ent, to_set);
|
||||
set<Text>(ent, to_set);
|
||||
}
|
||||
}
|
||||
|
||||
void UI::click_on(const string& name, bool required) {
|
||||
auto ent = entity(name);
|
||||
|
||||
if(required) {
|
||||
assert(has<Clickable>(ent) &&
|
||||
"click_on required '{}' to exist but it doesn't");
|
||||
void UI::click_on(const std::string& name, Modifiers mods) {
|
||||
click_on(entity(name), mods);
|
||||
}
|
||||
|
||||
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, THEME.LABEL_SIZE};
|
||||
to_set.init(cell, $font);
|
||||
to_set.text->setFillColor(THEME.TEXT_COLOR);
|
||||
set<Label>(ent, to_set);
|
||||
}
|
||||
void UI::click_on(Entity gui_id, Modifiers mods) {
|
||||
if(auto to_click = get_if<Clickable>(gui_id)) {
|
||||
to_click->action(mods);
|
||||
};
|
||||
}
|
||||
|
||||
wstring to_wstring(const string& str) {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue