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 StringSelector thumbImageURLSelector = new StringSelector();
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 UpdateState(isFocused, false);
213 // Relayout basic component: track, thumb and indicator
214 private void RelayoutBaseComponent(bool isInitial = true)
216 if (direction == DirectionType.Horizontal)
218 if (slidedTrackImage != null)
220 slidedTrackImage.ParentOrigin = NUI.ParentOrigin.CenterLeft;
221 slidedTrackImage.PivotPoint = NUI.PivotPoint.CenterLeft;
222 slidedTrackImage.PositionUsesPivotPoint = true;
224 if (thumbImage != null)
226 thumbImage.ParentOrigin = NUI.ParentOrigin.CenterRight;
227 thumbImage.PivotPoint = NUI.PivotPoint.Center;
228 thumbImage.PositionUsesPivotPoint = true;
230 if (lowIndicatorImage != null)
232 lowIndicatorImage.ParentOrigin = NUI.ParentOrigin.CenterLeft;
233 lowIndicatorImage.PivotPoint = NUI.PivotPoint.CenterLeft;
234 lowIndicatorImage.PositionUsesPivotPoint = true;
236 if (highIndicatorImage != null)
238 highIndicatorImage.ParentOrigin = NUI.ParentOrigin.CenterRight;
239 highIndicatorImage.PivotPoint = NUI.PivotPoint.CenterRight;
240 highIndicatorImage.PositionUsesPivotPoint = true;
242 if (lowIndicatorText != null)
244 lowIndicatorText.ParentOrigin = NUI.ParentOrigin.CenterLeft;
245 lowIndicatorText.PivotPoint = NUI.PivotPoint.CenterLeft;
246 lowIndicatorText.PositionUsesPivotPoint = true;
248 if (highIndicatorText != null)
250 highIndicatorText.ParentOrigin = NUI.ParentOrigin.CenterRight;
251 highIndicatorText.PivotPoint = NUI.PivotPoint.CenterRight;
252 highIndicatorText.PositionUsesPivotPoint = true;
254 if (panGestureDetector != null)
258 panGestureDetector.RemoveDirection(PanGestureDetector.DirectionVertical);
260 panGestureDetector.AddDirection(PanGestureDetector.DirectionHorizontal);
263 else if (direction == DirectionType.Vertical)
265 if (slidedTrackImage != null)
267 slidedTrackImage.ParentOrigin = NUI.ParentOrigin.BottomCenter;
268 slidedTrackImage.PivotPoint = NUI.PivotPoint.BottomCenter;
269 slidedTrackImage.PositionUsesPivotPoint = true;
271 if (thumbImage != null)
273 thumbImage.ParentOrigin = NUI.ParentOrigin.TopCenter;
274 thumbImage.PivotPoint = NUI.PivotPoint.Center;
275 thumbImage.PositionUsesPivotPoint = true;
277 if (lowIndicatorImage != null)
279 lowIndicatorImage.ParentOrigin = NUI.ParentOrigin.BottomCenter;
280 lowIndicatorImage.PivotPoint = NUI.PivotPoint.BottomCenter;
281 lowIndicatorImage.PositionUsesPivotPoint = true;
283 if (highIndicatorImage != null)
285 highIndicatorImage.ParentOrigin = NUI.ParentOrigin.TopCenter;
286 highIndicatorImage.PivotPoint = NUI.PivotPoint.TopCenter;
287 highIndicatorImage.PositionUsesPivotPoint = true;
289 if (lowIndicatorText != null)
291 lowIndicatorText.ParentOrigin = NUI.ParentOrigin.BottomCenter;
292 lowIndicatorText.PivotPoint = NUI.PivotPoint.BottomCenter;
293 lowIndicatorText.PositionUsesPivotPoint = true;
295 if (highIndicatorText != null)
297 highIndicatorText.ParentOrigin = NUI.ParentOrigin.TopCenter;
298 highIndicatorText.PivotPoint = NUI.PivotPoint.TopCenter;
299 highIndicatorText.PositionUsesPivotPoint = true;
301 if (panGestureDetector != null)
305 panGestureDetector.RemoveDirection(PanGestureDetector.DirectionHorizontal);
307 panGestureDetector.AddDirection(PanGestureDetector.DirectionVertical);
312 private int BgTrackLength()
314 int bgTrackLength = 0;
315 IndicatorType type = CurrentIndicatorType();
317 if (type == IndicatorType.None)
319 if (direction == DirectionType.Horizontal)
321 bgTrackLength = this.Size2D.Width;
323 else if (direction == DirectionType.Vertical)
325 bgTrackLength = this.Size2D.Height;
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)
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;
339 else if (direction == DirectionType.Vertical)
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;
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)
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;
357 else if (direction == DirectionType.Vertical)
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;
364 return bgTrackLength;
367 private void UpdateLowIndicatorSize()
369 if (lowIndicatorSize != null)
371 if (lowIndicatorImage != null)
373 lowIndicatorImage.Size = lowIndicatorSize;
375 if (lowIndicatorText != null)
377 lowIndicatorText.Size = lowIndicatorSize;
382 if (lowIndicatorImage != null && Style != null && Style.LowIndicatorImage != null && Style.LowIndicatorImage.Size != null)
384 lowIndicatorImage.Size = Style.LowIndicatorImage.Size;
386 if (lowIndicatorText != null && Style != null && Style.LowIndicator != null && Style.LowIndicator.Size != null)
388 lowIndicatorText.Size = Style.LowIndicator.Size;
393 private void UpdateBgTrackSize()
395 if (bgTrackImage == null)
399 int curTrackThickness = (int)CurrentTrackThickness();
400 int bgTrackLength = BgTrackLength();
401 if (direction == DirectionType.Horizontal)
403 bgTrackImage.Size2D = new Size2D(bgTrackLength, curTrackThickness);
405 else if (direction == DirectionType.Vertical)
407 bgTrackImage.Size2D = new Size2D(curTrackThickness, bgTrackLength);
411 private void UpdateBgTrackPosition()
413 if (bgTrackImage == null)
417 IndicatorType type = CurrentIndicatorType();
419 if (type == IndicatorType.None)
421 bgTrackImage.Position2D = new Position2D(0, 0);
423 else if (type == IndicatorType.Image)
425 Size lowIndicatorImageSize = LowIndicatorImageSize();
426 Size highIndicatorImageSize = HighIndicatorImageSize();
427 int curSpace = (int)CurrentSpaceBetweenTrackAndIndicator();
428 if (direction == DirectionType.Horizontal)
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);
434 else if (direction == DirectionType.Vertical)
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);
441 else if (type == IndicatorType.Text)
443 Size lowIndicatorTextSize = LowIndicatorTextSize();
444 Size highIndicatorTextSize = HighIndicatorTextSize();
445 int curSpace = (int)CurrentSpaceBetweenTrackAndIndicator();
446 if (direction == DirectionType.Horizontal)
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);
452 else if (direction == DirectionType.Vertical)
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));
461 private void UpdateValue()
463 if (slidedTrackImage == null)
467 if (curValue < minValue || curValue > maxValue || minValue >= maxValue)
473 ratio = (float)(curValue - minValue) / (float)(maxValue - minValue);
475 uint curTrackThickness = CurrentTrackThickness();
477 if (direction == DirectionType.Horizontal)
479 if (LayoutDirection == ViewLayoutDirectionType.RTL)
481 ratio = 1.0f - ratio;
483 float slidedTrackLength = (float)BgTrackLength() * ratio;
484 slidedTrackImage.Size2D = new Size2D((int)(slidedTrackLength + round), (int)curTrackThickness); //Add const round to reach Math.Round function.
486 else if (direction == DirectionType.Vertical)
488 float slidedTrackLength = (float)BgTrackLength() * ratio;
489 slidedTrackImage.Size2D = new Size2D((int)(curTrackThickness + round), (int)slidedTrackLength); //Add const round to reach Math.Round function.
493 private uint CurrentTrackThickness()
495 uint curTrackThickness = 0;
496 if (trackThickness != null)
498 curTrackThickness = trackThickness.Value;
502 if (Style != null && Style.TrackThickness != null)
504 curTrackThickness = Style.TrackThickness.Value;
507 return curTrackThickness;
510 private uint CurrentSpaceBetweenTrackAndIndicator()
513 if (spaceBetweenTrackAndIndicator != null)
515 curSpace = spaceBetweenTrackAndIndicator.Start;
519 if (Style != null && Style.TrackPadding != null)
521 curSpace = Style.TrackPadding.Start;
527 private IndicatorType CurrentIndicatorType()
529 IndicatorType type = IndicatorType.None;
532 type = (IndicatorType)Style.IndicatorType;
537 private Size LowIndicatorImageSize()
539 Size size = new Size(0, 0);
540 if (lowIndicatorSize != null)
542 size = lowIndicatorSize;
546 if (Style != null && Style.LowIndicatorImage != null && Style.LowIndicatorImage.Size != null)
548 size = Style.LowIndicatorImage.Size;
554 private Size HighIndicatorImageSize()
556 Size size = new Size(0, 0);
557 if (highIndicatorSize != null)
559 size = highIndicatorSize;
563 if (Style != null && Style.HighIndicatorImage != null && Style.HighIndicatorImage.Size != null)
565 size = Style.HighIndicatorImage.Size;
571 private Size LowIndicatorTextSize()
573 Size size = new Size(0, 0);
574 if (lowIndicatorSize != null)
576 size = lowIndicatorSize;
580 if (Style != null && Style.LowIndicator != null && Style.LowIndicator.Size != null)
582 size = Style.LowIndicator.Size;
588 private Size HighIndicatorTextSize()
590 Size size = new Size(0, 0);
591 if (highIndicatorSize != null)
593 size = highIndicatorSize;
597 if (Style != null && Style.HighIndicator != null && Style.HighIndicator.Size != null)
599 size = Style.HighIndicator.Size;