alestane

Inheritance Exposure

Apr 27th, 2021 (edited)
710
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.85 KB | None | 0 0
  1. #include <chrono>
  2. #include <functional>
  3.  
  4. class Project {
  5. public:
  6.     using seconds = std::chrono::duration<float>;
  7. };
  8.  
  9. class Timer {
  10. public:
  11.     Timer(float seconds) : _remaining{ seconds } {}
  12.     virtual bool operator()(Project::seconds, Project::seconds);
  13.     float remaining() const { return _remaining; }
  14. protected:
  15.     virtual void operator()(Project::seconds time) = 0;
  16. private:
  17.     float _remaining;
  18. };
  19.  
  20. bool Timer::operator()(Project::seconds frame, Project::seconds total)
  21. {
  22.     _remaining -= frame.count();
  23.     if (_remaining <= 0) {
  24.         (*this)(total);
  25.         return false;
  26.     }
  27.     return true;
  28. }
  29.  
  30. class Easing : public Timer {
  31. public:
  32.     Easing(float seconds) : Timer{ seconds }, _duration{ seconds } {}
  33.     bool operator()(Project::seconds, Project::seconds) override;
  34. protected:
  35.     void operator()(Project::seconds) override {}
  36.     virtual void operator()(float progress, float fraction) = 0;
  37. private:
  38.     float _duration;
  39. };
  40.  
  41. bool Easing::operator()(Project::seconds frame, Project::seconds total)
  42. {
  43.     float elapsed = _duration - remaining();
  44.     operator()(frame.count() / _duration, elapsed / _duration);
  45.     return Timer::operator()(frame, total);
  46. }
  47.  
  48. class Slide : public Easing {
  49. public:
  50.     Slide(float duration, long& target, float destination)
  51.         : Easing{duration}, _target { target }, _destination{ destination }, _start{ target / 0.5 }
  52.     {}
  53. //    using Easing::operator(); // required fix
  54. protected:
  55.     void operator()(float progress, float fraction) override;
  56. private:
  57.     long& _target;
  58.     double _destination, _start;
  59. };
  60.  
  61. void Slide::operator()(float progress, float fraction)
  62. {
  63.     _target += (_destination - _start) * progress);
  64. }
  65.  
  66. long x = 16;
  67. std::function<bool(Project::seconds, Project::seconds)> motion { Slide { 4.0f, x, 12.15f } }; // fails without using declaration
Add Comment
Please, Sign In to add comment