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