Created a nice utility library for doing animations, and used it in the ritual crafting UI.
This commit is contained in:
parent
0a40135f5d
commit
1aa6674e42
14 changed files with 213 additions and 150 deletions
|
@ -2,6 +2,7 @@
|
|||
#include "components.hpp"
|
||||
#include "guecs.hpp"
|
||||
#include "rand.hpp"
|
||||
#include "animation.hpp"
|
||||
|
||||
namespace gui {
|
||||
using namespace guecs;
|
||||
|
@ -13,12 +14,12 @@ namespace gui {
|
|||
$gui.position(STATUS_UI_X, STATUS_UI_Y, STATUS_UI_WIDTH, STATUS_UI_HEIGHT);
|
||||
$gui.layout(
|
||||
"[_]"
|
||||
"[inv_slot5 | inv_slot6 | inv_slot7| inv_slot8]"
|
||||
"[inv_slot9 | inv_slot10 | inv_slot11| inv_slot12]"
|
||||
"[inv_slot13 | inv_slot14 | inv_slot15| inv_slot16]"
|
||||
"[inv_slot17 | inv_slot18 | inv_slot19| inv_slot20]"
|
||||
"[inv_slot21 | inv_slot22 | inv_slot23| inv_slot24]"
|
||||
"[*%(100,500)circle_area]"
|
||||
"[*%(100,600)circle_area]"
|
||||
"[_]"
|
||||
"[_]"
|
||||
"[_]"
|
||||
"[_]"
|
||||
|
@ -27,17 +28,17 @@ namespace gui {
|
|||
}
|
||||
|
||||
void RitualUI::init() {
|
||||
// $gui.world().set_the<Background>({$gui.$parser});
|
||||
|
||||
for(auto& [name, cell] : $gui.cells()) {
|
||||
auto button = $gui.entity(name);
|
||||
|
||||
if(name == "circle_area") {
|
||||
// $gui.set<Rectangle>(button, {});
|
||||
$gui.set<Sprite>(button, {"the_ritual_circle"});
|
||||
$gui.set<Clickable>(button, {
|
||||
[this](auto, auto){ dbc::log("circle clicked"); }
|
||||
});
|
||||
} else if(name.starts_with("inv_slot")) {
|
||||
$gui.set<Sprite>(button, {"the_ritual_circle"});
|
||||
// $gui.set<Rectangle>(button, {});
|
||||
$gui.set<Clickable>(button, {
|
||||
[this, name](auto, auto){ dbc::log(fmt::format("inv_slot {}", name)); }
|
||||
|
@ -53,13 +54,7 @@ namespace gui {
|
|||
$ritual_ui.sprite->setPosition({0,0});
|
||||
$ritual_ui.sprite->setTextureRect($ritual_closed_rect);
|
||||
$ritual_state = RitualUIState::CLOSED;
|
||||
$ritual_anim.simple = false;
|
||||
$ritual_anim.looped = false;
|
||||
$ritual_anim.easing = ease::NONE;
|
||||
$ritual_anim.stationary = true;
|
||||
$ritual_anim.texture_width = 380;
|
||||
$ritual_anim.frames = 3;
|
||||
$ritual_anim.speed = 0.2f;
|
||||
$ritual_anim = animation::load("ritual_blanket");
|
||||
|
||||
$gui.init();
|
||||
}
|
||||
|
@ -75,20 +70,11 @@ namespace gui {
|
|||
void RitualUI::toggle() {
|
||||
using enum RitualUIState;
|
||||
|
||||
switch($ritual_state) {
|
||||
case OPEN:
|
||||
$ritual_state = CLOSING;
|
||||
break;
|
||||
case CLOSED:
|
||||
$ritual_state = OPENING;
|
||||
$ritual_anim.play();
|
||||
break;
|
||||
case OPENING: // ignored
|
||||
break;
|
||||
case CLOSING: // ignored
|
||||
break;
|
||||
default:
|
||||
dbc::sentinel("INVALID RitualUIState");
|
||||
if($ritual_state == OPEN) {
|
||||
$ritual_state = CLOSING;
|
||||
} else if($ritual_state == CLOSED) {
|
||||
$ritual_state = OPENING;
|
||||
$ritual_anim.play();
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -98,40 +84,16 @@ namespace gui {
|
|||
}
|
||||
|
||||
void RitualUI::render(sf::RenderWindow &window) {
|
||||
sf::IntRect rect;
|
||||
sf::Vector2f scale{1.0, 1.0};
|
||||
sf::Vector2f pos{0, 0};
|
||||
|
||||
using enum RitualUIState;
|
||||
switch($ritual_state) {
|
||||
case OPEN: {
|
||||
rect = $ritual_open_rect;
|
||||
} break;
|
||||
case CLOSED: {
|
||||
rect = $ritual_closed_rect;
|
||||
}
|
||||
break;
|
||||
case OPENING: {
|
||||
if($ritual_anim.playing) {
|
||||
rect = $ritual_closed_rect;
|
||||
$ritual_anim.step(scale, pos, rect);
|
||||
} else {
|
||||
$ritual_state = OPEN;
|
||||
rect = $ritual_open_rect;
|
||||
}
|
||||
}
|
||||
break;
|
||||
case CLOSING: {
|
||||
rect = $ritual_closed_rect;
|
||||
$ritual_state = CLOSED;
|
||||
} break;
|
||||
default:
|
||||
dbc::sentinel("INVALID RitualUIState");
|
||||
}
|
||||
|
||||
$ritual_ui.sprite->setTextureRect(rect);
|
||||
$ritual_ui.sprite->setPosition(pos);
|
||||
$ritual_ui.sprite->setScale(scale);
|
||||
if($ritual_state == OPENING) {
|
||||
if(!animation::apply($ritual_anim, $ritual_ui)) {
|
||||
$ritual_state = OPEN;
|
||||
}
|
||||
} else if($ritual_state == CLOSING) {
|
||||
$ritual_ui.sprite->setTextureRect($ritual_closed_rect);
|
||||
$ritual_state = CLOSED;
|
||||
}
|
||||
|
||||
window.draw(*$ritual_ui.sprite);
|
||||
if($ritual_state == OPEN) $gui.render(window);
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue