2 using System.Collections.Generic;
5 using System.Threading.Tasks;
6 using Tizen.NUI.BaseComponents;
8 namespace Tizen.NUI.Samples
10 public class ContactCardLayouter
12 private const float DEFAULT_PADDING = 25.0f;
14 private const float MINIMUM_ITEMS_PER_ROW_OR_COLUMN = 3.0f;
16 private const float HEADER_HEIGHT_TO_UNFOLDED_SIZE_RATIO = 0.1f;
17 private readonly Vector2 HEADER_FOLDED_POSITION_AS_RATIO_OF_SIZE = new Vector2(-0.05f, -1.5f);
18 private readonly Vector2 HEADER_UNFOLDED_POSITION = new Vector2(0.0f, 0.0f);
20 const float IMAGE_SIZE_AS_RATIO_TO_FOLDED_SIZE = 0.5f;
21 private readonly Vector2 IMAGE_FOLDED_POSITION_AS_RATIO_OF_SIZE = new Vector2(0.5f, 0.25f);
23 const float FOLDED_TEXT_POSITION_AS_RATIO_OF_IMAGE_SIZE = 1.01f;
25 private readonly Vector4 ROOTVIEW_COLOR = new Vector4(211.0f / 255.0f, 211.0f / 255.0f, 211.0f / 255.0f, 1.0f);
28 private ContactCardLayoutInfo mContactCardLayoutInfo;
29 private List<ContactCard> mContactCards;
31 private Vector2 mLastPosition;
32 private Vector2 mPositionIncrementer;
33 private int mItemsPerRow;
35 private bool mInitialized;
37 private View rootView;
39 public ContactCardLayouter()
44 private void Initialize()
46 mContactCardLayoutInfo = new ContactCardLayoutInfo();
47 mContactCards = new List<ContactCard>();
48 mLastPosition = new Vector2();
49 mPositionIncrementer = new Vector2();
55 Size2D = new Size2D(Window.Instance.WindowSize.Width, Window.Instance.WindowSize.Height),
56 BackgroundColor = ROOTVIEW_COLOR,
57 PositionUsesPivotPoint = false,
60 Window.Instance.Add(rootView);
64 public void AddContact(string contactName, string contactAddress, string imagePath)
68 // Set up the common layouting info shared between all contact cards when first called
69 mContactCardLayoutInfo.unfoldedPosition = mContactCardLayoutInfo.padding = new Vector2(DEFAULT_PADDING, DEFAULT_PADDING);
70 Vector2 windowSize = new Vector2(Window.Instance.Size.Width, Window.Instance.Size.Height);
71 mContactCardLayoutInfo.unfoldedSize = windowSize - mContactCardLayoutInfo.padding * (MINIMUM_ITEMS_PER_ROW_OR_COLUMN - 1.0f);
73 // Calculate the size of the folded card (use the minimum of width/height as size)
74 mContactCardLayoutInfo.foldedSize = (mContactCardLayoutInfo.unfoldedSize - (mContactCardLayoutInfo.padding * (MINIMUM_ITEMS_PER_ROW_OR_COLUMN - 1.0f))) / MINIMUM_ITEMS_PER_ROW_OR_COLUMN;
75 mContactCardLayoutInfo.foldedSize.Width = mContactCardLayoutInfo.foldedSize.Height = Math.Min(mContactCardLayoutInfo.foldedSize.Width, mContactCardLayoutInfo.foldedSize.Height);
77 // Set the size and positions of the header
78 mContactCardLayoutInfo.headerSize.Width = mContactCardLayoutInfo.unfoldedSize.Width;
79 mContactCardLayoutInfo.headerSize.Height = mContactCardLayoutInfo.unfoldedSize.Height * HEADER_HEIGHT_TO_UNFOLDED_SIZE_RATIO;
80 mContactCardLayoutInfo.headerFoldedPosition = mContactCardLayoutInfo.headerSize * HEADER_FOLDED_POSITION_AS_RATIO_OF_SIZE;
81 mContactCardLayoutInfo.headerUnfoldedPosition = HEADER_UNFOLDED_POSITION;
83 // Set the image size and positions
84 mContactCardLayoutInfo.imageSize = mContactCardLayoutInfo.foldedSize * IMAGE_SIZE_AS_RATIO_TO_FOLDED_SIZE;
85 mContactCardLayoutInfo.imageFoldedPosition = mContactCardLayoutInfo.imageSize * IMAGE_FOLDED_POSITION_AS_RATIO_OF_SIZE;
86 mContactCardLayoutInfo.imageUnfoldedPosition.X = mContactCardLayoutInfo.padding.Width;
87 mContactCardLayoutInfo.imageUnfoldedPosition.Y = mContactCardLayoutInfo.headerSize.Height + mContactCardLayoutInfo.padding.Height;
89 // Set the positions of the contact name
90 mContactCardLayoutInfo.textFoldedPosition.X = 0.0f;
91 mContactCardLayoutInfo.textFoldedPosition.Y = mContactCardLayoutInfo.imageFoldedPosition.X + mContactCardLayoutInfo.imageSize.Height * FOLDED_TEXT_POSITION_AS_RATIO_OF_IMAGE_SIZE;
92 mContactCardLayoutInfo.textUnfoldedPosition.X = mContactCardLayoutInfo.padding.Width;
93 mContactCardLayoutInfo.textUnfoldedPosition.Y = mContactCardLayoutInfo.imageUnfoldedPosition.Y + mContactCardLayoutInfo.imageSize.Height + mContactCardLayoutInfo.padding.Height;
95 // Figure out the positions of the contact cards
96 mItemsPerRow = (int)((mContactCardLayoutInfo.unfoldedSize.Width + mContactCardLayoutInfo.padding.Width) / (mContactCardLayoutInfo.foldedSize.Width + mContactCardLayoutInfo.padding.Width));
97 mLastPosition = new Vector2(mContactCardLayoutInfo.unfoldedPosition.X, mContactCardLayoutInfo.unfoldedPosition.Y);
98 mPositionIncrementer.X = mContactCardLayoutInfo.foldedSize.Width + mContactCardLayoutInfo.padding.Width;
99 mPositionIncrementer.Y = mContactCardLayoutInfo.foldedSize.Height + mContactCardLayoutInfo.padding.Height;
104 mContactCards.Add(new ContactCard(mContactCardLayoutInfo, contactName, contactAddress, imagePath, NextCardPosition(), rootView));
108 private Vector2 NextCardPosition()
110 int currentNumOfCards = mContactCards.Count();
112 if(currentNumOfCards > 0)
114 if(currentNumOfCards % mItemsPerRow != 0)
116 mLastPosition.X += mPositionIncrementer.X;
120 mLastPosition.X = mContactCardLayoutInfo.unfoldedPosition.X;
121 mLastPosition.Y += mPositionIncrementer.Y;
125 return mLastPosition;