/
SDL_uikitview.m
190 lines (145 loc) · 5.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
/*
Simple DirectMedia Layer
Copyright (C) 1997-2014 Sam Lantinga <slouken@libsdl.org>
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must not
claim that you wrote the original software. If you use this software
in a product, an acknowledgment in the product documentation would be
appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be
misrepresented as being the original software.
3. This notice may not be removed or altered from any source distribution.
*/
#include "../../SDL_internal.h"
#if SDL_VIDEO_DRIVER_UIKIT
#include "SDL_uikitview.h"
#include "../../events/SDL_mouse_c.h"
#include "../../events/SDL_touch_c.h"
29
#include "../../events/SDL_events_c.h"
30
31
32
33
#import "SDL_uikitappdelegate.h"
#import "SDL_uikitmodes.h"
#import "SDL_uikitwindow.h"
34
35
@implementation SDL_uikitview {
36
SDL_Window *sdlwindow;
37
38
SDL_TouchID touchId;
39
UITouch * __weak firstFingerDown;
40
}
41
42
- (instancetype)initWithFrame:(CGRect)frame
43
{
44
45
46
if ((self = [super initWithFrame:frame])) {
self.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
self.autoresizesSubviews = YES;
47
48
self.multipleTouchEnabled = YES;
49
50
51
52
touchId = 1;
SDL_AddTouch(touchId, "");
}
53
54
55
56
return self;
}
57
- (void)setSDLWindow:(SDL_Window *)window
58
{
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
SDL_WindowData *data = nil;
if (window == sdlwindow) {
return;
}
if (sdlwindow) {
SDL_uikitview *view = nil;
data = (__bridge SDL_WindowData *) sdlwindow->driverdata;
[data.views removeObject:self];
[self removeFromSuperview];
/* Restore the next-oldest view in the old window. */
if (data.views.count > 0) {
view = data.views[data.views.count - 1];
}
data.viewcontroller.view = view;
if (data.uiwindow.rootViewController != data.viewcontroller) {
data.uiwindow.rootViewController = data.viewcontroller;
} else if (view) {
[data.uiwindow addSubview:view];
}
[data.uiwindow layoutIfNeeded];
}
if (window) {
data = (__bridge SDL_WindowData *) window->driverdata;
/* Make sure the SDL window has a strong reference to this view. */
[data.views addObject:self];
/* Replace the view controller's old view with this one. */
[data.viewcontroller.view removeFromSuperview];
data.viewcontroller.view = self;
if (data.uiwindow.rootViewController != data.viewcontroller) {
/* The root view controller handles rotation and the status bar.
* Assigning it also adds the controller's view to the window. */
data.uiwindow.rootViewController = data.viewcontroller;
} else {
[data.uiwindow addSubview:self];
}
/* The view's bounds may not be correct until the next event cycle. That
* might happen after the current dimensions are queried, so we force a
* layout now to immediately update the bounds. */
[data.uiwindow layoutIfNeeded];
}
sdlwindow = window;
114
115
}
116
117
- (CGPoint)touchLocation:(UITouch *)touch shouldNormalize:(BOOL)normalize
{
118
CGPoint point = [touch locationInView:self];
119
120
if (normalize) {
121
CGRect bounds = self.bounds;
122
123
124
point.x /= bounds.size.width;
point.y /= bounds.size.height;
}
125
126
127
128
129
130
return point;
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
131
for (UITouch *touch in touches) {
132
if (!firstFingerDown) {
133
134
CGPoint locationInView = [self touchLocation:touch shouldNormalize:NO];
135
/* send mouse moved event */
136
SDL_SendMouseMotion(sdlwindow, SDL_TOUCH_MOUSEID, 0, locationInView.x, locationInView.y);
137
138
/* send mouse down event */
139
SDL_SendMouseButton(sdlwindow, SDL_TOUCH_MOUSEID, SDL_PRESSED, SDL_BUTTON_LEFT);
140
141
firstFingerDown = touch;
142
143
144
145
146
147
148
149
150
151
}
CGPoint locationInView = [self touchLocation:touch shouldNormalize:YES];
SDL_SendTouch(touchId, (SDL_FingerID)((size_t)touch),
SDL_TRUE, locationInView.x, locationInView.y, 1.0f);
}
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
152
for (UITouch *touch in touches) {
153
if (touch == firstFingerDown) {
154
/* send mouse up */
155
SDL_SendMouseButton(sdlwindow, SDL_TOUCH_MOUSEID, SDL_RELEASED, SDL_BUTTON_LEFT);
156
firstFingerDown = nil;
157
158
159
}
CGPoint locationInView = [self touchLocation:touch shouldNormalize:YES];
160
SDL_SendTouch(touchId, (SDL_FingerID)((size_t)touch),
161
162
163
164
165
166
SDL_FALSE, locationInView.x, locationInView.y, 1.0f);
}
}
- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event
{
167
[self touchesEnded:touches withEvent:event];
168
169
170
171
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
172
for (UITouch *touch in touches) {
173
if (touch == firstFingerDown) {
174
175
176
CGPoint locationInView = [self touchLocation:touch shouldNormalize:NO];
/* send moved event */
177
SDL_SendMouseMotion(sdlwindow, SDL_TOUCH_MOUSEID, 0, locationInView.x, locationInView.y);
178
179
180
}
CGPoint locationInView = [self touchLocation:touch shouldNormalize:YES];
181
SDL_SendTouchMotion(touchId, (SDL_FingerID)((size_t)touch),
182
183
184
185
186
187
188
189
190
locationInView.x, locationInView.y, 1.0f);
}
}
@end
#endif /* SDL_VIDEO_DRIVER_UIKIT */
/* vi: set ts=4 sw=4 expandtab: */