Using migrated Public Visual API
[platform/core/uifw/dali-demo.git] / examples / mesh-sorting / mesh-sorting-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 #include <stdio.h>
21 #include <sstream>
22 #include <cstring>
23
24 // INTERNAL INCLUDES
25 #include "shared/view.h"
26 #include "shared/utility.h"
27
28 using namespace Dali;
29
30 namespace
31 {
32
33 const char* IMAGES[] =
34 {
35   DEMO_IMAGE_DIR "people-medium-1.jpg",
36   DEMO_IMAGE_DIR "people-medium-4.jpg",
37   DEMO_IMAGE_DIR "people-medium-11.jpg",
38   DEMO_IMAGE_DIR "people-small-16.jpg",
39   DEMO_IMAGE_DIR "people-medium-15.jpg",
40   DEMO_IMAGE_DIR "people-medium-6.jpg",
41 };
42 const unsigned int NUMBER_OF_SAMPLES(sizeof(IMAGES)/sizeof(const char*));
43
44
45 #define MAKE_SHADER(A)#A
46
47 const char* VERTEX_SHADER = MAKE_SHADER(
48 uniform   highp   float   uHue;
49 attribute mediump vec2    aPosition;
50 attribute highp   vec2    aTexCoord;
51 varying   mediump vec2    vTexCoord;
52 uniform   mediump mat4    uMvpMatrix;
53 uniform   mediump vec3    uSize;
54 varying   mediump vec3    vGlobColor;
55
56 vec3 hsv2rgb(vec3 c)
57 {
58   vec4 K = vec4(1.0, 2.0 / 3.0, 1.0 / 3.0, 3.0);
59   vec3 p = abs(fract(c.xxx + K.xyz) * 6.0 - K.www);
60   return c.z * mix(K.xxx, clamp(p - K.xxx, 0.0, 1.0), c.y);
61 }
62
63 void main()
64 {
65   mediump vec4 vertexPosition = vec4(aPosition, 0.0, 1.0);
66   vertexPosition.xyz *= uSize;
67   vertexPosition = uMvpMatrix * vertexPosition;
68   vGlobColor = hsv2rgb( vec3( clamp(uHue, 0.0, 1.0), 1.0, 1.0 ) );
69
70   vTexCoord = aTexCoord;
71   gl_Position = vertexPosition;
72 }
73 );
74
75 const char* FRAGMENT_SHADER = MAKE_SHADER(
76 varying mediump vec2  vTexCoord;
77 varying mediump vec3  vGlobColor;
78 uniform lowp    vec4  uColor;
79 uniform sampler2D     sTexture;
80
81 void main()
82 {
83   gl_FragColor = texture2D( sTexture, vTexCoord ) * uColor * vec4(vGlobColor, 1.0) ;
84 }
85 );
86
87 } // anonymous namespace
88
89 // This example shows how to use a simple mesh
90 //
91 class ExampleController : public ConnectionTracker
92 {
93 public:
94
95   /**
96    * The example controller constructor.
97    * @param[in] application The application instance
98    */
99   ExampleController( Application& application )
100   : mApplication( application ),
101     mZMode(0)
102   {
103     // Connect to the Application's Init signal
104     mApplication.InitSignal().Connect( this, &ExampleController::Create );
105     memset(mDepthIndices, 0, sizeof(mDepthIndices));
106   }
107
108   /**
109    * The example controller destructor
110    */
111   ~ExampleController()
112   {
113     // Nothing to do here;
114   }
115
116   /**
117    * Invoked upon creation of application
118    * @param[in] application The application instance
119    */
120   void Create( Application& application )
121   {
122     Stage stage = Stage::GetCurrent();
123     stage.KeyEventSignal().Connect(this, &ExampleController::OnKeyEvent);
124
125     mStageSize = stage.GetSize();
126
127     // The Init signal is received once (only) during the Application lifetime
128
129     // Hide the indicator bar
130     application.GetWindow().ShowIndicator( Dali::Window::INVISIBLE );
131
132     mShader = Shader::New( VERTEX_SHADER, FRAGMENT_SHADER );
133     mGeometry = DemoHelper::CreateTexturedQuad();
134
135     TextureSet firstTextureSet;
136
137     for( unsigned i=0; i<NUMBER_OF_SAMPLES; ++i)
138     {
139       Texture texture = DemoHelper::LoadTexture( IMAGES[i] );
140       TextureSet textureSet = TextureSet::New();
141       textureSet.SetTexture( 0u, texture );
142       if( i==0 ) { firstTextureSet = textureSet; }
143
144       Renderer renderer = Renderer::New( mGeometry, mShader );
145       renderer.SetTextures( textureSet );
146       Actor meshActor = Actor::New();
147       mActors[i] = meshActor;
148       meshActor.AddRenderer( renderer );
149       meshActor.SetSize(175, 175);
150       meshActor.RegisterProperty("index", (int)i);
151
152       renderer.SetProperty( Renderer::Property::DEPTH_INDEX, 0 );
153       // Test with actor alpha
154       meshActor.SetParentOrigin( ParentOrigin::CENTER );
155       meshActor.SetAnchorPoint( AnchorPoint::CENTER );
156       meshActor.SetPosition( 40.0f*(i-(NUMBER_OF_SAMPLES*0.5f)), 40.0f*(i-(NUMBER_OF_SAMPLES*0.5f)), i*10 );
157
158       meshActor.SetOpacity( i%2?0.7f:1.0f );
159
160       meshActor.RegisterProperty("uHue", i/(float)NUMBER_OF_SAMPLES);
161
162       meshActor.TouchSignal().Connect(this, &ExampleController::OnTouched);
163       std::ostringstream oss;
164       oss << "Mesh Actor " << i;
165       meshActor.SetName(oss.str());
166       stage.Add( meshActor );
167     }
168
169     mActors[NUMBER_OF_SAMPLES-2].GetRendererAt(0).SetTextures( firstTextureSet );
170
171     stage.GetRootLayer().TouchSignal().Connect(this, &ExampleController::OnStageTouched);
172   }
173
174   void PrintDepths()
175   {
176     switch( mZMode )
177     {
178       case 0:
179       {
180         printf("Children Z ordered back to front\n");
181         break;
182       }
183       case 1:
184       {
185         printf("All children set to same Z=0\n");
186         break;
187       }
188       case 2:
189       {
190         printf("Children Z ordered front to back\n");
191         break;
192       }
193     }
194
195     for( unsigned i=0; i<NUMBER_OF_SAMPLES; ++i)
196     {
197       printf("DepthIndex[%d]=%d\n", i, mDepthIndices[i]);
198     }
199     printf("\n");
200   }
201
202   bool OnTouched( Actor actor, const TouchData& event )
203   {
204     if( event.GetState( 0 ) == PointState::UP )
205     {
206       int index = actor.GetProperty<int>(actor.GetPropertyIndex("index"));
207
208       int newDepthIndex = (mDepthIndices[index] + 10) % 30;
209       mDepthIndices[index] = newDepthIndex;
210
211       Renderer renderer = actor.GetRendererAt(0);
212       renderer.SetProperty( Renderer::Property::DEPTH_INDEX, newDepthIndex);
213
214       PrintDepths();
215     }
216     return true;
217   }
218
219   bool OnStageTouched( Actor rootLayer, const TouchData& event )
220   {
221     if( event.GetState( 0 ) == PointState::UP )
222     {
223       switch( mZMode )
224       {
225         case 0:
226         {
227           mZMode = 1;
228           for(unsigned int i=1; i < rootLayer.GetChildCount(); ++i)
229           {
230             Actor child = rootLayer.GetChildAt(i);
231             child.SetZ( 0.0f );
232           }
233           PrintDepths();
234           break;
235         }
236         case 1:
237         {
238           mZMode = 2;
239           for(unsigned int i=1; i < rootLayer.GetChildCount(); ++i)
240           {
241             Actor child = rootLayer.GetChildAt(i);
242             child.SetZ( 100-i*10 );
243           }
244           PrintDepths();
245           break;
246         }
247         case 2:
248         {
249           mZMode = 0;
250           for(unsigned int i=1; i < rootLayer.GetChildCount(); ++i)
251           {
252             Actor child = rootLayer.GetChildAt(i);
253             child.SetZ( i*10 );
254           }
255           PrintDepths();
256           break;
257         }
258       }
259     }
260     return true;
261   }
262
263   /**
264    * Invoked whenever the quit button is clicked
265    * @param[in] button the quit button
266    */
267   bool OnQuitButtonClicked( Toolkit::Button button )
268   {
269     // quit the application
270     mApplication.Quit();
271     return true;
272   }
273
274   void OnKeyEvent(const KeyEvent& event)
275   {
276     if(event.state == KeyEvent::Down)
277     {
278       if( IsKey( event, Dali::DALI_KEY_ESCAPE) || IsKey( event, Dali::DALI_KEY_BACK) )
279       {
280         mApplication.Quit();
281       }
282     }
283   }
284
285 private:
286
287   Application&  mApplication;                             ///< Application instance
288   Vector3 mStageSize;                                     ///< The size of the stage
289
290   Shader   mShader;
291   Geometry mGeometry;
292
293   int mDepthIndices[NUMBER_OF_SAMPLES];
294   Actor mActors[NUMBER_OF_SAMPLES];
295   int mZMode;
296 };
297
298 void RunTest( Application& application )
299 {
300   ExampleController test( application );
301
302   application.MainLoop();
303 }
304
305 // Entry point for Linux & SLP applications
306 //
307 int DALI_EXPORT_API main( int argc, char **argv )
308 {
309   Application application = Application::New( &argc, &argv );
310
311   RunTest( application );
312
313   return 0;
314 }