f31bcccd0b80d1526d13b145985a24a84186ec94
[platform/core/csapi/tizenfx.git] / src / Tizen.NUI.Components / Controls / Slider.Internal.cs
1 using System;
2 using System.ComponentModel;
3 using Tizen.NUI.BaseComponents;
4
5 namespace Tizen.NUI.Components
6 {
7     public partial class Slider
8     {
9         // the background track image object
10         private ImageView bgTrackImage = null;
11         // the slided track image object
12         private ImageView slidedTrackImage = null;
13         // the thumb image object
14         private ImageView thumbImage = null;
15         // the low indicator image object
16         private ImageView lowIndicatorImage = null;
17         // the high indicator image object
18         private ImageView highIndicatorImage = null;
19         // the low indicator text object
20         private TextLabel lowIndicatorText = null;
21         // the high indicator text object
22         private TextLabel highIndicatorText = null;
23         // the direction type
24         private DirectionType direction = DirectionType.Horizontal;
25         // the indicator type
26         private IndicatorType indicatorType = IndicatorType.None;
27         private const float round = 0.5f;
28         // the minimum value
29         private float minValue = 0;
30         // the maximum value
31         private float maxValue = 100;
32         // the current value
33         private float curValue = 0;
34         // the size of the low indicator
35         private Size lowIndicatorSize = null;
36         // the size of the high indicator
37         private Size highIndicatorSize = null;
38         // the track thickness value
39         private uint? trackThickness = null;
40         // the value of the space between track and indicator object
41         private Extents _spaceBetweenTrackAndIndicator = null;
42
43         private PanGestureDetector panGestureDetector = null;
44         private float currentSlidedOffset;
45         private EventHandler<ValueChangedArgs> valueChangedHandler;
46         private EventHandler<SlidingFinishedArgs> slidingFinishedHandler;
47         private EventHandler<StateChangedArgs> stateChangedHandler;
48
49         bool isFocused = false;
50         bool isPressed = false;
51
52         private StringSelector thumbImageURLSelector = new StringSelector();
53
54         private void Initialize()
55         {
56             currentSlidedOffset = 0;
57             isFocused = false;
58             isPressed = false;
59             LayoutDirectionChanged += OnLayoutDirectionChanged;
60         }
61
62         private void OnLayoutDirectionChanged(object sender, LayoutDirectionChangedEventArgs e)
63         {
64             RelayoutRequest();
65         }
66
67         private ImageView CreateSlidedTrack()
68         {
69             if (null == slidedTrackImage)
70             {
71                 slidedTrackImage = new ImageView()
72                 {
73                     WidthResizePolicy = ResizePolicyType.Fixed,
74                     HeightResizePolicy = ResizePolicyType.Fixed
75                 };
76
77                 if (bgTrackImage != null)
78                 {
79                     bgTrackImage.Add(slidedTrackImage);
80                 }
81
82                 if (null != thumbImage)
83                 {
84                     slidedTrackImage.Add(thumbImage);
85                 }
86             }
87
88             return slidedTrackImage;
89         }
90
91         private TextLabel CreateLowIndicatorText()
92         {
93             if (null == lowIndicatorText)
94             {
95                 lowIndicatorText = new TextLabel()
96                 {
97                     WidthResizePolicy = ResizePolicyType.Fixed,
98                     HeightResizePolicy = ResizePolicyType.Fixed
99                 };
100                 this.Add(lowIndicatorText);
101             }
102
103             return lowIndicatorText;
104         }
105
106         private TextLabel CreateHighIndicatorText()
107         {
108             if (null == highIndicatorText)
109             {
110                 highIndicatorText = new TextLabel()
111                 {
112                     WidthResizePolicy = ResizePolicyType.Fixed,
113                     HeightResizePolicy = ResizePolicyType.Fixed
114                 };
115                 this.Add(highIndicatorText);
116             }
117
118             return highIndicatorText;
119         }
120
121         private ImageView CreateBackgroundTrack()
122         {
123             if (null == bgTrackImage)
124             {
125                 bgTrackImage = new ImageView()
126                 {
127                     WidthResizePolicy = ResizePolicyType.Fixed,
128                     HeightResizePolicy = ResizePolicyType.Fixed,
129                     ParentOrigin = Tizen.NUI.ParentOrigin.Center,
130                     PivotPoint = Tizen.NUI.PivotPoint.Center,
131                     PositionUsesPivotPoint = true
132                 };
133                 this.Add(bgTrackImage);
134
135                 if (null != slidedTrackImage)
136                 {
137                     bgTrackImage.Add(slidedTrackImage);
138                 }
139
140                 bgTrackImage.TouchEvent += OnTouchEventForBgTrack;
141             }
142
143             return bgTrackImage;
144         }
145
146         private ImageView CreateThumb()
147         {
148             if (null == thumbImage)
149             {
150                 thumbImage = new ImageView()
151                 {
152                     WidthResizePolicy = ResizePolicyType.Fixed,
153                     HeightResizePolicy = ResizePolicyType.Fixed,
154                     ParentOrigin = NUI.ParentOrigin.Center,
155                     PivotPoint = NUI.PivotPoint.Center,
156                     PositionUsesPivotPoint = true
157                 };
158                 if (slidedTrackImage != null)
159                 {
160                     slidedTrackImage.Add(thumbImage);
161                 }
162                 thumbImage.TouchEvent += OnTouchEventForThumb;
163
164                 panGestureDetector = new PanGestureDetector();
165                 panGestureDetector.Attach(thumbImage);
166                 panGestureDetector.Detected += OnPanGestureDetected;
167             }
168
169             return thumbImage;
170         }
171
172         private void OnPanGestureDetected(object source, PanGestureDetector.DetectedEventArgs e)
173         {
174             if (e.PanGesture.State == Gesture.StateType.Started)
175             {
176                 if (direction == DirectionType.Horizontal)
177                 {
178                     currentSlidedOffset = slidedTrackImage.SizeWidth;
179                 }
180                 else if (direction == DirectionType.Vertical)
181                 {
182                     currentSlidedOffset = slidedTrackImage.SizeHeight;
183                 }
184                 UpdateState(isFocused, true);
185             }
186
187             if (e.PanGesture.State == Gesture.StateType.Continuing || e.PanGesture.State == Gesture.StateType.Started)
188             {
189                 if (direction == DirectionType.Horizontal)
190                 {
191                     CalculateCurrentValueByGesture(e.PanGesture.Displacement.X);
192                 }
193                 else if (direction == DirectionType.Vertical)
194                 {
195                     CalculateCurrentValueByGesture(-e.PanGesture.Displacement.Y);
196                 }
197                 UpdateValue();
198             }
199
200             if (e.PanGesture.State == Gesture.StateType.Finished)
201             {
202                 if (null != slidingFinishedHandler)
203                 {
204                     SlidingFinishedArgs args = new SlidingFinishedArgs();
205                     args.CurrentValue = curValue;
206                     slidingFinishedHandler(this, args);
207                 }
208
209                 UpdateState(isFocused, false);
210             }
211         }
212
213         // Relayout basic component: track, thumb and indicator
214         private void RelayoutBaseComponent(bool isInitial = true)
215         {
216             if (direction == DirectionType.Horizontal)
217             {
218                 if (slidedTrackImage != null)
219                 {
220                     slidedTrackImage.ParentOrigin = NUI.ParentOrigin.CenterLeft;
221                     slidedTrackImage.PivotPoint = NUI.PivotPoint.CenterLeft;
222                     slidedTrackImage.PositionUsesPivotPoint = true;
223                 }
224                 if (thumbImage != null)
225                 {
226                     thumbImage.ParentOrigin = NUI.ParentOrigin.CenterRight;
227                     thumbImage.PivotPoint = NUI.PivotPoint.Center;
228                     thumbImage.PositionUsesPivotPoint = true;
229                 }
230                 if (lowIndicatorImage != null)
231                 {
232                     lowIndicatorImage.ParentOrigin = NUI.ParentOrigin.CenterLeft;
233                     lowIndicatorImage.PivotPoint = NUI.PivotPoint.CenterLeft;
234                     lowIndicatorImage.PositionUsesPivotPoint = true;
235                 }
236                 if (highIndicatorImage != null)
237                 {
238                     highIndicatorImage.ParentOrigin = NUI.ParentOrigin.CenterRight;
239                     highIndicatorImage.PivotPoint = NUI.PivotPoint.CenterRight;
240                     highIndicatorImage.PositionUsesPivotPoint = true;
241                 }
242                 if (lowIndicatorText != null)
243                 {
244                     lowIndicatorText.ParentOrigin = NUI.ParentOrigin.CenterLeft;
245                     lowIndicatorText.PivotPoint = NUI.PivotPoint.CenterLeft;
246                     lowIndicatorText.PositionUsesPivotPoint = true;
247                 }
248                 if (highIndicatorText != null)
249                 {
250                     highIndicatorText.ParentOrigin = NUI.ParentOrigin.CenterRight;
251                     highIndicatorText.PivotPoint = NUI.PivotPoint.CenterRight;
252                     highIndicatorText.PositionUsesPivotPoint = true;
253                 }
254                 if (panGestureDetector != null)
255                 {
256                     if (!isInitial)
257                     {
258                         panGestureDetector.RemoveDirection(PanGestureDetector.DirectionVertical);
259                     }
260                     panGestureDetector.AddDirection(PanGestureDetector.DirectionHorizontal);
261                 }
262             }
263             else if (direction == DirectionType.Vertical)
264             {
265                 if (slidedTrackImage != null)
266                 {
267                     slidedTrackImage.ParentOrigin = NUI.ParentOrigin.BottomCenter;
268                     slidedTrackImage.PivotPoint = NUI.PivotPoint.BottomCenter;
269                     slidedTrackImage.PositionUsesPivotPoint = true;
270                 }
271                 if (thumbImage != null)
272                 {
273                     thumbImage.ParentOrigin = NUI.ParentOrigin.TopCenter;
274                     thumbImage.PivotPoint = NUI.PivotPoint.Center;
275                     thumbImage.PositionUsesPivotPoint = true;
276                 }
277                 if (lowIndicatorImage != null)
278                 {
279                     lowIndicatorImage.ParentOrigin = NUI.ParentOrigin.BottomCenter;
280                     lowIndicatorImage.PivotPoint = NUI.PivotPoint.BottomCenter;
281                     lowIndicatorImage.PositionUsesPivotPoint = true;
282                 }
283                 if (highIndicatorImage != null)
284                 {
285                     highIndicatorImage.ParentOrigin = NUI.ParentOrigin.TopCenter;
286                     highIndicatorImage.PivotPoint = NUI.PivotPoint.TopCenter;
287                     highIndicatorImage.PositionUsesPivotPoint = true;
288                 }
289                 if (lowIndicatorText != null)
290                 {
291                     lowIndicatorText.ParentOrigin = NUI.ParentOrigin.BottomCenter;
292                     lowIndicatorText.PivotPoint = NUI.PivotPoint.BottomCenter;
293                     lowIndicatorText.PositionUsesPivotPoint = true;
294                 }
295                 if (highIndicatorText != null)
296                 {
297                     highIndicatorText.ParentOrigin = NUI.ParentOrigin.TopCenter;
298                     highIndicatorText.PivotPoint = NUI.PivotPoint.TopCenter;
299                     highIndicatorText.PositionUsesPivotPoint = true;
300                 }
301                 if (panGestureDetector != null)
302                 {
303                     if (!isInitial)
304                     {
305                         panGestureDetector.RemoveDirection(PanGestureDetector.DirectionHorizontal);
306                     }
307                     panGestureDetector.AddDirection(PanGestureDetector.DirectionVertical);
308                 }
309             }
310         }
311
312         private int BgTrackLength()
313         {
314             int bgTrackLength = 0;
315             IndicatorType type = CurrentIndicatorType();
316
317             if (type == IndicatorType.None)
318             {
319                 if (direction == DirectionType.Horizontal)
320                 {
321                     bgTrackLength = this.Size2D.Width;
322                 }
323                 else if (direction == DirectionType.Vertical)
324                 {
325                     bgTrackLength = this.Size2D.Height;
326                 }
327             }
328             else if (type == IndicatorType.Image)
329             {// <lowIndicatorImage> <spaceBetweenTrackAndIndicator> <bgTrack> <spaceBetweenTrackAndIndicator> <highIndicatorImage>
330                 Size lowIndicatorImageSize = LowIndicatorImageSize();
331                 Size highIndicatorImageSize = HighIndicatorImageSize();
332                 int curSpace = (int)CurrentSpaceBetweenTrackAndIndicator();
333                 if (direction == DirectionType.Horizontal)
334                 {
335                     int lowIndicatorSpace = ((lowIndicatorImageSize.Width == 0) ? (0) : ((int)(curSpace + lowIndicatorImageSize.Width)));
336                     int highIndicatorSpace = ((highIndicatorImageSize.Width == 0) ? (0) : ((int)(curSpace + highIndicatorImageSize.Width)));
337                     bgTrackLength = this.Size2D.Width - lowIndicatorSpace - highIndicatorSpace;
338                 }
339                 else if (direction == DirectionType.Vertical)
340                 {
341                     int lowIndicatorSpace = ((lowIndicatorImageSize.Height == 0) ? (0) : ((int)(curSpace + lowIndicatorImageSize.Height)));
342                     int highIndicatorSpace = ((highIndicatorImageSize.Height == 0) ? (0) : ((int)(curSpace + highIndicatorImageSize.Height)));
343                     bgTrackLength = this.Size2D.Height - lowIndicatorSpace - highIndicatorSpace;
344                 }
345             }
346             else if (type == IndicatorType.Text)
347             {// <lowIndicatorText> <spaceBetweenTrackAndIndicator> <bgTrack> <spaceBetweenTrackAndIndicator> <highIndicatorText>
348                 Size lowIndicatorTextSize = LowIndicatorTextSize();
349                 Size highIndicatorTextSize = HighIndicatorTextSize();
350                 int curSpace = (int)CurrentSpaceBetweenTrackAndIndicator();
351                 if (direction == DirectionType.Horizontal)
352                 {
353                     int lowIndicatorSpace = ((lowIndicatorTextSize.Width == 0) ? (0) : ((int)(curSpace + lowIndicatorTextSize.Width)));
354                     int highIndicatorSpace = ((highIndicatorTextSize.Width == 0) ? (0) : ((int)(curSpace + highIndicatorTextSize.Width)));
355                     bgTrackLength = this.Size2D.Width - lowIndicatorSpace - highIndicatorSpace;
356                 }
357                 else if (direction == DirectionType.Vertical)
358                 {
359                     int lowIndicatorSpace = ((lowIndicatorTextSize.Height == 0) ? (0) : ((int)(curSpace + lowIndicatorTextSize.Height)));
360                     int highIndicatorSpace = ((highIndicatorTextSize.Height == 0) ? (0) : ((int)(curSpace + highIndicatorTextSize.Height)));
361                     bgTrackLength = this.Size2D.Height - lowIndicatorSpace - highIndicatorSpace;
362                 }
363             }
364             return bgTrackLength;
365         }
366
367         private void UpdateLowIndicatorSize()
368         {
369             if (lowIndicatorSize != null)
370             {
371                 if (lowIndicatorImage != null)
372                 {
373                     lowIndicatorImage.Size = lowIndicatorSize;
374                 }
375                 if (lowIndicatorText != null)
376                 {
377                     lowIndicatorText.Size = lowIndicatorSize;
378                 }
379             }
380             else
381             {
382                 if (lowIndicatorImage != null && Style != null && Style.LowIndicatorImage != null && Style.LowIndicatorImage.Size != null)
383                 {
384                     lowIndicatorImage.Size = Style.LowIndicatorImage.Size;
385                 }
386                 if (lowIndicatorText != null && Style != null && Style.LowIndicator != null && Style.LowIndicator.Size != null)
387                 {
388                     lowIndicatorText.Size = Style.LowIndicator.Size;
389                 }
390             }
391         }
392
393         private void UpdateBgTrackSize()
394         {
395             if (bgTrackImage == null)
396             {
397                 return;
398             }
399             int curTrackThickness = (int)CurrentTrackThickness();
400             int bgTrackLength = BgTrackLength();
401             if (direction == DirectionType.Horizontal)
402             {
403                 bgTrackImage.Size2D = new Size2D(bgTrackLength, curTrackThickness);
404             }
405             else if (direction == DirectionType.Vertical)
406             {
407                 bgTrackImage.Size2D = new Size2D(curTrackThickness, bgTrackLength);
408             }
409         }
410
411         private void UpdateBgTrackPosition()
412         {
413             if (bgTrackImage == null)
414             {
415                 return;
416             }
417             IndicatorType type = CurrentIndicatorType();
418
419             if (type == IndicatorType.None)
420             {
421                 bgTrackImage.Position2D = new Position2D(0, 0);
422             }
423             else if (type == IndicatorType.Image)
424             {
425                 Size lowIndicatorImageSize = LowIndicatorImageSize();
426                 Size highIndicatorImageSize = HighIndicatorImageSize();
427                 int curSpace = (int)CurrentSpaceBetweenTrackAndIndicator();
428                 if (direction == DirectionType.Horizontal)
429                 {
430                     int lowIndicatorSpace = ((lowIndicatorImageSize.Width == 0) ? (0) : ((int)(curSpace + lowIndicatorImageSize.Width)));
431                     int highIndicatorSpace = ((highIndicatorImageSize.Width == 0) ? (0) : ((int)(curSpace + highIndicatorImageSize.Width)));
432                     bgTrackImage.Position2D = new Position2D(lowIndicatorSpace - (lowIndicatorSpace + highIndicatorSpace) / 2, 0);
433                 }
434                 else if (direction == DirectionType.Vertical)
435                 {
436                     int lowIndicatorSpace = ((lowIndicatorImageSize.Height == 0) ? (0) : ((int)(curSpace + lowIndicatorImageSize.Height)));
437                     int highIndicatorSpace = ((highIndicatorImageSize.Height == 0) ? (0) : ((int)(curSpace + highIndicatorImageSize.Height)));
438                     bgTrackImage.Position2D = new Position2D(0, lowIndicatorSpace - (lowIndicatorSpace + highIndicatorSpace) / 2);
439                 }
440             }
441             else if (type == IndicatorType.Text)
442             {
443                 Size lowIndicatorTextSize = LowIndicatorTextSize();
444                 Size highIndicatorTextSize = HighIndicatorTextSize();
445                 int curSpace = (int)CurrentSpaceBetweenTrackAndIndicator();
446                 if (direction == DirectionType.Horizontal)
447                 {
448                     int lowIndicatorSpace = ((lowIndicatorTextSize.Width == 0) ? (0) : ((int)(curSpace + lowIndicatorTextSize.Width)));
449                     int highIndicatorSpace = ((highIndicatorTextSize.Width == 0) ? (0) : ((int)(curSpace + highIndicatorTextSize.Width)));
450                     bgTrackImage.Position2D = new Position2D(lowIndicatorSpace - (lowIndicatorSpace + highIndicatorSpace) / 2, 0);
451                 }
452                 else if (direction == DirectionType.Vertical)
453                 {
454                     int lowIndicatorSpace = ((lowIndicatorTextSize.Height == 0) ? (0) : ((int)(curSpace + lowIndicatorTextSize.Height)));
455                     int highIndicatorSpace = ((highIndicatorTextSize.Height == 0) ? (0) : ((int)(curSpace + highIndicatorTextSize.Height)));
456                     bgTrackImage.Position2D = new Position2D(0, -(lowIndicatorSpace - (lowIndicatorSpace + highIndicatorSpace) / 2));
457                 }
458             }
459         }
460
461         private void UpdateValue()
462         {
463             if (slidedTrackImage == null)
464             {
465                 return;
466             }
467             if (curValue < minValue || curValue > maxValue || minValue >= maxValue)
468             {
469                 return;
470             }
471
472             float ratio = 0;
473             ratio = (float)(curValue - minValue) / (float)(maxValue - minValue);
474
475             uint curTrackThickness = CurrentTrackThickness();
476
477             if (direction == DirectionType.Horizontal)
478             {
479                 if (LayoutDirection == ViewLayoutDirectionType.RTL)
480                 {
481                     ratio = 1.0f - ratio;
482                 }
483                 float slidedTrackLength = (float)BgTrackLength() * ratio;
484                 slidedTrackImage.Size2D = new Size2D((int)(slidedTrackLength + round), (int)curTrackThickness); //Add const round to reach Math.Round function.
485             }
486             else if (direction == DirectionType.Vertical)
487             {
488                 float slidedTrackLength = (float)BgTrackLength() * ratio;
489                 slidedTrackImage.Size2D = new Size2D((int)(curTrackThickness + round), (int)slidedTrackLength); //Add const round to reach Math.Round function.
490             }
491         }
492
493         private uint CurrentTrackThickness()
494         {
495             uint curTrackThickness = 0;
496             if (trackThickness != null)
497             {
498                 curTrackThickness = trackThickness.Value;
499             }
500             else
501             {
502                 if (Style != null && Style.TrackThickness != null)
503                 {
504                     curTrackThickness = Style.TrackThickness.Value;
505                 }
506             }
507             return curTrackThickness;
508         }
509
510         private uint CurrentSpaceBetweenTrackAndIndicator()
511         {
512             uint curSpace = 0;
513             if (spaceBetweenTrackAndIndicator != null)
514             {
515                 curSpace = spaceBetweenTrackAndIndicator.Start;
516             }
517             else
518             {
519                 if (Style != null && Style.TrackPadding != null)
520                 {
521                     curSpace = Style.TrackPadding.Start;
522                 }
523             }
524             return curSpace;
525         }
526
527         private IndicatorType CurrentIndicatorType()
528         {
529             IndicatorType type = IndicatorType.None;
530             if (Style != null)
531             {
532                 type = (IndicatorType)Style.IndicatorType;
533             }
534             return type;
535         }
536
537         private Size LowIndicatorImageSize()
538         {
539             Size size = new Size(0, 0);
540             if (lowIndicatorSize != null)
541             {
542                 size = lowIndicatorSize;
543             }
544             else
545             {
546                 if (Style != null && Style.LowIndicatorImage != null && Style.LowIndicatorImage.Size != null)
547                 {
548                     size = Style.LowIndicatorImage.Size;
549                 }
550             }
551             return size;
552         }
553
554         private Size HighIndicatorImageSize()
555         {
556             Size size = new Size(0, 0);
557             if (highIndicatorSize != null)
558             {
559                 size = highIndicatorSize;
560             }
561             else
562             {
563                 if (Style != null && Style.HighIndicatorImage != null && Style.HighIndicatorImage.Size != null)
564                 {
565                     size = Style.HighIndicatorImage.Size;
566                 }
567             }
568             return size;
569         }
570
571         private Size LowIndicatorTextSize()
572         {
573             Size size = new Size(0, 0);
574             if (lowIndicatorSize != null)
575             {
576                 size = lowIndicatorSize;
577             }
578             else
579             {
580                 if (Style != null && Style.LowIndicator != null && Style.LowIndicator.Size != null)
581                 {
582                     size = Style.LowIndicator.Size;
583                 }
584             }
585             return size;
586         }
587
588         private Size HighIndicatorTextSize()
589         {
590             Size size = new Size(0, 0);
591             if (highIndicatorSize != null)
592             {
593                 size = highIndicatorSize;
594             }
595             else
596             {
597                 if (Style != null && Style.HighIndicator != null && Style.HighIndicator.Size != null)
598                 {
599                     size = Style.HighIndicator.Size;
600                 }
601             }
602             return size;
603         }
604     }
605 }