Get the FSM for the GUIworked out and fix the rendering so it works right.
This commit is contained in:
parent
81cbc24064
commit
d81e127686
3 changed files with 95 additions and 61 deletions
|
|
@ -15,38 +15,103 @@
|
|||
|
||||
using namespace std::chrono_literals;
|
||||
|
||||
struct UI {
|
||||
guecs::UI $ui;
|
||||
namespace animator {
|
||||
struct UI {
|
||||
guecs::UI $ui;
|
||||
|
||||
void init(const std::string& sprite_name) {
|
||||
$ui.position(0,0, SCREEN_WIDTH, SCREEN_HEIGHT);
|
||||
$ui.layout(
|
||||
"[*%(100,200)data|*%=(300,200)viewer|_|_]"
|
||||
"[_|_|_|_]");
|
||||
void init(const std::string& sprite_name) {
|
||||
$ui.position(0,0, SCREEN_WIDTH, SCREEN_HEIGHT);
|
||||
$ui.layout(
|
||||
"[*%(100,200)data|*%=(300,200)viewer|_|_]"
|
||||
"[_|_|_|_]");
|
||||
|
||||
for(auto& [name, cell] : $ui.cells()) {
|
||||
auto comp = $ui.entity(name);
|
||||
if(name == "viewer") {
|
||||
$ui.set<guecs::Sprite>(comp, {
|
||||
sprite_name, 0, false});
|
||||
} else {
|
||||
$ui.set<guecs::Rectangle>(comp, {});
|
||||
$ui.set<guecs::Text>(comp, {guecs::to_wstring(name)});
|
||||
$ui.set<guecs::Effect>(comp, {});
|
||||
for(auto& [name, cell] : $ui.cells()) {
|
||||
auto comp = $ui.entity(name);
|
||||
if(name == "viewer") {
|
||||
$ui.set<guecs::Sprite>(comp, {
|
||||
sprite_name, 0, false});
|
||||
} else {
|
||||
$ui.set<guecs::Rectangle>(comp, {});
|
||||
$ui.set<guecs::Text>(comp, {guecs::to_wstring(name)});
|
||||
$ui.set<guecs::Effect>(comp, {});
|
||||
}
|
||||
}
|
||||
|
||||
$ui.init();
|
||||
}
|
||||
|
||||
void render(sf::RenderWindow& window) {
|
||||
$ui.render(window);
|
||||
}
|
||||
|
||||
bool mouse(float x, float y, guecs::Modifiers mods) {
|
||||
return $ui.mouse(x, y, mods);
|
||||
}
|
||||
};
|
||||
|
||||
enum class State {
|
||||
START=__LINE__,
|
||||
END=__LINE__,
|
||||
};
|
||||
|
||||
struct FSM : public DeadSimpleFSM<State, game::Event> {
|
||||
UI $ui;
|
||||
gui::routing::Router $router;
|
||||
sf::RenderWindow $window{sf::VideoMode({SCREEN_WIDTH, SCREEN_HEIGHT}), "Animation Crafting Tool"};
|
||||
|
||||
void init(const std::string &sprite_name) {
|
||||
$ui.init(sprite_name);
|
||||
$window.setVerticalSyncEnabled(VSYNC);
|
||||
if(FRAME_LIMIT) $window.setFramerateLimit(FRAME_LIMIT);
|
||||
$window.setPosition({0,0});
|
||||
}
|
||||
|
||||
void event(game::Event ev, std::any data={}) {
|
||||
switch($state) {
|
||||
FSM_STATE(State, START, ev);
|
||||
FSM_STATE(State, END, ev);
|
||||
}
|
||||
}
|
||||
|
||||
$ui.init();
|
||||
}
|
||||
void START(game::Event ev) {
|
||||
state(State::START);
|
||||
}
|
||||
|
||||
void render(sf::RenderWindow& window) {
|
||||
$ui.render(window);
|
||||
}
|
||||
void END(game::Event ev) {
|
||||
}
|
||||
|
||||
bool mouse(float x, float y, guecs::Modifiers mods) {
|
||||
return $ui.mouse(x, y, mods);
|
||||
}
|
||||
};
|
||||
void handle_keyboard_mouse() {
|
||||
while(const auto ev = $window.pollEvent()) {
|
||||
using enum game::Event;
|
||||
auto gui_ev = $router.process_event(ev);
|
||||
auto mouse_pos = $window.mapPixelToCoords($router.position);
|
||||
|
||||
switch(gui_ev) {
|
||||
case MOUSE_CLICK:
|
||||
$ui.mouse(mouse_pos.x, mouse_pos.y, guecs::NO_MODS);
|
||||
break;
|
||||
case MOUSE_MOVE:
|
||||
$ui.mouse(mouse_pos.x, mouse_pos.y, {1 << guecs::ModBit::hover});
|
||||
break;
|
||||
case QUIT:
|
||||
state(State::END);
|
||||
default:
|
||||
break; // ignored
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void render() {
|
||||
$window.clear();
|
||||
$ui.render($window);
|
||||
$window.display();
|
||||
}
|
||||
|
||||
bool active() {
|
||||
return !in_state(State::END);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
int main(int argc, char* argv[]) {
|
||||
shaders::init();
|
||||
|
|
@ -59,44 +124,15 @@ int main(int argc, char* argv[]) {
|
|||
dbc::check(argc == 2, "USAGE: animator <sprite>");
|
||||
std::string sprite_name{argv[1]};
|
||||
|
||||
sf::RenderWindow window(sf::VideoMode({SCREEN_WIDTH, SCREEN_HEIGHT}), "Animation Crafting Tool");
|
||||
window.setVerticalSyncEnabled(VSYNC);
|
||||
|
||||
if(FRAME_LIMIT) window.setFramerateLimit(FRAME_LIMIT);
|
||||
window.setPosition({0,0});
|
||||
|
||||
sound::mute(true);
|
||||
sound::play("ambient_1", true);
|
||||
|
||||
UI main{};
|
||||
main.init(sprite_name);
|
||||
gui::routing::Router router;
|
||||
animator::FSM main;
|
||||
main.init(argv[1]);
|
||||
|
||||
while(true) {
|
||||
while(const auto ev = window.pollEvent()) {
|
||||
using enum game::Event;
|
||||
auto gui_ev = router.process_event(ev);
|
||||
auto mouse_pos = window.mapPixelToCoords(router.position);
|
||||
|
||||
switch(gui_ev) {
|
||||
case MOUSE_CLICK:
|
||||
main.mouse(mouse_pos.x, mouse_pos.y, guecs::NO_MODS);
|
||||
break;
|
||||
case MOUSE_MOVE:
|
||||
main.mouse(mouse_pos.x, mouse_pos.y, {1 << guecs::ModBit::hover});
|
||||
break;
|
||||
case QUIT:
|
||||
return 0;
|
||||
default:
|
||||
break; // ignored
|
||||
}
|
||||
|
||||
window.clear();
|
||||
main.render(window);
|
||||
window.display();
|
||||
}
|
||||
|
||||
std::this_thread::sleep_for(10ms);
|
||||
while(main.active()) {
|
||||
main.render();
|
||||
main.handle_keyboard_mouse();
|
||||
}
|
||||
|
||||
return 0;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue