From fedfcd46a30b054982ea1b4ccad84e9a09a314f4 Mon Sep 17 00:00:00 2001 From: John McCardle Date: Tue, 7 Mar 2023 20:03:09 -0500 Subject: [PATCH] Test animation now moves the entire UIMenu object (and children) --- src/Animation.cpp | 12 ++++++------ src/Animation.h | 4 ++-- src/Button.h | 3 +++ src/McRFPy_API.cpp | 7 ++++++- src/PythonScene.cpp | 10 ++++++++-- src/UIMenu.cpp | 17 +++++++++++++---- 6 files changed, 38 insertions(+), 15 deletions(-) diff --git a/src/Animation.cpp b/src/Animation.cpp index cc9d0b2..0ed59b3 100644 --- a/src/Animation.cpp +++ b/src/Animation.cpp @@ -108,12 +108,12 @@ bool Animation::isDone() { namespace animation_template_implementations { // instantiate to compile concrete templates - LerpAnimation implement_vector2f; + //LerpAnimation implement_vector2f; auto implement_v2f_const = LerpAnimation>(4.0, sf::Vector2(), sf::Vector2f(1,1), [](){}, [](sf::Vector2f v){}, false); - LerpAnimation implement_vector2i; - LerpAnimation implment_int; - LerpAnimation implment_string; - LerpAnimation implement_float; - DiscreteAnimation implement_int_discrete; + //LerpAnimation implement_vector2i; + //LerpAnimation implment_int; + //LerpAnimation implment_string; + //LerpAnimation implement_float; + //DiscreteAnimation implement_int_discrete; } diff --git a/src/Animation.h b/src/Animation.h index e8a80e6..8c56dab 100644 --- a/src/Animation.h +++ b/src/Animation.h @@ -12,7 +12,7 @@ public: //Animation(float, T, T*, std::function, bool); // lerp //Animation(float, std::vector, T*, std::function, bool); // discrete Animation(float, std::function, 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, std::function, bool); - LerpAnimation() {}; + //LerpAnimation() {}; void step(float) override final; void cancel() override final; }; diff --git a/src/Button.h b/src/Button.h index 7d207b9..189e0d3 100644 --- a/src/Button.h +++ b/src/Button.h @@ -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: diff --git a/src/McRFPy_API.cpp b/src/McRFPy_API.cpp index 0382aa1..6de00c2 100644 --- a/src/McRFPy_API.cpp +++ b/src/McRFPy_API.cpp @@ -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) ); diff --git a/src/PythonScene.cpp b/src/PythonScene.cpp index 3245ac6..911fb34 100644 --- a/src/PythonScene.cpp +++ b/src/PythonScene.cpp @@ -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; } diff --git a/src/UIMenu.cpp b/src/UIMenu.cpp index 485df59..61f09c0 100644 --- a/src/UIMenu.cpp +++ b/src/UIMenu.cpp @@ -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); }