MCPcopy Create free account
hub / github.com/TankOs/SFGUI / HandleMouseButtonEvent

Method HandleMouseButtonEvent

src/SFGUI/Scale.cpp:67–124  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

65}
66
67void Scale::HandleMouseButtonEvent( sf::Mouse::Button button, bool press, int x, int y ) {
68 if( button != sf::Mouse::Button::Left ) {
69 return;
70 }
71
72 if( m_drag_offset ) {
73 m_drag_offset.reset();
74 m_dragging = false;
75 }
76
77 if( !GetAllocation().contains( sf::Vector2f( sf::Vector2( x, y ) ) ) ) {
78 return;
79 }
80
81 if( press ) {
82 if( !GetSliderRect().contains( sf::Vector2f( sf::Vector2( x, y ) ) - GetAllocation().position ) ) {
83 Adjustment::Ptr adjustment( GetAdjustment() );
84
85 auto minor_step = adjustment->GetMinorStep();
86 auto range = adjustment->GetUpper() - adjustment->GetLower();
87 auto steps = range / minor_step;
88 auto needed_steps = 0.f;
89
90 auto trough_position = 0.f;
91 auto trough_length = 0.f;
92
93 if( GetOrientation() == Orientation::HORIZONTAL ) {
94 trough_position = static_cast<float>( x ) - ( GetAllocation().position.x + GetSliderRect().size.x / 2.f );
95 trough_length = GetAllocation().size.x - GetSliderRect().size.x;
96 }
97
98 if( GetOrientation() == Orientation::VERTICAL ) {
99 trough_position = static_cast<float>( y ) - ( GetAllocation().position.y + GetSliderRect().size.y / 2.f );
100 trough_length = GetAllocation().size.y - GetSliderRect().size.y;
101 }
102
103 trough_position = std::min( trough_position, trough_length );
104
105 auto trough_ratio = trough_position / trough_length;
106
107 for( ; needed_steps < steps; needed_steps += 1.f ) {
108 if( ( 1.f / steps ) * needed_steps > trough_ratio ) {
109 break;
110 }
111 }
112
113 needed_steps = std::max( needed_steps - 1.f, 0.f );
114
115 adjustment->SetValue( needed_steps * minor_step );
116 }
117
118 m_dragging = true;
119 m_drag_offset.reset( new sf::Vector2f(
120 static_cast<float>( x ) - ( GetAllocation().position.x + GetSliderRect().position.x + GetSliderRect().size.x / 2.f ),
121 static_cast<float>( y ) - ( GetAllocation().position.y + GetSliderRect().position.y + GetSliderRect().size.y / 2.f )
122 ) );
123 }
124}

Callers

nothing calls this directly

Calls 4

GetMinorStepMethod · 0.80
GetUpperMethod · 0.80
GetLowerMethod · 0.80
SetValueMethod · 0.45

Tested by

no test coverage detected