[NUI] integration from DevelNUI to master (#3309)
[platform/core/csapi/tizenfx.git] / test / NUITizenGallery / Examples / BoxViewTest3 / BoxViewTest3.xaml.cs
1 /*
2  * Copyright(c) 2021 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 Tizen.NUI;
19 using Tizen.NUI.BaseComponents;
20 using Tizen.NUI.Components;
21
22 namespace NUITizenGallery
23 {
24     public partial class BoxViewTest3 : View
25     {
26         public BoxViewTest3()
27         {
28             InitializeComponent();
29             Subscribe();
30             ApplyColor();
31         }
32
33         private void Subscribe()
34         {
35             red.ValueChanged += OnSliderValueChanged;
36             green.ValueChanged += OnSliderValueChanged;
37             blue.ValueChanged += OnSliderValueChanged;
38             alpha.ValueChanged += OnSliderValueChanged;
39         }
40
41         private void Unsubscribe()
42         {
43             red.ValueChanged -= OnSliderValueChanged;
44             green.ValueChanged -= OnSliderValueChanged;
45             blue.ValueChanged -= OnSliderValueChanged;
46             alpha.ValueChanged -= OnSliderValueChanged;
47         }
48
49         private void OnSliderValueChanged(object sender, SliderValueChangedEventArgs e)
50         {
51             ApplyColor();
52         }
53
54         private void ApplyColor()
55         {
56             float r = red.CurrentValue / 255f;
57             float g = green.CurrentValue / 255f;
58             float b = blue.CurrentValue / 255f;
59             float a = alpha.CurrentValue / 255f;
60
61             colorBox.BackgroundColor = new Color(r, g, b, a);
62             preColorBox.BackgroundColor = new Color(r * a, g * a, b * a, a);
63
64             output.Text = String.Format("R={0:000}, G={1:000}, B={2:000}, A={3:000}", r * 255f, g * 255f, b * 255f, a * 255f);
65         }
66
67         protected override void Dispose(DisposeTypes type)
68         {
69             if (Disposed)
70             {
71                 return;
72             }
73
74             if (type == DisposeTypes.Explicit)
75             {
76                 Unsubscribe();
77                 RemoveAllChildren(true);
78             }
79
80             base.Dispose(type);
81         }
82
83         private void RemoveAllChildren(bool dispose = false)
84         {
85             RecursiveRemoveChildren(this, dispose);
86         }
87
88         private void RecursiveRemoveChildren(View parent, bool dispose)
89         {
90             if (parent == null)
91             {
92                 return;
93             }
94
95             int maxChild = (int)parent.ChildCount;
96             for (int i = maxChild - 1; i >= 0; --i)
97             {
98                 View child = parent.GetChildAt((uint)i);
99                 if (child == null)
100                 {
101                     continue;
102                 }
103
104                 RecursiveRemoveChildren(child, dispose);
105                 parent.Remove(child);
106                 if (dispose)
107                 {
108                     child.Dispose();
109                 }
110             }
111         }
112     }
113 }