void Scale::HandleMouseButtonEvent( sf::Mouse::Button button, bool press, int x, int y ) { if( button != sf::Mouse::Left ) { return; } if( m_drag_offset ) { m_drag_offset.reset(); m_dragging = false; } if( !GetAllocation().contains( static_cast<float>( x ), static_cast<float>( y ) ) ) { return; } if( press ) { if( !GetSliderRect().contains( static_cast<float>( x ) - GetAllocation().left, static_cast<float>( y ) - GetAllocation().top ) ) { Adjustment::Ptr adjustment( GetAdjustment() ); auto minor_step = adjustment->GetMinorStep(); auto range = adjustment->GetUpper() - adjustment->GetLower(); auto steps = range / minor_step; auto needed_steps = 0.f; auto trough_position = 0.f; auto trough_length = 0.f; if( GetOrientation() == Orientation::HORIZONTAL ) { trough_position = static_cast<float>( x ) - ( GetAllocation().left + GetSliderRect().width / 2.f ); trough_length = GetAllocation().width - GetSliderRect().width; } if( GetOrientation() == Orientation::VERTICAL ) { trough_position = static_cast<float>( y ) - ( GetAllocation().top + GetSliderRect().height / 2.f ); trough_length = GetAllocation().height - GetSliderRect().height; } trough_position = std::min( trough_position, trough_length ); auto trough_ratio = trough_position / trough_length; for( ; needed_steps < steps; needed_steps += 1.f ) { if( ( 1.f / steps ) * needed_steps > trough_ratio ) { break; } } needed_steps = std::max( needed_steps - 1.f, 0.f ); adjustment->SetValue( needed_steps * minor_step ); } m_dragging = true; m_drag_offset.reset( new sf::Vector2f( static_cast<float>( x ) - ( GetAllocation().left + GetSliderRect().left + GetSliderRect().width / 2.f ), static_cast<float>( y ) - ( GetAllocation().top + GetSliderRect().top + GetSliderRect().height / 2.f ) ) ); } }
void mxExpressionSlider::GetThumbRect( int barnum, RECT &rcThumb ) { RECT rc; GetSliderRect( rc ); RECT rcBar = rc; GetBarRect( rcBar ); float frac = 0.0f; if ( m_flMax[ barnum ] - m_flMin[ barnum ] > 0 ) { frac = (m_flCurrent[ barnum ] - m_flMin[ barnum ]) / ( m_flMax[ barnum ] - m_flMin[ barnum ] ); } int tickmark = (int)( frac * m_nTicks[ barnum ] + 0.5 ); tickmark = min( m_nTicks[ barnum ], tickmark ); tickmark = max( 0, tickmark ); int thumbwidth = 20; int thumbheight = 14; int xoffset = -thumbwidth / 2; int yoffset = -thumbheight / 2 + 2; int leftedge = rcBar.left + (int)( (float)( rcBar.right - rcBar.left ) * (float)(tickmark) / (float)m_nTicks[ barnum ] ); rcThumb.left = leftedge + xoffset; rcThumb.right = rcThumb.left + thumbwidth; rcThumb.top = rcBar.top + yoffset; rcThumb.bottom = rcThumb.top + thumbheight; }
void Scrollbar::HandleMouseMoveEvent( int x, int y ) { if( !m_dragging || ( x == std::numeric_limits<int>::min() ) || ( y == std::numeric_limits<int>::min() ) ) { return; } Adjustment::Ptr adjustment( GetAdjustment() ); auto slider_rect = GetSliderRect(); auto value_range = std::max( adjustment->GetUpper() - adjustment->GetLower() - adjustment->GetPageSize(), adjustment->GetMinorStep() / 2.f ); auto steps = value_range / adjustment->GetMinorStep(); if( GetOrientation() == Orientation::HORIZONTAL ) { auto stepper_length = GetAllocation().height; auto slider_center_x = slider_rect.left + slider_rect.width / 2.0f; auto step_distance = ( GetAllocation().width - 2.f * stepper_length ) / steps; auto delta = ( static_cast<float>( x ) - (slider_center_x + m_slider_click_offset) ); while( delta < (-step_distance / 2) ) { adjustment->Decrement(); delta += step_distance; } while( delta > (step_distance / 2) ) { adjustment->Increment(); delta -= step_distance; } } else { auto stepper_length = GetAllocation().width; auto slider_center_y = slider_rect.top + slider_rect.height / 2.0f; auto step_distance = (GetAllocation().height - 2.f * stepper_length) / steps; auto delta = static_cast<float>( y ) - (slider_center_y + m_slider_click_offset); while( delta < (-step_distance / 2) ) { adjustment->Decrement(); delta += step_distance; } while( delta > (step_distance / 2) ) { adjustment->Increment(); delta -= step_distance; } } }
//----------------------------------------------------------------------------- // Purpose: // Input : &rc - //----------------------------------------------------------------------------- void mxExpressionSlider::GetBarRect( RECT &rcBar ) { RECT rc; GetSliderRect( rc ); rcBar = rc; InflateRect( &rcBar, -10, 0 ); rcBar.top += 5; rcBar.bottom = rcBar.top + 2; int midy = ( rcBar.top + rcBar.bottom ) / 2; rcBar.top = midy - 1; rcBar.bottom = midy + 1; }
void ScrollZoomWidget::UpdateSliderPosition() { QRect sliderRect = GetSliderRect(); zoomSlider->move(sliderRect.x(), sliderRect.y()); zoomSlider->resize(sliderRect.width(), sliderRect.height()); }
void Scrollbar::HandleMouseButtonEvent( sf::Mouse::Button button, bool press, int x, int y ) { if( button != sf::Mouse::Left ) { return; } if( press ) { auto slider_rect = GetSliderRect(); slider_rect.left += GetAllocation().left; slider_rect.top += GetAllocation().top; if( slider_rect.contains( static_cast<float>( x ), static_cast<float>( y ) ) ) { m_dragging = true; if( GetOrientation() == Orientation::HORIZONTAL ) { auto slider_mid = slider_rect.left + slider_rect.width / 2.f; m_slider_click_offset = static_cast<float>( x ) + GetAllocation().left - slider_mid; } else { auto slider_mid = slider_rect.top + slider_rect.height / 2.f; m_slider_click_offset = static_cast<float>( y ) + GetAllocation().top - slider_mid; } return; } if( GetOrientation() == Orientation::HORIZONTAL ) { auto stepper_length = GetAllocation().height; sf::FloatRect decrease_stepper_rect( GetAllocation().left, GetAllocation().top, stepper_length, GetAllocation().height ); sf::FloatRect increase_stepper_rect( GetAllocation().left + GetAllocation().width - stepper_length, GetAllocation().top, stepper_length, GetAllocation().height ); if( decrease_stepper_rect.contains( static_cast<float>( x ), static_cast<float>( y ) ) ) { m_decrease_pressed = true; GetAdjustment()->Decrement(); m_elapsed_time = 0.f; m_repeat_wait = true; Invalidate(); return; } if( increase_stepper_rect.contains( static_cast<float>( x ), static_cast<float>( y ) ) ) { m_increase_pressed = true; GetAdjustment()->Increment(); m_elapsed_time = 0.f; m_repeat_wait = true; Invalidate(); return; } } else { auto stepper_length = GetAllocation().width; sf::FloatRect decrease_stepper_rect( GetAllocation().left, GetAllocation().top, GetAllocation().width, stepper_length ); sf::FloatRect increase_stepper_rect( GetAllocation().left, GetAllocation().top + GetAllocation().height - stepper_length, GetAllocation().width, stepper_length ); if( decrease_stepper_rect.contains( static_cast<float>( x ), static_cast<float>( y ) ) ) { m_decrease_pressed = true; GetAdjustment()->Decrement(); m_elapsed_time = 0.f; m_repeat_wait = true; Invalidate(); return; } if( increase_stepper_rect.contains( static_cast<float>( x ), static_cast<float>( y ) ) ) { m_increase_pressed = true; GetAdjustment()->Increment(); m_elapsed_time = 0.f; m_repeat_wait = true; Invalidate(); return; } } auto slider_center_x = slider_rect.left + slider_rect.width / 2.f; auto slider_center_y = slider_rect.top + slider_rect.height / 2.f; if( GetOrientation() == Orientation::HORIZONTAL ) { if( GetAllocation().contains( static_cast<float>( x ), static_cast<float>( y ) ) ) { if( static_cast<float>( x ) < slider_center_x ) { m_page_decreasing = x; GetAdjustment()->DecrementPage(); m_elapsed_time = 0.f; m_repeat_wait = true; Invalidate(); return; } else { m_page_increasing = x; GetAdjustment()->IncrementPage(); m_elapsed_time = 0.f; m_repeat_wait = true; Invalidate(); return; } } } else { if( GetAllocation().contains( static_cast<float>( x ), static_cast<float>( y ) ) ) { if( static_cast<float>( y ) < slider_center_y ) { m_page_decreasing = y; GetAdjustment()->DecrementPage(); m_elapsed_time = 0.f; m_repeat_wait = true; Invalidate(); return; } else { m_page_increasing = y; GetAdjustment()->IncrementPage(); m_elapsed_time = 0.f; m_repeat_wait = true; Invalidate(); return; } } } } else { m_dragging = false; m_decrease_pressed = false; m_increase_pressed = false; m_page_decreasing = 0; m_page_increasing = 0; m_slider_click_offset = 0.f; Invalidate(); return; } }
void Scrollbar::HandleUpdate( float seconds ) { auto stepper_speed = Context::Get().GetEngine().GetProperty<float>( "StepperSpeed", shared_from_this() ); m_elapsed_time += seconds; if( m_elapsed_time < (1.f / stepper_speed) ) { return; } if( m_repeat_wait ) { auto stepper_repeat_delay = Context::Get().GetEngine().GetProperty<sf::Uint32>( "StepperRepeatDelay", shared_from_this() ); if( m_elapsed_time < (static_cast<float>( stepper_repeat_delay ) / 1000.f) ) { return; } m_repeat_wait = false; } m_elapsed_time = 0.f; // Increment / Decrement value while one of the steppers is pressed if( m_decrease_pressed ) { GetAdjustment()->Decrement(); Invalidate(); return; } else if( m_increase_pressed ) { GetAdjustment()->Increment(); Invalidate(); return; } auto slider_rect = GetSliderRect(); slider_rect.left += GetAllocation().left; slider_rect.top += GetAllocation().top; // Increment / Decrement page while mouse is pressed on the trough if( m_page_decreasing ) { GetAdjustment()->DecrementPage(); if( GetOrientation() == Orientation::HORIZONTAL ) { if( slider_rect.left + slider_rect.width < static_cast<float>( m_page_decreasing ) ) { m_page_decreasing = 0; } } else { if( slider_rect.top + slider_rect.height < static_cast<float>( m_page_decreasing ) ) { m_page_decreasing = 0; } } Invalidate(); return; } else if( m_page_increasing ) { GetAdjustment()->IncrementPage(); if( GetOrientation() == Orientation::HORIZONTAL ) { if( slider_rect.left + slider_rect.width > static_cast<float>( m_page_increasing ) ) { m_page_increasing = 0; } } else { if( slider_rect.top + slider_rect.height > static_cast<float>( m_page_increasing ) ) { m_page_increasing = 0; } } Invalidate(); return; } }