DALi Version 1.4.27
[platform/core/uifw/dali-demo.git] / examples / contact-cards / contact-card-layouter.cpp
1 /*
2  * Copyright (c) 2016 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 // CLASS HEADER
19 #include "contact-card-layouter.h"
20
21 // EXTERNAL INCLUDES
22 #include <dali/public-api/common/stage.h>
23
24 // INTERNAL INCLUDES
25 #include "contact-card.h"
26
27 using namespace Dali;
28
29 namespace
30 {
31 const float DEFAULT_PADDING = 25.0f;
32
33 const float MINIMUM_ITEMS_PER_ROW_OR_COLUMN( 3.0f );
34
35 const float HEADER_HEIGHT_TO_UNFOLDED_SIZE_RATIO( 0.1f );
36 const Vector2 HEADER_FOLDED_POSITION_AS_RATIO_OF_SIZE( -0.05f, -1.5f );
37 const Vector2 HEADER_UNFOLDED_POSITION( Vector2::ZERO );
38
39 const float IMAGE_SIZE_AS_RATIO_TO_FOLDED_SIZE( 0.5f );
40 const Vector2 IMAGE_FOLDED_POSITION_AS_RATIO_OF_SIZE( 0.5f, 0.25f );
41
42 const float FOLDED_TEXT_POSITION_AS_RATIO_OF_IMAGE_SIZE( 1.01f );
43 } // unnamed namespace
44
45 ContactCardLayouter::ContactCardLayouter()
46 : mContactCardLayoutInfo(),
47   mContactCards(),
48   mLastPosition(),
49   mPositionIncrementer(),
50   mItemsPerRow( 0 ),
51   mInitialized( false )
52 {
53 }
54
55 ContactCardLayouter::~ContactCardLayouter()
56 {
57   // Nothing to do as ContactCardContainer uses intrusive pointers so they will be automatically deleted
58 }
59
60 void ContactCardLayouter::AddContact( const std::string& contactName, const std::string& contactAddress, const std::string& imagePath )
61 {
62   if( ! mInitialized )
63   {
64     // Set up the common layouting info shared between all contact cards when first called
65
66     mContactCardLayoutInfo.unfoldedPosition = mContactCardLayoutInfo.padding = Vector2( DEFAULT_PADDING, DEFAULT_PADDING );
67     mContactCardLayoutInfo.unfoldedSize = Stage::GetCurrent().GetSize() - mContactCardLayoutInfo.padding * ( MINIMUM_ITEMS_PER_ROW_OR_COLUMN - 1.0f );
68
69     // Calculate the size of the folded card (use the minimum of width/height as size)
70     mContactCardLayoutInfo.foldedSize = ( mContactCardLayoutInfo.unfoldedSize - ( mContactCardLayoutInfo.padding * ( MINIMUM_ITEMS_PER_ROW_OR_COLUMN - 1.0f ) ) ) / MINIMUM_ITEMS_PER_ROW_OR_COLUMN;
71     mContactCardLayoutInfo.foldedSize.width = mContactCardLayoutInfo.foldedSize.height = std::min( mContactCardLayoutInfo.foldedSize.width, mContactCardLayoutInfo.foldedSize.height );
72
73     // Set the size and positions of the header
74     mContactCardLayoutInfo.headerSize.width = mContactCardLayoutInfo.unfoldedSize.width;
75     mContactCardLayoutInfo.headerSize.height = mContactCardLayoutInfo.unfoldedSize.height * HEADER_HEIGHT_TO_UNFOLDED_SIZE_RATIO;
76     mContactCardLayoutInfo.headerFoldedPosition = mContactCardLayoutInfo.headerSize * HEADER_FOLDED_POSITION_AS_RATIO_OF_SIZE;
77     mContactCardLayoutInfo.headerUnfoldedPosition = HEADER_UNFOLDED_POSITION;
78
79     // Set the image size and positions
80     mContactCardLayoutInfo.imageSize = mContactCardLayoutInfo.foldedSize * IMAGE_SIZE_AS_RATIO_TO_FOLDED_SIZE;
81     mContactCardLayoutInfo.imageFoldedPosition = mContactCardLayoutInfo.imageSize * IMAGE_FOLDED_POSITION_AS_RATIO_OF_SIZE;
82     mContactCardLayoutInfo.imageUnfoldedPosition.x = mContactCardLayoutInfo.padding.width;
83     mContactCardLayoutInfo.imageUnfoldedPosition.y = mContactCardLayoutInfo.headerSize.height + mContactCardLayoutInfo.padding.height;
84
85     // Set the positions of the contact name
86     mContactCardLayoutInfo.textFoldedPosition.x = 0.0f;
87     mContactCardLayoutInfo.textFoldedPosition.y = mContactCardLayoutInfo.imageFoldedPosition.x + mContactCardLayoutInfo.imageSize.height * FOLDED_TEXT_POSITION_AS_RATIO_OF_IMAGE_SIZE;
88     mContactCardLayoutInfo.textUnfoldedPosition.x = mContactCardLayoutInfo.padding.width;
89     mContactCardLayoutInfo.textUnfoldedPosition.y = mContactCardLayoutInfo.imageUnfoldedPosition.y + mContactCardLayoutInfo.imageSize.height + mContactCardLayoutInfo.padding.height;
90
91     // Figure out the positions of the contact cards
92     mItemsPerRow = ( mContactCardLayoutInfo.unfoldedSize.width + mContactCardLayoutInfo.padding.width ) / ( mContactCardLayoutInfo.foldedSize.width + mContactCardLayoutInfo.padding.width );
93     mLastPosition = mContactCardLayoutInfo.unfoldedPosition;
94     mPositionIncrementer.x = mContactCardLayoutInfo.foldedSize.width + mContactCardLayoutInfo.padding.width;
95     mPositionIncrementer.y = mContactCardLayoutInfo.foldedSize.height + mContactCardLayoutInfo.padding.height;
96
97     mInitialized = true;
98   }
99
100   // Create a new contact card and add to our container
101   mContactCards.push_back( new ContactCard( mContactCardLayoutInfo, contactName, contactAddress, imagePath, NextCardPosition() ) );
102 }
103
104 const Vector2& ContactCardLayouter::NextCardPosition()
105 {
106   size_t currentNumOfCards = mContactCards.size();
107
108   if( currentNumOfCards )
109   {
110     if( currentNumOfCards % mItemsPerRow )
111     {
112       mLastPosition.x += mPositionIncrementer.x;
113     }
114     else // go to the next row
115     {
116       mLastPosition.x = mContactCardLayoutInfo.unfoldedPosition.x;
117       mLastPosition.y += mPositionIncrementer.y;
118     }
119   }
120   return mLastPosition;
121 }