Skip to content

Latest commit

 

History

History
190 lines (145 loc) · 5.83 KB

SDL_uikitview.m

File metadata and controls

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"
Jan 15, 2015
Jan 15, 2015
29
#include "../../events/SDL_events_c.h"
Jan 15, 2015
Jan 15, 2015
31
32
33
#import "SDL_uikitappdelegate.h"
#import "SDL_uikitmodes.h"
#import "SDL_uikitwindow.h"
Jul 23, 2014
Jul 23, 2014
35
@implementation SDL_uikitview {
Jan 15, 2015
Jan 15, 2015
36
SDL_Window *sdlwindow;
Jul 23, 2014
Jul 23, 2014
37
38
SDL_TouchID touchId;
Jan 15, 2015
Jan 15, 2015
39
UITouch * __weak firstFingerDown;
Jul 23, 2014
Jul 23, 2014
40
}
Jan 15, 2015
Jan 15, 2015
42
- (instancetype)initWithFrame:(CGRect)frame
Jan 15, 2015
Jan 15, 2015
44
45
46
if ((self = [super initWithFrame:frame])) {
self.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
self.autoresizesSubviews = YES;
Jul 29, 2014
Jul 29, 2014
48
self.multipleTouchEnabled = YES;
Jul 29, 2014
Jul 29, 2014
50
51
52
touchId = 1;
SDL_AddTouch(touchId, "");
}
53
54
55
56
return self;
}
Jan 15, 2015
Jan 15, 2015
57
- (void)setSDLWindow:(SDL_Window *)window
Nov 24, 2014
Nov 24, 2014
58
{
Jan 15, 2015
Jan 15, 2015
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;
Nov 24, 2014
Nov 24, 2014
114
115
}
116
117
- (CGPoint)touchLocation:(UITouch *)touch shouldNormalize:(BOOL)normalize
{
Nov 20, 2014
Nov 20, 2014
118
CGPoint point = [touch locationInView:self];
119
120
if (normalize) {
Jul 23, 2014
Jul 23, 2014
121
CGRect bounds = self.bounds;
122
123
124
point.x /= bounds.size.width;
point.y /= bounds.size.height;
}
Jul 15, 2014
Jul 15, 2014
125
126
127
128
129
130
return point;
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
Jul 14, 2014
Jul 14, 2014
131
for (UITouch *touch in touches) {
Jan 15, 2015
Jan 15, 2015
132
if (!firstFingerDown) {
133
134
CGPoint locationInView = [self touchLocation:touch shouldNormalize:NO];
Jan 15, 2015
Jan 15, 2015
135
/* send mouse moved event */
Nov 28, 2014
Nov 28, 2014
136
SDL_SendMouseMotion(sdlwindow, SDL_TOUCH_MOUSEID, 0, locationInView.x, locationInView.y);
137
138
/* send mouse down event */
Nov 28, 2014
Nov 28, 2014
139
SDL_SendMouseButton(sdlwindow, SDL_TOUCH_MOUSEID, SDL_PRESSED, SDL_BUTTON_LEFT);
Jan 15, 2015
Jan 15, 2015
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
{
Jul 14, 2014
Jul 14, 2014
152
for (UITouch *touch in touches) {
Jan 15, 2015
Jan 15, 2015
153
if (touch == firstFingerDown) {
154
/* send mouse up */
Nov 28, 2014
Nov 28, 2014
155
SDL_SendMouseButton(sdlwindow, SDL_TOUCH_MOUSEID, SDL_RELEASED, SDL_BUTTON_LEFT);
Jan 15, 2015
Jan 15, 2015
156
firstFingerDown = nil;
157
158
159
}
CGPoint locationInView = [self touchLocation:touch shouldNormalize:YES];
Jul 23, 2014
Jul 23, 2014
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
{
Nov 20, 2014
Nov 20, 2014
167
[self touchesEnded:touches withEvent:event];
168
169
170
171
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
Jul 14, 2014
Jul 14, 2014
172
for (UITouch *touch in touches) {
Jan 15, 2015
Jan 15, 2015
173
if (touch == firstFingerDown) {
174
175
176
CGPoint locationInView = [self touchLocation:touch shouldNormalize:NO];
/* send moved event */
Nov 28, 2014
Nov 28, 2014
177
SDL_SendMouseMotion(sdlwindow, SDL_TOUCH_MOUSEID, 0, locationInView.x, locationInView.y);
178
179
180
}
CGPoint locationInView = [self touchLocation:touch shouldNormalize:YES];
Jul 23, 2014
Jul 23, 2014
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: */