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 "contact-card-layouter.h"
22 #include "contact-card.h"
28 const float DEFAULT_PADDING = 25.0f;
30 const float MINIMUM_ITEMS_PER_ROW_OR_COLUMN(3.0f);
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);
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);
39 const float FOLDED_TEXT_POSITION_AS_RATIO_OF_IMAGE_SIZE(1.01f);
40 } // unnamed namespace
42 ContactCardLayouter::ContactCardLayouter()
43 : mContactCardLayoutInfo(),
46 mPositionIncrementer(),
52 ContactCardLayouter::~ContactCardLayouter()
54 // Nothing to do as ContactCardContainer uses intrusive pointers so they will be automatically deleted
57 void ContactCardLayouter::AddContact(Dali::Window window, const std::string& contactName, const std::string& contactAddress, const std::string& imagePath)
61 // Set up the common layouting info shared between all contact cards when first called
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);
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);
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;
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;
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;
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;
97 // Create a new contact card and add to our container
98 mContactCards.push_back(new ContactCard(window, mContactCardLayoutInfo, contactName, contactAddress, imagePath, NextCardPosition()));
101 const Vector2& ContactCardLayouter::NextCardPosition()
103 size_t currentNumOfCards = mContactCards.size();
105 if(currentNumOfCards)
107 if(currentNumOfCards % mItemsPerRow)
109 mLastPosition.x += mPositionIncrementer.x;
111 else // go to the next row
113 mLastPosition.x = mContactCardLayoutInfo.unfoldedPosition.x;
114 mLastPosition.y += mPositionIncrementer.y;
117 return mLastPosition;