OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #import "sky_surface.h" | 5 #import "sky_surface.h" |
6 | 6 |
7 #import <QuartzCore/QuartzCore.h> | 7 #import <QuartzCore/QuartzCore.h> |
8 #import <OpenGLES/EAGL.h> | 8 #import <OpenGLES/EAGL.h> |
9 #import <OpenGLES/EAGLDrawable.h> | 9 #import <OpenGLES/EAGLDrawable.h> |
10 | 10 |
(...skipping 17 matching lines...) Expand all Loading... |
28 return sky::EVENT_TYPE_POINTER_MOVE; | 28 return sky::EVENT_TYPE_POINTER_MOVE; |
29 case UITouchPhaseEnded: | 29 case UITouchPhaseEnded: |
30 return sky::EVENT_TYPE_POINTER_UP; | 30 return sky::EVENT_TYPE_POINTER_UP; |
31 case UITouchPhaseCancelled: | 31 case UITouchPhaseCancelled: |
32 return sky::EVENT_TYPE_POINTER_CANCEL; | 32 return sky::EVENT_TYPE_POINTER_CANCEL; |
33 } | 33 } |
34 | 34 |
35 return sky::EVENT_TYPE_UNKNOWN; | 35 return sky::EVENT_TYPE_UNKNOWN; |
36 } | 36 } |
37 | 37 |
| 38 static inline int64 InputEventTimestampFromNSTimeInterval( |
| 39 NSTimeInterval interval) { |
| 40 return base::TimeDelta::FromSecondsD(interval).InMilliseconds(); |
| 41 } |
| 42 |
| 43 static sky::InputEventPtr BasicInputEventFromRecognizer( |
| 44 sky::EventType type, |
| 45 UIGestureRecognizer* recognizer) { |
| 46 auto input = sky::InputEvent::New(); |
| 47 input->type = type; |
| 48 input->time_stamp = InputEventTimestampFromNSTimeInterval( |
| 49 CACurrentMediaTime()); |
| 50 |
| 51 input->gesture_data = sky::GestureData::New(); |
| 52 |
| 53 CGPoint windowCoordinates = [recognizer locationInView:recognizer.view]; |
| 54 const CGFloat scale = [UIScreen mainScreen].scale; |
| 55 input->gesture_data->x = windowCoordinates.x * scale; |
| 56 input->gesture_data->y = windowCoordinates.y * scale; |
| 57 return input.Pass(); |
| 58 } |
| 59 |
38 @implementation SkySurface { | 60 @implementation SkySurface { |
39 BOOL _platformViewInitialized; | 61 BOOL _platformViewInitialized; |
40 | 62 |
41 sky::ViewportObserverPtr _viewport_observer; | 63 sky::ViewportObserverPtr _viewport_observer; |
42 scoped_ptr<sky::shell::ShellView> _shell_view; | 64 scoped_ptr<sky::shell::ShellView> _shell_view; |
43 } | 65 } |
44 | 66 |
45 -(instancetype) initWithShellView:(sky::shell::ShellView *) shellView { | 67 -(instancetype) initWithShellView:(sky::shell::ShellView *) shellView { |
46 self = [super init]; | 68 self = [super init]; |
47 if (self) | 69 if (self) { |
48 _shell_view.reset(shellView); | 70 _shell_view.reset(shellView); |
| 71 [self installGestureRecognizers]; |
| 72 } |
49 return self; | 73 return self; |
50 } | 74 } |
51 | 75 |
52 - (gfx::AcceleratedWidget)acceleratedWidget { | 76 - (gfx::AcceleratedWidget)acceleratedWidget { |
53 return (gfx::AcceleratedWidget)self.layer; | 77 return (gfx::AcceleratedWidget)self.layer; |
54 } | 78 } |
55 | 79 |
56 - (void)layoutSubviews { | 80 - (void)layoutSubviews { |
57 [super layoutSubviews]; | 81 [super layoutSubviews]; |
58 | 82 |
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
116 | 140 |
117 #pragma mark - UIResponder overrides for raw touches | 141 #pragma mark - UIResponder overrides for raw touches |
118 | 142 |
119 - (void)dispatchTouches:(NSSet*)touches phase:(UITouchPhase)phase { | 143 - (void)dispatchTouches:(NSSet*)touches phase:(UITouchPhase)phase { |
120 auto eventType = EventTypeFromUITouchPhase(phase); | 144 auto eventType = EventTypeFromUITouchPhase(phase); |
121 const CGFloat scale = [UIScreen mainScreen].scale; | 145 const CGFloat scale = [UIScreen mainScreen].scale; |
122 | 146 |
123 for (UITouch* touch in touches) { | 147 for (UITouch* touch in touches) { |
124 auto input = sky::InputEvent::New(); | 148 auto input = sky::InputEvent::New(); |
125 input->type = eventType; | 149 input->type = eventType; |
126 | 150 input->time_stamp = InputEventTimestampFromNSTimeInterval(touch.timestamp); |
127 auto timedelta = base::TimeDelta::FromSecondsD(touch.timestamp); | |
128 input->time_stamp = timedelta.InMilliseconds(); | |
129 | 151 |
130 input->pointer_data = sky::PointerData::New(); | 152 input->pointer_data = sky::PointerData::New(); |
131 input->pointer_data->kind = sky::POINTER_KIND_TOUCH; | 153 input->pointer_data->kind = sky::POINTER_KIND_TOUCH; |
132 | 154 |
133 CGPoint windowCoordinates = [touch locationInView:nil]; | 155 CGPoint windowCoordinates = [touch locationInView:nil]; |
134 | 156 |
135 input->pointer_data->x = windowCoordinates.x * scale; | 157 input->pointer_data->x = windowCoordinates.x * scale; |
136 input->pointer_data->y = windowCoordinates.y * scale; | 158 input->pointer_data->y = windowCoordinates.y * scale; |
137 | 159 |
138 _viewport_observer->OnInputEvent(input.Pass()); | 160 _viewport_observer->OnInputEvent(input.Pass()); |
139 } | 161 } |
140 } | 162 } |
141 | 163 |
142 - (void)touchesBegan:(NSSet*)touches withEvent:(UIEvent*)event { | 164 - (void)touchesBegan:(NSSet*)touches withEvent:(UIEvent*)event { |
143 [self dispatchTouches:touches phase:UITouchPhaseBegan]; | 165 [self dispatchTouches:touches phase:UITouchPhaseBegan]; |
144 } | 166 } |
145 | 167 |
146 - (void)touchesMoved:(NSSet*)touches withEvent:(UIEvent*)event { | 168 - (void)touchesMoved:(NSSet*)touches withEvent:(UIEvent*)event { |
147 [self dispatchTouches:touches phase:UITouchPhaseMoved]; | 169 [self dispatchTouches:touches phase:UITouchPhaseMoved]; |
148 } | 170 } |
149 | 171 |
150 - (void)touchesEnded:(NSSet*)touches withEvent:(UIEvent*)event { | 172 - (void)touchesEnded:(NSSet*)touches withEvent:(UIEvent*)event { |
151 [self dispatchTouches:touches phase:UITouchPhaseEnded]; | 173 [self dispatchTouches:touches phase:UITouchPhaseEnded]; |
152 } | 174 } |
153 | 175 |
154 - (void)touchesCancelled:(NSSet*)touches withEvent:(UIEvent*)event { | 176 - (void)touchesCancelled:(NSSet*)touches withEvent:(UIEvent*)event { |
155 [self dispatchTouches:touches phase:UITouchPhaseCancelled]; | 177 [self dispatchTouches:touches phase:UITouchPhaseCancelled]; |
156 } | 178 } |
157 | 179 |
| 180 #pragma mark - Gesture Recognizers |
| 181 |
| 182 -(void) installGestureRecognizers { |
| 183 // For: |
| 184 // GESTURE_FLING_CANCEL |
| 185 // GESTURE_FLING_START |
| 186 UISwipeGestureRecognizer *swipe = [[UISwipeGestureRecognizer alloc] |
| 187 initWithTarget:self action:@selector(onFling:)]; |
| 188 [self addGestureRecognizer: swipe]; |
| 189 [swipe release]; |
| 190 |
| 191 // For: |
| 192 // GESTURE_LONG_PRESS |
| 193 // GESTURE_SHOW_PRESS |
| 194 UILongPressGestureRecognizer *longPress = |
| 195 [[UILongPressGestureRecognizer alloc] |
| 196 initWithTarget:self action:@selector(onLongPress:)]; |
| 197 [self addGestureRecognizer: longPress]; |
| 198 [longPress release]; |
| 199 |
| 200 // For: |
| 201 // GESTURE_SCROLL_BEGIN |
| 202 // GESTURE_SCROLL_END |
| 203 // GESTURE_SCROLL_UPDATE |
| 204 UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] |
| 205 initWithTarget:self action:@selector(onScroll:)]; |
| 206 [self addGestureRecognizer: pan]; |
| 207 [pan release]; |
| 208 |
| 209 // For: |
| 210 // GESTURE_TAP |
| 211 // GESTURE_TAP_DOWN |
| 212 UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] |
| 213 initWithTarget:self action:@selector(onTap:)]; |
| 214 [self addGestureRecognizer: tap]; |
| 215 [tap release]; |
| 216 } |
| 217 |
| 218 -(void) onFling:(UISwipeGestureRecognizer *) recognizer { |
| 219 // Swipes are discrete gestures already. So there is no equivalent to a cancel |
| 220 if (recognizer.state != UIGestureRecognizerStateEnded) { |
| 221 return; |
| 222 } |
| 223 |
| 224 auto input = BasicInputEventFromRecognizer( |
| 225 sky::EVENT_TYPE_GESTURE_FLING_START, recognizer); |
| 226 _viewport_observer->OnInputEvent(input.Pass()); |
| 227 } |
| 228 |
| 229 -(void) onLongPress:(UILongPressGestureRecognizer *) recognizer { |
| 230 if (recognizer.state != UIGestureRecognizerStateEnded) { |
| 231 return; |
| 232 } |
| 233 |
| 234 auto input = BasicInputEventFromRecognizer(sky::EVENT_TYPE_GESTURE_LONG_PRESS, |
| 235 recognizer); |
| 236 _viewport_observer->OnInputEvent(input.Pass()); |
| 237 } |
| 238 |
| 239 -(void) onScroll:(UIPanGestureRecognizer *) recognizer { |
| 240 sky::EventType type = sky::EVENT_TYPE_UNKNOWN; |
| 241 switch (recognizer.state) { |
| 242 case UIGestureRecognizerStateBegan: |
| 243 type = sky::EVENT_TYPE_GESTURE_SCROLL_BEGIN; |
| 244 break; |
| 245 case UIGestureRecognizerStateChanged: |
| 246 type = sky::EVENT_TYPE_GESTURE_SCROLL_UPDATE; |
| 247 break; |
| 248 case UIGestureRecognizerStateEnded: |
| 249 case UIGestureRecognizerStateCancelled: |
| 250 case UIGestureRecognizerStateFailed: |
| 251 type = sky::EVENT_TYPE_GESTURE_SCROLL_END; |
| 252 break; |
| 253 default: |
| 254 break; |
| 255 } |
| 256 |
| 257 if (type == sky::EVENT_TYPE_UNKNOWN) { |
| 258 return; |
| 259 } |
| 260 |
| 261 auto input = BasicInputEventFromRecognizer(type, recognizer); |
| 262 auto scale = [UIScreen mainScreen].scale; |
| 263 auto translation = [recognizer translationInView: self]; |
| 264 auto velocity = [recognizer velocityInView: self]; |
| 265 |
| 266 input->gesture_data->dx = translation.x * scale; |
| 267 input->gesture_data->dy = translation.y * scale; |
| 268 input->gesture_data->velocityX = velocity.x * scale; |
| 269 input->gesture_data->velocityY = velocity.y * scale; |
| 270 |
| 271 _viewport_observer->OnInputEvent(input.Pass()); |
| 272 } |
| 273 |
| 274 -(void) onTap:(UITapGestureRecognizer *) recognizer { |
| 275 |
| 276 if (recognizer.state != UIGestureRecognizerStateEnded) { |
| 277 return; |
| 278 } |
| 279 |
| 280 auto input = BasicInputEventFromRecognizer(sky::EVENT_TYPE_GESTURE_TAP, |
| 281 recognizer); |
| 282 _viewport_observer->OnInputEvent(input.Pass()); |
| 283 } |
| 284 |
158 #pragma mark - Misc. | 285 #pragma mark - Misc. |
159 | 286 |
160 + (Class)layerClass { | 287 + (Class)layerClass { |
161 return [CAEAGLLayer class]; | 288 return [CAEAGLLayer class]; |
162 } | 289 } |
163 | 290 |
164 - (void)dealloc { | 291 - (void)dealloc { |
165 [self notifySurfaceDestruction]; | 292 [self notifySurfaceDestruction]; |
166 [super dealloc]; | 293 [super dealloc]; |
167 } | 294 } |
168 | 295 |
169 @end | 296 @end |
OLD | NEW |