Merge remote-tracking branch 'origin/master' into tizen
[platform/core/csapi/tizenfx.git] / test / Tizen.AIAvatar.Example / src / ControlPannelComponent.cs
1 /*
2  * Copyright(c) 2023 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
18 using global::System;
19 using Tizen.NUI;
20 using Tizen.NUI.BaseComponents;
21 using Tizen.NUI.Scene3D;
22 using Tizen.NUI.Components;
23
24 namespace AIAvatar
25 {
26     public class UIControlPanel
27     {
28         private readonly Vector2 SceneViewPadding = new Vector2(0.0f, 0.0f);
29         private ButtonStyle buttonStyle;
30         private SliderStyle sliderStyle;
31
32         private Window uiWindow;
33         private AvatarScene mainScene;
34         private View controlPannel;
35
36         private TextField editor;
37
38         private readonly float ControlPannelWidthScale = 0.25f;// Relative size of window width. windowSize.Width * 0.5f
39
40         public event EventHandler OnExitButtonClicked;
41
42         public void MakeControlPannel(Window uiWindow, AvatarScene mainScene)
43         {
44             this.uiWindow = uiWindow;
45             this.mainScene = mainScene;
46
47             if (controlPannel != null)
48             {
49                 return;
50             }
51
52             InitializeButtonStyle();
53             InitializeSliderStyle();
54
55             controlPannel = new ScrollableBase()
56             {
57                 BackgroundColor = new Color(0.85f, 0.85f, 0.85f, 0.25f),
58
59                 PivotPoint = PivotPoint.TopLeft,
60                 ParentOrigin = ParentOrigin.TopLeft,
61                 PositionUsesPivotPoint = true,
62                 //CornerRadius = 30.0f,
63                 Padding = new Extents(30, 30, 30, 30),
64
65                 Layout = new LinearLayout()
66                 {
67                     LinearOrientation = LinearLayout.Orientation.Vertical,
68                     HorizontalAlignment = HorizontalAlignment.Begin,
69                     VerticalAlignment = VerticalAlignment.Top,
70                     CellPadding = new Size2D(10, 20),
71                 },
72                 HideScrollbar = false,
73             };
74             uiWindow.Add(controlPannel);
75
76             // Make control buttons
77             MakeControlPannelComponents();
78         }
79
80         public void ReizeUIPanel(View avatarScene, View circleView)
81         {
82             var windowSize = uiWindow.Size;
83             var layoutLTR = (windowSize.Width >= windowSize.Height);
84
85             var sceneViewSize = new Vector2(0, 0);
86             var sceneViewPosition = new Vector2(0, 0);
87             var controlPannelSize = new Vector2(0, 0);
88             var controlPannelPosition = new Vector2(0, 0);
89             if (layoutLTR) // layout Left to Right
90             {
91                 sceneViewSize = new Vector2(windowSize.Width - SceneViewPadding.X * 3.0f - ControlPannelWidthScale * windowSize.Width,
92                                             windowSize.Height - SceneViewPadding.Y * 2.0f);
93                 sceneViewPosition = new Vector2(SceneViewPadding.X * 2.0f + ControlPannelWidthScale * windowSize.Width, SceneViewPadding.Y);
94                 controlPannelSize = new Vector2(ControlPannelWidthScale * windowSize.Width, windowSize.Height - SceneViewPadding.Y * 2.0f);
95                 controlPannelPosition = new Vector2(SceneViewPadding.X, SceneViewPadding.Y);
96             }
97             else // layout Top to Bottom
98             {
99                 sceneViewSize = new Vector2(windowSize.Width - SceneViewPadding.X * 2.0f,
100                                         windowSize.Height - SceneViewPadding.Y * 3.0f - ControlPannelWidthScale * windowSize.Height);
101                 sceneViewPosition = new Vector2(SceneViewPadding.X, SceneViewPadding.Y * 2.0f + ControlPannelWidthScale * windowSize.Height);
102                 controlPannelSize = new Vector2(windowSize.Width - SceneViewPadding.X * 2.0f, ControlPannelWidthScale * windowSize.Height);
103                 controlPannelPosition = new Vector2(SceneViewPadding.X, SceneViewPadding.Y);
104             }
105
106             // Update SceneView and ControlPannel size/position if we has.
107             if (avatarScene != null && circleView != null)
108             {
109                 avatarScene.SizeWidth = sceneViewSize.Width;
110                 avatarScene.SizeHeight = sceneViewSize.Height;
111                 avatarScene.PositionX = 0;//sceneViewPosition.X;
112                 avatarScene.PositionY = 0;//sceneViewPosition.Y;
113
114                 circleView.SizeWidth = sceneViewSize.Width;
115                 circleView.SizeHeight = sceneViewSize.Width;
116                 circleView.PositionX = sceneViewPosition.X;
117                 circleView.PositionY = sceneViewPosition.Y;
118             }
119
120             if (controlPannel != null)
121             {
122                 controlPannel.SizeWidth = controlPannelSize.Width;
123                 controlPannel.SizeHeight = controlPannelSize.Height;
124                 controlPannel.PositionX = controlPannelPosition.X;
125                 controlPannel.PositionY = controlPannelPosition.Y;
126             }
127         }
128
129         private void InitializeButtonStyle()
130         {
131             buttonStyle = new ButtonStyle()
132             {
133                 Size = new Size(252, 48),
134                 ItemSpacing = new Size2D(8, 8),
135                 CornerRadius = 12.0f,
136                 ItemHorizontalAlignment = HorizontalAlignment.Center,
137                 ItemVerticalAlignment = VerticalAlignment.Center,
138                 BorderlineWidth = 5.0f,
139                 BackgroundColor = new Selector<Color>()
140                 {
141                     Normal = Styles.BIG_TAG_FOCUS_BACKGROUND_COLOR1,
142                     Pressed = Styles.BIG_TAG_NORMAL_BACKGROUND_COLOR1,
143                     Focused = Styles.BIG_TAG_FOCUS_BACKGROUND_COLOR2,
144                     Selected = Styles.BIG_TAG_NORMAL_BACKGROUND_COLOR1,
145                     Disabled = new Color(0.792f, 0.792f, 0.792f, 1),
146                 },
147                 BorderlineColorSelector = new Selector<Color>()
148                 {
149                     Normal = Styles.BIG_TAG_FOCUS_BACKGROUND_COLOR2,
150                     Pressed = Styles.BIG_TAG_NORMAL_BACKGROUND_COLOR2,
151                     Focused = Styles.BIG_TAG_FOCUS_BACKGROUND_COLOR1,
152                     Selected = Styles.BIG_TAG_NORMAL_BACKGROUND_COLOR2,
153                     Disabled = new Color(0.94f, 0.95f, 0.93f, 1.0f),
154                 },
155                 Text = new TextLabelStyle()
156                 {
157                     TextColor = new Color("#0D0D0D"),
158                     PixelSize = 24,
159                 },
160             };
161         }
162
163         private void InitializeSliderStyle()
164         {
165             sliderStyle = new SliderStyle()
166             {
167                 Size = new Size(850, 50),
168                 TrackThickness = 8,
169                 Track = new ImageViewStyle()
170                 {
171                     Size = new Size(800, 8),
172                     CornerRadius = 4.0f,
173                     BackgroundColor = new Selector<Color>()
174                     {
175                         Normal = new Color(1.0f, 0.37f, 0.0f, 0.1f),
176                         Disabled = new Color(1.0f, 0.37f, 0.0f, 0.1f),
177                     },
178                 },
179                 Progress = new ImageViewStyle()
180                 {
181                     Size = new Size(800, 8),
182                     CornerRadius = 4.0f,
183                     BackgroundColor = new Selector<Color>()
184                     {
185                         //Normal = new Color("#FF6200"),
186                         Normal = Styles.BIG_TAG_FOCUS_BACKGROUND_COLOR2,
187                         Disabled = new Color("#CACACA"),
188                     },
189                 },
190                 Thumb = new ImageViewStyle()
191                 {
192                     Size = new Size(24, 24),
193                     BackgroundColor = new Selector<Color>()
194                     {
195                         Normal = Styles.BIG_TAG_FOCUS_BACKGROUND_COLOR1,
196                         Pressed = Styles.BIG_TAG_NORMAL_BACKGROUND_COLOR1,
197                         Focused = Styles.BIG_TAG_FOCUS_BACKGROUND_COLOR2,
198                         Disabled = new Color(0.94f, 0.95f, 0.93f, 1.0f),
199                     },
200                     BorderlineWidth = 5.0f,
201                     BorderlineColorSelector = new Selector<Color>()
202                     {
203                         Normal = Styles.BIG_TAG_FOCUS_BACKGROUND_COLOR2,
204                         Pressed = Styles.BIG_TAG_NORMAL_BACKGROUND_COLOR2,
205                         Focused = Styles.BIG_TAG_FOCUS_BACKGROUND_COLOR1,
206                         Disabled = new Color(0.94f, 0.95f, 0.93f, 1.0f),
207                     },
208                     CornerRadius = new Vector4(0.5f, 0.5f, 0.5f, 0.5f),
209                     CornerRadiusPolicy = VisualTransformPolicyType.Relative,
210                 },
211                 ValueIndicatorImage = new ImageViewStyle()
212                 {
213                     Size = new Size(43, 40),
214                     BorderlineWidth = 1.0f,
215                     BorderlineColor = new Color("#FF6200"),
216                     BackgroundColor = new Color(1.0f, 1.0f, 1.0f, 0.0f),
217                     CornerRadius = 12.0f,
218                 },
219                 ValueIndicatorText = new TextLabelStyle()
220                 {
221                     SizeHeight = 24,
222                     PixelSize = 16,
223                     TextColor = new Color("#FF6200"),
224                 }
225             };
226         }
227
228         private void MakeControlPannelComponents()
229         {
230             AddControlEditor();
231             AddControlButton("ChangeAvatar", "button_avatar", mainScene.ChangeAvatar);
232             AddControlButton("Random Animation", "button_bvh", mainScene.StartRandomAnimation);
233             AddControlButton("EyeBlink", "button_bvh", mainScene.EyeBlink);
234
235             AddControlButton("TTS Init", "button_avatar", mainScene.InintTTsTest);
236             AddControlButton("TTS Start", "button_avatar", mainScene.StartTTSTest);
237             AddControlButton("TTS Stop", "button_avatar", mainScene.StopTTSTest);
238             AddControlButton("CZ", "button_avatar", mainScene.StartAvatarTalk_1);
239             AddControlButton("DK", "button_avatar", mainScene.StartAvatarTalk_2);
240             AddControlButton("GR", "button_avatar", mainScene.StartAvatarTalk_3);
241             AddControlButton("Start Mic", "button_bvh", mainScene.StartMic);
242             AddControlButton("Stop Mic", "button_bvh", mainScene.StopMic);
243             AddControlButton("Show/Hide", "button_bvh", mainScene.ShowHide);
244             AddControlSlider("Camera FOV", "slider_camera_fov", 0.1f, 1.3f, mainScene.GetSelectedCamera().FieldOfView.ConvertToFloat(), mainScene.SetupSceneViewCameraFov);
245             AddControlSlider("IBL intensity", "slider_ibl_factor", 0.1f, 0.8f, mainScene.IBLFactor, mainScene.SetupSceneViewIBLFactor);
246             AddControlButton("Quit", "button_quit", Exit);
247         }
248
249         private void AddControlEditor()
250         {
251             editor = new TextField()
252             {
253                 Text = Utils.TTSText,
254                 PlaceholderText = "Input Your Message",
255                 Name = "InputText",
256                 BackgroundColor = Color.White,
257                 WidthSpecification = LayoutParamPolicies.MatchParent,
258             };
259             editor.TextChanged += (obj, e) => {
260                 Utils.TTSText = e.TextField.Text;
261             };
262
263             controlPannel.Add(editor);
264         }
265
266         private void AddControlButton(string buttonText, string buttonName, Action func)
267         {
268             var button = new Button(buttonStyle)
269             {
270                 Text = buttonText,
271                 Name = buttonName,
272                 WidthSpecification = LayoutParamPolicies.MatchParent,
273             };
274             button.Clicked += (o, e) =>
275             {
276                 Button me = o as Button;
277                 if (me == null) return;
278
279                 func();
280             };
281
282             controlPannel.Add(button);
283         }
284
285         private Slider AddControlSlider(string sliderText, string sliderName, float minValue, float maxValue, float currentValue, Action<float> func)
286         {
287             var dummy = new View()
288             {
289                 WidthSpecification = LayoutParamPolicies.MatchParent,
290                 Layout = new LinearLayout()
291                 {
292                     LinearOrientation = LinearLayout.Orientation.Horizontal,
293                     HorizontalAlignment = HorizontalAlignment.Begin,
294                     VerticalAlignment = VerticalAlignment.Center,
295                     CellPadding = new Size2D(5, 0),
296                 },
297             };
298             var label = new TextLabel()
299             {
300                 WidthSpecification = LayoutParamPolicies.MatchParent,
301                 Text = sliderText,
302                 FontSizeScale = 0.5f,
303                 Focusable = false,
304                 Weight = 0.2f,
305                 MultiLine = true,
306             };
307             dummy.Add(label);
308             var slider = new Slider(sliderStyle)
309             {
310                 WidthSpecification = LayoutParamPolicies.MatchParent,
311                 MinValue = minValue,
312                 MaxValue = maxValue,
313                 CurrentValue = currentValue,
314                 Name = sliderName,
315                 Weight = 0.8f,
316             };
317             slider.ValueChanged += (o, e) =>
318             {
319                 func(e.CurrentValue);
320             };
321             dummy.Add(slider);
322             controlPannel.Add(dummy);
323
324             return slider;
325         }
326
327         private void Exit()
328         {
329             OnExitButtonClicked?.Invoke(null, null);
330         }
331     }
332 }