2 * Copyright (c) 2020 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 "shared/view.h"
25 #include "generated/line-mesh-vert.h"
26 #include "generated/line-mesh-frag.h"
35 const unsigned short INDEX_LINES[] = {0, 1, 1, 2, 2, 3, 3, 4, 4, 0};
36 const unsigned short INDEX_LOOP[] = {0, 1, 2, 3, 4};
37 const unsigned short INDEX_STRIP[] = {0, 1, 2, 3, 4, 0};
38 const unsigned short* INDICES[3] = {&INDEX_LINES[0], &INDEX_LOOP[0], &INDEX_STRIP[0]};
39 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])};
41 Geometry CreateGeometry()
51 // Create new geometry object
52 Vertex pentagonVertexData[5] =
54 {Vector2(0.0f, 1.00f), Vector2(0.0f, -1.00f), Vector3(1.0f, 1.0f, 1.0f)}, // 0
55 {Vector2(-0.95f, 0.31f), Vector2(0.59f, 0.81f), Vector3(1.0f, 0.0f, 0.0f)}, // 1
56 {Vector2(-0.59f, -0.81f), Vector2(-0.95f, -0.31f), Vector3(0.0f, 1.0f, 0.0f)}, // 2
57 {Vector2(0.59f, -0.81f), Vector2(0.95f, -0.31f), Vector3(0.0f, 0.0f, 1.0f)}, // 3
58 {Vector2(0.95f, 0.31f), Vector2(-0.59f, 0.81f), Vector3(1.0f, 1.0f, 0.0f)}, // 4
61 Property::Map pentagonVertexFormat;
62 pentagonVertexFormat["aPosition1"] = Property::VECTOR2;
63 pentagonVertexFormat["aPosition2"] = Property::VECTOR2;
64 pentagonVertexFormat["aColor"] = Property::VECTOR3;
65 VertexBuffer pentagonVertices = VertexBuffer::New(pentagonVertexFormat);
66 pentagonVertices.SetData(pentagonVertexData, 5);
68 // Create the geometry object
69 Geometry pentagonGeometry = Geometry::New();
70 pentagonGeometry.AddVertexBuffer(pentagonVertices);
71 pentagonGeometry.SetIndexBuffer(INDICES[0], INDICES_SIZE[0]);
72 pentagonGeometry.SetType(Geometry::LINES);
73 return pentagonGeometry;
76 } // anonymous namespace
78 // This example shows how to morph between 2 meshes with the same number of
80 class ExampleController : public ConnectionTracker
84 * The example controller constructor.
85 * @param[in] application The application instance
87 ExampleController(Application& application)
88 : mApplication(application),
98 mPrimitiveType(Geometry::LINES),
99 mCurrentIndexCount(0),
102 // Connect to the Application's Init signal
103 mApplication.InitSignal().Connect(this, &ExampleController::Create);
107 * The example controller destructor
111 // Nothing to do here;
115 * Invoked upon creation of application
116 * @param[in] application The application instance
118 void Create(Application& application)
120 Window window = application.GetWindow();
123 mPrimitiveType = Geometry::LINES;
124 mCurrentIndexCount = 10;
127 CreateRadioButtons();
129 window.KeyEventSignal().Connect(this, &ExampleController::OnKeyEvent);
131 mWindowSize = window.GetSize();
135 window.SetBackgroundColor(Vector4(0.0f, 0.2f, 0.2f, 1.0f));
139 * Invoked whenever application changes the type of geometry drawn
143 Window window = mApplication.GetWindow();
145 // destroy mesh actor and its resources if already exists
148 window.Remove(mMeshActor);
152 mShader = Shader::New(SHADER_LINE_MESH_VERT, SHADER_LINE_MESH_FRAG);
153 mGeometry = CreateGeometry();
154 mRenderer = Renderer::New(mGeometry, mShader);
156 mRenderer.SetIndexRange(0, 10); // lines
157 mPrimitiveType = Geometry::LINES;
159 mMeshActor = Actor::New();
160 mMeshActor.AddRenderer(mRenderer);
161 mMeshActor.SetProperty(Actor::Property::SIZE, Vector2(200, 200));
162 mMeshActor.SetProperty(DevelActor::Property::UPDATE_SIZE_HINT, Vector2(400, 400));
164 Property::Index morphAmountIndex = mMeshActor.RegisterProperty("uMorphAmount", 0.0f);
166 mRenderer.SetProperty(Renderer::Property::DEPTH_INDEX, 0);
168 mMeshActor.SetProperty(Actor::Property::PARENT_ORIGIN, ParentOrigin::CENTER);
169 mMeshActor.SetProperty(Actor::Property::ANCHOR_POINT, AnchorPoint::CENTER);
170 window.Add(mMeshActor);
172 Animation animation = Animation::New(5);
173 KeyFrames keyFrames = KeyFrames::New();
174 keyFrames.Add(0.0f, 0.0f);
175 keyFrames.Add(1.0f, 1.0f);
177 animation.AnimateBetween(Property(mMeshActor, morphAmountIndex), keyFrames, AlphaFunction(AlphaFunction::SIN));
178 animation.SetLooping(true);
185 void CreateRadioButtons()
187 Window window = mApplication.GetWindow();
189 Toolkit::TableView modeSelectTableView = Toolkit::TableView::New(4, 1);
190 modeSelectTableView.SetProperty(Actor::Property::PARENT_ORIGIN, ParentOrigin::TOP_LEFT);
191 modeSelectTableView.SetProperty(Actor::Property::ANCHOR_POINT, AnchorPoint::TOP_LEFT);
192 modeSelectTableView.SetFitHeight(0);
193 modeSelectTableView.SetFitHeight(1);
194 modeSelectTableView.SetFitHeight(2);
195 modeSelectTableView.SetCellPadding(Vector2(6.0f, 0.0f));
196 modeSelectTableView.SetProperty(Actor::Property::SCALE, Vector3(0.8f, 0.8f, 0.8f));
198 const char* labels[] =
204 for(int i = 0; i < 3; ++i)
206 Dali::Toolkit::RadioButton radio = Dali::Toolkit::RadioButton::New();
208 radio.SetProperty(Toolkit::Button::Property::LABEL,
210 .Add(Toolkit::Visual::Property::TYPE, Toolkit::Visual::TEXT)
211 .Add(Toolkit::TextVisual::Property::TEXT, labels[i])
212 .Add(Toolkit::TextVisual::Property::TEXT_COLOR, Vector4(0.8f, 0.8f, 0.8f, 1.0f)));
214 radio.SetProperty(Actor::Property::PARENT_ORIGIN, ParentOrigin::TOP_LEFT);
215 radio.SetProperty(Actor::Property::ANCHOR_POINT, AnchorPoint::TOP_LEFT);
216 radio.SetProperty(Toolkit::Button::Property::SELECTED, i == 0);
217 radio.PressedSignal().Connect(this, &ExampleController::OnButtonPressed);
219 modeSelectTableView.AddChild(radio, Toolkit::TableView::CellPosition(i, 0));
222 Toolkit::TableView elementCountTableView = Toolkit::TableView::New(1, 3);
223 elementCountTableView.SetCellPadding(Vector2(6.0f, 0.0f));
224 elementCountTableView.SetProperty(Actor::Property::PARENT_ORIGIN, ParentOrigin::BOTTOM_LEFT);
225 elementCountTableView.SetProperty(Actor::Property::ANCHOR_POINT, AnchorPoint::BOTTOM_LEFT);
226 elementCountTableView.SetFitHeight(0);
227 elementCountTableView.SetFitWidth(0);
228 elementCountTableView.SetFitWidth(1);
229 elementCountTableView.SetFitWidth(2);
230 mMinusButton = Toolkit::PushButton::New();
231 mMinusButton.SetProperty(Toolkit::Button::Property::LABEL, "<<");
232 mMinusButton.SetProperty(Actor::Property::PARENT_ORIGIN, ParentOrigin::TOP_LEFT);
233 mMinusButton.SetProperty(Actor::Property::ANCHOR_POINT, AnchorPoint::CENTER_LEFT);
235 Toolkit::PushButton mPlusButton = Toolkit::PushButton::New();
236 mPlusButton.SetProperty(Toolkit::Button::Property::LABEL, ">>");
237 mPlusButton.SetProperty(Actor::Property::PARENT_ORIGIN, ParentOrigin::TOP_LEFT);
238 mPlusButton.SetProperty(Actor::Property::ANCHOR_POINT, AnchorPoint::CENTER_RIGHT);
240 mMinusButton.ClickedSignal().Connect(this, &ExampleController::OnButtonClicked);
241 mPlusButton.ClickedSignal().Connect(this, &ExampleController::OnButtonClicked);
243 mIndicesCountLabel = Toolkit::TextLabel::New();
244 mIndicesCountLabel.SetProperty(Actor::Property::PARENT_ORIGIN, ParentOrigin::CENTER);
245 mIndicesCountLabel.SetProperty(Actor::Property::ANCHOR_POINT, AnchorPoint::TOP_LEFT);
247 std::stringstream str;
248 str << mCurrentIndexCount;
249 mIndicesCountLabel.SetProperty(Toolkit::TextLabel::Property::TEXT, str.str());
250 mIndicesCountLabel.SetProperty(Toolkit::TextLabel::Property::TEXT_COLOR, Vector4(1.0, 1.0, 1.0, 1.0));
251 mIndicesCountLabel.SetProperty(Toolkit::TextLabel::Property::VERTICAL_ALIGNMENT, "BOTTOM");
252 mIndicesCountLabel.SetResizePolicy(ResizePolicy::USE_NATURAL_SIZE, Dimension::WIDTH);
253 mIndicesCountLabel.SetResizePolicy(ResizePolicy::USE_NATURAL_SIZE, Dimension::HEIGHT);
255 elementCountTableView.AddChild(mMinusButton, Toolkit::TableView::CellPosition(0, 0));
256 elementCountTableView.AddChild(mIndicesCountLabel, Toolkit::TableView::CellPosition(0, 1));
257 elementCountTableView.AddChild(mPlusButton, Toolkit::TableView::CellPosition(0, 2));
259 window.Add(modeSelectTableView);
260 window.Add(elementCountTableView);
264 * Invoked whenever the quit button is clicked
265 * @param[in] button the quit button
267 bool OnQuitButtonClicked(Toolkit::Button button)
269 // quit the application
274 void OnKeyEvent(const KeyEvent& event)
276 if(event.GetState() == KeyEvent::DOWN)
278 if(IsKey(event, Dali::DALI_KEY_ESCAPE) || IsKey(event, Dali::DALI_KEY_BACK))
285 bool OnButtonPressed(Toolkit::Button button)
288 if(button == mButtons[0])
290 mCurrentIndexCount = 10;
292 mPrimitiveType = Geometry::LINES;
295 else if(button == mButtons[1])
297 mCurrentIndexCount = 5;
299 mPrimitiveType = Geometry::LINE_LOOP;
304 mCurrentIndexCount = 6;
306 mPrimitiveType = Geometry::LINE_STRIP;
310 std::stringstream str;
311 str << mCurrentIndexCount;
312 mIndicesCountLabel.SetProperty(Toolkit::TextLabel::Property::TEXT, str.str());
313 mGeometry.SetType(mPrimitiveType);
314 mGeometry.SetIndexBuffer(INDICES[indicesArray], INDICES_SIZE[indicesArray]);
315 mRenderer.SetIndexRange(0, mCurrentIndexCount);
319 bool OnButtonClicked(Toolkit::Button button)
321 if(button == mMinusButton)
323 if(--mCurrentIndexCount < 2)
324 mCurrentIndexCount = 2;
328 if(++mCurrentIndexCount > mMaxIndexCount)
329 mCurrentIndexCount = mMaxIndexCount;
332 std::stringstream str;
333 str << mCurrentIndexCount;
334 mIndicesCountLabel.SetProperty(Toolkit::TextLabel::Property::TEXT, str.str());
335 mRenderer.SetIndexRange(0, mCurrentIndexCount);
340 Application& mApplication; ///< Application instance
341 Vector3 mWindowSize; ///< The size of the window
347 Toolkit::RadioButton mButtons[3];
348 Toolkit::PushButton mMinusButton;
349 Toolkit::PushButton mPlusButton;
350 Toolkit::TextLabel mIndicesCountLabel;
351 Geometry::Type mPrimitiveType;
352 int mCurrentIndexCount;
356 int DALI_EXPORT_API main(int argc, char** argv)
358 Application application = Application::New(&argc, &argv);
359 ExampleController test(application);
360 application.MainLoop();