Revert "[NUI] Refactoring Theme and StyleManager (#1981)" (#2013)
[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, View.ControlStateChangedEventArgs args)
59         {
60             UpdateLottieView(button, args.PreviousState, 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             if (lottieStyle.PlayRange != null && lottieStyle.PlayRange.GetValue(ControlState.Normal, out var result))
74             {
75                 result.Show(lottieView, true);
76             }
77         }
78
79         internal static void UpdateLottieView(Button button, ControlState previousState, LottieAnimationView lottieView)
80         {
81             var lottieStyle = ((ILottieButtonStyle)button.Style);
82             if (lottieStyle.PlayRange != null && lottieStyle.PlayRange.GetValue(button.ControlState, out var result))
83             {
84                 result.Show(lottieView, !previousState.Contains(ControlState.Pressed));
85             }
86         }
87     }
88 }