[NUI] Sync with dalihub (#969)
[platform/core/csapi/tizenfx.git] / src / Tizen.NUI.CommonUI / Controls / Loading.cs
1 /*
2  * Copyright(c) 2019 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 using System;
18 using System.Collections.Generic;
19 using System.ComponentModel;
20
21 namespace Tizen.NUI.CommonUI
22 {
23     /// <summary>
24     /// The Loading class of nui component. It's used to indicate informs users of the ongoing operation.
25     /// </summary>
26     /// <since_tizen> 6 </since_tizen>
27     /// This will be public opened in tizen_5.5 after ACR done. Before ACR, need to be hidden as inhouse API.
28     [EditorBrowsable(EditorBrowsableState.Never)]
29     public class Loading : Control
30     {
31         private LoadingAttributes loadingAttrs = null;  // Loading Attributes
32         private AnimatedImageVisual imageVisual = null;
33
34         /// <summary>
35         /// The constructor of Loading
36         /// </summary>
37         /// <since_tizen> 6 </since_tizen>
38         /// This will be public opened in tizen_5.5 after ACR done. Before ACR, need to be hidden as inhouse API.
39         [EditorBrowsable(EditorBrowsableState.Never)]
40         public Loading() : base()
41         {
42             Initialize();
43         }
44
45         /// <summary>
46         /// Constructor of the Loading class with special style.
47         /// </summary>
48         /// <param name="style">The string to initialize the Loading.</param>
49         /// <since_tizen> 6 </since_tizen>
50         /// This will be public opened in tizen_5.5 after ACR done. Before ACR, need to be hidden as inhouse API.
51         [EditorBrowsable(EditorBrowsableState.Never)]
52         public Loading(string style) : base(style)
53         {
54             Initialize();
55         }
56
57         /// <summary>
58         /// The constructor of the Loading class with specific Attributes.
59         /// </summary>
60         /// <param name="attributes">The Attributes object to initialize the Loading.</param>
61         /// <since_tizen> 6 </since_tizen>
62         /// This will be public opened in tizen_5.5 after ACR done. Before ACR, need to be hidden as inhouse API.
63         [EditorBrowsable(EditorBrowsableState.Never)]
64         public Loading(LoadingAttributes attributes) : base(attributes)
65         {
66             Initialize();
67         }
68
69         /// <summary>
70         /// Gets or sets loading image resource array.
71         /// </summary>
72         /// <since_tizen> 6 </since_tizen>
73         /// This will be public opened in tizen_5.5 after ACR done. Before ACR, need to be hidden as inhouse API.
74         [EditorBrowsable(EditorBrowsableState.Never)]
75         public string[] ImageArray
76         {
77             get
78             {
79                 return loadingAttrs.ImageArray;
80             }
81             set
82             {
83                 if (null != value)
84                 {
85                     loadingAttrs.ImageArray = value;
86                     imageVisual.URLS = new List<string>(value);
87                 }
88             }
89         }
90
91         /// <summary>
92         /// Gets or sets loading size.
93         /// </summary>
94         /// <since_tizen> 6 </since_tizen>
95         /// This will be public opened in tizen_5.5 after ACR done. Before ACR, need to be hidden as inhouse API.
96         [EditorBrowsable(EditorBrowsableState.Never)]
97         public Size2D LoadingSize
98         {
99             get
100             {
101                 return loadingAttrs.LoadingSize ?? new Size2D(100, 100);
102             }
103             set
104             {
105                 loadingAttrs.LoadingSize = value;
106                 imageVisual.Size = value;
107             }
108         }
109
110         /// <summary>
111         /// Gets or sets FPS of loading.
112         /// </summary>
113         /// <since_tizen> 6 </since_tizen>
114         /// This will be public opened in tizen_5.5 after ACR done. Before ACR, need to be hidden as inhouse API.
115         [EditorBrowsable(EditorBrowsableState.Never)]
116         public int FPS
117         {
118             get
119             {
120                 return loadingAttrs?.FPS?.All ?? (int)(1000.0f / 16.6f);
121             }
122             set
123             {
124                 if (value != 0) //It will crash if 0 
125                 {
126                     if (null == loadingAttrs.FPS)
127                     {
128                         loadingAttrs.FPS = new IntSelector();
129                     }
130                     loadingAttrs.FPS.All = value;
131                     imageVisual.FrameDelay = 1000.0f / value;
132                 }
133             }
134         }
135
136         /// <summary>
137         /// Get Loading attribues.
138         /// </summary>
139         /// <since_tizen> 6 </since_tizen>
140         /// This will be public opened in tizen_5.5 after ACR done. Before ACR, need to be hidden as inhouse API.
141         [EditorBrowsable(EditorBrowsableState.Never)]
142         protected override Attributes GetAttributes()
143         {
144             return new LoadingAttributes();
145         }
146
147         /// <summary>
148         /// Dispose Loading.
149         /// </summary>
150         /// <param name="type">Dispose type.</param>
151         /// <since_tizen> 6 </since_tizen>
152         /// This will be public opened in tizen_5.5 after ACR done. Before ACR, need to be hidden as inhouse API.
153         [EditorBrowsable(EditorBrowsableState.Never)]
154         protected override void Dispose(DisposeTypes type)
155         {
156             if (disposed)
157             {
158                 return;
159             }
160
161             if (type == DisposeTypes.Explicit)
162             {
163                 //Called by User
164                 //Release your own managed resources here.
165                 //You should release all of your own disposable objects here.
166                 RemoveVisual("loadingImageVisual");
167             }
168
169             //Release your own unmanaged resources here.
170             //You should not access any managed member here except static instance.
171             //because the execution order of Finalizes is non-deterministic.
172             //Unreference this from if a static instance refer to this. 
173
174             //You must call base.Dispose(type) just before exit.
175             base.Dispose(type);
176         }
177
178         /// <summary>
179         /// Update Loading by attributes.
180         /// </summary>
181         /// <since_tizen> 6 </since_tizen>
182         /// This will be public opened in tizen_5.5 after ACR done. Before ACR, need to be hidden as inhouse API.
183         [EditorBrowsable(EditorBrowsableState.Never)]
184         protected override void OnUpdate()
185         {
186         }
187
188         private void Initialize()
189         {
190             loadingAttrs = attributes as LoadingAttributes;
191             if (null == loadingAttrs)
192             {
193                 throw new Exception("Loading attribute parse error.");
194             }
195             ApplyAttributes(this, loadingAttrs);
196
197             imageVisual = new AnimatedImageVisual()
198             {
199                 URLS = new List<string>(),
200                 FrameDelay = 16.6f,
201                 LoopCount = -1,
202                 Size = new Size2D(100, 100),
203                 Position = new Vector2(0, 0),
204                 Origin = Visual.AlignType.Center,
205                 AnchorPoint = Visual.AlignType.Center
206             };
207
208             UpdateVisual();
209
210             this.AddVisual("loadingImageVisual", imageVisual);
211         }
212
213         private void UpdateVisual()
214         {
215             if (null != loadingAttrs.ImageArray)
216             {
217                 imageVisual.URLS = new List<string>(loadingAttrs.ImageArray);
218             }
219             if (null != loadingAttrs.FPS)
220             {
221                 imageVisual.FrameDelay = 1000.0f / (float)loadingAttrs.FPS.All;
222             }
223             if (null != loadingAttrs.LoadingSize)
224             {
225                 imageVisual.Size = loadingAttrs.LoadingSize;
226             }
227         }
228     }
229 }