[dali_1.2.1] Merge branch 'devel/master'
[platform/core/uifw/dali-demo.git] / examples / line-mesh / line-mesh-example.cpp
1 /*
2  * Copyright (c) 2016 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 // EXTERNAL INCLUDES
19 #include <dali/public-api/rendering/renderer.h>
20 #include <dali-toolkit/dali-toolkit.h>
21
22 // INTERNAL INCLUDES
23 #include "shared/view.h"
24
25 #include <sstream>
26
27 using namespace Dali;
28
29 namespace
30 {
31
32 #define MAKE_SHADER(A)#A
33
34 const char* VERTEX_SHADER = MAKE_SHADER(
35 attribute mediump vec2    aPosition1;
36 attribute mediump vec2    aPosition2;
37 attribute lowp    vec3    aColor;
38 uniform   mediump mat4    uMvpMatrix;
39 uniform   mediump vec3    uSize;
40 uniform   mediump float   uMorphAmount;
41
42 varying   lowp    vec3    vColor;
43
44 void main()
45 {
46   mediump vec2 morphPosition = mix(aPosition1, aPosition2, uMorphAmount);
47   mediump vec4 vertexPosition = vec4(morphPosition, 0.0, 1.0);
48   vColor = aColor;
49   vertexPosition.xyz *= uSize;
50   vertexPosition = uMvpMatrix * vertexPosition;
51   gl_Position = vertexPosition;
52 }
53 );
54
55 const char* FRAGMENT_SHADER = MAKE_SHADER(
56 uniform lowp  vec4    uColor;
57 uniform sampler2D     sTexture;
58
59 varying   lowp        vec3 vColor;
60
61 void main()
62 {
63   gl_FragColor = uColor * vec4( vColor, 1.0 );
64 }
65 );
66
67 const unsigned short INDEX_LINES[] = { 0, 1, 1, 2, 2, 3, 3, 4, 4, 0 };
68 const unsigned short INDEX_LOOP[] =  { 0, 1, 2, 3, 4 };
69 const unsigned short INDEX_STRIP[] = { 0, 1, 2, 3, 4, 0 };
70 const unsigned short* INDICES[3] = { &INDEX_LINES[0], &INDEX_LOOP[0], &INDEX_STRIP[0] };
71 const unsigned int INDICES_SIZE[3] = { sizeof(INDEX_LINES)/sizeof(INDEX_LINES[0]), sizeof(INDEX_LOOP)/sizeof(INDEX_LOOP[0]), sizeof(INDEX_STRIP)/sizeof(INDEX_STRIP[0])};
72
73 Geometry CreateGeometry()
74 {
75   // Create vertices
76   struct Vertex
77   {
78     Vector2 position1;
79     Vector2 position2;
80     Vector3 color;
81   };
82
83   // Create new geometry object
84   Vertex pentagonVertexData[5] =
85     {
86       { Vector2(  0.0f,   1.00f),  Vector2(  0.0f,  -1.00f),  Vector3( 1.0f, 1.0f, 1.0f ) }, // 0
87       { Vector2( -0.95f,  0.31f),  Vector2(  0.59f,  0.81f),  Vector3( 1.0f, 0.0f, 0.0f ) }, // 1
88       { Vector2( -0.59f, -0.81f),  Vector2( -0.95f, -0.31f),  Vector3( 0.0f, 1.0f, 0.0f ) }, // 2
89       { Vector2(  0.59f, -0.81f),  Vector2(  0.95f, -0.31f),  Vector3( 0.0f, 0.0f, 1.0f ) }, // 3
90       { Vector2(  0.95f,  0.31f),  Vector2( -0.59f,  0.81f),  Vector3( 1.0f, 1.0f, 0.0f ) }, // 4
91     };
92
93   Property::Map pentagonVertexFormat;
94   pentagonVertexFormat["aPosition1"] = Property::VECTOR2;
95   pentagonVertexFormat["aPosition2"] = Property::VECTOR2;
96   pentagonVertexFormat["aColor"] = Property::VECTOR3;
97   PropertyBuffer pentagonVertices = PropertyBuffer::New( pentagonVertexFormat );
98   pentagonVertices.SetData(pentagonVertexData, 5);
99
100
101   // Create the geometry object
102   Geometry pentagonGeometry = Geometry::New();
103   pentagonGeometry.AddVertexBuffer( pentagonVertices );
104   pentagonGeometry.SetIndexBuffer( INDICES[0], INDICES_SIZE[0] );
105   pentagonGeometry.SetType( Geometry::LINES );
106   return pentagonGeometry;
107 }
108
109 } // anonymous namespace
110
111 // This example shows how to morph between 2 meshes with the same number of
112 // vertices.
113 class ExampleController : public ConnectionTracker
114 {
115 public:
116
117   /**
118    * The example controller constructor.
119    * @param[in] application The application instance
120    */
121   ExampleController( Application& application )
122   : mApplication( application )
123   {
124     // Connect to the Application's Init signal
125     mApplication.InitSignal().Connect( this, &ExampleController::Create );
126   }
127
128   /**
129    * The example controller destructor
130    */
131   ~ExampleController()
132   {
133     // Nothing to do here;
134   }
135
136   /**
137    * Invoked upon creation of application
138    * @param[in] application The application instance
139    */
140   void Create( Application& application )
141   {
142     Stage stage = Stage::GetCurrent();
143
144     // initial settings
145     mPrimitiveType = Geometry::LINES;
146     mCurrentIndexCount = 10;
147     mMaxIndexCount = 10;
148
149     CreateRadioButtons();
150
151     stage.KeyEventSignal().Connect(this, &ExampleController::OnKeyEvent);
152
153     mStageSize = stage.GetSize();
154
155     Initialise();
156
157     // Hide the indicator bar
158     application.GetWindow().ShowIndicator( Dali::Window::INVISIBLE );
159
160     stage.SetBackgroundColor(Vector4(0.0f, 0.2f, 0.2f, 1.0f));
161   }
162
163   /**
164    * Invoked whenever application changes the type of geometry drawn
165    */
166   void Initialise()
167   {
168     Stage stage = Stage::GetCurrent();
169
170     // destroy mesh actor and its resources if already exists
171     if( mMeshActor )
172     {
173       stage.Remove( mMeshActor );
174       mMeshActor.Reset();
175     }
176
177     mShader = Shader::New( VERTEX_SHADER, FRAGMENT_SHADER );
178     mGeometry = CreateGeometry();
179     mRenderer = Renderer::New( mGeometry, mShader );
180
181     mRenderer.SetIndexRange( 0, 10 ); // lines
182     mPrimitiveType = Geometry::LINES;
183
184     mMeshActor = Actor::New();
185     mMeshActor.AddRenderer( mRenderer );
186     mMeshActor.SetSize(200, 200);
187
188     Property::Index morphAmountIndex = mMeshActor.RegisterProperty( "uMorphAmount", 0.0f );
189
190     mRenderer.SetProperty( Renderer::Property::DEPTH_INDEX, 0 );
191
192     mMeshActor.SetParentOrigin( ParentOrigin::CENTER );
193     mMeshActor.SetAnchorPoint( AnchorPoint::CENTER );
194     stage.Add( mMeshActor );
195
196     Animation  animation = Animation::New(5);
197     KeyFrames keyFrames = KeyFrames::New();
198     keyFrames.Add(0.0f, 0.0f);
199     keyFrames.Add(1.0f, 1.0f);
200
201     animation.AnimateBetween( Property( mMeshActor, morphAmountIndex ), keyFrames, AlphaFunction(AlphaFunction::SIN) );
202     animation.SetLooping(true);
203     animation.Play();
204   }
205
206   /**
207    * Invoked on create
208    */
209   void CreateRadioButtons()
210   {
211     Stage stage = Stage::GetCurrent();
212
213     Toolkit::TableView modeSelectTableView = Toolkit::TableView::New( 4, 1 );
214     modeSelectTableView.SetParentOrigin( ParentOrigin::TOP_LEFT );
215     modeSelectTableView.SetAnchorPoint( AnchorPoint::TOP_LEFT );
216     modeSelectTableView.SetFitHeight( 0 );
217     modeSelectTableView.SetFitHeight( 1 );
218     modeSelectTableView.SetFitHeight( 2 );
219     modeSelectTableView.SetCellPadding( Vector2( 6.0f, 0.0f ) );
220     modeSelectTableView.SetScale( Vector3( 0.8f, 0.8f, 0.8f ));
221
222     const char* labels[] =
223     {
224       "LINES",
225       "LINE_LOOP",
226       "LINE_STRIP"
227     };
228
229     for( int i = 0; i < 3; ++i )
230     {
231       Property::Map labelMap;
232       labelMap[ "text" ]      = labels[i];
233       labelMap[ "textColor" ] = Vector4( 0.8f, 0.8f, 0.8f, 1.0f );
234
235       Dali::Toolkit::RadioButton radio = Dali::Toolkit::RadioButton::New();
236
237       radio.SetProperty( Dali::Toolkit::Button::Property::LABEL, labelMap );
238       radio.SetParentOrigin( ParentOrigin::TOP_LEFT );
239       radio.SetAnchorPoint( AnchorPoint::TOP_LEFT );
240       radio.SetSelected( i == 0 );
241       radio.PressedSignal().Connect( this, &ExampleController::OnButtonPressed );
242       mButtons[i] = radio;
243       modeSelectTableView.AddChild( radio, Toolkit::TableView::CellPosition( i,  0 ) );
244     }
245
246     Toolkit::TableView elementCountTableView = Toolkit::TableView::New( 1, 3 );
247     elementCountTableView.SetCellPadding( Vector2( 6.0f, 0.0f ) );
248     elementCountTableView.SetParentOrigin( ParentOrigin::BOTTOM_LEFT );
249     elementCountTableView.SetAnchorPoint( AnchorPoint::BOTTOM_LEFT );
250     elementCountTableView.SetFitHeight( 0 );
251     elementCountTableView.SetFitWidth( 0 );
252     elementCountTableView.SetFitWidth( 1 );
253     elementCountTableView.SetFitWidth( 2 );
254     mMinusButton = Toolkit::PushButton::New();
255     mMinusButton.SetLabelText( "<<" );
256     mMinusButton.SetParentOrigin( ParentOrigin::TOP_LEFT );
257     mMinusButton.SetAnchorPoint( AnchorPoint::CENTER_LEFT );
258
259     Toolkit::PushButton mPlusButton = Toolkit::PushButton::New();
260     mPlusButton.SetLabelText( ">>" );
261     mPlusButton.SetParentOrigin( ParentOrigin::TOP_LEFT );
262     mPlusButton.SetAnchorPoint( AnchorPoint::CENTER_RIGHT );
263
264     mMinusButton.ClickedSignal().Connect( this, &ExampleController::OnButtonClicked );
265     mPlusButton.ClickedSignal().Connect( this, &ExampleController::OnButtonClicked );
266
267     mIndicesCountLabel = Toolkit::TextLabel::New();
268     mIndicesCountLabel.SetParentOrigin( ParentOrigin::CENTER );
269     mIndicesCountLabel.SetAnchorPoint( AnchorPoint::TOP_LEFT );
270
271     std::stringstream str;
272     str << mCurrentIndexCount;
273     mIndicesCountLabel.SetProperty( Toolkit::TextLabel::Property::TEXT, str.str() );
274     mIndicesCountLabel.SetProperty( Toolkit::TextLabel::Property::TEXT_COLOR, Vector4( 1.0, 1.0, 1.0, 1.0 ) );
275     mIndicesCountLabel.SetProperty( Toolkit::TextLabel::Property::VERTICAL_ALIGNMENT, "BOTTOM");
276     mIndicesCountLabel.SetResizePolicy( ResizePolicy::USE_NATURAL_SIZE, Dimension::WIDTH );
277     mIndicesCountLabel.SetResizePolicy( ResizePolicy::USE_NATURAL_SIZE, Dimension::HEIGHT );
278
279     elementCountTableView.AddChild( mMinusButton, Toolkit::TableView::CellPosition( 0,  0 ) );
280     elementCountTableView.AddChild( mIndicesCountLabel, Toolkit::TableView::CellPosition( 0,  1 ) );
281     elementCountTableView.AddChild( mPlusButton, Toolkit::TableView::CellPosition( 0,  2 ) );
282
283     stage.Add(modeSelectTableView);
284     stage.Add(elementCountTableView);
285   }
286
287   /**
288    * Invoked whenever the quit button is clicked
289    * @param[in] button the quit button
290    */
291   bool OnQuitButtonClicked( Toolkit::Button button )
292   {
293     // quit the application
294     mApplication.Quit();
295     return true;
296   }
297
298   void OnKeyEvent(const KeyEvent& event)
299   {
300     if(event.state == KeyEvent::Down)
301     {
302       if( IsKey( event, Dali::DALI_KEY_ESCAPE) || IsKey( event, Dali::DALI_KEY_BACK) )
303       {
304         mApplication.Quit();
305       }
306     }
307   }
308
309   bool OnButtonPressed( Toolkit::Button button )
310   {
311     int indicesArray;
312     if( button == mButtons[0] )
313     {
314       mCurrentIndexCount = 10;
315       mMaxIndexCount = 10;
316       mPrimitiveType = Geometry::LINES;
317       indicesArray = 0;
318     }
319     else if( button == mButtons[1] )
320     {
321       mCurrentIndexCount = 5;
322       mMaxIndexCount = 5;
323       mPrimitiveType = Geometry::LINE_LOOP;
324       indicesArray = 1;
325     }
326     else
327     {
328       mCurrentIndexCount = 6;
329       mMaxIndexCount = 6;
330       mPrimitiveType = Geometry::LINE_STRIP;
331       indicesArray = 2;
332     }
333
334     std::stringstream str;
335     str << mCurrentIndexCount;
336     mIndicesCountLabel.SetProperty( Toolkit::TextLabel::Property::TEXT, str.str() );
337     mGeometry.SetType( mPrimitiveType );
338     mGeometry.SetIndexBuffer( INDICES[ indicesArray ], INDICES_SIZE[ indicesArray ] );
339     mRenderer.SetIndexRange( 0, mCurrentIndexCount );
340     return true;
341   }
342
343   bool OnButtonClicked( Toolkit::Button button )
344   {
345     if( button == mMinusButton )
346     {
347       if (--mCurrentIndexCount < 2 )
348         mCurrentIndexCount = 2;
349     }
350     else
351     {
352       if (++mCurrentIndexCount > mMaxIndexCount )
353         mCurrentIndexCount = mMaxIndexCount;
354     }
355
356     std::stringstream str;
357     str << mCurrentIndexCount;
358     mIndicesCountLabel.SetProperty( Toolkit::TextLabel::Property::TEXT, str.str() );
359     mRenderer.SetIndexRange( 0, mCurrentIndexCount );
360     return true;
361   }
362
363 private:
364
365   Application&  mApplication;                             ///< Application instance
366   Vector3 mStageSize;                                     ///< The size of the stage
367
368   Shader   mShader;
369   Geometry mGeometry;
370   Renderer mRenderer;
371   Actor    mMeshActor;
372   Toolkit::RadioButton  mButtons[3];
373   Toolkit::PushButton   mMinusButton;
374   Toolkit::PushButton   mPlusButton;
375   Toolkit::TextLabel    mIndicesCountLabel;
376   Geometry::Type mPrimitiveType;
377   int      mCurrentIndexCount;
378   int      mMaxIndexCount;
379 };
380
381 void RunTest( Application& application )
382 {
383   ExampleController test( application );
384
385   application.MainLoop();
386 }
387
388 // Entry point for Linux & SLP applications
389 //
390 int DALI_EXPORT_API main( int argc, char **argv )
391 {
392   Application application = Application::New( &argc, &argv );
393
394   RunTest( application );
395
396   return 0;
397 }