💿🐜 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.

132 lines
3.2 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. #ifndef ANTKEEPER_TIMELINE_HPP
  20. #define ANTKEEPER_TIMELINE_HPP
  21. #include <functional>
  22. #include <set>
  23. #include <tuple>
  24. /**
  25. * Timeline which executes cues (scheduled functions) when advanced over their respective positions in time.
  26. */
  27. class timeline
  28. {
  29. public:
  30. /// Scheduled function consisting of a time and function object.
  31. typedef std::tuple<float, std::function<void()>> cue;
  32. /// List of cues.
  33. typedef std::list<cue> sequence;
  34. /**
  35. * Creates a timeline.
  36. */
  37. timeline();
  38. /**
  39. * Advances the timeline position (t) by @p dt, triggering any cues scheduled between `t` and `dt`. If autoremove is enabled, triggered cues will be removed.
  40. *
  41. * @param dt Delta time by which the timeline position will be advanced.
  42. */
  43. void advance(float dt);
  44. /**
  45. * Sets the timeline position to @p t.
  46. *
  47. * @param t Position in time to which the timeline position will be set.
  48. */
  49. void seek(float t);
  50. /**
  51. * Adds a cue to the timeline.
  52. *
  53. * @param c Cue to add.
  54. */
  55. void add_cue(const cue& c);
  56. /**
  57. * Removes a cue from the timeline. If there are multiple identical cues (same time and function), they will all be removed.
  58. *
  59. * @param c Cue to remove.
  60. */
  61. void remove_cue(const cue& c);
  62. /**
  63. * Removes all cues on `[start, end)`.
  64. *
  65. * @param start Starting position in time (inclusive).
  66. * @param end Ending position in time (non-inclusive).
  67. */
  68. void remove_cues(float start, float end);
  69. /**
  70. * Adds a sequence of cues to the timeline.
  71. *
  72. * @param s Sequence of cues to add.
  73. */
  74. void add_sequence(const sequence& s);
  75. /**
  76. * Removes a sequence of cues from the timeline.
  77. *
  78. * @param s Sequence of cues to remove.
  79. */
  80. void remove_sequence(const sequence& s);
  81. /**
  82. * Removes all cues from the timeline.
  83. */
  84. void clear();
  85. /**
  86. * If enabled, cues will be automatically removed from the timeline when they are triggered.
  87. *
  88. * @param enabled Whether to enable autoremove.
  89. */
  90. void set_autoremove(bool enabled);
  91. /**
  92. * Returns the current position in time on the timeline.
  93. */
  94. float get_position() const;
  95. /**
  96. * Returns all the cues on `[start, end)`.
  97. *
  98. * @param start Starting position in time (inclusive).
  99. * @param end Ending position in time (non-inclusive).
  100. * @return All cues on `[start, end)`.
  101. */
  102. sequence get_cues(float start, float end) const;
  103. private:
  104. std::multiset<cue, std::function<bool(const cue&, const cue&)>> cues;
  105. float position;
  106. bool autoremove;
  107. };
  108. inline float timeline::get_position() const
  109. {
  110. return position;
  111. }
  112. #endif // ANTKEEPER_TIMELINE_HPP