Skip to content

Commit 7c5c7db

Browse files
committed
animation!!
1 parent f642887 commit 7c5c7db

36 files changed

+400
-232
lines changed

resources/shaders/circle.frag

+3
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@ uniform vec2 center;
88
uniform float radius;
99
uniform vec4 color;
1010

11+
uniform bool has_input_color;
12+
uniform vec4 input_color;
13+
1114
float circle(in vec2 _center, in float _radius, in vec2 xy)
1215
{
1316
vec2 d = xy - _center;

src/animation/anim.hpp

+11-1
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,20 @@ template <typename T>
66
class anim {
77
public:
88
anim(T val);
9+
anim(T val, time_pt start);
910

10-
virtual T get(double t);
11+
virtual std::shared_ptr<anim<T>> copy();
12+
13+
time_pt get_start() const;
14+
void set_start(time_pt start);
15+
16+
virtual T at(double t);
17+
T at(time_pt time);
18+
19+
bool active;
1120
protected:
1221
T value;
22+
time_pt start;
1323
};
1424

1525
#include "anim.tpp"

src/animation/anim.tpp

+25-2
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,32 @@
11
#include "anim.hpp"
22

33
template <typename T>
4-
anim<T>::anim(T val) : value(val) { };
4+
anim<T>::anim(T val) : anim(val, std::chrono::steady_clock::now()) { }
55

66
template <typename T>
7-
T anim<T>::get(double time) {
7+
anim<T>::anim(T val, time_pt start) : active(false), value(val), start(start) { }
8+
9+
template <typename T>
10+
std::shared_ptr<anim<T>> anim<T>::copy() {
11+
return std::make_shared<anim<T>>(this->value, this->start);
12+
}
13+
14+
template <typename T>
15+
time_pt anim<T>::get_start() const {
16+
return this->start;
17+
}
18+
19+
template <typename T>
20+
void anim<T>::set_start(time_pt start) {
21+
this->start = start;
22+
}
23+
24+
template <typename T>
25+
T anim<T>::at(double time) {
826
return this->value;
927
}
28+
29+
template <typename T>
30+
T anim<T>::at(time_pt time) {
31+
return this->at(std::chrono::duration<double>(time - this->start).count());
32+
}

src/animation/core.hpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,6 @@
77
#include "anim.hpp"
88
#include "tween.hpp"
99
#include "lerp.hpp"
10-
#include "follow.hpp"
10+
#include "func.hpp"
1111

1212
#include "easing.hpp"

src/animation/follow.hpp

-16
This file was deleted.

src/animation/follow.tpp

-9
This file was deleted.

src/animation/func.hpp

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#pragma once
2+
3+
#include "../common.hpp"
4+
#include "anim.hpp"
5+
6+
template <typename T>
7+
class func : public anim<T> {
8+
public:
9+
func(std::function<T(double)> f);
10+
11+
std::shared_ptr<anim<T>> copy() override;
12+
13+
T at(double t) override;
14+
protected:
15+
std::function<T(double)> f;
16+
};
17+
18+
#include "func.tpp"

src/animation/func.tpp

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#include "func.hpp"
2+
3+
template <typename T>
4+
func<T>::func(std::function<T(double)> f) : anim<T>(f(0)), f(f) { }
5+
6+
template <typename T>
7+
std::shared_ptr<anim<T>> func<T>::copy() {
8+
return std::make_shared<func<T>>(this->f);
9+
}
10+
11+
template <typename T>
12+
T func<T>::at(double t) {
13+
return this->f(t);
14+
}

src/animation/lerp.hpp

+3-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@ class lerp : public anim<T> {
88
public:
99
lerp(T val, std::function<T(double)> target, double speed);
1010

11-
T get(double t) override;
11+
std::shared_ptr<anim<T>> copy() override;
12+
13+
T at(double t) override;
1214
protected:
1315
double speed;
1416
double last_eval;

src/animation/lerp.tpp

+6-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,12 @@ template <typename T>
44
lerp<T>::lerp(T val, std::function<T(double)> target, double speed) : anim<T>(val), target(target), speed(speed), last_eval(0) { }
55

66
template <typename T>
7-
T lerp<T>::get(double t) {
7+
std::shared_ptr<anim<T>> lerp<T>::copy() {
8+
return std::make_shared<lerp<T>>(this->value, this->target, this->speed);
9+
}
10+
11+
template <typename T>
12+
T lerp<T>::at(double t) {
813
if (t > this->last_eval){
914
this->val = this->_target + (this->_target - this->val) * exp(-max(this->_speed, 0.0d) * (t - this->last_eval));
1015
this->last_eval = t;

src/animation/tween.hpp

+3-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@ class tween : public anim<T> {
88
public:
99
tween(std::function<T(double)> path, std::function<double(double)> easing_fn, double duration);
1010

11-
T get(double t) override;
11+
std::shared_ptr<anim<T>> copy() override;
12+
13+
T at(double t) override;
1214
protected:
1315
std::function<T(double)> path;
1416
std::function<double(double)> easing_fn;

src/animation/tween.tpp

+9-4
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,17 @@
22

33
template <typename T>
44
tween<T>::tween(std::function<T(double)> path, std::function<double(double)> easing_fn, double duration) : anim<T>(path(0)), path(path), easing_fn(easing_fn), duration(duration) {
5-
if (this->_duration <= 0.0f) {
6-
this->_duration = 1e-10; // small positive value
5+
if (this->duration <= 0.0f) {
6+
this->duration = 1e-10; // small positive value
77
}
88
}
99

1010
template <typename T>
11-
T tween<T>::get(double t) {
12-
return path(easing_fn(t / this->_duration));
11+
std::shared_ptr<anim<T>> tween<T>::copy() {
12+
return std::make_shared<tween<T>>(this->path, this->easing_fn, this->duration);
13+
}
14+
15+
template <typename T>
16+
T tween<T>::at(double t) {
17+
return path(easing_fn(std::min(t, this->duration) / this->duration));
1318
}

src/animation/var.hpp

+10-7
Original file line numberDiff line numberDiff line change
@@ -6,20 +6,23 @@
66
template <class T>
77
class var {
88
public:
9-
var(T val) : var(std::make_shared<anim<T>>(val), {}) { }
9+
var(T val);
1010
var(std::shared_ptr<anim<T>> ani);
11-
var(std::shared_ptr<anim<T>> ani, std::chrono::time_point<std::chrono::steady_clock> start);
11+
12+
var & operator=(const T & val);
1213

1314
operator T();
14-
virtual T operator()(double t);
15-
T operator()(std::chrono::time_point<std::chrono::steady_clock> time);
15+
T operator()(double t);
1616
T operator()();
1717

18-
void set_anim(std::shared_ptr<anim<T>> ani, std::chrono::time_point<std::chrono::steady_clock> start);
18+
std::shared_ptr<anim<T>> & operator[](int i);
19+
bool has(int i);
20+
21+
std::vector<int> get_channels();
22+
1923
void set_anim(std::shared_ptr<anim<T>> ani);
2024
private:
21-
std::shared_ptr<anim<T>> ani;
22-
std::chrono::time_point<std::chrono::steady_clock> start;
25+
std::map<int, std::shared_ptr<anim<T>>> anis;
2326
};
2427

2528
#include "var.tpp"

src/animation/var.tpp

+28-11
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,18 @@
11
#include "var.hpp"
22

33
template <class T>
4-
var<T>::var(std::shared_ptr<anim<T>> ani) : var(ani, std::chrono::steady_clock::now()) { }
4+
var<T>::var(T val) : var(std::make_shared<anim<T>>(val)) { }
55

66
template <class T>
7-
var<T>::var(std::shared_ptr<anim<T>> ani, std::chrono::time_point<std::chrono::steady_clock> start) : ani(ani), start(start) { }
7+
var<T>::var(std::shared_ptr<anim<T>> ani) : anis({}) {
8+
this->set_anim(ani);
9+
}
10+
11+
template<typename T>
12+
var<T> & var<T>::operator=(const T & val) {
13+
this->anis[BASE] = std::make_shared<anim<T>>(val);
14+
return *this;
15+
}
816

917
template <typename T>
1018
var<T>::operator T() {
@@ -13,26 +21,35 @@ var<T>::operator T() {
1321

1422
template <typename T>
1523
T var<T>::operator()(double time) {
16-
return this->ani->get(time);
24+
return this->anis[BASE]->at(time);
25+
}
26+
27+
template <typename T>
28+
T var<T>::operator()() {
29+
return this->anis[BASE]->at(std::chrono::steady_clock::now());
1730
}
1831

1932
template <typename T>
20-
T var<T>::operator()(std::chrono::time_point<std::chrono::steady_clock> time) {
21-
return this->operator()(std::chrono::duration<double>(time - this->start).count());
33+
std::shared_ptr<anim<T>> & var<T>::operator[](int i) {
34+
return this->anis[i];
2235
}
2336

2437
template <typename T>
25-
T var<T>::operator()() {
26-
return this->operator()(std::chrono::steady_clock::now());
38+
bool var<T>::has(int i) {
39+
return this->anis.count(i) != 0;
2740
}
2841

2942
template <typename T>
30-
void var<T>::set_anim(std::shared_ptr<anim<T>> ani, std::chrono::time_point<std::chrono::steady_clock> start) {
31-
this->ani = ani;
32-
this->start = start;
43+
std::vector<int> var<T>::get_channels() {
44+
std::vector<int> channels;
45+
for (auto & [i, _] : anis) {
46+
channels.push_back(i);
47+
}
48+
return channels;
3349
}
3450

3551
template <typename T>
3652
void var<T>::set_anim(std::shared_ptr<anim<T>> ani) {
37-
this->set_anim(ani, std::chrono::steady_clock::now());
53+
this->anis.clear();
54+
this->anis[BASE] = ani;
3855
}

src/app.cpp

+5-1
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ void app::init_opengl() {
8080

8181
::init_shaders();
8282
::init_component_defaults();
83+
::init_animation_defaults();
8384

8485
glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
8586

@@ -128,8 +129,11 @@ void app::cursor_position_callback(GLFWwindow * window, double xpos, double ypos
128129
get_instance().active_scene->move(current_cursor_pos - get_instance().last_cursor_pos);
129130
} else if (get_instance().lmb_down_time != std::nullopt) {
130131
get_instance().active_scene->drag(current_cursor_pos - get_instance().last_cursor_pos);
131-
}
132+
} else {
133+
get_instance().active_scene->hover(current_cursor_pos);
134+
}
132135

136+
get_instance().last_mouse_move_time = std::chrono::steady_clock::now();
133137
get_instance().last_cursor_pos = current_cursor_pos;
134138
}
135139

src/app.hpp

+4-3
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,13 @@ class app {
2323

2424
std::shared_ptr<scene> active_scene;
2525

26-
std::chrono::time_point<std::chrono::steady_clock> last_frame_time;
26+
time_pt last_frame_time;
2727

28+
time_pt last_mouse_move_time;
2829
glm::vec2 last_cursor_pos;
29-
std::optional<std::chrono::time_point<std::chrono::steady_clock>> lmb_down_time;
30+
std::optional<time_pt> lmb_down_time;
3031
std::optional<glm::vec2> lmb_down_pos;
31-
std::optional<std::chrono::time_point<std::chrono::steady_clock>> rmb_down_time;
32+
std::optional<time_pt> rmb_down_time;
3233
std::optional<glm::vec2> rmb_down_pos;
3334

3435
void init_opengl();

src/common.hpp

+6-1
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@
3232
#include "imgui/imgui_impl_opengl3.h"
3333
#include "imgui/imgui_stdlib.h"
3434

35+
typedef std::chrono::time_point<std::chrono::steady_clock> time_pt;
36+
3537
#define ZERO2 glm::vec2( 0.0f, 0.0f)
3638
#define ZERO3 glm::vec3( 0.0f, 0.0f, 0.0f)
3739
#define ONE3 glm::vec3( 1.0f, 1.0f, 1.0f)
@@ -45,4 +47,7 @@
4547

4648
#define BLACK glm::vec4(0.00f, 0.00f, 0.00f, 1.00f)
4749
#define RED glm::vec4(1.00f, 0.00f, 0.00f, 1.00f)
48-
#define WHITE glm::vec4(1.00f, 1.00f, 1.00f, 1.00f)
50+
#define WHITE glm::vec4(1.00f, 1.00f, 1.00f, 1.00f)
51+
52+
#define BASE 0
53+
#define OVERLAY 1

src/components/arrow.cpp

+12-12
Original file line numberDiff line numberDiff line change
@@ -24,27 +24,27 @@ std::shared_ptr<component> arrow::copy() {
2424
}
2525

2626
void arrow::calc_vertices() {
27-
float dist = glm::length(get_dst() - get_src());
28-
glm::vec3 v = glm::normalize(get_dst() - get_src());
27+
float dist = glm::length(dst() - src());
28+
glm::vec3 v = glm::normalize(dst() - src());
2929
if (dist == 0) v = ZERO3;
3030
glm::vec3 perp(v.y, -v.x, 0.0f);
3131

32-
glm::vec3 end = get_dst() - v * (2 * get_width());
32+
glm::vec3 end = dst() - v * (2 * width());
3333
glm::vec3 bgn;
34-
if (dist <= 2 * get_width()) {
34+
if (dist <= 2 * width()) {
3535
bgn = end;
3636
} else {
37-
bgn = src;
37+
bgn = src();
3838
}
3939

4040
vertices = {
41-
bgn + perp * (get_width() / 2),
42-
bgn - perp * (get_width() / 2),
43-
end - perp * (get_width() / 2),
44-
end - perp * get_width(),
45-
get_dst(),
46-
end + perp * get_width(),
47-
end + perp * (get_width() / 2),
41+
bgn + perp * (width() / 2),
42+
bgn - perp * (width() / 2),
43+
end - perp * (width() / 2),
44+
end - perp * width(),
45+
dst(),
46+
end + perp * width(),
47+
end + perp * (width() / 2),
4848
};
4949
}
5050

0 commit comments

Comments
 (0)