2 * Copyright (c) 2017 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>
21 #include <dali-toolkit/devel-api/visuals/visual-properties-devel.h>
22 #include <dali-toolkit/devel-api/visuals/text-visual-properties.h>
25 #include "shared/view.h"
34 #define MAKE_SHADER(A)#A
36 const char* VERTEX_SHADER = MAKE_SHADER(
37 attribute mediump vec2 aPosition1;
38 attribute mediump vec2 aPosition2;
39 attribute lowp vec3 aColor;
40 uniform mediump mat4 uMvpMatrix;
41 uniform mediump vec3 uSize;
42 uniform mediump float uMorphAmount;
44 varying lowp vec3 vColor;
48 mediump vec2 morphPosition = mix(aPosition1, aPosition2, uMorphAmount);
49 mediump vec4 vertexPosition = vec4(morphPosition, 0.0, 1.0);
51 vertexPosition.xyz *= uSize;
52 vertexPosition = uMvpMatrix * vertexPosition;
53 gl_Position = vertexPosition;
57 const char* FRAGMENT_SHADER = MAKE_SHADER(
58 uniform lowp vec4 uColor;
59 uniform sampler2D sTexture;
61 varying lowp vec3 vColor;
65 gl_FragColor = uColor * vec4( vColor, 1.0 );
69 const unsigned short INDEX_LINES[] = { 0, 1, 1, 2, 2, 3, 3, 4, 4, 0 };
70 const unsigned short INDEX_LOOP[] = { 0, 1, 2, 3, 4 };
71 const unsigned short INDEX_STRIP[] = { 0, 1, 2, 3, 4, 0 };
72 const unsigned short* INDICES[3] = { &INDEX_LINES[0], &INDEX_LOOP[0], &INDEX_STRIP[0] };
73 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])};
75 Geometry CreateGeometry()
85 // Create new geometry object
86 Vertex pentagonVertexData[5] =
88 { Vector2( 0.0f, 1.00f), Vector2( 0.0f, -1.00f), Vector3( 1.0f, 1.0f, 1.0f ) }, // 0
89 { Vector2( -0.95f, 0.31f), Vector2( 0.59f, 0.81f), Vector3( 1.0f, 0.0f, 0.0f ) }, // 1
90 { Vector2( -0.59f, -0.81f), Vector2( -0.95f, -0.31f), Vector3( 0.0f, 1.0f, 0.0f ) }, // 2
91 { Vector2( 0.59f, -0.81f), Vector2( 0.95f, -0.31f), Vector3( 0.0f, 0.0f, 1.0f ) }, // 3
92 { Vector2( 0.95f, 0.31f), Vector2( -0.59f, 0.81f), Vector3( 1.0f, 1.0f, 0.0f ) }, // 4
95 Property::Map pentagonVertexFormat;
96 pentagonVertexFormat["aPosition1"] = Property::VECTOR2;
97 pentagonVertexFormat["aPosition2"] = Property::VECTOR2;
98 pentagonVertexFormat["aColor"] = Property::VECTOR3;
99 PropertyBuffer pentagonVertices = PropertyBuffer::New( pentagonVertexFormat );
100 pentagonVertices.SetData(pentagonVertexData, 5);
103 // Create the geometry object
104 Geometry pentagonGeometry = Geometry::New();
105 pentagonGeometry.AddVertexBuffer( pentagonVertices );
106 pentagonGeometry.SetIndexBuffer( INDICES[0], INDICES_SIZE[0] );
107 pentagonGeometry.SetType( 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 ),
133 mIndicesCountLabel(),
134 mPrimitiveType( Geometry::LINES ),
135 mCurrentIndexCount( 0 ),
138 // Connect to the Application's Init signal
139 mApplication.InitSignal().Connect( this, &ExampleController::Create );
143 * The example controller destructor
147 // Nothing to do here;
151 * Invoked upon creation of application
152 * @param[in] application The application instance
154 void Create( Application& application )
156 Stage stage = Stage::GetCurrent();
159 mPrimitiveType = Geometry::LINES;
160 mCurrentIndexCount = 10;
163 CreateRadioButtons();
165 stage.KeyEventSignal().Connect(this, &ExampleController::OnKeyEvent);
167 mStageSize = stage.GetSize();
171 // Hide the indicator bar
172 application.GetWindow().ShowIndicator( Dali::Window::INVISIBLE );
174 stage.SetBackgroundColor(Vector4(0.0f, 0.2f, 0.2f, 1.0f));
178 * Invoked whenever application changes the type of geometry drawn
182 Stage stage = Stage::GetCurrent();
184 // destroy mesh actor and its resources if already exists
187 stage.Remove( mMeshActor );
191 mShader = Shader::New( VERTEX_SHADER, FRAGMENT_SHADER );
192 mGeometry = CreateGeometry();
193 mRenderer = Renderer::New( mGeometry, mShader );
195 mRenderer.SetIndexRange( 0, 10 ); // lines
196 mPrimitiveType = Geometry::LINES;
198 mMeshActor = Actor::New();
199 mMeshActor.AddRenderer( mRenderer );
200 mMeshActor.SetSize(200, 200);
202 Property::Index morphAmountIndex = mMeshActor.RegisterProperty( "uMorphAmount", 0.0f );
204 mRenderer.SetProperty( Renderer::Property::DEPTH_INDEX, 0 );
206 mMeshActor.SetParentOrigin( ParentOrigin::CENTER );
207 mMeshActor.SetAnchorPoint( AnchorPoint::CENTER );
208 stage.Add( mMeshActor );
210 Animation animation = Animation::New(5);
211 KeyFrames keyFrames = KeyFrames::New();
212 keyFrames.Add(0.0f, 0.0f);
213 keyFrames.Add(1.0f, 1.0f);
215 animation.AnimateBetween( Property( mMeshActor, morphAmountIndex ), keyFrames, AlphaFunction(AlphaFunction::SIN) );
216 animation.SetLooping(true);
223 void CreateRadioButtons()
225 Stage stage = Stage::GetCurrent();
227 Toolkit::TableView modeSelectTableView = Toolkit::TableView::New( 4, 1 );
228 modeSelectTableView.SetParentOrigin( ParentOrigin::TOP_LEFT );
229 modeSelectTableView.SetAnchorPoint( AnchorPoint::TOP_LEFT );
230 modeSelectTableView.SetFitHeight( 0 );
231 modeSelectTableView.SetFitHeight( 1 );
232 modeSelectTableView.SetFitHeight( 2 );
233 modeSelectTableView.SetCellPadding( Vector2( 6.0f, 0.0f ) );
234 modeSelectTableView.SetScale( Vector3( 0.8f, 0.8f, 0.8f ));
236 const char* labels[] =
243 for( int i = 0; i < 3; ++i )
245 Dali::Toolkit::RadioButton radio = Dali::Toolkit::RadioButton::New();
247 radio.SetProperty( Toolkit::Button::Property::LABEL,
249 .Add( Toolkit::Visual::Property::TYPE, Toolkit::DevelVisual::TEXT )
250 .Add( Toolkit::TextVisual::Property::TEXT, labels[i] )
251 .Add( Toolkit::TextVisual::Property::TEXT_COLOR, Vector4( 0.8f, 0.8f, 0.8f, 1.0f ) )
254 radio.SetParentOrigin( ParentOrigin::TOP_LEFT );
255 radio.SetAnchorPoint( AnchorPoint::TOP_LEFT );
256 radio.SetProperty( Toolkit::Button::Property::SELECTED, i == 0 );
257 radio.PressedSignal().Connect( this, &ExampleController::OnButtonPressed );
259 modeSelectTableView.AddChild( radio, Toolkit::TableView::CellPosition( i, 0 ) );
262 Toolkit::TableView elementCountTableView = Toolkit::TableView::New( 1, 3 );
263 elementCountTableView.SetCellPadding( Vector2( 6.0f, 0.0f ) );
264 elementCountTableView.SetParentOrigin( ParentOrigin::BOTTOM_LEFT );
265 elementCountTableView.SetAnchorPoint( AnchorPoint::BOTTOM_LEFT );
266 elementCountTableView.SetFitHeight( 0 );
267 elementCountTableView.SetFitWidth( 0 );
268 elementCountTableView.SetFitWidth( 1 );
269 elementCountTableView.SetFitWidth( 2 );
270 mMinusButton = Toolkit::PushButton::New();
271 mMinusButton.SetProperty( Toolkit::Button::Property::LABEL, "<<" );
272 mMinusButton.SetParentOrigin( ParentOrigin::TOP_LEFT );
273 mMinusButton.SetAnchorPoint( AnchorPoint::CENTER_LEFT );
275 Toolkit::PushButton mPlusButton = Toolkit::PushButton::New();
276 mPlusButton.SetProperty( Toolkit::Button::Property::LABEL, ">>" );
277 mPlusButton.SetParentOrigin( ParentOrigin::TOP_LEFT );
278 mPlusButton.SetAnchorPoint( AnchorPoint::CENTER_RIGHT );
280 mMinusButton.ClickedSignal().Connect( this, &ExampleController::OnButtonClicked );
281 mPlusButton.ClickedSignal().Connect( this, &ExampleController::OnButtonClicked );
283 mIndicesCountLabel = Toolkit::TextLabel::New();
284 mIndicesCountLabel.SetParentOrigin( ParentOrigin::CENTER );
285 mIndicesCountLabel.SetAnchorPoint( AnchorPoint::TOP_LEFT );
287 std::stringstream str;
288 str << mCurrentIndexCount;
289 mIndicesCountLabel.SetProperty( Toolkit::TextLabel::Property::TEXT, str.str() );
290 mIndicesCountLabel.SetProperty( Toolkit::TextLabel::Property::TEXT_COLOR, Vector4( 1.0, 1.0, 1.0, 1.0 ) );
291 mIndicesCountLabel.SetProperty( Toolkit::TextLabel::Property::VERTICAL_ALIGNMENT, "BOTTOM");
292 mIndicesCountLabel.SetResizePolicy( ResizePolicy::USE_NATURAL_SIZE, Dimension::WIDTH );
293 mIndicesCountLabel.SetResizePolicy( ResizePolicy::USE_NATURAL_SIZE, Dimension::HEIGHT );
295 elementCountTableView.AddChild( mMinusButton, Toolkit::TableView::CellPosition( 0, 0 ) );
296 elementCountTableView.AddChild( mIndicesCountLabel, Toolkit::TableView::CellPosition( 0, 1 ) );
297 elementCountTableView.AddChild( mPlusButton, Toolkit::TableView::CellPosition( 0, 2 ) );
299 stage.Add(modeSelectTableView);
300 stage.Add(elementCountTableView);
304 * Invoked whenever the quit button is clicked
305 * @param[in] button the quit button
307 bool OnQuitButtonClicked( Toolkit::Button button )
309 // quit the application
314 void OnKeyEvent(const KeyEvent& event)
316 if(event.state == KeyEvent::Down)
318 if( IsKey( event, Dali::DALI_KEY_ESCAPE) || IsKey( event, Dali::DALI_KEY_BACK) )
325 bool OnButtonPressed( Toolkit::Button button )
328 if( button == mButtons[0] )
330 mCurrentIndexCount = 10;
332 mPrimitiveType = Geometry::LINES;
335 else if( button == mButtons[1] )
337 mCurrentIndexCount = 5;
339 mPrimitiveType = Geometry::LINE_LOOP;
344 mCurrentIndexCount = 6;
346 mPrimitiveType = Geometry::LINE_STRIP;
350 std::stringstream str;
351 str << mCurrentIndexCount;
352 mIndicesCountLabel.SetProperty( Toolkit::TextLabel::Property::TEXT, str.str() );
353 mGeometry.SetType( mPrimitiveType );
354 mGeometry.SetIndexBuffer( INDICES[ indicesArray ], INDICES_SIZE[ indicesArray ] );
355 mRenderer.SetIndexRange( 0, mCurrentIndexCount );
359 bool OnButtonClicked( Toolkit::Button button )
361 if( button == mMinusButton )
363 if (--mCurrentIndexCount < 2 )
364 mCurrentIndexCount = 2;
368 if (++mCurrentIndexCount > mMaxIndexCount )
369 mCurrentIndexCount = mMaxIndexCount;
372 std::stringstream str;
373 str << mCurrentIndexCount;
374 mIndicesCountLabel.SetProperty( Toolkit::TextLabel::Property::TEXT, str.str() );
375 mRenderer.SetIndexRange( 0, mCurrentIndexCount );
381 Application& mApplication; ///< Application instance
382 Vector3 mStageSize; ///< The size of the stage
388 Toolkit::RadioButton mButtons[3];
389 Toolkit::PushButton mMinusButton;
390 Toolkit::PushButton mPlusButton;
391 Toolkit::TextLabel mIndicesCountLabel;
392 Geometry::Type mPrimitiveType;
393 int mCurrentIndexCount;
397 void RunTest( Application& application )
399 ExampleController test( application );
401 application.MainLoop();
404 // Entry point for Linux & SLP applications
406 int DALI_EXPORT_API main( int argc, char **argv )
408 Application application = Application::New( &argc, &argv );
410 RunTest( application );