time.cpp 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. /*
  2. * time.cpp - time-related nodes
  3. * Copyright (C) 2017 caryoscelus
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. #include <core/all_types.h>
  19. #include <core/node_info.h>
  20. #include <core/node/proxy_node.h>
  21. #include <core/node/property.h>
  22. #include <core/context.h>
  23. namespace rainynite::core {
  24. namespace nodes {
  25. template <typename T>
  26. class TimeMap : public ProxyNode<T> {
  27. public:
  28. TimeMap() {
  29. this->template init<T>(source, {});
  30. this->template init<double>(multiplier, 1);
  31. this->template init<Time>(offset, {});
  32. }
  33. public:
  34. NodeInContext get_proxy(shared_ptr<Context> ctx) const override {
  35. auto t = ctx->get_time() * get_multiplier()->get(ctx) + get_offset()->get(ctx);
  36. auto nctx = make_shared<Context>(*ctx);
  37. nctx->set_time(t);
  38. return { get_source(), nctx };
  39. }
  40. private:
  41. NODE_PROPERTY(source, T);
  42. NODE_PROPERTY(multiplier, double);
  43. NODE_PROPERTY(offset, Time);
  44. };
  45. NODE_INFO_TEMPLATE(TimeMap, TimeMap<T>, T);
  46. TYPE_INSTANCES(TimeMapNodeInfo)
  47. class Now : public Node<Time> {
  48. public:
  49. Now() {
  50. }
  51. public:
  52. Time get(shared_ptr<Context> ctx) const override {
  53. return ctx->get_time();
  54. }
  55. };
  56. REGISTER_NODE(Now);
  57. template <typename T>
  58. class TimeLoop : public ProxyNode<T> {
  59. public:
  60. TimeLoop() {
  61. this->template init<T>(source, {});
  62. this->template init<Time>(period, {});
  63. this->template init<Time>(offset, {});
  64. }
  65. public:
  66. NodeInContext get_proxy(shared_ptr<Context> ctx) const override {
  67. auto period = get_period()->get(ctx);
  68. auto t = ctx->get_time() + get_offset()->get(ctx);
  69. Time time;
  70. if (period <= Time()) {
  71. time = t;
  72. } else {
  73. time = t - std::floor(t/period)*period;
  74. }
  75. auto nctx = make_shared<Context>(*ctx);
  76. nctx->set_time(time);
  77. return { get_source(), nctx };
  78. }
  79. private:
  80. NODE_PROPERTY(source, T);
  81. NODE_PROPERTY(period, Time);
  82. NODE_PROPERTY(offset, Time);
  83. };
  84. NODE_INFO_TEMPLATE(TimeLoop, TimeLoop<T>, T);
  85. TYPE_INSTANCES(TimeLoopNodeInfo)
  86. } // namespace nodes
  87. } // namespace rainynite::core