Browse Source

wheelScrolled event now accepts distance instead of bool

Ivan Savenko 2 years ago
parent
commit
dc8e9cd048

+ 24 - 11
client/eventsSDL/InputHandler.cpp

@@ -162,18 +162,31 @@ void InputHandler::preprocessEvent(const SDL_Event & ev)
 	{
 		boost::unique_lock<boost::mutex> lock(eventsMutex);
 
-		if(ev.type == SDL_MOUSEMOTION && !eventsQueue.empty() && eventsQueue.back().type == SDL_MOUSEMOTION)
+		// In a sequence of motion events, skip all but the last one.
+		// This prevents freezes when every motion event takes longer to handle than interval at which
+		// the events arrive (like dragging on the minimap in world view, with redraw at every event)
+		// so that the events would start piling up faster than they can be processed.
+		if (!eventsQueue.empty())
 		{
-			// In a sequence of mouse motion events, skip all but the last one.
-			// This prevents freezes when every motion event takes longer to handle than interval at which
-			// the events arrive (like dragging on the minimap in world view, with redraw at every event)
-			// so that the events would start piling up faster than they can be processed.
-			int xAccumulated = eventsQueue.back().motion.xrel + ev.motion.xrel;
-			int yAccumulated = eventsQueue.back().motion.yrel + ev.motion.yrel;
-			eventsQueue.back() = ev;
-			eventsQueue.back().motion.xrel = xAccumulated;
-			eventsQueue.back().motion.yrel = yAccumulated;
-			return;
+			const SDL_Event & prev = eventsQueue.back();
+
+			if(ev.type == SDL_MOUSEMOTION && prev.type == SDL_MOUSEMOTION)
+			{
+				SDL_Event accumulated = ev;
+				accumulated.motion.xrel += prev.motion.xrel;
+				accumulated.motion.yrel += prev.motion.yrel;
+				eventsQueue.back() = accumulated;
+				return;
+			}
+
+			if(ev.type == SDL_FINGERMOTION && prev.type == SDL_FINGERMOTION && ev.tfinger.fingerId == prev.tfinger.fingerId)
+			{
+				SDL_Event accumulated = ev;
+				accumulated.tfinger.dx += prev.tfinger.dx;
+				accumulated.tfinger.dy += prev.tfinger.dy;
+				eventsQueue.back() = accumulated;
+				return;
+			}
 		}
 		eventsQueue.push_back(ev);
 	}

+ 1 - 5
client/eventsSDL/InputSourceMouse.cpp

@@ -52,11 +52,7 @@ void InputSourceMouse::handleEventMouseButtonDown(const SDL_MouseButtonEvent & b
 
 void InputSourceMouse::handleEventMouseWheel(const SDL_MouseWheelEvent & wheel)
 {
-	// SDL doesn't have the proper values for mouse positions on SDL_MOUSEWHEEL, refetch them
-	int x = 0, y = 0;
-	SDL_GetMouseState(&x, &y);
-
-	GH.events().dispatchMouseScrolled(Point(wheel.x, wheel.y), Point(x, y));
+	GH.events().dispatchMouseScrolled(Point(wheel.x, wheel.y), GH.getCursorPosition());
 }
 
 void InputSourceMouse::handleEventMouseButtonUp(const SDL_MouseButtonEvent & button)

+ 4 - 1
client/gui/EventDispatcher.cpp

@@ -193,7 +193,10 @@ void EventDispatcher::dispatchMouseScrolled(const Point & distance, const Point
 	{
 		if(!vstd::contains(wheelInterested,i))
 			continue;
-		i->wheelScrolled(distance.y < 0, i->isInside(position));
+
+		// ignore distance value and only provide its sign - we expect one scroll "event" to move sliders and such by 1 point,
+		// and not by system-specific "number of lines to scroll", which is what 'distance' represents
+		i->wheelScrolled( std::clamp(distance.y, -1, 1) , i->isInside(position));
 	}
 }
 

+ 1 - 1
client/gui/EventsReceiver.h

@@ -44,7 +44,7 @@ protected:
 	virtual void clickDouble() {}
 
 	virtual void gesturePanning(const Point & distanceDelta) {}
-	virtual void wheelScrolled(bool down, bool in) {}
+	virtual void wheelScrolled(int distance, bool inside) {}
 	virtual void mouseMoved(const Point & cursorPosition) {}
 	virtual void hover(bool on) {}
 

+ 3 - 3
client/mapView/MapViewActions.cpp

@@ -64,11 +64,11 @@ void MapViewActions::mouseMoved(const Point & cursorPosition)
 	handleHover(cursorPosition);
 }
 
-void MapViewActions::wheelScrolled(bool down, bool in)
+void MapViewActions::wheelScrolled(int distance, bool inside)
 {
-	if (!in)
+	if (!inside)
 		return;
-	adventureInt->hotkeyZoom(down ? -1 : +1);
+	adventureInt->hotkeyZoom(distance);
 }
 
 void MapViewActions::gesturePanning(const Point & distance)

+ 1 - 1
client/mapView/MapViewActions.h

@@ -34,5 +34,5 @@ public:
 	void gesturePanning(const Point & distance) override;
 	void hover(bool on) override;
 	void mouseMoved(const Point & cursorPosition) override;
-	void wheelScrolled(bool down, bool in) override;
+	void wheelScrolled(int distance, bool inside) override;
 };

+ 2 - 2
client/widgets/Buttons.cpp

@@ -707,7 +707,7 @@ void CSlider::showAll(Canvas & to)
 	CIntObject::showAll(to);
 }
 
-void CSlider::wheelScrolled(bool down, bool in)
+void CSlider::wheelScrolled(int distance, bool in)
 {
 	if (scrollBounds)
 	{
@@ -719,7 +719,7 @@ void CSlider::wheelScrolled(bool down, bool in)
 
 	// vertical slider -> scrolling up move slider upwards
 	// horizontal slider -> scrolling up moves slider towards right
-	bool positive = (down != horizontal);
+	bool positive = ((distance < 0) != horizontal);
 
 	moveTo(value + 3 * (positive ? +scrollStep : -scrollStep));
 }

+ 1 - 1
client/widgets/Buttons.h

@@ -238,7 +238,7 @@ public:
 	void addCallback(std::function<void(int)> callback);
 
 	void keyPressed(EShortcut key) override;
-	void wheelScrolled(bool down, bool in) override;
+	void wheelScrolled(int distance, bool in) override;
 	void clickLeft(tribool down, bool previousState) override;
 	void mouseMoved (const Point & cursorPosition) override;
 	void showAll(Canvas & to) override;