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

136 lines
3.2 KiB

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