Dali 3D User Interface Engine
utc-Dali-ItemLayout.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2015 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 #include <iostream>
19 #include <stdlib.h>
20 
21 // Need to override adaptor classes for toolkit test harness, so include
22 // test harness headers before dali headers.
24 
25 #include <dali.h>
27 
28 using namespace Dali;
29 using namespace Toolkit;
30 
31 namespace
32 {
33 const unsigned int TOTAL_ITEM_NUMBER = 200;
34 const char* TEST_IMAGE_FILE_NAME = "gallery_image_01.jpg";
35 
36 
37 // Implementation of ItemFactory for providing actors to ItemView
39 {
40 public:
41 
47  {
48  }
49 
53  virtual ~TestItemFactory()
54  {
55  }
56 
57 public: // From ItemFactory
58 
63  virtual unsigned int GetNumberOfItems()
64  {
65  return TOTAL_ITEM_NUMBER;
66  }
67 
73  virtual Actor NewItem(unsigned int itemId)
74  {
75  // Create an image actor for this item
76  Image image = ResourceImage::New( TEST_IMAGE_FILE_NAME );
77  Actor actor = ImageActor::New(image);
78 
79  return actor;
80  }
81 };
82 
84 
86 
87 // Implementation of ItemLayout
88 class TestItemLayout : public ItemLayout
89 {
90 public:
91 
96  {
97  }
98 
102  virtual ~TestItemLayout()
103  {
104  }
105 
109  static TestItemLayoutPtr New()
110  {
111  return TestItemLayoutPtr(new TestItemLayout());
112  }
113 
114 public: // From ItemLayout
115 
124  virtual float GetMinimumLayoutPosition(unsigned int numberOfItems, Vector3 layoutSize) const
125  {
126  return 0.0f;
127  }
128 
137  virtual float GetClosestAnchorPosition(float layoutPosition) const
138  {
139  return 0.0f;
140  }
141 
149  virtual float GetItemScrollToPosition(unsigned int itemId) const
150  {
151  return 0.0f;
152  }
153 
161  virtual ItemRange GetItemsWithinArea(float firstItemPosition, Vector3 layoutSize) const
162  {
163  return ItemRange(0, 10);
164  }
165 
172  virtual unsigned int GetReserveItemCount(Vector3 layoutSize) const
173  {
174  return 0;
175  }
176 
184  virtual void GetDefaultItemSize( unsigned int itemId, const Vector3& layoutSize, Vector3& itemSize ) const
185  {
186  }
187 
192  virtual Degree GetScrollDirection() const
193  {
194  return Degree( 0.0f );
195  }
196 
201  virtual float GetScrollSpeedFactor() const
202  {
203  return 0;
204  }
205 
210  virtual float GetMaximumSwipeSpeed() const
211  {
212  return 0;
213  }
214 
219  virtual float GetItemFlickAnimationDuration() const
220  {
221  return 0;
222  }
223 
224  /*
225  * @brief Applies constraints defined by the layout to an actor.
226  *
227  * @param[in] actor The actor to constrain.
228  * @param[in] itemId The ID of the item represented by the actor.
229  * @param[in] layoutSize the current size of the item view instance.
230  * @param[in] itemViewActor The item view instance which requests the application of constraints.
231  */
232  virtual void ApplyConstraints( Actor& actor, const int itemId, const Vector3& layoutSize, const Actor& itemViewActor )
233  {
234  }
235 
244  virtual Vector3 GetItemPosition(int itemID, float currentLayoutPosition, const Vector3& layoutSize) const
245  {
246  return Vector3::ZERO;
247  }
248 };
249 
250 } // namespace
251 
253 {
254  ToolkitTestApplication application;
255 
256  // Create the ItemView actor
257  TestItemFactory factory;
258  ItemView view = ItemView::New(factory);
259 
260  // Create a grid layout and add it to ItemView
262  view.AddLayout(*gridLayout);
263 
264  // Set the orientation of the layout to be horizontal from left to right
265  ItemLayoutPtr layout = view.GetLayout(0);
266 
267  DALI_TEST_CHECK(gridLayout == layout);
268 
269  layout->SetOrientation(ControlOrientation::Left);
270 
271  // Check the orientation of the layout is horizontal from left to right
272  DALI_TEST_CHECK(layout->GetOrientation() == ControlOrientation::Left);
273 
274  Vector3 itemSize(100.0f, 100.0f, 100.0f);
275  layout->SetItemSize(itemSize);
276 
277  Vector3 itemSize1;
278  layout->GetItemSize(0u, Vector3(Stage::GetCurrent().GetSize()), itemSize1);
279 
280  DALI_TEST_CHECK(itemSize == itemSize1);
281 
282  float position = layout->GetClosestOnScreenLayoutPosition(0, 0.0f, Vector3(Stage::GetCurrent().GetSize()));
283 
284  DALI_TEST_EQUALS(position, 0.0f, TEST_LOCATION);
285 
286  int focusItem = layout->GetNextFocusItemID(0, TOTAL_ITEM_NUMBER, Control::KeyboardFocus::LEFT, true);
287 
288  DALI_TEST_CHECK(focusItem != 0);
289 
290  float flickSpeedFactor = layout->GetFlickSpeedFactor();
291 
292  DALI_TEST_CHECK(flickSpeedFactor != 0.0f);
293 
295  view.AddLayout(*depthLayout);
296 
297  layout = view.GetLayout(1);
298  DALI_TEST_CHECK(depthLayout == layout);
299 
301  view.AddLayout(*listLayout);
302 
303  layout = view.GetLayout(2);
304  DALI_TEST_CHECK(listLayout == layout);
305 
307  view.AddLayout(*spiralLayout);
308 
309  layout = view.GetLayout(3);
310  DALI_TEST_CHECK(spiralLayout == layout);
311  END_TEST;
312 }
313 
315 {
316  ToolkitTestApplication application;
317 
319  DALI_TEST_CHECK( gridLayout );
320  DALI_TEST_CHECK( !gridLayout->GetExtension() );
321 
322  END_TEST;
323 }
324 
326 {
327  ToolkitTestApplication application;
328 
330  DALI_TEST_CHECK( layout );
331  DALI_TEST_EQUALS(layout->GetClosestOnScreenLayoutPosition(0, 0.0f, Vector3::ZERO), 0.0f, TEST_LOCATION );
332  DALI_TEST_EQUALS(layout->GetClosestOnScreenLayoutPosition(0, 0.0f, Vector3(-800.0f, -1200.0f, 0.0f)), 0.0f, TEST_LOCATION );
333 
334  END_TEST;
335 }
336 
338 {
339  ToolkitTestApplication application;
340 
342  DALI_TEST_CHECK( layout );
343  DALI_TEST_EQUALS(layout->GetNextFocusItemID(0, 100, Control::KeyboardFocus::LEFT, true), 99, TEST_LOCATION );
344  DALI_TEST_EQUALS(layout->GetNextFocusItemID(110, 100, Control::KeyboardFocus::RIGHT, true), 0, TEST_LOCATION );
345 
346  END_TEST;
347 }
348 
350 {
351  ToolkitTestApplication application;
352 
353  unsigned int uBeginItemFirst = 100u, uEndItemFirst = 300u;
354  unsigned int uBeginItemSecond = 290u, uEndItemSecond = 400;
355  unsigned int uInterBeginCheck=290u , uInterEndCheck=301u;
356  bool bIsInThisRange = false, bOutOfThisRange = false;
357 
358  Toolkit::ItemRange objItemRangeFirst(uBeginItemFirst, uEndItemFirst);
359  Toolkit::ItemRange objItemRangeSecond(uBeginItemSecond, uEndItemSecond);
360  ItemRange itmInterSect = objItemRangeFirst.Intersection(objItemRangeSecond);
361 
362  bIsInThisRange = itmInterSect.Within(uInterBeginCheck);
363  DALI_TEST_EQUALS(bIsInThisRange, true, TEST_LOCATION );
364 
365  bOutOfThisRange = itmInterSect.Within(uInterEndCheck);
366  DALI_TEST_EQUALS(bOutOfThisRange, false, TEST_LOCATION );
367 
368  END_TEST;
369 }
Dali Docs Home
Read more about Dali