2 * Copyright (c) 2017 Samsung Electronics Co., Ltd.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 #include <dali-toolkit/dali-toolkit.h>
25 #include "shared/view.h"
26 #include "shared/utility.h"
33 const char* IMAGES[] =
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",
42 const unsigned int NUMBER_OF_SAMPLES(sizeof(IMAGES)/sizeof(const char*));
45 #define MAKE_SHADER(A)#A
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;
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);
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 ) );
70 vTexCoord = aTexCoord;
71 gl_Position = vertexPosition;
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;
83 gl_FragColor = texture2D( sTexture, vTexCoord ) * uColor * vec4(vGlobColor, 1.0) ;
87 } // anonymous namespace
89 // This example shows how to use a simple mesh
91 class ExampleController : public ConnectionTracker
96 * The example controller constructor.
97 * @param[in] application The application instance
99 ExampleController( Application& application )
100 : mApplication( application ),
103 // Connect to the Application's Init signal
104 mApplication.InitSignal().Connect( this, &ExampleController::Create );
105 memset(mDepthIndices, 0, sizeof(mDepthIndices));
109 * The example controller destructor
113 // Nothing to do here;
117 * Invoked upon creation of application
118 * @param[in] application The application instance
120 void Create( Application& application )
122 Stage stage = Stage::GetCurrent();
123 stage.KeyEventSignal().Connect(this, &ExampleController::OnKeyEvent);
125 mStageSize = stage.GetSize();
127 // The Init signal is received once (only) during the Application lifetime
129 // Hide the indicator bar
130 application.GetWindow().ShowIndicator( Dali::Window::INVISIBLE );
132 mShader = Shader::New( VERTEX_SHADER, FRAGMENT_SHADER );
133 mGeometry = DemoHelper::CreateTexturedQuad();
135 TextureSet firstTextureSet;
137 for( unsigned i=0; i<NUMBER_OF_SAMPLES; ++i)
139 Texture texture = DemoHelper::LoadTexture( IMAGES[i] );
140 TextureSet textureSet = TextureSet::New();
141 textureSet.SetTexture( 0u, texture );
142 if( i==0 ) { firstTextureSet = textureSet; }
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);
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 );
158 meshActor.SetOpacity( i%2?0.7f:1.0f );
160 meshActor.RegisterProperty("uHue", i/(float)NUMBER_OF_SAMPLES);
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 );
169 mActors[NUMBER_OF_SAMPLES-2].GetRendererAt(0).SetTextures( firstTextureSet );
171 stage.GetRootLayer().TouchSignal().Connect(this, &ExampleController::OnStageTouched);
180 printf("Children Z ordered back to front\n");
185 printf("All children set to same Z=0\n");
190 printf("Children Z ordered front to back\n");
195 for( unsigned i=0; i<NUMBER_OF_SAMPLES; ++i)
197 printf("DepthIndex[%d]=%d\n", i, mDepthIndices[i]);
202 bool OnTouched( Actor actor, const TouchData& event )
204 if( event.GetState( 0 ) == PointState::UP )
206 int index = actor.GetProperty<int>(actor.GetPropertyIndex("index"));
208 int newDepthIndex = (mDepthIndices[index] + 10) % 30;
209 mDepthIndices[index] = newDepthIndex;
211 Renderer renderer = actor.GetRendererAt(0);
212 renderer.SetProperty( Renderer::Property::DEPTH_INDEX, newDepthIndex);
219 bool OnStageTouched( Actor rootLayer, const TouchData& event )
221 if( event.GetState( 0 ) == PointState::UP )
228 for(unsigned int i=1; i < rootLayer.GetChildCount(); ++i)
230 Actor child = rootLayer.GetChildAt(i);
239 for(unsigned int i=1; i < rootLayer.GetChildCount(); ++i)
241 Actor child = rootLayer.GetChildAt(i);
242 child.SetZ( 100-i*10 );
250 for(unsigned int i=1; i < rootLayer.GetChildCount(); ++i)
252 Actor child = rootLayer.GetChildAt(i);
264 * Invoked whenever the quit button is clicked
265 * @param[in] button the quit button
267 bool OnQuitButtonClicked( Toolkit::Button button )
269 // quit the application
274 void OnKeyEvent(const KeyEvent& event)
276 if(event.state == KeyEvent::Down)
278 if( IsKey( event, Dali::DALI_KEY_ESCAPE) || IsKey( event, Dali::DALI_KEY_BACK) )
287 Application& mApplication; ///< Application instance
288 Vector3 mStageSize; ///< The size of the stage
293 int mDepthIndices[NUMBER_OF_SAMPLES];
294 Actor mActors[NUMBER_OF_SAMPLES];
298 void RunTest( Application& application )
300 ExampleController test( application );
302 application.MainLoop();
305 // Entry point for Linux & SLP applications
307 int DALI_EXPORT_API main( int argc, char **argv )
309 Application application = Application::New( &argc, &argv );
311 RunTest( application );