2 * Copyright (c) 2016 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 "contact-card-layouter.h"
22 #include <dali/public-api/common/stage.h>
25 #include "contact-card.h"
31 const float DEFAULT_PADDING = 25.0f;
33 const float MINIMUM_ITEMS_PER_ROW_OR_COLUMN( 3.0f );
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 );
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 );
42 const float FOLDED_TEXT_POSITION_AS_RATIO_OF_IMAGE_SIZE( 1.01f );
43 } // unnamed namespace
45 ContactCardLayouter::ContactCardLayouter()
46 : mContactCardLayoutInfo(),
49 mPositionIncrementer(),
55 ContactCardLayouter::~ContactCardLayouter()
57 // Nothing to do as ContactCardContainer uses intrusive pointers so they will be automatically deleted
60 void ContactCardLayouter::AddContact( const std::string& contactName, const std::string& contactAddress, const std::string& imagePath )
64 // Set up the common layouting info shared between all contact cards when first called
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 );
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 );
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;
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;
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;
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;
100 // Create a new contact card and add to our container
101 mContactCards.push_back( new ContactCard( mContactCardLayoutInfo, contactName, contactAddress, imagePath, NextCardPosition() ) );
104 const Vector2& ContactCardLayouter::NextCardPosition()
106 size_t currentNumOfCards = mContactCards.size();
108 if( currentNumOfCards )
110 if( currentNumOfCards % mItemsPerRow )
112 mLastPosition.x += mPositionIncrementer.x;
114 else // go to the next row
116 mLastPosition.x = mContactCardLayoutInfo.unfoldedPosition.x;
117 mLastPosition.y += mPositionIncrementer.y;
120 return mLastPosition;