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"
32 #define MAKE_SHADER(A) #A
34 const char* VERTEX_SHADER = MAKE_SHADER(
35 attribute mediump vec2 aPosition1;
36 attribute mediump vec2 aPosition2;
37 attribute lowp vec3 aColor;
38 uniform mediump mat4 uMvpMatrix;
39 uniform mediump vec3 uSize;
40 uniform mediump float uMorphAmount;
42 varying lowp vec3 vColor;
45 mediump vec2 morphPosition = mix(aPosition1, aPosition2, uMorphAmount);
46 mediump vec4 vertexPosition = vec4(morphPosition, 0.0, 1.0);
48 vertexPosition.xyz *= uSize;
49 vertexPosition = uMvpMatrix * vertexPosition;
50 gl_Position = vertexPosition;
53 const char* FRAGMENT_SHADER = MAKE_SHADER(
54 uniform lowp vec4 uColor;
55 uniform sampler2D sTexture;
57 varying lowp vec3 vColor;
60 gl_FragColor = uColor * vec4(vColor, 1.0);
63 const unsigned short INDEX_LINES[] = {0, 1, 1, 2, 2, 3, 3, 4, 4, 0};
64 const unsigned short INDEX_LOOP[] = {0, 1, 2, 3, 4};
65 const unsigned short INDEX_STRIP[] = {0, 1, 2, 3, 4, 0};
66 const unsigned short* INDICES[3] = {&INDEX_LINES[0], &INDEX_LOOP[0], &INDEX_STRIP[0]};
67 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])};
69 Geometry CreateGeometry()
79 // Create new geometry object
80 Vertex pentagonVertexData[5] =
82 {Vector2(0.0f, 1.00f), Vector2(0.0f, -1.00f), Vector3(1.0f, 1.0f, 1.0f)}, // 0
83 {Vector2(-0.95f, 0.31f), Vector2(0.59f, 0.81f), Vector3(1.0f, 0.0f, 0.0f)}, // 1
84 {Vector2(-0.59f, -0.81f), Vector2(-0.95f, -0.31f), Vector3(0.0f, 1.0f, 0.0f)}, // 2
85 {Vector2(0.59f, -0.81f), Vector2(0.95f, -0.31f), Vector3(0.0f, 0.0f, 1.0f)}, // 3
86 {Vector2(0.95f, 0.31f), Vector2(-0.59f, 0.81f), Vector3(1.0f, 1.0f, 0.0f)}, // 4
89 Property::Map pentagonVertexFormat;
90 pentagonVertexFormat["aPosition1"] = Property::VECTOR2;
91 pentagonVertexFormat["aPosition2"] = Property::VECTOR2;
92 pentagonVertexFormat["aColor"] = Property::VECTOR3;
93 VertexBuffer pentagonVertices = VertexBuffer::New(pentagonVertexFormat);
94 pentagonVertices.SetData(pentagonVertexData, 5);
96 // Create the geometry object
97 Geometry pentagonGeometry = Geometry::New();
98 pentagonGeometry.AddVertexBuffer(pentagonVertices);
99 pentagonGeometry.SetIndexBuffer(INDICES[0], INDICES_SIZE[0]);
100 pentagonGeometry.SetType(Geometry::LINES);
101 return pentagonGeometry;
104 } // anonymous namespace
106 // This example shows how to morph between 2 meshes with the same number of
108 class ExampleController : public ConnectionTracker
112 * The example controller constructor.
113 * @param[in] application The application instance
115 ExampleController(Application& application)
116 : mApplication(application),
125 mIndicesCountLabel(),
126 mPrimitiveType(Geometry::LINES),
127 mCurrentIndexCount(0),
130 // Connect to the Application's Init signal
131 mApplication.InitSignal().Connect(this, &ExampleController::Create);
135 * The example controller destructor
139 // Nothing to do here;
143 * Invoked upon creation of application
144 * @param[in] application The application instance
146 void Create(Application& application)
148 Window window = application.GetWindow();
151 mPrimitiveType = Geometry::LINES;
152 mCurrentIndexCount = 10;
155 CreateRadioButtons();
157 window.KeyEventSignal().Connect(this, &ExampleController::OnKeyEvent);
159 mWindowSize = window.GetSize();
163 window.SetBackgroundColor(Vector4(0.0f, 0.2f, 0.2f, 1.0f));
167 * Invoked whenever application changes the type of geometry drawn
171 Window window = mApplication.GetWindow();
173 // destroy mesh actor and its resources if already exists
176 window.Remove(mMeshActor);
180 mShader = Shader::New(VERTEX_SHADER, FRAGMENT_SHADER);
181 mGeometry = CreateGeometry();
182 mRenderer = Renderer::New(mGeometry, mShader);
184 mRenderer.SetIndexRange(0, 10); // lines
185 mPrimitiveType = Geometry::LINES;
187 mMeshActor = Actor::New();
188 mMeshActor.AddRenderer(mRenderer);
189 mMeshActor.SetProperty(Actor::Property::SIZE, Vector2(200, 200));
190 mMeshActor.SetProperty(DevelActor::Property::UPDATE_SIZE_HINT, Vector2(400, 400));
192 Property::Index morphAmountIndex = mMeshActor.RegisterProperty("uMorphAmount", 0.0f);
194 mRenderer.SetProperty(Renderer::Property::DEPTH_INDEX, 0);
196 mMeshActor.SetProperty(Actor::Property::PARENT_ORIGIN, ParentOrigin::CENTER);
197 mMeshActor.SetProperty(Actor::Property::ANCHOR_POINT, AnchorPoint::CENTER);
198 window.Add(mMeshActor);
200 Animation animation = Animation::New(5);
201 KeyFrames keyFrames = KeyFrames::New();
202 keyFrames.Add(0.0f, 0.0f);
203 keyFrames.Add(1.0f, 1.0f);
205 animation.AnimateBetween(Property(mMeshActor, morphAmountIndex), keyFrames, AlphaFunction(AlphaFunction::SIN));
206 animation.SetLooping(true);
213 void CreateRadioButtons()
215 Window window = mApplication.GetWindow();
217 Toolkit::TableView modeSelectTableView = Toolkit::TableView::New(4, 1);
218 modeSelectTableView.SetProperty(Actor::Property::PARENT_ORIGIN, ParentOrigin::TOP_LEFT);
219 modeSelectTableView.SetProperty(Actor::Property::ANCHOR_POINT, AnchorPoint::TOP_LEFT);
220 modeSelectTableView.SetFitHeight(0);
221 modeSelectTableView.SetFitHeight(1);
222 modeSelectTableView.SetFitHeight(2);
223 modeSelectTableView.SetCellPadding(Vector2(6.0f, 0.0f));
224 modeSelectTableView.SetProperty(Actor::Property::SCALE, Vector3(0.8f, 0.8f, 0.8f));
226 const char* labels[] =
232 for(int i = 0; i < 3; ++i)
234 Dali::Toolkit::RadioButton radio = Dali::Toolkit::RadioButton::New();
236 radio.SetProperty(Toolkit::Button::Property::LABEL,
238 .Add(Toolkit::Visual::Property::TYPE, Toolkit::Visual::TEXT)
239 .Add(Toolkit::TextVisual::Property::TEXT, labels[i])
240 .Add(Toolkit::TextVisual::Property::TEXT_COLOR, Vector4(0.8f, 0.8f, 0.8f, 1.0f)));
242 radio.SetProperty(Actor::Property::PARENT_ORIGIN, ParentOrigin::TOP_LEFT);
243 radio.SetProperty(Actor::Property::ANCHOR_POINT, AnchorPoint::TOP_LEFT);
244 radio.SetProperty(Toolkit::Button::Property::SELECTED, i == 0);
245 radio.PressedSignal().Connect(this, &ExampleController::OnButtonPressed);
247 modeSelectTableView.AddChild(radio, Toolkit::TableView::CellPosition(i, 0));
250 Toolkit::TableView elementCountTableView = Toolkit::TableView::New(1, 3);
251 elementCountTableView.SetCellPadding(Vector2(6.0f, 0.0f));
252 elementCountTableView.SetProperty(Actor::Property::PARENT_ORIGIN, ParentOrigin::BOTTOM_LEFT);
253 elementCountTableView.SetProperty(Actor::Property::ANCHOR_POINT, AnchorPoint::BOTTOM_LEFT);
254 elementCountTableView.SetFitHeight(0);
255 elementCountTableView.SetFitWidth(0);
256 elementCountTableView.SetFitWidth(1);
257 elementCountTableView.SetFitWidth(2);
258 mMinusButton = Toolkit::PushButton::New();
259 mMinusButton.SetProperty(Toolkit::Button::Property::LABEL, "<<");
260 mMinusButton.SetProperty(Actor::Property::PARENT_ORIGIN, ParentOrigin::TOP_LEFT);
261 mMinusButton.SetProperty(Actor::Property::ANCHOR_POINT, AnchorPoint::CENTER_LEFT);
263 Toolkit::PushButton mPlusButton = Toolkit::PushButton::New();
264 mPlusButton.SetProperty(Toolkit::Button::Property::LABEL, ">>");
265 mPlusButton.SetProperty(Actor::Property::PARENT_ORIGIN, ParentOrigin::TOP_LEFT);
266 mPlusButton.SetProperty(Actor::Property::ANCHOR_POINT, AnchorPoint::CENTER_RIGHT);
268 mMinusButton.ClickedSignal().Connect(this, &ExampleController::OnButtonClicked);
269 mPlusButton.ClickedSignal().Connect(this, &ExampleController::OnButtonClicked);
271 mIndicesCountLabel = Toolkit::TextLabel::New();
272 mIndicesCountLabel.SetProperty(Actor::Property::PARENT_ORIGIN, ParentOrigin::CENTER);
273 mIndicesCountLabel.SetProperty(Actor::Property::ANCHOR_POINT, AnchorPoint::TOP_LEFT);
275 std::stringstream str;
276 str << mCurrentIndexCount;
277 mIndicesCountLabel.SetProperty(Toolkit::TextLabel::Property::TEXT, str.str());
278 mIndicesCountLabel.SetProperty(Toolkit::TextLabel::Property::TEXT_COLOR, Vector4(1.0, 1.0, 1.0, 1.0));
279 mIndicesCountLabel.SetProperty(Toolkit::TextLabel::Property::VERTICAL_ALIGNMENT, "BOTTOM");
280 mIndicesCountLabel.SetResizePolicy(ResizePolicy::USE_NATURAL_SIZE, Dimension::WIDTH);
281 mIndicesCountLabel.SetResizePolicy(ResizePolicy::USE_NATURAL_SIZE, Dimension::HEIGHT);
283 elementCountTableView.AddChild(mMinusButton, Toolkit::TableView::CellPosition(0, 0));
284 elementCountTableView.AddChild(mIndicesCountLabel, Toolkit::TableView::CellPosition(0, 1));
285 elementCountTableView.AddChild(mPlusButton, Toolkit::TableView::CellPosition(0, 2));
287 window.Add(modeSelectTableView);
288 window.Add(elementCountTableView);
292 * Invoked whenever the quit button is clicked
293 * @param[in] button the quit button
295 bool OnQuitButtonClicked(Toolkit::Button button)
297 // quit the application
302 void OnKeyEvent(const KeyEvent& event)
304 if(event.GetState() == KeyEvent::DOWN)
306 if(IsKey(event, Dali::DALI_KEY_ESCAPE) || IsKey(event, Dali::DALI_KEY_BACK))
313 bool OnButtonPressed(Toolkit::Button button)
316 if(button == mButtons[0])
318 mCurrentIndexCount = 10;
320 mPrimitiveType = Geometry::LINES;
323 else if(button == mButtons[1])
325 mCurrentIndexCount = 5;
327 mPrimitiveType = Geometry::LINE_LOOP;
332 mCurrentIndexCount = 6;
334 mPrimitiveType = Geometry::LINE_STRIP;
338 std::stringstream str;
339 str << mCurrentIndexCount;
340 mIndicesCountLabel.SetProperty(Toolkit::TextLabel::Property::TEXT, str.str());
341 mGeometry.SetType(mPrimitiveType);
342 mGeometry.SetIndexBuffer(INDICES[indicesArray], INDICES_SIZE[indicesArray]);
343 mRenderer.SetIndexRange(0, mCurrentIndexCount);
347 bool OnButtonClicked(Toolkit::Button button)
349 if(button == mMinusButton)
351 if(--mCurrentIndexCount < 2)
352 mCurrentIndexCount = 2;
356 if(++mCurrentIndexCount > mMaxIndexCount)
357 mCurrentIndexCount = mMaxIndexCount;
360 std::stringstream str;
361 str << mCurrentIndexCount;
362 mIndicesCountLabel.SetProperty(Toolkit::TextLabel::Property::TEXT, str.str());
363 mRenderer.SetIndexRange(0, mCurrentIndexCount);
368 Application& mApplication; ///< Application instance
369 Vector3 mWindowSize; ///< The size of the window
375 Toolkit::RadioButton mButtons[3];
376 Toolkit::PushButton mMinusButton;
377 Toolkit::PushButton mPlusButton;
378 Toolkit::TextLabel mIndicesCountLabel;
379 Geometry::Type mPrimitiveType;
380 int mCurrentIndexCount;
384 int DALI_EXPORT_API main(int argc, char** argv)
386 Application application = Application::New(&argc, &argv);
387 ExampleController test(application);
388 application.MainLoop();