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