Bläddra i källkod

Sliders will automatically display their current value if "valueTextVisible" is set to true in the form.
"valueTextAlignment" and "valueTextPrecision" properties have also been added.

Adam Blake 13 år sedan
förälder
incheckning
c7e07e01b7
3 ändrade filer med 103 tillägg och 11 borttagningar
  1. 0 2
      gameplay/src/PlatformBlackBerry.cpp
  2. 60 5
      gameplay/src/Slider.cpp
  3. 43 4
      gameplay/src/Slider.h

+ 0 - 2
gameplay/src/PlatformBlackBerry.cpp

@@ -599,8 +599,6 @@ void Platform::pollGamepadState(Gamepad* gamepad)
         // the top at -128.
         // 1 / 128 == 0.0078125f
         // 1 / 127 == 0.0078740157480315f
-        //float x = (float)analog[0] * 0.0078125f;
-        //float y = -(float)analog[1] * 0.0078125f;
         float x = (float)analog[0];
         float y = -(float)analog[1];
         x *= (x < 0) ? 0.0078125f : 0.0078740157480315f;

+ 60 - 5
gameplay/src/Slider.cpp

@@ -9,7 +9,9 @@ static const float SCROLL_FRACTION = 0.1f;
 // e.g. to prevent its parent container from scrolling at the same time.
 static const float SLIDER_THRESHOLD = 5.0f;
 
-Slider::Slider() : _min(0.0f), _max(0.0f), _step(0.0f), _value(0.0f), _minImage(NULL), _maxImage(NULL), _trackImage(NULL), _markerImage(NULL)
+Slider::Slider() : _min(0.0f), _max(0.0f), _step(0.0f), _value(0.0f), _minImage(NULL),
+    _maxImage(NULL), _trackImage(NULL), _markerImage(NULL), _valueTextVisible(false),
+    _valueTextAlignment(Font::ALIGN_BOTTOM_HCENTER), _valueTextPrecision(0), _valueText("")
 {
 }
 
@@ -40,6 +42,13 @@ Slider* Slider::create(Theme::Style* style, Properties* properties)
     slider->_max = properties->getFloat("max");
     slider->_value = properties->getFloat("value");
     slider->_step = properties->getFloat("step");
+    slider->_valueTextVisible = properties->getBool("valueTextVisible");
+    slider->_valueTextPrecision = properties->getInt("valueTextPrecision");
+
+    if (properties->exists("valueTextAlignment"))
+    {
+        slider->_valueTextAlignment = Font::getJustify(properties->getString("valueTextAlignment"));
+    }
 
     return slider;
 }
@@ -49,7 +58,7 @@ void Slider::setMin(float min)
     _min = min;
 }
 
-float Slider::getMin()
+float Slider::getMin() const
 {
     return _min;
 }
@@ -59,7 +68,7 @@ void Slider::setMax(float max)
     _max = max;
 }
 
-float Slider::getMax()
+float Slider::getMax() const
 {
     return _max;
 }
@@ -69,12 +78,12 @@ void Slider::setStep(float step)
     _step = step;
 }
 
-float Slider::getStep()
+float Slider::getStep() const
 {
     return _step;
 }
 
-float Slider::getValue()
+float Slider::getValue() const
 {
     return _value;
 }
@@ -259,6 +268,10 @@ void Slider::update(const Control* container, const Vector2& offset)
     _maxImage = getImage("maxCap", _state);
     _markerImage = getImage("marker", _state);
     _trackImage = getImage("track", _state);
+
+    char s[32];
+    sprintf(s, "%.*f", _valueTextPrecision, _value);
+    _valueText = s;
 }
 
 void Slider::drawImages(SpriteBatch* spriteBatch, const Rectangle& clip)
@@ -314,9 +327,51 @@ void Slider::drawImages(SpriteBatch* spriteBatch, const Rectangle& clip)
     spriteBatch->draw(pos.x, pos.y, markerRegion.width, markerRegion.height, marker.u1, marker.v1, marker.u2, marker.v2, markerColor, _viewportClipBounds);
 }
 
+void Slider::drawText(const Rectangle& clip)
+{
+    Label::drawText(clip);
+
+    if (_valueTextVisible && _font)
+    {
+        _font->start();
+        _font->drawText(_valueText.c_str(), _textBounds, _textColor, getFontSize(_state), _valueTextAlignment, true, getTextRightToLeft(_state), &_viewportClipBounds);
+        _font->finish();
+    }
+}
+
 const char* Slider::getType() const
 {
     return "slider";
 }
 
+void Slider::setValueTextVisible(bool valueTextVisible)
+{
+    _valueTextVisible = valueTextVisible;
+}
+
+bool Slider::getValueTextVisible() const
+{
+    return _valueTextVisible;
+}
+
+void Slider::setValueTextAlignment(Font::Justify alignment)
+{
+    _valueTextAlignment = alignment;
+}
+
+Font::Justify Slider::getValueTextAlignment() const
+{
+    return _valueTextAlignment;
+}
+
+void Slider::setValueTextPrecision(unsigned int precision)
+{
+    _valueTextPrecision = precision;
+}
+
+unsigned int Slider::getValueTextPrecision() const
+{
+    return _valueTextPrecision;
+}
+
 }

+ 43 - 4
gameplay/src/Slider.h

@@ -59,7 +59,7 @@ public:
      *
      * @return The minimum value that can be set on this slider.
      */
-    float getMin();
+    float getMin() const;
 
     /**
      * Set the maximum value that can be set on this slider.
@@ -73,7 +73,7 @@ public:
      *
      * @return The maximum value that can be set on this slider.
      */
-    float getMax();
+    float getMax() const;
 
     /**
      * Set this slider's step size.  If this is greater than zero, the marker
@@ -88,7 +88,7 @@ public:
      *
      * @return This slider's step size.
      */
-    float getStep();
+    float getStep() const;
 
     /**
      * Set this slider's value.  The new value will be clamped to fit within
@@ -103,13 +103,25 @@ public:
      *
      * @return This slider's current value.
      */
-    float getValue();
+    float getValue() const;
 
     /**
      * @see Control::getType
      */
     const char* getType() const;
 
+    void setValueTextVisible(bool displayValue);
+
+    bool getValueTextVisible() const;
+
+    void setValueTextAlignment(Font::Justify alignment);
+
+    Font::Justify getValueTextAlignment() const;
+
+    void setValueTextPrecision(unsigned int precision);
+
+    unsigned int getValueTextPrecision() const;
+
     /**
      * Add a listener to be notified of specific events affecting
      * this control.  Event types can be OR'ed together.
@@ -180,6 +192,13 @@ protected:
      */
     void drawImages(SpriteBatch* spriteBatch, const Rectangle& clip);
 
+    /**
+     * Draw this slider's text.
+     *
+     * @param clip The clipping rectangle of this slider's parent container.
+     */
+    void drawText(const Rectangle& clip);
+
     /**
      * Called when a slider's properties change. Updates this slider's internal rendering
      * properties, such as its text viewport.
@@ -249,6 +268,26 @@ protected:
      */
     Theme::ThemeImage* _markerImage;
 
+    /**
+     * Whether to display this slider's value.
+     */
+    bool _valueTextVisible;
+
+    /**
+     * Alignment of value text.
+     */
+    Font::Justify _valueTextAlignment;
+
+    /**
+     * Number of digits after the decimal to draw for value text.
+     */
+    unsigned int _valueTextPrecision;
+
+    /**
+     * The text displayed by this slider if set to display its value.
+     */
+    std::string _valueText;
+
 private:
 
     /**