16 #ifndef EMP_WEB_ANIMATE_H 17 #define EMP_WEB_ANIMATE_H 21 #include "../base/assert.h" 22 #include "../base/vector.h" 86 template <
typename... T>
93 if (!active && !do_step)
return;
102 for (
auto & w : targets) { w.Redraw(); }
106 requestAnimFrame(
function() {
emp.Callback($0); });
120 Animate() : active(false), do_step(false), run_time(0.0), frame_count(0)
123 callback_id = JSWrap( std::function<
void()>([
this](){ this->
AdvanceFrame(); }) );
128 template <
typename... W_TYPES>
135 template <
typename... W_TYPES>
136 Animate(
const std::function<
void(
double)> & fun, W_TYPES&... targets)
139 template <
typename... W_TYPES>
140 Animate(
const std::function<
void()> & fun, W_TYPES&... targets)
211 anim_fun = [fun](
const Animate &){fun();};
218 Button GetToggleButton(
const std::string & but_name,
const std::string & start_label=
"Start",
const std::string & stop_label=
"Stop") {
219 toggle_but =
Button( [
this, but_name, start_label, stop_label]() {
221 toggle_but.
Label(active ? stop_label : start_label);
222 }, start_label, but_name);
230 step_but =
Button( [
this, but_name, label]() {
231 this->do_step =
true;
double GetTime()
Get the current time, as provided by the web browser.
Definition: emfunctions.h:53
Button GetStepButton(const std::string &but_name, const std::string &label="Step")
Definition: Animate.h:229
Animate(const std::function< void()> &fun, W_TYPES &...targets)
Definition: Animate.h:140
void SetCallback(const std::function< void(double)> &fun)
Definition: Animate.h:205
Button step_but
A button to advance this animation one step.
Definition: Animate.h:83
Button toggle_but
A button to start/stop this animation.
Definition: Animate.h:81
void Start()
Start this animation running.
Definition: Animate.h:150
anim_fun_t anim_fun
Function to repeatedly run for animation.
Definition: Animate.h:68
bool GetActive() const
Determine if this animation is currently running.
Definition: Animate.h:176
Animate(const anim_fun_t &fun, W_TYPES &...targets)
Definition: Animate.h:129
double run_time
How much run time has accumulated?
Definition: Animate.h:77
void LoadTargets(const web::Widget &target1, const T &...other_targets)
Definition: Animate.h:87
bool GetDoStep() const
Determine if this animation is currently in the process of running a single step. ...
Definition: Animate.h:179
void push_back(PB_Ts &&...args)
Definition: vector.h:189
int frame_count
How many animation frames have gone by?
Definition: Animate.h:79
bool active
Is this animation currently running?
Definition: Animate.h:70
An object that, when active, repeatedly calls a function as fast as possible, to a maximum of 60 fram...
Definition: Animate.h:62
void SetCallback(const std::function< void()> &fun)
Set a new function for this animation to call when running that takes no arguments.
Definition: Animate.h:210
Specialized, useful function for Empirical.
double GetStepTime() const
Determine how long the last step between frames took.
Definition: Animate.h:191
double GetPrevTime() const
Determine the time point when this animation last updated a frame.
Definition: Animate.h:185
bool do_step
Should this animation take just a single step?
Definition: Animate.h:71
void AdvanceFrame()
Definition: Animate.h:92
void Stop()
Halt this animation for now.
Definition: Animate.h:160
double prev_time
What was the time point of the previous frame?
Definition: Animate.h:75
std::function< void(const Animate &)> anim_fun_t
The full version of the animate function takes a const reference to the animate object.
Definition: Animate.h:66
void LoadTargets()
Definition: Animate.h:85
int GetFrameCount() const
Determine how many total frames have existed thus far in this animation.
Definition: Animate.h:197
Animate & operator=(const Animate &)=delete
static bool InitializeAnim()
Stub for when Emscripten is not in use.
Definition: init.h:104
double GetStartTime() const
Return the time point that this animation started MOST RECENTLY.
Definition: Animate.h:182
Button GetToggleButton(const std::string &but_name, const std::string &start_label="Start", const std::string &stop_label="Stop")
Definition: Animate.h:218
double GetRunTime() const
Determine the total amount of time that this animation has run.
Definition: Animate.h:194
Wrap a C++ function and convert it to an integer that can be called from Javascript.
void Step()
Take a single step in this animation.
Definition: Animate.h:166
If we are in emscripten, make sure to include the header.
Definition: array.h:37
virtual ~Animate()
Definition: Animate.h:143
double GetCurTime() const
Get the current time of the animation.
Definition: Animate.h:188
Build a debug wrapper emp::vector around std::vector.
Definition: vector.h:42
emp::vector< web::Widget > targets
What widgets should be refreshed after each frame?
Definition: Animate.h:69
double cur_time
What time did the current frame start?
Definition: Animate.h:76
void ToggleActive()
Toggle whether this animation is running or paused.
Definition: Animate.h:173
Animate()
Definition: Animate.h:120
Animate(const std::function< void(double)> &fun, W_TYPES &...targets)
Definition: Animate.h:136
double start_time
At what time did this animation most recently start?
Definition: Animate.h:74
size_t callback_id
Intenral ID for javascript to call back AdvanceFrame()
Definition: Animate.h:72
virtual void DoFrame()
Definition: Animate.h:114
void SetCallback(const anim_fun_t &fun)
Definition: Animate.h:201