Last active
August 29, 2015 14:13
-
-
Save stevetranby/b37ad3f913ed30cecf08 to your computer and use it in GitHub Desktop.
This is the relevant code for creating a menu from code within a cocos2d-x 3.x project. Here I call to create the menu at launch in AppDelegate. The STDevice platform implementation utility class is used similar to how other platform-specific code is written in the core engine.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
////////////////////////////////////////////////////////// | |
// STDevice.h | |
class STDevice { | |
public: | |
// implementation of getInstance() is left to platform impl file | |
static STDevice* getInstance(); | |
}; | |
////////////////////////////////////////////////////////// | |
// AppDelegate.cpp | |
auto glviewImpl = dynamic_cast<GLViewImpl*>(glview); | |
if(glviewImpl) { | |
// here getInstance creates implementation specific | |
auto device = STDevice::getInstance(); | |
device->addMainMenu(glviewImpl); | |
} | |
////////////////////////////////////////////////////////// | |
// STDeviceMac.h | |
@interface STDeviceImplMac : NSObject | |
{ | |
void* _macDelegate; | |
} | |
@property(nonatomic, assign) void* macDelegate; | |
- (id)initWithDict:(NSDictionary*)dict delegate:(void*)pDelegate; | |
- (IBAction)test:(id)sender; | |
@end | |
class CC_DLL STDeviceMac : public STDevice { | |
public: | |
virtual void addMainMenu(cocos2d::GLViewImpl* glview); | |
protected: | |
// This is used since I'm not sure if or how I can creates | |
// a target/action in the c++ class | |
STDeviceImplMac* _deviceImpl; | |
}; | |
////////////////////////////////////////////////////////// | |
// STDeviceMac.mm | |
STDevice* STDevice::getInstance() | |
{ | |
if (s_sharedInstance == nullptr) | |
{ | |
s_sharedInstance = new STDeviceMac(); | |
s_sharedInstance->init(); | |
} | |
return s_sharedInstance; | |
} | |
bool STDeviceMac::init() | |
{ | |
_deviceImpl = [[STDeviceImplMac alloc] initWithDict:nil delegate:nullptr]; | |
return true; | |
} | |
void STDeviceMac::addMainMenu(GLViewImpl* glview) { | |
NSWindow * appWindow = (NSWindow *)glfwGetCocoaWindow(glview->getWindow()); | |
if(appWindow) | |
{ | |
// make your obj-c calls here | |
NSMenu* menu = [NSApp windowsMenu]; | |
{ | |
NSMenuItem* menuItem = [[NSMenuItem alloc] initWithTitle:@"Check for Updates" | |
action:@selector(checkForUpdates:) | |
keyEquivalent:@""]; | |
[menuItem setTarget:[SUUpdater sharedUpdater]]; | |
[menu addItem:menuItem]; | |
} | |
{ | |
NSMenuItem* menuItem = [[NSMenuItem alloc] initWithTitle:@"Steve's Test" | |
action:@selector(test:) | |
keyEquivalent:@""]; | |
[menuItem setTarget:_deviceImpl]; | |
[menu addItem:menuItem]; | |
} | |
} | |
} | |
@implementation STDeviceImplMac | |
// mac delegate is used to pass back to c++ classes | |
@synthesize macDelegate = _macDelegate; | |
- (id)initWithDict:(NSDictionary*)dict delegate:(void*)pDelegate | |
{ | |
if((self = [super init])) | |
{ | |
self.macDelegate = pDelegate; | |
} | |
return self; | |
} | |
- (IBAction)test:(id)sender | |
{ | |
NSLog(@"menu clicked"); | |
// let's create a window for fun | |
NSUInteger mask = NSTitledWindowMask | NSClosableWindowMask | NSResizableWindowMask; | |
NSRect winrect = {{100,100},{600,600}}; | |
NSWindow* win = [[NSWindow alloc] initWithContentRect:winrect styleMask:mask backing:NSBackingStoreBuffered defer:NO]; | |
[NSApp addWindowsItem:win title:@"Debug Title" filename:NO]; | |
[win setTitle:@"Steve Debug"]; | |
[[NSApp mainWindow] addChildWindow:win ordered:NSWindowAbove]; | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment