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



commit c34815ebc3521af2fc51e170abc41bf9fdf6be21
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        |   72 +++++++++++++++++++++++++++++++++++++++
 gtk/gtkprogresstrackerprivate.h |   51 +++++++++++++++++++++++++++
 3 files changed, 125 insertions(+), 0 deletions(-)
---
diff --git a/gtk/Makefile.am b/gtk/Makefile.am
index 45f862a..b0c1b7f 100644
--- a/gtk/Makefile.am
+++ b/gtk/Makefile.am
@@ -519,6 +519,7 @@ gtk_private_h_sources =             \
        gtkprintutils.h         \
        gtkprivate.h            \
        gtkpixelcacheprivate.h  \
+       gtkprogresstrackerprivate.h     \
        gtkquery.h              \
        gtkrangeprivate.h       \
        gtkrbtree.h             \
@@ -829,6 +830,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..2fcd726
--- /dev/null
+++ b/gtk/gtkprogresstracker.c
@@ -0,0 +1,72 @@
+/*
+ * 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"
+
+void
+gtk_progress_tracker_start (GtkProgressTracker *helper,
+                            guint duration)
+{
+  helper->is_running = TRUE;
+  helper->progress = 0.0;
+  /* We will not actually record a start time until our first frame has been
+   * recorded. */
+  helper->start_time = 0;
+  helper->end_time = duration;
+}
+
+void
+gtk_progress_tracker_finish (GtkProgressTracker *helper)
+{
+  helper->is_running = FALSE;
+}
+
+void
+gtk_progress_tracker_next_frame (GtkProgressTracker *helper,
+                                 guint frame_time)
+{
+  if (!helper->is_running)
+    return;
+
+  if (helper->start_time == 0)
+    {
+      helper->start_time += frame_time;
+      helper->end_time += frame_time;
+    }
+
+  if (frame_time >= helper->end_time)
+    {
+      helper->is_running = FALSE;
+      return;
+    }
+
+  helper->progress = (frame_time - helper->start_time) / (double) (helper->end_time - helper->start_time);
+}
+
+gboolean
+gtk_progress_tracker_is_running (GtkProgressTracker *helper)
+{
+  return helper->is_running;
+}
+
+gdouble
+gtk_progress_tracker_get_progress (GtkProgressTracker *helper)
+{
+  return helper->is_running ? helper->progress : 1.0;
+}
diff --git a/gtk/gtkprogresstrackerprivate.h b/gtk/gtkprogresstrackerprivate.h
new file mode 100644
index 0000000..9be7b11
--- /dev/null
+++ b/gtk/gtkprogresstrackerprivate.h
@@ -0,0 +1,51 @@
+/*
+ * 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>
+
+G_BEGIN_DECLS
+
+typedef struct _GtkProgressTracker GtkProgressTracker;
+
+struct _GtkProgressTracker
+{
+  gboolean is_running;
+  guint start_time;
+  guint end_time;
+  double progress;
+};
+
+void                 gtk_progress_tracker_start        (GtkProgressTracker *helper,
+                                                        guint duration);
+
+void                 gtk_progress_tracker_finish       (GtkProgressTracker *helper);
+
+void                 gtk_progress_tracker_next_frame   (GtkProgressTracker *helper,
+                                                        guint frame_time);
+
+gboolean             gtk_progress_tracker_is_running   (GtkProgressTracker *helper);
+
+gdouble              gtk_progress_tracker_get_progress (GtkProgressTracker *helper);
+
+G_END_DECLS
+
+#endif /* __GTK_PROGRESS_TRACKER_PRIVATE_H__ */


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