Author: toshok
Date: 2007-06-07 04:35:19 -0400 (Thu, 07 Jun 2007)
New Revision: 78824

Modified:
   trunk/moon/src/ChangeLog
   trunk/moon/src/animation.cpp
   trunk/moon/src/animation.h
   trunk/moon/src/demo.cpp
   trunk/moon/src/runtime.cpp
   trunk/moon/src/runtime.h
Log:
2007-06-07  Chris Toshok  <[EMAIL PROTECTED]>

        * demo.cpp: make all the animation stuff work declaratively, and
        test some of the different repeatbehavior and autoreverse
        settings.
        
        * animation.cpp: huge block of nasty code to determine our
        progress given the combinations of autoreverse and repeatbehavior.

        * animation.h: Add Duration stucture, add a few C api calls for
        getting/setting timeline properties.

        * runtime.h, runtime.cpp: add DURATION support to Value.
        


Modified: trunk/moon/src/ChangeLog
===================================================================
--- trunk/moon/src/ChangeLog    2007-06-07 08:16:32 UTC (rev 78823)
+++ trunk/moon/src/ChangeLog    2007-06-07 08:35:19 UTC (rev 78824)
@@ -1,3 +1,17 @@
+2007-06-07  Chris Toshok  <[EMAIL PROTECTED]>
+
+       * demo.cpp: make all the animation stuff work declaratively, and
+       test some of the different repeatbehavior and autoreverse
+       settings.
+       
+       * animation.cpp: huge block of nasty code to determine our
+       progress given the combinations of autoreverse and repeatbehavior.
+
+       * animation.h: Add Duration stucture, add a few C api calls for
+       getting/setting timeline properties.
+
+       * runtime.h, runtime.cpp: add DURATION support to Value.
+       
 2007-06-07  Jackson Harper  <[EMAIL PROTECTED]>
 
        * runtime.cpp|h: Some pieces of the triggers framework.

Modified: trunk/moon/src/animation.cpp
===================================================================
--- trunk/moon/src/animation.cpp        2007-06-07 08:16:32 UTC (rev 78823)
+++ trunk/moon/src/animation.cpp        2007-06-07 08:35:19 UTC (rev 78824)
@@ -92,24 +92,103 @@
 
 Clock::Clock (Timeline *tl)
   : timeline (tl),
+    current_state (STOPPED),
     queued_events (0)
 {
        SetObjectType (Value::CLOCK);
+
+       RepeatBehavior *repeat = timeline_get_repeat_behavior (timeline);
+       if (repeat->HasCount ()) {
+               // remaining_iterations is an int.. GetCount returns a double.  
badness?
+               remaining_iterations = (int)repeat->GetCount ();
+       }
+       else {
+               // XXX treat both Forever and Duration as Forever.
+               remaining_iterations = -1;
+       }
+
+       autoreverse = timeline_get_autoreverse (timeline);
+       completed_iterations = 0.0;
+
+       Duration *duration = timeline_get_duration (timeline);
 }
 
 void
 Clock::TimeUpdated (guint64 parent_clock_time)
 {
+       if ((current_state & (STOPPED | PAUSED)) != 0)
+               return;
+
        current_time = parent_clock_time - start_time;
-       //printf ("queueing  CURRENT_TIME_INVALIDATED!\n");
        QueueEvent (CURRENT_TIME_INVALIDATED);
+
+       // update our progress
+       Duration *duration = timeline_get_duration (timeline);
+
+       if (*duration == Duration::Automatic  /* useful only on clock groups, 
means the clock stops when all child clocks have stopped. */
+           || *duration == Duration::Forever /* for Forever duration 
timelines, progress is always 0.0 */) {
+               current_progress = 0.0;
+       }
+       else {
+               // we've got a timespan, so our progress will have changed
+               guint64 duration_timespan = duration->GetTimeSpan();
+
+               double new_progress;
+
+               new_progress = (double)(current_time - start_time - 
completed_iterations * (autoreverse ? 2 : 1) * duration_timespan) / 
duration_timespan;
+
+               if (new_progress >= 1.0) {
+                       if (reversed) {
+                               reversed = false;
+
+                               /* we were heading from 1.0 to 0.0 and
+                                  passed it.  decrement our
+                                  remaining-iteration count (if we have
+                                  one) */
+                               completed_iterations += 0.5;
+
+                               current_progress = new_progress - 1.0;
+
+                               if (remaining_iterations > 0)
+                                       remaining_iterations --;
+
+                               if (remaining_iterations == 0) {
+                                       current_progress = 0.0;
+                                       Stop ();
+                               }
+                       }
+                       else {
+                               if (autoreverse) {
+                                       reversed = true;
+                                       completed_iterations += 0.5;
+                                       current_progress = 1.0 - (new_progress 
- 1.0);
+                               }
+                               else {
+
+                                       completed_iterations += 1.0;
+
+                                       current_progress = new_progress - 1.0;
+
+                                       if (remaining_iterations > 0)
+                                               remaining_iterations --;
+
+                                       if (remaining_iterations == 0) {
+                                               current_progress = 1.0;
+                                               Stop ();
+                                       }
+                               }
+                       }
+               }
+               else {
+                       current_progress = reversed ? 1.0 - new_progress : 
new_progress;
+               }
+       }
 }
 
 void
 Clock::RaiseAccumulatedEvents ()
 {
        if ((queued_events & CURRENT_TIME_INVALIDATED) != 0) {
-//       printf ("EMITTING CURRENT_TIME_INVALIDATED\n");
          events->Emit (/*this,*/ "CurrentTimeInvalidated");
        }
 
@@ -137,28 +216,21 @@
 double
 Clock::GetCurrentProgress ()
 {
-       // XXX this is wrong - we shouldn't call GetCurrentTime() here since it 
calls get_now().
-
-//   if (current_state == ClockStopped)
-//     return 0.0;
-
-       guint64/*TimeSpan*/ current_time = GetCurrentTime ();
-       guint64 duration = timeline->GetValue 
(Timeline::DurationProperty)->u.ui64;
-
-//     printf ("Clock::GetCurrentProgress (), current_time = %llu, start_time 
= %llu, current_time - start_time = %llu, duration = %llu\n", current_time, 
start_time, current_time - start_time, duration);
-       
-       return (double)(current_time - start_time) / duration;
+       return current_progress;
 }
 
 void
 Clock::Begin (guint64 start_time)
 {
        this->start_time = start_time;
+       current_state = RUNNING; /* should we invalidate the state here? */
+       QueueEvent (CURRENT_STATE_INVALIDATED);
 }
 
 void
 Clock::Pause ()
 {
+       current_state = (ClockState)(current_state | PAUSED);
 }
 
 void
@@ -169,6 +241,7 @@
 void
 Clock::Resume ()
 {
+       current_state = (ClockState)(current_state & ~PAUSED);
 }
 
 void
@@ -189,6 +262,8 @@
 void
 Clock::Stop ()
 {
+       current_state = STOPPED;
+       QueueEvent (CURRENT_STATE_INVALIDATED);
 }
 
 
@@ -226,12 +301,10 @@
 void
 ClockGroup::TimeUpdated (guint64 parent_clock_time)
 {
-//   printf ("ClockGroup::TimeUpdated\n");
        /* recompute our current_time */
        this->Clock::TimeUpdated (parent_clock_time);
 
        for (GList *l = child_clocks; l; l = l->next) {
-//       printf ("+ Calling child TimeUpdated\n");
                ((Clock*)l->data)->TimeUpdated (current_time);
        }
 }
@@ -244,7 +317,6 @@
 
        /* now cause our children to raise theirs*/
        for (GList *l = child_clocks; l; l = l->next) {
-//             printf ("+ Calling child RaiseAccumulatedEvents\n");
                ((Clock*)l->data)->RaiseAccumulatedEvents ();
        }
 }
@@ -330,18 +402,38 @@
        timeline->SetValue (Timeline::AutoReverseProperty, Value(autoreverse));
 }
 
+bool
+timeline_get_autoreverse (Timeline *timeline)
+{
+       return timeline->GetValue (Timeline::AutoReverseProperty)->u.z;
+}
+
 void
-timeline_set_duration (Timeline *timeline, guint64 duration)
+timeline_set_duration (Timeline *timeline, Duration duration)
 {
        timeline->SetValue (Timeline::DurationProperty, Value(duration));
 }
 
+Duration*
+timeline_get_duration (Timeline *timeline)
+{
+       return timeline->GetValue (Timeline::DurationProperty)->u.duration;
+}
+
 void
 timeline_set_repeat_behavior (Timeline *timeline, RepeatBehavior behavior)
 {
        timeline->SetValue (Timeline::RepeatBehaviorProperty, Value(behavior));
 }
 
+RepeatBehavior *
+timeline_get_repeat_behavior (Timeline *timeline)
+{
+       return timeline->GetValue (Timeline::RepeatBehaviorProperty)->u.repeat;
+}
+
+
+
 /* timeline group */
 
 TimelineGroup::TimelineGroup ()
@@ -435,12 +527,17 @@
 void
 Storyboard::Begin ()
 {
+       // This creates the clock tree for the hierarchy.  if a
+       // Timeline A is a child of TimelineGroup B, then Clock cA
+       // will be a child of ClockGroup cB.
        root_clock = CreateClock ();
 
-       // walk the clock tree hooking up the correct properties and creating 
AnimationStorage's
+       // walk the clock tree hooking up the correct properties and
+       // creating AnimationStorage's for AnimationClocks.
        HookupAnimationsRecurse (root_clock);
 
-       /* hack to make storyboards work..  we need to attach them to 
TimeManager's list of clocks */
+       // hack to make storyboards work..  we need to attach them to
+       // TimeManager's list of clocks
        TimeManager::Instance()->AddChild (root_clock);
 
        root_clock->Begin (TimeManager::GetCurrentGlobalTime());
@@ -566,11 +663,6 @@
 
        double progress = animationClock->GetCurrentProgress ();
 
-//     printf ("in GetCurrentValue, by = %f, from = %f, to = %f\n", by, from, 
to);
-//     printf ("Progress = %lf\n", progress);
-
-//     // XXX we should really use "by" here...
-//     printf ("returning %f\n", from + (to-from) * progress);
        return new Value (from + (to-from) * progress);
 }
 
@@ -620,13 +712,12 @@
 }
 
 
-guint64
-duration_from_seconds (int seconds)
-{
-       return (guint64)seconds * 10000000;
-}
 
+
+
 RepeatBehavior RepeatBehavior::Forever (RepeatBehavior::FOREVER);
+Duration Duration::Automatic (Duration::AUTOMATIC);
+Duration Duration::Forever (Duration::FOREVER);
 
 void 
 animation_init ()
@@ -634,7 +725,7 @@
        /* Timeline properties */
        Timeline::AutoReverseProperty = DependencyObject::Register 
(Value::TIMELINE, "AutoReverse", new Value (false));
        Timeline::BeginTimeProperty = DependencyObject::Register 
(Value::TIMELINE, "BeginTime", new Value ((guint64)0));
-       Timeline::DurationProperty = DependencyObject::Register 
(Value::TIMELINE, "Duration", new Value ((guint64)0));
+       Timeline::DurationProperty = DependencyObject::Register 
(Value::TIMELINE, "Duration", new Value (Duration::Automatic));
        Timeline::RepeatBehaviorProperty = DependencyObject::Register 
(Value::TIMELINE, "RepeatBehavior", new Value (RepeatBehavior ((double)1)));
        //DependencyObject::Register (DependencyObject::TIMELINE, 
"FillBehavior", new Value (0));
        //DependencyObject::Register (DependencyObject::TIMELINE, "SpeedRatio", 
new Value (0));

Modified: trunk/moon/src/animation.h
===================================================================
--- trunk/moon/src/animation.h  2007-06-07 08:16:32 UTC (rev 78823)
+++ trunk/moon/src/animation.h  2007-06-07 08:35:19 UTC (rev 78824)
@@ -3,23 +3,87 @@
 G_BEGIN_DECLS
 
 // misc types
+struct Duration {
+ public:
+       enum DurationKind {
+               TIMESPAN,
+               AUTOMATIC,
+               FOREVER
+       };
+
+       Duration (guint64/*TimeSpan*/ duration)
+         : k (TIMESPAN),
+           timespan (duration) { }
+
+       Duration (const Duration &duration)
+       {
+               k = duration.k;
+               timespan = duration.timespan;
+       }
+
+       Duration (DurationKind kind) : k(kind) { };
+
+       bool HasTimeSpan () { return k == TIMESPAN; }
+       guint64/*TimeSpan*/ GetTimeSpan() { return timespan; }
+
+       static Duration Automatic;
+       static Duration Forever;
+
+       // XXX tons more operators here
+       bool operator!= (const Duration &v) const
+       {
+               return !(*this == v);
+       }
+
+       bool operator== (const Duration &v) const
+       {
+               if (v.k != k)
+                       return false;
+
+               if (v.k == TIMESPAN)
+                       return timespan == v.timespan;
+
+               return true;
+       }
+
+
+       // This should live in a TimeSpan class, but oh well.. */
+       static Duration FromSeconds (int seconds) { return (guint64)seconds * 
10000000; }
+
+       DurationKind k;
+ private:
+       guint64/*TimeSpan*/ timespan;
+};
+
+
+
+
+
 struct RepeatBehavior {
   public:
-       RepeatBehavior (RepeatBehavior *repeat)
-         : k (repeat->k),
-           duration (repeat->duration),
-           count (repeat->count)
+       enum RepeatKind {
+               COUNT,
+               DURATION,
+               FOREVER
+       };
+
+       RepeatBehavior (const RepeatBehavior &repeat)
        {
+               k = repeat.k;
+               duration = repeat.duration;
+               count = repeat.count;
        }
 
        RepeatBehavior (double count)
-       {
-               k = COUNT;
-       }
+         : k (COUNT),
+           count (count) { }
 
+       RepeatBehavior (RepeatKind kind) : k(kind) { }
+
        RepeatBehavior (guint64/*TimeSpan*/ duration)
+         : k (DURATION),
+           duration (duration)
        {
-               k = DURATION;
        }
 
        static RepeatBehavior Forever;
@@ -47,18 +111,10 @@
        bool HasCount() { return k == COUNT; }
        bool HasDuration () { return k == DURATION; }
 
+       RepeatKind k;
   private:
-       enum RepeatKind {
-               COUNT,
-               DURATION,
-               FOREVER
-       };
-
-       RepeatKind k;
        double count;
        guint64/*TimeSpan*/ duration;
-
-       RepeatBehavior (RepeatKind kind) : k(kind) { };
 };
 
 
@@ -114,6 +170,7 @@
 
 class Clock : public DependencyObject {
  public:
+       // events to queue up
        enum {
                CURRENT_GLOBAL_SPEED_INVALIDATED = 0x01,
                CURRENT_STATE_INVALIDATED        = 0x02,
@@ -122,6 +179,14 @@
                SPEED_CHANGED                    = 0x10
        };
 
+       // states
+       enum ClockState {
+               RUNNING = 0x00,
+               STOPPED = 0x01,
+
+               PAUSED  = 0x80
+       };
+
        Clock (Timeline *timeline);
 
        virtual void SpeedChanged () { };
@@ -143,14 +208,22 @@
        virtual void TimeUpdated (guint64 parent_time);
 
  protected:
+       double current_progress;
        guint64/*TimeSpan*/ current_time;
        guint64/*TimeSpan*/ start_time; /* the time we actually started.  used 
for computing CurrentTime */
+
        void QueueEvent (int event);
 
  private:
        Timeline *timeline;
        int queued_events;
-       Clock *parent_clock;
+
+       bool reversed;  // if we're presently working our way from 1.0 progress 
to 0.0
+       bool autoreverse;
+       int remaining_iterations;
+       double completed_iterations;
+
+       ClockState current_state;
 };
 
 
@@ -194,8 +267,13 @@
 };
 
 void timeline_set_autoreverse (Timeline *timeline, bool autoreverse);
-void timeline_set_duration (Timeline *timeline, guint64 duration);
+bool timeline_get_autoreverse (Timeline *timeline);
+
+void timeline_set_duration (Timeline *timeline, Duration duration);
+Duration* timeline_get_duration (Timeline *timeline);
+
 void timeline_set_repeat_behavior (Timeline *timeline, RepeatBehavior 
behavior);
+RepeatBehavior* timeline_get_repeat_behavior (Timeline *timeline);
 
 
 
@@ -388,8 +466,4 @@
 
 BeginStoryboard *begin_storyboard_new ();
 
-
-// XXX this should instead be TimeSpan.FromSeconds, or something.
-guint64 duration_from_seconds (int seconds);
-
 G_END_DECLS

Modified: trunk/moon/src/demo.cpp
===================================================================
--- trunk/moon/src/demo.cpp     2007-06-07 08:16:32 UTC (rev 78823)
+++ trunk/moon/src/demo.cpp     2007-06-07 08:35:19 UTC (rev 78824)
@@ -9,6 +9,7 @@
 #include <sys/time.h>
 #include "runtime.h"
 #include "transform.h"
+#include "animation.h"
 #include "shape.h"
 
 static UIElement *v;
@@ -19,6 +20,8 @@
 static ScaleTransform *s_trans;
 static TranslateTransform *t_trans;
 
+extern NameScope *global_NameScope;
+
 static int do_fps = FALSE;
 static uint64_t last_time;
 
@@ -62,29 +65,6 @@
 }
 
 static gboolean
-animate (gpointer data)
-{
-       rotate_transform_set_angle (v_trans,
-                                   rotate_transform_get_angle (v_trans) + 3);
-
-       rotate_transform_set_angle (r_trans,
-                                   rotate_transform_get_angle (r_trans) - 3);
-       static double scale_change = -0.02;
-
-       scale_transform_set_scale_x (s_trans,
-                                    scale_transform_get_scale_x (s_trans) + 
scale_change);
-       scale_transform_set_scale_y (s_trans,
-                                    scale_transform_get_scale_y (s_trans) + 
scale_change);
-
-       if ((scale_change < 0 && scale_transform_get_scale_x (s_trans) < 0.3)
-           || (scale_change > 0 && scale_transform_get_scale_x (s_trans) > 
0.75)) {
-           scale_change = -scale_change;
-       }
-
-       return TRUE;
-}
-
-static gboolean
 delete_event (GtkWidget *widget, GdkEvent *e, gpointer data)
 {
        gtk_main_quit ();
@@ -104,6 +84,8 @@
        gdk_threads_init ();
        runtime_init ();
 
+       TimeManager::Instance()->Start();
+
        w = gtk_window_new (GTK_WINDOW_TOPLEVEL);
        gtk_signal_connect (GTK_OBJECT (w), "delete_event", G_CALLBACK 
(delete_event), NULL);
        Surface *t = surface_new (600, 600);
@@ -191,7 +173,58 @@
                item_set_render_transform (v2, s_trans);
                panel_child_add (canvas, v2);
 #endif
-               gtk_timeout_add (60, animate, NULL);
+
+               Storyboard *sb = new Storyboard ();
+
+               DoubleAnimation *r_anim = new DoubleAnimation ();
+               DoubleAnimation *v_anim = new DoubleAnimation ();
+               DoubleAnimation *sx_anim = new DoubleAnimation ();
+               DoubleAnimation *sy_anim = new DoubleAnimation ();
+
+               // The rectangle rotates completely around every 4
+               // seconds, and stops after the second time around
+               global_NameScope->RegisterName ("rect-transform", r_trans);
+               double_animation_set_to (r_anim, -360.0);
+               timeline_set_repeat_behavior (r_anim, RepeatBehavior(2.0));
+               timeline_set_duration (r_anim, Duration::FromSeconds (4));
+               timeline_group_add_child (sb, r_anim);
+               storyboard_child_set_target_name (sb, r_anim, "rect-transform");
+               storyboard_child_set_target_property (sb, r_anim, "Angle");
+
+               // The rotating video takes 5 seconds to complete the rotation
+               global_NameScope->RegisterName ("video-transform", v_trans);
+               double_animation_set_to (v_anim, 360.0);
+               timeline_set_repeat_behavior (v_anim, RepeatBehavior::Forever);
+               timeline_set_duration (v_anim, Duration::FromSeconds (5));
+               timeline_group_add_child (sb, v_anim);
+               storyboard_child_set_target_name (sb, v_anim, 
"video-transform");
+               storyboard_child_set_target_property (sb, v_anim, "Angle");
+
+
+               // for the scaled items, we scale X and Y differently,
+               // the X scaling is completed in 6 seconds, and the y
+               // in 7.
+               global_NameScope->RegisterName ("scale-transform", s_trans);
+
+               double_animation_set_from (sx_anim, 1.0);
+               double_animation_set_to (sx_anim, 0.0);
+               timeline_set_repeat_behavior (sx_anim, RepeatBehavior::Forever);
+               timeline_set_duration (sx_anim, Duration::FromSeconds (6));
+               timeline_set_autoreverse (sx_anim, true);
+               timeline_group_add_child (sb, sx_anim);
+               storyboard_child_set_target_name (sb, sx_anim, 
"scale-transform");
+               storyboard_child_set_target_property (sb, sx_anim, "ScaleX");
+
+               double_animation_set_from (sy_anim, 1.0);
+               double_animation_set_to (sy_anim, 0.0);
+               timeline_set_repeat_behavior (sy_anim, RepeatBehavior::Forever);
+               timeline_set_duration (sy_anim, Duration::FromSeconds (7));
+               timeline_set_autoreverse (sy_anim, true);
+               timeline_group_add_child (sb, sy_anim);
+               storyboard_child_set_target_name (sb, sy_anim, 
"scale-transform");
+               storyboard_child_set_target_property (sb, sy_anim, "ScaleY");
+
+               sb->Begin ();
        }               
        if (do_fps){
                t->frames = 0;

Modified: trunk/moon/src/runtime.cpp
===================================================================
--- trunk/moon/src/runtime.cpp  2007-06-07 08:16:32 UTC (rev 78823)
+++ trunk/moon/src/runtime.cpp  2007-06-07 08:35:19 UTC (rev 78824)
@@ -161,6 +161,13 @@
        u.repeat = new RepeatBehavior (repeat);
 }
 
+Value::Value (Duration duration)
+{
+       Init();
+       k = DURATION;
+       u.duration = new Duration (duration);
+}
+
 Value::Value (const char* s)
 {
        Init ();

Modified: trunk/moon/src/runtime.h
===================================================================
--- trunk/moon/src/runtime.h    2007-06-07 08:16:32 UTC (rev 78823)
+++ trunk/moon/src/runtime.h    2007-06-07 08:35:19 UTC (rev 78824)
@@ -21,6 +21,7 @@
        GHashTable *event_hash;
 };
 
+struct Duration;
 struct RepeatBehavior;
 class Transform;
 class TransformGroup;
@@ -157,6 +158,7 @@
                POINT = 8,
                RECT = 9,
                REPEATBEHAVIOR = 10,
+               DURATION = 11,
 
                DEPENDENCY_OBJECT = 1000,
 
@@ -217,6 +219,7 @@
                Point *point;
                Rect *rect;
                RepeatBehavior *repeat;
+               Duration *duration;
        } u;
 
        void Init ();
@@ -232,6 +235,7 @@
        Value (Point pt);
        Value (Rect rect);
        Value (RepeatBehavior repeat);
+       Value (Duration duration);
        Value (const char* s);
        
        ~Value ();

_______________________________________________
Mono-patches maillist  -  [email protected]
http://lists.ximian.com/mailman/listinfo/mono-patches

Reply via email to