| 71 | } |
| 72 | |
| 73 | void Viewport::HandleEvent( const sf::Event& event ) { |
| 74 | // Ignore event when widget is not visible. |
| 75 | if( !IsGloballyVisible() ) { |
| 76 | return; |
| 77 | } |
| 78 | |
| 79 | // Pass event to child |
| 80 | if( GetChild() ) { |
| 81 | auto offset_x = ( -GetAllocation().position.x + m_horizontal_adjustment->GetValue() ); |
| 82 | auto offset_y = ( -GetAllocation().position.y + m_vertical_adjustment->GetValue() ); |
| 83 | |
| 84 | const auto onMouseButtonPressedOrRelease = [&]( const auto& mouseBottonEvent ) { |
| 85 | if( !GetAllocation().contains( sf::Vector2f( mouseBottonEvent.position ) ) ) { |
| 86 | return; |
| 87 | } |
| 88 | |
| 89 | auto altered_event = mouseBottonEvent; |
| 90 | altered_event.position.x += static_cast<int>( offset_x ); |
| 91 | altered_event.position.y += static_cast<int>( offset_y ); |
| 92 | |
| 93 | GetChild()->HandleEvent( altered_event ); |
| 94 | }; |
| 95 | |
| 96 | if( const auto* mouseButtonPressed = event.getIf<sf::Event::MouseButtonPressed>() ) { |
| 97 | onMouseButtonPressedOrRelease( *mouseButtonPressed ); |
| 98 | } |
| 99 | else if( const auto* mouseButtonReleased = event.getIf<sf::Event::MouseButtonReleased>() ) { |
| 100 | onMouseButtonPressedOrRelease( *mouseButtonReleased ); |
| 101 | } |
| 102 | else if( const auto* mouseLeft = event.getIf<sf::Event::MouseLeft>() ) { |
| 103 | GetChild()->HandleEvent( *mouseLeft ); |
| 104 | } |
| 105 | else if( const auto* mouseMoved = event.getIf<sf::Event::MouseMoved>() ) { // All MouseMove events |
| 106 | auto altered_event = *mouseMoved; |
| 107 | if( !GetAllocation().contains( sf::Vector2f( mouseMoved->position ) ) ) { |
| 108 | // Nice hack to cause scrolledwindow children to get out of |
| 109 | // prelight state when the mouse leaves the child allocation. |
| 110 | altered_event.position.x = -1; |
| 111 | altered_event.position.y = -1; |
| 112 | } |
| 113 | else { |
| 114 | altered_event.position.x += static_cast<int>( offset_x ); |
| 115 | altered_event.position.y += static_cast<int>( offset_y ); |
| 116 | } |
| 117 | GetChild()->HandleEvent( altered_event ); |
| 118 | } |
| 119 | else if( const auto* mouseWheelScrolled = event.getIf<sf::Event::MouseWheelScrolled>() ) { // All MouseWheel events |
| 120 | if( !GetAllocation().contains( sf::Vector2f( mouseWheelScrolled->position ) ) ) { |
| 121 | return; |
| 122 | } |
| 123 | |
| 124 | auto altered_event = *mouseWheelScrolled; |
| 125 | altered_event.position.x += static_cast<int>( offset_x ); |
| 126 | altered_event.position.y += static_cast<int>( offset_y ); |
| 127 | |
| 128 | GetChild()->HandleEvent( altered_event ); |
| 129 | } |
| 130 | else { // Pass event unaltered if it is a non-mouse event |