Skip to content

Commit

Permalink
Can chain easers.
Browse files Browse the repository at this point in the history
  • Loading branch information
dsheeler committed Jan 22, 2018
1 parent f592c73 commit 67c7279
Show file tree
Hide file tree
Showing 12 changed files with 391 additions and 207 deletions.
7 changes: 5 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,11 @@ CFLAGS += $(shell pkg-config --cflags pangocairo) \
$(shell pkg-config --cflags fftw3)
SRCS = drawable.cc v4l2_wayland.cc muxing.cc sound_shape.cc midi.cc kmeter.cc \
video_file_source.cc dingle_dots.cc v4l2.cc sprite.cc snapshot_shape.cc \
easer.cc easing.c
OBJS = $(SRCS:.cc=.o) $(SRCS:.c=.o)
easer.cc
CSRCS= easing.c

OBJS := $(SRCS:.cc=.o) $(CSRCS:.c=.o)

HDRS = drawable.h muxing.h sound_shape.h midi.h v4l2_wayland.h kmeter.h \
video_file_source.h dingle_dots.h v4l2.h sprite.h snapshot_shape.h \
easer.h easing.h
Expand Down
35 changes: 32 additions & 3 deletions dingle_dots.cc
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ DingleDots::DingleDots() { }
int DingleDots::init(char *dev_name, int width, int height,
char *video_file_name, int video_bitrate) {
int ret;
this->s_pressed = 0;
this->smdown = 0;
this->drawing_rect.width = width;
this->drawing_rect.height = height;
this->recording_started = 0;
Expand Down Expand Up @@ -42,7 +44,7 @@ int DingleDots::init(char *dev_name, int width, int height,
this->dragging = 0;
this->selection_in_progress = 0;
this->motion_threshold = 0.001;
for (int i = 0; i < MAX_NSOUND_SHAPES; ++i) {
for (int i = 0; i < MAX_NUM_SOUND_SHAPES; ++i) {
this->sound_shapes[i].clear_state();
}
snapshot_shape.init("SNAPSHOT", this->drawing_rect.width / 2., this->drawing_rect.width / 16.,
Expand All @@ -63,7 +65,7 @@ int DingleDots::init(char *dev_name, int width, int height,
}

int DingleDots::deactivate_sound_shapes() {
for (int i = 0; i < MAX_NSOUND_SHAPES; i++) {
for (int i = 0; i < MAX_NUM_SOUND_SHAPES; i++) {
if (this->sound_shapes[i].active) {
this->sound_shapes[i].deactivate();
}
Expand Down Expand Up @@ -139,7 +141,7 @@ int DingleDots::add_note(char *scale_name,
}
midi_note_to_octave_name(midi_note, octave_name);
sprintf(label, "%.2f\n%s\n%d\n%s", freq, snum, scale_num, octave_name);
for (i = 0; i < MAX_NSOUND_SHAPES; i++) {
for (i = 0; i < MAX_NUM_SOUND_SHAPES; i++) {
SoundShape *s = &this->sound_shapes[i];
if (s->active) continue;

Expand All @@ -152,3 +154,30 @@ int DingleDots::add_note(char *scale_name,
return -1;
}

void DingleDots::get_sound_shapes(std::vector<Drawable *> &sound_shapes)
{
for (int i = 0; i < MAX_NUM_SOUND_SHAPES; ++i) {
SoundShape *ss = &this->sound_shapes[i];
if (ss->active) sound_shapes.push_back(ss);
}
sound_shapes.push_back(&this->snapshot_shape);
}

void DingleDots::get_sources(std::vector<Drawable *> &list)
{
for (int i = 0; i < MAX_NUM_V4L2; i++) {
if (this->v4l2[i].active) {
list.push_back(&this->v4l2[i]);
}
}
for (int j = 0; j < MAX_NUM_VIDEO_FILES; j++) {
if (this->vf[j].active) {
list.push_back(&this->vf[j]);
}
}
for (int i = 0; i < MAX_NUM_SPRITES; i++) {
if (this->sprites[i].active) {
list.push_back(&this->sprites[i]);
}
}
}
7 changes: 5 additions & 2 deletions dingle_dots.h
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,9 @@ class DingleDots {
int current_video_file_source_index;
int current_sprite_index;
V4l2 v4l2[MAX_NUM_V4L2];
Sprite sprites[2];
Sprite sprites[MAX_NUM_SPRITES];
SnapshotShape snapshot_shape;
SoundShape sound_shapes[MAX_NSOUND_SHAPES];
SoundShape sound_shapes[MAX_NUM_SOUND_SHAPES];
kmeter meters[2];
GdkRectangle drawing_rect;
int doing_motion;
Expand All @@ -92,6 +92,7 @@ class DingleDots {
uint8_t mdown;
uint8_t dragging;
uint8_t smdown;
uint8_t s_pressed;
GdkPoint mdown_pos;
GtkWidget *ctl_window;
GtkWidget *drawing_area;
Expand All @@ -113,6 +114,8 @@ class DingleDots {
color random_color();
uint8_t get_animating() const;
void set_animating(const uint8_t &value);
void get_sound_shapes(std::vector<Drawable *> &sound_shapes);
void get_sources(std::vector<Drawable *> &list);
};

#endif
14 changes: 11 additions & 3 deletions drawable.cc
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#include <math.h>

#include "drawable.h"

#include "easer.h"

double Drawable::get_opacity() const
{
Expand Down Expand Up @@ -43,18 +43,26 @@ Drawable::Drawable(double x, double y, int64_t z, double opacity, double scale)
this->scale = scale;
}

void Drawable::update_easers()
{
void Drawable::update_easers() {
std::vector<Easer *> to_finalize;
for (std::vector<Easer *>::iterator it = this->easers.begin(); it != this->easers.end(); ++it) {
Easer *easer = *it;
if (easer->active) {
easer->update_value();
if (easer->done()) {
to_finalize.push_back(easer);
}
} else {
it = easers.erase(it);
if (it == this->easers.end()) break;
else --it;
}
}
while(to_finalize.size()) {
Easer *e = to_finalize.back();
e->finalize();
to_finalize.pop_back();
}
}

bool Drawable::render(std::vector<cairo_t *> &contexts) {
Expand Down
1 change: 1 addition & 0 deletions drawable.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ struct rectangle_double {
double height;
};

//class Easer;
class Drawable {
private:
void render_label(cairo_t *cr);
Expand Down
101 changes: 92 additions & 9 deletions easer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include "easer.h"
#include "v4l2_wayland.h"
#include "dingle_dots.h"
#include "drawable.h"

Easer::Easer() {}

Expand All @@ -21,39 +22,124 @@ EasingFuncPtr Easer::easer_type_to_easing_func(Easer_Type type)
break;
case EASER_QUAD_EASE_IN_OUT:
func = &QuadraticEaseInOut;
break;
case EASER_CUBIC_EASE_IN:
func = & CubicEaseIn;
break;
case EASER_CUBIC_EASE_OUT:
func = &CubicEaseOut;
break;
case EASER_CUBIC_EASE_IN_OUT:
func = &CubicEaseInOut;
break;
case EASER_QUARTIC_EASE_IN:
func = &QuarticEaseIn;
break;
case EASER_QUARTIC_EASE_OUT:
func = &QuarticEaseOut;
break;
case EASER_QUARTIC_EASE_IN_OUT:
func = &QuarticEaseInOut;
break;
case EASER_QUINTIC_EASE_IN:
func = &QuinticEaseIn;
break;
case EASER_QUINTIC_EASE_OUT:
func = &QuinticEaseOut;
break;
case EASER_QUINTIC_EASE_IN_OUT:
func = &QuinticEaseInOut;
break;
case EASER_SINE_EASE_IN:
func = &SineEaseIn;
break;
case EASER_SINE_EASE_OUT:
func = &SineEaseOut;
break;
case EASER_SINE_EASE_IN_OUT:
func = &SineEaseInOut;
break;
case EASER_CIRCULAR_EASE_IN:
func = &CircularEaseIn;
break;
case EASER_CIRCULAR_EASE_OUT:
func = &CircularEaseOut;
break;
case EASER_CIRCULAR_EASE_IN_OUT:
func = &CircularEaseInOut;
break;
case EASER_EXPONENTIAL_EASE_IN:
func = &ExponentialEaseIn;
break;
case EASER_EXPONENTIAL_EASE_OUT:
func = &ExponentialEaseOut;
break;
case EASER_EXPONENTIAL_EASE_IN_OUT:
func = &ExponentialEaseInOut;
break;
case EASER_BACK_EASE_IN:
func = &BackEaseIn;
break;
case EASER_BACK_EASE_OUT:
func = &BackEaseOut;
break;
case EASER_BACK_EASE_IN_OUT:
func = &BackEaseInOut;
break;
case EASER_ELASTIC_EASE_IN:
func = &ElasticEaseIn;
break;
case EASER_ELASTIC_EASE_OUT:
func = &ElasticEaseOut;
break;
case EASER_ELASTIC_EASE_IN_OUT:
func = &ElasticEaseInOut;
break;
case EASER_BOUNCE_EASE_IN:
func = &BounceEaseIn;
break;
case EASER_BOUNCE_EASE_IN_OUT:
func = &BounceEaseInOut;
break;
case EASER_BOUNCE_EASE_OUT:
func = &BounceEaseOut;
break;
case EASER_BOUNCE_EASE_IN_OUT:
func = &BounceEaseInOut;
break;
default:
func = 0;
break;
}
return func;
}

void Easer::start(DingleDots *dd, Easer_Type type, double *tvalue, double value_start, double value_finish,
void Easer::initialize(Drawable *target, DingleDots *dd, Easer_Type type, double *tvalue, double value_start, double value_finish,
double duration_secs)
{
this->active = FALSE;
this->target = target;
this->dd = dd;
this->value = tvalue;
this->duration_secs = duration_secs;
this->value_start = value_start;
this->value_finish = value_finish;
clock_gettime(CLOCK_MONOTONIC, &this->start_ts);
this->easing_func = easer_type_to_easing_func(type);
dd->set_animating(dd->get_animating()+1);
}


void Easer::start() {
target->easers.push_back(this);
this->active = TRUE;
clock_gettime(CLOCK_MONOTONIC, &this->start_ts);
dd->set_animating(dd->get_animating()+1);
}

void Easer::finalize() {
*this->value = this->value_finish;
dd->set_animating(dd->get_animating() - 1);
for (std::vector<Easer *>::iterator it = this->start_when_finished.begin();
it != this->start_when_finished.end(); ++it) {
Easer *e = *it;
e->start();
}
this->active = FALSE;
}

Expand All @@ -65,9 +151,6 @@ void Easer::update_value() {
easer_value = (this->easing_func)(ratio_complete);
delta = this->value_finish - this->value_start;
*this->value = this->value_start + easer_value * delta;
if (this->done()) {
finalize();
}
}

bool Easer::done()
Expand Down
36 changes: 33 additions & 3 deletions easer.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,40 +2,70 @@
#define EASER_H

#include <time.h>
#include <vector>
#include "easing.h"
//#include "drawable.h"

typedef enum {
EASER_LINEAR = 1,
EASER_QUAD_EASE_IN,
EASER_QUAD_EASE_OUT,
EASER_QUAD_EASE_IN_OUT,
EASER_CUBIC_EASE_IN,
EASER_CUBIC_EASE_OUT,
EASER_CUBIC_EASE_IN_OUT,
EASER_QUARTIC_EASE_IN,
EASER_QUARTIC_EASE_OUT,
EASER_QUARTIC_EASE_IN_OUT,
EASER_QUINTIC_EASE_IN,
EASER_QUINTIC_EASE_OUT,
EASER_QUINTIC_EASE_IN_OUT,
EASER_SINE_EASE_IN,
EASER_SINE_EASE_OUT,
EASER_SINE_EASE_IN_OUT,
EASER_CIRCULAR_EASE_IN,
EASER_CIRCULAR_EASE_OUT,
EASER_CIRCULAR_EASE_IN_OUT,
EASER_EXPONENTIAL_EASE_IN,
EASER_EXPONENTIAL_EASE_OUT,
EASER_EXPONENTIAL_EASE_IN_OUT,
EASER_ELASTIC_EASE_IN,
EASER_ELASTIC_EASE_OUT,
EASER_ELASTIC_EASE_IN_OUT,
EASER_BACK_EASE_IN,
EASER_BACK_EASE_OUT,
EASER_BACK_EASE_IN_OUT,
EASER_BOUNCE_EASE_IN,
EASER_BOUNCE_EASE_OUT,
EASER_BOUNCE_EASE_IN_OUT
EASER_BOUNCE_EASE_IN_OUT,
EASER_JIGGLE_EASE,
} Easer_Type;

class DingleDots;

class Drawable;
typedef AHFloat (*EasingFuncPtr)(AHFloat);
class Easer {
public:
Easer();
static EasingFuncPtr easer_type_to_easing_func(Easer_Type);
void start(DingleDots *dd, Easer_Type type, double *value, double value_start, double value_finish, double duration_secs);
void initialize(Drawable *target, DingleDots *dd, Easer_Type type, double *value, double value_start, double value_finish, double duration_secs);
void start();
void finalize();
void update_value();
bool done();
double get_ratio_complete();
double left_secs();

EasingFuncPtr easing_func;
Drawable *target;
DingleDots *dd;
bool active;
float duration_secs;
double *value;
double value_start;
double value_finish;
struct timespec start_ts;
std::vector<Easer *> start_when_finished;
};

#endif // EASER_H
Loading

0 comments on commit 67c7279

Please sign in to comment.