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