I've been porting my cocos2D iOS game to Mac and it works without problems but I don't understand why I get two windows open every time I launch the app.
One of them is the cocos2d window with the main menu scene and the properties and name I give but there is another blank white window with the app name (I mean the Xcode project name). I suppose this is a trivial issue but I really can't avoid that window from appearing.
What am I doing wrong?
This is my AppDelegate window initialization:
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification{
CCDirectorMac *director = (CCDirectorMac*) [CCDirector sharedDirector];
//Posiciona ventana y define escalado
NSRect aFrame=[[NSScreen mainScreen] frame];
CGSize winSize = CGSizeMake(1024,768);
CC_DIRECTOR_INIT(winSize);
[self.window showsResizeIndicator];
[director setResizeMode:kCCDirectorResize_AutoScale];
[director setProjection:kCCDirectorProjection2D];
[window_ setContentAspectRatio:NSMakeSize(winSize.width,winSize.height)];
[window_ setStyleMask:[window_ styleMask] | NSResizableWindowMask | NSMiniaturizableWindowMask];
[window_ setTitle:#"Barman Hero"];
aFrame=[[NSScreen mainScreen] frame];
if (aFrame.size.width<=winSize.width || aFrame.size.height<=winSize.height) [window_ zoom:self];
[window_ center];
[glView_ setFrameSize:NSMakeSize(window_.frame.size.width,window_.frame.size.height-22)];
// Enable "moving" mouse event. Default no.
[window_ setAcceptsMouseMovedEvents:NO];
.....
.....
.....
//Carga escena principal
[[CCDirectorMac sharedDirector] runWithScene:[MainMenu scene]];
}
Thanks in advance.
It is likely that you have a Window defined in MainMenu.xib that is being opened semi-automatically. Remove the window from MainMenu.xib and any code that might reference it and it should no longer open the second window.
Related
I use window.getSystemHandle() in SFML and convert it to NSWindow *, the following code is used to make the window transparent:
NSWindow *cocoa_window = (NSWindow*)window.getSystemHandle() ;
GLint opaque = 0;
[cocoa_window setStyleMask:NSWindowStyleMaskBorderless];
[cocoa_window setLevel: NSFloatingWindowLevel];
[[[cocoa_window contentView] openGLContext] setValues:&opaque forParameter:NSOpenGLContextParameterSurfaceOpacity];
[cocoa_window setBackgroundColor:[NSColor clearColor]];
[cocoa_window setOpaque:NO];
And I clean the window by:
window.clear(sf::Color::Transparent);
However, I can't click through the transparent part ( I want to receive mouse events when the user click on the opaque part) of the window. What can I do?
Should I replace the line [[[cocoa_window contentView] openGLContext] ...; with something else? Or do something on SFML part instead?
In a Mac app how can I open a NSWindow on a specific NSScreen (let's say the second screen)?
This is how I show the window, but it only shows on the main screen
self.windowController = NSStoryboard(name: "Main", bundle: nil).instantiateControllerWithIdentifier("mainWindow") as! NSWindowController
let window = self.windowController.window!
window.makeKeyAndOrderFront(self)
Answers in both Swift and OC are welcome.
Use the class function 'screens' to get an array of all the screens you have. From the array, pick the screen you want your window to appear on. Use the co-ordinates on that window (which are relative to the main window) to make a rect for your new window like this;
[self.window setFrame:CGRectMake(pos.x, pos.y, [mywindow frame].size.width
, [mywindow frame].size.height) display:YES];
where pos is computed from the array of screens and your selectoin.
I would like to display an NSWindow created in a storyboard full screen on a secondary monitor. The below code results in the window being displayed on the primary screen/main screen. The Y origin is ok but the X origin is 0 where it should be -1680. The code below worked before Yosemite.
NSScreen *screen = [[NSScreen screens] objectAtIndex:2];
NSRect mainDisplayRect = [screen frame];
[PresenterWindow setFrame: mainDisplayRect display:YES animate:YES];
[PresenterWindow makeKeyAndOrderFront:sender];
[PresenterWindow setLevel: CGShieldingWindowLevel()];
I had also tried the following with the same result:
[[NSWindow alloc] initWithContentRect:NSMakeRect(0, 0, [screen frame].size.width, [screen frame].size.height)
styleMask:NSBorderlessWindowMask
backing:NSBackingStoreBuffered
defer:NO
screen:screen];
Please advise how I can fix this.
I've tried solutions from some other questions:
Cocoa Open a fullscreen window on the second screen maintaining the app visible on the first - did not work - same result.
Display os x window full screen on secondary monitor using Cocoa - nope - neither with enterFullScreenMode:withOptions: method of NSView.
I have some new observations:
If i activate the "Launch At Startup" in the storyboard for my NSWindow the window is viewed fullscreen on my secondary monitor. If i then do a orderOut and run the above code the window is viewed in correct size but on my primary/main screen. The window is not activated to "Release When Close". So when I deactivate "Launch At Startup" the window is again viewed in the primary/main screen with the size of the secondary screen.
After some days I got it to work:
The NSWindow in the storyboard was marked with "Launch At Startup"
In the applcationDidFinishLaunching i wrote the following:
[PresenterWindow setLevel: NSNormalWindowLevel];
[PresenterWindow orderOut:self];
To open full screen I used this:
NSScreen *screen = [[NSScreen screens] objectAtIndex:1];
NSRect mainDisplayRect = [screen frame];
[PresenterWindow setFrame: mainDisplayRect display:YES animate:YES];
[PresenterWindow setLevel: CGShieldingWindowLevel()];
[PresenterWindow makeKeyAndOrderFront:screen];
To close full screen and to be able to open full screen again correctly:
[PresenterWindow setLevel: NSNormalWindowLevel];
[PresenterWindow orderOut:(sender)];
Following iOS 8 code is called every second:
- (void)appendString(NSString *)newString toTextView:(UITextView *)textView {
textView.scrollEnabled = NO;
textView.text = [NSString stringWithFormat:#"%#%#%#", textView.text, newString, #"\n"];
textView.scrollEnabled = YES;
[textView scrollRangeToVisible:NSMakeRange(textView.text.length, 0)];
}
The goal is to have the same scrolling down behaviour as the XCode console when the text starts running off the bottom. Unfortunately, setText causes the view to reset to the top before I can scroll down again with scrollRangeToVisible.
This was solved in iOS7 with the above code and it worked, but after upgrading last week to iOS8, that solution no longer seems to work anymore.
I can't figure out how to get this going fluently without the jumping behaviour?
I meet this problem too. You can try this.
textView.layoutManager.allowsNonContiguousLayout = NO;
refrence:http://hayatomo.com/2014/09/26/1307
The following two solutions don't work for me on iOS 8.0.
textView.scrollEnabled = NO;
[textView.setText: text];
textView.scrollEnabled = YES;
and
CGPoint offset = textView.contentOffset;
[textView.setText: text];
[textView setContentOffset:offset];
I setup a delegate to the textview to monitor the scroll event, and noticed that after my operation to restore the offset, the offset is reset to 0 again. So I instead use the main operation queue to make sure my restore operation happens after the "reset to 0" option.
Here's my solution that works for iOS 8.0.
CGPoint offset = self.textView.contentOffset;
self.textView.attributedText = replace;
[[NSOperationQueue mainQueue] addOperationWithBlock: ^{
[self.textView setContentOffset: offset];
}];
Try just to add text to UITextView (without scrollRangeToVisible/scrollEnabled). It seams that hack with scroll enabled/disabled is no more needed in iOS8 SDK. UITextView scrolls automatically.
I'm trying to figure out how to get the frame of all visible windows.
I tried the following code, but it only works for the app itself other windows report {0,0,0,0}
NSArray *windowArray = [NSWindow windowNumbersWithOptions:NSWindowNumberListAllApplications | NSWindowNumberListAllSpaces];
for(NSNumber *number in windowArray){
NSLog(#"Window number: %#", number);
NSWindow *window = [[NSApplication sharedApplication] windowWithWindowNumber:[number intValue]];
NSLog(#"Window: %#", NSStringFromRect( [[window contentView] frame]));
}
Sample code is appreciated.
I figured it out:
NSMutableArray *windows = (__bridge NSMutableArray *)CGWindowListCopyWindowInfo(kCGWindowListOptionOnScreenOnly | kCGWindowListExcludeDesktopElements, kCGNullWindowID);
for (NSDictionary *window in windows) {
NSString *name = [window objectForKey:#"kCGWindowName" ];
CGRect bounds;
CGRectMakeWithDictionaryRepresentation((CFDictionaryRef)[window objectForKey:#"kCGWindowBounds"], &bounds);
NSLog(#"%#: %#",name,NSStringFromRect(bounds));
}
You can't create an NSWindow for a window of another application. In general, you can't access the objects of other applications except through an interface that they cooperate with, like scripting.
You can get what you're looking for using the Quartz Window Services (a.k.a. CGWindowList) API.
I'm not at all sure that the window numbers returned by Cocoa are the same as the window numbers used by that API. In fact, the docs for -[NSWindow windowNumber] specifically say "note that this isn’t the same as the global window number assigned by the window server". I'm note sure to what use you can put the window numbers returned by +[NSWindow windowNumbersWithOptions:] which are not for your application's windows.