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