2 using System.ComponentModel;
3 using Tizen.NUI.BaseComponents;
5 namespace Tizen.NUI.Components
7 public partial class Slider
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;
24 private DirectionType direction = DirectionType.Horizontal;
26 private IndicatorType indicatorType = IndicatorType.None;
27 private const float round = 0.5f;
29 private float minValue = 0;
31 private float maxValue = 100;
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;
43 private PanGestureDetector panGestureDetector = null;
44 private float currentSlidedOffset;
45 private EventHandler<ValueChangedArgs> valueChangedHandler;
46 private EventHandler<SlidingFinishedArgs> slidingFinishedHandler;
47 private EventHandler<SliderValueChangedEventArgs> sliderValueChangedHandler;
48 private EventHandler<SliderSlidingFinishedEventArgs> sliderSlidingFinishedHandler;
49 private EventHandler<StateChangedArgs> stateChangedHandler;
51 bool isFocused = false;
52 bool isPressed = false;
54 private void Initialize()
56 currentSlidedOffset = 0;
59 LayoutDirectionChanged += OnLayoutDirectionChanged;
62 private void OnLayoutDirectionChanged(object sender, LayoutDirectionChangedEventArgs e)
67 private ImageView CreateSlidedTrack()
69 if (null == slidedTrackImage)
71 slidedTrackImage = new ImageView()
73 WidthResizePolicy = ResizePolicyType.Fixed,
74 HeightResizePolicy = ResizePolicyType.Fixed
77 if (bgTrackImage != null)
79 bgTrackImage.Add(slidedTrackImage);
82 if (null != thumbImage)
84 slidedTrackImage.Add(thumbImage);
88 return slidedTrackImage;
91 private TextLabel CreateLowIndicatorText()
93 if (null == lowIndicatorText)
95 lowIndicatorText = new TextLabel()
97 WidthResizePolicy = ResizePolicyType.Fixed,
98 HeightResizePolicy = ResizePolicyType.Fixed
100 this.Add(lowIndicatorText);
103 return lowIndicatorText;
106 private TextLabel CreateHighIndicatorText()
108 if (null == highIndicatorText)
110 highIndicatorText = new TextLabel()
112 WidthResizePolicy = ResizePolicyType.Fixed,
113 HeightResizePolicy = ResizePolicyType.Fixed
115 this.Add(highIndicatorText);
118 return highIndicatorText;
121 private ImageView CreateBackgroundTrack()
123 if (null == bgTrackImage)
125 bgTrackImage = new ImageView()
127 WidthResizePolicy = ResizePolicyType.Fixed,
128 HeightResizePolicy = ResizePolicyType.Fixed,
129 ParentOrigin = Tizen.NUI.ParentOrigin.Center,
130 PivotPoint = Tizen.NUI.PivotPoint.Center,
131 PositionUsesPivotPoint = true
133 this.Add(bgTrackImage);
135 if (null != slidedTrackImage)
137 bgTrackImage.Add(slidedTrackImage);
140 bgTrackImage.TouchEvent += OnTouchEventForBgTrack;
146 private ImageView CreateThumb()
148 if (null == thumbImage)
150 thumbImage = new ImageView()
152 WidthResizePolicy = ResizePolicyType.Fixed,
153 HeightResizePolicy = ResizePolicyType.Fixed,
154 ParentOrigin = NUI.ParentOrigin.Center,
155 PivotPoint = NUI.PivotPoint.Center,
156 PositionUsesPivotPoint = true
158 if (slidedTrackImage != null)
160 slidedTrackImage.Add(thumbImage);
162 thumbImage.TouchEvent += OnTouchEventForThumb;
164 panGestureDetector = new PanGestureDetector();
165 panGestureDetector.Attach(thumbImage);
166 panGestureDetector.Detected += OnPanGestureDetected;
172 private void OnPanGestureDetected(object source, PanGestureDetector.DetectedEventArgs e)
174 if (e.PanGesture.State == Gesture.StateType.Started)
176 if (direction == DirectionType.Horizontal)
178 currentSlidedOffset = slidedTrackImage.SizeWidth;
180 else if (direction == DirectionType.Vertical)
182 currentSlidedOffset = slidedTrackImage.SizeHeight;
184 UpdateState(isFocused, true);
187 if (e.PanGesture.State == Gesture.StateType.Continuing || e.PanGesture.State == Gesture.StateType.Started)
189 if (direction == DirectionType.Horizontal)
191 CalculateCurrentValueByGesture(e.PanGesture.Displacement.X);
193 else if (direction == DirectionType.Vertical)
195 CalculateCurrentValueByGesture(-e.PanGesture.Displacement.Y);
200 if (e.PanGesture.State == Gesture.StateType.Finished)
202 if (null != slidingFinishedHandler)
204 SlidingFinishedArgs args = new SlidingFinishedArgs();
205 args.CurrentValue = curValue;
206 slidingFinishedHandler(this, args);
209 if (null != sliderSlidingFinishedHandler)
211 SliderSlidingFinishedEventArgs args = new SliderSlidingFinishedEventArgs();
212 args.CurrentValue = curValue;
213 sliderSlidingFinishedHandler(this, args);
216 UpdateState(isFocused, false);
220 // Relayout basic component: track, thumb and indicator
221 private void RelayoutBaseComponent(bool isInitial = true)
223 if (direction == DirectionType.Horizontal)
225 if (slidedTrackImage != null)
227 slidedTrackImage.ParentOrigin = NUI.ParentOrigin.CenterLeft;
228 slidedTrackImage.PivotPoint = NUI.PivotPoint.CenterLeft;
229 slidedTrackImage.PositionUsesPivotPoint = true;
231 if (thumbImage != null)
233 thumbImage.ParentOrigin = NUI.ParentOrigin.CenterRight;
234 thumbImage.PivotPoint = NUI.PivotPoint.Center;
235 thumbImage.PositionUsesPivotPoint = true;
237 if (lowIndicatorImage != null)
239 lowIndicatorImage.ParentOrigin = NUI.ParentOrigin.CenterLeft;
240 lowIndicatorImage.PivotPoint = NUI.PivotPoint.CenterLeft;
241 lowIndicatorImage.PositionUsesPivotPoint = true;
243 if (highIndicatorImage != null)
245 highIndicatorImage.ParentOrigin = NUI.ParentOrigin.CenterRight;
246 highIndicatorImage.PivotPoint = NUI.PivotPoint.CenterRight;
247 highIndicatorImage.PositionUsesPivotPoint = true;
249 if (lowIndicatorText != null)
251 lowIndicatorText.ParentOrigin = NUI.ParentOrigin.CenterLeft;
252 lowIndicatorText.PivotPoint = NUI.PivotPoint.CenterLeft;
253 lowIndicatorText.PositionUsesPivotPoint = true;
255 if (highIndicatorText != null)
257 highIndicatorText.ParentOrigin = NUI.ParentOrigin.CenterRight;
258 highIndicatorText.PivotPoint = NUI.PivotPoint.CenterRight;
259 highIndicatorText.PositionUsesPivotPoint = true;
261 if (panGestureDetector != null)
265 panGestureDetector.RemoveDirection(PanGestureDetector.DirectionVertical);
267 panGestureDetector.AddDirection(PanGestureDetector.DirectionHorizontal);
270 else if (direction == DirectionType.Vertical)
272 if (slidedTrackImage != null)
274 slidedTrackImage.ParentOrigin = NUI.ParentOrigin.BottomCenter;
275 slidedTrackImage.PivotPoint = NUI.PivotPoint.BottomCenter;
276 slidedTrackImage.PositionUsesPivotPoint = true;
278 if (thumbImage != null)
280 thumbImage.ParentOrigin = NUI.ParentOrigin.TopCenter;
281 thumbImage.PivotPoint = NUI.PivotPoint.Center;
282 thumbImage.PositionUsesPivotPoint = true;
284 if (lowIndicatorImage != null)
286 lowIndicatorImage.ParentOrigin = NUI.ParentOrigin.BottomCenter;
287 lowIndicatorImage.PivotPoint = NUI.PivotPoint.BottomCenter;
288 lowIndicatorImage.PositionUsesPivotPoint = true;
290 if (highIndicatorImage != null)
292 highIndicatorImage.ParentOrigin = NUI.ParentOrigin.TopCenter;
293 highIndicatorImage.PivotPoint = NUI.PivotPoint.TopCenter;
294 highIndicatorImage.PositionUsesPivotPoint = true;
296 if (lowIndicatorText != null)
298 lowIndicatorText.ParentOrigin = NUI.ParentOrigin.BottomCenter;
299 lowIndicatorText.PivotPoint = NUI.PivotPoint.BottomCenter;
300 lowIndicatorText.PositionUsesPivotPoint = true;
302 if (highIndicatorText != null)
304 highIndicatorText.ParentOrigin = NUI.ParentOrigin.TopCenter;
305 highIndicatorText.PivotPoint = NUI.PivotPoint.TopCenter;
306 highIndicatorText.PositionUsesPivotPoint = true;
308 if (panGestureDetector != null)
312 panGestureDetector.RemoveDirection(PanGestureDetector.DirectionHorizontal);
314 panGestureDetector.AddDirection(PanGestureDetector.DirectionVertical);
319 private int BgTrackLength()
321 int bgTrackLength = 0;
322 IndicatorType type = CurrentIndicatorType();
324 if (type == IndicatorType.None)
326 if (direction == DirectionType.Horizontal)
328 bgTrackLength = this.Size2D.Width;
330 else if (direction == DirectionType.Vertical)
332 bgTrackLength = this.Size2D.Height;
335 else if (type == IndicatorType.Image)
336 {// <lowIndicatorImage> <spaceBetweenTrackAndIndicator> <bgTrack> <spaceBetweenTrackAndIndicator> <highIndicatorImage>
337 Size lowIndicatorImageSize = LowIndicatorImageSize();
338 Size highIndicatorImageSize = HighIndicatorImageSize();
339 int curSpace = (int)CurrentSpaceBetweenTrackAndIndicator();
340 if (direction == DirectionType.Horizontal)
342 int lowIndicatorSpace = ((lowIndicatorImageSize.Width == 0) ? (0) : ((int)(curSpace + lowIndicatorImageSize.Width)));
343 int highIndicatorSpace = ((highIndicatorImageSize.Width == 0) ? (0) : ((int)(curSpace + highIndicatorImageSize.Width)));
344 bgTrackLength = this.Size2D.Width - lowIndicatorSpace - highIndicatorSpace;
346 else if (direction == DirectionType.Vertical)
348 int lowIndicatorSpace = ((lowIndicatorImageSize.Height == 0) ? (0) : ((int)(curSpace + lowIndicatorImageSize.Height)));
349 int highIndicatorSpace = ((highIndicatorImageSize.Height == 0) ? (0) : ((int)(curSpace + highIndicatorImageSize.Height)));
350 bgTrackLength = this.Size2D.Height - lowIndicatorSpace - highIndicatorSpace;
353 else if (type == IndicatorType.Text)
354 {// <lowIndicatorText> <spaceBetweenTrackAndIndicator> <bgTrack> <spaceBetweenTrackAndIndicator> <highIndicatorText>
355 Size lowIndicatorTextSize = LowIndicatorTextSize();
356 Size highIndicatorTextSize = HighIndicatorTextSize();
357 int curSpace = (int)CurrentSpaceBetweenTrackAndIndicator();
358 if (direction == DirectionType.Horizontal)
360 int lowIndicatorSpace = ((lowIndicatorTextSize.Width == 0) ? (0) : ((int)(curSpace + lowIndicatorTextSize.Width)));
361 int highIndicatorSpace = ((highIndicatorTextSize.Width == 0) ? (0) : ((int)(curSpace + highIndicatorTextSize.Width)));
362 bgTrackLength = this.Size2D.Width - lowIndicatorSpace - highIndicatorSpace;
364 else if (direction == DirectionType.Vertical)
366 int lowIndicatorSpace = ((lowIndicatorTextSize.Height == 0) ? (0) : ((int)(curSpace + lowIndicatorTextSize.Height)));
367 int highIndicatorSpace = ((highIndicatorTextSize.Height == 0) ? (0) : ((int)(curSpace + highIndicatorTextSize.Height)));
368 bgTrackLength = this.Size2D.Height - lowIndicatorSpace - highIndicatorSpace;
371 return bgTrackLength;
374 private void UpdateLowIndicatorSize()
376 if (lowIndicatorSize != null)
378 if (lowIndicatorImage != null)
380 lowIndicatorImage.Size = lowIndicatorSize;
382 if (lowIndicatorText != null)
384 lowIndicatorText.Size = lowIndicatorSize;
389 if (lowIndicatorImage != null && lowIndicatorImage != null && lowIndicatorImage.Size != null)
391 lowIndicatorImage.Size = sliderStyle.LowIndicatorImage.Size;
393 if (lowIndicatorText != null && lowIndicatorText != null && lowIndicatorText.Size != null)
395 lowIndicatorText.Size = sliderStyle.LowIndicator.Size;
400 private void UpdateBgTrackSize()
402 if (bgTrackImage == null)
406 int curTrackThickness = (int)CurrentTrackThickness();
407 int bgTrackLength = BgTrackLength();
408 if (direction == DirectionType.Horizontal)
410 bgTrackImage.Size2D = new Size2D(bgTrackLength, curTrackThickness);
412 else if (direction == DirectionType.Vertical)
414 bgTrackImage.Size2D = new Size2D(curTrackThickness, bgTrackLength);
418 private void UpdateBgTrackPosition()
420 if (bgTrackImage == null)
424 IndicatorType type = CurrentIndicatorType();
426 if (type == IndicatorType.None)
428 bgTrackImage.Position2D = new Position2D(0, 0);
430 else if (type == IndicatorType.Image)
432 Size lowIndicatorImageSize = LowIndicatorImageSize();
433 Size highIndicatorImageSize = HighIndicatorImageSize();
434 int curSpace = (int)CurrentSpaceBetweenTrackAndIndicator();
435 if (direction == DirectionType.Horizontal)
437 int lowIndicatorSpace = ((lowIndicatorImageSize.Width == 0) ? (0) : ((int)(curSpace + lowIndicatorImageSize.Width)));
438 int highIndicatorSpace = ((highIndicatorImageSize.Width == 0) ? (0) : ((int)(curSpace + highIndicatorImageSize.Width)));
439 bgTrackImage.Position2D = new Position2D(lowIndicatorSpace - (lowIndicatorSpace + highIndicatorSpace) / 2, 0);
441 else if (direction == DirectionType.Vertical)
443 int lowIndicatorSpace = ((lowIndicatorImageSize.Height == 0) ? (0) : ((int)(curSpace + lowIndicatorImageSize.Height)));
444 int highIndicatorSpace = ((highIndicatorImageSize.Height == 0) ? (0) : ((int)(curSpace + highIndicatorImageSize.Height)));
445 bgTrackImage.Position2D = new Position2D(0, lowIndicatorSpace - (lowIndicatorSpace + highIndicatorSpace) / 2);
448 else if (type == IndicatorType.Text)
450 Size lowIndicatorTextSize = LowIndicatorTextSize();
451 Size highIndicatorTextSize = HighIndicatorTextSize();
452 int curSpace = (int)CurrentSpaceBetweenTrackAndIndicator();
453 if (direction == DirectionType.Horizontal)
455 int lowIndicatorSpace = ((lowIndicatorTextSize.Width == 0) ? (0) : ((int)(curSpace + lowIndicatorTextSize.Width)));
456 int highIndicatorSpace = ((highIndicatorTextSize.Width == 0) ? (0) : ((int)(curSpace + highIndicatorTextSize.Width)));
457 bgTrackImage.Position2D = new Position2D(lowIndicatorSpace - (lowIndicatorSpace + highIndicatorSpace) / 2, 0);
459 else if (direction == DirectionType.Vertical)
461 int lowIndicatorSpace = ((lowIndicatorTextSize.Height == 0) ? (0) : ((int)(curSpace + lowIndicatorTextSize.Height)));
462 int highIndicatorSpace = ((highIndicatorTextSize.Height == 0) ? (0) : ((int)(curSpace + highIndicatorTextSize.Height)));
463 bgTrackImage.Position2D = new Position2D(0, -(lowIndicatorSpace - (lowIndicatorSpace + highIndicatorSpace) / 2));
468 private void UpdateValue()
470 if (slidedTrackImage == null)
474 if (curValue < minValue || curValue > maxValue || minValue >= maxValue)
480 ratio = (float)(curValue - minValue) / (float)(maxValue - minValue);
482 uint curTrackThickness = CurrentTrackThickness();
484 if (direction == DirectionType.Horizontal)
486 if (LayoutDirection == ViewLayoutDirectionType.RTL)
488 ratio = 1.0f - ratio;
490 float slidedTrackLength = (float)BgTrackLength() * ratio;
491 slidedTrackImage.Size2D = new Size2D((int)(slidedTrackLength + round), (int)curTrackThickness); //Add const round to reach Math.Round function.
493 else if (direction == DirectionType.Vertical)
495 float slidedTrackLength = (float)BgTrackLength() * ratio;
496 slidedTrackImage.Size2D = new Size2D((int)(curTrackThickness + round), (int)slidedTrackLength); //Add const round to reach Math.Round function.
500 private uint CurrentTrackThickness()
502 uint curTrackThickness = 0;
503 if (trackThickness != null)
505 curTrackThickness = trackThickness.Value;
509 if (sliderStyle != null && sliderStyle.TrackThickness != null)
511 curTrackThickness = sliderStyle.TrackThickness.Value;
514 return curTrackThickness;
517 private uint CurrentSpaceBetweenTrackAndIndicator()
520 if (spaceBetweenTrackAndIndicator != null)
522 curSpace = spaceBetweenTrackAndIndicator.Start;
526 if (sliderStyle != null && sliderStyle.TrackPadding != null)
528 curSpace = sliderStyle.TrackPadding.Start;
534 private IndicatorType CurrentIndicatorType()
536 IndicatorType type = IndicatorType.None;
537 if (sliderStyle != null)
539 type = (IndicatorType)sliderStyle.IndicatorType;
544 private Size LowIndicatorImageSize()
546 Size size = new Size(0, 0);
547 if (lowIndicatorSize != null)
549 size = lowIndicatorSize;
553 if (sliderStyle != null && sliderStyle.LowIndicatorImage != null && sliderStyle.LowIndicatorImage.Size != null)
555 size = sliderStyle.LowIndicatorImage.Size;
561 private Size HighIndicatorImageSize()
563 Size size = new Size(0, 0);
564 if (highIndicatorSize != null)
566 size = highIndicatorSize;
570 if (sliderStyle != null && sliderStyle.HighIndicatorImage != null && sliderStyle.HighIndicatorImage.Size != null)
572 size = sliderStyle.HighIndicatorImage.Size;
578 private Size LowIndicatorTextSize()
580 Size size = new Size(0, 0);
581 if (lowIndicatorSize != null)
583 size = lowIndicatorSize;
587 if (sliderStyle != null && sliderStyle.LowIndicator != null && sliderStyle.LowIndicator.Size != null)
589 size = sliderStyle.LowIndicator.Size;
595 private Size HighIndicatorTextSize()
597 Size size = new Size(0, 0);
598 if (highIndicatorSize != null)
600 size = highIndicatorSize;
604 if (sliderStyle != null && sliderStyle.HighIndicator != null && sliderStyle.HighIndicator.Size != null)
606 size = sliderStyle.HighIndicator.Size;