Cocos2d-x OS X application menus - xcode

I have a cocos2d-x application that I have been mostly debugging on iOS. Now I am working on the OS X side of things.
As expected, everything works pretty much the same, except that I have some gesture commands in my iOS app that do not translate to OS X.
I would like to create menu items (OS X level) and associated shortcuts to accomplish some of the gesture interactions (at least as one option).
Creating a MainMenu.xib and linking it in the project settings doesn't seem to be the whole story. The menus are still the generic "AppName" and Window menus, and not the ones I created in the xib file.
I believe I need to wire it up to the AppDelegate somehow, but I'm not sure how, and I haven't had any success finding documentation to that effect, at least in relation to how it interplays with Cocos2d-x.

Essentially you just need to create the menu in an Objective-C++ class that you call from AppDelegate.cpp or wherever. The extension will be .mm in order to invoke the objective-c compiler/runtime. You can then create the NSMenu and add items or replace an entire menu set[ServiceMenu/WindowMenu/MainMenu]
NSMenu reference: https://developer.apple.com/library/mac/documentation/Cocoa/Reference/ApplicationKit/Classes/NSMenu_Class/index.html)
I found the only interesting part was receiving an action from the menu where I believe you need to then also create an objective-c class as the receiver (target).
You should be able to load the .xib file in as a entirely new menu to replace the one GLFW3 creates, but I haven't looked into that yet.
Update: Here's an example project.
https://github.com/stevetranby/TestNSMenuCocos2Dx
Update: Added example to project for loading the menu with .xib

Related

Where is the "Use Storyboards" button in Xcode 8.3?

In older versions, it was a "Use Storyboards" button when creating a Cocoa app that determines whether to create a storyboard file or a xib one, but now I can't find it.
Because it isn't there. It is superfluous. Just make your Cocoa project and get started. If you then want to make a xib, make one. If you want to make another storyboard, make one.
If you want to write an application that uses no storyboards at all but uses a main nib as in the old days, then make an Application xib file or a MainMenu xib file and point the target's Main Interface setting at it, and delete the default storyboard. (I just wrote an application that works that way, and it was no trouble at all. Just watch out because a modern xib has "Prefer coder" turned on by default, which may come as a surprise to an old AppKit fogie like myself.)

Does the Yosemite (OSX 10.10) feature of xcode live views work with NSView on OSX?

I'm trying to determine if the live view feature works with NSView on OSX 10.10, or is it only iOS at the moment?
It works great with UIView for iOS projects, however I haven't been able to get OSX projects to work.
Yes it does work. You just need to add the same symbols for IBDesignable (before the class interface declaration) and IBInspectable (before the property you want to edit in IB) in the right places in you view class header and be sure your nib or storyboard has the class set properly in tge inspector in IB.
You also need to note that the must be properties and they are somewhat limited in type.
Sometimes IB is still a little flaky about recognizing it right away, so you might need to build or reopen the tab with that nib or storyboard.
Also I would expect your mileage may vary with subclasses of more complex views and remember this only works for objects that are descendants of NSView at some point. No NSCell descendants.

Xcode 4.3.2 - missing iOS controls

I think I'm missing some controls in the tool window in Xcode 4.3.2.
I created a new iOS 5 project, and my list of controls looks like this:
I noticed that I got more controls when I opened a sample project downloaded from the Apple dev site.
Then I tried to open my own project, which started out showing a code file. Then I got a huge list. But as soon as I view the storyboard, or a Xib file it reverts back to the shorter list.
In the huge list of controls, some of them doesn't look iOS-ish. I suspect that it loads the whole lot Xcode has to offer across project types (both iOS, Mac). Looks like this:
Do I need to manually load some frameworks, or similar into my project in order to get more controls?
The first screenshot shows the CocoaTouch controls, the second one shows standard Cocoa Controls. Not all of the latter ones are available on iOS/CocoaTouch. You can change the filter with the drop-down menu directly above the list ("Objects" in the first screenshot, "Object Library" in the second one)

Adding outlets to iPhone View

I am creating a custom iPhone View in MonoTouch through the interface builder in XCode (In monodevelop New File -> MonoTouch -> iPhone View).
When selecting a new iPhone View it creates an xib file, but no .h file is created, so how can I create outlets?
I usually drag an outlet from my label/button etc. to the .h file, but since it doesn't exist for this view, I don't know how to create the outlets.
Can someone point me in the right direction, since all articles on google are for the old versions, where outlets where created differently.
When you double-click a .xib in MonoDevelop, MonoDevelop will generate header files for your [Register]'d C# classes that subclass ObjC types and export them to a temporary Xcode project, where you can use Xcode to drag&drop Outlets and Actions.
Adding a new iPhone View file (.xib) does not auto-create any backing C# classes for you, it just creates the .xib, therefore MonoDevelop does not autogenerate any header files for you when you double-click the .xib.
When Xcode launches, you can manually create some Objective-C headers for this .xib and drag&drop outlets or actions to it (or you can create C# classes in MonoDevelop before double-clicking the .xib). When you switch back to MonoDevelop, MonoDevelop will "import" the header files, translating them into the equivalent C#.
While in Xcode, you can also add .xib's there and MonoDevelop will import those as well.
The Xamarin documentation has a good tutorial on this. There is a section for Adding Outlets and Actions to the UI using Interface Builder in Xcode4
Adding an Outlet In order to create the Outlet, use the following
procedure:
Determine for which control you want an Outlet.
Hold down the Control key on the keyboard, and then drag from the control to an empty space in your code file after the #interface
definition.

High-Level App Design/Architecture

I've done a fair amount of iOS development in the past couple of years, so I'm pretty familiar with iOS architecture and app design (everything's a ViewController that you either push, pop, or stick into tab bars). I've recently started exploring proper Mac app development and feel a little lost. I'd like to really just have a sanity check and maybe some advice as to what the proper way to build an app like this is:
I'd like to build a library-style, single window app, that will spawn additional windows during its operation, but not as full-blown documents. The main window will be laid out much like OS X Lion's Mail.app, with a three-wide split view containing:
A source list, or high-level topic selection
A list view of items pertaining to the topic selected in the first pane
A detail view, which shows the details of the object selected in the middle pane
Like I said, really similar to Mail.app as far as looks go.
My question is really how to glue all this together from inside XCode. Here's where my confusion lies so far:
The default project generated a NIB with a main menu and window. I like to encapsulate functionality, so should I make a window controller for this window and somehow hook it up in Interface Builder, or does window-specific functionality belong somewhere else?
If possible, I'd like each of my three panes to be separate view controllers. I created three NSViewController subclasses (XCode automatically generated NIBs), and added (to the main menu/window NIB) view controller objects with each class specified, hooking up each one's view property to one of the three Custom View generic NSView objects I dropped into the NSSplitView. When I tried to set each view controller's NIB, only the main menu/window NIB appeared in the drop-down, and typing the desired one by hand seemed to have no effect (the view's contents didn't actually appear when running the app). This makes me think I'm doing something wrong.
I'm a little fuzzy on what types of views I should use for each of the first two panes. I'll obviously build a custom one for the final pane, but it seems like the first two should be present in the Cocoa framework already.
Anyway, if I'm doing completely the wrong thing, don't bother addressing my questions; just tell me what I should be doing instead. I think I just need a proper Mac developer to point me in the right direction.
With regard to your first question, you don't need to use the main window that Apple supplies in MainMenu.xib. If you want, you are free to delete that window from the nib and then instantiate an NSWindowController in your applicationDidFinishLaunching: delegate method which then loads and controls the main window.
You are definitely confused about NSViewController, which is not really all that surprising, since you might assume that it works like UIViewController.
In fact, NSViewController is completely different to UIViewController and does not have the same level of Interface Builder support. You can't place a view controller in a window in IB, for example, whereas this is standard practice on iOS. NSViewController is a relatively new class on the Mac and generally you use it to load views programmatically and manage the view content.
The class that most closely maps to UIViewController on the Mac is NSWindowController. This has been around a lot longer than NSViewController and in fact many Mac apps don't use NSViewController at all.
Generally, each window in your app should have a window controller managing it. You can use subclasses of NSWindowController to handle a lot of the functionality for each window.
If you want to use NSViewController, then you should use your window controller to manage those view controller objects. This is generally done programmatically due to the aforesaid lack of Interface Builder support. Each NSViewController instance loads its view from a specific nib file. You generally don't add view controllers in Interface Builder.
For your source list you would generally use an NSOutlineView if you have multiple sections or an NSTableView. These two objects are used whenever you need a list of items. NSOutlineView is hierarchical, whereas NSTableView is flat.
I hope this helps.

Resources