void OvrScrollBarComponent::UpdateScrollBar( OvrVRMenuMgr & menuMgr, VRMenuObject * self, const int numElements ) { NumOfItems = numElements; if ( IsVertical ) { ScrollBarCurrentbaseLength = ScrollBarBaseHeight; } else { ScrollBarCurrentbaseLength = ScrollBarBaseWidth; } ScrollBarCurrentThumbLength = ScrollBarCurrentbaseLength / NumOfItems; ScrollBarCurrentThumbLength = ( ScrollBarCurrentThumbLength < BASE_THUMB_WIDTH ) ? BASE_THUMB_WIDTH : ScrollBarCurrentThumbLength; if ( IsVertical ) { ScrollBarThumbHeight = ScrollBarCurrentThumbLength; } else { ScrollBarThumbWidth = ScrollBarCurrentThumbLength; } VRMenuObject * thumb = menuMgr.ToObject( self->ChildHandleForId( menuMgr, ScrollBarThumbId ) ); if ( thumb != NULL && NumOfItems > 0 ) { thumb->SetSurfaceDims( 0, Vector2f( ScrollBarThumbWidth, ScrollBarThumbHeight ) ); thumb->RegenerateSurfaceGeometry( 0, false ); } VRMenuObject * base = menuMgr.ToObject( self->ChildHandleForId( menuMgr, ScrollBarBaseId ) ); if ( thumb != NULL ) { base->SetSurfaceDims( 0, Vector2f( ScrollBarBaseWidth, ScrollBarBaseHeight ) ); base->RegenerateSurfaceGeometry( 0, false ); } }
void OvrScrollBarComponent::SetScrollFrac( OvrVRMenuMgr & menuMgr, VRMenuObject * self, const float pos ) { if ( NumOfItems <= 0 ) return; // Updating thumb length when pulled beyond its bounds const float scrollEnd = (float)( NumOfItems - 1 ); const float outOfBoundsOffset = 0.5f; const float minThumbLength = ScrollBarCurrentThumbLength * THUMB_SHRINK_FACTOR; const float maxThumbLength = ScrollBarCurrentThumbLength; const float minThumbOffset = 0.0f; const float maxThumbOffset = ( maxThumbLength - minThumbLength ) * 0.5f; float currentThumbLength = ScrollBarCurrentThumbLength; float thumbPosOffset = 0.0f; if ( pos < 0.0f ) { currentThumbLength = LinearRangeMapFloat( pos, -outOfBoundsOffset, 0.0f, minThumbLength, maxThumbLength ); thumbPosOffset = LinearRangeMapFloat( pos, -outOfBoundsOffset, 0.0f, -maxThumbOffset, minThumbOffset ); } else if ( pos > scrollEnd ) { currentThumbLength = LinearRangeMapFloat( pos, scrollEnd + outOfBoundsOffset, scrollEnd, minThumbLength, maxThumbLength ); thumbPosOffset = LinearRangeMapFloat( pos, scrollEnd + outOfBoundsOffset, scrollEnd, maxThumbOffset, minThumbOffset ); } float thumbWidth = ScrollBarThumbWidth; float thumbHeight = ScrollBarThumbHeight; if ( IsVertical ) { thumbHeight = currentThumbLength; } else { thumbWidth = currentThumbLength; } VRMenuObject * thumb = menuMgr.ToObject( self->ChildHandleForId( menuMgr, ScrollBarThumbId ) ); if ( thumb != NULL && NumOfItems > 0 ) { thumb->SetSurfaceDims( 0, Vector2f( thumbWidth, thumbHeight ) ); thumb->RegenerateSurfaceGeometry( 0, false ); } // Updating thumb position float clampedPos = Alg::Clamp( pos, 0.0f, (float)(NumOfItems - 1) ); float thumbPos = LinearRangeMapFloat( clampedPos, 0.0f, (float)(NumOfItems - 1), 0.0f, ( ScrollBarCurrentbaseLength - currentThumbLength ) ); thumbPos -= ( ScrollBarCurrentbaseLength - currentThumbLength ) * 0.5f; thumbPos += thumbPosOffset; thumbPos *= VRMenuObject::DEFAULT_TEXEL_SCALE; if ( thumb != NULL ) { Vector3f direction = RIGHT; if ( IsVertical ) { direction = DOWN; } thumb->SetLocalPosition( ( direction * thumbPos ) - ( FWD * THUMB_FROM_BASE_OFFSET ) ); } }