parent
cf3da32681
commit
04f6fef921
After Width: | Height: | Size: 808 KiB |
@ -0,0 +1,110 @@ |
||||
#include "guecs/sfml/backend.hpp" |
||||
#include "guecs/sfml/components.hpp" |
||||
#include "guecs/ui.hpp" |
||||
#include <fmt/xchar.h> |
||||
#include <deque> |
||||
|
||||
constexpr const int WINDOW_WIDTH=1280; |
||||
constexpr const int WINDOW_HEIGHT=720; |
||||
constexpr const int FRAME_LIMIT=60; |
||||
constexpr const bool VSYNC=true; |
||||
|
||||
using std::string, std::wstring; |
||||
|
||||
enum class Event { |
||||
CLICKER, A_BUTTON |
||||
}; |
||||
|
||||
|
||||
struct ClickerUI { |
||||
guecs::UI $gui; |
||||
|
||||
ClickerUI() { |
||||
$gui.position(0, 0, WINDOW_WIDTH, WINDOW_HEIGHT); |
||||
$gui.layout( |
||||
"[_|*%(300,400)clicker|_|_|_]" |
||||
"[_|_ |_|_|_]" |
||||
"[_|_ |_|_|_]" |
||||
"[_|_ |_|_|_]" |
||||
"[a1|a2|a3|a4|a5]"); |
||||
} |
||||
|
||||
void init() { |
||||
$gui.set<guecs::Background>($gui.MAIN, {}); |
||||
|
||||
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::Label>(id, { guecs::to_wstring(name) }); |
||||
$gui.set<guecs::Clickable>(id, { |
||||
[&](auto, auto) { handle_button(Event::A_BUTTON); } |
||||
}); |
||||
} else { |
||||
$gui.set<guecs::Sprite>(id, {"clicker_the_dog"}); |
||||
$gui.set<guecs::Effect>(id, {0.1f, "ui_shader"}); |
||||
$gui.set<guecs::Clickable>(id, { |
||||
[&](auto, auto) { handle_button(Event::CLICKER); } |
||||
}); |
||||
} |
||||
} |
||||
|
||||
$gui.init(); |
||||
} |
||||
|
||||
void render(sf::RenderWindow& window) { |
||||
$gui.render(window); |
||||
// $gui.debug_layout(window);
|
||||
} |
||||
|
||||
void mouse(float x, float y, bool hover) { |
||||
$gui.mouse(x, y, hover); |
||||
} |
||||
|
||||
void handle_button(Event ev) { |
||||
using enum Event; |
||||
switch(ev) { |
||||
case CLICKER: |
||||
fmt::println("CLICKER LOVES YOU!"); |
||||
break; |
||||
|
||||
case A_BUTTON: |
||||
fmt::println("a button clicked"); |
||||
break; |
||||
|
||||
default: |
||||
assert(false && "invalid event"); |
||||
} |
||||
} |
||||
}; |
||||
|
||||
int main() { |
||||
sfml::Backend backend; |
||||
guecs::init(&backend); |
||||
|
||||
sf::RenderWindow window(sf::VideoMode({WINDOW_WIDTH, WINDOW_HEIGHT}), "LEL-GUECS Calculator"); |
||||
window.setFramerateLimit(FRAME_LIMIT); |
||||
window.setVerticalSyncEnabled(VSYNC); |
||||
|
||||
ClickerUI clicker; |
||||
clicker.init(); |
||||
|
||||
while(window.isOpen()) { |
||||
while (const auto event = window.pollEvent()) { |
||||
if(event->is<sf::Event::Closed>()) { |
||||
window.close(); |
||||
} |
||||
|
||||
if(const auto* mouse = event->getIf<sf::Event::MouseButtonPressed>()) { |
||||
if(mouse->button == sf::Mouse::Button::Left) { |
||||
sf::Vector2f pos = window.mapPixelToCoords(mouse->position); |
||||
clicker.mouse(pos.x, pos.y, false); |
||||
} |
||||
} |
||||
} |
||||
|
||||
clicker.render(window); |
||||
window.display(); |
||||
} |
||||
} |
Loading…
Reference in new issue