aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorFederico Angelilli <code@fedang.net>2024-03-14 00:58:05 +0100
committerFederico Angelilli <code@fedang.net>2024-03-14 00:58:05 +0100
commit398fdbaa5c02bbd138c59004053d2e8ad0082ead (patch)
treed846ed2d4e811ffeddde9dc77a5549fe38edbd34 /src
parentcbe94d7c42422856275ad41332cbc980054d6bee (diff)
Add animation scaffolding
Diffstat (limited to 'src')
-rw-r--r--src/animate.c40
-rw-r--r--src/animate.h21
-rw-r--r--src/button.c34
-rw-r--r--src/button.h5
4 files changed, 100 insertions, 0 deletions
diff --git a/src/animate.c b/src/animate.c
new file mode 100644
index 0000000..7b95e9c
--- /dev/null
+++ b/src/animate.c
@@ -0,0 +1,40 @@
+#include <glib.h>
+
+#include "animate.h"
+
+double quadratic_bezier(double x, double a, double b, double c)
+{
+ g_assert(x >= 0 && x <= 1);
+ g_assert(a >= 0 && a <= 1);
+ g_assert(b >= 0 && b <= 1);
+ g_assert(c >= 0 && c <= 1);
+
+ const double t = 1 - x;
+ return a * t * t + 2 * b * t * x + c * x * x;
+}
+
+double cubic_bezier(double x, double a, double b, double c, double d)
+{
+ g_assert(x >= 0 && x <= 1);
+ g_assert(a >= 0 && a <= 1);
+ g_assert(b >= 0 && b <= 1);
+ g_assert(c >= 0 && c <= 1);
+ g_assert(d >= 0 && d <= 1);
+
+ const double t = 1 - x;
+ return a * (t * t * t) + 3 * b * (t * t * x) + 3 * c * (t * x * x) + d * (x * x * x);
+}
+
+Animation *animation_create()
+{
+ Animation *anim = g_malloc0(sizeof(Animation));
+ // TODO
+ return anim;
+}
+
+void animation_destroy(Animation *anim)
+{
+ g_free(anim);
+}
+
+// vim: ts=4 sw=4 et
diff --git a/src/animate.h b/src/animate.h
new file mode 100644
index 0000000..3f8d4a6
--- /dev/null
+++ b/src/animate.h
@@ -0,0 +1,21 @@
+#ifndef COMET_ANIMATE_H
+#define COMET_ANIMATE_H
+
+typedef struct {
+ enum {
+ ANIM_SHINE,
+ } type;
+ GSourceFunc handler;
+} Animation;
+
+double quadratic_bezier(double x, double a, double b, double c);
+
+double cubic_bezier(double x, double a, double b, double c, double d);
+
+Animation *animation_create();
+
+void animation_destroy(Animation *anim);
+
+#endif
+
+// vim: ts=4 sw=4 et
diff --git a/src/button.c b/src/button.c
index 7bee576..01d4e36 100644
--- a/src/button.c
+++ b/src/button.c
@@ -1,6 +1,7 @@
#include <glib.h>
#include "button.h"
+#include "log.h"
Button *button_simple_create(PangoAlignment align, Color color, Color line_color)
{
@@ -63,8 +64,41 @@ void button_set_padding(Button *btn, int padding)
btn->padding = padding;
}
+bool button_set_animation(Button *btn, Animation *anim)
+{
+ if (btn->anim) return false;
+ btn->anim = anim;
+ return true;
+}
+
+// Wrapper for animation sources
+static gboolean anim_handler(gpointer data)
+{
+ Button *btn = data;
+ if (btn->anim->handler(btn->anim))
+ return G_SOURCE_CONTINUE;
+
+ g_free(btn->anim);
+ btn->anim = NULL;
+ return G_SOURCE_REMOVE;
+}
+
+void button_start_animation(Button *btn)
+{
+ // Maybe error out
+ if (btn->anim == NULL) {
+ log_warning("Button animation was not set");
+ return;
+ }
+
+ const int fps = 60;
+ if (btn->anim->handler(btn->anim))
+ g_timeout_add(1000 / 60, anim_handler, btn);
+}
+
void button_destroy(Button *btn)
{
if (btn->simple) g_free(CAST(btn, ButtonSimple)->text);
+ animation_destroy(btn->anim);
g_free(btn);
}
diff --git a/src/button.h b/src/button.h
index 0fb821c..923321e 100644
--- a/src/button.h
+++ b/src/button.h
@@ -5,6 +5,8 @@
#include <glib.h>
#include <pango/pangocairo.h>
+#include "animate.h"
+
// For pointers only
#define CAST(ptr, type) ((type *)ptr)
@@ -22,6 +24,7 @@ struct Button {
PangoAlignment align;
Color color;
Color line_color;
+ Animation *anim;
};
typedef struct {
@@ -57,6 +60,8 @@ void button_group_append(Button *btn, Button *child);
void button_set_padding(Button *btn, int padding);
+bool button_set_animation(Button *btn, Animation *anim);
+
void button_destroy(Button *btn);
#endif