[gtk+/wip/watson/progress-tracker: 1/13] progresstracker: simple struct to track animation progress



commit 7827434977db40889e51226e44effce780ca2e0f
Author: Matt Watson <mattdangerw gmail com>
Date:   Mon Feb 29 21:39:33 2016 -0800

    progresstracker: simple struct to track animation progress

 gtk/Makefile.am                 |    2 +
 gtk/gtkprogresstracker.c        |  148 +++++++++++++++++++++++++++++++++++++++
 gtk/gtkprogresstrackerprivate.h |   70 ++++++++++++++++++
 3 files changed, 220 insertions(+), 0 deletions(-)
---
diff --git a/gtk/Makefile.am b/gtk/Makefile.am
index d2b91d6..fd36b5d 100644
--- a/gtk/Makefile.am
+++ b/gtk/Makefile.am
@@ -520,6 +520,7 @@ gtk_private_h_sources =             \
        gtkprintutils.h         \
        gtkprivate.h            \
        gtkpixelcacheprivate.h  \
+       gtkprogresstrackerprivate.h     \
        gtkquery.h              \
        gtkrangeprivate.h       \
        gtkrbtree.h             \
@@ -831,6 +832,7 @@ gtk_base_c_sources =                \
        gtkprivate.c            \
        gtkprivatetypebuiltins.c \
        gtkprogressbar.c        \
+       gtkprogresstracker.c    \
        gtkpixelcache.c         \
        gtkpopover.c            \
        gtkpopovermenu.c        \
diff --git a/gtk/gtkprogresstracker.c b/gtk/gtkprogresstracker.c
new file mode 100644
index 0000000..ac2f9d8
--- /dev/null
+++ b/gtk/gtkprogresstracker.c
@@ -0,0 +1,148 @@
+/*
+ * Copyright © 2016 Endless Mobile Inc.
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library. If not, see <http://www.gnu.org/licenses/>.
+ *
+ * Authors: Matthew Watson <mattdangerw gmail com>
+ */
+
+#include "gtkprogresstrackerprivate.h"
+#include "gtkcsseasevalueprivate.h"
+
+#include <math.h>
+
+void
+gtk_progress_tracker_start (GtkProgressTracker *tracker,
+                            guint64 duration,
+                            gint64 delay,
+                            gdouble iteration_count)
+{
+  tracker->is_running = TRUE;
+  tracker->last_frame_time = 0;
+  tracker->duration = duration;
+  tracker->iteration = - delay / (gdouble) duration;
+  tracker->iteration_count = iteration_count;
+}
+
+void
+gtk_progress_tracker_finish (GtkProgressTracker *tracker)
+{
+  tracker->is_running = FALSE;
+}
+
+void
+gtk_progress_tracker_next_frame (GtkProgressTracker *tracker,
+                                 guint64 frame_time)
+{
+  gdouble delta;
+
+  if (!tracker->is_running)
+    return;
+
+  if (tracker->last_frame_time == 0)
+    {
+      tracker->last_frame_time = frame_time;
+      return;
+    }
+
+  if (frame_time < tracker->last_frame_time)
+    {
+      g_warning ("Progress tracker frame set backwards, ignoring\n");
+      return;
+    }
+
+  delta = (frame_time - tracker->last_frame_time) / (gdouble) tracker->duration;
+  tracker->last_frame_time = frame_time;
+  tracker->iteration += delta;
+}
+
+GtkProgressState
+gtk_progress_tracker_get_state (GtkProgressTracker *tracker)
+{
+  if (!tracker->is_running || tracker->iteration > tracker->iteration_count)
+    return GTK_PROGRESS_STATE_AFTER;
+  if (tracker->iteration < 0)
+    return GTK_PROGRESS_STATE_BEFORE;
+  return GTK_PROGRESS_STATE_DURING;
+}
+
+gdouble
+gtk_progress_tracker_get_iteration (GtkProgressTracker *tracker)
+{
+  return tracker->is_running ? tracker->iteration : 1.0;
+}
+
+gint
+gtk_progress_tracker_get_iteration_cycle (GtkProgressTracker *tracker)
+{
+  gdouble iteration;
+
+  if (!tracker->is_running)
+    return 0;
+
+  iteration = CLAMP (tracker->iteration, 0.0, tracker->iteration_count);
+
+  if (iteration == 0.0)
+    return 0;
+
+  return (gint) ceil (iteration) - 1;
+}
+
+gdouble
+gtk_progress_tracker_get_progress (GtkProgressTracker *tracker,
+                                   gboolean reversed)
+{
+  gdouble progress, iteration;
+
+  if (!tracker->is_running)
+    return reversed ? 0.0 : 1.0;
+
+  iteration = CLAMP (tracker->iteration, 0.0, tracker->iteration_count);
+
+  if (iteration == 0.0)
+    progress = 0.0;
+  else
+    {
+      progress = fmod (iteration, 1.0);
+      if (progress == 0.0)
+        progress = 1.0;
+    }
+
+  return reversed ? 1.0 - progress : progress;
+}
+
+/* From clutter-easing.c, based on Robert Penner's
+ * infamous easing equations, MIT license.
+ */
+static gdouble
+ease_out_cubic (gdouble t)
+{
+  gdouble p = t - 1;
+  return p * p * p + 1;
+}
+
+gdouble
+gtk_progress_tracker_get_ease (GtkProgressTracker *tracker,
+                               GtkCssValue *ease,
+                               gboolean reversed)
+{
+  gdouble progress = gtk_progress_tracker_get_progress (tracker, reversed);
+
+  /* XXX: We've combined our css easing and non css easing here in the same
+   * function. Would be nice to open up csseasevalue so you could create one
+   * without a css parser, and use the same easings across the entire codebase */
+  if (ease == NULL)
+    return ease_out_cubic (progress);
+  return _gtk_css_ease_value_transform (ease, progress);
+}
diff --git a/gtk/gtkprogresstrackerprivate.h b/gtk/gtkprogresstrackerprivate.h
new file mode 100644
index 0000000..bd451b0
--- /dev/null
+++ b/gtk/gtkprogresstrackerprivate.h
@@ -0,0 +1,70 @@
+/*
+ * Copyright © 2016 Endless Mobile Inc.
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library. If not, see <http://www.gnu.org/licenses/>.
+ *
+ * Authors: Matthew Watson <mattdangerw gmail com>
+ */
+
+#ifndef __GTK_PROGRESS_TRACKER_PRIVATE_H__
+#define __GTK_PROGRESS_TRACKER_PRIVATE_H__
+
+#include <glib-object.h>
+#include "gtkcsseasevalueprivate.h"
+
+G_BEGIN_DECLS
+
+typedef enum {
+  GTK_PROGRESS_STATE_BEFORE,
+  GTK_PROGRESS_STATE_DURING,
+  GTK_PROGRESS_STATE_AFTER,
+} GtkProgressState;
+
+typedef struct _GtkProgressTracker GtkProgressTracker;
+
+struct _GtkProgressTracker
+{
+  gboolean is_running;
+  guint64 last_frame_time;
+  guint64 duration;
+  gdouble iteration;
+  gdouble iteration_count;
+};
+
+void                 gtk_progress_tracker_start        (GtkProgressTracker *tracker,
+                                                        guint64 duration,
+                                                        gint64 delay,
+                                                        gdouble iteration_count);
+
+void                 gtk_progress_tracker_finish       (GtkProgressTracker *tracker);
+
+void                 gtk_progress_tracker_next_frame   (GtkProgressTracker *tracker,
+                                                        guint64 frame_time);
+
+GtkProgressState     gtk_progress_tracker_get_state    (GtkProgressTracker *tracker);
+
+gdouble              gtk_progress_tracker_get_iteration (GtkProgressTracker *tracker);
+
+gint                 gtk_progress_tracker_get_iteration_cycle (GtkProgressTracker *tracker);
+
+gdouble              gtk_progress_tracker_get_progress (GtkProgressTracker *tracker,
+                                                        gboolean reverse);
+
+gdouble              gtk_progress_tracker_get_ease     (GtkProgressTracker *tracker,
+                                                        GtkCssValue *ease,
+                                                        gboolean reverse);
+
+G_END_DECLS
+
+#endif /* __GTK_PROGRESS_TRACKER_PRIVATE_H__ */


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]