| 1 |
/**************************************************************************/ |
| 2 |
/* godot_content_view.mm */ |
| 3 |
/**************************************************************************/ |
| 4 |
/* This file is part of: */ |
| 5 |
/* GODOT ENGINE */ |
| 6 |
/* https://godotengine.org */ |
| 7 |
/**************************************************************************/ |
| 8 |
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */ |
| 9 |
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */ |
| 10 |
/* */ |
| 11 |
/* Permission is hereby granted, free of charge, to any person obtaining */ |
| 12 |
/* a copy of this software and associated documentation files (the */ |
| 13 |
/* "Software"), to deal in the Software without restriction, including */ |
| 14 |
/* without limitation the rights to use, copy, modify, merge, publish, */ |
| 15 |
/* distribute, sublicense, and/or sell copies of the Software, and to */ |
| 16 |
/* permit persons to whom the Software is furnished to do so, subject to */ |
| 17 |
/* the following conditions: */ |
| 18 |
/* */ |
| 19 |
/* The above copyright notice and this permission notice shall be */ |
| 20 |
/* included in all copies or substantial portions of the Software. */ |
| 21 |
/* */ |
| 22 |
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ |
| 23 |
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ |
| 24 |
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */ |
| 25 |
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ |
| 26 |
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ |
| 27 |
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ |
| 28 |
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ |
| 29 |
/**************************************************************************/ |
| 30 |
|
| 31 |
#import "godot_content_view.h" |
| 32 |
|
| 33 |
#import "display_server_macos.h" |
| 34 |
#import "godot_window.h" |
| 35 |
#import "key_mapping_macos.h" |
| 36 |
|
| 37 |
#include "core/profiling/profiling.h" |
| 38 |
#include "main/main.h" |
| 39 |
|
| 40 |
@implementation GodotContentLayerDelegate |
| 41 |
|
| 42 |
- (id)init { |
| 43 |
self = [super init]; |
| 44 |
window_id = DisplayServer::INVALID_WINDOW_ID; |
| 45 |
need_redraw = false; |
| 46 |
return self; |
| 47 |
} |
| 48 |
|
| 49 |
- (void)setWindowID:(DisplayServerMacOS::WindowID)wid { |
| 50 |
window_id = wid; |
| 51 |
} |
| 52 |
|
| 53 |
- (void)setNeedRedraw:(bool)redraw { |
| 54 |
need_redraw = redraw; |
| 55 |
} |
| 56 |
|
| 57 |
- (void)displayLayer:(CALayer *)layer { |
| 58 |
DisplayServerMacOS *ds = (DisplayServerMacOS *)DisplayServer::get_singleton(); |
| 59 |
if (OS::get_singleton()->get_main_loop() && ds->get_is_resizing() && need_redraw) { |
| 60 |
GodotProfileFrameMark; |
| 61 |
GodotProfileZone("[GodotContentLayerDelegate displayLayer]"); |
| 62 |
|
| 63 |
Main::force_redraw(); |
| 64 |
if (!Main::is_iterating()) { // Avoid cyclic loop. |
| 65 |
Main::iteration(); |
| 66 |
} |
| 67 |
need_redraw = false; |
| 68 |
} |
| 69 |
} |
| 70 |
|
| 71 |
@end |
| 72 |
|
| 73 |
@implementation GodotContentView |
| 74 |
|
| 75 |
- (BOOL)acceptsFirstMouse:(NSEvent *)event { |
| 76 |
return YES; |
| 77 |
} |
| 78 |
|
| 79 |
- (void)setFrameSize:(NSSize)newSize { |
| 80 |
DisplayServerMacOS *ds = (DisplayServerMacOS *)DisplayServer::get_singleton(); |
| 81 |
if (ds && ds->has_window(window_id)) { |
| 82 |
DisplayServerMacOS::WindowData &wd = ds->get_window(window_id); |
| 83 |
NSRect frameRect = [wd.window_object frame]; |
| 84 |
if (wd.fs_transition || wd.initial_size) { |
| 85 |
self.layerContentsPlacement = NSViewLayerContentsPlacementScaleAxesIndependently; |
| 86 |
wd.initial_size = false; |
| 87 |
} else { |
| 88 |
bool left = (wd.last_frame_rect.origin.x != frameRect.origin.x); |
| 89 |
bool bottom = (wd.last_frame_rect.origin.y != frameRect.origin.y); |
| 90 |
bool right = (wd.last_frame_rect.origin.x + wd.last_frame_rect.size.width != frameRect.origin.x + frameRect.size.width); |
| 91 |
bool top = (wd.last_frame_rect.origin.y + wd.last_frame_rect.size.height != frameRect.origin.y + frameRect.size.height); |
| 92 |
|
| 93 |
if (left && top) { |
| 94 |
self.layerContentsPlacement = NSViewLayerContentsPlacementBottomRight; |
| 95 |
} else if (left && bottom) { |
| 96 |
self.layerContentsPlacement = NSViewLayerContentsPlacementTopRight; |
| 97 |
} else if (left) { |
| 98 |
self.layerContentsPlacement = NSViewLayerContentsPlacementRight; |
| 99 |
} else if (right && top) { |
| 100 |
self.layerContentsPlacement = NSViewLayerContentsPlacementBottomLeft; |
| 101 |
} else if (right && bottom) { |
| 102 |
self.layerContentsPlacement = NSViewLayerContentsPlacementTopLeft; |
| 103 |
} else if (right) { |
| 104 |
self.layerContentsPlacement = NSViewLayerContentsPlacementLeft; |
| 105 |
} |
| 106 |
} |
| 107 |
wd.last_frame_rect = frameRect; |
| 108 |
} |
| 109 |
|
| 110 |
[super setFrameSize:newSize]; |
| 111 |
[layer_delegate setNeedRedraw:true]; |
| 112 |
[self.layer setNeedsDisplay]; // Force "drawRect" call. |
| 113 |
} |
| 114 |
|
| 115 |
- (void)updateLayerDelegate { |
| 116 |
self.layer.delegate = layer_delegate; |
| 117 |
self.layer.autoresizingMask = kCALayerHeightSizable | kCALayerWidthSizable; |
| 118 |
self.layer.needsDisplayOnBoundsChange = YES; |
| 119 |
} |
| 120 |
|
| 121 |
- (void)addObserver:(NSObject *)targetObserver forKeyPath:(NSString *)keyPath options:(NSKeyValueObservingOptions)options context:(void *)context { |
| 122 |
[registered_observers addObject:[[targetObserver description] stringByAppendingString:keyPath]]; |
| 123 |
[super addObserver:targetObserver forKeyPath:keyPath options:options context:context]; |
| 124 |
} |
| 125 |
|
| 126 |
- (void)removeObserver:(NSObject *)targetObserver forKeyPath:(NSString *)keyPath { |
| 127 |
if ([registered_observers containsObject:[[targetObserver description] stringByAppendingString:keyPath]]) { |
| 128 |
@try { |
| 129 |
[super removeObserver:targetObserver forKeyPath:keyPath]; |
| 130 |
[registered_observers removeObject:[[targetObserver description] stringByAppendingString:keyPath]]; |
| 131 |
} @catch (NSException *exception) { |
| 132 |
ERR_PRINT("NSException: " + String::utf8([exception reason].UTF8String)); |
| 133 |
} |
| 134 |
} |
| 135 |
} |
| 136 |
|
| 137 |
- (id)init { |
| 138 |
self = [super init]; |
| 139 |
layer_delegate = [[GodotContentLayerDelegate alloc] init]; |
| 140 |
window_id = DisplayServer::INVALID_WINDOW_ID; |
| 141 |
tracking_area = nil; |
| 142 |
ime_input_event_in_progress = false; |
| 143 |
mouse_down_control = false; |
| 144 |
ignore_momentum_scroll = false; |
| 145 |
last_pen_inverted = false; |
| 146 |
registered_observers = [[NSMutableSet alloc] init]; |
| 147 |
[self updateTrackingAreas]; |
| 148 |
|
| 149 |
self.layerContentsRedrawPolicy = NSViewLayerContentsRedrawDuringViewResize; |
| 150 |
self.layerContentsPlacement = NSViewLayerContentsPlacementTopLeft; |
| 151 |
|
| 152 |
[self registerForDraggedTypes:[NSArray arrayWithObject:NSPasteboardTypeFileURL]]; |
| 153 |
marked_text = [[NSMutableAttributedString alloc] init]; |
| 154 |
return self; |
| 155 |
} |
| 156 |
|
| 157 |
- (void)setWindowID:(DisplayServerMacOS::WindowID)wid { |
| 158 |
window_id = wid; |
| 159 |
[layer_delegate setWindowID:window_id]; |
| 160 |
} |
| 161 |
|
| 162 |
// MARK: Backing Layer |
| 163 |
|
| 164 |
- (CALayer *)makeBackingLayer { |
| 165 |
CAMetalLayer *layer = [CAMetalLayer new]; |
| 166 |
layer.edgeAntialiasingMask = 0; |
| 167 |
layer.masksToBounds = NO; |
| 168 |
layer.presentsWithTransaction = NO; |
| 169 |
[layer removeAllAnimations]; |
| 170 |
return layer; |
| 171 |
} |
| 172 |
|
| 173 |
- (BOOL)wantsUpdateLayer { |
| 174 |
return YES; |
| 175 |
} |
| 176 |
|
| 177 |
- (BOOL)isOpaque { |
| 178 |
return YES; |
| 179 |
} |
| 180 |
|
| 181 |
// MARK: IME |
| 182 |
|
| 183 |
- (BOOL)hasMarkedText { |
| 184 |
return (marked_text.length > 0); |
| 185 |
} |
| 186 |
|
| 187 |
- (NSRange)markedRange { |
| 188 |
return NSMakeRange(0, marked_text.length); |
| 189 |
} |
| 190 |
|
| 191 |
- (NSRange)selectedRange { |
| 192 |
static const NSRange kEmptyRange = { NSNotFound, 0 }; |
| 193 |
return kEmptyRange; |
| 194 |
} |
| 195 |
|
| 196 |
- (void)setMarkedText:(id)aString selectedRange:(NSRange)selectedRange replacementRange:(NSRange)replacementRange { |
| 197 |
if ([aString isKindOfClass:[NSAttributedString class]]) { |
| 198 |
marked_text = [[NSMutableAttributedString alloc] initWithAttributedString:aString]; |
| 199 |
} else { |
| 200 |
marked_text = [[NSMutableAttributedString alloc] initWithString:aString]; |
| 201 |
} |
| 202 |
if (marked_text.length == 0) { |
| 203 |
[self unmarkText]; |
| 204 |
return; |
| 205 |
} |
| 206 |
|
| 207 |
DisplayServerMacOS *ds = (DisplayServerMacOS *)DisplayServer::get_singleton(); |
| 208 |
if (!ds || !ds->has_window(window_id)) { |
| 209 |
return; |
| 210 |
} |
| 211 |
|
| 212 |
DisplayServerMacOS::WindowData &wd = ds->get_window(window_id); |
| 213 |
if (wd.im_active) { |
| 214 |
ime_input_event_in_progress = true; |
| 215 |
ds->pop_last_key_event(); |
| 216 |
ds->update_im_text(Point2i(selectedRange.location, selectedRange.length), String::utf8([[marked_text mutableString] UTF8String])); |
| 217 |
} |
| 218 |
} |
| 219 |
|
| 220 |
- (void)doCommandBySelector:(SEL)aSelector { |
| 221 |
[self tryToPerform:aSelector with:self]; |
| 222 |
} |
| 223 |
|
| 224 |
- (void)unmarkText { |
| 225 |
if (ime_input_event_in_progress) { |
| 226 |
ime_suppress_next_keyup = true; |
| 227 |
} |
| 228 |
ime_input_event_in_progress = false; |
| 229 |
[[marked_text mutableString] setString:@""]; |
| 230 |
|
| 231 |
DisplayServerMacOS *ds = (DisplayServerMacOS *)DisplayServer::get_singleton(); |
| 232 |
if (!ds || !ds->has_window(window_id)) { |
| 233 |
return; |
| 234 |
} |
| 235 |
|
| 236 |
DisplayServerMacOS::WindowData &wd = ds->get_window(window_id); |
| 237 |
if (wd.im_active) { |
| 238 |
ds->update_im_text(Point2i(), String()); |
| 239 |
} |
| 240 |
} |
| 241 |
|
| 242 |
- (NSArray *)validAttributesForMarkedText { |
| 243 |
return [NSArray array]; |
| 244 |
} |
| 245 |
|
| 246 |
- (NSAttributedString *)attributedSubstringForProposedRange:(NSRange)aRange actualRange:(NSRangePointer)actualRange { |
| 247 |
return nil; |
| 248 |
} |
| 249 |
|
| 250 |
- (NSUInteger)characterIndexForPoint:(NSPoint)aPoint { |
| 251 |
return 0; |
| 252 |
} |
| 253 |
|
| 254 |
- (NSRect)firstRectForCharacterRange:(NSRange)aRange actualRange:(NSRangePointer)actualRange { |
| 255 |
DisplayServerMacOS *ds = (DisplayServerMacOS *)DisplayServer::get_singleton(); |
| 256 |
if (!ds || !ds->has_window(window_id)) { |
| 257 |
return NSMakeRect(0, 0, 0, 0); |
| 258 |
} |
| 259 |
|
| 260 |
DisplayServerMacOS::WindowData &wd = ds->get_window(window_id); |
| 261 |
if (!wd.im_active) { |
| 262 |
return NSMakeRect(0, 0, 0, 0); |
| 263 |
} |
| 264 |
|
| 265 |
const NSRect content_rect = [wd.window_view frame]; |
| 266 |
const float scale = ds->screen_get_max_scale(); |
| 267 |
NSRect point_in_window_rect = NSMakeRect(wd.im_position.x / scale, content_rect.size.height - (wd.im_position.y / scale) - 1, 0, 0); |
| 268 |
NSPoint point_on_screen = [wd.window_object convertRectToScreen:point_in_window_rect].origin; |
| 269 |
|
| 270 |
return NSMakeRect(point_on_screen.x, point_on_screen.y, 0, 0); |
| 271 |
} |
| 272 |
|
| 273 |
- (void)cancelComposition { |
| 274 |
[self unmarkText]; |
| 275 |
[[NSTextInputContext currentInputContext] discardMarkedText]; |
| 276 |
} |
| 277 |
|
| 278 |
- (void)insertText:(id)aString { |
| 279 |
[self insertText:aString replacementRange:NSMakeRange(0, 0)]; |
| 280 |
} |
| 281 |
|
| 282 |
- (void)insertText:(id)aString replacementRange:(NSRange)replacementRange { |
| 283 |
NSString *characters; |
| 284 |
if ([aString isKindOfClass:[NSAttributedString class]]) { |
| 285 |
characters = [aString string]; |
| 286 |
} else { |
| 287 |
characters = (NSString *)aString; |
| 288 |
} |
| 289 |
|
| 290 |
NSCharacterSet *ctrl_chars = [NSCharacterSet controlCharacterSet]; |
| 291 |
NSCharacterSet *wsnl_chars = [NSCharacterSet whitespaceAndNewlineCharacterSet]; |
| 292 |
if ([characters rangeOfCharacterFromSet:ctrl_chars].length && [characters rangeOfCharacterFromSet:wsnl_chars].length == 0) { |
| 293 |
[[NSTextInputContext currentInputContext] discardMarkedText]; |
| 294 |
[self cancelComposition]; |
| 295 |
return; |
| 296 |
} |
| 297 |
|
| 298 |
DisplayServerMacOS *ds = (DisplayServerMacOS *)DisplayServer::get_singleton(); |
| 299 |
if (!ds || !ds->has_window(window_id)) { |
| 300 |
[self cancelComposition]; |
| 301 |
return; |
| 302 |
} |
| 303 |
|
| 304 |
Char16String text; |
| 305 |
text.resize_uninitialized([characters length] + 1); |
| 306 |
[characters getCharacters:(unichar *)text.ptrw() range:NSMakeRange(0, [characters length])]; |
| 307 |
|
| 308 |
String u32text = String::utf16(text.ptr(), text.length()); |
| 309 |
|
| 310 |
for (int i = 0; i < u32text.length(); i++) { |
| 311 |
const char32_t codepoint = u32text[i]; |
| 312 |
if ((codepoint & 0xFF00) == 0xF700) { |
| 313 |
continue; |
| 314 |
} |
| 315 |
|
| 316 |
DisplayServerMacOS::KeyEvent ke; |
| 317 |
|
| 318 |
ke.window_id = window_id; |
| 319 |
ke.macos_state = 0; |
| 320 |
ke.pressed = true; |
| 321 |
ke.echo = false; |
| 322 |
ke.raw = false; // IME input event. |
| 323 |
ke.keycode = Key::NONE; |
| 324 |
ke.physical_keycode = Key::NONE; |
| 325 |
ke.key_label = Key::NONE; |
| 326 |
ke.unicode = fix_unicode(codepoint); |
| 327 |
|
| 328 |
ds->push_to_key_event_buffer(ke); |
| 329 |
} |
| 330 |
[self cancelComposition]; |
| 331 |
} |
| 332 |
|
| 333 |
// MARK: Drag and drop |
| 334 |
|
| 335 |
- (NSDragOperation)draggingEntered:(id<NSDraggingInfo>)sender { |
| 336 |
return NSDragOperationCopy; |
| 337 |
} |
| 338 |
|
| 339 |
- (NSDragOperation)draggingUpdated:(id<NSDraggingInfo>)sender { |
| 340 |
return NSDragOperationCopy; |
| 341 |
} |
| 342 |
|
| 343 |
- (BOOL)performDragOperation:(id<NSDraggingInfo>)sender { |
| 344 |
DisplayServerMacOS *ds = (DisplayServerMacOS *)DisplayServer::get_singleton(); |
| 345 |
if (!ds || !ds->has_window(window_id)) { |
| 346 |
return NO; |
| 347 |
} |
| 348 |
|
| 349 |
DisplayServerMacOS::WindowData &wd = ds->get_window(window_id); |
| 350 |
if (wd.drop_files_callback.is_valid()) { |
| 351 |
Vector<String> files; |
| 352 |
NSPasteboard *pboard = [sender draggingPasteboard]; |
| 353 |
|
| 354 |
NSArray *items = pboard.pasteboardItems; |
| 355 |
for (NSPasteboardItem *item in items) { |
| 356 |
NSString *url = [item stringForType:NSPasteboardTypeFileURL]; |
| 357 |
NSString *file = [NSURL URLWithString:url].path; |
| 358 |
files.push_back(String::utf8([file UTF8String])); |
| 359 |
} |
| 360 |
Variant v_files = files; |
| 361 |
const Variant *v_args[1] = { &v_files }; |
| 362 |
Variant ret; |
| 363 |
Callable::CallError ce; |
| 364 |
wd.drop_files_callback.callp((const Variant **)&v_args, 1, ret, ce); |
| 365 |
if (ce.error != Callable::CallError::CALL_OK) { |
| 366 |
ERR_FAIL_V_MSG(NO, vformat("Failed to execute drop files callback: %s.", Variant::get_callable_error_text(wd.drop_files_callback, v_args, 1, ce))); |
| 367 |
} |
| 368 |
return YES; |
| 369 |
} |
| 370 |
|
| 371 |
return NO; |
| 372 |
} |
| 373 |
|
| 374 |
// MARK: Focus |
| 375 |
|
| 376 |
- (BOOL)canBecomeKeyView { |
| 377 |
DisplayServerMacOS *ds = (DisplayServerMacOS *)DisplayServer::get_singleton(); |
| 378 |
if (!ds || !ds->has_window(window_id)) { |
| 379 |
return YES; |
| 380 |
} |
| 381 |
|
| 382 |
DisplayServerMacOS::WindowData &wd = ds->get_window(window_id); |
| 383 |
return !wd.no_focus; |
| 384 |
} |
| 385 |
|
| 386 |
- (BOOL)acceptsFirstResponder { |
| 387 |
return YES; |
| 388 |
} |
| 389 |
|
| 390 |
// MARK: Mouse |
| 391 |
|
| 392 |
- (void)cursorUpdate:(NSEvent *)event { |
| 393 |
DisplayServerMacOS *ds = (DisplayServerMacOS *)DisplayServer::get_singleton(); |
| 394 |
if (!ds) { |
| 395 |
return; |
| 396 |
} |
| 397 |
|
| 398 |
ds->cursor_update_shape(); |
| 399 |
} |
| 400 |
|
| 401 |
- (void)processMouseEvent:(NSEvent *)event index:(MouseButton)index pressed:(bool)pressed outofstream:(bool)outofstream { |
| 402 |
DisplayServerMacOS *ds = (DisplayServerMacOS *)DisplayServer::get_singleton(); |
| 403 |
if (!ds || !ds->has_window(window_id)) { |
| 404 |
return; |
| 405 |
} |
| 406 |
|
| 407 |
DisplayServerMacOS::WindowData &wd = ds->get_window(window_id); |
| 408 |
|
| 409 |
Ref<InputEventMouseButton> mb; |
| 410 |
mb.instantiate(); |
| 411 |
mb->set_window_id(window_id); |
| 412 |
if (outofstream) { |
| 413 |
ds->update_mouse_pos(wd, [wd.window_object mouseLocationOutsideOfEventStream]); |
| 414 |
} else { |
| 415 |
ds->update_mouse_pos(wd, [event locationInWindow]); |
| 416 |
} |
| 417 |
ds->get_key_modifier_state([event modifierFlags], mb); |
| 418 |
mb->set_button_index(index); |
| 419 |
mb->set_pressed(pressed); |
| 420 |
mb->set_position(wd.mouse_pos); |
| 421 |
mb->set_global_position(wd.mouse_pos); |
| 422 |
mb->set_button_mask(ds->mouse_get_button_state()); |
| 423 |
if (!outofstream && index == MouseButton::LEFT && pressed) { |
| 424 |
mb->set_double_click([event clickCount] == 2); |
| 425 |
} |
| 426 |
|
| 427 |
Input::get_singleton()->parse_input_event(mb); |
| 428 |
} |
| 429 |
|
| 430 |
- (void)mouseDown:(NSEvent *)event { |
| 431 |
DisplayServerMacOS *ds = (DisplayServerMacOS *)DisplayServer::get_singleton(); |
| 432 |
if (ds && ds->has_window(window_id)) { |
| 433 |
DisplayServerMacOS::WindowData &wd = ds->get_window(window_id); |
| 434 |
wd.edge = DisplayServer::WINDOW_EDGE_MAX; |
| 435 |
} |
| 436 |
if (([event modifierFlags] & NSEventModifierFlagControl)) { |
| 437 |
mouse_down_control = true; |
| 438 |
[self processMouseEvent:event index:MouseButton::RIGHT pressed:true outofstream:false]; |
| 439 |
} else { |
| 440 |
mouse_down_control = false; |
| 441 |
[self processMouseEvent:event index:MouseButton::LEFT pressed:true outofstream:false]; |
| 442 |
} |
| 443 |
} |
| 444 |
|
| 445 |
- (void)mouseDragged:(NSEvent *)event { |
| 446 |
DisplayServerMacOS *ds = (DisplayServerMacOS *)DisplayServer::get_singleton(); |
| 447 |
if (ds && ds->has_window(window_id)) { |
| 448 |
DisplayServerMacOS::WindowData &wd = ds->get_window(window_id); |
| 449 |
if (wd.edge != DisplayServer::WINDOW_EDGE_MAX) { |
| 450 |
Size2i max_size = wd.max_size / ds->screen_get_max_scale(); |
| 451 |
Size2i min_size = wd.min_size / ds->screen_get_max_scale(); |
| 452 |
NSRect frame = [wd.window_object frame]; |
| 453 |
switch (wd.edge) { |
| 454 |
case DisplayServer::WINDOW_EDGE_TOP_LEFT: { |
| 455 |
int clamped_dx = CLAMP(frame.size.width - event.deltaX, min_size.x, max_size.x) - frame.size.width; |
| 456 |
int clamped_dy = CLAMP(frame.size.height - event.deltaY, min_size.y, max_size.y) - frame.size.height; |
| 457 |
[wd.window_object setFrame:NSMakeRect(frame.origin.x - clamped_dx, frame.origin.y, frame.size.width + clamped_dx, frame.size.height + clamped_dy) display:YES]; |
| 458 |
} break; |
| 459 |
case DisplayServer::WINDOW_EDGE_TOP: { |
| 460 |
int clamped_dy = CLAMP(frame.size.height - event.deltaY, min_size.y, max_size.y) - frame.size.height; |
| 461 |
[wd.window_object setFrame:NSMakeRect(frame.origin.x, frame.origin.y, frame.size.width, frame.size.height + clamped_dy) display:YES]; |
| 462 |
} break; |
| 463 |
case DisplayServer::WINDOW_EDGE_TOP_RIGHT: { |
| 464 |
int clamped_dx = CLAMP(frame.size.width + event.deltaX, min_size.x, max_size.x) - frame.size.width; |
| 465 |
int clamped_dy = CLAMP(frame.size.height - event.deltaY, min_size.y, max_size.y) - frame.size.height; |
| 466 |
[wd.window_object setFrame:NSMakeRect(frame.origin.x, frame.origin.y, frame.size.width + clamped_dx, frame.size.height + clamped_dy) display:YES]; |
| 467 |
} break; |
| 468 |
case DisplayServer::WINDOW_EDGE_LEFT: { |
| 469 |
int clamped_dx = CLAMP(frame.size.width - event.deltaX, min_size.x, max_size.x) - frame.size.width; |
| 470 |
[wd.window_object setFrame:NSMakeRect(frame.origin.x - clamped_dx, frame.origin.y, frame.size.width + clamped_dx, frame.size.height) display:YES]; |
| 471 |
} break; |
| 472 |
case DisplayServer::WINDOW_EDGE_RIGHT: { |
| 473 |
int clamped_dx = CLAMP(frame.size.width + event.deltaX, min_size.x, max_size.x) - frame.size.width; |
| 474 |
[wd.window_object setFrame:NSMakeRect(frame.origin.x, frame.origin.y, frame.size.width + clamped_dx, frame.size.height) display:YES]; |
| 475 |
} break; |
| 476 |
case DisplayServer::WINDOW_EDGE_BOTTOM_LEFT: { |
| 477 |
int clamped_dx = CLAMP(frame.size.width - event.deltaX, min_size.x, max_size.x) - frame.size.width; |
| 478 |
int clamped_dy = CLAMP(frame.size.height + event.deltaY, min_size.y, max_size.y) - frame.size.height; |
| 479 |
[wd.window_object setFrame:NSMakeRect(frame.origin.x - clamped_dx, frame.origin.y - clamped_dy, frame.size.width + clamped_dx, frame.size.height + clamped_dy) display:YES]; |
| 480 |
} break; |
| 481 |
case DisplayServer::WINDOW_EDGE_BOTTOM: { |
| 482 |
int clamped_dy = CLAMP(frame.size.height + event.deltaY, min_size.y, max_size.y) - frame.size.height; |
| 483 |
[wd.window_object setFrame:NSMakeRect(frame.origin.x, frame.origin.y - clamped_dy, frame.size.width, frame.size.height + clamped_dy) display:YES]; |
| 484 |
} break; |
| 485 |
case DisplayServer::WINDOW_EDGE_BOTTOM_RIGHT: { |
| 486 |
int clamped_dx = CLAMP(frame.size.width + event.deltaX, min_size.x, max_size.x) - frame.size.width; |
| 487 |
int clamped_dy = CLAMP(frame.size.height + event.deltaY, min_size.y, max_size.y) - frame.size.height; |
| 488 |
[wd.window_object setFrame:NSMakeRect(frame.origin.x, frame.origin.y - clamped_dy, frame.size.width + clamped_dx, frame.size.height + clamped_dy) display:YES]; |
| 489 |
} break; |
| 490 |
default: |
| 491 |
break; |
| 492 |
} |
| 493 |
return; |
| 494 |
} |
| 495 |
} |
| 496 |
[self mouseMoved:event]; |
| 497 |
} |
| 498 |
|
| 499 |
- (void)mouseUp:(NSEvent *)event { |
| 500 |
DisplayServerMacOS *ds = (DisplayServerMacOS *)DisplayServer::get_singleton(); |
| 501 |
if (ds && ds->has_window(window_id)) { |
| 502 |
DisplayServerMacOS::WindowData &wd = ds->get_window(window_id); |
| 503 |
wd.edge = DisplayServer::WINDOW_EDGE_MAX; |
| 504 |
} |
| 505 |
if (mouse_down_control) { |
| 506 |
[self processMouseEvent:event index:MouseButton::RIGHT pressed:false outofstream:false]; |
| 507 |
} else { |
| 508 |
[self processMouseEvent:event index:MouseButton::LEFT pressed:false outofstream:false]; |
| 509 |
} |
| 510 |
} |
| 511 |
|
| 512 |
- (void)mouseMoved:(NSEvent *)event { |
| 513 |
DisplayServerMacOS *ds = (DisplayServerMacOS *)DisplayServer::get_singleton(); |
| 514 |
if (!ds || !ds->has_window(window_id)) { |
| 515 |
return; |
| 516 |
} |
| 517 |
|
| 518 |
DisplayServerMacOS::WindowData &wd = ds->get_window(window_id); |
| 519 |
|
| 520 |
NSPoint delta = NSMakePoint([event deltaX], [event deltaY]); |
| 521 |
NSPoint mpos = [event locationInWindow]; |
| 522 |
|
| 523 |
if (ds->update_mouse_wrap(wd, delta, mpos, [event timestamp])) { |
| 524 |
return; |
| 525 |
} |
| 526 |
|
| 527 |
Ref<InputEventMouseMotion> mm; |
| 528 |
mm.instantiate(); |
| 529 |
|
| 530 |
mm->set_window_id(window_id); |
| 531 |
mm->set_button_mask(ds->mouse_get_button_state()); |
| 532 |
ds->update_mouse_pos(wd, mpos); |
| 533 |
mm->set_position(wd.mouse_pos); |
| 534 |
mm->set_pressure([event pressure]); |
| 535 |
NSEventSubtype subtype = [event subtype]; |
| 536 |
if (subtype == NSEventSubtypeTabletPoint) { |
| 537 |
const NSPoint p = [event tilt]; |
| 538 |
mm->set_tilt(Vector2(p.x, -p.y)); |
| 539 |
mm->set_pen_inverted(last_pen_inverted); |
| 540 |
} else if (subtype == NSEventSubtypeTabletProximity) { |
| 541 |
// Check if using the eraser end of pen only on proximity event. |
| 542 |
last_pen_inverted = [event pointingDeviceType] == NSPointingDeviceTypeEraser; |
| 543 |
mm->set_pen_inverted(last_pen_inverted); |
| 544 |
} |
| 545 |
mm->set_global_position(wd.mouse_pos); |
| 546 |
mm->set_velocity(Input::get_singleton()->get_last_mouse_velocity()); |
| 547 |
mm->set_screen_velocity(mm->get_velocity()); |
| 548 |
const Vector2i relativeMotion = Vector2i(delta.x, delta.y) * ds->screen_get_max_scale(); |
| 549 |
mm->set_relative(relativeMotion); |
| 550 |
mm->set_relative_screen_position(relativeMotion); |
| 551 |
ds->get_key_modifier_state([event modifierFlags], mm); |
| 552 |
|
| 553 |
const NSRect contentRect = [wd.window_view frame]; |
| 554 |
if (NSPointInRect([event locationInWindow], contentRect) && [NSWindow windowNumberAtPoint:[NSEvent mouseLocation] belowWindowWithWindowNumber:0 /*topmost*/] == [wd.window_object windowNumber]) { |
| 555 |
ds->mouse_enter_window(window_id); |
| 556 |
} |
| 557 |
|
| 558 |
Input::get_singleton()->parse_input_event(mm); |
| 559 |
} |
| 560 |
|
| 561 |
- (void)rightMouseDown:(NSEvent *)event { |
| 562 |
[self processMouseEvent:event index:MouseButton::RIGHT pressed:true outofstream:false]; |
| 563 |
} |
| 564 |
|
| 565 |
- (void)rightMouseDragged:(NSEvent *)event { |
| 566 |
[self mouseMoved:event]; |
| 567 |
} |
| 568 |
|
| 569 |
- (void)rightMouseUp:(NSEvent *)event { |
| 570 |
[self processMouseEvent:event index:MouseButton::RIGHT pressed:false outofstream:false]; |
| 571 |
} |
| 572 |
|
| 573 |
- (void)otherMouseDown:(NSEvent *)event { |
| 574 |
if ((int)[event buttonNumber] == 2) { |
| 575 |
[self processMouseEvent:event index:MouseButton::MIDDLE pressed:true outofstream:false]; |
| 576 |
} else if ((int)[event buttonNumber] == 3) { |
| 577 |
[self processMouseEvent:event index:MouseButton::MB_XBUTTON1 pressed:true outofstream:false]; |
| 578 |
} else if ((int)[event buttonNumber] == 4) { |
| 579 |
[self processMouseEvent:event index:MouseButton::MB_XBUTTON2 pressed:true outofstream:false]; |
| 580 |
} else { |
| 581 |
return; |
| 582 |
} |
| 583 |
} |
| 584 |
|
| 585 |
- (void)otherMouseDragged:(NSEvent *)event { |
| 586 |
[self mouseMoved:event]; |
| 587 |
} |
| 588 |
|
| 589 |
- (void)otherMouseUp:(NSEvent *)event { |
| 590 |
if ((int)[event buttonNumber] == 2) { |
| 591 |
[self processMouseEvent:event index:MouseButton::MIDDLE pressed:false outofstream:false]; |
| 592 |
} else if ((int)[event buttonNumber] == 3) { |
| 593 |
[self processMouseEvent:event index:MouseButton::MB_XBUTTON1 pressed:false outofstream:false]; |
| 594 |
} else if ((int)[event buttonNumber] == 4) { |
| 595 |
[self processMouseEvent:event index:MouseButton::MB_XBUTTON2 pressed:false outofstream:false]; |
| 596 |
} else { |
| 597 |
return; |
| 598 |
} |
| 599 |
} |
| 600 |
|
| 601 |
- (void)swipeWithEvent:(NSEvent *)event { |
| 602 |
// Swipe gesture on Trackpad/Magic Mouse, or physical back/forward mouse buttons. |
| 603 |
if ([event phase] == NSEventPhaseEnded || [event phase] == NSEventPhaseChanged) { |
| 604 |
if (Math::is_equal_approx([event deltaX], 1.0)) { |
| 605 |
// Swipe left (back). |
| 606 |
[self processMouseEvent:event index:MouseButton::MB_XBUTTON1 pressed:true outofstream:true]; |
| 607 |
[self processMouseEvent:event index:MouseButton::MB_XBUTTON1 pressed:false outofstream:true]; |
| 608 |
} else if (Math::is_equal_approx([event deltaX], -1.0)) { |
| 609 |
// Swipe right (forward). |
| 610 |
[self processMouseEvent:event index:MouseButton::MB_XBUTTON2 pressed:true outofstream:true]; |
| 611 |
[self processMouseEvent:event index:MouseButton::MB_XBUTTON2 pressed:false outofstream:true]; |
| 612 |
} |
| 613 |
} |
| 614 |
} |
| 615 |
|
| 616 |
- (void)mouseExited:(NSEvent *)event { |
| 617 |
DisplayServerMacOS *ds = (DisplayServerMacOS *)DisplayServer::get_singleton(); |
| 618 |
if (!ds || !ds->has_window(window_id)) { |
| 619 |
return; |
| 620 |
} |
| 621 |
|
| 622 |
if (ds->mouse_get_mode() != DisplayServer::MOUSE_MODE_CAPTURED) { |
| 623 |
ds->mouse_exit_window(window_id); |
| 624 |
} |
| 625 |
} |
| 626 |
|
| 627 |
- (void)mouseEntered:(NSEvent *)event { |
| 628 |
DisplayServerMacOS *ds = (DisplayServerMacOS *)DisplayServer::get_singleton(); |
| 629 |
if (!ds || !ds->has_window(window_id)) { |
| 630 |
return; |
| 631 |
} |
| 632 |
|
| 633 |
if (ds->mouse_get_mode() != DisplayServer::MOUSE_MODE_CAPTURED) { |
| 634 |
ds->mouse_enter_window(window_id); |
| 635 |
} |
| 636 |
|
| 637 |
ds->cursor_update_shape(); |
| 638 |
} |
| 639 |
|
| 640 |
- (void)magnifyWithEvent:(NSEvent *)event { |
| 641 |
DisplayServerMacOS *ds = (DisplayServerMacOS *)DisplayServer::get_singleton(); |
| 642 |
if (!ds || !ds->has_window(window_id)) { |
| 643 |
return; |
| 644 |
} |
| 645 |
|
| 646 |
DisplayServerMacOS::WindowData &wd = ds->get_window(window_id); |
| 647 |
|
| 648 |
Ref<InputEventMagnifyGesture> ev; |
| 649 |
ev.instantiate(); |
| 650 |
ev->set_window_id(window_id); |
| 651 |
ds->get_key_modifier_state([event modifierFlags], ev); |
| 652 |
ds->update_mouse_pos(wd, [event locationInWindow]); |
| 653 |
ev->set_position(wd.mouse_pos); |
| 654 |
ev->set_factor([event magnification] + 1.0); |
| 655 |
|
| 656 |
Input::get_singleton()->parse_input_event(ev); |
| 657 |
} |
| 658 |
|
| 659 |
- (void)updateTrackingAreas { |
| 660 |
if (tracking_area != nil) { |
| 661 |
[self removeTrackingArea:tracking_area]; |
| 662 |
} |
| 663 |
|
| 664 |
NSTrackingAreaOptions options = NSTrackingMouseEnteredAndExited | NSTrackingActiveWhenFirstResponder | NSTrackingCursorUpdate | NSTrackingInVisibleRect; |
| 665 |
tracking_area = [[NSTrackingArea alloc] initWithRect:[self bounds] options:options owner:self userInfo:nil]; |
| 666 |
|
| 667 |
[self addTrackingArea:tracking_area]; |
| 668 |
[super updateTrackingAreas]; |
| 669 |
} |
| 670 |
|
| 671 |
// MARK: Keyboard |
| 672 |
|
| 673 |
- (void)keyDown:(NSEvent *)event { |
| 674 |
DisplayServerMacOS *ds = (DisplayServerMacOS *)DisplayServer::get_singleton(); |
| 675 |
if (!ds || !ds->has_window(window_id)) { |
| 676 |
return; |
| 677 |
} |
| 678 |
|
| 679 |
DisplayServerMacOS::WindowData &wd = ds->get_window(window_id); |
| 680 |
|
| 681 |
ignore_momentum_scroll = true; |
| 682 |
|
| 683 |
// Ignore all input if IME input is in progress. |
| 684 |
if (!ime_input_event_in_progress) { |
| 685 |
NSString *characters = [event characters]; |
| 686 |
NSUInteger length = [characters length]; |
| 687 |
|
| 688 |
if (!wd.im_active && length > 0 && keycode_has_unicode(KeyMappingMacOS::remap_key([event keyCode], [event modifierFlags], true))) { |
| 689 |
// Fallback unicode character handler used if IME is not active. |
| 690 |
Char16String text; |
| 691 |
text.resize_uninitialized([characters length] + 1); |
| 692 |
[characters getCharacters:(unichar *)text.ptrw() range:NSMakeRange(0, [characters length])]; |
| 693 |
|
| 694 |
String u32text = String::utf16(text.ptr(), text.length()); |
| 695 |
|
| 696 |
DisplayServerMacOS::KeyEvent ke; |
| 697 |
ke.window_id = window_id; |
| 698 |
ke.macos_state = [event modifierFlags]; |
| 699 |
ke.pressed = true; |
| 700 |
ke.echo = [event isARepeat]; |
| 701 |
ke.keycode = KeyMappingMacOS::remap_key([event keyCode], [event modifierFlags], false); |
| 702 |
ke.physical_keycode = KeyMappingMacOS::translate_key([event keyCode]); |
| 703 |
ke.key_label = KeyMappingMacOS::remap_key([event keyCode], [event modifierFlags], true); |
| 704 |
ke.raw = true; |
| 705 |
|
| 706 |
if (u32text.is_empty()) { |
| 707 |
ke.unicode = 0; |
| 708 |
ds->push_to_key_event_buffer(ke); |
| 709 |
} |
| 710 |
for (int i = 0; i < u32text.length(); i++) { |
| 711 |
const char32_t codepoint = u32text[i]; |
| 712 |
ke.unicode = fix_unicode(codepoint); |
| 713 |
ds->push_to_key_event_buffer(ke); |
| 714 |
} |
| 715 |
} else { |
| 716 |
DisplayServerMacOS::KeyEvent ke; |
| 717 |
|
| 718 |
ke.window_id = window_id; |
| 719 |
ke.macos_state = [event modifierFlags]; |
| 720 |
ke.pressed = true; |
| 721 |
ke.echo = [event isARepeat]; |
| 722 |
ke.keycode = KeyMappingMacOS::remap_key([event keyCode], [event modifierFlags], false); |
| 723 |
ke.physical_keycode = KeyMappingMacOS::translate_key([event keyCode]); |
| 724 |
ke.key_label = KeyMappingMacOS::remap_key([event keyCode], [event modifierFlags], true); |
| 725 |
ke.unicode = 0; |
| 726 |
ke.location = KeyMappingMacOS::translate_location([event keyCode]); |
| 727 |
ke.raw = false; |
| 728 |
|
| 729 |
ds->push_to_key_event_buffer(ke); |
| 730 |
} |
| 731 |
} |
| 732 |
|
| 733 |
// Pass events to IME handler |
| 734 |
if (wd.im_active) { |
| 735 |
[self interpretKeyEvents:[NSArray arrayWithObject:event]]; |
| 736 |
} |
| 737 |
} |
| 738 |
|
| 739 |
- (void)flagsChanged:(NSEvent *)event { |
| 740 |
DisplayServerMacOS *ds = (DisplayServerMacOS *)DisplayServer::get_singleton(); |
| 741 |
if (!ds || !ds->has_window(window_id)) { |
| 742 |
return; |
| 743 |
} |
| 744 |
ignore_momentum_scroll = true; |
| 745 |
|
| 746 |
DisplayServerMacOS::KeyEvent ke; |
| 747 |
|
| 748 |
ke.window_id = window_id; |
| 749 |
ke.echo = false; |
| 750 |
ke.raw = true; |
| 751 |
|
| 752 |
int key = [event keyCode]; |
| 753 |
int mod = [event modifierFlags]; |
| 754 |
|
| 755 |
if (key == 0x36 || key == 0x37) { |
| 756 |
if (mod & NSEventModifierFlagCommand) { |
| 757 |
mod &= ~NSEventModifierFlagCommand; |
| 758 |
ke.pressed = true; |
| 759 |
} else { |
| 760 |
ke.pressed = false; |
| 761 |
} |
| 762 |
} else if (key == 0x38 || key == 0x3c) { |
| 763 |
if (mod & NSEventModifierFlagShift) { |
| 764 |
mod &= ~NSEventModifierFlagShift; |
| 765 |
ke.pressed = true; |
| 766 |
} else { |
| 767 |
ke.pressed = false; |
| 768 |
} |
| 769 |
} else if (key == 0x3a || key == 0x3d) { |
| 770 |
if (mod & NSEventModifierFlagOption) { |
| 771 |
mod &= ~NSEventModifierFlagOption; |
| 772 |
ke.pressed = true; |
| 773 |
} else { |
| 774 |
ke.pressed = false; |
| 775 |
} |
| 776 |
} else if (key == 0x3b || key == 0x3e) { |
| 777 |
if (mod & NSEventModifierFlagControl) { |
| 778 |
mod &= ~NSEventModifierFlagControl; |
| 779 |
ke.pressed = true; |
| 780 |
} else { |
| 781 |
ke.pressed = false; |
| 782 |
} |
| 783 |
} else { |
| 784 |
return; |
| 785 |
} |
| 786 |
|
| 787 |
ke.macos_state = mod; |
| 788 |
ke.keycode = KeyMappingMacOS::remap_key(key, mod, false); |
| 789 |
ke.physical_keycode = KeyMappingMacOS::translate_key(key); |
| 790 |
ke.key_label = KeyMappingMacOS::remap_key(key, mod, true); |
| 791 |
ke.unicode = 0; |
| 792 |
ke.location = KeyMappingMacOS::translate_location(key); |
| 793 |
|
| 794 |
ds->push_to_key_event_buffer(ke); |
| 795 |
} |
| 796 |
|
| 797 |
- (void)keyUp:(NSEvent *)event { |
| 798 |
DisplayServerMacOS *ds = (DisplayServerMacOS *)DisplayServer::get_singleton(); |
| 799 |
if (!ds || !ds->has_window(window_id)) { |
| 800 |
return; |
| 801 |
} |
| 802 |
|
| 803 |
// Ignore all input if IME input is in progress. |
| 804 |
if (ime_suppress_next_keyup) { |
| 805 |
ime_suppress_next_keyup = false; |
| 806 |
return; |
| 807 |
} |
| 808 |
|
| 809 |
if (!ime_input_event_in_progress) { |
| 810 |
DisplayServerMacOS::KeyEvent ke; |
| 811 |
|
| 812 |
ke.window_id = window_id; |
| 813 |
ke.macos_state = [event modifierFlags]; |
| 814 |
ke.pressed = false; |
| 815 |
ke.echo = [event isARepeat]; |
| 816 |
ke.keycode = KeyMappingMacOS::remap_key([event keyCode], [event modifierFlags], false); |
| 817 |
ke.physical_keycode = KeyMappingMacOS::translate_key([event keyCode]); |
| 818 |
ke.key_label = KeyMappingMacOS::remap_key([event keyCode], [event modifierFlags], true); |
| 819 |
ke.unicode = 0; |
| 820 |
ke.location = KeyMappingMacOS::translate_location([event keyCode]); |
| 821 |
ke.raw = true; |
| 822 |
|
| 823 |
ds->push_to_key_event_buffer(ke); |
| 824 |
} |
| 825 |
} |
| 826 |
|
| 827 |
// MARK: Scroll and pan |
| 828 |
|
| 829 |
- (void)processScrollEvent:(NSEvent *)event button:(MouseButton)button factor:(double)factor { |
| 830 |
DisplayServerMacOS *ds = (DisplayServerMacOS *)DisplayServer::get_singleton(); |
| 831 |
if (!ds || !ds->has_window(window_id)) { |
| 832 |
return; |
| 833 |
} |
| 834 |
|
| 835 |
DisplayServerMacOS::WindowData &wd = ds->get_window(window_id); |
| 836 |
MouseButtonMask mask = mouse_button_to_mask(button); |
| 837 |
|
| 838 |
Ref<InputEventMouseButton> sc; |
| 839 |
sc.instantiate(); |
| 840 |
|
| 841 |
sc->set_window_id(window_id); |
| 842 |
ds->get_key_modifier_state([event modifierFlags], sc); |
| 843 |
sc->set_button_index(button); |
| 844 |
sc->set_factor(factor); |
| 845 |
sc->set_pressed(true); |
| 846 |
sc->set_position(wd.mouse_pos); |
| 847 |
sc->set_global_position(wd.mouse_pos); |
| 848 |
BitField<MouseButtonMask> scroll_mask = ds->mouse_get_button_state(); |
| 849 |
scroll_mask.set_flag(mask); |
| 850 |
sc->set_button_mask(scroll_mask); |
| 851 |
|
| 852 |
Input::get_singleton()->parse_input_event(sc); |
| 853 |
|
| 854 |
sc.instantiate(); |
| 855 |
sc->set_window_id(window_id); |
| 856 |
sc->set_button_index(button); |
| 857 |
sc->set_factor(factor); |
| 858 |
sc->set_pressed(false); |
| 859 |
sc->set_position(wd.mouse_pos); |
| 860 |
sc->set_global_position(wd.mouse_pos); |
| 861 |
scroll_mask.clear_flag(mask); |
| 862 |
sc->set_button_mask(scroll_mask); |
| 863 |
|
| 864 |
Input::get_singleton()->parse_input_event(sc); |
| 865 |
} |
| 866 |
|
| 867 |
- (void)processPanEvent:(NSEvent *)event dx:(double)dx dy:(double)dy { |
| 868 |
DisplayServerMacOS *ds = (DisplayServerMacOS *)DisplayServer::get_singleton(); |
| 869 |
if (!ds || !ds->has_window(window_id)) { |
| 870 |
return; |
| 871 |
} |
| 872 |
|
| 873 |
DisplayServerMacOS::WindowData &wd = ds->get_window(window_id); |
| 874 |
|
| 875 |
Ref<InputEventPanGesture> pg; |
| 876 |
pg.instantiate(); |
| 877 |
|
| 878 |
pg->set_window_id(window_id); |
| 879 |
ds->get_key_modifier_state([event modifierFlags], pg); |
| 880 |
pg->set_position(wd.mouse_pos); |
| 881 |
pg->set_delta(Vector2(-dx, -dy)); |
| 882 |
|
| 883 |
Input::get_singleton()->parse_input_event(pg); |
| 884 |
} |
| 885 |
|
| 886 |
- (void)scrollWheel:(NSEvent *)event { |
| 887 |
DisplayServerMacOS *ds = (DisplayServerMacOS *)DisplayServer::get_singleton(); |
| 888 |
if (!ds || !ds->has_window(window_id)) { |
| 889 |
return; |
| 890 |
} |
| 891 |
|
| 892 |
DisplayServerMacOS::WindowData &wd = ds->get_window(window_id); |
| 893 |
ds->update_mouse_pos(wd, [event locationInWindow]); |
| 894 |
|
| 895 |
double delta_x = [event scrollingDeltaX]; |
| 896 |
double delta_y = [event scrollingDeltaY]; |
| 897 |
|
| 898 |
if ([event hasPreciseScrollingDeltas]) { |
| 899 |
delta_x *= 0.03; |
| 900 |
delta_y *= 0.03; |
| 901 |
} |
| 902 |
|
| 903 |
if ([event momentumPhase] != NSEventPhaseNone) { |
| 904 |
if (ignore_momentum_scroll) { |
| 905 |
return; |
| 906 |
} |
| 907 |
} else { |
| 908 |
ignore_momentum_scroll = false; |
| 909 |
} |
| 910 |
|
| 911 |
if ([event phase] != NSEventPhaseNone || [event momentumPhase] != NSEventPhaseNone) { |
| 912 |
[self processPanEvent:event dx:delta_x dy:delta_y]; |
| 913 |
} else { |
| 914 |
if (std::abs(delta_x)) { |
| 915 |
[self processScrollEvent:event button:(0 > delta_x ? MouseButton::WHEEL_RIGHT : MouseButton::WHEEL_LEFT) factor:std::abs(delta_x * 0.3)]; |
| 916 |
} |
| 917 |
if (std::abs(delta_y)) { |
| 918 |
[self processScrollEvent:event button:(0 < delta_y ? MouseButton::WHEEL_UP : MouseButton::WHEEL_DOWN) factor:std::abs(delta_y * 0.3)]; |
| 919 |
} |
| 920 |
} |
| 921 |
} |
| 922 |
|
| 923 |
@end |