Permalink
Browse files

Fixed bug #396. Implemented a more robust visibility system for widge…

…ts that would not cause headaches and actually saves some lines of code.
  • Loading branch information...
1 parent 5f86191 commit cd359c16923d6e74477720cc758acd226f5a2cb9 @binary1248 binary1248 committed Apr 20, 2012
View
@@ -235,7 +235,7 @@ void SampleApp::Run() {
}
m_scrolled_window = sfg::ScrolledWindow::Create();
- m_scrolled_window->SetRequisition( sf::Vector2f( .0f, 150.f ) );
+ m_scrolled_window->SetRequisition( sf::Vector2f( .0f, 160.f ) );
m_scrolled_window->SetScrollbarPolicy( sfg::ScrolledWindow::HORIZONTAL_AUTOMATIC | sfg::ScrolledWindow::VERTICAL_AUTOMATIC );
m_scrolled_window->SetPlacement( sfg::ScrolledWindow::TOP_LEFT );
m_scrolled_window->AddWithViewport( m_scrolled_window_box );
@@ -475,7 +475,7 @@ void SampleApp::OnToggleTitlebarClick() {
}
void SampleApp::OnHideWindowClicked() {
- m_wndmain->Show( !m_wndmain->IsVisible() );
+ m_wndmain->Show( !m_wndmain->IsLocallyVisible() );
}
void SampleApp::OnToggleSpaceClick() {
@@ -58,8 +58,6 @@ class SFGUI_API Container : public Widget {
*/
Container();
- virtual void Draw( bool draw = true );
-
/** Handle adding children.
* @param child Child widget.
*/
@@ -72,11 +70,7 @@ class SFGUI_API Container : public Widget {
/** Handle visibility change.
*/
- virtual void HandleVisibilityChange();
-
- /** Handle parent visibility change.
- */
- virtual void HandleParentVisibilityChange();
+ virtual void HandleGlobalVisibilityChange();
/** Handle update.
*/
@@ -193,7 +193,7 @@ class SFGUI_API Notebook : public Container {
void HandleRemove( const Widget::Ptr& child );
void HandleSizeChange();
void HandleUpdate( float seconds );
- void HandleVisibilityChange();
+ void HandleGlobalVisibilityChange();
void RecalculateSize();
@@ -139,7 +139,6 @@ class SFGUI_API ScrolledWindow : public Container {
void HandleSizeChange();
void HandleAdd( const Widget::Ptr& child );
- void HandleVisibilityChange();
mutable sf::FloatRect m_content_allocation;
View
@@ -41,10 +41,17 @@ class SFGUI_API Widget : public Object, public EnableSharedFromThis<Widget> {
*/
bool IsSensitive() const;
- /** Check if widget is visible, i.e. gets rendered.
- * @return true when visible.
+ /** Check if widget is locally visible, i.e. is rendered when allowed to.
+ * This DOES NOT take into account the global visibility of it's parents.
+ * @return true when locally visible.
*/
- bool IsVisible() const;
+ bool IsLocallyVisible() const;
+
+ /** Check if widget is globaly visible, i.e. is currently rendered.
+ * This takes into account the global visibility of it's parents.
+ * @return true when globally visible.
+ */
+ bool IsGloballyVisible() const;
/** Show (or hide) widget.
* @param show true to show, false to hide.
@@ -161,9 +168,9 @@ class SFGUI_API Widget : public Object, public EnableSharedFromThis<Widget> {
*/
virtual void HandleAbsolutePositionChange();
- /** Handle parent visibility change.
+ /** Handle global visibility change.
*/
- virtual void HandleParentVisibilityChange();
+ virtual void HandleGlobalVisibilityChange();
/** Update position of drawable.
*/
@@ -194,16 +201,6 @@ class SFGUI_API Widget : public Object, public EnableSharedFromThis<Widget> {
*/
virtual void Refresh();
- /** Check if widget is drawn.
- * @return true when widget is drawn.
- */
- bool IsDrawn() const;
-
- /** Draw widget.
- * @param draw true to draw widget.
- */
- virtual void Draw( bool draw = true );
-
/** Set hierarchy level of this widget.
* @param level New hierarchy level of this widget..
*/
@@ -337,9 +334,9 @@ class SFGUI_API Widget : public Object, public EnableSharedFromThis<Widget> {
*/
virtual void HandleFocusChange( const Widget::Ptr& focused_widget );
- /** Handle visibility change.
+ /** Handle local visibility change.
*/
- virtual void HandleVisibilityChange();
+ virtual void HandleLocalVisibilityChange();
/** Handle update.
*/
View
@@ -190,7 +190,7 @@ void Box::AllocateChildren() const {
bool Box::IsChildInteresting( const sfg::Widget::PtrConst& child ) const {
return
- child->IsVisible() &&
+ child->IsLocallyVisible() &&
(child->GetRequisition().x > 0.f || child->GetAllocation().width > 0.0f) &&
(child->GetRequisition().y > 0.f || child->GetAllocation().height > 0.0f)
;
View
@@ -64,7 +64,7 @@ void Container::Refresh() {
void Container::HandleEvent( const sf::Event& event ) {
// Ignore event when widget is not visible.
- if( !IsVisible() ) {
+ if( !IsGloballyVisible() ) {
return;
}
@@ -123,24 +123,13 @@ void Container::HandleAbsolutePositionChange() {
Widget::HandleAbsolutePositionChange();
}
-void Container::HandleVisibilityChange() {
- Widget::HandleVisibilityChange();
+void Container::HandleGlobalVisibilityChange() {
+ Widget::HandleGlobalVisibilityChange();
std::size_t children_size = m_children.size();
for( std::size_t index = 0; index < children_size; ++index ) {
- m_children[index]->Draw( IsDrawn() );
- m_children[index]->HandleParentVisibilityChange();
- }
-}
-
-void Container::HandleParentVisibilityChange() {
- Widget::HandleParentVisibilityChange();
-
- std::size_t children_size = m_children.size();
-
- for( std::size_t index = 0; index < children_size; ++index ) {
- m_children[index]->HandleParentVisibilityChange();
+ m_children[index]->HandleGlobalVisibilityChange();
}
}
@@ -164,16 +153,6 @@ void Container::HandleSetHierarchyLevel() {
}
}
-void Container::Draw( bool draw ) {
- Widget::Draw( draw );
-
- std::size_t children_size = m_children.size();
-
- for( std::size_t index = 0; index < children_size; ++index ) {
- m_children[index]->Draw( IsDrawn() && IsVisible() );
- }
-}
-
void Container::HandleViewportUpdate() {
std::size_t children_size = m_children.size();
View
@@ -52,7 +52,7 @@ void Desktop::HandleEvent( const sf::Event& event ) {
Widget::Ptr widget( m_children[index] );
// Skip widget if not visible.
- if( !widget->IsVisible() ) {
+ if( !widget->IsLocallyVisible() ) {
continue;
}
View
@@ -501,7 +501,7 @@ void Notebook::HandleUpdate( float seconds ) {
}
void Notebook::RecalculateSize() {
- if( !IsVisible() ) {
+ if( !IsGloballyVisible() ) {
return;
}
@@ -653,7 +653,9 @@ void Notebook::RecalculateSize() {
Invalidate();
}
-void Notebook::HandleVisibilityChange() {
+void Notebook::HandleGlobalVisibilityChange() {
+ Container::HandleGlobalVisibilityChange();
+
RecalculateSize();
}
@@ -178,8 +178,8 @@ void ScrolledWindow::RecalculateAdjustments() const {
RecalculateContentAllocation();
- m_horizontal_scrollbar->Show( IsDrawn() && IsHorizontalScrollbarVisible() );
- m_vertical_scrollbar->Show( IsDrawn() && IsVerticalScrollbarVisible() );
+ m_horizontal_scrollbar->Show( IsHorizontalScrollbarVisible() );
+ m_vertical_scrollbar->Show( IsVerticalScrollbarVisible() );
}
void ScrolledWindow::RecalculateContentAllocation() const {
@@ -345,11 +345,4 @@ const std::string& ScrolledWindow::GetName() const {
return name;
}
-void ScrolledWindow::HandleVisibilityChange() {
- Widget::HandleVisibilityChange();
-
- m_horizontal_scrollbar->Show( IsDrawn() && IsHorizontalScrollbarVisible() );
- m_vertical_scrollbar->Show( IsDrawn() && IsVerticalScrollbarVisible() );
-}
-
}
View
@@ -64,7 +64,7 @@ void Viewport::HandleAbsolutePositionChange() {
void Viewport::HandleEvent( const sf::Event& event ) {
// Ignore event when widget is not visible.
- if( !IsVisible() ) {
+ if( !IsGloballyVisible() ) {
return;
}
View
@@ -34,10 +34,29 @@ bool Widget::IsSensitive() const {
return m_sensitive;
}
-bool Widget::IsVisible() const {
+bool Widget::IsLocallyVisible() const {
return m_visible;
}
+bool Widget::IsGloballyVisible() const {
+ // If not locally visible, also cannot be globally visible.
+ if( !IsLocallyVisible() ) {
+ return false;
+ }
+
+ // At this point we know the widget is locally visible.
+
+ PtrConst parent( m_parent.lock() );
+
+ // If locally visible and no parent, globally visible.
+ if( !parent ) {
+ return true;
+ }
+
+ // Return parent's global visibility.
+ return parent->IsGloballyVisible();
+}
+
void Widget::GrabFocus( Ptr widget ) {
Container::Ptr parent( m_parent.lock() );
@@ -157,7 +176,7 @@ void Widget::Update( float seconds ) {
if( m_drawable ) {
m_drawable->SetPosition( GetAbsolutePosition() );
m_drawable->SetLevel( m_hierarchy_level );
- m_drawable->Show( IsDrawn() && IsVisible() );
+ m_drawable->Show( IsGloballyVisible() );
m_drawable->SetViewport( m_viewport );
}
}
@@ -199,8 +218,6 @@ void Widget::SetParent( const Widget::Ptr& parent ) {
m_parent = cont;
SetHierarchyLevel( parent->GetHierarchyLevel() + 1 );
-
- Draw( parent->IsDrawn() );
}
void Widget::SetPosition( const sf::Vector2f& position ) {
@@ -217,7 +234,7 @@ void Widget::SetPosition( const sf::Vector2f& position ) {
}
void Widget::HandleEvent( const sf::Event& event ) {
- if( !IsVisible() ) {
+ if( !IsGloballyVisible() ) {
return;
}
@@ -363,11 +380,15 @@ void Widget::Show( bool show ) {
return;
}
+ bool old_global_visibility = IsGloballyVisible();
+
m_visible = show;
- Draw( m_visible );
+ HandleLocalVisibilityChange();
- HandleVisibilityChange();
+ if( old_global_visibility != IsGloballyVisible() ) {
+ HandleGlobalVisibilityChange();
+ }
RequestResize();
}
@@ -465,16 +486,17 @@ void Widget::HandleFocusChange( const Widget::Ptr& focused_widget ) {
}
}
-void Widget::HandleVisibilityChange() {
- if( m_state == PRELIGHT ) {
- SetState( NORMAL );
- }
+void Widget::HandleLocalVisibilityChange() {
}
-void Widget::HandleParentVisibilityChange() {
+void Widget::HandleGlobalVisibilityChange() {
if( m_state == PRELIGHT ) {
SetState( NORMAL );
}
+
+ if( m_drawable ) {
+ m_drawable->Show( IsGloballyVisible() );
+ }
}
void Widget::HandleAbsolutePositionChange() {
@@ -499,18 +521,6 @@ void Widget::HandleSetHierarchyLevel() {
}
}
-bool Widget::IsDrawn() const {
- return m_drawn;
-}
-
-void Widget::Draw( bool draw ) {
- m_drawn = draw;
-
- if( m_drawable ) {
- m_drawable->Show( draw && IsVisible() );
- }
-}
-
void Widget::SetHierarchyLevel( int level ) {
m_hierarchy_level = level;

0 comments on commit cd359c1

Please sign in to comment.