Antkeeper  0.0.1
timeline.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2023 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 
21 
22 auto cue_compare = [](const typename timeline::cue& a, const typename timeline::cue& b)
23 {
24  return std::get<0>(a) < std::get<0>(b);
25 };
26 
28  cues(cue_compare),
29  position(0.0f),
30  autoremove(false)
31 {}
32 
33 void timeline::advance(float dt)
34 {
35  auto lower_bound = cues.lower_bound({position, nullptr});
36  auto upper_bound = cues.upper_bound({position + dt, nullptr});
37 
38  for (auto iterator = lower_bound; iterator != upper_bound; ++iterator)
39  {
40  std::get<1>(*iterator)();
41  }
42 
43  if (autoremove && lower_bound != upper_bound)
44  {
45  cues.erase(lower_bound, upper_bound);
46  }
47 
48  position += dt;
49 }
50 
51 void timeline::seek(float t)
52 {
53  position = t;
54 }
55 
56 void timeline::add_cue(const cue& c)
57 {
58  cues.emplace(c);
59 }
60 
61 void timeline::remove_cue(const cue& c)
62 {
63  cues.erase(c);
64 }
65 
66 void timeline::remove_cues(float start, float end)
67 {
68  auto lower_bound = cues.lower_bound({start, nullptr});
69  auto upper_bound = cues.upper_bound({end, nullptr});
70  cues.erase(lower_bound, upper_bound);
71 }
72 
74 {
75  for (const cue& c: s)
76  {
77  add_cue(c);
78  }
79 }
80 
82 {
83  for (const cue& c: s)
84  {
85  remove_cue(c);
86  }
87 }
88 
90 {
91  cues.clear();
92 }
93 
94 void timeline::set_autoremove(bool enabled)
95 {
96  autoremove = enabled;
97 }
98 
99 typename timeline::sequence timeline::get_cues(float start, float end) const
100 {
101  sequence s;
102 
103  auto lower_bound = cues.lower_bound({start, nullptr});
104  auto upper_bound = cues.upper_bound({end, nullptr});
105  for (auto iterator = lower_bound; iterator != upper_bound; ++iterator)
106  {
107  s.push_back(*iterator);
108  }
109 
110  return s;
111 }
112 
void add_cue(const cue &c)
Adds a cue to the timeline.
Definition: timeline.cpp:56
void seek(float t)
Sets the timeline position to t.
Definition: timeline.cpp:51
timeline()
Creates a timeline.
Definition: timeline.cpp:27
void remove_cue(const cue &c)
Removes a cue from the timeline.
Definition: timeline.cpp:61
void remove_sequence(const sequence &s)
Removes a sequence of cues from the timeline.
Definition: timeline.cpp:81
sequence get_cues(float start, float end) const
Returns all the cues on [start, end).
Definition: timeline.cpp:99
void advance(float dt)
Advances the timeline position (t) by dt, triggering any cues scheduled between t and dt.
Definition: timeline.cpp:33
std::list< cue > sequence
List of cues.
Definition: timeline.hpp:37
void set_autoremove(bool enabled)
If enabled, cues will be automatically removed from the timeline when they are triggered.
Definition: timeline.cpp:94
void clear()
Removes all cues from the timeline.
Definition: timeline.cpp:89
void remove_cues(float start, float end)
Removes all cues on [start, end).
Definition: timeline.cpp:66
std::tuple< float, std::function< void()> > cue
Scheduled function consisting of a time and function object.
Definition: timeline.hpp:34
void add_sequence(const sequence &s)
Adds a sequence of cues to the timeline.
Definition: timeline.cpp:73
auto cue_compare
Definition: timeline.cpp:22