Compare commits
6 commits
Author | SHA1 | Date | |
---|---|---|---|
![]() |
070244269e | ||
![]() |
41d568ab25 | ||
![]() |
4b4992185f | ||
![]() |
5d0d8b16fc | ||
![]() |
4c019048d0 | ||
![]() |
7e64879f78 |
7 changed files with 74 additions and 40 deletions
|
@ -169,7 +169,7 @@ struct CalculatorUI {
|
|||
$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) {
|
||||
|
|
|
@ -92,9 +92,9 @@ struct ClickerUI {
|
|||
auto id = $gui.entity(name);
|
||||
if(name != "clicker") {
|
||||
$gui.set<guecs::Effect>(id, {});
|
||||
$gui.set<guecs::Sprite>(id, { "clicker_treat_bone" });
|
||||
$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); }
|
||||
});
|
||||
}
|
||||
}
|
||||
|
@ -103,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
|
||||
|
@ -126,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) {
|
||||
|
|
|
@ -36,6 +36,20 @@ namespace guecs {
|
|||
void render(sf::RenderWindow& window, sf::Shader *shader_ptr);
|
||||
};
|
||||
|
||||
struct Icon : public Sprite {
|
||||
template<typename... Args>
|
||||
Icon(Args... args) : Sprite(args...)
|
||||
{
|
||||
stretch = false;
|
||||
is_icon = true;
|
||||
}
|
||||
|
||||
Icon() {
|
||||
stretch = false;
|
||||
is_icon = true;
|
||||
};
|
||||
};
|
||||
|
||||
struct Rectangle {
|
||||
int padding = THEME.PADDING;
|
||||
sf::Color color = THEME.FILL_COLOR;
|
||||
|
|
|
@ -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.5.0',
|
||||
version: '0.6.0',
|
||||
default_options: [
|
||||
'cpp_std=c++20',
|
||||
'cpp_args=-D_GLIBCXX_DEBUG=1 -D_GLIBCXX_DEBUG_PEDANTIC=1',
|
||||
|
|
|
@ -97,6 +97,8 @@ namespace guecs {
|
|||
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) {
|
||||
|
|
|
@ -89,6 +89,10 @@ namespace guecs {
|
|||
query<lel::Cell, Sprite>([&](auto, auto &cell, auto &sprite) {
|
||||
sprite.init(cell);
|
||||
});
|
||||
|
||||
query<lel::Cell, Icon>([&](auto, auto &cell, auto &icon) {
|
||||
icon.init(cell);
|
||||
});
|
||||
}
|
||||
|
||||
void UI::debug_layout(sf::RenderWindow& window) {
|
||||
|
@ -134,15 +138,20 @@ namespace guecs {
|
|||
sprite.render(window, shader_ptr);
|
||||
});
|
||||
|
||||
query<Icon>([&](auto ent, auto& icon) {
|
||||
auto shader_ptr = find_shader(ent, false);
|
||||
icon.render(window, shader_ptr);
|
||||
});
|
||||
|
||||
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) &&
|
||||
|
@ -160,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();
|
||||
});
|
||||
|
||||
|
@ -188,6 +197,16 @@ 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);
|
||||
|
||||
|
@ -201,25 +220,14 @@ namespace guecs {
|
|||
}
|
||||
}
|
||||
|
||||
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::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