瀏覽代碼

add pingpong support

David Rose 24 年之前
父節點
當前提交
6a75a90cec
共有 2 個文件被更改,包括 53 次插入0 次删除
  1. 50 0
      panda/src/chan/animControl.cxx
  2. 3 0
      panda/src/chan/animControl.h

+ 50 - 0
panda/src/chan/animControl.cxx

@@ -190,6 +190,33 @@ loop(bool restart, int from, int to) {
   }
 }
 
+////////////////////////////////////////////////////////////////////
+//     Function: AnimControl::pingpong
+//       Access: Public
+//  Description: Loops the animation from the frame "from" to and
+//               including the frame "to", and then back in the
+//               opposite direction, indefinitely.
+////////////////////////////////////////////////////////////////////
+void AnimControl::
+pingpong(bool restart, int from, int to) {
+  nassertv(get_num_frames() > 0);
+
+  nassertv(from >= 0 && from < get_num_frames());
+  nassertv(to >= 0 && to < get_num_frames());
+  _as_of_time = ClockObject::get_global_clock()->get_frame_time();
+  _playing = true;
+
+  _actions = _user_actions;
+
+  insert_reverse_action(_actions, (to-1+get_num_frames())%get_num_frames());
+  insert_reverse_action(_actions, (to+1)%get_num_frames());
+
+  get_part()->control_activated(this);
+  if (restart) {
+    set_frame(from);
+  }
+}
+
 ////////////////////////////////////////////////////////////////////
 //     Function: AnimControl::stop
 //       Access: Public
@@ -466,6 +493,20 @@ insert_jump_action(Actions &actions, int frame, int jump_to) {
   actions.insert(pair<int, Action>(frame, new_action));
 }
 
+////////////////////////////////////////////////////////////////////
+//     Function: AnimControl::insert_reverse_action
+//       Access: Private, Static
+//  Description: Inserts a "reverse" action at the indicated frame
+//               number.  The animation will stop and go back the
+//               other direction when it reaches the indicated frame.
+////////////////////////////////////////////////////////////////////
+void AnimControl::
+insert_reverse_action(Actions &actions, int frame) {
+  Action new_action;
+  new_action._type = AT_reverse;
+  actions.insert(pair<int, Action>(frame, new_action));
+}
+
 
 ////////////////////////////////////////////////////////////////////
 //     Function: AnimControl::set_frame
@@ -596,6 +637,7 @@ do_action(int frame, const Action &action,
   switch (action._type) {
   case AT_stop:
   case AT_jump:
+  case AT_reverse:
     sequence_frame = frame;
     sequence_action = &action;
     break;
@@ -630,6 +672,10 @@ do_sequence_action(int, const Action &action) {
     set_frame(action._jump_to);
     break;
 
+  case AT_reverse:
+    _play_rate = -_play_rate;
+    break;
+
   case AT_event:
   default:
     chan_cat.error() << "Invalid sequence action!\n";
@@ -657,6 +703,10 @@ output(ostream &out) const {
     out << "jump to " << _jump_to;
     break;
 
+  case AT_reverse:
+    out << "reverse";
+    break;
+
   default:
     out << "**error**";
   }

+ 3 - 0
panda/src/chan/animControl.h

@@ -53,6 +53,7 @@ PUBLISHED:
   void play(int from, int to, const CPT_Event &stop_event = NULL);
   void loop(bool restart);
   void loop(bool restart, int from, int to);
+  void pingpong(bool restart, int from, int to);
   void stop();
   void pose(int frame);
 
@@ -92,6 +93,7 @@ private:
     AT_event,
     AT_stop,
     AT_jump,
+    AT_reverse,
   };
 
 #ifdef WIN32_VC
@@ -113,6 +115,7 @@ private:
   static void insert_event_action(Actions &actions, int frame, const CPT_Event &event);
   static void insert_stop_action(Actions &actions, int frame);
   static void insert_jump_action(Actions &actions, int frame, int jump_to);
+  static void insert_reverse_action(Actions &actions, int frame);
 
   void set_frame(double frame);