diff --git a/examples/projects/Youbot/KeyController.hpp b/examples/projects/Youbot/KeyController.hpp new file mode 100644 index 00000000..1f1ddc02 --- /dev/null +++ b/examples/projects/Youbot/KeyController.hpp @@ -0,0 +1,66 @@ + +#ifndef THREEPP_KEYCONTROLLER_HPP +#define THREEPP_KEYCONTROLLER_HPP + +#include "threepp/input/KeyListener.hpp" + +#include "Youbot.hpp" + +class KeyController: public threepp::KeyListener { + +public: + KeyController(Youbot& youbot) + : youbot_(&youbot) {} + + void onKeyPressed(KeyEvent evt) override { + if (evt.key == Key::W) { + keyState_.up = true; + } else if (evt.key == Key::S) { + keyState_.down = true; + } else if (evt.key == Key::D) { + keyState_.right = true; + } else if (evt.key == Key::A) { + keyState_.left = true; + } + } + + void onKeyReleased(KeyEvent evt) override { + if (evt.key == Key::W) { + keyState_.up = false; + } else if (evt.key == Key::S) { + keyState_.down = false; + } else if (evt.key == Key::D) { + keyState_.right = false; + } else if (evt.key == Key::A) { + keyState_.left = false; + } + } + + void update(float dt) { + + if (keyState_.up) { + youbot_->driveForwards(dt); + } + if (keyState_.down) { + youbot_->driveBackwards(dt); + } + if (keyState_.right) { + youbot_->driveRight(dt); + } + if (keyState_.left) { + youbot_->driveLeft(dt); + } + } + +private: + Youbot* youbot_; + + struct KeyState { + bool left = false; + bool right = false; + bool up = false; + bool down = false; + } keyState_; +}; + +#endif//THREEPP_KEYCONTROLLER_HPP diff --git a/examples/projects/Youbot/Youbot.hpp b/examples/projects/Youbot/Youbot.hpp index a76dbeed..932962fa 100644 --- a/examples/projects/Youbot/Youbot.hpp +++ b/examples/projects/Youbot/Youbot.hpp @@ -8,32 +8,9 @@ using namespace threepp; -struct Youbot: Object3D, KeyListener { - - void onKeyPressed(KeyEvent evt) override { - if (evt.key == Key::W) { - keyState_.up = true; - } else if (evt.key == Key::S) { - keyState_.down = true; - } else if (evt.key == Key::D) { - keyState_.right = true; - } else if (evt.key == Key::A) { - keyState_.left = true; - } - } - - void onKeyReleased(KeyEvent evt) override { - if (evt.key == Key::W) { - keyState_.up = false; - } else if (evt.key == Key::S) { - keyState_.down = false; - } else if (evt.key == Key::D) { - keyState_.right = false; - } else if (evt.key == Key::A) { - keyState_.left = false; - } - } +class Youbot: public Object3D { +public: void driveForwards(float dt) { float scale = 100; translateX(translationSpeed * dt); @@ -88,22 +65,6 @@ struct Youbot: Object3D, KeyListener { }; } - void update(float dt) { - - if (keyState_.up) { - driveForwards(dt); - } - if (keyState_.down) { - driveBackwards(dt); - } - if (keyState_.right) { - driveRight(dt); - } - if (keyState_.left) { - driveLeft(dt); - } - } - static std::unique_ptr create(const std::filesystem::path& path) { AssimpLoader loader; auto model = loader.load(path); @@ -113,15 +74,6 @@ struct Youbot: Object3D, KeyListener { } private: - struct KeyState { - bool left = false; - bool right = false; - bool up = false; - bool down = false; - }; - - - KeyState keyState_; float rotationSpeed = 2; float translationSpeed = 5; diff --git a/examples/projects/Youbot/youbot.cpp b/examples/projects/Youbot/youbot.cpp index 2f55a25f..3692dce1 100644 --- a/examples/projects/Youbot/youbot.cpp +++ b/examples/projects/Youbot/youbot.cpp @@ -1,6 +1,7 @@ #include "threepp/threepp.hpp" +#include "KeyController.hpp" #include "Youbot.hpp" #include @@ -45,10 +46,12 @@ int main() { std::shared_ptr youbot; + std::unique_ptr keyController; auto future = std::async([&] { youbot = Youbot::create("data/models/collada/youbot.dae"); + keyController = std::make_unique(*youbot); renderer.invokeLater([&] { - canvas.addKeyListener(*youbot); + canvas.addKeyListener(*keyController); scene->add(youbot); handle.setText("Use WASD keys to steer robot", opts); }); @@ -70,6 +73,8 @@ int main() { renderer.render(*scene, *camera); hud.apply(renderer); - if (youbot) youbot->update(dt); + if (youbot) keyController->update(dt); }); + + future.get(); } diff --git a/examples/projects/Youbot/youbot_kine.cpp b/examples/projects/Youbot/youbot_kine.cpp index 4296cc83..68d85c17 100644 --- a/examples/projects/Youbot/youbot_kine.cpp +++ b/examples/projects/Youbot/youbot_kine.cpp @@ -5,6 +5,7 @@ #include "kine/Kine.hpp" #include "kine/ik/CCDSolver.hpp" +#include "KeyController.hpp" #include "threepp/extras/imgui/ImguiContext.hpp" #include @@ -107,13 +108,15 @@ int main() { std::shared_ptr youbot; + std::unique_ptr keyController; auto future = std::async([&] { youbot = Youbot::create("data/models/collada/youbot.dae"); youbot->add(targetHelper); youbot->add(endEffectorHelper); endEffectorHelper->visible = true; + keyController = std::make_unique(*youbot); renderer.invokeLater([&] { - canvas.addKeyListener(*youbot); + canvas.addKeyListener(*keyController); scene->add(youbot); hud.remove(handle); }); @@ -180,7 +183,7 @@ int main() { } youbot->setJointValues(ui.values); - youbot->update(dt); + keyController->update(dt); } else { hud.apply(renderer);