path-animation.cpp
15.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
/*
* Copyright (c) 2014 Samsung Electronics Co., Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
/**
* This example shows how to use path animations in DALi
*/
// EXTERNAL INCLUDES
#include <dali-toolkit/dali-toolkit.h>
#include <dali/devel-api/actors/mesh-actor.h>
#include <dali/devel-api/geometry/mesh.h>
#include <dali/devel-api/geometry/mesh-factory.h>
// INTERNAL INCLUDES
#include "shared/view.h"
using namespace Dali;
using namespace Dali::Toolkit;
namespace
{
const char* BACKGROUND_IMAGE( DALI_IMAGE_DIR "background-default.png" );
const char* ACTOR_IMAGE( DALI_IMAGE_DIR "dali-logo.png" );
const char* TOOLBAR_IMAGE( DALI_IMAGE_DIR "top-bar.png" );
const char* APPLICATION_TITLE( "Path Animation Example" );
}; //Unnamed namespace
/**
* @brief The main class of the demo.
*/
class PathController : public ConnectionTracker
{
public:
PathController( Application& application )
: mApplication( application )
{
// Connect to the Application's Init signal
mApplication.InitSignal().Connect( this, &PathController::Create );
}
~PathController()
{
// Nothing to do here.
}
/*
* Create a control composed of a label and an slider
* @param[in] label The text to be displayed ny the label
* @param[in] size The size of the slider
* @param[in] callback Pointer to the callback function to be called when user moves the slider
*/
Actor CreateVectorComponentControl( const std::string& label, const Vector3& size, bool(PathController::*callback)(Slider,float) )
{
TextLabel text = TextLabel::New(label);
text.SetResizePolicy( ResizePolicy::USE_NATURAL_SIZE, Dimension::WIDTH );
text.SetResizePolicy( ResizePolicy::USE_NATURAL_SIZE, Dimension::HEIGHT );
text.SetColor( Vector4(0.0f,0.0f,0.0f,1.0f));
Slider slider = Slider::New();
slider.SetAnchorPoint( AnchorPoint::CENTER_LEFT);
slider.SetParentOrigin( ParentOrigin::CENTER_RIGHT);
slider.SetProperty(Slider::Property::LOWER_BOUND, -1.0f );
slider.SetProperty(Slider::Property::UPPER_BOUND, 1.0f );
Property::Array marks;
float mark = -1.0f;
for(unsigned short i(0); i<21; ++i )
{
marks.PushBack( mark );
mark += 0.1f;
}
slider.SetProperty(Slider::Property::MARKS, marks);
slider.SetProperty(Slider::Property::SNAP_TO_MARKS, true );
slider.SetSize(size);
slider.SetScale( 0.5f );
slider.ValueChangedSignal().Connect(this,callback);
text.Add( slider );
return text;
}
/**
* Crate all the GUI controls
*/
void CreateControls()
{
Stage stage = Stage::GetCurrent();
//TextInput
Dali::Layer controlsLayer = Dali::Layer::New();
controlsLayer.SetResizePolicy( ResizePolicy::SIZE_RELATIVE_TO_PARENT, Dimension::ALL_DIMENSIONS );
controlsLayer.SetSizeModeFactor( Vector3( 1.0f, 0.3f, 1.0f ) );
controlsLayer.SetPosition( 0.0f, stage.GetSize().y*0.8f, 0.0f );
controlsLayer.SetAnchorPoint( AnchorPoint::TOP_LEFT);
controlsLayer.SetParentOrigin( ParentOrigin::TOP_LEFT);
controlsLayer.TouchedSignal().Connect(this, &PathController::OnTouchGuiLayer);
stage.Add( controlsLayer );
Vector3 textInputSize( stage.GetSize().x, stage.GetSize().y*0.04f, 0.0f );
Actor control0 = CreateVectorComponentControl("x:", textInputSize, &PathController::OnSliderXValueChange );
control0.SetY( stage.GetSize().y*0.05f );
control0.SetAnchorPoint(AnchorPoint::TOP_LEFT);
controlsLayer.Add( control0 );
Actor control1 = CreateVectorComponentControl("y:", textInputSize, &PathController::OnSliderYValueChange );
control1.SetAnchorPoint(AnchorPoint::TOP_LEFT);
control1.SetParentOrigin( ParentOrigin::BOTTOM_LEFT );
control1.SetPosition(0.0f,stage.GetSize().y*0.01,0.0f);
control0.Add( control1 );
Actor control2 =CreateVectorComponentControl("z:", textInputSize, &PathController::OnSliderZValueChange );
control2.SetAnchorPoint(AnchorPoint::TOP_LEFT);
control2.SetParentOrigin( ParentOrigin::BOTTOM_LEFT );
control2.SetPosition(0.0f,stage.GetSize().y*0.01,0.0f);
control1.Add( control2 );
}
/**
* Draws the path and the control points for the path
* @param[in] resolution Number of segments for the path.
*/
void DrawPath( unsigned int resolution )
{
Stage stage = Dali::Stage::GetCurrent();
//Create path mesh actor
Dali::MeshData meshData = MeshFactory::NewPath( mPath, resolution );
Dali::Material material = Material::New("LineMaterial");
material.SetDiffuseColor( Vector4(0.0f,0.0f,0.0f,1.0f));
meshData.SetMaterial(material);
Dali::Mesh mesh = Dali::Mesh::New( meshData );
if( mMeshPath )
{
stage.Remove( mMeshPath );
}
mMeshPath = Dali::MeshActor::New( mesh );
mMeshPath.SetAnchorPoint( AnchorPoint::TOP_LEFT );
mMeshPath.SetParentOrigin( ParentOrigin::TOP_LEFT );
stage.Add( mMeshPath );
////Create mesh connecting interpolation points and control points
std::vector<Dali::MeshData::Vertex> vVertex;
std::vector<unsigned short> vIndex;
size_t pointCount = mPath.GetPointCount();
size_t controlPointIndex = 0;
for( size_t i(0); i<pointCount; ++i )
{
vVertex.push_back( MeshData::Vertex(mPath.GetPoint(i),Vector2::ZERO, Vector3::ZERO ) );
if( i<pointCount-1)
{
vVertex.push_back( MeshData::Vertex(mPath.GetControlPoint(controlPointIndex),Vector2::ZERO, Vector3::ZERO ));
vVertex.push_back( MeshData::Vertex(mPath.GetControlPoint(controlPointIndex+1),Vector2::ZERO, Vector3::ZERO ));
}
controlPointIndex += 2;
}
size_t segmentCount = 2*(pointCount-2)+2;
unsigned short index=0;
for( size_t i(0); i<segmentCount; ++i, ++index )
{
vIndex.push_back(index);
vIndex.push_back(index+1);
if( ~i & 1 )
{
index++;
}
}
meshData.SetLineData( vVertex, vIndex, material );
meshData.SetMaterial(material);
mesh = Dali::Mesh::New( meshData );
if( mMeshHandlers )
{
stage.Remove( mMeshHandlers );
}
mMeshHandlers = Dali::MeshActor::New( mesh );
mMeshHandlers.SetAnchorPoint( AnchorPoint::TOP_LEFT );
mMeshHandlers.SetParentOrigin( ParentOrigin::TOP_LEFT );
stage.Add( mMeshHandlers );
//Create actors representing interpolation points
index = 0;
for( size_t i(0); i<pointCount; ++i )
{
if( !mControlPoint[index] )
{
mControlPoint[index] = Toolkit::CreateSolidColorActor(Vector4(1.0f,1.0f,1.0f,1.0f));
mControlPoint[index].SetParentOrigin( ParentOrigin::TOP_LEFT);
mControlPoint[index].SetAnchorPoint( AnchorPoint::CENTER );
mControlPoint[index].SetSize( 20.0f, 20.0f );
mContentLayer.Add(mControlPoint[index]);
}
std::string name( "Knot");
name.push_back(i);
mControlPoint[index].SetName( name );
mControlPoint[index].SetPosition( mPath.GetPoint(i) );
mControlPoint[index].SetColor( Vector4(0.0f,0.0f,0.0f,1.0f));
++index;
}
//Create actors representing control points
size_t controlPointCount=2*(pointCount-1);
for( size_t i(0); i<controlPointCount; ++i )
{
if( !mControlPoint[index])
{
mControlPoint[index] = Toolkit::CreateSolidColorActor(Vector4(1.0f,1.0f,1.0f,1.0f));
mControlPoint[index].SetParentOrigin( ParentOrigin::TOP_LEFT);
mControlPoint[index].SetAnchorPoint( AnchorPoint::CENTER );
mControlPoint[index].SetSize( 20.0f, 20.0f );
mContentLayer.Add(mControlPoint[index]);
}
std::string name( "ControlPoint");
name.push_back(i);
mControlPoint[index].SetName( name );
mControlPoint[index].SetPosition( mPath.GetControlPoint(i) );
mControlPoint[index].SetColor( Vector4(1.0f,0.0f,0.0f,1.0f));
++index;
}
}
/**
* Create the path animation.
*/
void CreateAnimation()
{
if( !mAnimation )
{
mAnimation = Animation::New( 2.0f );
}
else
{
mAnimation.Pause();
mAnimation.Clear();
mActor.SetOrientation( Quaternion() );
}
mAnimation.Animate( mActor, mPath, mForward );
mAnimation.SetLooping( true );
mAnimation.Play();
}
/**
* Get closest control point.void
* @param[in] point Point from where the distance is computed
* @return The closest actor if the distance is beyond 0.5cm or an uninitialized actor otherwise
*/
Actor GetClosestActor(const Vector3& point)
{
size_t pointCount = mPath.GetPointCount();
size_t controlPointCount = 3*pointCount - 2;
Actor result;
float minDistance = 1.0/0.0;
for( size_t i(0); i<controlPointCount; ++i )
{
Vector3 v = mControlPoint[i].GetCurrentPosition() - point;
float distance = v.LengthSquared();
if( distance < minDistance )
{
result = mControlPoint[i];
minDistance = distance;
}
}
Vector2 dpi = Dali::Stage::GetCurrent().GetDpi();
float distanceTreshold = 0.2f * dpi.x * 0.2f * dpi.x;
if( minDistance < distanceTreshold )
{
return result;
}
else
{
return Actor();
}
}
/**
* Callback called when the background layer is touched
*/
bool OnTouchLayer(Actor actor, const TouchEvent& event)
{
if(event.GetPointCount()>0)
{
const TouchPoint& point = event.GetPoint(0);
if(point.state==TouchPoint::Up)
{
//Stop dragging
mDragActor.Reset();
}
else if(point.state==TouchPoint::Down)
{
Vector3 touchPoint = Vector3(event.GetPoint(0).screen.x, event.GetPoint(0).screen.y, 0.0f);
if(!mDragActor )
{
mDragActor = GetClosestActor( touchPoint );
if( !mDragActor && mPath.GetPointCount() < 10 )
{
// Add new point
Vector3 lastPoint = mPath.GetPoint( mPath.GetPointCount()-1);
mPath.AddPoint( touchPoint );
Vector3 displacement = (touchPoint-lastPoint)/8;
mPath.AddControlPoint( lastPoint + displacement );
mPath.AddControlPoint( touchPoint - displacement);
DrawPath( 200u );
CreateAnimation();
}
}
}
else if( mDragActor && point.state==TouchPoint::Motion )
{
Vector3 touchPoint = Vector3(event.GetPoint(0).screen.x, event.GetPoint(0).screen.y, 0.0f);
std::string actorName(mDragActor.GetName());
if( actorName.compare(0, 4, "Knot") == 0)
{
int index = actorName[4];
mPath.GetPoint(index) = touchPoint;
}
else
{
int index = actorName[12];
mPath.GetControlPoint(index) = touchPoint;
}
DrawPath( 200u );
CreateAnimation();
}
}
return true;
}
bool OnTouchGuiLayer(Actor actor, const TouchEvent& event)
{
mDragActor.Reset();
return false;
}
/**
* Callback called when user changes slider X
* @param[in] slider The slider that has generated the signal
* @param[in] value The new value
*/
bool OnSliderXValueChange( Slider s, float value)
{
if( fabs( value ) - Math::MACHINE_EPSILON_1000 < 0.0f )
{
mForward.x = 0.0f;
}
else
{
mForward.x = value;
}
CreateAnimation();
return true;
}
/**
* Callback called when user changes slider Y
* @param[in] slider The slider that has generated the signal
* @param[in] value The new value
*/
bool OnSliderYValueChange( Slider s, float value)
{
if( fabs( value ) - Math::MACHINE_EPSILON_1000 < 0.0f )
{
mForward.y = 0.0f;
}
else
{
mForward.y = value;
}
CreateAnimation();
return true;
}
/**
* Callback called when user changes slider Z
* @param[in] slider The slider that has generated the signal
* @param[in] value The new value
*/
bool OnSliderZValueChange( Slider s, float value)
{
if( fabs( value ) - Math::MACHINE_EPSILON_1000 < 0.0f )
{
mForward.z = 0.0f;
}
else
{
mForward.z = value;
}
CreateAnimation();
return true;
}
/**
* Main key event handler.
* Quit on escape key.
*/
void OnKeyEvent(const KeyEvent& event)
{
if( event.state == KeyEvent::Down )
{
if( IsKey( event, Dali::DALI_KEY_ESCAPE ) ||
IsKey( event, Dali::DALI_KEY_BACK ) )
{
mApplication.Quit();
}
}
}
/**
* One-time setup in response to Application InitSignal.
*/
void Create( Application& application )
{
DemoHelper::RequestThemeChange();
// Get a handle to the stage:
Stage stage = Stage::GetCurrent();
// Connect to input event signals:
stage.KeyEventSignal().Connect(this, &PathController::OnKeyEvent);
// Create a default view with a default tool bar:
Toolkit::Control view; ///< The View instance.
Toolkit::ToolBar toolBar; ///< The View's Toolbar.
mContentLayer = DemoHelper::CreateView( mApplication,
view,
toolBar,
BACKGROUND_IMAGE,
TOOLBAR_IMAGE,
"" );
mContentLayer.TouchedSignal().Connect(this, &PathController::OnTouchLayer);
//Title
TextLabel title = DemoHelper::CreateToolBarLabel( APPLICATION_TITLE );
toolBar.AddControl( title, DemoHelper::DEFAULT_VIEW_STYLE.mToolBarTitlePercentage, Alignment::HorizontalCenter );
//Path
mPath = Dali::Path::New();
mPath.AddPoint( Vector3( 10.0f, stage.GetSize().y*0.5f, 0.0f ));
mPath.AddPoint( Vector3( stage.GetSize().x*0.5f, stage.GetSize().y*0.5f, 0.0f ));
mPath.GenerateControlPoints(0.25f);
DrawPath( 200u );
//Actor
Image img = ResourceImage::New(ACTOR_IMAGE);
mActor = ImageActor::New( img );
mActor.SetAnchorPoint( AnchorPoint::CENTER );
mActor.SetSize( 100, 50, 1 );
stage.Add( mActor );
CreateAnimation();
CreateControls();
}
private:
Application& mApplication;
Layer mContentLayer; ///< The content layer
Path mPath; ///< The path used in the animation
ImageActor mActor; ///< Actor being animated
Vector3 mForward; ///< Current forward vector
Animation mAnimation; ///< Path animation
MeshActor mMeshPath; ///< Mesh actor for the path
MeshActor mMeshHandlers; ///< Mesh actor for the handlers of the path
Actor mControlPoint[28]; ///< ImageActors represeting control points of the path
Actor mDragActor; ///< Reference to the actor currently being dragged
};
void RunTest( Application& application )
{
PathController test( application );
application.MainLoop();
}
/** Entry point for Linux & Tizen applications */
int main( int argc, char **argv )
{
Application application = Application::New( &argc, &argv );
RunTest( application );
return 0;
}