2 * Copyright (c) 2022 Samsung Electronics Co., Ltd.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 #include <dali-toolkit/dali-toolkit.h>
20 #include <dali-toolkit/devel-api/controls/table-view/table-view.h>
23 #include "generated/line-mesh-frag.h"
24 #include "generated/line-mesh-vert.h"
25 #include "shared/view.h"
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])};
39 Geometry CreateGeometry()
49 // Create new geometry object
50 Vertex pentagonVertexData[5] =
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
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);
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;
74 } // anonymous namespace
76 // This example shows how to morph between 2 meshes with the same number of
78 class ExampleController : public ConnectionTracker
82 * The example controller constructor.
83 * @param[in] application The application instance
85 ExampleController(Application& application)
86 : mApplication(application),
96 mPrimitiveType(Geometry::LINES),
97 mCurrentIndexCount(0),
100 // Connect to the Application's Init signal
101 mApplication.InitSignal().Connect(this, &ExampleController::Create);
105 * The example controller destructor
109 // Nothing to do here;
113 * Invoked upon creation of application
114 * @param[in] application The application instance
116 void Create(Application& application)
118 Window window = application.GetWindow();
121 mPrimitiveType = Geometry::LINES;
122 mCurrentIndexCount = 10;
125 CreateRadioButtons();
127 window.KeyEventSignal().Connect(this, &ExampleController::OnKeyEvent);
129 mWindowSize = window.GetSize();
133 window.SetBackgroundColor(Vector4(0.0f, 0.2f, 0.2f, 1.0f));
137 * Invoked whenever application changes the type of geometry drawn
141 Window window = mApplication.GetWindow();
143 // destroy mesh actor and its resources if already exists
146 window.Remove(mMeshActor);
150 mShader = Shader::New(SHADER_LINE_MESH_VERT, SHADER_LINE_MESH_FRAG);
151 mGeometry = CreateGeometry();
152 mRenderer = Renderer::New(mGeometry, mShader);
154 mRenderer.SetIndexRange(0, 10); // lines
155 mPrimitiveType = Geometry::LINES;
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));
162 Property::Index morphAmountIndex = mMeshActor.RegisterProperty("uMorphAmount", 0.0f);
164 mRenderer.SetProperty(Renderer::Property::DEPTH_INDEX, 0);
166 mMeshActor.SetProperty(Actor::Property::PARENT_ORIGIN, ParentOrigin::CENTER);
167 mMeshActor.SetProperty(Actor::Property::ANCHOR_POINT, AnchorPoint::CENTER);
168 window.Add(mMeshActor);
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);
175 animation.AnimateBetween(Property(mMeshActor, morphAmountIndex), keyFrames, AlphaFunction(AlphaFunction::SIN));
176 animation.SetLooping(true);
183 void CreateRadioButtons()
185 Window window = mApplication.GetWindow();
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));
196 const char* labels[] =
202 for(int i = 0; i < 3; ++i)
204 Dali::Toolkit::RadioButton radio = Dali::Toolkit::RadioButton::New();
206 radio.SetProperty(Toolkit::Button::Property::LABEL,
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)));
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);
217 modeSelectTableView.AddChild(radio, Toolkit::TableView::CellPosition(i, 0));
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);
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);
238 mMinusButton.ClickedSignal().Connect(this, &ExampleController::OnButtonClicked);
239 mPlusButton.ClickedSignal().Connect(this, &ExampleController::OnButtonClicked);
241 mIndicesCountLabel = Toolkit::TextLabel::New();
242 mIndicesCountLabel.SetProperty(Actor::Property::PARENT_ORIGIN, ParentOrigin::CENTER);
243 mIndicesCountLabel.SetProperty(Actor::Property::ANCHOR_POINT, AnchorPoint::TOP_LEFT);
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);
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));
257 window.Add(modeSelectTableView);
258 window.Add(elementCountTableView);
262 * Invoked whenever the quit button is clicked
263 * @param[in] button the quit button
265 bool OnQuitButtonClicked(Toolkit::Button button)
267 // quit the application
272 void OnKeyEvent(const KeyEvent& event)
274 if(event.GetState() == KeyEvent::DOWN)
276 if(IsKey(event, Dali::DALI_KEY_ESCAPE) || IsKey(event, Dali::DALI_KEY_BACK))
283 bool OnButtonPressed(Toolkit::Button button)
286 if(button == mButtons[0])
288 mCurrentIndexCount = 10;
290 mPrimitiveType = Geometry::LINES;
293 else if(button == mButtons[1])
295 mCurrentIndexCount = 5;
297 mPrimitiveType = Geometry::LINE_LOOP;
302 mCurrentIndexCount = 6;
304 mPrimitiveType = Geometry::LINE_STRIP;
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);
317 bool OnButtonClicked(Toolkit::Button button)
319 if(button == mMinusButton)
321 if(--mCurrentIndexCount < 2)
322 mCurrentIndexCount = 2;
326 if(++mCurrentIndexCount > mMaxIndexCount)
327 mCurrentIndexCount = mMaxIndexCount;
330 std::stringstream str;
331 str << mCurrentIndexCount;
332 mIndicesCountLabel.SetProperty(Toolkit::TextLabel::Property::TEXT, str.str());
333 mRenderer.SetIndexRange(0, mCurrentIndexCount);
338 Application& mApplication; ///< Application instance
339 Vector3 mWindowSize; ///< The size of the window
345 Toolkit::RadioButton mButtons[3];
346 Toolkit::PushButton mMinusButton;
347 Toolkit::PushButton mPlusButton;
348 Toolkit::TextLabel mIndicesCountLabel;
349 Geometry::Type mPrimitiveType;
350 int mCurrentIndexCount;
354 int DALI_EXPORT_API main(int argc, char** argv)
356 Application application = Application::New(&argc, &argv);
357 ExampleController test(application);
358 application.MainLoop();