Panda3D
cLerpInterval.cxx
1 // Filename: cLerpInterval.cxx
2 // Created by: drose (27Aug02)
3 //
4 ////////////////////////////////////////////////////////////////////
5 //
6 // PANDA 3D SOFTWARE
7 // Copyright (c) Carnegie Mellon University. All rights reserved.
8 //
9 // All use of this software is subject to the terms of the revised BSD
10 // license. You should have received a copy of this license along
11 // with this source code in a file named "LICENSE."
12 //
13 ////////////////////////////////////////////////////////////////////
14 
15 #include "cLerpInterval.h"
16 #include "string_utils.h"
17 
18 TypeHandle CLerpInterval::_type_handle;
19 
20 ////////////////////////////////////////////////////////////////////
21 // Function: CLerpInterval::string_blend_type
22 // Access: Published, Static
23 // Description: Returns the BlendType enumerated value corresponding
24 // to the indicated string, or BT_invalid if the string
25 // doesn't match anything.
26 ////////////////////////////////////////////////////////////////////
27 CLerpInterval::BlendType CLerpInterval::
28 string_blend_type(const string &blend_type) {
29  if (blend_type == "easeIn") {
30  return BT_ease_in;
31  } else if (blend_type == "easeOut") {
32  return BT_ease_out;
33  } else if (blend_type == "easeInOut") {
34  return BT_ease_in_out;
35  } else if (blend_type == "noBlend") {
36  return BT_no_blend;
37  } else {
38  return BT_invalid;
39  }
40 }
41 
42 ////////////////////////////////////////////////////////////////////
43 // Function: CLerpInterval::compute_delta
44 // Access: Protected
45 // Description: Given a t value in the range [0, get_duration()],
46 // returns the corresponding delta value clamped to the
47 // range [0, 1], after scaling by duration and applying
48 // the blend type.
49 ////////////////////////////////////////////////////////////////////
50 double CLerpInterval::
51 compute_delta(double t) const {
52  double duration = get_duration();
53  if (duration == 0.0) {
54  // If duration is 0, the lerp works as a set. Thus, the delta is
55  // always 1.0, the terminating value.
56  return 1.0;
57  }
58  t /= duration;
59  t = min(max(t, 0.0), 1.0);
60 
61  switch (_blend_type) {
62  case BT_ease_in:
63  {
64  double t2 = t * t;
65  return ((3.0 * t2) - (t2 * t)) * 0.5;
66  }
67 
68  case BT_ease_out:
69  {
70  double t2 = t * t;
71  return ((3.0 * t) - (t2 * t)) * 0.5;
72  }
73 
74  case BT_ease_in_out:
75  {
76  double t2 = t * t;
77  return (3.0 * t2) - (2.0 * t * t2);
78  }
79 
80  default:
81  return t;
82  }
83 }
static BlendType string_blend_type(const string &blend_type)
Returns the BlendType enumerated value corresponding to the indicated string, or BT_invalid if the st...
double get_duration() const
Returns the duration of the interval in seconds.
Definition: cInterval.I:32
TypeHandle is the identifier used to differentiate C++ class types.
Definition: typeHandle.h:85