Make application origin start from top left - objective-c

I'm building a cross platform application, and on most platforms, when you have a rectangle for like say a window frame position, it normally starts from the top left.
Like for say:
Rectangle rect = new Rectangle(0, 0, 100, 100); // Starts at top left corner
but for Macos it starts on the bottom left.
Is there any way to change this?
Also, I'm looking for this to be applied to NSWindow.

There's a method in NSWindow that allow you to position the top-left corner of the window’s frame rectangle at a given point in screen coordinates:
func setFrameTopLeftPoint(_ point: NSPoint)
Or, You could just do an extra step for macOS to get the flipped y:
flippedY = screenHeight - y - windowHeight

Related

Dynamically changing size and frame position of UITextView

What I would like to do is to make a custom UITextView to allow dynamically changing size and positions of it like PicLab's text edit function.
PicLab tools article
In sum for my simple questions following
1, How to move a position of TextView at your disposal
2, How to change a size of TextView by finger slide
3, How to dynamically change size of fonts corresponding to size of TextView
That would be great if sharing me with useful links as well as codes.
to move the position of a UITextView (actually of any UI element in iOS), you can modify its frame property. the frame property defines the rectangle that this elements takes on the screen, being defined with x and y positions ( (0, 0) is the top left corner of the screen).
you can also change the size of a UITextView by modifying the frame, this time instead of modifying x and y, you'll have to modify its height and width
an example of setting a UITextField with a width of 200 and height of 50 would look like in the top left corner of the screen looks like:
CGFloat x = 0.0;
CGFloat y = 0.0;
CGFloat width = 2000.0;
CGFloat height = 50.0;
textField.frame = CGRectMake(x, y, width, height)
the changing of the font size will have to be implemented dynamically by a custom function of yours, you can use the class method of UIFont fontWithName:size for this.
Frame cannot be set for coordinates and dimension directly, you have to assign a new object. A quick search found this, Change ios Frame Size(width e height) keeping position (x,y).

Cocoa NSRect OUTSIDE of an intersection

I have two rects that intersect. They have the same dimensions, the only difference is that one of them is lower down the screen than the other. I know there is a way to get the rect of their intersection, but that's not what I want. I actually want a new rect from the area that lies outside of their intersection.
The top part of the lower view intersects with the bottom part of the top view. The new rect should not have that area. I basically want a rect with the same origin and width as the bottom view, but without the part that intersects with the top rect.
Thanks for the help.
CGRect intersectRect = CGRectIntersection(highestRect, lowestRect);
CGRect theRectYouWant = CGRectMake(0, 0, 0, 0);
if(!CGRectIsNull(intersectRect)) {
theRectYouWant =
CGRectMake(lowestRect.origin.x,
intersectRect.origin.y + intersectRect.size.height,
lowestRect.size.width,
lowestRect.size.height - intersectRect.size.height);
}
Have a look on this page for more, Elbimio ;)

Center a MacGLView in a Window

I have an cocos2D app that runs in 4/3 ratio and I want to enter fullscreen mode resizing the view to maintain the aspect ratio without showing black bars at screen sides. So, when the app enters fullscreen, I do this:
NSRect aFrame=[[NSScreen mainScreen] frame];
[glView_ setFrameSize:NSMakeSize(aFrame.size.width,aFrame.size.height*1.2)];
This way I got fullscreen wide but (as I wanted) the ratio is untouched. My problem now is that the glview is not centered on screen. It extends from left bottom point so I got my game unevenly clipped (all clipping is done at top). I've trying to use [glview setFrameOrigin] and similar to select the visible portion of the view without success.
I just want to displace my view a certain number of pixels down so it shows the center area of whole view. Is it possible?.
Thanks in advice.
You are changing the dimensions of glView_, but not its origin.
NSRect aFrame=[[NSScreen mainScreen] frame];
CGFloat x = 0.0f;
CGFloat y = aFrame.size.height;
CGFloat width = aFrame.size.width;
CGFloat height = aFrame.size.height * 1.2f;
glView_.frame = NSRectFromCGRect((CGRectMake(x, y/2 - height / 2, width, height));

NSWindow setFrame | setPosition on the screen

In my application, I need to display a multiple window of same NIB, at a time, and it will be closed/released on the timer basis,
everything is working fine, except the window position. My requirement is, it should display the window exactly below the previous window, I am able to calculate origin for the new window if any window of same NIB is present.
I am using this function to get the origin:
-(void)awakeFromNib{
NSString *eventMsg = [NSString stringWithFormat:#"%s is set “,eventName];
[pTextField setStringValue:eventMsg];
[pWindow setFrameOrigin:[self pointstoDisplayScreen]];
/* On timer callback , i will show the fadeout and fadein effects*/
[self startTimer];
}
/* This method returns the coordinate where to
draw the window
*/
-(NSPoint)pointstoDisplayScreen{
NSRect frame = [[NSScreen mainScreen] visibleFrame];
NSRect windowRect = [pWindow frame];
CGFloat yCoordinate = frame.size.height-windowRect.size.height;
NSPoint point = NSMakePoint(frame.size.width - windowRect.size.width, frame.size.height-windowRect.size.height);
/* Let there be some gap, if any window present and let it draw below to
the existing window
*/
point.y -= (windowRect.size.height + windowOffset)*noOfWindow;
NSLog([NSString stringWithFormat:#"PositionToDisplayScreen x = [%f] y = [%f]",point.x,point.y ]);
return point;
}
The problem is if if previous window is present it draws the new point, slightly below and toward right side of the existing window,
Is there any property i need set, the problem is, if the previous position is exactly on the top right, then it draw the new window on the opposite corner.
[NSWindowController setShouldCascadeWindows:NO];
By setting this, its working, Now all the notification drawing at the top right corner of the screen.
Thanks.
Your window location is "jumping" because you are not doing any bounds checking to determine that the calculated new origin is within the screen bounds.
The class NSScreen will give you details of the screen(s), for each on you cant get its frame.
Now you need to decide how you want a window origin to move; e.g. to the screen edge and no further, onto an adjacent screen if there is one, wrapping (off right means come on left), etc. The choice will depend on your application.
Armed with the screen frame(s) and the movement algorithm you can calculate your new origin.

How do I clip or change alpha of an image (pixels) in Quartz?

I'm working on making an iPhone App where there are two ImageViews and when you touch the top one, wherever you tapped, the bottom one shows instead.
Basically what I want to do is cut an ellipse/roundedrect out of an image. To do this I was thinking on either clipping the image, or changing the alpha pixels in the rect to zero. I am new to Quartz 2D Programming so I am not sure how to do this.
Assuming I have:
UIImageView *topImage;
UIImageView *bottomImage;
How do I delete a CGRect/Ellipse/RoundedRect from these images.
This is kind of like those lottery tickets that you have to scratch off to reveal if you won.
I would generally try to make a mask from a path (here containing a rounded rectangle), then masking the image with it, as demonstrated in the apple docs. The one of the benefits of this is that for hit testing all you need to do is CGPathContainsPoint with the point that was touched (as in it will test whether it was in the visible area of the image).
I tried this code:
CGContextRef ctx = UIGraphicsGetCurrentContext();
CGRect frame = CGRectMake(100, 100, 100, 100);
CGPathRef roundedRectPath = [self newPathForRoundedRect:frame radius:5];
CGContextAddPath(ctx, roundedRectPath);
CGContextClip (ctx);
CGPathRelease(roundedRectPath);
(Together with the rounded rect path function you sent)
This is on a white view and beneath the view there is a gray Window, so I thought this would just show gray instead of white in CGRect frame but it didn't do anything...