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<StateChangedArgs> stateChangedHandler;
49 bool isFocused = false;
50 bool isPressed = false;
52 private void Initialize()
54 currentSlidedOffset = 0;
57 LayoutDirectionChanged += OnLayoutDirectionChanged;
60 private void OnLayoutDirectionChanged(object sender, LayoutDirectionChangedEventArgs e)
65 private ImageView CreateSlidedTrack()
67 if (null == slidedTrackImage)
69 slidedTrackImage = new ImageView()
71 WidthResizePolicy = ResizePolicyType.Fixed,
72 HeightResizePolicy = ResizePolicyType.Fixed
75 if (bgTrackImage != null)
77 bgTrackImage.Add(slidedTrackImage);
80 if (null != thumbImage)
82 slidedTrackImage.Add(thumbImage);
86 return slidedTrackImage;
89 private TextLabel CreateLowIndicatorText()
91 if (null == lowIndicatorText)
93 lowIndicatorText = new TextLabel()
95 WidthResizePolicy = ResizePolicyType.Fixed,
96 HeightResizePolicy = ResizePolicyType.Fixed
98 this.Add(lowIndicatorText);
101 return lowIndicatorText;
104 private TextLabel CreateHighIndicatorText()
106 if (null == highIndicatorText)
108 highIndicatorText = new TextLabel()
110 WidthResizePolicy = ResizePolicyType.Fixed,
111 HeightResizePolicy = ResizePolicyType.Fixed
113 this.Add(highIndicatorText);
116 return highIndicatorText;
119 private ImageView CreateBackgroundTrack()
121 if (null == bgTrackImage)
123 bgTrackImage = new ImageView()
125 WidthResizePolicy = ResizePolicyType.Fixed,
126 HeightResizePolicy = ResizePolicyType.Fixed,
127 ParentOrigin = Tizen.NUI.ParentOrigin.Center,
128 PivotPoint = Tizen.NUI.PivotPoint.Center,
129 PositionUsesPivotPoint = true
131 this.Add(bgTrackImage);
133 if (null != slidedTrackImage)
135 bgTrackImage.Add(slidedTrackImage);
138 bgTrackImage.TouchEvent += OnTouchEventForBgTrack;
144 private ImageView CreateThumb()
146 if (null == thumbImage)
148 thumbImage = new ImageView()
150 WidthResizePolicy = ResizePolicyType.Fixed,
151 HeightResizePolicy = ResizePolicyType.Fixed,
152 ParentOrigin = NUI.ParentOrigin.Center,
153 PivotPoint = NUI.PivotPoint.Center,
154 PositionUsesPivotPoint = true
156 if (slidedTrackImage != null)
158 slidedTrackImage.Add(thumbImage);
160 thumbImage.TouchEvent += OnTouchEventForThumb;
162 panGestureDetector = new PanGestureDetector();
163 panGestureDetector.Attach(thumbImage);
164 panGestureDetector.Detected += OnPanGestureDetected;
170 private void OnPanGestureDetected(object source, PanGestureDetector.DetectedEventArgs e)
172 if (e.PanGesture.State == Gesture.StateType.Started)
174 if (direction == DirectionType.Horizontal)
176 currentSlidedOffset = slidedTrackImage.SizeWidth;
178 else if (direction == DirectionType.Vertical)
180 currentSlidedOffset = slidedTrackImage.SizeHeight;
182 UpdateState(isFocused, true);
185 if (e.PanGesture.State == Gesture.StateType.Continuing || e.PanGesture.State == Gesture.StateType.Started)
187 if (direction == DirectionType.Horizontal)
189 CalculateCurrentValueByGesture(e.PanGesture.Displacement.X);
191 else if (direction == DirectionType.Vertical)
193 CalculateCurrentValueByGesture(-e.PanGesture.Displacement.Y);
198 if (e.PanGesture.State == Gesture.StateType.Finished)
200 if (null != slidingFinishedHandler)
202 SlidingFinishedArgs args = new SlidingFinishedArgs();
203 args.CurrentValue = curValue;
204 slidingFinishedHandler(this, args);
207 UpdateState(isFocused, false);
211 // Relayout basic component: track, thumb and indicator
212 private void RelayoutBaseComponent(bool isInitial = true)
214 if (direction == DirectionType.Horizontal)
216 if (slidedTrackImage != null)
218 slidedTrackImage.ParentOrigin = NUI.ParentOrigin.CenterLeft;
219 slidedTrackImage.PivotPoint = NUI.PivotPoint.CenterLeft;
220 slidedTrackImage.PositionUsesPivotPoint = true;
222 if (thumbImage != null)
224 thumbImage.ParentOrigin = NUI.ParentOrigin.CenterRight;
225 thumbImage.PivotPoint = NUI.PivotPoint.Center;
226 thumbImage.PositionUsesPivotPoint = true;
228 if (lowIndicatorImage != null)
230 lowIndicatorImage.ParentOrigin = NUI.ParentOrigin.CenterLeft;
231 lowIndicatorImage.PivotPoint = NUI.PivotPoint.CenterLeft;
232 lowIndicatorImage.PositionUsesPivotPoint = true;
234 if (highIndicatorImage != null)
236 highIndicatorImage.ParentOrigin = NUI.ParentOrigin.CenterRight;
237 highIndicatorImage.PivotPoint = NUI.PivotPoint.CenterRight;
238 highIndicatorImage.PositionUsesPivotPoint = true;
240 if (lowIndicatorText != null)
242 lowIndicatorText.ParentOrigin = NUI.ParentOrigin.CenterLeft;
243 lowIndicatorText.PivotPoint = NUI.PivotPoint.CenterLeft;
244 lowIndicatorText.PositionUsesPivotPoint = true;
246 if (highIndicatorText != null)
248 highIndicatorText.ParentOrigin = NUI.ParentOrigin.CenterRight;
249 highIndicatorText.PivotPoint = NUI.PivotPoint.CenterRight;
250 highIndicatorText.PositionUsesPivotPoint = true;
252 if (panGestureDetector != null)
256 panGestureDetector.RemoveDirection(PanGestureDetector.DirectionVertical);
258 panGestureDetector.AddDirection(PanGestureDetector.DirectionHorizontal);
261 else if (direction == DirectionType.Vertical)
263 if (slidedTrackImage != null)
265 slidedTrackImage.ParentOrigin = NUI.ParentOrigin.BottomCenter;
266 slidedTrackImage.PivotPoint = NUI.PivotPoint.BottomCenter;
267 slidedTrackImage.PositionUsesPivotPoint = true;
269 if (thumbImage != null)
271 thumbImage.ParentOrigin = NUI.ParentOrigin.TopCenter;
272 thumbImage.PivotPoint = NUI.PivotPoint.Center;
273 thumbImage.PositionUsesPivotPoint = true;
275 if (lowIndicatorImage != null)
277 lowIndicatorImage.ParentOrigin = NUI.ParentOrigin.BottomCenter;
278 lowIndicatorImage.PivotPoint = NUI.PivotPoint.BottomCenter;
279 lowIndicatorImage.PositionUsesPivotPoint = true;
281 if (highIndicatorImage != null)
283 highIndicatorImage.ParentOrigin = NUI.ParentOrigin.TopCenter;
284 highIndicatorImage.PivotPoint = NUI.PivotPoint.TopCenter;
285 highIndicatorImage.PositionUsesPivotPoint = true;
287 if (lowIndicatorText != null)
289 lowIndicatorText.ParentOrigin = NUI.ParentOrigin.BottomCenter;
290 lowIndicatorText.PivotPoint = NUI.PivotPoint.BottomCenter;
291 lowIndicatorText.PositionUsesPivotPoint = true;
293 if (highIndicatorText != null)
295 highIndicatorText.ParentOrigin = NUI.ParentOrigin.TopCenter;
296 highIndicatorText.PivotPoint = NUI.PivotPoint.TopCenter;
297 highIndicatorText.PositionUsesPivotPoint = true;
299 if (panGestureDetector != null)
303 panGestureDetector.RemoveDirection(PanGestureDetector.DirectionHorizontal);
305 panGestureDetector.AddDirection(PanGestureDetector.DirectionVertical);
310 private int BgTrackLength()
312 int bgTrackLength = 0;
313 IndicatorType type = CurrentIndicatorType();
315 if (type == IndicatorType.None)
317 if (direction == DirectionType.Horizontal)
319 bgTrackLength = this.Size2D.Width;
321 else if (direction == DirectionType.Vertical)
323 bgTrackLength = this.Size2D.Height;
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)
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;
337 else if (direction == DirectionType.Vertical)
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;
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)
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;
355 else if (direction == DirectionType.Vertical)
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;
362 return bgTrackLength;
365 private void UpdateLowIndicatorSize()
367 if (lowIndicatorSize != null)
369 if (lowIndicatorImage != null)
371 lowIndicatorImage.Size = lowIndicatorSize;
373 if (lowIndicatorText != null)
375 lowIndicatorText.Size = lowIndicatorSize;
380 if (lowIndicatorImage != null && lowIndicatorImage != null && lowIndicatorImage.Size != null)
382 lowIndicatorImage.Size = sliderStyle.LowIndicatorImage.Size;
384 if (lowIndicatorText != null && lowIndicatorText != null && lowIndicatorText.Size != null)
386 lowIndicatorText.Size = sliderStyle.LowIndicator.Size;
391 private void UpdateBgTrackSize()
393 if (bgTrackImage == null)
397 int curTrackThickness = (int)CurrentTrackThickness();
398 int bgTrackLength = BgTrackLength();
399 if (direction == DirectionType.Horizontal)
401 bgTrackImage.Size2D = new Size2D(bgTrackLength, curTrackThickness);
403 else if (direction == DirectionType.Vertical)
405 bgTrackImage.Size2D = new Size2D(curTrackThickness, bgTrackLength);
409 private void UpdateBgTrackPosition()
411 if (bgTrackImage == null)
415 IndicatorType type = CurrentIndicatorType();
417 if (type == IndicatorType.None)
419 bgTrackImage.Position2D = new Position2D(0, 0);
421 else if (type == IndicatorType.Image)
423 Size lowIndicatorImageSize = LowIndicatorImageSize();
424 Size highIndicatorImageSize = HighIndicatorImageSize();
425 int curSpace = (int)CurrentSpaceBetweenTrackAndIndicator();
426 if (direction == DirectionType.Horizontal)
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);
432 else if (direction == DirectionType.Vertical)
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);
439 else if (type == IndicatorType.Text)
441 Size lowIndicatorTextSize = LowIndicatorTextSize();
442 Size highIndicatorTextSize = HighIndicatorTextSize();
443 int curSpace = (int)CurrentSpaceBetweenTrackAndIndicator();
444 if (direction == DirectionType.Horizontal)
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);
450 else if (direction == DirectionType.Vertical)
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));
459 private void UpdateValue()
461 if (slidedTrackImage == null)
465 if (curValue < minValue || curValue > maxValue || minValue >= maxValue)
471 ratio = (float)(curValue - minValue) / (float)(maxValue - minValue);
473 uint curTrackThickness = CurrentTrackThickness();
475 if (direction == DirectionType.Horizontal)
477 if (LayoutDirection == ViewLayoutDirectionType.RTL)
479 ratio = 1.0f - ratio;
481 float slidedTrackLength = (float)BgTrackLength() * ratio;
482 slidedTrackImage.Size2D = new Size2D((int)(slidedTrackLength + round), (int)curTrackThickness); //Add const round to reach Math.Round function.
484 else if (direction == DirectionType.Vertical)
486 float slidedTrackLength = (float)BgTrackLength() * ratio;
487 slidedTrackImage.Size2D = new Size2D((int)(curTrackThickness + round), (int)slidedTrackLength); //Add const round to reach Math.Round function.
491 private uint CurrentTrackThickness()
493 uint curTrackThickness = 0;
494 if (trackThickness != null)
496 curTrackThickness = trackThickness.Value;
500 if (sliderStyle != null && sliderStyle.TrackThickness != null)
502 curTrackThickness = sliderStyle.TrackThickness.Value;
505 return curTrackThickness;
508 private uint CurrentSpaceBetweenTrackAndIndicator()
511 if (spaceBetweenTrackAndIndicator != null)
513 curSpace = spaceBetweenTrackAndIndicator.Start;
517 if (sliderStyle != null && sliderStyle.TrackPadding != null)
519 curSpace = sliderStyle.TrackPadding.Start;
525 private IndicatorType CurrentIndicatorType()
527 IndicatorType type = IndicatorType.None;
528 if (sliderStyle != null)
530 type = (IndicatorType)sliderStyle.IndicatorType;
535 private Size LowIndicatorImageSize()
537 Size size = new Size(0, 0);
538 if (lowIndicatorSize != null)
540 size = lowIndicatorSize;
544 if (sliderStyle != null && sliderStyle.LowIndicatorImage != null && sliderStyle.LowIndicatorImage.Size != null)
546 size = sliderStyle.LowIndicatorImage.Size;
552 private Size HighIndicatorImageSize()
554 Size size = new Size(0, 0);
555 if (highIndicatorSize != null)
557 size = highIndicatorSize;
561 if (sliderStyle != null && sliderStyle.HighIndicatorImage != null && sliderStyle.HighIndicatorImage.Size != null)
563 size = sliderStyle.HighIndicatorImage.Size;
569 private Size LowIndicatorTextSize()
571 Size size = new Size(0, 0);
572 if (lowIndicatorSize != null)
574 size = lowIndicatorSize;
578 if (sliderStyle != null && sliderStyle.LowIndicator != null && sliderStyle.LowIndicator.Size != null)
580 size = sliderStyle.LowIndicator.Size;
586 private Size HighIndicatorTextSize()
588 Size size = new Size(0, 0);
589 if (highIndicatorSize != null)
591 size = highIndicatorSize;
595 if (sliderStyle != null && sliderStyle.HighIndicator != null && sliderStyle.HighIndicator.Size != null)
597 size = sliderStyle.HighIndicator.Size;