home *** CD-ROM | disk | FTP | other *** search
/ Nebula 1995 August / NEBULA.mdf / SourceCode / OOP_Course / Examples / DragDemo_1.1 / DemoDiagram.m < prev    next >
Encoding:
Text File  |  1993-01-18  |  3.0 KB  |  126 lines

  1. #import "DemoDiagram.h"
  2. #import "DiagramCell.h"
  3.  
  4. @implementation DemoDiagram
  5.  
  6. - initFrame:(NXRect *)r
  7. {
  8.     [super initFrame:r];
  9.     
  10.     /* Compute view limits for later use. */
  11.     minX=bounds.origin.x;
  12.     maxX=minX+bounds.size.width-1.0;
  13.     minY=bounds.origin.y;
  14.     maxY=minY+bounds.size.height-1.0;
  15.     
  16.     itemList=[[List alloc] initCount:10];  //List of graphic items in the diagram
  17.     [self setClipping:NO];            //Faster, and we'll be careful
  18.     return self;
  19. }
  20.  
  21. - setBackground:(NXImage *)anImage
  22. {
  23.     bgImage=anImage;
  24.     return self;
  25. }
  26.  
  27. /* Drawing the view consists simply of compositing the background, then compositing
  28. each of the graphic items. */
  29.  
  30. - drawSelf:(NXRect *)rect :(int)c
  31. {
  32.     int i;
  33.     [bgImage composite:NX_COPY toPoint:&bounds.origin];
  34.     for (i=0; i<[itemList count]; i++){
  35.         [[itemList objectAt:i] drawSelf];
  36.     }
  37.     return self;
  38. }
  39.  
  40. - addItem:item
  41. {
  42.     [itemList addObject:item];
  43.     [self display];
  44.     return self;
  45. }
  46.  
  47. /* When the mouse goes down, search the item list, from top to bottom, for the first
  48. object answering self to the mouseHit request.  That becomes the moused object. */
  49.  
  50. - mouseDownAction:(NXPoint *)location
  51. {
  52.     id item;
  53.     int i;
  54.     mousedObject=nil;
  55.     
  56.     for (i=[itemList count]-1; i>=0; i--){
  57.         item=[itemList objectAt:i];
  58.         if([item mouseHit:location]){
  59.             mousedObject=item;
  60.             oldMouse=*location;        //save first mouse location
  61.             break;
  62.         }
  63.     }
  64.     [self lockFocus];            //prepare to draw while dragging
  65.     return self;
  66. }
  67.  
  68. /* When dragging, ask each item to draw its intersection with the dirty rectangle. */
  69.  
  70. - mouseDraggedAction:(NXPoint *)newMouse
  71. {
  72.     int i;
  73.     NXPoint mouseLoc;        //location of the mouse cursor;
  74.     float dx,dy;            //relative x and y changes;
  75.     NXRect dirtyRect,newRect;
  76.     
  77.     if(!mousedObject)return self;    // If no mousehit in mouseDown
  78.     
  79.     /* Save rectangle before moving as initial dirty rect */
  80.     [mousedObject getFrame:&dirtyRect];
  81.  
  82.     /* This is the new location of the mouse */
  83.     mouseLoc=*newMouse;
  84.     
  85.     /* First, constrain mouse point to appear to be in the view */
  86.     if(mouseLoc.x > maxX)mouseLoc.x = maxX;
  87.     if(mouseLoc.x < minX)mouseLoc.x = minX;
  88.     if(mouseLoc.y > maxY)mouseLoc.y = maxY;
  89.     if(mouseLoc.y < minY)mouseLoc.y = minY;
  90.     
  91.     /* Now, find effective change in x and y */
  92.     dx=mouseLoc.x-oldMouse.x;
  93.     dy=mouseLoc.y-oldMouse.y;
  94.         
  95.     /* Save mouse location for next time around */
  96.     oldMouse=mouseLoc;
  97.     
  98.     /* Finally, ask item to move as far as it can within the bounds of the view. */
  99.     [mousedObject moveBy:dx :dy inViewRect:&bounds];
  100.     
  101.     /* Find out where it ended up */
  102.     [mousedObject getFrame:&newRect];
  103.     
  104.     /* Compute dirty rectangle that needs redrawing */
  105.     NXUnionRect(&newRect,&dirtyRect);  //replaces second rect with union
  106.     
  107.     /* Now time to draw in the view */
  108.     
  109.         /* Erase dirty rectangle in the view*/
  110.         [bgImage composite:NX_COPY fromRect:&dirtyRect toPoint:&dirtyRect.origin];
  111.     
  112.         /* For each object, redraw the portion that intersects the dirty rect. */
  113.         for (i=0; i<[itemList count]; i++)
  114.             [[itemList objectAt:i] drawInDirtyRect:&dirtyRect];
  115.  
  116.     [window flushWindow];
  117.     return self;
  118. }
  119.  
  120. - mouseUpAction:(NXPoint *)location
  121. {
  122.     [self unlockFocus];
  123.     return self;
  124. }
  125.  
  126. @end