2 * Copyright (c) 2016 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/public-api/rendering/renderer.h>
20 #include <dali-toolkit/dali-toolkit.h>
23 #include "shared/view.h"
32 #define MAKE_SHADER(A)#A
34 const char* VERTEX_SHADER = MAKE_SHADER(
35 attribute mediump vec2 aPosition1;
36 attribute mediump vec2 aPosition2;
37 attribute lowp vec3 aColor;
38 uniform mediump mat4 uMvpMatrix;
39 uniform mediump vec3 uSize;
40 uniform mediump float uMorphAmount;
42 varying lowp vec3 vColor;
46 mediump vec2 morphPosition = mix(aPosition1, aPosition2, uMorphAmount);
47 mediump vec4 vertexPosition = vec4(morphPosition, 0.0, 1.0);
49 vertexPosition.xyz *= uSize;
50 vertexPosition = uMvpMatrix * vertexPosition;
51 gl_Position = vertexPosition;
55 const char* FRAGMENT_SHADER = MAKE_SHADER(
56 uniform lowp vec4 uColor;
57 uniform sampler2D sTexture;
59 varying lowp vec3 vColor;
63 gl_FragColor = uColor * vec4( vColor, 1.0 );
67 const unsigned short INDEX_LINES[] = { 0, 1, 1, 2, 2, 3, 3, 4, 4, 0 };
68 const unsigned short INDEX_LOOP[] = { 0, 1, 2, 3, 4 };
69 const unsigned short INDEX_STRIP[] = { 0, 1, 2, 3, 4, 0 };
70 const unsigned short* INDICES[3] = { &INDEX_LINES[0], &INDEX_LOOP[0], &INDEX_STRIP[0] };
71 const unsigned int INDICES_SIZE[3] = { sizeof(INDEX_LINES)/sizeof(INDEX_LINES[0]), sizeof(INDEX_LOOP)/sizeof(INDEX_LOOP[0]), sizeof(INDEX_STRIP)/sizeof(INDEX_STRIP[0])};
73 Geometry CreateGeometry()
83 // Create new geometry object
84 Vertex pentagonVertexData[5] =
86 { Vector2( 0.0f, 1.00f), Vector2( 0.0f, -1.00f), Vector3( 1.0f, 1.0f, 1.0f ) }, // 0
87 { Vector2( -0.95f, 0.31f), Vector2( 0.59f, 0.81f), Vector3( 1.0f, 0.0f, 0.0f ) }, // 1
88 { Vector2( -0.59f, -0.81f), Vector2( -0.95f, -0.31f), Vector3( 0.0f, 1.0f, 0.0f ) }, // 2
89 { Vector2( 0.59f, -0.81f), Vector2( 0.95f, -0.31f), Vector3( 0.0f, 0.0f, 1.0f ) }, // 3
90 { Vector2( 0.95f, 0.31f), Vector2( -0.59f, 0.81f), Vector3( 1.0f, 1.0f, 0.0f ) }, // 4
93 Property::Map pentagonVertexFormat;
94 pentagonVertexFormat["aPosition1"] = Property::VECTOR2;
95 pentagonVertexFormat["aPosition2"] = Property::VECTOR2;
96 pentagonVertexFormat["aColor"] = Property::VECTOR3;
97 PropertyBuffer pentagonVertices = PropertyBuffer::New( pentagonVertexFormat );
98 pentagonVertices.SetData(pentagonVertexData, 5);
101 // Create the geometry object
102 Geometry pentagonGeometry = Geometry::New();
103 pentagonGeometry.AddVertexBuffer( pentagonVertices );
104 pentagonGeometry.SetIndexBuffer( INDICES[0], INDICES_SIZE[0] );
105 pentagonGeometry.SetType( Geometry::LINES );
106 return pentagonGeometry;
109 } // anonymous namespace
111 // This example shows how to morph between 2 meshes with the same number of
113 class ExampleController : public ConnectionTracker
118 * The example controller constructor.
119 * @param[in] application The application instance
121 ExampleController( Application& application )
122 : mApplication( application ),
131 mIndicesCountLabel(),
132 mPrimitiveType( Geometry::LINES ),
133 mCurrentIndexCount( 0 ),
136 // Connect to the Application's Init signal
137 mApplication.InitSignal().Connect( this, &ExampleController::Create );
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();
157 mPrimitiveType = Geometry::LINES;
158 mCurrentIndexCount = 10;
161 CreateRadioButtons();
163 stage.KeyEventSignal().Connect(this, &ExampleController::OnKeyEvent);
165 mStageSize = stage.GetSize();
169 // Hide the indicator bar
170 application.GetWindow().ShowIndicator( Dali::Window::INVISIBLE );
172 stage.SetBackgroundColor(Vector4(0.0f, 0.2f, 0.2f, 1.0f));
176 * Invoked whenever application changes the type of geometry drawn
180 Stage stage = Stage::GetCurrent();
182 // destroy mesh actor and its resources if already exists
185 stage.Remove( mMeshActor );
189 mShader = Shader::New( VERTEX_SHADER, FRAGMENT_SHADER );
190 mGeometry = CreateGeometry();
191 mRenderer = Renderer::New( mGeometry, mShader );
193 mRenderer.SetIndexRange( 0, 10 ); // lines
194 mPrimitiveType = Geometry::LINES;
196 mMeshActor = Actor::New();
197 mMeshActor.AddRenderer( mRenderer );
198 mMeshActor.SetSize(200, 200);
200 Property::Index morphAmountIndex = mMeshActor.RegisterProperty( "uMorphAmount", 0.0f );
202 mRenderer.SetProperty( Renderer::Property::DEPTH_INDEX, 0 );
204 mMeshActor.SetParentOrigin( ParentOrigin::CENTER );
205 mMeshActor.SetAnchorPoint( AnchorPoint::CENTER );
206 stage.Add( mMeshActor );
208 Animation animation = Animation::New(5);
209 KeyFrames keyFrames = KeyFrames::New();
210 keyFrames.Add(0.0f, 0.0f);
211 keyFrames.Add(1.0f, 1.0f);
213 animation.AnimateBetween( Property( mMeshActor, morphAmountIndex ), keyFrames, AlphaFunction(AlphaFunction::SIN) );
214 animation.SetLooping(true);
221 void CreateRadioButtons()
223 Stage stage = Stage::GetCurrent();
225 Toolkit::TableView modeSelectTableView = Toolkit::TableView::New( 4, 1 );
226 modeSelectTableView.SetParentOrigin( ParentOrigin::TOP_LEFT );
227 modeSelectTableView.SetAnchorPoint( AnchorPoint::TOP_LEFT );
228 modeSelectTableView.SetFitHeight( 0 );
229 modeSelectTableView.SetFitHeight( 1 );
230 modeSelectTableView.SetFitHeight( 2 );
231 modeSelectTableView.SetCellPadding( Vector2( 6.0f, 0.0f ) );
232 modeSelectTableView.SetScale( Vector3( 0.8f, 0.8f, 0.8f ));
234 const char* labels[] =
241 for( int i = 0; i < 3; ++i )
243 Property::Map labelMap;
244 labelMap[ "text" ] = labels[i];
245 labelMap[ "textColor" ] = Vector4( 0.8f, 0.8f, 0.8f, 1.0f );
247 Dali::Toolkit::RadioButton radio = Dali::Toolkit::RadioButton::New();
249 radio.SetProperty( Dali::Toolkit::Button::Property::LABEL, labelMap );
250 radio.SetParentOrigin( ParentOrigin::TOP_LEFT );
251 radio.SetAnchorPoint( AnchorPoint::TOP_LEFT );
252 radio.SetSelected( i == 0 );
253 radio.PressedSignal().Connect( this, &ExampleController::OnButtonPressed );
255 modeSelectTableView.AddChild( radio, Toolkit::TableView::CellPosition( i, 0 ) );
258 Toolkit::TableView elementCountTableView = Toolkit::TableView::New( 1, 3 );
259 elementCountTableView.SetCellPadding( Vector2( 6.0f, 0.0f ) );
260 elementCountTableView.SetParentOrigin( ParentOrigin::BOTTOM_LEFT );
261 elementCountTableView.SetAnchorPoint( AnchorPoint::BOTTOM_LEFT );
262 elementCountTableView.SetFitHeight( 0 );
263 elementCountTableView.SetFitWidth( 0 );
264 elementCountTableView.SetFitWidth( 1 );
265 elementCountTableView.SetFitWidth( 2 );
266 mMinusButton = Toolkit::PushButton::New();
267 mMinusButton.SetLabelText( "<<" );
268 mMinusButton.SetParentOrigin( ParentOrigin::TOP_LEFT );
269 mMinusButton.SetAnchorPoint( AnchorPoint::CENTER_LEFT );
271 Toolkit::PushButton mPlusButton = Toolkit::PushButton::New();
272 mPlusButton.SetLabelText( ">>" );
273 mPlusButton.SetParentOrigin( ParentOrigin::TOP_LEFT );
274 mPlusButton.SetAnchorPoint( AnchorPoint::CENTER_RIGHT );
276 mMinusButton.ClickedSignal().Connect( this, &ExampleController::OnButtonClicked );
277 mPlusButton.ClickedSignal().Connect( this, &ExampleController::OnButtonClicked );
279 mIndicesCountLabel = Toolkit::TextLabel::New();
280 mIndicesCountLabel.SetParentOrigin( ParentOrigin::CENTER );
281 mIndicesCountLabel.SetAnchorPoint( AnchorPoint::TOP_LEFT );
283 std::stringstream str;
284 str << mCurrentIndexCount;
285 mIndicesCountLabel.SetProperty( Toolkit::TextLabel::Property::TEXT, str.str() );
286 mIndicesCountLabel.SetProperty( Toolkit::TextLabel::Property::TEXT_COLOR, Vector4( 1.0, 1.0, 1.0, 1.0 ) );
287 mIndicesCountLabel.SetProperty( Toolkit::TextLabel::Property::VERTICAL_ALIGNMENT, "BOTTOM");
288 mIndicesCountLabel.SetResizePolicy( ResizePolicy::USE_NATURAL_SIZE, Dimension::WIDTH );
289 mIndicesCountLabel.SetResizePolicy( ResizePolicy::USE_NATURAL_SIZE, Dimension::HEIGHT );
291 elementCountTableView.AddChild( mMinusButton, Toolkit::TableView::CellPosition( 0, 0 ) );
292 elementCountTableView.AddChild( mIndicesCountLabel, Toolkit::TableView::CellPosition( 0, 1 ) );
293 elementCountTableView.AddChild( mPlusButton, Toolkit::TableView::CellPosition( 0, 2 ) );
295 stage.Add(modeSelectTableView);
296 stage.Add(elementCountTableView);
300 * Invoked whenever the quit button is clicked
301 * @param[in] button the quit button
303 bool OnQuitButtonClicked( Toolkit::Button button )
305 // quit the application
310 void OnKeyEvent(const KeyEvent& event)
312 if(event.state == KeyEvent::Down)
314 if( IsKey( event, Dali::DALI_KEY_ESCAPE) || IsKey( event, Dali::DALI_KEY_BACK) )
321 bool OnButtonPressed( Toolkit::Button button )
324 if( button == mButtons[0] )
326 mCurrentIndexCount = 10;
328 mPrimitiveType = Geometry::LINES;
331 else if( button == mButtons[1] )
333 mCurrentIndexCount = 5;
335 mPrimitiveType = Geometry::LINE_LOOP;
340 mCurrentIndexCount = 6;
342 mPrimitiveType = Geometry::LINE_STRIP;
346 std::stringstream str;
347 str << mCurrentIndexCount;
348 mIndicesCountLabel.SetProperty( Toolkit::TextLabel::Property::TEXT, str.str() );
349 mGeometry.SetType( mPrimitiveType );
350 mGeometry.SetIndexBuffer( INDICES[ indicesArray ], INDICES_SIZE[ indicesArray ] );
351 mRenderer.SetIndexRange( 0, mCurrentIndexCount );
355 bool OnButtonClicked( Toolkit::Button button )
357 if( button == mMinusButton )
359 if (--mCurrentIndexCount < 2 )
360 mCurrentIndexCount = 2;
364 if (++mCurrentIndexCount > mMaxIndexCount )
365 mCurrentIndexCount = mMaxIndexCount;
368 std::stringstream str;
369 str << mCurrentIndexCount;
370 mIndicesCountLabel.SetProperty( Toolkit::TextLabel::Property::TEXT, str.str() );
371 mRenderer.SetIndexRange( 0, mCurrentIndexCount );
377 Application& mApplication; ///< Application instance
378 Vector3 mStageSize; ///< The size of the stage
384 Toolkit::RadioButton mButtons[3];
385 Toolkit::PushButton mMinusButton;
386 Toolkit::PushButton mPlusButton;
387 Toolkit::TextLabel mIndicesCountLabel;
388 Geometry::Type mPrimitiveType;
389 int mCurrentIndexCount;
393 void RunTest( Application& application )
395 ExampleController test( application );
397 application.MainLoop();
400 // Entry point for Linux & SLP applications
402 int DALI_EXPORT_API main( int argc, char **argv )
404 Application application = Application::New( &argc, &argv );
406 RunTest( application );