Changes following "Remove geometry scvene object"
[platform/core/uifw/dali-demo.git] / examples / line-mesh / line-mesh-example.cpp
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 // EXTERNAL INCLUDES
19 #include <dali/devel-api/rendering/renderer.h>
20 #include <dali-toolkit/dali-toolkit.h>
21
22 // INTERNAL INCLUDES
23 #include "shared/view.h"
24
25 using namespace Dali;
26
27 namespace
28 {
29
30 #define MAKE_SHADER(A)#A
31
32 const char* VERTEX_SHADER = MAKE_SHADER(
33 attribute mediump vec2    aPosition1;
34 attribute mediump vec2    aPosition2;
35 attribute lowp    vec3    aColor;
36 uniform   mediump mat4    uMvpMatrix;
37 uniform   mediump vec3    uSize;
38 uniform   mediump float   uMorphAmount;
39
40 varying   lowp    vec3    vColor;
41
42 void main()
43 {
44   mediump vec2 morphPosition = mix(aPosition1, aPosition2, uMorphAmount);
45   mediump vec4 vertexPosition = vec4(morphPosition, 0.0, 1.0);
46   vColor = aColor;
47   vertexPosition.xyz *= uSize;
48   vertexPosition = uMvpMatrix * vertexPosition;
49   gl_Position = vertexPosition;
50 }
51 );
52
53 const char* FRAGMENT_SHADER = MAKE_SHADER(
54 uniform lowp  vec4    uColor;
55 uniform sampler2D     sTexture;
56
57 varying   lowp        vec3 vColor;
58
59 void main()
60 {
61   gl_FragColor = uColor * vec4( vColor, 1.0 );
62 }
63 );
64
65 const unsigned short indexLines[] = { 0, 1, 1, 2, 2, 3, 3, 4, 4, 0 };
66 const unsigned short indexLoop[] =  { 0, 1, 2, 3, 4 };
67 const unsigned short indexStrip[] = { 0, 1, 2, 3, 4, 0 };
68 const unsigned short* indices[3] = { &indexLines[0], &indexLoop[0], &indexStrip[0] };
69 const unsigned int indicesSize[3] = { sizeof(indexLines)/sizeof(indexLines[0]), sizeof(indexLoop)/sizeof(indexLoop[0]), sizeof(indexStrip)/sizeof(indexStrip[0])};
70
71 Geometry CreateGeometry()
72 {
73   // Create vertices
74   struct Vertex
75   {
76     Vector2 position1;
77     Vector2 position2;
78     Vector3 color;
79   };
80
81   // Create new geometry object
82   Vertex pentagonVertexData[5] =
83     {
84       { Vector2(  0.0f,   1.00f),  Vector2(  0.0f,  -1.00f),  Vector3( 1.0f, 1.0f, 1.0f ) }, // 0
85       { Vector2( -0.95f,  0.31f),  Vector2(  0.59f,  0.81f),  Vector3( 1.0f, 0.0f, 0.0f ) }, // 1
86       { Vector2( -0.59f, -0.81f),  Vector2( -0.95f, -0.31f),  Vector3( 0.0f, 1.0f, 0.0f ) }, // 2
87       { Vector2(  0.59f, -0.81f),  Vector2(  0.95f, -0.31f),  Vector3( 0.0f, 0.0f, 1.0f ) }, // 3
88       { Vector2(  0.95f,  0.31f),  Vector2( -0.59f,  0.81f),  Vector3( 1.0f, 1.0f, 0.0f ) }, // 4
89     };
90
91   Property::Map pentagonVertexFormat;
92   pentagonVertexFormat["aPosition1"] = Property::VECTOR2;
93   pentagonVertexFormat["aPosition2"] = Property::VECTOR2;
94   pentagonVertexFormat["aColor"] = Property::VECTOR3;
95   PropertyBuffer pentagonVertices = PropertyBuffer::New( pentagonVertexFormat );
96   pentagonVertices.SetData(pentagonVertexData, 5);
97
98
99   // Create the geometry object
100   Geometry pentagonGeometry = Geometry::New();
101   pentagonGeometry.AddVertexBuffer( pentagonVertices );
102   pentagonGeometry.SetIndexBuffer( indices[0], indicesSize[0] );
103   pentagonGeometry.SetGeometryType( Geometry::LINES );
104   return pentagonGeometry;
105 }
106
107 } // anonymous namespace
108
109 // This example shows how to morph between 2 meshes with the same number of
110 // vertices.
111 class ExampleController : public ConnectionTracker
112 {
113 public:
114
115   /**
116    * The example controller constructor.
117    * @param[in] application The application instance
118    */
119   ExampleController( Application& application )
120   : mApplication( application )
121   {
122     // Connect to the Application's Init signal
123     mApplication.InitSignal().Connect( this, &ExampleController::Create );
124   }
125
126   /**
127    * The example controller destructor
128    */
129   ~ExampleController()
130   {
131     // Nothing to do here;
132   }
133
134   /**
135    * Invoked upon creation of application
136    * @param[in] application The application instance
137    */
138   void Create( Application& application )
139   {
140     Stage stage = Stage::GetCurrent();
141
142     CreateRadioButtons();
143
144     stage.KeyEventSignal().Connect(this, &ExampleController::OnKeyEvent);
145
146     mStageSize = stage.GetSize();
147
148     // The Init signal is received once (only) during the Application lifetime
149     ReInitialise( Geometry::LINES );
150
151     // Hide the indicator bar
152     application.GetWindow().ShowIndicator( Dali::Window::INVISIBLE );
153
154     stage.SetBackgroundColor(Vector4(0.0f, 0.2f, 0.2f, 1.0f));
155   }
156
157   /**
158    * Invoked whenever application changes the type of geometry drawn
159    * @param[in] type of geometry
160    */
161   void ReInitialise( Geometry::GeometryType geometryType )
162   {
163     Stage stage = Stage::GetCurrent();
164
165     // destroy mesh actor and its resources if already exists
166     if( mMeshActor )
167     {
168       stage.Remove( mMeshActor );
169       mMeshActor.Reset();
170     }
171
172     mShader = Shader::New( VERTEX_SHADER, FRAGMENT_SHADER );
173     mGeometry = CreateGeometry();
174     mRenderer = Renderer::New( mGeometry, mShader );
175
176     mMeshActor = Actor::New();
177     mMeshActor.AddRenderer( mRenderer );
178     mMeshActor.SetSize(200, 200);
179
180     Property::Index morphAmountIndex = mMeshActor.RegisterProperty( "uMorphAmount", 0.0f );
181
182     mRenderer.SetProperty( Renderer::Property::DEPTH_INDEX, 0 );
183
184     mMeshActor.SetParentOrigin( ParentOrigin::CENTER );
185     mMeshActor.SetAnchorPoint( AnchorPoint::CENTER );
186     stage.Add( mMeshActor );
187
188     Animation  animation = Animation::New(5);
189     KeyFrames keyFrames = KeyFrames::New();
190     keyFrames.Add(0.0f, 0.0f);
191     keyFrames.Add(1.0f, 1.0f);
192
193     animation.AnimateBetween( Property( mMeshActor, morphAmountIndex ), keyFrames, AlphaFunction(AlphaFunction::SIN) );
194     animation.SetLooping(true);
195     animation.Play();
196   }
197
198   /**
199    * Invoked on create
200    */
201   void CreateRadioButtons()
202   {
203     Stage stage = Stage::GetCurrent();
204
205     Toolkit::TableView modeSelectTableView = Toolkit::TableView::New( 3, 1 );
206     modeSelectTableView.SetParentOrigin( ParentOrigin::TOP_LEFT );
207     modeSelectTableView.SetAnchorPoint( AnchorPoint::TOP_LEFT );
208     modeSelectTableView.SetFitHeight( 0 );
209     modeSelectTableView.SetFitHeight( 1 );
210     modeSelectTableView.SetFitHeight( 2 );
211     modeSelectTableView.SetCellPadding( Vector2( 6.0f, 0.0f ) );
212     modeSelectTableView.SetScale( Vector3( 0.5f, 0.5f, 0.5f ));
213
214     const char* labels[] =
215     {
216       "LINES",
217       "LINE_LOOP",
218       "LINE_STRIP"
219     };
220
221     for( int i = 0; i < 3; ++i )
222     {
223       Property::Map labelMap;
224       labelMap[ "text" ]      = labels[i];
225       labelMap[ "textColor" ] = Vector4( 0.8f, 0.8f, 0.8f, 1.0f );
226
227       Dali::Toolkit::RadioButton radio = Dali::Toolkit::RadioButton::New();
228
229       radio.SetProperty( Dali::Toolkit::RadioButton::Property::LABEL, labelMap );
230       radio.SetParentOrigin( ParentOrigin::TOP_LEFT );
231       radio.SetAnchorPoint( AnchorPoint::TOP_LEFT );
232       radio.SetSelected( i == 0 );
233       radio.PressedSignal().Connect( this, &ExampleController::OnButtonPressed );
234       mButtons[i] = radio;
235       modeSelectTableView.AddChild( radio, Toolkit::TableView::CellPosition( i,  0 ) );
236     }
237     stage.Add(modeSelectTableView);
238   }
239
240   /**
241    * Invoked whenever the quit button is clicked
242    * @param[in] button the quit button
243    */
244   bool OnQuitButtonClicked( Toolkit::Button button )
245   {
246     // quit the application
247     mApplication.Quit();
248     return true;
249   }
250
251   void OnKeyEvent(const KeyEvent& event)
252   {
253     if(event.state == KeyEvent::Down)
254     {
255       if( IsKey( event, Dali::DALI_KEY_ESCAPE) || IsKey( event, Dali::DALI_KEY_BACK) )
256       {
257         mApplication.Quit();
258       }
259     }
260   }
261
262   bool OnButtonPressed( Toolkit::Button button )
263   {
264     const Geometry::GeometryType geomTypes[] =
265     {
266       Geometry::LINES,
267       Geometry::LINE_LOOP,
268       Geometry::LINE_STRIP
269     };
270
271     size_t index;
272     if( button == mButtons[0] )
273     {
274       index = 0;
275     }
276     else if( button == mButtons[1] )
277     {
278       index = 1;
279     }
280     else
281     {
282       index = 2;
283     }
284
285     mGeometry.SetIndexBuffer( indices[index], indicesSize[index] );
286     mGeometry.SetGeometryType( geomTypes[ index ] );
287
288     return true;
289   }
290
291
292
293 private:
294
295   Application&  mApplication;                             ///< Application instance
296   Vector3 mStageSize;                                     ///< The size of the stage
297
298   Shader   mShader;
299   Geometry mGeometry;
300   Renderer mRenderer;
301   Actor    mMeshActor;
302   Toolkit::RadioButton mButtons[3];
303 };
304
305 void RunTest( Application& application )
306 {
307   ExampleController test( application );
308
309   application.MainLoop();
310 }
311
312 // Entry point for Linux & SLP applications
313 //
314 int DALI_EXPORT_API main( int argc, char **argv )
315 {
316   Application application = Application::New( &argc, &argv );
317
318   RunTest( application );
319
320   return 0;
321 }