Dali 3D User Interface Engine
path-constrainer-impl.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2015 Samsung Electronics Co., Ltd.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  */
17 
18 // CLASS HEADER
20 
21 //EXTRENAL INCLUDES
22 #include <cstring> // for strcmp
23 
24 // INTERNAL INCLUDES
28 
29 namespace Dali
30 {
31 
32 namespace Internal
33 {
34 
35 namespace
36 {
37 
38 // Properties
39 // Name Type writable animatable constraint-input enum for index-checking
41 DALI_PROPERTY( "forward", VECTOR3, true, false, false, Dali::PathConstrainer::Property::FORWARD )
42 DALI_PROPERTY( "points", ARRAY, true, false, false, Dali::PathConstrainer::Property::POINTS )
43 DALI_PROPERTY( "controlPoints", ARRAY, true, false, false, Dali::PathConstrainer::Property::CONTROL_POINTS )
45 
46 } //Unnamed namespace
47 
49 {
50  return new PathConstrainer();
51 }
52 
54 : Constrainer(),
55  mPath( Path::New() )
56 {
57 }
58 
60 {
61 }
62 
64 {
65  return DEFAULT_PROPERTY_COUNT;
66 }
67 
69 {
70  indices.Reserve( DEFAULT_PROPERTY_COUNT );
71 
72  for ( int i = 0; i < DEFAULT_PROPERTY_COUNT; ++i )
73  {
74  indices.PushBack( i );
75  }
76 }
77 
79 {
80  if ( ( index >= 0 ) && ( index < DEFAULT_PROPERTY_COUNT ) )
81  {
82  return DEFAULT_PROPERTY_DETAILS[index].name;
83  }
84 
85  // index out of range
86  return NULL;
87 }
88 
90 {
92 
93  // Look for name in default properties
94  for( int i = 0; i < DEFAULT_PROPERTY_COUNT; ++i )
95  {
96  const Internal::PropertyDetails* property = &DEFAULT_PROPERTY_DETAILS[ i ];
97  if( 0 == strcmp( name.c_str(), property->name ) ) // dont want to convert rhs to string
98  {
99  index = i;
100  break;
101  }
102  }
103  return index;
104 }
105 
107 {
108  if( index < DEFAULT_PROPERTY_COUNT )
109  {
110  return DEFAULT_PROPERTY_DETAILS[index].type;
111  }
112 
113  // index out of range
114  return Property::NONE;
115 }
116 
118 {
120  {
121  return Property::Value( mForward );
122  }
123  else
124  {
126  {
128  Property::Array* array = value.GetArray();
129  const Dali::Vector<Vector3>& point = mPath->GetPoints();
130  Property::Array::SizeType pointCount = point.Size();
131 
132  if( array )
133  {
134  array->Reserve( pointCount );
135  for( Property::Array::SizeType i = 0; i < pointCount; ++i )
136  {
137  array->PushBack( point[i] );
138  }
139  }
140  return value;
141  }
143  {
145  Property::Array* array = value.GetArray();
146  const Dali::Vector<Vector3>& point = mPath->GetControlPoints();
147  Property::Array::SizeType pointCount = point.Size();
148 
149  if( array )
150  {
151  array->Reserve( pointCount );
152  for( Property::Array::SizeType i = 0; i < pointCount; ++i )
153  {
154  array->PushBack( point[i] );
155  }
156  }
157  return value;
158  }
159  }
160 
161  return Property::Value();
162 }
163 
165 {
167  {
168  propertyValue.Get(mForward);
169  }
170  else if( index == Dali::PathConstrainer::Property::POINTS )
171  {
172  const Property::Array* array = propertyValue.GetArray();
173  mPath->ClearPoints();
174  if( array )
175  {
176  for( Property::Array::SizeType i = 0, count = array->Count(); i < count; ++i )
177  {
178  Vector3 point;
179  array->GetElementAt( i ).Get( point );
180  mPath->AddPoint( point );
181  }
182  }
183  }
185  {
186  const Property::Array* array = propertyValue.GetArray();
187  mPath->ClearControlPoints();
188  if( array )
189  {
190  for( Property::Array::SizeType i = 0, count = array->Count(); i < count; ++i )
191  {
192  Vector3 point;
193  array->GetElementAt( i ).Get( point );
194  mPath->AddControlPoint( point );
195  }
196  }
197  }
198 }
199 
201 {
202  if( index < DEFAULT_PROPERTY_COUNT )
203  {
204  return DEFAULT_PROPERTY_DETAILS[index].writable;
205  }
206 
207  return false;
208 }
209 
211 {
212  if( index < DEFAULT_PROPERTY_COUNT )
213  {
214  return DEFAULT_PROPERTY_DETAILS[index].animatable;
215  }
216 
217  return false;
218 }
219 
221 {
222  if( index < DEFAULT_PROPERTY_COUNT )
223  {
224  return DEFAULT_PROPERTY_DETAILS[index].constraintInput;
225  }
226 
227  return false;
228 }
229 
230 void PathConstrainer::Apply( Property target, Property source, const Vector2& range, const Vector2& wrap)
231 {
232  Dali::Property::Type propertyType = target.object.GetPropertyType( target.propertyIndex);
233  if( propertyType == Dali::Property::VECTOR3)
234  {
235  // If property type is Vector3, constrain its value to the position of the path
236  Dali::Constraint constraint = Dali::Constraint::New<Vector3>( target.object, target.propertyIndex, PathConstraintFunctor( mPath, range, wrap ) );
237  constraint.AddSource( Dali::Source(source.object, source.propertyIndex ) );
238 
239  constraint.SetTag( reinterpret_cast<size_t>( this ) );
241  constraint.Apply();
242  }
243  else if( propertyType == Dali::Property::ROTATION )
244  {
245  // If property type is Rotation, constrain its value to align the forward vector to the tangent of the path
246  Dali::Constraint constraint = Dali::Constraint::New<Quaternion>( target.object, target.propertyIndex, PathConstraintFunctor( mPath, range, mForward, wrap) );
247  constraint.AddSource( Dali::Source(source.object, source.propertyIndex ) );
248 
249  constraint.SetTag( reinterpret_cast<size_t>( this ) );
251  constraint.Apply();
252  }
253 
254  //Start observing the object
255  Observe( target.object );
256 }
257 
258 } // Internal
259 
260 } // Dali
Dali Docs Home
Read more about Dali