2 * Copyright (c) 2021 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>
21 #include <dali/devel-api/actors/actor-devel.h>
24 #include "generated/line-mesh-frag.h"
25 #include "generated/line-mesh-vert.h"
26 #include "shared/view.h"
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])};
40 Geometry CreateGeometry()
50 // Create new geometry object
51 Vertex pentagonVertexData[5] =
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
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);
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;
75 } // anonymous namespace
77 // This example shows how to morph between 2 meshes with the same number of
79 class ExampleController : public ConnectionTracker
83 * The example controller constructor.
84 * @param[in] application The application instance
86 ExampleController(Application& application)
87 : mApplication(application),
97 mPrimitiveType(Geometry::LINES),
98 mCurrentIndexCount(0),
101 // Connect to the Application's Init signal
102 mApplication.InitSignal().Connect(this, &ExampleController::Create);
106 * The example controller destructor
110 // Nothing to do here;
114 * Invoked upon creation of application
115 * @param[in] application The application instance
117 void Create(Application& application)
119 Window window = application.GetWindow();
122 mPrimitiveType = Geometry::LINES;
123 mCurrentIndexCount = 10;
126 CreateRadioButtons();
128 window.KeyEventSignal().Connect(this, &ExampleController::OnKeyEvent);
130 mWindowSize = window.GetSize();
134 window.SetBackgroundColor(Vector4(0.0f, 0.2f, 0.2f, 1.0f));
138 * Invoked whenever application changes the type of geometry drawn
142 Window window = mApplication.GetWindow();
144 // destroy mesh actor and its resources if already exists
147 window.Remove(mMeshActor);
151 mShader = Shader::New(SHADER_LINE_MESH_VERT, SHADER_LINE_MESH_FRAG);
152 mGeometry = CreateGeometry();
153 mRenderer = Renderer::New(mGeometry, mShader);
155 mRenderer.SetIndexRange(0, 10); // lines
156 mPrimitiveType = Geometry::LINES;
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));
163 Property::Index morphAmountIndex = mMeshActor.RegisterProperty("uMorphAmount", 0.0f);
165 mRenderer.SetProperty(Renderer::Property::DEPTH_INDEX, 0);
167 mMeshActor.SetProperty(Actor::Property::PARENT_ORIGIN, ParentOrigin::CENTER);
168 mMeshActor.SetProperty(Actor::Property::ANCHOR_POINT, AnchorPoint::CENTER);
169 window.Add(mMeshActor);
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);
176 animation.AnimateBetween(Property(mMeshActor, morphAmountIndex), keyFrames, AlphaFunction(AlphaFunction::SIN));
177 animation.SetLooping(true);
184 void CreateRadioButtons()
186 Window window = mApplication.GetWindow();
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));
197 const char* labels[] =
203 for(int i = 0; i < 3; ++i)
205 Dali::Toolkit::RadioButton radio = Dali::Toolkit::RadioButton::New();
207 radio.SetProperty(Toolkit::Button::Property::LABEL,
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)));
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);
218 modeSelectTableView.AddChild(radio, Toolkit::TableView::CellPosition(i, 0));
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);
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);
239 mMinusButton.ClickedSignal().Connect(this, &ExampleController::OnButtonClicked);
240 mPlusButton.ClickedSignal().Connect(this, &ExampleController::OnButtonClicked);
242 mIndicesCountLabel = Toolkit::TextLabel::New();
243 mIndicesCountLabel.SetProperty(Actor::Property::PARENT_ORIGIN, ParentOrigin::CENTER);
244 mIndicesCountLabel.SetProperty(Actor::Property::ANCHOR_POINT, AnchorPoint::TOP_LEFT);
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);
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));
258 window.Add(modeSelectTableView);
259 window.Add(elementCountTableView);
263 * Invoked whenever the quit button is clicked
264 * @param[in] button the quit button
266 bool OnQuitButtonClicked(Toolkit::Button button)
268 // quit the application
273 void OnKeyEvent(const KeyEvent& event)
275 if(event.GetState() == KeyEvent::DOWN)
277 if(IsKey(event, Dali::DALI_KEY_ESCAPE) || IsKey(event, Dali::DALI_KEY_BACK))
284 bool OnButtonPressed(Toolkit::Button button)
287 if(button == mButtons[0])
289 mCurrentIndexCount = 10;
291 mPrimitiveType = Geometry::LINES;
294 else if(button == mButtons[1])
296 mCurrentIndexCount = 5;
298 mPrimitiveType = Geometry::LINE_LOOP;
303 mCurrentIndexCount = 6;
305 mPrimitiveType = Geometry::LINE_STRIP;
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);
318 bool OnButtonClicked(Toolkit::Button button)
320 if(button == mMinusButton)
322 if(--mCurrentIndexCount < 2)
323 mCurrentIndexCount = 2;
327 if(++mCurrentIndexCount > mMaxIndexCount)
328 mCurrentIndexCount = mMaxIndexCount;
331 std::stringstream str;
332 str << mCurrentIndexCount;
333 mIndicesCountLabel.SetProperty(Toolkit::TextLabel::Property::TEXT, str.str());
334 mRenderer.SetIndexRange(0, mCurrentIndexCount);
339 Application& mApplication; ///< Application instance
340 Vector3 mWindowSize; ///< The size of the window
346 Toolkit::RadioButton mButtons[3];
347 Toolkit::PushButton mMinusButton;
348 Toolkit::PushButton mPlusButton;
349 Toolkit::TextLabel mIndicesCountLabel;
350 Geometry::Type mPrimitiveType;
351 int mCurrentIndexCount;
355 int DALI_EXPORT_API main(int argc, char** argv)
357 Application application = Application::New(&argc, &argv);
358 ExampleController test(application);
359 application.MainLoop();