Test animation now moves the entire UIMenu object (and children)

This commit is contained in:
John McCardle 2023-03-07 20:03:09 -05:00
parent c551c721ce
commit fedfcd46a3
6 changed files with 38 additions and 15 deletions

View File

@ -108,12 +108,12 @@ bool Animation::isDone() {
namespace animation_template_implementations {
// instantiate to compile concrete templates
LerpAnimation<sf::Vector2f> implement_vector2f;
//LerpAnimation<sf::Vector2f> implement_vector2f;
auto implement_v2f_const = LerpAnimation<sf::Vector2<float>>(4.0, sf::Vector2<float>(), sf::Vector2f(1,1), [](){}, [](sf::Vector2f v){}, false);
LerpAnimation<sf::Vector2i> implement_vector2i;
LerpAnimation<int> implment_int;
LerpAnimation<std::string> implment_string;
LerpAnimation<float> implement_float;
DiscreteAnimation<int> implement_int_discrete;
//LerpAnimation<sf::Vector2i> implement_vector2i;
//LerpAnimation<int> implment_int;
//LerpAnimation<std::string> implment_string;
//LerpAnimation<float> implement_float;
//DiscreteAnimation<int> implement_int_discrete;
}

View File

@ -12,7 +12,7 @@ public:
//Animation(float, T, T*, std::function<void()>, bool); // lerp
//Animation(float, std::vector<T>, T*, std::function<void()>, bool); // discrete
Animation(float, std::function<void()>, bool);
Animation() {};
//Animation() {};
virtual void step(float) = 0;
virtual void cancel() = 0;
bool isDone();
@ -27,7 +27,7 @@ class LerpAnimation: public Animation
public:
~LerpAnimation() { cancel(); }
LerpAnimation(float, T, T, std::function<void()>, std::function<void(T)>, bool);
LerpAnimation() {};
//LerpAnimation() {};
void step(float) override final;
void cancel() override final;
};

View File

@ -26,6 +26,9 @@ public:
void setTextColor(sf::Color c) { caption.setFillColor(c); }
void render(sf::RenderWindow & window);
auto contains(sf::Vector2i p) { return rect.getGlobalBounds().contains(p.x, p.y); }
auto contains(sf::Vector2f rel, sf::Vector2i p) {
return rect.getGlobalBounds().contains(p.x - rel.x, p.y - rel.y);
}
auto getAction() { return action; }
private:

View File

@ -639,7 +639,12 @@ PyObject* McRFPy_API::_createAnimation(PyObject *self, PyObject *args) {
sf::Vector2f(100, 100),
McRFPy_API::menus[menu_key]->box.getPosition(),
[](){McRFPy_API::executePyString("print('animation callback')");},
[=](sf::Vector2f v) {std::cout << "write lambda!" << std::endl; McRFPy_API::menus[menu_key]->box.setPosition(v); std::cout << "Position set to" << McRFPy_API::menus[menu_key]->box.getPosition().x << ", " << McRFPy_API::menus[menu_key]->box.getPosition().y << std::endl;},
[=](sf::Vector2f v) {
std::cout << "write lambda!" << std::endl;
McRFPy_API::menus[menu_key]->box.setPosition(v);
std::cout << "Position set to" << McRFPy_API::menus[menu_key]->box.getPosition().x
<< ", " << McRFPy_API::menus[menu_key]->box.getPosition().y << std::endl;
},
false)
);

View File

@ -44,7 +44,7 @@ PythonScene::PythonScene(GameEngine* g, std::string pymodule)
}
void PythonScene::animate() {
std::cout << "Number of animations: " << McRFPy_API::animations.size() << std::endl;
//std::cout << "Number of animations: " << McRFPy_API::animations.size() << std::endl;
auto frametime = game->getFrameTime();
auto it = McRFPy_API::animations.begin();
while (it != McRFPy_API::animations.end()) {
@ -122,7 +122,13 @@ void PythonScene::doLClick(sf::Vector2i mousepos) {
for (auto pair : McRFPy_API::menus) {
if (!pair.second->visible) continue;
for (auto b : pair.second->buttons) {
if (b.contains(mousepos)) {
//std::cout << "Box: " << pair.second->box.getPosition().x << ", "
//<< pair.second->box.getPosition().y << "; Button:" << b.rect.getPosition().x <<
//", " << b.rect.getPosition().y << "; Mouse: " << mousepos.x << ", " <<
//mousepos.y << std::endl;
// JANK: provide the button a relative coordinate.
if (b.contains(pair.second->box.getPosition(), mousepos)) {
McRFPy_API::doAction(b.getAction());
return;
}

View File

@ -21,9 +21,18 @@ void UIMenu::render(sf::RenderWindow & window)
for (auto& c : captions) {
//auto s = std::string(c.getString());
//std::cout << s << std::flush << std::endl;
c.move(box.getPosition());
window.draw(c);
c.move(-box.getPosition());
}
for (auto& b : buttons) { b.render(window); }
for (auto& b : buttons) {
//b.render(window);
b.setPosition(box.getPosition() + b.rect.getPosition());
//b.caption.setPosition(box.getPosition() + b.caption.getPosition());
b.render(window);
b.setPosition(b.rect.getPosition() - box.getPosition());
//b.caption.setPosition(b.caption.getPosition() - box.getPosition());
}
}
void UIMenu::refresh()
@ -34,10 +43,10 @@ void UIMenu::refresh()
void UIMenu::add_caption(const char* text, int tsize, sf::Color color)
{
auto c = sf::Text();
auto bpos = box.getPosition();
//auto bpos = box.getPosition();
c.setFillColor(color);
c.setPosition(bpos.x + 10, bpos.y + next_text);
c.setPosition(10, next_text);
next_text += 50;
c.setCharacterSize(tsize);
c.setString(text);
@ -48,7 +57,7 @@ void UIMenu::add_caption(const char* text, int tsize, sf::Color color)
void UIMenu::add_button(Button b)
{
b.setPosition(box.getPosition() + sf::Vector2f(box.getSize().x / 2.0f, next_button));
b.setPosition(sf::Vector2f(box.getSize().x / 2.0f, next_button));
next_button += 50;
buttons.push_back(b);
}