1 /*
2  *  Copyright (C) 2005-2018 Team Kodi
3  *  This file is part of Kodi - https://kodi.tv
4  *
5  *  SPDX-License-Identifier: GPL-2.0-or-later
6  *  See LICENSES/README.md for more information.
7  */
8 
9 #pragma once
10 
11 /*!
12 \file GUISliderControl.h
13 \brief
14 */
15 
16 #include "GUIControl.h"
17 #include "GUITexture.h"
18 
19 #include <array>
20 
21 #define SLIDER_CONTROL_TYPE_INT         1
22 #define SLIDER_CONTROL_TYPE_FLOAT       2
23 #define SLIDER_CONTROL_TYPE_PERCENTAGE  3
24 
25 typedef struct
26 {
27   const char *action;
28   const char *formatString;
29   int         infoCode;
30   bool        fireOnDrag;
31 } SliderAction;
32 
33 /*!
34  \ingroup controls
35  \brief
36  */
37 class CGUISliderControl :
38       public CGUIControl
39 {
40 public:
41   typedef enum {
42     RangeSelectorLower = 0,
43     RangeSelectorUpper = 1
44   } RangeSelector;
45 
46   CGUISliderControl(int parentID, int controlID, float posX, float posY, float width, float height, const CTextureInfo& backGroundTexture, const CTextureInfo& mibTexture, const CTextureInfo& nibTextureFocus, int iType, ORIENTATION orientation);
47   ~CGUISliderControl() override = default;
Clone()48   CGUISliderControl *Clone() const override { return new CGUISliderControl(*this); };
49 
50   void Process(unsigned int currentTime, CDirtyRegionList &dirtyregions) override;
51   void Render() override;
52   bool OnAction(const CAction &action) override;
IsActive()53   virtual bool IsActive() const { return true; };
54   void AllocResources() override;
55   void FreeResources(bool immediately = false) override;
56   void DynamicResourceAlloc(bool bOnOff) override;
57   void SetInvalid() override;
58   virtual void SetRange(int iStart, int iEnd);
59   virtual void SetFloatRange(float fStart, float fEnd);
60   bool OnMessage(CGUIMessage& message) override;
61   bool ProcessSelector(CGUITexture* nib,
62                        unsigned int currentTime,
63                        float fScale,
64                        RangeSelector selector);
65   void SetRangeSelection(bool rangeSelection);
GetRangeSelection()66   bool GetRangeSelection() const { return m_rangeSelection; }
67   void SetRangeSelector(RangeSelector selector);
68   void SwitchRangeSelector();
69   void SetInfo(int iInfo);
70   void SetPercentage(float iPercent, RangeSelector selector = RangeSelectorLower, bool updateCurrent = false);
71   float GetPercentage(RangeSelector selector = RangeSelectorLower) const;
72   void SetIntValue(int iValue, RangeSelector selector = RangeSelectorLower, bool updateCurrent = false);
73   int GetIntValue(RangeSelector selector = RangeSelectorLower) const;
74   void SetFloatValue(float fValue, RangeSelector selector = RangeSelectorLower, bool updateCurrent = false);
75   float GetFloatValue(RangeSelector selector = RangeSelectorLower) const;
76   void SetIntInterval(int iInterval);
77   void SetFloatInterval(float fInterval);
SetType(int iType)78   void SetType(int iType) { m_iType = iType; };
GetType()79   int GetType() const { return m_iType; }
80   std::string GetDescription() const override;
SetTextValue(const std::string & textValue)81   void SetTextValue(const std::string &textValue) { m_textValue = textValue; };
82   void SetAction(const std::string &action);
83 
84 protected:
85   CGUISliderControl(const CGUISliderControl& control);
86 
87   bool HitTest(const CPoint &point) const override;
88   EVENT_RESULT OnMouseEvent(const CPoint &point, const CMouseEvent &event) override;
89   bool UpdateColors() override;
90   virtual void Move(int iNumSteps);
91   virtual void SetFromPosition(const CPoint &point, bool guessSelector = false);
92   /*! \brief Get the current position of the slider as a proportion
93    \return slider position in the range [0,1]
94    */
95   float GetProportion(RangeSelector selector = RangeSelectorLower) const;
96 
97   /*! \brief Send a click message (and/or action) to the app in response to a slider move
98    */
99   void SendClick();
100 
101   std::unique_ptr<CGUITexture> m_guiBackground;
102   std::unique_ptr<CGUITexture> m_guiSelectorLower;
103   std::unique_ptr<CGUITexture> m_guiSelectorUpper;
104   std::unique_ptr<CGUITexture> m_guiSelectorLowerFocus;
105   std::unique_ptr<CGUITexture> m_guiSelectorUpperFocus;
106   int m_iType;
107 
108   bool m_rangeSelection;
109   RangeSelector m_currentSelector;
110 
111   std::array<float, 2> m_percentValues;
112 
113   std::array<int, 2> m_intValues;
114   int m_iStart;
115   int m_iInterval;
116   int m_iEnd;
117 
118   std::array<float, 2> m_floatValues;
119   float m_fStart;
120   float m_fInterval;
121   float m_fEnd;
122 
123   int m_iInfoCode;
124   std::string m_textValue; ///< Allows overriding of the text value to be displayed (parent must update when the slider updates)
125   const SliderAction *m_action; ///< Allows the skin to configure the action of a click on the slider \sa SendClick
126   bool m_dragging; ///< Whether we're in a (mouse/touch) drag operation or not - some actions are sent only on release.
127   ORIENTATION m_orientation;
128 };
129 
130