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 PropertyBuffer CreateIndexBuffer( Geometry::GeometryType geometryType )
68 const unsigned int indexDataLines[] = { 0, 1, 1, 2, 2, 3, 3, 4, 4, 0 };
69 const unsigned int indexDataLoops[] = { 0, 1, 2, 3, 4 };
70 const unsigned int indexDataStrips[] = { 0, 1, 2, 3, 4, 0 };
72 // Create index buffer if doesn't exist
73 Property::Map indexFormat;
74 indexFormat["indices"] = Property::INTEGER;
75 PropertyBuffer indices = PropertyBuffer::New( indexFormat );
78 switch( geometryType )
82 indices.SetData( indexDataLines, sizeof(indexDataLines)/sizeof(indexDataLines[0]) );
85 case Geometry::LINE_LOOP:
87 indices.SetData( indexDataLoops, sizeof(indexDataLoops)/sizeof(indexDataLoops[0]) );
90 case Geometry::LINE_STRIP:
92 indices.SetData( indexDataStrips, sizeof(indexDataStrips)/sizeof(indexDataStrips[0]) );
95 default: // this will never happen, but compilers yells
103 Geometry CreateGeometry()
113 // Create new geometry object
114 Vertex pentagonVertexData[5] =
116 { Vector2( 0.0f, 1.00f), Vector2( 0.0f, -1.00f), Vector3( 1.0f, 1.0f, 1.0f ) }, // 0
117 { Vector2( -0.95f, 0.31f), Vector2( 0.59f, 0.81f), Vector3( 1.0f, 0.0f, 0.0f ) }, // 1
118 { Vector2( -0.59f, -0.81f), Vector2( -0.95f, -0.31f), Vector3( 0.0f, 1.0f, 0.0f ) }, // 2
119 { Vector2( 0.59f, -0.81f), Vector2( 0.95f, -0.31f), Vector3( 0.0f, 0.0f, 1.0f ) }, // 3
120 { Vector2( 0.95f, 0.31f), Vector2( -0.59f, 0.81f), Vector3( 1.0f, 1.0f, 0.0f ) }, // 4
123 Property::Map pentagonVertexFormat;
124 pentagonVertexFormat["aPosition1"] = Property::VECTOR2;
125 pentagonVertexFormat["aPosition2"] = Property::VECTOR2;
126 pentagonVertexFormat["aColor"] = Property::VECTOR3;
127 PropertyBuffer pentagonVertices = PropertyBuffer::New( pentagonVertexFormat );
128 pentagonVertices.SetData(pentagonVertexData, 5);
131 PropertyBuffer indices = CreateIndexBuffer( Geometry::LINES );
133 // Create the geometry object
134 Geometry pentagonGeometry = Geometry::New();
135 pentagonGeometry.AddVertexBuffer( pentagonVertices );
136 pentagonGeometry.SetIndexBuffer( indices );
137 pentagonGeometry.SetGeometryType( Geometry::LINES );
138 return pentagonGeometry;
141 } // anonymous namespace
143 // This example shows how to morph between 2 meshes with the same number of
145 class ExampleController : public ConnectionTracker
150 * The example controller constructor.
151 * @param[in] application The application instance
153 ExampleController( Application& application )
154 : mApplication( application )
156 // Connect to the Application's Init signal
157 mApplication.InitSignal().Connect( this, &ExampleController::Create );
161 * The example controller destructor
165 // Nothing to do here;
169 * Invoked upon creation of application
170 * @param[in] application The application instance
172 void Create( Application& application )
174 Stage stage = Stage::GetCurrent();
176 CreateRadioButtons();
178 stage.KeyEventSignal().Connect(this, &ExampleController::OnKeyEvent);
180 mStageSize = stage.GetSize();
182 // The Init signal is received once (only) during the Application lifetime
183 ReInitialise( Geometry::LINES );
185 // Hide the indicator bar
186 application.GetWindow().ShowIndicator( Dali::Window::INVISIBLE );
188 stage.SetBackgroundColor(Vector4(0.0f, 0.2f, 0.2f, 1.0f));
192 * Invoked whenever application changes the type of geometry drawn
193 * @param[in] type of geometry
195 void ReInitialise( Geometry::GeometryType geometryType )
197 Stage stage = Stage::GetCurrent();
199 // destroy mesh actor and its resources if already exists
202 stage.Remove( mMeshActor );
206 mShader = Shader::New( VERTEX_SHADER, FRAGMENT_SHADER );
207 mGeometry = CreateGeometry();
208 mRenderer = Renderer::New( mGeometry, mShader );
210 mMeshActor = Actor::New();
211 mMeshActor.AddRenderer( mRenderer );
212 mMeshActor.SetSize(200, 200);
214 Property::Index morphAmountIndex = mMeshActor.RegisterProperty( "uMorphAmount", 0.0f );
216 mRenderer.SetProperty( Renderer::Property::DEPTH_INDEX, 0 );
218 mMeshActor.SetParentOrigin( ParentOrigin::CENTER );
219 mMeshActor.SetAnchorPoint( AnchorPoint::CENTER );
220 stage.Add( mMeshActor );
222 Animation animation = Animation::New(5);
223 KeyFrames keyFrames = KeyFrames::New();
224 keyFrames.Add(0.0f, 0.0f);
225 keyFrames.Add(1.0f, 1.0f);
227 animation.AnimateBetween( Property( mMeshActor, morphAmountIndex ), keyFrames, AlphaFunction(AlphaFunction::SIN) );
228 animation.SetLooping(true);
235 void CreateRadioButtons()
237 Stage stage = Stage::GetCurrent();
239 Toolkit::TableView modeSelectTableView = Toolkit::TableView::New( 3, 1 );
240 modeSelectTableView.SetParentOrigin( ParentOrigin::TOP_LEFT );
241 modeSelectTableView.SetAnchorPoint( AnchorPoint::TOP_LEFT );
242 modeSelectTableView.SetFitHeight( 0 );
243 modeSelectTableView.SetFitHeight( 1 );
244 modeSelectTableView.SetFitHeight( 2 );
245 modeSelectTableView.SetCellPadding( Vector2( 6.0f, 0.0f ) );
246 modeSelectTableView.SetScale( Vector3( 0.5f, 0.5f, 0.5f ));
248 const char* labels[] =
255 for( int i = 0; i < 3; ++i )
257 Property::Map labelMap;
258 labelMap[ "text" ] = labels[i];
259 labelMap[ "textColor" ] = Vector4( 0.8f, 0.8f, 0.8f, 1.0f );
261 Dali::Toolkit::RadioButton radio = Dali::Toolkit::RadioButton::New();
263 radio.SetProperty( Dali::Toolkit::RadioButton::Property::LABEL, labelMap );
264 radio.SetParentOrigin( ParentOrigin::TOP_LEFT );
265 radio.SetAnchorPoint( AnchorPoint::TOP_LEFT );
266 radio.SetSelected( i == 0 );
267 radio.PressedSignal().Connect( this, &ExampleController::OnButtonPressed );
269 modeSelectTableView.AddChild( radio, Toolkit::TableView::CellPosition( i, 0 ) );
271 stage.Add(modeSelectTableView);
275 * Invoked whenever the quit button is clicked
276 * @param[in] button the quit button
278 bool OnQuitButtonClicked( Toolkit::Button button )
280 // quit the application
285 void OnKeyEvent(const KeyEvent& event)
287 if(event.state == KeyEvent::Down)
289 if( IsKey( event, Dali::DALI_KEY_ESCAPE) || IsKey( event, Dali::DALI_KEY_BACK) )
296 bool OnButtonPressed( Toolkit::Button button )
298 const Geometry::GeometryType geomTypes[] =
306 if( button == mButtons[0] )
310 else if( button == mButtons[1] )
319 PropertyBuffer indices = CreateIndexBuffer( geomTypes[ index ] );
320 mGeometry.SetIndexBuffer( indices );
321 mGeometry.SetGeometryType( geomTypes[ index ] );
330 Application& mApplication; ///< Application instance
331 Vector3 mStageSize; ///< The size of the stage
337 Toolkit::RadioButton mButtons[3];
340 void RunTest( Application& application )
342 ExampleController test( application );
344 application.MainLoop();
347 // Entry point for Linux & SLP applications
349 int main( int argc, char **argv )
351 Application application = Application::New( &argc, &argv );
353 RunTest( application );