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