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>
29 const char* MATERIAL_SAMPLE( DALI_IMAGE_DIR "gallery-small-48.jpg" );
30 const char* MATERIAL_SAMPLE2( DALI_IMAGE_DIR "gallery-medium-19.jpg" );
32 #define MAKE_SHADER(A)#A
34 const char* VERTEX_SHADER = MAKE_SHADER(
35 attribute mediump vec2 aPosition1;
36 attribute mediump vec2 aPosition2;
37 uniform mediump mat4 uMvpMatrix;
38 uniform mediump vec3 uSize;
39 uniform mediump float uMorphAmount;
43 mediump vec2 morphPosition = mix(aPosition1, aPosition2, uMorphAmount);
44 mediump vec4 vertexPosition = vec4(morphPosition, 0.0, 1.0);
45 vertexPosition.xyz *= uSize;
46 vertexPosition = uMvpMatrix * vertexPosition;
47 gl_Position = vertexPosition;
51 const char* FRAGMENT_SHADER = MAKE_SHADER(
52 uniform lowp vec4 uColor;
53 uniform sampler2D sTexture;
57 gl_FragColor = uColor;
61 Geometry CreateGeometry()
64 struct Vertex { Vector2 position; };
65 Vertex pentagonVertexData[5] =
67 { Vector2( 0.0f, 1.00f) }, // 0
68 { Vector2( -0.95f, 0.31f) }, // 1
69 { Vector2( -0.59f, -0.81f) }, // 2
70 { Vector2( 0.59f, -0.81f) }, // 3
71 { Vector2( 0.95f, 0.31f) }, // 4
74 Vertex pentacleVertexData[5] =
76 { Vector2( 0.0f, -1.00f) }, //
77 { Vector2( 0.59f, 0.81f) }, //
78 { Vector2( -0.95f, -0.31f) }, //
79 { Vector2( 0.95f, -0.31f) }, //
80 { Vector2( -0.59f, 0.81f) }, //
83 Property::Map pentagonVertexFormat;
84 pentagonVertexFormat["aPosition1"] = Property::VECTOR2;
85 PropertyBuffer pentagonVertices = PropertyBuffer::New( pentagonVertexFormat, 5 );
86 pentagonVertices.SetData(pentagonVertexData);
88 Property::Map pentacleVertexFormat;
89 pentacleVertexFormat["aPosition2"] = Property::VECTOR2;
90 PropertyBuffer pentacleVertices = PropertyBuffer::New( pentacleVertexFormat, 5 );
91 pentacleVertices.SetData(pentacleVertexData);
94 unsigned int indexData[10] = { 0, 1, 1, 2, 2, 3, 3, 4, 4, 0 };
95 Property::Map indexFormat;
96 indexFormat["indices"] = Property::UNSIGNED_INTEGER;
97 PropertyBuffer indices = PropertyBuffer::New( indexFormat, sizeof(indexData)/sizeof(indexData[0]) );
98 indices.SetData(indexData);
100 // Create the geometry object
101 Geometry pentagonGeometry = Geometry::New();
102 pentagonGeometry.AddVertexBuffer( pentagonVertices );
103 pentagonGeometry.AddVertexBuffer( pentacleVertices );
104 pentagonGeometry.SetIndexBuffer( indices );
106 pentagonGeometry.SetGeometryType( Geometry::LINES );
108 return pentagonGeometry;
111 } // anonymous namespace
113 // This example shows how to morph between 2 meshes with the same number of
115 class ExampleController : public ConnectionTracker
120 * The example controller constructor.
121 * @param[in] application The application instance
123 ExampleController( Application& application )
124 : mApplication( application )
126 // Connect to the Application's Init signal
127 mApplication.InitSignal().Connect( this, &ExampleController::Create );
131 * The example controller destructor
135 // Nothing to do here;
139 * Invoked upon creation of application
140 * @param[in] application The application instance
142 void Create( Application& application )
144 Stage stage = Stage::GetCurrent();
145 stage.KeyEventSignal().Connect(this, &ExampleController::OnKeyEvent);
147 mStageSize = stage.GetSize();
149 // The Init signal is received once (only) during the Application lifetime
151 // Hide the indicator bar
152 application.GetWindow().ShowIndicator( Dali::Window::INVISIBLE );
154 mImage = ResourceImage::New( MATERIAL_SAMPLE );
155 mSampler = Sampler::New(mImage, "sTexture");
156 mShader = Shader::New( VERTEX_SHADER, FRAGMENT_SHADER );
158 mMaterial = Material::New( mShader );
159 mMaterial.AddSampler( mSampler );
161 mGeometry = CreateGeometry();
163 mRenderer = Renderer::New( mGeometry, mMaterial );
165 mMeshActor = Actor::New();
166 mMeshActor.AddRenderer( mRenderer );
167 mMeshActor.SetSize(200, 200);
169 Property::Index morphAmountIndex = mMeshActor.RegisterProperty( "uMorphAmount", 0.0f );
171 mRenderer.SetDepthIndex(0);
173 mMeshActor.SetParentOrigin( ParentOrigin::CENTER );
174 mMeshActor.SetAnchorPoint( AnchorPoint::CENTER );
175 stage.Add( mMeshActor );
177 mChangeImageTimer = Timer::New( 5000 );
178 mChangeImageTimer.TickSignal().Connect( this, &ExampleController::OnTimer );
179 mChangeImageTimer.Start();
181 Animation animation = Animation::New(5);
182 KeyFrames keyFrames = KeyFrames::New();
183 keyFrames.Add(0.0f, 0.0f);
184 keyFrames.Add(1.0f, 1.0f);
186 animation.AnimateBetween( Property( mMeshActor, morphAmountIndex ), keyFrames, AlphaFunction(AlphaFunction::SIN) );
187 animation.SetLooping(true);
190 stage.SetBackgroundColor(Vector4(0.0f, 0.2f, 0.2f, 1.0f));;
194 * Invoked whenever the quit button is clicked
195 * @param[in] button the quit button
197 bool OnQuitButtonClicked( Toolkit::Button button )
199 // quit the application
206 Image image = ResourceImage::New( MATERIAL_SAMPLE2 );
207 mSampler.SetImage( image );
211 void OnKeyEvent(const KeyEvent& event)
213 if(event.state == KeyEvent::Down)
215 if( IsKey( event, Dali::DALI_KEY_ESCAPE) || IsKey( event, Dali::DALI_KEY_BACK) )
224 Application& mApplication; ///< Application instance
225 Vector3 mStageSize; ///< The size of the stage
236 Timer mChangeImageTimer;
239 void RunTest( Application& application )
241 ExampleController test( application );
243 application.MainLoop();
246 // Entry point for Linux & SLP applications
248 int main( int argc, char **argv )
250 Application application = Application::New( &argc, &argv );
252 RunTest( application );