9dc19ac0bec2508119fe646b24f360e4f07de78f
[platform/core/csapi/tizenfx.git] / src / Tizen.NUI / src / public / Visuals / VisualMaps.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.ComponentModel;
19 using Tizen.NUI.BaseComponents;
20
21 namespace Tizen.NUI
22 {
23     /// <summary>
24     /// A class encapsulating the transform map of the visual.
25     /// </summary>
26     /// <since_tizen> 3 </since_tizen>
27     public class VisualMap : IDisposable
28     {
29         private bool disposed = false;
30         /// <summary>
31         /// outputVisualMap.
32         /// </summary>
33         /// <since_tizen> 3 </since_tizen>
34         [Obsolete("Deprecated in API9, Will be removed in API11, Please use OutputVisualMap")]
35         [System.Diagnostics.CodeAnalysis.SuppressMessage("Design", "CA1051:Do not declare visible instance fields", Justification = "<Pending>")]
36         protected PropertyMap _outputVisualMap = null;
37
38         /// <summary>
39         /// The shader of the visual.
40         /// </summary>
41         /// <since_tizen> 3 </since_tizen>
42         [Obsolete("Deprecated in API9, Will be removed in API11, Please use Shader")]
43         [System.Diagnostics.CodeAnalysis.SuppressMessage("Design", "CA1051:Do not declare visible instance fields", Justification = "<Pending>")]
44         protected PropertyMap _shader = null;
45         //private PropertyMap _transform = null;
46
47         /// <summary>
48         /// The premultipliedAlpha of the visual.
49         /// </summary>
50         /// <since_tizen> 3 </since_tizen>
51         [Obsolete("Deprecated in API9, Will be removed in API11, Please use PremultipliedAlpha")]
52         [System.Diagnostics.CodeAnalysis.SuppressMessage("Design", "CA1051:Do not declare visible instance fields", Justification = "<Pending>")]
53         protected bool? _premultipliedAlpha = null;
54
55         /// <summary>
56         /// The mixColor of the Visual.
57         /// </summary>
58         /// <since_tizen> 3 </since_tizen>
59         [Obsolete("Deprecated in API9, Will be removed in API11, Please use MixColor")]
60         [System.Diagnostics.CodeAnalysis.SuppressMessage("Design", "CA1051:Do not declare visible instance fields", Justification = "<Pending>")]
61         protected Color _mixColor = null;
62
63         /// <summary>
64         /// The opacity of the visual.
65         /// </summary>
66         /// <since_tizen> 3 </since_tizen>
67         [Obsolete("Deprecated in API9, Will be removed in API11, Please use Opacity")]
68         [System.Diagnostics.CodeAnalysis.SuppressMessage("Design", "CA1051:Do not declare visible instance fields", Justification = "<Pending>")]
69         protected float? _opacity = null;
70
71         /// <summary>
72         /// The FittingMode of the visual.
73         /// </summary>
74         /// <since_tizen> 5 </since_tizen>
75         [Obsolete("Deprecated in API9, Will be removed in API11, Please use VisualFittingMode")]
76         [System.Diagnostics.CodeAnalysis.SuppressMessage("Design", "CA1051:Do not declare visible instance fields", Justification = "<Pending>")]
77         protected VisualFittingModeType? _visualFittingMode = null;
78
79         /// <summary>
80         /// The corner radius value of the visual.
81         /// </summary>
82         [EditorBrowsable(EditorBrowsableState.Never)]
83         private float? cornerRadius = null;
84
85         /// <summary>
86         /// The map for visual.
87         /// </summary>
88         /// <since_tizen> 3 </since_tizen>
89         [Obsolete("Deprecated in API9, Will be removed in API11, Please not use _comonlyUsedMap")]
90         [System.Diagnostics.CodeAnalysis.SuppressMessage("Design", "CA1051:Do not declare visible instance fields", Justification = "<Pending>")]
91         protected PropertyMap _commonlyUsedMap = null;
92
93         private Vector2 visualSize = null;
94         private Vector2 visualOffset = null;
95         private Vector2 visualOffsetPolicy = null;
96         private Vector2 visualSizePolicy = null;
97         private Visual.AlignType? visualOrigin = null;
98         private Visual.AlignType? visualAnchorPoint = null;
99
100         private PropertyMap visualTransformMap = null;
101
102         private int? depthIndex = null;
103
104         /// <summary>
105         /// Constructor.
106         /// </summary>
107         /// <since_tizen> 3 </since_tizen>
108         public VisualMap()
109         {
110         }
111
112         /// <summary>
113         /// Gets or sets the size of the visual.<br />
114         /// It can be either relative (percentage of the parent)
115         /// or absolute (in world units).<br />
116         /// Optional.
117         /// </summary>
118         /// <since_tizen> 3 </since_tizen>
119         public Size2D Size
120         {
121             get
122             {
123                 return visualSize ?? (new Size2D(1, 1));
124             }
125             set
126             {
127                 visualSize = value;
128                 if (visualSizePolicy == null)
129                 {
130                     visualSizePolicy = new Vector2(1.0f, 1.0f);
131                 }
132                 UpdateVisual();
133             }
134         }
135
136         /// <summary>
137         /// Gets or sets the offset of the visual.<br />
138         /// It can be either relative (percentage of the parent)
139         /// or absolute (in world units).<br />
140         /// Optional.
141         /// </summary>
142         /// <since_tizen> 3 </since_tizen>
143         public Vector2 Position
144         {
145             get
146             {
147                 return visualOffset ?? (new Vector2(0.0f, 0.0f));
148             }
149             set
150             {
151                 visualOffset = value;
152                 if (visualOffsetPolicy == null)
153                 {
154                     visualOffsetPolicy = new Vector2(1.0f, 1.0f);
155                 }
156                 UpdateVisual();
157             }
158         }
159
160         /// <summary>
161         /// Gets or sets the relative size of the visual<br />
162         /// (percentage [0.0f to 1.0f] of the control).<br />
163         /// Optional.
164         /// </summary>
165         /// <since_tizen> 3 </since_tizen>
166         public RelativeVector2 RelativeSize
167         {
168             get
169             {
170                 return visualSize ?? (new RelativeVector2(1.0f, 1.0f));
171             }
172             set
173             {
174                 visualSize = value;
175                 visualSizePolicy = new Vector2(0.0f, 0.0f);
176                 UpdateVisual();
177             }
178         }
179
180         /// <summary>
181         /// Gets or sets the relative offset of the visual<br />
182         /// (percentage [0.0f to 1.0f] of the control).<br />
183         /// Optional.
184         /// </summary>
185         /// <since_tizen> 3 </since_tizen>
186         public RelativeVector2 RelativePosition
187         {
188             get
189             {
190                 return visualOffset ?? (new RelativeVector2(0.0f, 0.0f));
191             }
192             set
193             {
194                 visualOffset = value;
195                 visualOffsetPolicy = new Vector2(0.0f, 0.0f);
196                 UpdateVisual();
197             }
198         }
199
200         /// <summary>
201         /// Gets or sets whether the x and y offset values are relative<br />
202         /// (percentage [0.0f to 1.0f] of the control) or absolute (in world units).<br />
203         /// By default, both the x and the y offset are relative.<br />
204         /// Optional.
205         /// </summary>
206         /// <since_tizen> 3 </since_tizen>
207         public VisualTransformPolicyType PositionPolicy
208         {
209             get
210             {
211                 if (visualOffsetPolicy != null && visualOffsetPolicy.X == 1.0f
212                     && visualOffsetPolicy.Y == 1.0f)
213                 {
214                     return VisualTransformPolicyType.Absolute;
215                 }
216                 return VisualTransformPolicyType.Relative;
217             }
218             set
219             {
220                 switch (value)
221                 {
222                     case VisualTransformPolicyType.Relative:
223                         visualOffsetPolicy = new Vector2(0.0f, 0.0f);
224                         break;
225                     case VisualTransformPolicyType.Absolute:
226                         visualOffsetPolicy = new Vector2(1.0f, 1.0f);
227                         break;
228                     default:
229                         visualOffsetPolicy = new Vector2(0.0f, 0.0f);
230                         break;
231                 }
232                 UpdateVisual();
233             }
234         }
235
236         /// <summary>
237         /// Gets or sets whether the x offset values are relative<br />
238         /// (percentage [0.0f to 1.0f] of the control) or absolute (in world units).<br />
239         /// By default, the x offset is relative.<br />
240         /// Optional.
241         /// </summary>
242         /// <since_tizen> 3 </since_tizen>
243         public VisualTransformPolicyType PositionPolicyX
244         {
245             get
246             {
247                 if (visualOffsetPolicy != null && visualOffsetPolicy.X == 1.0f)
248                 {
249                     return VisualTransformPolicyType.Absolute;
250                 }
251                 return VisualTransformPolicyType.Relative;
252             }
253             set
254             {
255                 float x = 0.0f;
256
257                 switch (value)
258                 {
259                     case VisualTransformPolicyType.Relative:
260                         x = 0.0f;
261                         break;
262                     case VisualTransformPolicyType.Absolute:
263                         x = 1.0f;
264                         break;
265                     default:
266                         x = 0.0f;
267                         break;
268                 }
269                 visualOffsetPolicy = new Vector2(x, visualOffsetPolicy?.Y ?? 0);
270
271                 UpdateVisual();
272             }
273         }
274
275         /// <summary>
276         /// Gets or sets whether the y offset values are relative<br />
277         /// (percentage [0.0f to 1.0f] of the control) or absolute (in world units).<br />
278         /// By default, the y offset is relative.<br />
279         /// Optional.
280         /// </summary>
281         /// <since_tizen> 3 </since_tizen>
282         public VisualTransformPolicyType PositionPolicyY
283         {
284             get
285             {
286                 if (visualOffsetPolicy != null && visualOffsetPolicy.Y == 1.0f)
287                 {
288                     return VisualTransformPolicyType.Absolute;
289                 }
290                 return VisualTransformPolicyType.Relative;
291             }
292             set
293             {
294                 float y = 0.0f;
295
296                 switch (value)
297                 {
298                     case VisualTransformPolicyType.Relative:
299                         y = 0.0f;
300                         break;
301                     case VisualTransformPolicyType.Absolute:
302                         y = 1.0f;
303                         break;
304                     default:
305                         y = 0.0f;
306                         break;
307                 }
308                 visualOffsetPolicy = new Vector2(visualOffsetPolicy?.X ?? 0, y);
309                 UpdateVisual();
310             }
311         }
312
313         /// <summary>
314         /// Gets or sets whether the size values of the width or the height are relative<br />
315         /// (percentage [0.0f to 1.0f] of the control) or absolute (in world units).<br />
316         /// By default, offsets of both the width and the height are relative to the control's size.<br />
317         /// Optional.
318         /// </summary>
319         /// <since_tizen> 3 </since_tizen>
320         public VisualTransformPolicyType SizePolicy
321         {
322             get
323             {
324                 if (visualSizePolicy != null && visualSizePolicy.X == 1.0f
325                     && visualSizePolicy.Y == 1.0f)
326                 {
327                     return VisualTransformPolicyType.Absolute;
328                 }
329                 return VisualTransformPolicyType.Relative;
330             }
331             set
332             {
333                 switch (value)
334                 {
335                     case VisualTransformPolicyType.Relative:
336                         visualSizePolicy = new Vector2(0.0f, 0.0f);
337                         break;
338                     case VisualTransformPolicyType.Absolute:
339                         visualSizePolicy = new Vector2(1.0f, 1.0f);
340                         break;
341                     default:
342                         visualSizePolicy = new Vector2(0.0f, 0.0f);
343                         break;
344                 }
345                 UpdateVisual();
346             }
347         }
348
349         /// <summary>
350         /// Gets or sets whether size values of the width are relative.<br />
351         /// (percentage [0.0f to 1.0f] of the control) or absolute (in world units).<br />
352         /// By default, the value of the width is relative to the control's width.<br />
353         /// Optional.
354         /// </summary>
355         /// <since_tizen> 3 </since_tizen>
356         public VisualTransformPolicyType SizePolicyWidth
357         {
358             get
359             {
360                 if (visualSizePolicy != null && visualSizePolicy.Width == 1.0f)
361                 {
362                     return VisualTransformPolicyType.Absolute;
363                 }
364                 return VisualTransformPolicyType.Relative;
365             }
366             set
367             {
368                 float width = 0.0f;
369                 switch (value)
370                 {
371                     case VisualTransformPolicyType.Relative:
372                         width = 0.0f;
373                         break;
374                     case VisualTransformPolicyType.Absolute:
375                         width = 1.0f;
376                         break;
377                     default:
378                         width = 0.0f;
379                         break;
380                 }
381
382                 visualSizePolicy = new Vector2(width, visualSizePolicy?.Height ?? 0);
383                 UpdateVisual();
384             }
385         }
386
387         /// <summary>
388         /// Gets or sets whether size values of the height are relative<br />
389         /// (percentage [0.0f to 1.0f] of the control) or absolute (in world units).<br />
390         /// By default, the height value is relative to the control's height.<br />
391         /// Optional.
392         /// </summary>
393         /// <since_tizen> 3 </since_tizen>
394         public VisualTransformPolicyType SizePolicyHeight
395         {
396             get
397             {
398                 if (visualSizePolicy != null && visualSizePolicy.Height == 1.0f)
399                 {
400                     return VisualTransformPolicyType.Absolute;
401                 }
402                 return VisualTransformPolicyType.Relative;
403             }
404             set
405             {
406                 float height = 0.0f;
407
408                 switch (value)
409                 {
410                     case VisualTransformPolicyType.Relative:
411                         height = 0.0f;
412                         break;
413                     case VisualTransformPolicyType.Absolute:
414                         height = 1.0f;
415                         break;
416                     default:
417                         height = 0.0f;
418                         break;
419                 }
420                 visualSizePolicy = new Vector2(visualSizePolicy?.Width ?? 0, height);
421                 UpdateVisual();
422             }
423         }
424
425         /// <summary>
426         /// Gets or sets the origin of the visual within its control area.<br />
427         /// By default, the origin is center.<br />
428         /// Optional.
429         /// </summary>
430         /// <since_tizen> 3 </since_tizen>
431         public Visual.AlignType Origin
432         {
433             get
434             {
435                 return visualOrigin ?? (Visual.AlignType.Center);
436             }
437             set
438             {
439                 visualOrigin = value;
440                 UpdateVisual();
441             }
442         }
443
444         /// <summary>
445         /// Gets or sets the anchor point of the visual.<br />
446         /// By default, the anchor point is center.<br />
447         /// Optional.
448         /// </summary>
449         /// <since_tizen> 3 </since_tizen>
450         public Visual.AlignType AnchorPoint
451         {
452             get
453             {
454                 return visualAnchorPoint ?? (Visual.AlignType.Center);
455             }
456             set
457             {
458                 visualAnchorPoint = value;
459                 UpdateVisual();
460             }
461         }
462
463         /// <summary>
464         /// Gets or sets the depth index of the visual.<br />
465         /// By default, the depth index is 0.<br />
466         /// Optional.
467         /// </summary>
468         /// <since_tizen> 3 </since_tizen>
469         public int DepthIndex
470         {
471             get
472             {
473                 return depthIndex ?? (0);
474             }
475             set
476             {
477                 depthIndex = value;
478             }
479         }
480
481         /// <summary>
482         /// Gets the transform map used by the visual.
483         /// </summary>
484         /// <since_tizen> 3 </since_tizen>
485         public PropertyMap OutputTransformMap
486         {
487             get
488             {
489                 ComposingTransformMap();
490                 return visualTransformMap;
491             }
492         }
493
494         /// <summary>
495         /// Gets the property map to create the visual.
496         /// </summary>
497         /// <since_tizen> 3 </since_tizen>
498         public PropertyMap OutputVisualMap
499         {
500             get
501             {
502                 ComposingPropertyMap();
503                 return _outputVisualMap;
504             }
505         }
506
507
508         /// <summary>
509         /// The shader to use in the visual.
510         /// </summary>
511         /// <since_tizen> 3 </since_tizen>
512         public PropertyMap Shader
513         {
514             get
515             {
516                 return _shader;
517             }
518             set
519             {
520                 _shader = value;
521                 UpdateVisual();
522             }
523         }
524
525         /// <summary>
526         /// Enables or disables the premultiplied alpha. <br />
527         /// The premultiplied alpha is false by default unless this behavior is modified by the derived visual type.
528         /// </summary>
529         /// <since_tizen> 3 </since_tizen>
530         public bool PremultipliedAlpha
531         {
532             get
533             {
534                 return _premultipliedAlpha ?? (false);
535             }
536             set
537             {
538                 _premultipliedAlpha = value;
539                 UpdateVisual();
540             }
541         }
542
543         /// <summary>
544         /// Mix color is a blend color for any visual.
545         /// </summary>
546         /// <since_tizen> 3 </since_tizen>
547         public Color MixColor
548         {
549             get
550             {
551                 return _mixColor;
552             }
553             set
554             {
555                 _mixColor = value;
556                 UpdateVisual();
557             }
558         }
559
560         /// <summary>
561         /// Opacity is the alpha component of the mix color discussed above.
562         /// </summary>
563         /// <since_tizen> 3 </since_tizen>
564         public float Opacity
565         {
566             get
567             {
568                 return _opacity ?? (1.0f);
569             }
570             set
571             {
572                 _opacity = value;
573                 UpdateVisual();
574             }
575         }
576
577         /// <summary>
578         /// The fitting mode of the visual.
579         /// The default is defined by the type of visual (if it is suitable to be stretched or not).
580         /// </summary>
581         /// <since_tizen> 5 </since_tizen>
582         public VisualFittingModeType VisualFittingMode
583         {
584             get
585             {
586                 if (_visualFittingMode == null)
587                 {
588                     if (this is AnimatedImageVisual ||
589                         this is MeshVisual ||
590                         this is PrimitiveVisual ||
591                         this is TextVisual)
592                     {
593                         return VisualFittingModeType.FitKeepAspectRatio;
594                     }
595                     else
596                     {
597                         return VisualFittingModeType.Fill;
598                     }
599                 }
600                 else
601                 {
602                     return (VisualFittingModeType)_visualFittingMode;
603                 }
604             }
605             set
606             {
607                 _visualFittingMode = value;
608                 UpdateVisual();
609             }
610         }
611
612         /// <summary>
613         /// The corner radius of the visual.
614         /// </summary>
615         [EditorBrowsable(EditorBrowsableState.Never)]
616         public float CornerRadius
617         {
618             protected get
619             {
620                 return cornerRadius ?? (0.0f);
621             }
622             set
623             {
624                 cornerRadius = value;
625                 UpdateVisual();
626             }
627         }
628
629         internal string Name
630         {
631             set;
632             get;
633         }
634
635         internal int VisualIndex
636         {
637             set;
638             get;
639         }
640
641         internal VisualView Parent
642         {
643             set;
644             get;
645         }
646
647         /// <summary>
648         /// Suppress UpdateVisual() to update properties to Parent.
649         /// If it is set to true, UpdateVisual() is ignored unless it is called with force.
650         /// </summary>
651         internal bool SuppressUpdateVisual { get; set; } = false;
652
653         internal void UpdateVisual(bool force = false)
654         {
655             if (VisualIndex > 0 && (!SuppressUpdateVisual || force))
656             {
657                 NUILog.Debug("UpdateVisual()! VisualIndex=" + VisualIndex);
658                 Parent.UpdateVisual(VisualIndex, Name, this);
659             }
660             else
661             {
662                 NUILog.Debug("VisualIndex was not set");
663             }
664         }
665
666         /// <summary>
667         /// Compose the out visual map.
668         /// </summary>
669         /// <since_tizen> 3 </since_tizen>
670         protected virtual void ComposingPropertyMap()
671         {
672             if (null == _outputVisualMap)
673             {
674                 _outputVisualMap = new PropertyMap();
675             }
676
677             if (_shader != null)
678             {
679                 PropertyValue temp = new PropertyValue(_shader);
680                 _outputVisualMap.Add(Visual.Property.Shader, temp);
681                 temp.Dispose();
682             }
683             if (_premultipliedAlpha != null)
684             {
685                 PropertyValue temp = new PropertyValue((bool)_premultipliedAlpha);
686                 _outputVisualMap.Add(Visual.Property.PremultipliedAlpha, temp);
687                 temp.Dispose();
688             }
689             if (_mixColor != null)
690             {
691                 PropertyValue temp = new PropertyValue(_mixColor);
692                 _outputVisualMap.Add(Visual.Property.MixColor, temp);
693                 temp.Dispose();
694             }
695             if (_opacity != null)
696             {
697                 PropertyValue temp = new PropertyValue((float)_opacity);
698                 _outputVisualMap.Add(Visual.Property.Opacity, temp);
699                 temp.Dispose();
700             }
701             if (_visualFittingMode != null)
702             {
703                 PropertyValue temp = new PropertyValue((int)_visualFittingMode);
704                 _outputVisualMap.Add(Visual.Property.VisualFittingMode, temp);
705                 temp.Dispose();
706             }
707             if (cornerRadius != null)
708             {
709                 PropertyValue temp = new PropertyValue((int)cornerRadius);
710                 _outputVisualMap.Add(Visual.Property.CornerRadius, temp);
711                 temp.Dispose();
712             }
713         }
714
715         private void ComposingTransformMap()
716         {
717             visualTransformMap = new PropertyMap();
718             if (visualSize != null)
719             {
720                 PropertyValue temp = new PropertyValue(visualSize);
721                 visualTransformMap.Add((int)VisualTransformPropertyType.Size, temp);
722                 temp.Dispose();
723             }
724             if (visualOffset != null)
725             {
726                 PropertyValue temp = new PropertyValue(visualOffset);
727                 visualTransformMap.Add((int)VisualTransformPropertyType.Offset, temp);
728                 temp.Dispose();
729             }
730             if (visualOffsetPolicy != null)
731             {
732                 PropertyValue temp = new PropertyValue(visualOffsetPolicy);
733                 visualTransformMap.Add((int)VisualTransformPropertyType.OffsetPolicy, temp);
734                 temp.Dispose();
735             }
736             if (visualSizePolicy != null)
737             {
738                 PropertyValue temp = new PropertyValue(visualSizePolicy);
739                 visualTransformMap.Add((int)VisualTransformPropertyType.SizePolicy, temp);
740                 temp.Dispose();
741             }
742             if (visualOrigin != null)
743             {
744                 PropertyValue temp = new PropertyValue((int)visualOrigin);
745                 visualTransformMap.Add((int)VisualTransformPropertyType.Origin, temp);
746                 temp.Dispose();
747             }
748             if (visualAnchorPoint != null)
749             {
750                 PropertyValue temp = new PropertyValue((int)visualAnchorPoint);
751                 visualTransformMap.Add((int)VisualTransformPropertyType.AnchorPoint, temp);
752                 temp.Dispose();
753             }
754         }
755
756         [EditorBrowsable(EditorBrowsableState.Never)]
757         protected virtual void Dispose(bool disposing)
758         {
759             if (disposed)
760             {
761                 return;
762             }
763             if (disposing)
764             {
765                 _commonlyUsedMap?.Dispose();
766                 _mixColor?.Dispose();
767                 _outputVisualMap?.Dispose();
768                 _shader?.Dispose();
769                 visualOffset?.Dispose();
770                 visualOffsetPolicy?.Dispose();
771                 visualSize?.Dispose();
772                 visualSizePolicy?.Dispose();
773                 visualTransformMap?.Dispose();
774             }
775             disposed = true;
776         }
777
778         [EditorBrowsable(EditorBrowsableState.Never)]
779         public void Dispose()
780         {
781             Dispose(true);
782             global::System.GC.SuppressFinalize(this);
783         }
784     }
785 }