cocos2d / cocos2d-x

Cocos2d-x is a suite of open-source, cross-platform, game-development tools utilized by millions of developers across the globe. Its core has evolved to serve as the foundation for Cocos Creator 1.x & 2.x.
https://www.cocos.com/en/cocos2d-x
18.22k stars 7.06k forks source link

`ui::ScrollView::setScrollBarOpacity()` doesn't update `ScrollViewBar`'s effective opacity without autohide being enabled #15657

Open tankorsmash opened 8 years ago

tankorsmash commented 8 years ago

Cocos2dx 3.10 c++

The following line sets the opacity of the scrollbarview

listview->setScrollBarOpacity(255);

but it doesn't actually take into effect unless you toggle the autohide:

listview->setScrollBarAutoHideEnabled(false);

which calls

 //UIScrollViewBar.cpp line 145
ProtectedNode::setOpacity(_opacity);

It's because the opacity of the ProtectedNode inside the ScrollViewBar is unchanged, because it's not the same as the opacity as ScrollViewBar.

I don't know the solution because I don't yet know why the setOpacity call for ScrollView is not doing it already.

larsven commented 8 years ago

The behavior is changed in 3.11 but not to the better.

Setting setScrollBarAutoHideEnabled() to false makes the scrollbar invisible as opacity is always 0. setScrollBarOpacity() does not have any effect in this case.

minggo commented 8 years ago

@zilongshanren could you please take a look?

larsven commented 8 years ago

Here is a minimal change to make setScrollBarOpacity() work. It solves the problem temporarily but requires that setScrollBarOpacity() is called after setScrollBarAutoHideEnabled(false).

diff --git a/cocos2d/cocos/ui/UIScrollViewBar.cpp b/cocos2d/cocos/ui/UIScrollViewBar.cpp
index 96c2c74..a5060f3 100644
--- a/cocos2d/cocos/ui/UIScrollViewBar.cpp
+++ b/cocos2d/cocos/ui/UIScrollViewBar.cpp
@@ -164,6 +164,15 @@ void ScrollViewBar::updateLength(float length)
     _upperHalfCircle->setPositionY(_body->getPositionY() + length);
 }

+void ScrollViewBar::setOpacity(GLubyte opacity)
+{
+   _opacity = opacity;
+   if (!_autoHideEnabled)
+   {
+       ProtectedNode::setOpacity(_opacity);
+   }
+}
+
 void ScrollViewBar::onEnter()
 {
 #if CC_ENABLE_SCRIPT_BINDING
diff --git a/cocos2d/cocos/ui/UIScrollViewBar.h b/cocos2d/cocos/ui/UIScrollViewBar.h
index 4a40e4c..2017545 100644
--- a/cocos2d/cocos/ui/UIScrollViewBar.h
+++ b/cocos2d/cocos/ui/UIScrollViewBar.h
@@ -130,7 +130,7 @@ public:
     /**
      * @lua NA
      */
-    virtual void setOpacity(GLubyte opacity) override { _opacity = opacity; }
+   virtual void setOpacity(GLubyte opacity) override;
     virtual GLubyte getOpacity() const override { return _opacity; }
     virtual void onEnter() override;
     virtual void update(float deltaTime) override;