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.
19 #include <dali/devel-api/rendering/renderer.h>
20 #include <dali-toolkit/dali-toolkit.h>
23 #include "shared/view.h"
30 #define MAKE_SHADER(A)#A
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;
40 varying lowp vec3 vColor;
44 mediump vec2 morphPosition = mix(aPosition1, aPosition2, uMorphAmount);
45 mediump vec4 vertexPosition = vec4(morphPosition, 0.0, 1.0);
47 vertexPosition.xyz *= uSize;
48 vertexPosition = uMvpMatrix * vertexPosition;
49 gl_Position = vertexPosition;
53 const char* FRAGMENT_SHADER = MAKE_SHADER(
54 uniform lowp vec4 uColor;
55 uniform sampler2D sTexture;
57 varying lowp vec3 vColor;
61 gl_FragColor = uColor * vec4( vColor, 1.0 );
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])};
71 Geometry CreateGeometry()
81 // Create new geometry object
82 Vertex pentagonVertexData[5] =
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
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);
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;
107 } // anonymous namespace
109 // This example shows how to morph between 2 meshes with the same number of
111 class ExampleController : public ConnectionTracker
116 * The example controller constructor.
117 * @param[in] application The application instance
119 ExampleController( Application& application )
120 : mApplication( application )
122 // Connect to the Application's Init signal
123 mApplication.InitSignal().Connect( this, &ExampleController::Create );
127 * The example controller destructor
131 // Nothing to do here;
135 * Invoked upon creation of application
136 * @param[in] application The application instance
138 void Create( Application& application )
140 Stage stage = Stage::GetCurrent();
142 CreateRadioButtons();
144 stage.KeyEventSignal().Connect(this, &ExampleController::OnKeyEvent);
146 mStageSize = stage.GetSize();
148 // The Init signal is received once (only) during the Application lifetime
149 ReInitialise( Geometry::LINES );
151 // Hide the indicator bar
152 application.GetWindow().ShowIndicator( Dali::Window::INVISIBLE );
154 stage.SetBackgroundColor(Vector4(0.0f, 0.2f, 0.2f, 1.0f));
158 * Invoked whenever application changes the type of geometry drawn
159 * @param[in] type of geometry
161 void ReInitialise( Geometry::GeometryType geometryType )
163 Stage stage = Stage::GetCurrent();
165 // destroy mesh actor and its resources if already exists
168 stage.Remove( mMeshActor );
172 mShader = Shader::New( VERTEX_SHADER, FRAGMENT_SHADER );
173 mGeometry = CreateGeometry();
174 mRenderer = Renderer::New( mGeometry, mShader );
176 mMeshActor = Actor::New();
177 mMeshActor.AddRenderer( mRenderer );
178 mMeshActor.SetSize(200, 200);
180 Property::Index morphAmountIndex = mMeshActor.RegisterProperty( "uMorphAmount", 0.0f );
182 mRenderer.SetProperty( Renderer::Property::DEPTH_INDEX, 0 );
184 mMeshActor.SetParentOrigin( ParentOrigin::CENTER );
185 mMeshActor.SetAnchorPoint( AnchorPoint::CENTER );
186 stage.Add( mMeshActor );
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);
193 animation.AnimateBetween( Property( mMeshActor, morphAmountIndex ), keyFrames, AlphaFunction(AlphaFunction::SIN) );
194 animation.SetLooping(true);
201 void CreateRadioButtons()
203 Stage stage = Stage::GetCurrent();
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 ));
214 const char* labels[] =
221 for( int i = 0; i < 3; ++i )
223 Property::Map labelMap;
224 labelMap[ "text" ] = labels[i];
225 labelMap[ "textColor" ] = Vector4( 0.8f, 0.8f, 0.8f, 1.0f );
227 Dali::Toolkit::RadioButton radio = Dali::Toolkit::RadioButton::New();
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 );
235 modeSelectTableView.AddChild( radio, Toolkit::TableView::CellPosition( i, 0 ) );
237 stage.Add(modeSelectTableView);
241 * Invoked whenever the quit button is clicked
242 * @param[in] button the quit button
244 bool OnQuitButtonClicked( Toolkit::Button button )
246 // quit the application
251 void OnKeyEvent(const KeyEvent& event)
253 if(event.state == KeyEvent::Down)
255 if( IsKey( event, Dali::DALI_KEY_ESCAPE) || IsKey( event, Dali::DALI_KEY_BACK) )
262 bool OnButtonPressed( Toolkit::Button button )
264 const Geometry::GeometryType geomTypes[] =
272 if( button == mButtons[0] )
276 else if( button == mButtons[1] )
285 mGeometry.SetIndexBuffer( indices[index], indicesSize[index] );
286 mGeometry.SetGeometryType( geomTypes[ index ] );
295 Application& mApplication; ///< Application instance
296 Vector3 mStageSize; ///< The size of the stage
302 Toolkit::RadioButton mButtons[3];
305 void RunTest( Application& application )
307 ExampleController test( application );
309 application.MainLoop();
312 // Entry point for Linux & SLP applications
314 int DALI_EXPORT_API main( int argc, char **argv )
316 Application application = Application::New( &argc, &argv );
318 RunTest( application );