[NUI] Merge Button action state and support experimental theme by profile. (#1525)
[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             InitializeLottieView(button, LottieView);
52
53             return LottieView;
54         }
55
56         /// <inheritdoc/>
57         [EditorBrowsable(EditorBrowsableState.Never)]
58         public override void OnControlStateChanged(Button button, ControlStates previousState, Touch touchInfo)
59         {
60             UpdateLottieView(button, previousState, touchInfo, LottieView);
61         }
62
63         internal static void InitializeLottieView(Button button, LottieAnimationView lottieView)
64         {
65             if (button.Style as ILottieButtonStyle == null)
66             {
67                 throw new Exception("LottieButtonExtension must be used within a ILottieButtonStyle or derived class.");
68             }
69
70             var lottieStyle = (ILottieButtonStyle)button.Style;
71             lottieView.URL = lottieStyle.LottieUrl;
72             lottieView.StopBehavior = LottieAnimationView.StopBehaviorType.MaximumFrame;
73             lottieStyle.PlayRange?.GetValue(ControlStates.Normal)?.Show(lottieView, true);
74         }
75
76         internal static void UpdateLottieView(Button button, ControlStates previousState, Touch touchInfo, LottieAnimationView lottieView)
77         {
78             ((ILottieButtonStyle)button.Style).PlayRange?.GetValue(button.ControlState)?.Show(lottieView, ((int)previousState & (int)ControlStates.Pressed) == 0);
79         }
80     }
81 }