2 * Copyright (c) 2020 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/actors/actor-devel.h>
20 #include <dali-toolkit/dali-toolkit.h>
21 #include <dali-toolkit/devel-api/controls/table-view/table-view.h>
24 #include "shared/view.h"
33 #define MAKE_SHADER(A)#A
35 const char* VERTEX_SHADER = MAKE_SHADER(
36 attribute mediump vec2 aPosition1;
37 attribute mediump vec2 aPosition2;
38 attribute lowp vec3 aColor;
39 uniform mediump mat4 uMvpMatrix;
40 uniform mediump vec3 uSize;
41 uniform mediump float uMorphAmount;
43 varying lowp vec3 vColor;
47 mediump vec2 morphPosition = mix(aPosition1, aPosition2, uMorphAmount);
48 mediump vec4 vertexPosition = vec4(morphPosition, 0.0, 1.0);
50 vertexPosition.xyz *= uSize;
51 vertexPosition = uMvpMatrix * vertexPosition;
52 gl_Position = vertexPosition;
56 const char* FRAGMENT_SHADER = MAKE_SHADER(
57 uniform lowp vec4 uColor;
58 uniform sampler2D sTexture;
60 varying lowp vec3 vColor;
64 gl_FragColor = uColor * vec4( vColor, 1.0 );
68 const unsigned short INDEX_LINES[] = { 0, 1, 1, 2, 2, 3, 3, 4, 4, 0 };
69 const unsigned short INDEX_LOOP[] = { 0, 1, 2, 3, 4 };
70 const unsigned short INDEX_STRIP[] = { 0, 1, 2, 3, 4, 0 };
71 const unsigned short* INDICES[3] = { &INDEX_LINES[0], &INDEX_LOOP[0], &INDEX_STRIP[0] };
72 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])};
74 Geometry CreateGeometry()
84 // Create new geometry object
85 Vertex pentagonVertexData[5] =
87 { Vector2( 0.0f, 1.00f), Vector2( 0.0f, -1.00f), Vector3( 1.0f, 1.0f, 1.0f ) }, // 0
88 { Vector2( -0.95f, 0.31f), Vector2( 0.59f, 0.81f), Vector3( 1.0f, 0.0f, 0.0f ) }, // 1
89 { Vector2( -0.59f, -0.81f), Vector2( -0.95f, -0.31f), Vector3( 0.0f, 1.0f, 0.0f ) }, // 2
90 { Vector2( 0.59f, -0.81f), Vector2( 0.95f, -0.31f), Vector3( 0.0f, 0.0f, 1.0f ) }, // 3
91 { Vector2( 0.95f, 0.31f), Vector2( -0.59f, 0.81f), Vector3( 1.0f, 1.0f, 0.0f ) }, // 4
94 Property::Map pentagonVertexFormat;
95 pentagonVertexFormat["aPosition1"] = Property::VECTOR2;
96 pentagonVertexFormat["aPosition2"] = Property::VECTOR2;
97 pentagonVertexFormat["aColor"] = Property::VECTOR3;
98 VertexBuffer pentagonVertices = VertexBuffer::New( pentagonVertexFormat );
99 pentagonVertices.SetData(pentagonVertexData, 5);
102 // Create the geometry object
103 Geometry pentagonGeometry = Geometry::New();
104 pentagonGeometry.AddVertexBuffer( pentagonVertices );
105 pentagonGeometry.SetIndexBuffer( INDICES[0], INDICES_SIZE[0] );
106 pentagonGeometry.SetType( Geometry::LINES );
107 return pentagonGeometry;
110 } // anonymous namespace
112 // This example shows how to morph between 2 meshes with the same number of
114 class ExampleController : public ConnectionTracker
119 * The example controller constructor.
120 * @param[in] application The application instance
122 ExampleController( Application& application )
123 : mApplication( application ),
132 mIndicesCountLabel(),
133 mPrimitiveType( Geometry::LINES ),
134 mCurrentIndexCount( 0 ),
137 // Connect to the Application's Init signal
138 mApplication.InitSignal().Connect( this, &ExampleController::Create );
142 * The example controller destructor
146 // Nothing to do here;
150 * Invoked upon creation of application
151 * @param[in] application The application instance
153 void Create( Application& application )
155 Window window = application.GetWindow();
158 mPrimitiveType = Geometry::LINES;
159 mCurrentIndexCount = 10;
162 CreateRadioButtons();
164 window.KeyEventSignal().Connect(this, &ExampleController::OnKeyEvent);
166 mWindowSize = window.GetSize();
170 window.SetBackgroundColor(Vector4(0.0f, 0.2f, 0.2f, 1.0f));
174 * Invoked whenever application changes the type of geometry drawn
178 Window window = mApplication.GetWindow();
180 // destroy mesh actor and its resources if already exists
183 window.Remove( mMeshActor );
187 mShader = Shader::New( VERTEX_SHADER, FRAGMENT_SHADER );
188 mGeometry = CreateGeometry();
189 mRenderer = Renderer::New( mGeometry, mShader );
191 mRenderer.SetIndexRange( 0, 10 ); // lines
192 mPrimitiveType = Geometry::LINES;
194 mMeshActor = Actor::New();
195 mMeshActor.AddRenderer( mRenderer );
196 mMeshActor.SetProperty( Actor::Property::SIZE, Vector2(200, 200) );
197 mMeshActor.SetProperty( DevelActor::Property::UPDATE_SIZE_HINT, Vector2(400, 400) );
199 Property::Index morphAmountIndex = mMeshActor.RegisterProperty( "uMorphAmount", 0.0f );
201 mRenderer.SetProperty( Renderer::Property::DEPTH_INDEX, 0 );
203 mMeshActor.SetProperty( Actor::Property::PARENT_ORIGIN, ParentOrigin::CENTER );
204 mMeshActor.SetProperty( Actor::Property::ANCHOR_POINT, AnchorPoint::CENTER );
205 window.Add( mMeshActor );
207 Animation animation = Animation::New(5);
208 KeyFrames keyFrames = KeyFrames::New();
209 keyFrames.Add(0.0f, 0.0f);
210 keyFrames.Add(1.0f, 1.0f);
212 animation.AnimateBetween( Property( mMeshActor, morphAmountIndex ), keyFrames, AlphaFunction(AlphaFunction::SIN) );
213 animation.SetLooping(true);
220 void CreateRadioButtons()
222 Window window = mApplication.GetWindow();
224 Toolkit::TableView modeSelectTableView = Toolkit::TableView::New( 4, 1 );
225 modeSelectTableView.SetProperty( Actor::Property::PARENT_ORIGIN, ParentOrigin::TOP_LEFT );
226 modeSelectTableView.SetProperty( Actor::Property::ANCHOR_POINT, AnchorPoint::TOP_LEFT );
227 modeSelectTableView.SetFitHeight( 0 );
228 modeSelectTableView.SetFitHeight( 1 );
229 modeSelectTableView.SetFitHeight( 2 );
230 modeSelectTableView.SetCellPadding( Vector2( 6.0f, 0.0f ) );
231 modeSelectTableView.SetProperty( Actor::Property::SCALE, Vector3( 0.8f, 0.8f, 0.8f ));
233 const char* labels[] =
240 for( int i = 0; i < 3; ++i )
242 Dali::Toolkit::RadioButton radio = Dali::Toolkit::RadioButton::New();
244 radio.SetProperty( Toolkit::Button::Property::LABEL,
246 .Add( Toolkit::Visual::Property::TYPE, Toolkit::Visual::TEXT )
247 .Add( Toolkit::TextVisual::Property::TEXT, labels[i] )
248 .Add( Toolkit::TextVisual::Property::TEXT_COLOR, Vector4( 0.8f, 0.8f, 0.8f, 1.0f ) )
251 radio.SetProperty( Actor::Property::PARENT_ORIGIN, ParentOrigin::TOP_LEFT );
252 radio.SetProperty( Actor::Property::ANCHOR_POINT, AnchorPoint::TOP_LEFT );
253 radio.SetProperty( Toolkit::Button::Property::SELECTED, i == 0 );
254 radio.PressedSignal().Connect( this, &ExampleController::OnButtonPressed );
256 modeSelectTableView.AddChild( radio, Toolkit::TableView::CellPosition( i, 0 ) );
259 Toolkit::TableView elementCountTableView = Toolkit::TableView::New( 1, 3 );
260 elementCountTableView.SetCellPadding( Vector2( 6.0f, 0.0f ) );
261 elementCountTableView.SetProperty( Actor::Property::PARENT_ORIGIN, ParentOrigin::BOTTOM_LEFT );
262 elementCountTableView.SetProperty( Actor::Property::ANCHOR_POINT, AnchorPoint::BOTTOM_LEFT );
263 elementCountTableView.SetFitHeight( 0 );
264 elementCountTableView.SetFitWidth( 0 );
265 elementCountTableView.SetFitWidth( 1 );
266 elementCountTableView.SetFitWidth( 2 );
267 mMinusButton = Toolkit::PushButton::New();
268 mMinusButton.SetProperty( Toolkit::Button::Property::LABEL, "<<" );
269 mMinusButton.SetProperty( Actor::Property::PARENT_ORIGIN, ParentOrigin::TOP_LEFT );
270 mMinusButton.SetProperty( Actor::Property::ANCHOR_POINT, AnchorPoint::CENTER_LEFT );
272 Toolkit::PushButton mPlusButton = Toolkit::PushButton::New();
273 mPlusButton.SetProperty( Toolkit::Button::Property::LABEL, ">>" );
274 mPlusButton.SetProperty( Actor::Property::PARENT_ORIGIN, ParentOrigin::TOP_LEFT );
275 mPlusButton.SetProperty( Actor::Property::ANCHOR_POINT, AnchorPoint::CENTER_RIGHT );
277 mMinusButton.ClickedSignal().Connect( this, &ExampleController::OnButtonClicked );
278 mPlusButton.ClickedSignal().Connect( this, &ExampleController::OnButtonClicked );
280 mIndicesCountLabel = Toolkit::TextLabel::New();
281 mIndicesCountLabel.SetProperty( Actor::Property::PARENT_ORIGIN, ParentOrigin::CENTER );
282 mIndicesCountLabel.SetProperty( Actor::Property::ANCHOR_POINT, AnchorPoint::TOP_LEFT );
284 std::stringstream str;
285 str << mCurrentIndexCount;
286 mIndicesCountLabel.SetProperty( Toolkit::TextLabel::Property::TEXT, str.str() );
287 mIndicesCountLabel.SetProperty( Toolkit::TextLabel::Property::TEXT_COLOR, Vector4( 1.0, 1.0, 1.0, 1.0 ) );
288 mIndicesCountLabel.SetProperty( Toolkit::TextLabel::Property::VERTICAL_ALIGNMENT, "BOTTOM");
289 mIndicesCountLabel.SetResizePolicy( ResizePolicy::USE_NATURAL_SIZE, Dimension::WIDTH );
290 mIndicesCountLabel.SetResizePolicy( ResizePolicy::USE_NATURAL_SIZE, Dimension::HEIGHT );
292 elementCountTableView.AddChild( mMinusButton, Toolkit::TableView::CellPosition( 0, 0 ) );
293 elementCountTableView.AddChild( mIndicesCountLabel, Toolkit::TableView::CellPosition( 0, 1 ) );
294 elementCountTableView.AddChild( mPlusButton, Toolkit::TableView::CellPosition( 0, 2 ) );
296 window.Add(modeSelectTableView);
297 window.Add(elementCountTableView);
301 * Invoked whenever the quit button is clicked
302 * @param[in] button the quit button
304 bool OnQuitButtonClicked( Toolkit::Button button )
306 // quit the application
311 void OnKeyEvent(const KeyEvent& event)
313 if(event.GetState() == KeyEvent::Down)
315 if( IsKey( event, Dali::DALI_KEY_ESCAPE) || IsKey( event, Dali::DALI_KEY_BACK) )
322 bool OnButtonPressed( Toolkit::Button button )
325 if( button == mButtons[0] )
327 mCurrentIndexCount = 10;
329 mPrimitiveType = Geometry::LINES;
332 else if( button == mButtons[1] )
334 mCurrentIndexCount = 5;
336 mPrimitiveType = Geometry::LINE_LOOP;
341 mCurrentIndexCount = 6;
343 mPrimitiveType = Geometry::LINE_STRIP;
347 std::stringstream str;
348 str << mCurrentIndexCount;
349 mIndicesCountLabel.SetProperty( Toolkit::TextLabel::Property::TEXT, str.str() );
350 mGeometry.SetType( mPrimitiveType );
351 mGeometry.SetIndexBuffer( INDICES[ indicesArray ], INDICES_SIZE[ indicesArray ] );
352 mRenderer.SetIndexRange( 0, mCurrentIndexCount );
356 bool OnButtonClicked( Toolkit::Button button )
358 if( button == mMinusButton )
360 if (--mCurrentIndexCount < 2 )
361 mCurrentIndexCount = 2;
365 if (++mCurrentIndexCount > mMaxIndexCount )
366 mCurrentIndexCount = mMaxIndexCount;
369 std::stringstream str;
370 str << mCurrentIndexCount;
371 mIndicesCountLabel.SetProperty( Toolkit::TextLabel::Property::TEXT, str.str() );
372 mRenderer.SetIndexRange( 0, mCurrentIndexCount );
378 Application& mApplication; ///< Application instance
379 Vector3 mWindowSize; ///< The size of the window
385 Toolkit::RadioButton mButtons[3];
386 Toolkit::PushButton mMinusButton;
387 Toolkit::PushButton mPlusButton;
388 Toolkit::TextLabel mIndicesCountLabel;
389 Geometry::Type mPrimitiveType;
390 int mCurrentIndexCount;
394 int DALI_EXPORT_API main( int argc, char **argv )
396 Application application = Application::New( &argc, &argv );
397 ExampleController test( application );
398 application.MainLoop();