[NUI] Modify NUI.Components private variable (#2764)
[platform/core/csapi/tizenfx.git] / src / Tizen.NUI.Components / Controls / Extension / LottieButtonExtension.cs
1 /*
2  * Copyright(c) 2020 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.Components.Extension
22 {
23     /// <summary>
24     /// LottieButtonExtension is a ButtonExtension class that uses Lottie image for a Button icon.
25     /// <remark>
26     /// This extension must be used within a class implementing LottieButtonStyle.
27     /// </remark>
28     /// </summary>
29     [EditorBrowsable(EditorBrowsableState.Never)]
30     public class LottieButtonExtension : ButtonExtension
31     {
32         /// <summary>
33         /// A constructor that creates LottieButtonExtension with a specified Lottie resource URL
34         /// </summary>
35         [EditorBrowsable(EditorBrowsableState.Never)]
36         public LottieButtonExtension() : base()
37         {
38             LottieView = new LottieAnimationView();
39         }
40
41         /// <summary>
42         /// The Lottie view that will be used as an icon part in a Button.
43         /// </summary>
44         [EditorBrowsable(EditorBrowsableState.Never)]
45         protected LottieAnimationView LottieView { get; set; }
46
47         /// <inheritdoc/>
48         [EditorBrowsable(EditorBrowsableState.Never)]
49         public override ImageView OnCreateIcon(Button button, ImageView icon)
50         {
51             if (button == null)
52             {
53                 throw new ArgumentNullException("button");
54             }
55
56             LottieView.PositionUsesPivotPoint = true;
57             LottieView.ParentOrigin = NUI.ParentOrigin.Center;
58             LottieView.PivotPoint = NUI.PivotPoint.Center;
59
60             InitializeLottieView(button, LottieView);
61
62             return LottieView;
63         }
64
65         /// <inheritdoc/>
66         [EditorBrowsable(EditorBrowsableState.Never)]
67         public override void OnControlStateChanged(Button button, View.ControlStateChangedEventArgs args)
68         {
69             UpdateLottieView(button, args.PreviousState, LottieView);
70         }
71
72         internal static void InitializeLottieView(Button button, LottieAnimationView lottieView)
73         {
74             if (button.Style as ILottieButtonStyle == null)
75             {
76                 throw new Exception("LottieButtonExtension must be used within a ILottieButtonStyle or derived class.");
77             }
78
79             var lottieStyle = (ILottieButtonStyle)button.Style;
80             lottieView.URL = lottieStyle.LottieUrl;
81             lottieView.StopBehavior = LottieAnimationView.StopBehaviorType.MaximumFrame;
82             if (lottieStyle.PlayRange != null && lottieStyle.PlayRange.GetValue(ControlState.Normal, out var result))
83             {
84                 result.Show(lottieView, true);
85             }
86         }
87
88         internal static void UpdateLottieView(Button button, ControlState previousState, LottieAnimationView lottieView)
89         {
90             var lottieStyle = ((ILottieButtonStyle)button.Style);
91             if (lottieStyle.PlayRange != null && lottieStyle.PlayRange.GetValue(button.ControlState, out var result))
92             {
93                 result.Show(lottieView, !previousState.Contains(ControlState.Pressed));
94             }
95         }
96     }
97 }