home *** CD-ROM | disk | FTP | other *** search
/ Education Sampler 1992 [NeXTSTEP] / Education_1992_Sampler.iso / Programming / Source / tess / tess-1.0 / ImageCompositor.m < prev    next >
Encoding:
Text File  |  1992-06-30  |  6.3 KB  |  274 lines

  1. /*
  2.  *    ImageCompositor -- Randy Nelson
  3.  *    An general class that composites an image at the current mouse location
  4.  *
  5.  *    You may freely copy, distribute and reuse the code in this example.
  6.  *    NeXT disclaims any warranty of any kind, expressed or implied, as to
  7.  *    its fitness for any particular use.
  8.  */
  9.  
  10.  
  11. #import "ImageCompositor.h"
  12. #import <appkit/Application.h>
  13. #import <appkit/Window.h>
  14. #import <appkit/PrintInfo.h>
  15. #import <appkit/NXImage.h>
  16. #import <appkit/NXColorPanel.h>
  17. #import <appkit/NXColorWell.h>
  18. #import <appkit/Button.h>
  19. #import <appkit/Slider.h>
  20. #import <dpsclient/psops.h>
  21. #import <dpsclient/wraps.h>
  22. #import <appkit/publicWraps.h>
  23.  
  24. #define SQUARE 1
  25. #define CIRCLE 2
  26. #define BRUSHHEIGHT 48.0
  27. #define BRUSHWIDTH 48.0
  28. #define BRUSHNAME "theBrush"
  29.  
  30. @implementation ImageCompositor
  31.  
  32.  
  33. - initFrame:(const NXRect *)frameRect
  34. {
  35.     [super initFrame:frameRect];
  36.     
  37.     /* off-screen buffer */
  38.     screenImage = [[NXImage alloc] initSize:&bounds.size];
  39.     
  40.     /* undo buffer */
  41.     undoImage = [[NXImage alloc] initSize:&bounds.size];
  42.     
  43.     /* brush */
  44.     currentBrushSize.height = BRUSHHEIGHT;
  45.     currentBrushSize.width = BRUSHWIDTH;
  46.     brushImage = [[NXImage alloc] initSize:¤tBrushSize];
  47.     
  48.     /* we do alot of focus pocus */
  49.     [self allocateGState];
  50.     
  51.     return self;
  52. }
  53.  
  54. - selfInit:sender
  55. {
  56.     NXRect aRect;
  57.     
  58.     /* set up for horizontal printing */
  59.     [[[[[NXApp printInfo] setOrientation:NX_LANDSCAPE andAdjust:YES]
  60.         setHorizCentered:YES]
  61.             setVertCentered:YES]
  62.                 setMarginLeft:0.0
  63.                 right:0.0
  64.                 top:0.0
  65.                 bottom:0.0];
  66.     
  67.     /* set up initial brush state */
  68.     [brushImage setName:BRUSHNAME];
  69.     [brushSize setMinValue:1.0];
  70.     [brushSize setMaxValue:100.0];
  71.     [brushSize setFloatValue:25.0];
  72.     [brushColor setColor:NX_COLORBLACK];
  73.     [self drawBrushImage:self];
  74.     
  75.     /* set up window resizing limits */
  76.     [window getFrame:&aRect];
  77.     windowMax.width = aRect.size.width;
  78.     windowMax.height = aRect.size.height;
  79.     windowMin.width = windowMax.width/3;
  80.     windowMin.height = windowMax.height/3;
  81.     
  82.     /* show the windows */
  83.     [self erase:sender];
  84.     [window makeKeyAndOrderFront:self];
  85.     [[brushDisplay window] orderFront:self];
  86.     [self colorPanel:self];
  87.     return self;
  88. }
  89.  
  90. - drawSelf:(const NXRect *)r :(int)c
  91. {
  92.     /* load the appropriate portion of the off-screen buffer
  93.      * into the visible portion of the view
  94.      */
  95.     [screenImage composite:NX_COPY fromRect:r toPoint:&(r->origin)];
  96.     return self;
  97. }
  98.  
  99. - mouseDownAction:(NXPoint *)currentLocation
  100. {    
  101.     NXPoint zero = {0.0, 0.0};
  102.  
  103.     /* load the old off-screen buffer into the undo buffer */
  104.     if ([undoImage lockFocus]) {
  105.         [screenImage composite:NX_COPY toPoint:&zero];
  106.         [undoImage unlockFocus];
  107.     }
  108.     
  109.     /* center on the brush */
  110.     currentLocation->x  -= currentBrushCenter.x;
  111.     currentLocation->y  -= currentBrushCenter.y;
  112.  
  113.     /* draw the current brush into the view */
  114.     [self lockFocus];
  115.     [brushImage composite:NX_SOVER toPoint:currentLocation];
  116.     [self unlockFocus];
  117.     [window flushWindow];
  118.     
  119.     /* draw the current brush into the off-screen buffer */
  120.     if ([screenImage lockFocus]) {
  121.     [brushImage composite:NX_SOVER toPoint:currentLocation];
  122.     [screenImage unlockFocus];
  123.     }
  124.     
  125.     return self;
  126. }
  127.  
  128. - mouseDraggedAction:(NXPoint *)currentLocation
  129. {
  130.     /* center on the brush */
  131.     currentLocation->x  -= currentBrushCenter.x;
  132.     currentLocation->y  -= currentBrushCenter.y;
  133.  
  134.     /* draw the current brush into the view */
  135.     [self lockFocus];    
  136.     [brushImage composite:NX_SOVER toPoint:currentLocation];
  137.     [self unlockFocus];
  138.     [window flushWindow];
  139.  
  140.     /* draw the current brush into the off-screen buffer */
  141.     if ([screenImage lockFocus]) {
  142.     [brushImage composite:NX_SOVER toPoint:currentLocation];
  143.     [screenImage unlockFocus];
  144.     }
  145.     
  146.     return self;
  147. }
  148.  
  149. - mouseUpAction:(NXPoint *)currentLocation
  150. {
  151.     return self;
  152. }
  153.  
  154. - drawBrushImage:sender
  155. {
  156.     /* read all the brush parameters from interface */
  157.     currentBrushColor = [brushColor color];
  158.     currentBrushShape = [brushShape selectedTag];
  159.     currentBrushSize.height = currentBrushSize.width = [brushSize floatValue];
  160.     currentBrushCenter.x = (currentBrushSize.width/2);
  161.     currentBrushCenter.y = (currentBrushSize.height/2);
  162.     
  163.     /* draw the current brush */
  164.     [brushImage setSize:¤tBrushSize];
  165.     if ([brushImage lockFocus]) {
  166.     PScompositerect(0.0, 0.0, currentBrushSize.height,
  167.             currentBrushSize.width, NX_CLEAR);
  168.     NXSetColor(currentBrushColor);
  169.     switch (currentBrushShape) {
  170.         case SQUARE:{
  171.             PSrectfill(0.0, 0.0, currentBrushSize.height,
  172.                    currentBrushSize.width);
  173.             break;
  174.         }
  175.         case CIRCLE:{
  176.             PSarc(currentBrushCenter.x, currentBrushCenter.y,
  177.               currentBrushCenter.x, 0.0, 360.0);
  178.             break;
  179.         }
  180.         default:{
  181.             break;
  182.         }
  183.     }
  184.     PSfill();
  185.     [brushImage unlockFocus];
  186.     }
  187.     
  188.     [brushDisplay setIcon:BRUSHNAME];
  189.     return self;
  190. }
  191.  
  192. - undo:sender
  193. {
  194.     NXPoint zero = {0.0, 0.0};
  195.  
  196.     /* load the undo buffer into the off-screen buffer and display */
  197.     if ([screenImage lockFocus]) {
  198.     [undoImage composite:NX_COPY toPoint:&zero];
  199.     [screenImage unlockFocus];
  200.     }
  201.     
  202.     [self display];
  203.     return self;
  204. }
  205.  
  206. - erase:sender
  207. {
  208.     /* leave the undo buffer alone */
  209.     if ([screenImage lockFocus]) {
  210.     NXEraseRect(&bounds);
  211.     [screenImage unlockFocus];
  212.     }
  213.     
  214.     [self display];
  215.     return self;    
  216. }
  217.  
  218. - (BOOL)acceptsFirstMouse
  219. {
  220.     return YES;
  221. }
  222.  
  223. -appDidInit:sender
  224. {
  225.     [self selfInit:self];
  226.     return self;
  227. }
  228.  
  229. - windowWillResize:sender toSize:(NXSize *)newFrame
  230. {
  231.     if(newFrame->width > windowMax.width) {
  232.     newFrame->width = windowMax.width;
  233.     }
  234.     else if(newFrame->width < windowMin.width){
  235.     newFrame->width = windowMin.width;
  236.     }
  237.     if(newFrame->height > windowMax.height){
  238.     newFrame->height = windowMax.height;
  239.     }
  240.     else if(newFrame->height < windowMin.height){
  241.     newFrame->height = windowMin.height;
  242.     }
  243.     return self;
  244. }
  245.  
  246. - infoPanel:sender
  247. {
  248.     if (infoPanel == nil) {
  249.     [NXApp loadNibSection:"Info.nib" owner:self];
  250.     }
  251.     [infoPanel orderFront:sender];
  252.     return self;
  253. }
  254.  
  255. - helpPanel:sender
  256. {
  257.     if(helpPanel == nil){
  258.     [NXApp loadNibSection:"Help.nib" owner:self];
  259.     }
  260.     [helpPanel orderFront:sender];
  261.     return self;
  262. }
  263.  
  264. - colorPanel:sender
  265. {
  266.     [[[[NXColorPanel new]
  267.         setContinuous:YES]
  268.             moveTo:494.0 :54.0]
  269.                 orderFront:nil];
  270.     return self;
  271. }
  272.  
  273. @end
  274.