💿🐜 Antkeeper source code https://antkeeper.com
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

112 lines
2.4 KiB

  1. /*
  2. * Copyright (C) 2021 Christopher J. Howard
  3. *
  4. * This file is part of Antkeeper source code.
  5. *
  6. * Antkeeper source code is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * Antkeeper source code is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with Antkeeper source code. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #include "timeline.hpp"
  20. auto cue_compare = [](const typename timeline::cue& a, const typename timeline::cue& b)
  21. {
  22. return std::get<0>(a) < std::get<0>(b);
  23. };
  24. timeline::timeline():
  25. cues(cue_compare),
  26. position(0.0f),
  27. autoremove(false)
  28. {}
  29. void timeline::advance(float dt)
  30. {
  31. auto lower_bound = cues.lower_bound({position, nullptr});
  32. auto upper_bound = cues.upper_bound({position + dt, nullptr});
  33. for (auto iterator = lower_bound; iterator != upper_bound; ++iterator)
  34. {
  35. std::get<1>(*iterator)();
  36. }
  37. if (autoremove && lower_bound != upper_bound)
  38. {
  39. cues.erase(lower_bound, upper_bound);
  40. }
  41. position += dt;
  42. }
  43. void timeline::seek(float t)
  44. {
  45. position = t;
  46. }
  47. void timeline::add_cue(const cue& c)
  48. {
  49. cues.emplace(c);
  50. }
  51. void timeline::remove_cue(const cue& c)
  52. {
  53. cues.erase(c);
  54. }
  55. void timeline::remove_cues(float start, float end)
  56. {
  57. auto lower_bound = cues.lower_bound({start, nullptr});
  58. auto upper_bound = cues.upper_bound({end, nullptr});
  59. cues.erase(lower_bound, upper_bound);
  60. }
  61. void timeline::add_sequence(const sequence& s)
  62. {
  63. for (const cue& c: s)
  64. {
  65. add_cue(c);
  66. }
  67. }
  68. void timeline::remove_sequence(const sequence& s)
  69. {
  70. for (const cue& c: s)
  71. {
  72. remove_cue(c);
  73. }
  74. }
  75. void timeline::clear()
  76. {
  77. cues.clear();
  78. }
  79. void timeline::set_autoremove(bool enabled)
  80. {
  81. autoremove = enabled;
  82. }
  83. typename timeline::sequence timeline::get_cues(float start, float end) const
  84. {
  85. sequence s;
  86. auto lower_bound = cues.lower_bound({start, nullptr});
  87. auto upper_bound = cues.upper_bound({end, nullptr});
  88. for (auto iterator = lower_bound; iterator != upper_bound; ++iterator)
  89. {
  90. s.push_back(*iterator);
  91. }
  92. return s;
  93. }