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