Maelstrom: Use SDL's multi-touch support for the control buttons.

https://github.com/libsdl-org/Maelstrom/commit/7b5d203c09b02a8a2d0059ec3a996e28ad95f67f

From 7b5d203c09b02a8a2d0059ec3a996e28ad95f67f Mon Sep 17 00:00:00 2001
From: Sam Lantinga <[EMAIL REDACTED]>
Date: Mon, 7 Nov 2011 01:31:44 -0500
Subject: [PATCH] Use SDL's multi-touch support for the control buttons.

---
 MaelstromUI.cpp            | 32 ++++++++++++++++++++++++++++++++
 MaelstromUI.h              |  2 ++
 screenlib/SDL_FrameBuf.cpp | 16 ++++++++++++++++
 screenlib/SDL_FrameBuf.h   |  2 ++
 4 files changed, 52 insertions(+)

diff --git a/MaelstromUI.cpp b/MaelstromUI.cpp
index 87e6a51a..0a21a353 100644
--- a/MaelstromUI.cpp
+++ b/MaelstromUI.cpp
@@ -230,7 +230,10 @@ MaelstromUI::CreateElement(UIBaseElement *parent, const char *type, const char *
 UIElementControlButton::UIElementControlButton(UIBaseElement *parent, const char *name, UIDrawEngine *drawEngine) :
 	UIElement(parent, name, drawEngine)
 {
+#ifndef __IPHONEOS__
+	// Use the mouse if touch control isn't available
 	m_mouseEnabled = true;
+#endif
 }
 
 bool
@@ -270,6 +273,35 @@ UIElementControlButton::Load(rapidxml::xml_node<> *node, const UITemplates *temp
 	return true;
 }
 
+bool
+UIElementControlButton::HandleEvent(const SDL_Event &event)
+{
+	if (UIElement::HandleEvent(event)) {
+		return true;
+	}
+
+	if (event.type == SDL_FINGERDOWN) {
+		// Convert the touch coordinate into something useful here
+		int x, y;
+
+		if (!m_screen->ConvertTouchCoordinates(event.tfinger, &x, &y)) {
+			return false;
+		}
+		if (ContainsPoint(x, y)) {
+			m_finger = event.tfinger.fingerId;
+			OnMouseDown();
+			return true;
+		}
+	}
+	if (event.type == SDL_FINGERUP && event.tfinger.fingerId == m_finger) {
+		m_finger = 0;
+		OnMouseUp();
+		return true;
+	}
+
+	return false;
+}
+
 void
 UIElementControlButton::OnMouseDown()
 {
diff --git a/MaelstromUI.h b/MaelstromUI.h
index 9047819f..30e103be 100644
--- a/MaelstromUI.h
+++ b/MaelstromUI.h
@@ -67,11 +67,13 @@ class UIElementControlButton : public UIElement
 
 	override bool Load(rapidxml::xml_node<> *node, const UITemplates *templates);
 
+	override bool HandleEvent(const SDL_Event &event);
 	override void OnMouseDown();
 	override void OnMouseUp();
 
 protected:
 	unsigned char m_control;
+	SDL_FingerID m_finger;
 };
 
 //////////////////////////////////////////////////////////////////////////////
diff --git a/screenlib/SDL_FrameBuf.cpp b/screenlib/SDL_FrameBuf.cpp
index 489ca546..31585805 100644
--- a/screenlib/SDL_FrameBuf.cpp
+++ b/screenlib/SDL_FrameBuf.cpp
@@ -106,6 +106,22 @@ FrameBuf:: SetPalette(SDL_Color *colors)
 	}
 }
 
+// This routine or something like it should probably go in SDL
+bool
+FrameBuf::ConvertTouchCoordinates(const SDL_TouchFingerEvent &finger, int *x, int *y)
+{
+	int w, h;
+	SDL_Touch* inTouch = SDL_GetTouch(finger.touchId);
+	if (inTouch == NULL) {
+		return false;
+	}
+
+	SDL_GetWindowSize(window, &w, &h);
+	*x = (int)((((float)finger.x)/inTouch->xres)*w) - rect.x;
+	*y = (int)((((float)finger.y)/inTouch->yres)*h) - rect.y;
+	return true;
+}
+
 void
 FrameBuf:: QueueBlit(int dstx, int dsty, SDL_Texture *src,
 			int srcx, int srcy, int w, int h, clipval do_clip)
diff --git a/screenlib/SDL_FrameBuf.h b/screenlib/SDL_FrameBuf.h
index 793f4484..38885c5e 100644
--- a/screenlib/SDL_FrameBuf.h
+++ b/screenlib/SDL_FrameBuf.h
@@ -94,6 +94,8 @@ class FrameBuf : public ErrorBase {
 				break;
 		}
 	}
+	bool ConvertTouchCoordinates(const SDL_TouchFingerEvent &finger, int *x, int *y);
+
 	void ToggleFullScreen(void) {
 		if (SDL_GetWindowFlags(window) & SDL_WINDOW_FULLSCREEN) {
 			SDL_SetWindowFullscreen(window, SDL_FALSE);