summaryrefslogtreecommitdiffstats
path: root/xbmc/guilib/GUISliderControl.cpp
blob: 7fd5b6ea741fe4ea8d8cd2eceb95556a3c59763f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
/*
 *  Copyright (C) 2005-2018 Team Kodi
 *  This file is part of Kodi - https://kodi.tv
 *
 *  SPDX-License-Identifier: GPL-2.0-or-later
 *  See LICENSES/README.md for more information.
 */

#include "GUISliderControl.h"

#include "GUIComponent.h"
#include "GUIInfoManager.h"
#include "GUIWindowManager.h"
#include "ServiceBroker.h"
#include "guilib/guiinfo/GUIInfoLabels.h"
#include "input/Key.h"
#include "input/mouse/MouseStat.h"
#include "utils/MathUtils.h"
#include "utils/StringUtils.h"

static const SliderAction actions[] = {
    {"seek", "PlayerControl(SeekPercentage({:2f}))", PLAYER_PROGRESS, false},
    {"pvr.seek", "PVR.SeekPercentage({:2f})", PVR_TIMESHIFT_PROGRESS_PLAY_POS, false},
    {"volume", "SetVolume({:2f})", PLAYER_VOLUME, true}};

CGUISliderControl::CGUISliderControl(int parentID,
                                     int controlID,
                                     float posX,
                                     float posY,
                                     float width,
                                     float height,
                                     const CTextureInfo& backGroundTexture,
                                     const CTextureInfo& nibTexture,
                                     const CTextureInfo& nibTextureFocus,
                                     int iType,
                                     ORIENTATION orientation)
  : CGUIControl(parentID, controlID, posX, posY, width, height),
    m_guiBackground(CGUITexture::CreateTexture(posX, posY, width, height, backGroundTexture)),
    m_guiSelectorLower(CGUITexture::CreateTexture(posX, posY, width, height, nibTexture)),
    m_guiSelectorUpper(CGUITexture::CreateTexture(posX, posY, width, height, nibTexture)),
    m_guiSelectorLowerFocus(CGUITexture::CreateTexture(posX, posY, width, height, nibTextureFocus)),
    m_guiSelectorUpperFocus(CGUITexture::CreateTexture(posX, posY, width, height, nibTextureFocus))
{
  m_iType = iType;
  m_rangeSelection = false;
  m_currentSelector = RangeSelectorLower; // use lower selector by default
  m_percentValues[0] = 0;
  m_percentValues[1] = 100;
  m_iStart = 0;
  m_iEnd = 100;
  m_iInterval = 1;
  m_fStart = 0.0f;
  m_fEnd = 1.0f;
  m_fInterval = 0.1f;
  m_intValues[0] = m_iStart;
  m_intValues[1] = m_iEnd;
  m_floatValues[0] = m_fStart;
  m_floatValues[1] = m_fEnd;
  ControlType = GUICONTROL_SLIDER;
  m_orientation = orientation;
  m_iInfoCode = 0;
  m_dragging = false;
  m_action = NULL;
}

CGUISliderControl::CGUISliderControl(const CGUISliderControl& control)
  : CGUIControl(control),
    m_guiBackground(control.m_guiBackground->Clone()),
    m_guiSelectorLower(control.m_guiSelectorLower->Clone()),
    m_guiSelectorUpper(control.m_guiSelectorUpper->Clone()),
    m_guiSelectorLowerFocus(control.m_guiSelectorLowerFocus->Clone()),
    m_guiSelectorUpperFocus(control.m_guiSelectorUpperFocus->Clone()),
    m_iType(control.m_iType),
    m_rangeSelection(control.m_rangeSelection),
    m_currentSelector(control.m_currentSelector),
    m_percentValues(control.m_percentValues),
    m_intValues(control.m_intValues),
    m_iStart(control.m_iStart),
    m_iInterval(control.m_iInterval),
    m_iEnd(control.m_iEnd),
    m_floatValues(control.m_floatValues),
    m_fStart(control.m_fStart),
    m_fInterval(control.m_fInterval),
    m_fEnd(control.m_fEnd),
    m_iInfoCode(control.m_iInfoCode),
    m_textValue(control.m_textValue),
    m_action(control.m_action),
    m_dragging(control.m_dragging),
    m_orientation(control.m_orientation)
{
}

void CGUISliderControl::Process(unsigned int currentTime, CDirtyRegionList &dirtyregions)
{
  bool dirty = false;

  dirty |= m_guiBackground->SetPosition(m_posX, m_posY);
  int infoCode = m_iInfoCode;
  if (m_action && (!m_dragging || m_action->fireOnDrag))
    infoCode = m_action->infoCode;
  if (infoCode)
  {
    int val;
    if (CServiceBroker::GetGUI()->GetInfoManager().GetInt(val, infoCode, INFO::DEFAULT_CONTEXT))
      SetIntValue(val);
  }

  dirty |= m_guiBackground->SetHeight(m_height);
  dirty |= m_guiBackground->SetWidth(m_width);
  dirty |= m_guiBackground->Process(currentTime);

  CGUITexture* nibLower =
      (IsActive() && m_bHasFocus && !IsDisabled() && m_currentSelector == RangeSelectorLower)
          ? m_guiSelectorLowerFocus.get()
          : m_guiSelectorLower.get();

  float fScale = 1.0f;

  if (m_orientation == HORIZONTAL && m_guiBackground->GetTextureHeight() != 0)
    fScale = m_height / m_guiBackground->GetTextureHeight();
  else if (m_width != 0 && nibLower->GetTextureWidth() != 0)
    fScale = m_width / nibLower->GetTextureWidth();
  dirty |= ProcessSelector(nibLower, currentTime, fScale, RangeSelectorLower);
  if (m_rangeSelection)
  {
    CGUITexture* nibUpper =
        (IsActive() && m_bHasFocus && !IsDisabled() && m_currentSelector == RangeSelectorUpper)
            ? m_guiSelectorUpperFocus.get()
            : m_guiSelectorUpper.get();

    if (m_orientation == HORIZONTAL && m_guiBackground->GetTextureHeight() != 0)
      fScale = m_height / m_guiBackground->GetTextureHeight();
    else if (m_width != 0 && nibUpper->GetTextureWidth() != 0)
      fScale = m_width / nibUpper->GetTextureWidth();

    dirty |= ProcessSelector(nibUpper, currentTime, fScale, RangeSelectorUpper);
  }

  if (dirty)
    MarkDirtyRegion();

  CGUIControl::Process(currentTime, dirtyregions);
}

bool CGUISliderControl::ProcessSelector(CGUITexture* nib,
                                        unsigned int currentTime,
                                        float fScale,
                                        RangeSelector selector)
{
  bool dirty = false;
  // we render the nib centered at the appropriate percentage, except where the nib
  // would overflow the background image
  if (m_orientation == HORIZONTAL)
  {
    dirty |= nib->SetHeight(nib->GetTextureHeight() * fScale);
    dirty |= nib->SetWidth(nib->GetHeight() * 2);
  }
  else
  {
    dirty |= nib->SetWidth(nib->GetTextureWidth() * fScale);
    dirty |= nib->SetHeight(nib->GetWidth() * 2);
  }
  CAspectRatio ratio(CAspectRatio::AR_KEEP);
  ratio.align = ASPECT_ALIGN_LEFT | ASPECT_ALIGNY_CENTER;
  dirty |= nib->SetAspectRatio(ratio);
  dirty |= nib->Process(currentTime);
  CRect rect = nib->GetRenderRect();

  float offset;
  if (m_orientation == HORIZONTAL)
  {
    offset = GetProportion(selector) * m_width - rect.Width() / 2;
    if (offset > m_width - rect.Width())
      offset = m_width - rect.Width();
    if (offset < 0)
      offset = 0;
    dirty |=
        nib->SetPosition(m_guiBackground->GetXPosition() + offset, m_guiBackground->GetYPosition());
  }
  else
  {
    offset = GetProportion(selector) * m_height - rect.Height() / 2;
    if (offset > m_height - rect.Height())
      offset = m_height - rect.Height();
    if (offset < 0)
      offset = 0;
    dirty |=
        nib->SetPosition(m_guiBackground->GetXPosition(),
                         m_guiBackground->GetYPosition() + m_guiBackground->GetHeight() - offset -
                             ((nib->GetHeight() - rect.Height()) / 2 + rect.Height()));
  }
  dirty |= nib->Process(currentTime); // need to process again as the position may have changed

  return dirty;
}

void CGUISliderControl::Render()
{
  m_guiBackground->Render();
  CGUITexture* nibLower =
      (IsActive() && m_bHasFocus && !IsDisabled() && m_currentSelector == RangeSelectorLower)
          ? m_guiSelectorLowerFocus.get()
          : m_guiSelectorLower.get();
  nibLower->Render();
  if (m_rangeSelection)
  {
    CGUITexture* nibUpper =
        (IsActive() && m_bHasFocus && !IsDisabled() && m_currentSelector == RangeSelectorUpper)
            ? m_guiSelectorUpperFocus.get()
            : m_guiSelectorUpper.get();
    nibUpper->Render();
  }
  CGUIControl::Render();
}

bool CGUISliderControl::OnMessage(CGUIMessage& message)
{
  if (message.GetControlId() == GetID() )
  {
    switch (message.GetMessage())
    {
    case GUI_MSG_ITEM_SELECT:
      SetPercentage( (float)message.GetParam1() );
      return true;
      break;

    case GUI_MSG_LABEL_RESET:
      {
        SetPercentage(0, RangeSelectorLower);
        SetPercentage(100, RangeSelectorUpper);
        return true;
      }
      break;
    }
  }

  return CGUIControl::OnMessage(message);
}

bool CGUISliderControl::OnAction(const CAction &action)
{
  switch ( action.GetID() )
  {
  case ACTION_MOVE_LEFT:
    if (IsActive() && m_orientation == HORIZONTAL)
    {
      Move(-1);
      return true;
    }
    break;

  case ACTION_MOVE_RIGHT:
    if (IsActive() && m_orientation == HORIZONTAL)
    {
      Move(1);
      return true;
    }
    break;

  case ACTION_MOVE_UP:
    if (IsActive() && m_orientation == VERTICAL)
    {
      Move(1);
      return true;
    }
    break;

  case ACTION_MOVE_DOWN:
    if (IsActive() && m_orientation == VERTICAL)
    {
      Move(-1);
      return true;
    }
    break;

  case ACTION_SELECT_ITEM:
    if (m_rangeSelection)
      SwitchRangeSelector();
    return true;

  default:
    break;
  }
  return CGUIControl::OnAction(action);
}

void CGUISliderControl::Move(int iNumSteps)
{
  bool rangeSwap = false;
  switch (m_iType)
  {
  case SLIDER_CONTROL_TYPE_FLOAT:
    {
      float &value = m_floatValues[m_currentSelector];
      value += m_fInterval * iNumSteps;
      if (value < m_fStart) value = m_fStart;
      if (value > m_fEnd) value = m_fEnd;
      if (m_floatValues[0] > m_floatValues[1])
      {
        float valueLower = m_floatValues[0];
        m_floatValues[0] = m_floatValues[1];
        m_floatValues[1] = valueLower;
        rangeSwap = true;
      }
      break;
    }

  case SLIDER_CONTROL_TYPE_INT:
    {
      int &value = m_intValues[m_currentSelector];
      value += m_iInterval * iNumSteps;
      if (value < m_iStart) value = m_iStart;
      if (value > m_iEnd) value = m_iEnd;
      if (m_intValues[0] > m_intValues[1])
      {
        int valueLower = m_intValues[0];
        m_intValues[0] = m_intValues[1];
        m_intValues[1] = valueLower;
        rangeSwap = true;
      }
      break;
    }

  case SLIDER_CONTROL_TYPE_PERCENTAGE:
  default:
    {
      float &value = m_percentValues[m_currentSelector];
      value += m_iInterval * iNumSteps;
      if (value < 0) value = 0;
      if (value > 100) value = 100;
      if (m_percentValues[0] > m_percentValues[1])
      {
        float valueLower = m_percentValues[0];
        m_percentValues[0] = m_percentValues[1];
        m_percentValues[1] = valueLower;
        rangeSwap = true;
      }
      break;
    }
  }

  if (rangeSwap)
    SwitchRangeSelector();

  SendClick();
}

void CGUISliderControl::SendClick()
{
  float percent = 100*GetProportion();
  SEND_CLICK_MESSAGE(GetID(), GetParentID(), MathUtils::round_int(static_cast<double>(percent)));
  if (m_action && (!m_dragging || m_action->fireOnDrag))
  {
    std::string action = StringUtils::Format(m_action->formatString, percent);
    CGUIMessage message(GUI_MSG_EXECUTE, m_controlID, m_parentID);
    message.SetStringParam(action);
    CServiceBroker::GetGUI()->GetWindowManager().SendMessage(message);
  }
}

void CGUISliderControl::SetRangeSelection(bool rangeSelection)
{
  if (m_rangeSelection == rangeSelection)
    return;

  m_rangeSelection = rangeSelection;
  SetRangeSelector(RangeSelectorLower);
  SetInvalid();
}

void CGUISliderControl::SetRangeSelector(RangeSelector selector)
{
  if (m_currentSelector == selector)
    return;

  m_currentSelector = selector;
  SetInvalid();
}

void CGUISliderControl::SwitchRangeSelector()
{
  if (m_currentSelector == RangeSelectorLower)
    SetRangeSelector(RangeSelectorUpper);
  else
    SetRangeSelector(RangeSelectorLower);
}

void CGUISliderControl::SetPercentage(float percent, RangeSelector selector /* = RangeSelectorLower */, bool updateCurrent /* = false */)
{
  if (percent > 100) percent = 100;
  else if (percent < 0) percent = 0;

  float percentLower = selector == RangeSelectorLower ? percent : m_percentValues[0];
  float percentUpper = selector == RangeSelectorUpper ? percent : m_percentValues[1];
  const float oldValues[2] = {m_percentValues[0], m_percentValues[1]};

  if (!m_rangeSelection || percentLower <= percentUpper)
  {
    m_percentValues[0] = percentLower;
    m_percentValues[1] = percentUpper;
    if (updateCurrent)
      m_currentSelector = selector;
  }
  else
  {
    m_percentValues[0] = percentUpper;
    m_percentValues[1] = percentLower;
    if (updateCurrent)
        m_currentSelector = (selector == RangeSelectorLower ? RangeSelectorUpper : RangeSelectorLower);
  }
  if (oldValues[0] != m_percentValues[0] || oldValues[1] != m_percentValues[1])
    MarkDirtyRegion();
}

float CGUISliderControl::GetPercentage(RangeSelector selector /* = RangeSelectorLower */) const
{
  return m_percentValues[selector];
}

void CGUISliderControl::SetIntValue(int iValue, RangeSelector selector /* = RangeSelectorLower */, bool updateCurrent /* = false */)
{
  if (m_iType == SLIDER_CONTROL_TYPE_FLOAT)
    SetFloatValue((float)iValue, selector, updateCurrent);
  else if (m_iType == SLIDER_CONTROL_TYPE_INT)
  {
    if (iValue > m_iEnd) iValue = m_iEnd;
    else if (iValue < m_iStart) iValue = m_iStart;

    int iValueLower = selector == RangeSelectorLower ? iValue : m_intValues[0];
    int iValueUpper = selector == RangeSelectorUpper ? iValue : m_intValues[1];

    if (!m_rangeSelection || iValueLower <= iValueUpper)
    {
      m_intValues[0] = iValueLower;
      m_intValues[1] = iValueUpper;
      if (updateCurrent)
        m_currentSelector = selector;
    }
    else
    {
      m_intValues[0] = iValueUpper;
      m_intValues[1] = iValueLower;
      if (updateCurrent)
        m_currentSelector = (selector == RangeSelectorLower ? RangeSelectorUpper : RangeSelectorLower);
    }
  }
  else
    SetPercentage((float)iValue, selector, updateCurrent);
}

int CGUISliderControl::GetIntValue(RangeSelector selector /* = RangeSelectorLower */) const
{
  if (m_iType == SLIDER_CONTROL_TYPE_FLOAT)
    return (int)m_floatValues[selector];
  else if (m_iType == SLIDER_CONTROL_TYPE_INT)
    return m_intValues[selector];
  else
    return MathUtils::round_int(static_cast<double>(m_percentValues[selector]));
}

void CGUISliderControl::SetFloatValue(float fValue, RangeSelector selector /* = RangeSelectorLower */, bool updateCurrent /* = false */)
{
  if (m_iType == SLIDER_CONTROL_TYPE_FLOAT)
  {
    if (fValue > m_fEnd) fValue = m_fEnd;
    else if (fValue < m_fStart) fValue = m_fStart;

    float fValueLower = selector == RangeSelectorLower ? fValue : m_floatValues[0];
    float fValueUpper = selector == RangeSelectorUpper ? fValue : m_floatValues[1];

    if (!m_rangeSelection || fValueLower <= fValueUpper)
    {
      m_floatValues[0] = fValueLower;
      m_floatValues[1] = fValueUpper;
      if (updateCurrent)
        m_currentSelector = selector;
    }
    else
    {
      m_floatValues[0] = fValueUpper;
      m_floatValues[1] = fValueLower;
      if (updateCurrent)
        m_currentSelector = (selector == RangeSelectorLower ? RangeSelectorUpper : RangeSelectorLower);
    }
  }
  else if (m_iType == SLIDER_CONTROL_TYPE_INT)
    SetIntValue((int)fValue, selector, updateCurrent);
  else
    SetPercentage(fValue, selector, updateCurrent);
}

float CGUISliderControl::GetFloatValue(RangeSelector selector /* = RangeSelectorLower */) const
{
  if (m_iType == SLIDER_CONTROL_TYPE_FLOAT)
    return m_floatValues[selector];
  else if (m_iType == SLIDER_CONTROL_TYPE_INT)
    return (float)m_intValues[selector];
  else
    return m_percentValues[selector];
}

void CGUISliderControl::SetIntInterval(int iInterval)
{
  if (m_iType == SLIDER_CONTROL_TYPE_FLOAT)
    m_fInterval = (float)iInterval;
  else
    m_iInterval = iInterval;
}

void CGUISliderControl::SetFloatInterval(float fInterval)
{
  if (m_iType == SLIDER_CONTROL_TYPE_FLOAT)
    m_fInterval = fInterval;
  else
    m_iInterval = (int)fInterval;
}

void CGUISliderControl::SetRange(int iStart, int iEnd)
{
  if (m_iType == SLIDER_CONTROL_TYPE_FLOAT)
    SetFloatRange((float)iStart,(float)iEnd);
  else
  {
    m_intValues[0] = m_iStart = iStart;
    m_intValues[1] = m_iEnd = iEnd;
  }
}

void CGUISliderControl::SetFloatRange(float fStart, float fEnd)
{
  if (m_iType == SLIDER_CONTROL_TYPE_INT)
    SetRange((int)fStart, (int)fEnd);
  else
  {
    m_floatValues[0] = m_fStart = fStart;
    m_floatValues[1] = m_fEnd = fEnd;
  }
}

void CGUISliderControl::FreeResources(bool immediately)
{
  CGUIControl::FreeResources(immediately);
  m_guiBackground->FreeResources(immediately);
  m_guiSelectorLower->FreeResources(immediately);
  m_guiSelectorUpper->FreeResources(immediately);
  m_guiSelectorLowerFocus->FreeResources(immediately);
  m_guiSelectorUpperFocus->FreeResources(immediately);
}

void CGUISliderControl::DynamicResourceAlloc(bool bOnOff)
{
  CGUIControl::DynamicResourceAlloc(bOnOff);
  m_guiBackground->DynamicResourceAlloc(bOnOff);
  m_guiSelectorLower->DynamicResourceAlloc(bOnOff);
  m_guiSelectorUpper->DynamicResourceAlloc(bOnOff);
  m_guiSelectorLowerFocus->DynamicResourceAlloc(bOnOff);
  m_guiSelectorUpperFocus->DynamicResourceAlloc(bOnOff);
}

void CGUISliderControl::AllocResources()
{
  CGUIControl::AllocResources();
  m_guiBackground->AllocResources();
  m_guiSelectorLower->AllocResources();
  m_guiSelectorUpper->AllocResources();
  m_guiSelectorLowerFocus->AllocResources();
  m_guiSelectorUpperFocus->AllocResources();
}

void CGUISliderControl::SetInvalid()
{
  CGUIControl::SetInvalid();
  m_guiBackground->SetInvalid();
  m_guiSelectorLower->SetInvalid();
  m_guiSelectorUpper->SetInvalid();
  m_guiSelectorLowerFocus->SetInvalid();
  m_guiSelectorUpperFocus->SetInvalid();
}

bool CGUISliderControl::HitTest(const CPoint &point) const
{
  if (m_guiBackground->HitTest(point))
    return true;
  if (m_guiSelectorLower->HitTest(point))
    return true;
  if (m_rangeSelection && m_guiSelectorUpper->HitTest(point))
    return true;
  return false;
}

void CGUISliderControl::SetFromPosition(const CPoint &point, bool guessSelector /* = false */)
{

  float fPercent;
  if (m_orientation == HORIZONTAL)
    fPercent = (point.x - m_guiBackground->GetXPosition()) / m_guiBackground->GetWidth();
  else
    fPercent = (m_guiBackground->GetYPosition() + m_guiBackground->GetHeight() - point.y) /
               m_guiBackground->GetHeight();

  if (fPercent < 0) fPercent = 0;
  if (fPercent > 1) fPercent = 1;

  if (m_rangeSelection && guessSelector)
  {
    // choose selector which value is closer to value calculated from position
    if (fabs(GetPercentage(RangeSelectorLower) - 100 * fPercent) <= fabs(GetPercentage(RangeSelectorUpper) - 100 * fPercent))
      m_currentSelector = RangeSelectorLower;
    else
      m_currentSelector = RangeSelectorUpper;
  }

  switch (m_iType)
  {
    case SLIDER_CONTROL_TYPE_FLOAT:
    {
      float fValue = m_fStart + (m_fEnd - m_fStart) * fPercent;
      SetFloatValue(MathUtils::RoundF(fValue, m_fInterval), m_currentSelector, true);
      break;
    }

  case SLIDER_CONTROL_TYPE_INT:
    {
      int iValue = (int)(m_iStart + (float)(m_iEnd - m_iStart) * fPercent + 0.49f);
      SetIntValue(iValue, m_currentSelector, true);
      break;
    }

  case SLIDER_CONTROL_TYPE_PERCENTAGE:
  default:
    {
      SetPercentage(fPercent * 100, m_currentSelector, true);
      break;
    }
  }
  SendClick();
}

EVENT_RESULT CGUISliderControl::OnMouseEvent(const CPoint &point, const CMouseEvent &event)
{
  m_dragging = false;
  if (event.m_id == ACTION_MOUSE_DRAG || event.m_id == ACTION_MOUSE_DRAG_END)
  {
    m_dragging = true;
    bool guessSelector = false;
    if (static_cast<HoldAction>(event.m_state) == HoldAction::DRAG)
    { // grab exclusive access
      CGUIMessage msg(GUI_MSG_EXCLUSIVE_MOUSE, GetID(), GetParentID());
      SendWindowMessage(msg);
      guessSelector = true;
    }
    else if (static_cast<HoldAction>(event.m_state) == HoldAction::DRAG_END)
    { // release exclusive access
      m_dragging = false;
      CGUIMessage msg(GUI_MSG_EXCLUSIVE_MOUSE, 0, GetParentID());
      SendWindowMessage(msg);
    }
    SetFromPosition(point, guessSelector);
    return EVENT_RESULT_HANDLED;
  }
  else if (event.m_id == ACTION_MOUSE_LEFT_CLICK && m_guiBackground->HitTest(point))
  {
    SetFromPosition(point, true);
    return EVENT_RESULT_HANDLED;
  }
  else if (event.m_id == ACTION_MOUSE_WHEEL_UP)
  {
    if (m_guiBackground->HitTest(point))
    {
      Move(10);
      return EVENT_RESULT_HANDLED;
    }
  }
  else if (event.m_id == ACTION_MOUSE_WHEEL_DOWN)
  {
    if (m_guiBackground->HitTest(point))
    {
      Move(-10);
      return EVENT_RESULT_HANDLED;
    }
  }
  else if (event.m_id == ACTION_GESTURE_NOTIFY)
  {
    return EVENT_RESULT_PAN_HORIZONTAL_WITHOUT_INERTIA;
  }
  else if (event.m_id == ACTION_GESTURE_BEGIN)
  { // grab exclusive access
    CGUIMessage msg(GUI_MSG_EXCLUSIVE_MOUSE, GetID(), GetParentID());
    SendWindowMessage(msg);
    return EVENT_RESULT_HANDLED;
  }
  else if (event.m_id == ACTION_GESTURE_PAN)
  { // do the drag
    SetFromPosition(point);
    return EVENT_RESULT_HANDLED;
  }
  else if (event.m_id == ACTION_GESTURE_END || event.m_id == ACTION_GESTURE_ABORT)
  { // release exclusive access
    CGUIMessage msg(GUI_MSG_EXCLUSIVE_MOUSE, 0, GetParentID());
    SendWindowMessage(msg);
    return EVENT_RESULT_HANDLED;
  }
  return EVENT_RESULT_UNHANDLED;
}

void CGUISliderControl::SetInfo(int iInfo)
{
  m_iInfoCode = iInfo;
}

std::string CGUISliderControl::GetDescription() const
{
  if (!m_textValue.empty())
    return m_textValue;
  std::string description;
  if (m_iType == SLIDER_CONTROL_TYPE_FLOAT)
  {
    if (m_rangeSelection)
      description = StringUtils::Format("[{:2.2f}, {:2.2f}]", m_floatValues[0], m_floatValues[1]);
    else
      description = StringUtils::Format("{:2.2f}", m_floatValues[0]);
  }
  else if (m_iType == SLIDER_CONTROL_TYPE_INT)
  {
    if (m_rangeSelection)
      description = StringUtils::Format("[{}, {}]", m_intValues[0], m_intValues[1]);
    else
      description = std::to_string(m_intValues[0]);
  }
  else
  {
    if (m_rangeSelection)
      description = StringUtils::Format("[{}%, {}%]", MathUtils::round_int(static_cast<double>(m_percentValues[0])),
                                        MathUtils::round_int(static_cast<double>(m_percentValues[1])));
    else
      description = StringUtils::Format("{}%", MathUtils::round_int(static_cast<double>(m_percentValues[0])));
  }
  return description;
}

bool CGUISliderControl::UpdateColors(const CGUIListItem* item)
{
  bool changed = CGUIControl::UpdateColors(nullptr);
  changed |= m_guiBackground->SetDiffuseColor(m_diffuseColor);
  changed |= m_guiSelectorLower->SetDiffuseColor(m_diffuseColor);
  changed |= m_guiSelectorUpper->SetDiffuseColor(m_diffuseColor);
  changed |= m_guiSelectorLowerFocus->SetDiffuseColor(m_diffuseColor);
  changed |= m_guiSelectorUpperFocus->SetDiffuseColor(m_diffuseColor);

  return changed;
}

float CGUISliderControl::GetProportion(RangeSelector selector /* = RangeSelectorLower */) const
{
  if (m_iType == SLIDER_CONTROL_TYPE_FLOAT)
    return m_fStart != m_fEnd ? (GetFloatValue(selector) - m_fStart) / (m_fEnd - m_fStart) : 0.0f;
  else if (m_iType == SLIDER_CONTROL_TYPE_INT)
    return m_iStart != m_iEnd ? (float)(GetIntValue(selector) - m_iStart) / (float)(m_iEnd - m_iStart) : 0.0f;
  return 0.01f * GetPercentage(selector);
}

void CGUISliderControl::SetAction(const std::string &action)
{
  for (const SliderAction& a : actions)
  {
    if (StringUtils::EqualsNoCase(action, a.action))
    {
      m_action = &a;
      return;
    }
  }
  m_action = NULL;
}