소스 검색

Adding fade-out animation to scrollbars.
Adding an option allowing scrollbars to be always visible.

Adam Blake 13 년 전
부모
커밋
9c1002629d
2개의 변경된 파일124개의 추가작업 그리고 5개의 파일을 삭제
  1. 81 3
      gameplay/src/Container.cpp
  2. 43 2
      gameplay/src/Container.h

+ 81 - 3
gameplay/src/Container.cpp

@@ -31,6 +31,7 @@ Container::Container()
     : _layout(NULL), _scrollBarTopCap(NULL), _scrollBarVertical(NULL), _scrollBarBottomCap(NULL),
     : _layout(NULL), _scrollBarTopCap(NULL), _scrollBarVertical(NULL), _scrollBarBottomCap(NULL),
       _scrollBarLeftCap(NULL), _scrollBarHorizontal(NULL), _scrollBarRightCap(NULL),
       _scrollBarLeftCap(NULL), _scrollBarHorizontal(NULL), _scrollBarRightCap(NULL),
       _scroll(SCROLL_NONE), _scrollBarBounds(Rectangle::empty()), _scrollPosition(Vector2::zero()),
       _scroll(SCROLL_NONE), _scrollBarBounds(Rectangle::empty()), _scrollPosition(Vector2::zero()),
+      _scrollBarsAlwaysVisible(false), _scrollBarOpacity(1.0f),
       _scrolling(false), _firstX(0), _firstY(0),
       _scrolling(false), _firstX(0), _firstY(0),
       _lastX(0), _lastY(0),
       _lastX(0), _lastY(0),
       _startTimeX(0), _startTimeY(0), _lastTime(0),
       _startTimeX(0), _startTimeY(0), _lastTime(0),
@@ -84,7 +85,13 @@ Container* Container::create(Theme::Style* style, Properties* properties, Theme*
     Container* container = Container::create(getLayoutType(layoutString));
     Container* container = Container::create(getLayoutType(layoutString));
     container->initialize(style, properties);
     container->initialize(style, properties);
     container->_scroll = getScroll(properties->getString("scroll"));
     container->_scroll = getScroll(properties->getString("scroll"));
+    container->_scrollBarsAlwaysVisible = properties->getBool("scrollBarsAlwaysVisible");
+    if (!container->_scrollBarsAlwaysVisible)
+    {
+        container->_scrollBarOpacity = 0.0f;
+    }
     container->addControls(theme, properties);
     container->addControls(theme, properties);
+    container->_layout->update(container, container->_scrollPosition);
 
 
     return container;
     return container;
 }
 }
@@ -265,6 +272,20 @@ Container::Scroll Container::getScroll() const
     return _scroll;
     return _scroll;
 }
 }
 
 
+void Container::setScrollBarsAlwaysVisible(bool alwaysVisible)
+{
+    if (alwaysVisible != _scrollBarsAlwaysVisible)
+    {
+        _scrollBarsAlwaysVisible = alwaysVisible;
+        _dirty = true;
+    }
+}
+
+bool Container::getScrollBarsAlwaysVisible() const
+{
+    return _scrollBarsAlwaysVisible;
+}
+
 Animation* Container::getAnimation(const char* id) const
 Animation* Container::getAnimation(const char* id) const
 {
 {
     std::vector<Control*>::const_iterator itr = _controls.begin();
     std::vector<Control*>::const_iterator itr = _controls.begin();
@@ -365,26 +386,29 @@ void Container::draw(SpriteBatch* spriteBatch, const Rectangle& clip, bool needs
         }
         }
     }
     }
 
 
-    if (_scroll != SCROLL_NONE)
+    if (_scroll != SCROLL_NONE && (_scrollBarOpacity > 0.0f))
     {
     {
         // Draw scroll bars.
         // Draw scroll bars.
         Rectangle clipRegion(_viewportClipBounds);
         Rectangle clipRegion(_viewportClipBounds);
 
 
         spriteBatch->begin();
         spriteBatch->begin();
 
 
-        if (_scrollBarBounds.height > 0 && (_scrolling || _velocity.y))
+        if (_scrollBarBounds.height > 0)
         {
         {
             const Rectangle& topRegion = _scrollBarTopCap->getRegion();
             const Rectangle& topRegion = _scrollBarTopCap->getRegion();
             const Theme::UVs& topUVs = _scrollBarTopCap->getUVs();
             const Theme::UVs& topUVs = _scrollBarTopCap->getUVs();
             Vector4 topColor = _scrollBarTopCap->getColor();
             Vector4 topColor = _scrollBarTopCap->getColor();
+            topColor.w *= _scrollBarOpacity;
 
 
             const Rectangle& verticalRegion = _scrollBarVertical->getRegion();
             const Rectangle& verticalRegion = _scrollBarVertical->getRegion();
             const Theme::UVs& verticalUVs = _scrollBarVertical->getUVs();
             const Theme::UVs& verticalUVs = _scrollBarVertical->getUVs();
             Vector4 verticalColor = _scrollBarVertical->getColor();
             Vector4 verticalColor = _scrollBarVertical->getColor();
+            verticalColor.w *= _scrollBarOpacity;
 
 
             const Rectangle& bottomRegion = _scrollBarBottomCap->getRegion();
             const Rectangle& bottomRegion = _scrollBarBottomCap->getRegion();
             const Theme::UVs& bottomUVs = _scrollBarBottomCap->getUVs();
             const Theme::UVs& bottomUVs = _scrollBarBottomCap->getUVs();
             Vector4 bottomColor = _scrollBarBottomCap->getColor();
             Vector4 bottomColor = _scrollBarBottomCap->getColor();
+            bottomColor.w *= _scrollBarOpacity;
 
 
             clipRegion.width += verticalRegion.width;
             clipRegion.width += verticalRegion.width;
 
 
@@ -402,19 +426,22 @@ void Container::draw(SpriteBatch* spriteBatch, const Rectangle& clip, bool needs
             spriteBatch->draw(bounds.x, bounds.y, bounds.width, bounds.height, bottomUVs.u1, bottomUVs.v1, bottomUVs.u2, bottomUVs.v2, bottomColor, clipRegion);
             spriteBatch->draw(bounds.x, bounds.y, bounds.width, bounds.height, bottomUVs.u1, bottomUVs.v1, bottomUVs.u2, bottomUVs.v2, bottomColor, clipRegion);
         }
         }
 
 
-        if (_scrollBarBounds.width > 0 && (_scrolling || _velocity.x))
+        if (_scrollBarBounds.width > 0)
         {
         {
             const Rectangle& leftRegion = _scrollBarLeftCap->getRegion();
             const Rectangle& leftRegion = _scrollBarLeftCap->getRegion();
             const Theme::UVs& leftUVs = _scrollBarLeftCap->getUVs();
             const Theme::UVs& leftUVs = _scrollBarLeftCap->getUVs();
             Vector4 leftColor = _scrollBarLeftCap->getColor();
             Vector4 leftColor = _scrollBarLeftCap->getColor();
+            leftColor.w *= _scrollBarOpacity;
 
 
             const Rectangle& horizontalRegion = _scrollBarHorizontal->getRegion();
             const Rectangle& horizontalRegion = _scrollBarHorizontal->getRegion();
             const Theme::UVs& horizontalUVs = _scrollBarHorizontal->getUVs();
             const Theme::UVs& horizontalUVs = _scrollBarHorizontal->getUVs();
             Vector4 horizontalColor = _scrollBarHorizontal->getColor();
             Vector4 horizontalColor = _scrollBarHorizontal->getColor();
+            horizontalColor.w *= _scrollBarOpacity;
 
 
             const Rectangle& rightRegion = _scrollBarRightCap->getRegion();
             const Rectangle& rightRegion = _scrollBarRightCap->getRegion();
             const Theme::UVs& rightUVs = _scrollBarRightCap->getUVs();
             const Theme::UVs& rightUVs = _scrollBarRightCap->getUVs();
             Vector4 rightColor = _scrollBarRightCap->getColor();
             Vector4 rightColor = _scrollBarRightCap->getColor();
+            rightColor.w *= _scrollBarOpacity;
 
 
             clipRegion.height += horizontalRegion.height;
             clipRegion.height += horizontalRegion.height;
         
         
@@ -696,6 +723,15 @@ void Container::updateScroll()
                          ((-_scrollPosition.y) / totalHeight) * clipHeight,
                          ((-_scrollPosition.y) / totalHeight) * clipHeight,
                          scrollWidth, scrollHeight);
                          scrollWidth, scrollHeight);
 
 
+    // If scroll velocity is 0 and scrollbars are not always visible, trigger fade-out animation.
+    if (!_scrolling && _velocity.isZero() && !_scrollBarsAlwaysVisible && _scrollBarOpacity == 1.0f)
+    {
+        float to = 0;
+        _scrollBarOpacity = 0.99f;
+        Animation* animation = createAnimationFromTo("scrollbar-fade-out", ANIMATE_OPACITY, &_scrollBarOpacity, &to, Curve::QUADRATIC_IN_OUT, 500L);
+        animation->getClip()->play();
+    }
+
     // Position controls within scroll area.
     // Position controls within scroll area.
     _layout->update(this, _scrollPosition);
     _layout->update(this, _scrollPosition);
 }
 }
@@ -710,6 +746,7 @@ bool Container::touchEventScroll(Touch::TouchEvent evt, int x, int y, unsigned i
         _velocity.set(0, 0);
         _velocity.set(0, 0);
         _scrolling = true;
         _scrolling = true;
         _startTimeX = _startTimeY = 0;
         _startTimeX = _startTimeY = 0;
+        _scrollBarOpacity = 1.0f;
         return true;
         return true;
 
 
     case Touch::TOUCH_MOVE:
     case Touch::TOUCH_MOVE:
@@ -822,4 +859,45 @@ bool sortControlsByZOrder(Control* c1, Control* c2)
     return false;
     return false;
 }
 }
 
 
+unsigned int Container::getAnimationPropertyComponentCount(int propertyId) const
+{
+    switch(propertyId)
+    {
+    case ANIMATE_OPACITY:
+        return 1;
+    default:
+        return Control::getAnimationPropertyComponentCount(propertyId);
+        break;
+    }
+}
+
+void Container::getAnimationPropertyValue(int propertyId, AnimationValue* value)
+{
+    GP_ASSERT(value);
+
+    switch(propertyId)
+    {
+    case ANIMATE_OPACITY:
+        value->setFloat(0, _scrollBarOpacity);
+    default:
+        Control::getAnimationPropertyValue(propertyId, value);
+        break;
+    }
+}
+
+void Container::setAnimationPropertyValue(int propertyId, AnimationValue* value, float blendWeight)
+{
+    GP_ASSERT(value);
+
+    switch(propertyId)
+    {
+    case ANIMATE_OPACITY:
+        _scrollBarOpacity = Curve::lerp(blendWeight, _opacity, value->getFloat(0));
+        _dirty = true;
+    default:
+        Control::setAnimationPropertyValue(propertyId, value, blendWeight);
+        break;
+    }
+}
+
 }
 }

+ 43 - 2
gameplay/src/Container.h

@@ -57,6 +57,8 @@ public:
         SCROLL_BOTH = SCROLL_HORIZONTAL | SCROLL_VERTICAL
         SCROLL_BOTH = SCROLL_HORIZONTAL | SCROLL_VERTICAL
     };
     };
 
 
+    static const int ANIMATE_SCROLLBAR_OPACITY = 8;
+
     /**
     /**
      * Get this container's layout.
      * Get this container's layout.
      *
      *
@@ -127,14 +129,33 @@ public:
     const std::vector<Control*>& getControls() const;
     const std::vector<Control*>& getControls() const;
 
 
     /**
     /**
-     * Sets the scrolling for the container.
+     * Sets the allowed scroll directions for this container.
      *
      *
-     * @param scroll The scroll for the 
+     * @param scroll The allowed scroll directions for this container.
      */
      */
     void setScroll(Scroll scroll);
     void setScroll(Scroll scroll);
 
 
+    /**
+     * Gets the allowed scroll directions for this container.
+     *
+     * @return The allowed scroll directions for this container.
+     */
     Scroll getScroll() const;
     Scroll getScroll() const;
 
 
+    /**
+     * Set whether scrollbars are always visible, or only visible while scrolling.
+     *
+     * @param alwaysVisible Whether scrollbars are always visible.
+     */
+    void setScrollBarsAlwaysVisible(bool alwaysVisible);
+
+    /**
+     * Get whether scrollbars are always visible, or only visible while scrolling.
+     *
+     * @return Whether scrollbars are always visible.
+     */
+    bool getScrollBarsAlwaysVisible() const;
+
     /**
     /**
      * Gets the first animation in the control with the specified ID.
      * Gets the first animation in the control with the specified ID.
      *
      *
@@ -143,6 +164,21 @@ public:
      */
      */
     Animation* getAnimation(const char* id = NULL) const;
     Animation* getAnimation(const char* id = NULL) const;
 
 
+    /**
+     * @see AnimationTarget#getAnimationPropertyComponentCount
+     */
+    unsigned int getAnimationPropertyComponentCount(int propertyId) const;
+
+    /**
+     * @see AnimationTarget#getAnimationProperty
+     */
+    void getAnimationPropertyValue(int propertyId, AnimationValue* value);
+
+    /**
+     * @see AnimationTarget#setAnimationProperty
+     */
+    void setAnimationPropertyValue(int propertyId, AnimationValue* value, float blendWeight = 1.0f);
+
 protected:
 protected:
 
 
     /**
     /**
@@ -271,6 +307,11 @@ protected:
     // How far this layout has been scrolled in each direction.
     // How far this layout has been scrolled in each direction.
     Vector2 _scrollPosition;
     Vector2 _scrollPosition;
 
 
+    bool _scrollBarsAlwaysVisible;
+
+    // Used to animate scrollbars fading out.
+    float _scrollBarOpacity;
+
     // Whether the user is currently touching / holding the mouse down
     // Whether the user is currently touching / holding the mouse down
     // within this layout's container.
     // within this layout's container.
     bool _scrolling;
     bool _scrolling;