diff --git a/guecs.cpp b/guecs.cpp index 57113df..ed792f9 100644 --- a/guecs.cpp +++ b/guecs.cpp @@ -259,7 +259,7 @@ namespace guecs { bool UI::mouse(float x, float y, bool hover) { int action_count = 0; - $world.query([&](auto ent, auto& cell, auto &clicked) { + $world.query([&](auto ent, auto& cell) { if((x >= cell.x && x <= cell.x + cell.w) && (y >= cell.y && y <= cell.y + cell.h)) { @@ -275,11 +275,7 @@ namespace guecs { if(hover) return; - if(auto action_data = get_if(ent)) { - clicked.action(ent, action_data->data); - } else { - clicked.action(ent, {}); - } + click_on(ent); action_count++; } else { @@ -317,6 +313,27 @@ namespace guecs { } } + void UI::click_on(const std::string& name, bool required) { + auto ent = entity(name); + + if(required) { + dbc::check(has(ent), + fmt::format("click_on required '{}' to exist but it doesn't", name)); + } + + click_on(ent); + } + + void UI::click_on(DinkyECS::Entity ent) { + if(auto clicked = get_if(ent)) { + if(auto action_data = get_if(ent)) { + clicked->action(ent, action_data->data); + } else { + clicked->action(ent, {}); + } + } + } + void UI::show_label(string region, wstring content) { auto ent = entity(region); diff --git a/guecs.hpp b/guecs.hpp index ee5f568..d7f7ac5 100644 --- a/guecs.hpp +++ b/guecs.hpp @@ -155,6 +155,8 @@ namespace guecs { void init(); void render(sf::RenderWindow& window); bool mouse(float x, float y, bool hover); + void click_on(const std::string& name, bool required=false); + void click_on(DinkyECS::Entity slot_id); void debug_layout(sf::RenderWindow& window); template