Skip to content

Instantly share code, notes, and snippets.

@jacmoe
Forked from Kojirion/SlidingConsole.cpp
Created January 28, 2021 22:54
Show Gist options
  • Save jacmoe/e73c07e3e7fd7e845a34e2a7c0ebe81c to your computer and use it in GitHub Desktop.
Save jacmoe/e73c07e3e7fd7e845a34e2a7c0ebe81c to your computer and use it in GitHub Desktop.
Animating movement between two points with Thor - a SFML extension
#include <SFML/Graphics.hpp>
#include <Thor/Animations.hpp>
#include <Thor/Input.hpp>
#include <SFGUI/SFGUI.hpp>
#include <SFGUI/Widgets.hpp>
template <class Item>
void SetPosition(Item&, const sf::Vector2f&);
template <>
void SetPosition(sf::Transformable& transformable, const sf::Vector2f& pos){
transformable.setPosition(pos);
}
template <>
void SetPosition(sfg::Widget::Ptr& widget, const sf::Vector2f& pos){
widget->SetPosition(pos);
}
struct MoveAnimation{
MoveAnimation(const sf::Vector2f& from, const sf::Vector2f& to):
from(from),
direction(to-from)
{
}
template <typename Animated>
void operator()(Animated& animated, float progress){
auto pos = from + progress*direction;
SetPosition(animated, pos);
}
const sf::Vector2f from;
const sf::Vector2f direction;
};
int main()
{
sf::RenderWindow window(sf::VideoMode(800, 600), "Move animation");
window.setFramerateLimit(60);
//set up console
sfg::SFGUI sfgui;
sfg::Desktop desktop;
auto console = sfg::Window::Create(sfg::Window::SHADOW);
auto box = sfg::Box::Create(sfg::Box::Orientation::VERTICAL, 5.f);
auto label = sfg::Label::Create();
auto entry = sfg::Entry::Create();
auto& engine = desktop.GetEngine();
engine.SetProperty("Entry", "BackgroundColor", sf::Color::Transparent);
engine.SetProperty("Scrollbar", "SliderColor", sf::Color::Transparent);
engine.SetProperty("Scrollbar", "TroughColor", sf::Color::Transparent);
engine.SetProperty("Scrollbar", "StepperBackgroundColor", sf::Color::Transparent);
auto scrolledWindow = sfg::ScrolledWindow::Create();
scrolledWindow->SetRequisition({750.f,100.f});
scrolledWindow->SetScrollbarPolicy( sfg::ScrolledWindow::HORIZONTAL_AUTOMATIC | sfg::ScrolledWindow::VERTICAL_ALWAYS );
scrolledWindow->AddWithViewport(label);
box->Pack(scrolledWindow);
box->Pack(entry);
console->Add(box);
auto height = console->GetAllocation().height;
sf::Vector2f initialPos(0.f, -height); //place just out of screen
console->SetPosition(initialPos);
console->Show(false);
desktop.Add(console);
//set up animations
thor::AnimationMap<sfg::Widget::Ptr, std::string> animationMap;
thor::Animator<sfg::Widget::Ptr, std::string> animator(animationMap);
sf::Vector2f activePos(0.f, 0.f);
MoveAnimation slideDown(initialPos, activePos),
slideUp(activePos, initialPos);
auto duration = sf::seconds(0.5f);
animationMap.addAnimation("SlideDown", slideDown, duration);
animationMap.addAnimation("SlideUp", slideUp, duration);
//input
thor::ActionMap<std::string> actionMap;
thor::ActionMap<std::string>::CallbackSystem system;
actionMap["Close"] = thor::Action(sf::Event::Closed);
system.connect0("Close", [&window] {
window.close();
});
bool moving = false;
actionMap["ToggleConsole"] = thor::Action(sf::Keyboard::F2, thor::Action::PressOnce);
system.connect0("ToggleConsole", [&animator, console, &moving] {
if (moving)
return;
if (console->IsLocallyVisible()){
moving = true;
animator.play() << "SlideUp" << thor::Playback::notify([console, &moving]{
console->Show(false);
moving = false;
});
}else{
console->Show(true);
moving = true;
animator.play() << "SlideDown" << thor::Playback::notify([&moving]{
moving = false;
});
}
});
//main loop
sf::Clock clock;
while (window.isOpen()){
actionMap.update(window);
actionMap.invokeCallbacks(system, &window);
auto frameTime = clock.restart();
animator.update(frameTime);
sfg::Widget::Ptr widget = console; //necessary to pass base class ptr
animator.animate(widget);
desktop.Update(frameTime.asSeconds());
window.clear();
sfgui.Display(window);
window.display();
}
}
#include <SFML/Graphics.hpp>
#include <Thor/Animations.hpp>
#include <Thor/Input.hpp>
template <class Item>
void SetPosition(Item&, const sf::Vector2f&);
template <>
void SetPosition(sf::Transformable& transformable, const sf::Vector2f& pos){
transformable.setPosition(pos);
}
struct MoveAnimation{
MoveAnimation(const sf::Vector2f& from, const sf::Vector2f& to):
from(from),
direction(to-from)
{
}
template <typename Animated>
void operator()(Animated& animated, float progress){
auto pos = from + progress*direction;
SetPosition(animated, pos);
}
const sf::Vector2f from;
const sf::Vector2f direction;
};
int main()
{
sf::RenderWindow window(sf::VideoMode(600, 600), "Move animation");
//input
thor::ActionMap<std::string> actionMap;
thor::ActionMap<std::string>::CallbackSystem system;
actionMap["Close"] = thor::Action(sf::Event::Closed);
system.connect0("Close", [&window] {
window.close();
});
//shape to animate
sf::RectangleShape shape({100.f, 100.f});
shape.setFillColor(sf::Color::Green);
shape.setPosition({100.f, 100.f});
//set up animation
thor::AnimationMap<sf::Transformable, std::string> animationMap;
thor::Animator<sf::Transformable, std::string> animator(animationMap);
auto from = shape.getPosition();
auto to = sf::Vector2f(400.f, 400.f);
MoveAnimation move(from, to);
auto duration = sf::seconds(1.f);
animationMap.addAnimation("Move", move, duration);
animator.play() << "Move";
//main loop
sf::Clock clock;
while (window.isOpen()){
actionMap.update(window);
actionMap.invokeCallbacks(system, &window);
//animate shape
auto frameTime = clock.restart();
animator.update(frameTime);
animator.animate(shape);
window.clear();
window.draw(shape);
window.display();
}
}
#include <SFML/Graphics.hpp>
#include <Thor/Animations.hpp>
#include <Thor/Input.hpp>
struct MoveAnimation{
MoveAnimation(const sf::Vector2f& from, const sf::Vector2f& to):
from(from),
direction(to-from)
{
}
void operator()(sf::Transformable& animated, float progress){
auto pos = from + progress*direction;
animated.setPosition(pos);
}
const sf::Vector2f from;
const sf::Vector2f direction;
};
int main()
{
sf::RenderWindow window(sf::VideoMode(600, 600), "Move animation");
//input
thor::ActionMap<std::string> actionMap;
thor::ActionMap<std::string>::CallbackSystem system;
actionMap["Close"] = thor::Action(sf::Event::Closed);
system.connect0("Close", [&window] {
window.close();
});
//shape to animate
sf::RectangleShape shape({100.f, 100.f});
shape.setFillColor(sf::Color::Green);
shape.setPosition({100.f, 100.f});
//set up animation
thor::AnimationMap<sf::Transformable, std::string> animationMap;
thor::Animator<sf::Transformable, std::string> animator(animationMap);
auto from = shape.getPosition();
auto to = sf::Vector2f(400.f, 400.f);
MoveAnimation move(from, to);
auto duration = sf::seconds(1.f);
animationMap.addAnimation("Move", move, duration);
animator.play() << "Move";
//main loop
sf::Clock clock;
while (window.isOpen()){
actionMap.update(window);
actionMap.invokeCallbacks(system, &window);
//animate shape
auto frameTime = clock.restart();
animator.update(frameTime);
animator.animate(shape);
window.clear();
window.draw(shape);
window.display();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment