Skip to content

Commit

Permalink
macos: get interface and user_data from the window instead of storing…
Browse files Browse the repository at this point in the history
… it in the view
  • Loading branch information
eyelash committed Sep 18, 2024
1 parent d7f8924 commit ec8b437
Showing 1 changed file with 34 additions and 23 deletions.
57 changes: 34 additions & 23 deletions gral_macos.m
Original file line number Diff line number Diff line change
Expand Up @@ -415,8 +415,6 @@ - (void)windowDidResignKey:(NSNotification *)notification {

@interface GralView: NSView<NSTextInputClient> {
@public
struct gral_window_interface interface;
void *user_data;
BOOL is_pointer_locked;
}
@end
Expand All @@ -428,29 +426,34 @@ - (BOOL)isFlipped {
return YES;
}
- (void)drawRect:(NSRect)rect {
GralWindow *window = (GralWindow *)[self window];
CGContextRef context = [[NSGraphicsContext currentContext] CGContext];
interface.draw((struct gral_draw_context *)context, rect.origin.x, rect.origin.y, rect.size.width, rect.size.height, user_data);
window->interface.draw((struct gral_draw_context *)context, rect.origin.x, rect.origin.y, rect.size.width, rect.size.height, window->user_data);
}
- (void)setFrameSize:(NSSize)size {
[super setFrameSize:size];
interface.resize(size.width, size.height, user_data);
GralWindow *window = (GralWindow *)[self window];
window->interface.resize(size.width, size.height, window->user_data);
}
- (BOOL)acceptsFirstMouse:(NSEvent *)event {
return YES;
}
- (void)mouseEntered:(NSEvent *)event {
interface.mouse_enter(user_data);
GralWindow *window = (GralWindow *)[self window];
window->interface.mouse_enter(window->user_data);
}
- (void)mouseExited:(NSEvent *)event {
interface.mouse_leave(user_data);
GralWindow *window = (GralWindow *)[self window];
window->interface.mouse_leave(window->user_data);
}
- (void)mouseMoved:(NSEvent *)event {
GralWindow *window = (GralWindow *)[self window];
if (is_pointer_locked) {
interface.mouse_move_relative([event deltaX], [event deltaY], user_data);
window->interface.mouse_move_relative([event deltaX], [event deltaY], window->user_data);
}
else {
NSPoint location = [self convertPoint:[event locationInWindow] fromView:nil];
interface.mouse_move(location.x, location.y, user_data);
window->interface.mouse_move(location.x, location.y, window->user_data);
}
}
- (void)mouseDragged:(NSEvent *)event {
Expand All @@ -463,57 +466,66 @@ - (void)otherMouseDragged:(NSEvent *)event {
[self mouseMoved:event];
}
- (void)mouseDown:(NSEvent *)event {
GralWindow *window = (GralWindow *)[self window];
NSPoint location = [self convertPoint:[event locationInWindow] fromView:nil];
int modifiers = get_modifiers([event modifierFlags]);
interface.mouse_button_press(location.x, location.y, GRAL_PRIMARY_MOUSE_BUTTON, modifiers, user_data);
window->interface.mouse_button_press(location.x, location.y, GRAL_PRIMARY_MOUSE_BUTTON, modifiers, window->user_data);
if ([event clickCount] == 2) {
interface.double_click(location.x, location.y, GRAL_PRIMARY_MOUSE_BUTTON, modifiers, user_data);
window->interface.double_click(location.x, location.y, GRAL_PRIMARY_MOUSE_BUTTON, modifiers, window->user_data);
}
}
- (void)rightMouseDown:(NSEvent *)event {
GralWindow *window = (GralWindow *)[self window];
NSPoint location = [self convertPoint:[event locationInWindow] fromView:nil];
int modifiers = get_modifiers([event modifierFlags]);
interface.mouse_button_press(location.x, location.y, GRAL_SECONDARY_MOUSE_BUTTON, modifiers, user_data);
window->interface.mouse_button_press(location.x, location.y, GRAL_SECONDARY_MOUSE_BUTTON, modifiers, window->user_data);
if ([event clickCount] == 2) {
interface.double_click(location.x, location.y, GRAL_SECONDARY_MOUSE_BUTTON, modifiers, user_data);
window->interface.double_click(location.x, location.y, GRAL_SECONDARY_MOUSE_BUTTON, modifiers, window->user_data);
}
}
- (void)otherMouseDown:(NSEvent *)event {
GralWindow *window = (GralWindow *)[self window];
NSPoint location = [self convertPoint:[event locationInWindow] fromView:nil];
int modifiers = get_modifiers([event modifierFlags]);
interface.mouse_button_press(location.x, location.y, GRAL_MIDDLE_MOUSE_BUTTON, modifiers, user_data);
window->interface.mouse_button_press(location.x, location.y, GRAL_MIDDLE_MOUSE_BUTTON, modifiers, window->user_data);
if ([event clickCount] == 2) {
interface.double_click(location.x, location.y, GRAL_MIDDLE_MOUSE_BUTTON, modifiers, user_data);
window->interface.double_click(location.x, location.y, GRAL_MIDDLE_MOUSE_BUTTON, modifiers, window->user_data);
}
}
- (void)mouseUp:(NSEvent *)event {
GralWindow *window = (GralWindow *)[self window];
NSPoint location = [self convertPoint:[event locationInWindow] fromView:nil];
interface.mouse_button_release(location.x, location.y, GRAL_PRIMARY_MOUSE_BUTTON, user_data);
window->interface.mouse_button_release(location.x, location.y, GRAL_PRIMARY_MOUSE_BUTTON, window->user_data);
}
- (void)rightMouseUp:(NSEvent *)event {
GralWindow *window = (GralWindow *)[self window];
NSPoint location = [self convertPoint:[event locationInWindow] fromView:nil];
interface.mouse_button_release(location.x, location.y, GRAL_SECONDARY_MOUSE_BUTTON, user_data);
window->interface.mouse_button_release(location.x, location.y, GRAL_SECONDARY_MOUSE_BUTTON, window->user_data);
}
- (void)otherMouseUp:(NSEvent *)event {
GralWindow *window = (GralWindow *)[self window];
NSPoint location = [self convertPoint:[event locationInWindow] fromView:nil];
interface.mouse_button_release(location.x, location.y, GRAL_MIDDLE_MOUSE_BUTTON, user_data);
window->interface.mouse_button_release(location.x, location.y, GRAL_MIDDLE_MOUSE_BUTTON, window->user_data);
}
- (void)scrollWheel:(NSEvent *)event {
interface.scroll([event scrollingDeltaX], [event scrollingDeltaY], user_data);
GralWindow *window = (GralWindow *)[self window];
window->interface.scroll([event scrollingDeltaX], [event scrollingDeltaY], window->user_data);
}
- (void)keyDown:(NSEvent *)event {
GralWindow *window = (GralWindow *)[self window];
[self interpretKeyEvents:[NSArray arrayWithObject:event]];
unsigned short key_code = [event keyCode];
int key = get_key(key_code);
if (key) {
interface.key_press(key, key_code, get_modifiers([event modifierFlags]), user_data);
window->interface.key_press(key, key_code, get_modifiers([event modifierFlags]), window->user_data);
}
}
- (void)keyUp:(NSEvent *)event {
GralWindow *window = (GralWindow *)[self window];
unsigned short key_code = [event keyCode];
int key = get_key(key_code);
if (key) {
interface.key_release(key, key_code, user_data);
window->interface.key_release(key, key_code, window->user_data);
}
}
// NSTextInputClient implementation
Expand All @@ -539,8 +551,9 @@ - (NSAttributedString *)attributedSubstringForProposedRange:(NSRange)range actua
return nil;
}
- (void)insertText:(id)string replacementRange:(NSRange)replacementRange {
GralWindow *window = (GralWindow *)[self window];
if ([string isKindOfClass:[NSString class]]) {
interface.text([(NSString *)string UTF8String], user_data);
window->interface.text([(NSString *)string UTF8String], window->user_data);
}
}
- (NSUInteger)characterIndexForPoint:(NSPoint)point {
Expand All @@ -566,8 +579,6 @@ - (void)doCommandBySelector:(SEL)selector {
[window setDelegate:window];
[window setTitle:[NSString stringWithUTF8String:title]];
GralView *view = [[GralView alloc] init];
view->interface = *interface;
view->user_data = user_data;
view->is_pointer_locked = NO;
NSTrackingArea *trackingArea = [[NSTrackingArea alloc]
initWithRect:CGRectMake(0, 0, width, height)
Expand Down

0 comments on commit ec8b437

Please sign in to comment.