[NUI] NUI samples (#1225) (#1226)
[platform/core/csapi/tizenfx.git] / test / Tizen.NUI.Samples / Tizen.NUI.Samples / Samples / ContactCard / ContactCardLayouter.cs
1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Text;
5 using System.Threading.Tasks;
6 using Tizen.NUI.BaseComponents;
7
8 namespace Tizen.NUI.Samples
9 {
10     public class ContactCardLayouter
11     {
12         private const float DEFAULT_PADDING = 25.0f;
13
14         private const float MINIMUM_ITEMS_PER_ROW_OR_COLUMN = 3.0f;
15
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);
19
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);
22
23         const float FOLDED_TEXT_POSITION_AS_RATIO_OF_IMAGE_SIZE = 1.01f;
24
25         private readonly Vector4 ROOTVIEW_COLOR = new Vector4(211.0f / 255.0f, 211.0f / 255.0f, 211.0f / 255.0f, 1.0f);
26
27
28         private ContactCardLayoutInfo mContactCardLayoutInfo;
29         private List<ContactCard> mContactCards;
30
31         private Vector2 mLastPosition;
32         private Vector2 mPositionIncrementer;
33         private int mItemsPerRow;
34
35         private bool mInitialized;
36
37         private View rootView;
38
39         public ContactCardLayouter()
40         {
41             Initialize();
42         }
43
44         private void Initialize()
45         {
46             mContactCardLayoutInfo = new ContactCardLayoutInfo();
47             mContactCards = new List<ContactCard>();
48             mLastPosition = new Vector2();
49             mPositionIncrementer = new Vector2();
50             mItemsPerRow = 0;
51             mInitialized = false;
52
53             rootView = new View()
54             {
55                 Size2D = new Size2D(Window.Instance.WindowSize.Width, Window.Instance.WindowSize.Height),
56                 BackgroundColor = ROOTVIEW_COLOR,
57                 PositionUsesPivotPoint = false,
58             };
59
60             Window.Instance.Add(rootView);
61
62         }
63
64         public void AddContact(string contactName, string contactAddress, string imagePath)
65         {
66             if(!mInitialized)
67             {
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);
72
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);
76
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;
82
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;
88
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;
94
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;
100
101                 mInitialized = true;
102             }
103
104             mContactCards.Add(new ContactCard(mContactCardLayoutInfo, contactName, contactAddress, imagePath, NextCardPosition(), rootView));
105             
106         }
107
108         private Vector2 NextCardPosition()
109         {
110             int currentNumOfCards = mContactCards.Count();
111
112             if(currentNumOfCards > 0)
113             {
114                 if(currentNumOfCards % mItemsPerRow != 0)
115                 {
116                     mLastPosition.X += mPositionIncrementer.X;
117                 }
118                 else
119                 {
120                     mLastPosition.X = mContactCardLayoutInfo.unfoldedPosition.X;
121                     mLastPosition.Y += mPositionIncrementer.Y;
122                 }
123             }
124
125             return mLastPosition;
126         }
127
128     }
129 }