2 * Copyright (c) 2015 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.
21 #include "shared/view.h"
23 #include <dali-toolkit/dali-toolkit.h>
33 const char* MATERIAL_SAMPLES[] =
35 DALI_IMAGE_DIR "people-medium-1.jpg",
36 DALI_IMAGE_DIR "people-medium-4.jpg",
37 DALI_IMAGE_DIR "people-medium-11.jpg",
38 DALI_IMAGE_DIR "people-small-16.jpg",
39 DALI_IMAGE_DIR "people-medium-15.jpg",
40 DALI_IMAGE_DIR "people-medium-6.jpg",
42 const unsigned int NUMBER_OF_SAMPLES(sizeof(MATERIAL_SAMPLES)/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 Geometry CreateGeometry()
90 const float halfQuadSize = .5f;
91 struct TexturedQuadVertex { Vector2 position; Vector2 textureCoordinates; };
92 TexturedQuadVertex texturedQuadVertexData[4] = {
93 { Vector2(-halfQuadSize, -halfQuadSize), Vector2(0.f, 0.f) },
94 { Vector2( halfQuadSize, -halfQuadSize), Vector2(1.f, 0.f) },
95 { Vector2(-halfQuadSize, halfQuadSize), Vector2(0.f, 1.f) },
96 { Vector2( halfQuadSize, halfQuadSize), Vector2(1.f, 1.f) } };
98 Property::Map texturedQuadVertexFormat;
99 texturedQuadVertexFormat["aPosition"] = Property::VECTOR2;
100 texturedQuadVertexFormat["aTexCoord"] = Property::VECTOR2;
101 PropertyBuffer texturedQuadVertices = PropertyBuffer::New( texturedQuadVertexFormat, 4 );
102 texturedQuadVertices.SetData(texturedQuadVertexData);
105 unsigned int indexData[6] = { 0, 3, 1, 0, 2, 3 };
106 Property::Map indexFormat;
107 indexFormat["indices"] = Property::UNSIGNED_INTEGER;
108 PropertyBuffer indices = PropertyBuffer::New( indexFormat, 6 );
109 indices.SetData(indexData);
111 // Create the geometry object
112 Geometry texturedQuadGeometry = Geometry::New();
113 texturedQuadGeometry.AddVertexBuffer( texturedQuadVertices );
114 texturedQuadGeometry.SetIndexBuffer( indices );
116 return texturedQuadGeometry;
119 } // anonymous namespace
121 // This example shows how to use a simple mesh
123 class ExampleController : public ConnectionTracker
128 * The example controller constructor.
129 * @param[in] application The application instance
131 ExampleController( Application& application )
132 : mApplication( application ),
135 // Connect to the Application's Init signal
136 mApplication.InitSignal().Connect( this, &ExampleController::Create );
137 memset(mDepthIndices, 0, sizeof(mDepthIndices));
141 * The example controller destructor
145 // Nothing to do here;
149 * Invoked upon creation of application
150 * @param[in] application The application instance
152 void Create( Application& application )
154 Stage stage = Stage::GetCurrent();
155 stage.KeyEventSignal().Connect(this, &ExampleController::OnKeyEvent);
157 mStageSize = stage.GetSize();
159 // The Init signal is received once (only) during the Application lifetime
161 // Hide the indicator bar
162 application.GetWindow().ShowIndicator( Dali::Window::INVISIBLE );
164 mShader = Shader::New( VERTEX_SHADER, FRAGMENT_SHADER );
165 mGeometry = CreateGeometry();
169 for( unsigned i=0; i<NUMBER_OF_SAMPLES; ++i)
171 Image image = ResourceImage::New( MATERIAL_SAMPLES[i] );
172 Sampler sampler = Sampler::New(image, "sTexture");
173 Material material = Material::New( mShader );
174 material.AddSampler( sampler );
175 if( i==0 ) { firstMat = material; }
177 Renderer renderer = Renderer::New( mGeometry, material );
178 Actor meshActor = Actor::New();
179 mActors[i] = meshActor;
180 meshActor.AddRenderer( renderer );
181 meshActor.SetSize(175, 175);
182 meshActor.RegisterProperty("index", (int)i);
184 renderer.SetDepthIndex(0);
185 // Test with actor alpha
186 meshActor.SetParentOrigin( ParentOrigin::CENTER );
187 meshActor.SetAnchorPoint( AnchorPoint::CENTER );
188 meshActor.SetPosition( 40.0f*(i-(NUMBER_OF_SAMPLES*0.5f)), 40.0f*(i-(NUMBER_OF_SAMPLES*0.5f)), i*10 );
190 meshActor.SetOpacity( i%2?0.7f:1.0f );
192 meshActor.RegisterProperty("uHue", i/(float)NUMBER_OF_SAMPLES);
194 meshActor.TouchedSignal().Connect(this, &ExampleController::OnTouched);
195 std::ostringstream oss;
196 oss << "Mesh Actor " << i;
197 meshActor.SetName(oss.str());
198 stage.Add( meshActor );
201 mActors[NUMBER_OF_SAMPLES-2].GetRendererAt(0).SetMaterial( firstMat );
203 stage.GetRootLayer().TouchedSignal().Connect(this, &ExampleController::OnStageTouched);
212 printf("Children Z ordered back to front\n");
217 printf("All children set to same Z=0\n");
222 printf("Children Z ordered front to back\n");
227 for( unsigned i=0; i<NUMBER_OF_SAMPLES; ++i)
229 printf("DepthIndex[%d]=%d\n", i, mDepthIndices[i]);
234 bool OnTouched( Actor actor, const TouchEvent& event )
236 if( event.GetPoint(0).state == TouchPoint::Finished )
238 int index = actor.GetProperty<int>(actor.GetPropertyIndex("index"));
240 int newDepthIndex = (mDepthIndices[index] + 10) % 30;
241 mDepthIndices[index] = newDepthIndex;
243 Renderer renderer = actor.GetRendererAt(0);
244 renderer.SetDepthIndex(newDepthIndex);
251 bool OnStageTouched( Actor rootLayer, const TouchEvent& event )
253 if( event.GetPoint(0).state == TouchPoint::Finished )
260 for(unsigned int i=1; i < rootLayer.GetChildCount(); ++i)
262 Actor child = rootLayer.GetChildAt(i);
271 for(unsigned int i=1; i < rootLayer.GetChildCount(); ++i)
273 Actor child = rootLayer.GetChildAt(i);
274 child.SetZ( 100-i*10 );
282 for(unsigned int i=1; i < rootLayer.GetChildCount(); ++i)
284 Actor child = rootLayer.GetChildAt(i);
296 * Invoked whenever the quit button is clicked
297 * @param[in] button the quit button
299 bool OnQuitButtonClicked( Toolkit::Button button )
301 // quit the application
306 void OnKeyEvent(const KeyEvent& event)
308 if(event.state == KeyEvent::Down)
310 if( IsKey( event, Dali::DALI_KEY_ESCAPE) || IsKey( event, Dali::DALI_KEY_BACK) )
319 Application& mApplication; ///< Application instance
320 Vector3 mStageSize; ///< The size of the stage
325 int mDepthIndices[NUMBER_OF_SAMPLES];
326 Actor mActors[NUMBER_OF_SAMPLES];
330 void RunTest( Application& application )
332 ExampleController test( application );
334 application.MainLoop();
337 // Entry point for Linux & SLP applications
339 int main( int argc, char **argv )
341 Application application = Application::New( &argc, &argv );
343 RunTest( application );