Add using UPDATE_SIZE_HINT for some demos with custom shaders.
[platform/core/uifw/dali-demo.git] / examples / line-mesh / line-mesh-example.cpp
1 /*
2  * Copyright (c) 2020 Samsung Electronics Co., Ltd.
3  *
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
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  *
16  */
17
18 // EXTERNAL INCLUDES
19 #include <dali/devel-api/actors/actor-devel.h>
20 #include <dali-toolkit/dali-toolkit.h>
21
22 // INTERNAL INCLUDES
23 #include "shared/view.h"
24
25 #include <sstream>
26
27 using namespace Dali;
28
29 namespace
30 {
31
32 #define MAKE_SHADER(A)#A
33
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;
41
42 varying   lowp    vec3    vColor;
43
44 void main()
45 {
46   mediump vec2 morphPosition = mix(aPosition1, aPosition2, uMorphAmount);
47   mediump vec4 vertexPosition = vec4(morphPosition, 0.0, 1.0);
48   vColor = aColor;
49   vertexPosition.xyz *= uSize;
50   vertexPosition = uMvpMatrix * vertexPosition;
51   gl_Position = vertexPosition;
52 }
53 );
54
55 const char* FRAGMENT_SHADER = MAKE_SHADER(
56 uniform lowp  vec4    uColor;
57 uniform sampler2D     sTexture;
58
59 varying   lowp        vec3 vColor;
60
61 void main()
62 {
63   gl_FragColor = uColor * vec4( vColor, 1.0 );
64 }
65 );
66
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])};
72
73 Geometry CreateGeometry()
74 {
75   // Create vertices
76   struct Vertex
77   {
78     Vector2 position1;
79     Vector2 position2;
80     Vector3 color;
81   };
82
83   // Create new geometry object
84   Vertex pentagonVertexData[5] =
85     {
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
91     };
92
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);
99
100
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;
107 }
108
109 } // anonymous namespace
110
111 // This example shows how to morph between 2 meshes with the same number of
112 // vertices.
113 class ExampleController : public ConnectionTracker
114 {
115 public:
116
117   /**
118    * The example controller constructor.
119    * @param[in] application The application instance
120    */
121   ExampleController( Application& application )
122   : mApplication( application ),
123     mStageSize(),
124     mShader(),
125     mGeometry(),
126     mRenderer(),
127     mMeshActor(),
128     mButtons(),
129     mMinusButton(),
130     mPlusButton(),
131     mIndicesCountLabel(),
132     mPrimitiveType( Geometry::LINES ),
133     mCurrentIndexCount( 0 ),
134     mMaxIndexCount( 0 )
135   {
136     // Connect to the Application's Init signal
137     mApplication.InitSignal().Connect( this, &ExampleController::Create );
138   }
139
140   /**
141    * The example controller destructor
142    */
143   ~ExampleController()
144   {
145     // Nothing to do here;
146   }
147
148   /**
149    * Invoked upon creation of application
150    * @param[in] application The application instance
151    */
152   void Create( Application& application )
153   {
154     Stage stage = Stage::GetCurrent();
155
156     // initial settings
157     mPrimitiveType = Geometry::LINES;
158     mCurrentIndexCount = 10;
159     mMaxIndexCount = 10;
160
161     CreateRadioButtons();
162
163     stage.KeyEventSignal().Connect(this, &ExampleController::OnKeyEvent);
164
165     mStageSize = stage.GetSize();
166
167     Initialise();
168
169     // Hide the indicator bar
170     application.GetWindow().ShowIndicator( Dali::Window::INVISIBLE );
171
172     stage.SetBackgroundColor(Vector4(0.0f, 0.2f, 0.2f, 1.0f));
173   }
174
175   /**
176    * Invoked whenever application changes the type of geometry drawn
177    */
178   void Initialise()
179   {
180     Stage stage = Stage::GetCurrent();
181
182     // destroy mesh actor and its resources if already exists
183     if( mMeshActor )
184     {
185       stage.Remove( mMeshActor );
186       mMeshActor.Reset();
187     }
188
189     mShader = Shader::New( VERTEX_SHADER, FRAGMENT_SHADER );
190     mGeometry = CreateGeometry();
191     mRenderer = Renderer::New( mGeometry, mShader );
192
193     mRenderer.SetIndexRange( 0, 10 ); // lines
194     mPrimitiveType = Geometry::LINES;
195
196     mMeshActor = Actor::New();
197     mMeshActor.AddRenderer( mRenderer );
198     mMeshActor.SetProperty( Actor::Property::SIZE, Vector2(200, 200) );
199     mMeshActor.SetProperty( DevelActor::Property::UPDATE_SIZE_HINT, Vector2(400, 400) );
200
201     Property::Index morphAmountIndex = mMeshActor.RegisterProperty( "uMorphAmount", 0.0f );
202
203     mRenderer.SetProperty( Renderer::Property::DEPTH_INDEX, 0 );
204
205     mMeshActor.SetProperty( Actor::Property::PARENT_ORIGIN, ParentOrigin::CENTER );
206     mMeshActor.SetProperty( Actor::Property::ANCHOR_POINT, AnchorPoint::CENTER );
207     stage.Add( mMeshActor );
208
209     Animation  animation = Animation::New(5);
210     KeyFrames keyFrames = KeyFrames::New();
211     keyFrames.Add(0.0f, 0.0f);
212     keyFrames.Add(1.0f, 1.0f);
213
214     animation.AnimateBetween( Property( mMeshActor, morphAmountIndex ), keyFrames, AlphaFunction(AlphaFunction::SIN) );
215     animation.SetLooping(true);
216     animation.Play();
217   }
218
219   /**
220    * Invoked on create
221    */
222   void CreateRadioButtons()
223   {
224     Stage stage = Stage::GetCurrent();
225
226     Toolkit::TableView modeSelectTableView = Toolkit::TableView::New( 4, 1 );
227     modeSelectTableView.SetProperty( Actor::Property::PARENT_ORIGIN, ParentOrigin::TOP_LEFT );
228     modeSelectTableView.SetProperty( Actor::Property::ANCHOR_POINT, AnchorPoint::TOP_LEFT );
229     modeSelectTableView.SetFitHeight( 0 );
230     modeSelectTableView.SetFitHeight( 1 );
231     modeSelectTableView.SetFitHeight( 2 );
232     modeSelectTableView.SetCellPadding( Vector2( 6.0f, 0.0f ) );
233     modeSelectTableView.SetProperty( Actor::Property::SCALE, Vector3( 0.8f, 0.8f, 0.8f ));
234
235     const char* labels[] =
236     {
237       "LINES",
238       "LINE_LOOP",
239       "LINE_STRIP"
240     };
241
242     for( int i = 0; i < 3; ++i )
243     {
244       Dali::Toolkit::RadioButton radio = Dali::Toolkit::RadioButton::New();
245
246       radio.SetProperty( Toolkit::Button::Property::LABEL,
247                                  Property::Map()
248                                   .Add( Toolkit::Visual::Property::TYPE, Toolkit::Visual::TEXT )
249                                   .Add( Toolkit::TextVisual::Property::TEXT, labels[i] )
250                                   .Add( Toolkit::TextVisual::Property::TEXT_COLOR, Vector4( 0.8f, 0.8f, 0.8f, 1.0f ) )
251                                );
252
253       radio.SetProperty( Actor::Property::PARENT_ORIGIN, ParentOrigin::TOP_LEFT );
254       radio.SetProperty( Actor::Property::ANCHOR_POINT, AnchorPoint::TOP_LEFT );
255       radio.SetProperty( Toolkit::Button::Property::SELECTED, i == 0 );
256       radio.PressedSignal().Connect( this, &ExampleController::OnButtonPressed );
257       mButtons[i] = radio;
258       modeSelectTableView.AddChild( radio, Toolkit::TableView::CellPosition( i,  0 ) );
259     }
260
261     Toolkit::TableView elementCountTableView = Toolkit::TableView::New( 1, 3 );
262     elementCountTableView.SetCellPadding( Vector2( 6.0f, 0.0f ) );
263     elementCountTableView.SetProperty( Actor::Property::PARENT_ORIGIN, ParentOrigin::BOTTOM_LEFT );
264     elementCountTableView.SetProperty( Actor::Property::ANCHOR_POINT, AnchorPoint::BOTTOM_LEFT );
265     elementCountTableView.SetFitHeight( 0 );
266     elementCountTableView.SetFitWidth( 0 );
267     elementCountTableView.SetFitWidth( 1 );
268     elementCountTableView.SetFitWidth( 2 );
269     mMinusButton = Toolkit::PushButton::New();
270     mMinusButton.SetProperty( Toolkit::Button::Property::LABEL, "<<" );
271     mMinusButton.SetProperty( Actor::Property::PARENT_ORIGIN, ParentOrigin::TOP_LEFT );
272     mMinusButton.SetProperty( Actor::Property::ANCHOR_POINT, AnchorPoint::CENTER_LEFT );
273
274     Toolkit::PushButton mPlusButton = Toolkit::PushButton::New();
275     mPlusButton.SetProperty( Toolkit::Button::Property::LABEL, ">>" );
276     mPlusButton.SetProperty( Actor::Property::PARENT_ORIGIN, ParentOrigin::TOP_LEFT );
277     mPlusButton.SetProperty( Actor::Property::ANCHOR_POINT, AnchorPoint::CENTER_RIGHT );
278
279     mMinusButton.ClickedSignal().Connect( this, &ExampleController::OnButtonClicked );
280     mPlusButton.ClickedSignal().Connect( this, &ExampleController::OnButtonClicked );
281
282     mIndicesCountLabel = Toolkit::TextLabel::New();
283     mIndicesCountLabel.SetProperty( Actor::Property::PARENT_ORIGIN, ParentOrigin::CENTER );
284     mIndicesCountLabel.SetProperty( Actor::Property::ANCHOR_POINT, AnchorPoint::TOP_LEFT );
285
286     std::stringstream str;
287     str << mCurrentIndexCount;
288     mIndicesCountLabel.SetProperty( Toolkit::TextLabel::Property::TEXT, str.str() );
289     mIndicesCountLabel.SetProperty( Toolkit::TextLabel::Property::TEXT_COLOR, Vector4( 1.0, 1.0, 1.0, 1.0 ) );
290     mIndicesCountLabel.SetProperty( Toolkit::TextLabel::Property::VERTICAL_ALIGNMENT, "BOTTOM");
291     mIndicesCountLabel.SetResizePolicy( ResizePolicy::USE_NATURAL_SIZE, Dimension::WIDTH );
292     mIndicesCountLabel.SetResizePolicy( ResizePolicy::USE_NATURAL_SIZE, Dimension::HEIGHT );
293
294     elementCountTableView.AddChild( mMinusButton, Toolkit::TableView::CellPosition( 0,  0 ) );
295     elementCountTableView.AddChild( mIndicesCountLabel, Toolkit::TableView::CellPosition( 0,  1 ) );
296     elementCountTableView.AddChild( mPlusButton, Toolkit::TableView::CellPosition( 0,  2 ) );
297
298     stage.Add(modeSelectTableView);
299     stage.Add(elementCountTableView);
300   }
301
302   /**
303    * Invoked whenever the quit button is clicked
304    * @param[in] button the quit button
305    */
306   bool OnQuitButtonClicked( Toolkit::Button button )
307   {
308     // quit the application
309     mApplication.Quit();
310     return true;
311   }
312
313   void OnKeyEvent(const KeyEvent& event)
314   {
315     if(event.state == KeyEvent::Down)
316     {
317       if( IsKey( event, Dali::DALI_KEY_ESCAPE) || IsKey( event, Dali::DALI_KEY_BACK) )
318       {
319         mApplication.Quit();
320       }
321     }
322   }
323
324   bool OnButtonPressed( Toolkit::Button button )
325   {
326     int indicesArray;
327     if( button == mButtons[0] )
328     {
329       mCurrentIndexCount = 10;
330       mMaxIndexCount = 10;
331       mPrimitiveType = Geometry::LINES;
332       indicesArray = 0;
333     }
334     else if( button == mButtons[1] )
335     {
336       mCurrentIndexCount = 5;
337       mMaxIndexCount = 5;
338       mPrimitiveType = Geometry::LINE_LOOP;
339       indicesArray = 1;
340     }
341     else
342     {
343       mCurrentIndexCount = 6;
344       mMaxIndexCount = 6;
345       mPrimitiveType = Geometry::LINE_STRIP;
346       indicesArray = 2;
347     }
348
349     std::stringstream str;
350     str << mCurrentIndexCount;
351     mIndicesCountLabel.SetProperty( Toolkit::TextLabel::Property::TEXT, str.str() );
352     mGeometry.SetType( mPrimitiveType );
353     mGeometry.SetIndexBuffer( INDICES[ indicesArray ], INDICES_SIZE[ indicesArray ] );
354     mRenderer.SetIndexRange( 0, mCurrentIndexCount );
355     return true;
356   }
357
358   bool OnButtonClicked( Toolkit::Button button )
359   {
360     if( button == mMinusButton )
361     {
362       if (--mCurrentIndexCount < 2 )
363         mCurrentIndexCount = 2;
364     }
365     else
366     {
367       if (++mCurrentIndexCount > mMaxIndexCount )
368         mCurrentIndexCount = mMaxIndexCount;
369     }
370
371     std::stringstream str;
372     str << mCurrentIndexCount;
373     mIndicesCountLabel.SetProperty( Toolkit::TextLabel::Property::TEXT, str.str() );
374     mRenderer.SetIndexRange( 0, mCurrentIndexCount );
375     return true;
376   }
377
378 private:
379
380   Application&  mApplication;                             ///< Application instance
381   Vector3 mStageSize;                                     ///< The size of the stage
382
383   Shader   mShader;
384   Geometry mGeometry;
385   Renderer mRenderer;
386   Actor    mMeshActor;
387   Toolkit::RadioButton  mButtons[3];
388   Toolkit::PushButton   mMinusButton;
389   Toolkit::PushButton   mPlusButton;
390   Toolkit::TextLabel    mIndicesCountLabel;
391   Geometry::Type mPrimitiveType;
392   int      mCurrentIndexCount;
393   int      mMaxIndexCount;
394 };
395
396 int DALI_EXPORT_API main( int argc, char **argv )
397 {
398   Application application = Application::New( &argc, &argv );
399   ExampleController test( application );
400   application.MainLoop();
401   return 0;
402 }