33 #ifndef EMP_WEB_TWEEN_H 34 #define EMP_WEB_TWEEN_H 47 std::function<void(double)> set_fun;
50 std::function<double(double)> timing;
52 void Set(
const double frac) {
53 const double cur_val = (end_val - start_val) * timing(frac) + start_val;
60 std::function<void()> trigger;
77 static double LINEAR(
double in) {
return in; }
83 double frac = (cur_time - start_time + run_time) / duration;
86 if (frac > 1.0) { frac = 1.0; running =
false; }
91 for (
auto * p : paths) {
96 for (
auto f : update_funs) {
101 for (
auto & w : dependants) { w.Redraw(); }
105 requestAnimFrame(
function() {
emp.Callback($0); });
112 : duration(d*1000), default_target(t), running(false)
113 , start_time(0.0), cur_time(0.0), run_time(0.0)
116 callback_id = JSWrap( std::function<
void()>([
this](){ this->AdvanceFrame(); }) );
120 for (
auto * p : paths)
delete p;
121 for (
auto * e : events)
delete e;
138 double start_val,
double end_val, std::function<
double(
double)> timing=LINEAR) {
139 Path * new_path =
new Path({set_fun, start_val, end_val, timing});
146 double start_val,
double end_val, std::function<
double(
double)> timing=LINEAR) {
147 AddPath( [&set_var](
double v) { set_var = v; }, start_val, end_val, timing);
172 cur_time = start_time;
double GetTime()
Get the current time, as provided by the web browser.
Definition: emfunctions.h:53
Tween(double d=1.0, const Widget &t=nullptr)
Build a new tween, specifying the duration it should run for and the widget it should modify...
Definition: Tween.h:111
Tween & AddPath(double &set_var, double start_val, double end_val, std::function< double(double)> timing=LINEAR)
Alter the path of the change that this tween should take and the variable it should modify...
Definition: Tween.h:145
Tween & AddDependant(Widget w)
Add a dependant Widget to update as the Tween runs.
Definition: Tween.h:165
Widget GetDefaultTarget() const
Which widget does this Tween modify?
Definition: Tween.h:128
Tween & SetDefaultTarget(const Widget &w)
Change the target of this tween.
Definition: Tween.h:134
void push_back(PB_Ts &&...args)
Definition: vector.h:189
Specialized, useful function for Empirical.
void Start()
Start running this Tween, as configured.
Definition: Tween.h:168
~Tween()
Definition: Tween.h:118
Tween & AddPath(Widget w, std::string setting, double start_val, double end_val)
Definition: Tween.h:153
static bool InitializeAnim()
Stub for when Emscripten is not in use.
Definition: init.h:104
If we are in emscripten, make sure to include the header.
Definition: array.h:37
double GetDuration() const
Retrieve the full duration of this Tween.
Definition: Tween.h:125
#define emp_assert(...)
Definition: assert.h:199
Tween & AddUpdate(std::function< void(void)> ud_fun)
Add an additional function to update as the Tween runs.
Definition: Tween.h:159
Tween & SetDuration(double d)
Change the duration of this Tween.
Definition: Tween.h:131
Tween & AddPath(std::function< void(double)> set_fun, double start_val, double end_val, std::function< double(double)> timing=LINEAR)
Alter the path of the change that this tween should take and the function it should call...
Definition: Tween.h:137
void Stop()
Pause this Tween.
Definition: Tween.h:177