home *** CD-ROM | disk | FTP | other *** search
/ Peanuts NeXT Software Archives / Peanuts-Update.iso / Rhapsody / developer / examples / LocalizationExample.5.0.m.I.bs.tz / LocalizationExample.5.0.m.I.bs / LocalizationExample.5.0.I.bs / Source / FlyingWindow.m < prev    next >
Encoding:
Text File  |  1997-11-28  |  4.3 KB  |  111 lines

  1.  
  2. // Copyright (c) 1997 by Don Yacktman
  3. // All rights reserved.
  4. // Permission to use this code in your application, whether
  5. // commercial, shareware, or freeware, is granted.
  6. // This notice may not be removed or altered.
  7.  
  8.  
  9. #import "FlyingWindow.h"
  10. #import <libc.h>
  11.  
  12. static id contentImage = nil;
  13.  
  14. @implementation FlyingWindow
  15.  
  16. - initAt:(NSPoint *)thePoint
  17. {
  18.     // set up window with image and put it on the screen centered at thePoint
  19.     NSRect theFrame;
  20.     // make sure we have the mini-envelope image loaded
  21.     if (!contentImage)
  22.         contentImage = [NSImage imageNamed:@"FlyingIcon.tiff"];
  23.     // set up our frame (where we'll be on the screen and our size)
  24.     // get the size from the image
  25.     theFrame.size = [contentImage size];
  26.     // place our selves on the screen so that our center is at the
  27.     // point passed in by the caller of this method
  28.     theFrame.origin.x = thePoint->x - NSWidth(theFrame) / 2;
  29.     theFrame.origin.y = thePoint->y - NSHeight(theFrame) / 2;
  30.     // call the superclass designate initializer to set ourself up.
  31.     [self initWithContentRect:theFrame styleMask:NSBorderlessWindowMask backing:NSBackingStoreRetained defer:NO screen:NULL];
  32.     // set up a new random velocity vector
  33.     velocity.x = (random() % HORIZ_JUMPS) - HORIZ_JUMPS / 2;
  34.     velocity.y = (random() % VERT_JUMPS) + MIN_VERT;
  35.  
  36.     // create an NSImageView and insert it into ourself so that
  37.     // the mini-envelope image will be displayed
  38.     {
  39.         NSImageView *imageView = [[NSImageView alloc] initWithFrame:[[self contentView] frame]];
  40.         [imageView setImage:contentImage];
  41.         [imageView setImageAlignment:NSImageAlignCenter]; // this is redundant since it is the default
  42.         [imageView setImageFrameStyle:NSImageFrameNone]; // this is redundant since it is the default
  43.         [imageView setImageScaling:NSScaleNone]; // make sure the image isn't resized at all
  44.         [imageView setEditable:NO]; // make sure no one can replace the image by dragging in a new one
  45.         [[self contentView] addSubview:imageView];
  46.     }
  47.     // put us on the screen immediately
  48.     [self orderFront:nil];
  49.     return self;
  50. }
  51.  
  52. - reInitAt:(NSPoint *)thePoint
  53. {    // re-init the window as above, but for already created window.
  54.     // ideally, the code above should call this method and not be a
  55.     // cut and paste job.  The problem is that some of the things
  56.     // need to be set up in varying orders between the two methods.
  57.     // So we'd have to break the initializations into seperate private
  58.     // methods and call those methods from this and the above method.
  59.     // To reduce complexity in this example, I chose to not do that,
  60.     // even though IMHO it is a better design.
  61.     // get the frame size
  62.     NSSize theSize;
  63.     theSize = [contentImage size];
  64.     // reset our position on the screen, as above, so that we are centered on thePoint
  65.     [self setFrameOrigin:NSMakePoint((thePoint->x - theSize.width / 2), (thePoint->y - theSize.height / 2))];
  66.     // set up a new random velocity vector
  67.     velocity.x = (random() % HORIZ_JUMPS) - HORIZ_JUMPS / 2;
  68.     velocity.y = (random() % VERT_JUMPS) + MIN_VERT;
  69.     // put us on the screen
  70.     [self orderFront:nil];
  71.     return self;
  72. }
  73.  
  74. - move
  75. {    // move us one animation step.
  76.     // get our window's frame
  77.     NSRect theFrame = [self frame];
  78.     // Move ourself by the amount our present velocity requires
  79.     [self setFrameOrigin:NSMakePoint((NSMinX(theFrame) + velocity.x), (NSMinY(theFrame) + velocity.y))];
  80.     // update the velocity, taking gravity into account
  81.     velocity.y -= GRAVITY;
  82.     return self;
  83. }
  84.  
  85. - (BOOL)onScreen
  86. {    // are we still on the screen, or did we move out of bounds?
  87.     NSRect theFrame = [self frame];
  88.     NSScreen *myscreen = [self screen];
  89.  
  90.     // normalize (translate) to the screen's position
  91.     theFrame.origin.x -= [myscreen frame].origin.x;
  92.     theFrame.origin.y -= [myscreen frame].origin.y;
  93.     // consider us on screen if we're off the top since we'll be
  94.     // coming back down sometime
  95.     if (!myscreen) { // no screen returned?  Use standard NeXT screen size
  96.         if ((NSMaxX(theFrame) < 0) || (NSMaxY(theFrame) < 0) ||
  97.                 (NSMinX(theFrame) > 1120.0)) return NO;
  98.         return YES;
  99.     }
  100.     // if we are off the left, right, or bottom side of the screen,
  101.     // then we are off the screen.
  102.     if ((NSMaxX(theFrame) < NSMinX(([myscreen frame]))) ||
  103.         (NSMinX(theFrame) > NSMaxX(([myscreen frame]))) ||
  104.         (NSMaxY(theFrame) < NSMinY(([myscreen frame])))) {
  105.         return NO;
  106.     }
  107.     // otherwise, we're on the screen.
  108.     return YES;
  109. }
  110.  
  111. @end