···11+/*
22+ Simple DirectMedia Layer
33+ Copyright (C) 1997-2024 Sam Lantinga <slouken@libsdl.org>
44+55+ This software is provided 'as-is', without any express or implied
66+ warranty. In no event will the authors be held liable for any damages
77+ arising from the use of this software.
88+99+ Permission is granted to anyone to use this software for any purpose,
1010+ including commercial applications, and to alter it and redistribute it
1111+ freely, subject to the following restrictions:
1212+1313+ 1. The origin of this software must not be misrepresented; you must not
1414+ claim that you wrote the original software. If you use this software
1515+ in a product, an acknowledgment in the product documentation would be
1616+ appreciated but is not required.
1717+ 2. Altered source versions must be plainly marked as such, and must not be
1818+ misrepresented as being the original software.
1919+ 3. This notice may not be removed or altered from any source distribution.
2020+*/
2121+2222+#ifndef SDL_uikitpen_h_
2323+#define SDL_uikitpen_h_
2424+2525+#include "SDL_uikitvideo.h"
2626+#include "SDL_uikitwindow.h"
2727+2828+extern bool UIKit_InitPen(SDL_VideoDevice *_this);
2929+extern void UIKit_HandlePenEnter();
3030+extern void UIKit_HandlePenLeave();
3131+extern void UIKit_HandlePenHover(SDL_uikitview *view, CGPoint point);
3232+extern void UIKit_HandlePenMotion(SDL_uikitview *view, UITouch *pencil);
3333+extern void UIKit_HandlePenPress(SDL_uikitview *view, UITouch *pencil);
3434+extern void UIKit_HandlePenRelease(SDL_uikitview *view, UITouch *pencil);
3535+extern void UIKit_QuitPen(SDL_VideoDevice *_this);
3636+3737+#endif // SDL_uikitpen_h_
+93
src/video/uikit/SDL_uikitpen.m
···11+/*
22+ Simple DirectMedia Layer
33+ Copyright (C) 1997-2024 Sam Lantinga <slouken@libsdl.org>
44+55+ This software is provided 'as-is', without any express or implied
66+ warranty. In no event will the authors be held liable for any damages
77+ arising from the use of this software.
88+99+ Permission is granted to anyone to use this software for any purpose,
1010+ including commercial applications, and to alter it and redistribute it
1111+ freely, subject to the following restrictions:
1212+1313+ 1. The origin of this software must not be misrepresented; you must not
1414+ claim that you wrote the original software. If you use this software
1515+ in a product, an acknowledgment in the product documentation would be
1616+ appreciated but is not required.
1717+ 2. Altered source versions must be plainly marked as such, and must not be
1818+ misrepresented as being the original software.
1919+ 3. This notice may not be removed or altered from any source distribution.
2020+*/
2121+#include "SDL_internal.h"
2222+2323+#ifdef SDL_VIDEO_DRIVER_UIKIT
2424+2525+#include "SDL_uikitevents.h"
2626+#include "SDL_uikitpen.h"
2727+#include "SDL_uikitwindow.h"
2828+2929+#include "../../events/SDL_pen_c.h"
3030+3131+SDL_PenID penId;
3232+3333+typedef struct UIKit_PenHandle
3434+{
3535+ SDL_PenID pen;
3636+} UIKit_PenHandle;
3737+3838+bool UIKit_InitPen(SDL_VideoDevice *_this)
3939+{
4040+ return true;
4141+}
4242+4343+void UIKit_HandlePenEnter()
4444+{
4545+ SDL_PenInfo penInfo;
4646+ SDL_zero(penInfo);
4747+ penInfo.capabilities = SDL_PEN_CAPABILITY_PRESSURE | SDL_PEN_CAPABILITY_ROTATION | SDL_PEN_CAPABILITY_XTILT | SDL_PEN_CAPABILITY_YTILT | SDL_PEN_CAPABILITY_TANGENTIAL_PRESSURE;
4848+ penInfo.max_tilt = 90.0f;
4949+ penInfo.num_buttons = 0;
5050+ penInfo.subtype = SDL_PEN_TYPE_PENCIL;
5151+5252+ // probably make this better
5353+ penId = SDL_AddPenDevice(0, [@"Apple Pencil" UTF8String], &penInfo, calloc(1, sizeof(UIKit_PenHandle)));
5454+}
5555+5656+void UIKit_HandlePenHover(SDL_uikitview *view, CGPoint point)
5757+{
5858+ SDL_SendPenMotion(0, penId, [view getSDLWindow], point.x, point.y);
5959+}
6060+6161+void UIKit_HandlePenMotion(SDL_uikitview *view, UITouch *pencil)
6262+{
6363+ CGPoint point = [pencil locationInView:view];
6464+ SDL_SendPenMotion(UIKit_GetEventTimestamp([pencil timestamp]), penId, [view getSDLWindow], point.x, point.y);
6565+ SDL_SendPenAxis(UIKit_GetEventTimestamp([pencil timestamp]), penId, [view getSDLWindow], SDL_PEN_AXIS_PRESSURE, [pencil force] / [pencil maximumPossibleForce]);
6666+ NSLog(@"ALTITUDE: %f", [pencil altitudeAngle]);
6767+ NSLog(@"AZIMUTH VECTOR: %@", [NSValue valueWithCGVector: [pencil azimuthUnitVectorInView:view]]);
6868+ NSLog(@"AZIMUTH ANGLE: %f", [pencil azimuthAngleInView:view]);
6969+ // hold it
7070+ // SDL_SendPenAxis(0, penId, [view getSDLWindow], SDL_PEN_AXIS_XTILT, [pencil altitudeAngle] / M_PI);
7171+}
7272+7373+void UIKit_HandlePenPress(SDL_uikitview *view, UITouch *pencil)
7474+{
7575+ SDL_SendPenTouch(UIKit_GetEventTimestamp([pencil timestamp]), penId, [view getSDLWindow], false, true);
7676+}
7777+7878+void UIKit_HandlePenRelease(SDL_uikitview *view, UITouch *pencil)
7979+{
8080+ SDL_SendPenTouch(UIKit_GetEventTimestamp([pencil timestamp]), penId, [view getSDLWindow], false, false);
8181+}
8282+8383+void UIKit_HandlePenLeave()
8484+{
8585+ SDL_RemovePenDevice(0, penId);
8686+ penId = 0;
8787+}
8888+8989+void UIKit_QuitPen(SDL_VideoDevice *_this)
9090+{
9191+}
9292+9393+#endif // SDL_VIDEO_DRIVER_UIKIT