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