Post

Replies

Boosts

Views

Activity

Reply to Input with Metal-CPP, overriding MTK::View
@Hipreme @Graphics and Games Engineer @m0zrat I have some actual working code -- and wanted to share. I extended MTK::View with some Objective-c code (objcpp), and wrote an adapter. @Graphics and Games Engineer -- I first tried to use NSNotificationCenter to get a Notification of a GCKeyboard connection, and by assigning an objective-c block to keyChangedHandler; for whatever the reason, I could not get the notifcation center to propagate the NSNotification properly, so I gave up on this. @m0zrat Then, I read some documentation on basic event handling that I would recommend anyone who comes across this thread to read: basic event handling in objective-c key events in objective-c Finally, working code here --> First define a .hpp header, call it something like ViewAdapter.hpp namespace Explorer { class ViewAdapter { public: virtual MTK::View* get(CGRect frame); virtual void printDebug(); }; }; // namespace Explorer Then, declare an objective-c interface for your objcpp code. /** * 1) ViewAdapter -> ViewExtender (C++ -> Obj-C) * 2) ViewAdapter -> returns extension of MTK::View. (Obj-C -> C++) **/ @interface ViewExtender : MTKView { } + (void)load:(CGRect)frame; + (ViewExtender *)get; - (void)printDebug; @end Now it's time to implement both in the same file. You can mix objective-c code with c++ code by using the .mm extension. So your file should be called something like ViewExtender.mm ViewExtender *adapter; MTK::View *Explorer::ViewAdapter::get(CGRect frame) { [ViewExtender load: frame]; return (__bridge MTK::View *)[ViewExtender get]; } void Explorer::ViewAdapter::printDebug() { ViewExtender *ref = [ViewExtender get]; [ref printDebug]; } @implementation ViewExtender + (void)load:(CGRect)frame { NSLog(@"Loading Objective-c ViewAdapter ..."); NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; adapter = [[self alloc] initWithFrame:frame]; [adapter init]; [pool release]; } + (ViewExtender *)get { return adapter; } - (id)init { BOOL isFirstResponder = [self becomeFirstResponder]; NSLog(@"Is first responder: %@", isFirstResponder ? @"Yes" : @"No"); return self; } // Needs to be overwritten to accept keyboard events. // Learn more here: // https://developer.apple.com/library/archive/documentation/Cocoa/Conceptual/EventOverview/EventHandlingBasics/EventHandlingBasics.html#//apple_ref/doc/uid/10000060i-CH5 - (BOOL)acceptsFirstResponder { return YES; } - (void)keyDown:(NSEvent *)event { NSLog(@"Detected a key down event."); [event type]; } - (void)printDebug { NSLog(@"ViewAdapter debug info ..."); } @end This on its own will return to you a MTK::View* object, you still need to set the device by using MTK::View::setDevice(...). But now once your view is open, you should see once you type, a logging message should appear: 'Detected a key down event.' Let me know if this works for you!
Topic: Graphics & Games SubTopic: General Tags:
Dec ’23
Reply to Input with Metal-CPP, overriding MTK::View
@Hipreme @Graphics and Games Engineer @m0zrat I have some actual working code -- and wanted to share. I extended MTK::View with some Objective-c code (objcpp), and wrote an adapter. @Graphics and Games Engineer -- I first tried to use NSNotificationCenter to get a Notification of a GCKeyboard connection, and by assigning an objective-c block to keyChangedHandler; for whatever the reason, I could not get the notifcation center to propagate the NSNotification properly, so I gave up on this. @m0zrat Then, I read some documentation on basic event handling that I would recommend anyone who comes across this thread to read: basic event handling in objective-c key events in objective-c Finally, working code here --> First define a .hpp header, call it something like ViewAdapter.hpp namespace Explorer { class ViewAdapter { public: virtual MTK::View* get(CGRect frame); virtual void printDebug(); }; }; // namespace Explorer Then, declare an objective-c interface for your objcpp code. /** * 1) ViewAdapter -> ViewExtender (C++ -> Obj-C) * 2) ViewAdapter -> returns extension of MTK::View. (Obj-C -> C++) **/ @interface ViewExtender : MTKView { } + (void)load:(CGRect)frame; + (ViewExtender *)get; - (void)printDebug; @end Now it's time to implement both in the same file. You can mix objective-c code with c++ code by using the .mm extension. So your file should be called something like ViewExtender.mm ViewExtender *adapter; MTK::View *Explorer::ViewAdapter::get(CGRect frame) { [ViewExtender load: frame]; return (__bridge MTK::View *)[ViewExtender get]; } void Explorer::ViewAdapter::printDebug() { ViewExtender *ref = [ViewExtender get]; [ref printDebug]; } @implementation ViewExtender + (void)load:(CGRect)frame { NSLog(@"Loading Objective-c ViewAdapter ..."); NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; adapter = [[self alloc] initWithFrame:frame]; [adapter init]; [pool release]; } + (ViewExtender *)get { return adapter; } - (id)init { BOOL isFirstResponder = [self becomeFirstResponder]; NSLog(@"Is first responder: %@", isFirstResponder ? @"Yes" : @"No"); return self; } // Needs to be overwritten to accept keyboard events. // Learn more here: // https://developer.apple.com/library/archive/documentation/Cocoa/Conceptual/EventOverview/EventHandlingBasics/EventHandlingBasics.html#//apple_ref/doc/uid/10000060i-CH5 - (BOOL)acceptsFirstResponder { return YES; } - (void)keyDown:(NSEvent *)event { NSLog(@"Detected a key down event."); [event type]; } - (void)printDebug { NSLog(@"ViewAdapter debug info ..."); } @end This on its own will return to you a MTK::View* object, you still need to set the device by using MTK::View::setDevice(...). But now once your view is open, you should see once you type, a logging message should appear: 'Detected a key down event.' Let me know if this works for you!
Topic: Graphics & Games SubTopic: General Tags:
Replies
Boosts
Views
Activity
Dec ’23