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

  1.  
  2. /* TODO:
  3.    make drawing to TessView be done to an NXimage and define new drawSelf: method
  4. */
  5.  
  6. /* Generated by Interface Builder */
  7.  
  8. #import "TessSketch.h"
  9. #import "aGroup-inter.h"
  10. #import "Underlay.h"
  11. #import "SketchPad.h"
  12.  
  13. const char *const types[3] = { "tiff","eps",NULL};
  14.  
  15. @implementation TessSketch
  16. - initFrame: (const NXRect *) theframe
  17. {
  18.   [super initFrame: theframe];
  19.   mode = TIFF_MODE;
  20.   fprintf(stderr,"TessSketch: new\n");
  21.   [self initGroups];
  22.   underlay = [ Underlay new ]; 
  23.   [self addSubview: underlay];
  24.   [underlay  initFrame: &frame];
  25.   [underlay setAutosizing: NX_WIDTHSIZABLE | NX_HEIGHTSIZABLE];
  26.   sketchView = [ SketchPad new ];
  27.   /*   [sketchView initFrame: &frame]; */
  28.   generator = [GeneratorOverlay new];
  29.   [generator init];
  30.   [generator setUnderlay: underlay];
  31.   [self addSubview: generator : NX_ABOVE relativeTo: underlay];
  32.  
  33.   [self useGroup: P1];
  34.   return self;
  35. }  
  36.  
  37. -init
  38. {
  39. #ifdef DEBUG
  40.   fprintf(stderr,"INit\n");
  41. #endif
  42.   return self;
  43. }
  44.  
  45. - currentGroup
  46. {
  47.   return currentGroup;
  48. }
  49.  
  50. - initGroups
  51. {
  52. /*   int i; */
  53.   [aGroup setup];
  54. /*  for(i=P1;i<P6M;i++)
  55.     grouplist[i] = [GroupClasses[i] new];
  56. */
  57.   grouplist[P1] = [[p1 new] init];
  58.   grouplist[P2] = [[p2 new] init ];
  59.   grouplist[PM] = [[pm new] init ];
  60.   grouplist[PG] = [[pg new] init ];
  61.   grouplist[CM] = [[cm new] init ];
  62.   grouplist[PMM] = [[pmm new] init ];
  63.   grouplist[PMG] = [[pmg new] init ];
  64.   grouplist[PGG] = [[pgg new] init ];
  65.   grouplist[CMM] = [[cmm new] init ];
  66.   grouplist[P4] = [[p4 new] init ];
  67.   grouplist[P4M] = [[p4m new] init ];
  68.   grouplist[P4G] = [[p4g new] init ];
  69.   grouplist[P3] = [[p3 new] init ];
  70.   grouplist[P3M1] = [[p3m1 new] init ];
  71.   grouplist[P31M] = [[p31m new] init ];
  72.   grouplist[P6] = [[p6 new] init ];
  73.   grouplist[P6M] = [[p6m new] init ];
  74.   return self;
  75. }
  76.  
  77. - getOutputView
  78. {
  79.   return outputView;
  80. }
  81.  
  82. - doPaste:sender
  83. {
  84.   if (mode != TIFF_MODE)
  85.     [self switchToTiffMode];
  86.   return self;
  87. }
  88.  
  89. - switchToTiffMode
  90. /* This sets up tiff mode which allows only translating
  91.    the image over the generator */
  92. {
  93.   /* set up subviews */
  94.   //[sketchView removeFromSuperview];
  95.   responder = generator;
  96. //  [self addSubview: generator : NX_ABOVE relativeTo: underlay];
  97.   [generator setupGenerator: currentGroup];
  98.   return self;
  99. }
  100.  
  101. - setupOutputView
  102. {
  103.   NXRect afrm;
  104.   id pahoehoe;
  105.  
  106.   [outputView getFrame: &afrm];
  107.   pahoehoe = [[NXImage alloc] initSize: &afrm.size];
  108.   if(![pahoehoe useCacheWithDepth: NX_DefaultDepth]) 
  109.     fprintf(stderr,"Can't create cached image\n");
  110.   if([pahoehoe lockFocus]){
  111.     [outputView display];
  112.     [pahoehoe unlockFocus];
  113.     [outputView setImage: pahoehoe];
  114.   } else {
  115.     fprintf(stderr,"Could not lock init outView image\n");
  116.   }
  117.   return self;
  118. }
  119.  
  120. - copy:sender
  121. {
  122.     return self;
  123. }
  124.  
  125. - printPSCode: sender
  126. {
  127.   [outputView printPSCode: self];
  128.   return self;
  129. }
  130.  
  131. - tesselate:sender
  132. {
  133.   id img;
  134.   NXRect fr;
  135.   if(![outputView getImage])
  136.     [self setupOutputView];
  137.   img = [outputView getImage];
  138. #ifdef DEBUG
  139.   if([img lockFocus]){
  140.     fprintf(stderr,"Locked on outImg\n");
  141.     [img unlockFocus];
  142.   } else
  143.     fprintf(stderr,"Couldn't lock on outImg\n");
  144. #endif
  145.   [outputView getFrame: &fr];
  146.   [img setSize: &(fr.size)];
  147. #ifdef DEBUG
  148.   if([img lockFocus]){
  149.     fprintf(stderr,"Locked on outImg after setsiz\n");
  150.     [img unlockFocus];
  151.   } else
  152.     fprintf(stderr,"Couldn't lock on outImg after setsiz\n");
  153. #endif
  154.   [currentGroup performTesselation: self];
  155.   [outputView display];
  156.   return self;
  157. }
  158.  
  159. - toolResponse:sender
  160. {
  161.     return self;
  162. }
  163.  
  164. - clearPad:sender
  165. {
  166.   NXRect fr;
  167.  
  168.   [generator getFrame: &fr];
  169.   [scratchPad lockFocus];
  170.   [currentGroup makeLatticeUnitAt: &(fr.origin) fromImage:  [underlay getImage]];
  171.   [scratchPad unlockFocus];
  172.  
  173.   [outputView lockFocus];
  174.   [scratchPad composite: NX_COPY toPoint: &(bounds.origin)];
  175.   [outputView unlockFocus]; 
  176.  
  177.   [outputView display];
  178.   return self;
  179. }
  180.  
  181. - setGroup:sender
  182. {
  183.   [self useGroup: [[sender selectedCell] tag]];
  184.   return self;
  185. }
  186.  
  187. - drawImage
  188. {
  189.   /* underlay locks focus and calls this method */
  190.   /* I am supposed to draw a picture here */
  191.   PSmoveto(0.0,0.0);
  192.   PSlineto(300.0,300.0);
  193.   PSsetlinewidth(4.0);
  194.   PSstroke();
  195.   return self;
  196. }
  197.  
  198. - useGroup: (int) idnum
  199. {
  200. #ifdef DEBUG
  201.   fprintf(stderr,"Changing to group %d\n",idnum);
  202. #endif
  203.   currentGroup = grouplist[idnum];
  204.   [generator setupGenerator: currentGroup];
  205.   if (mode == SKETCH_MODE)
  206.     [underlay setImage: currentGroup];
  207.   [self display];
  208.   return self;
  209. }
  210.  
  211. - loadTiff:sender
  212. /* load a tiff image into the underlay */
  213. {
  214.   NXSize asize;
  215.   const char *const *filenames;
  216.   static char fn[1024];
  217.   if(opener == nil)
  218.     opener = [OpenPanel new];
  219.   [opener allowMultipleFiles: NO];
  220. /*  [underlay clear]; */
  221.   [self switchToTiffMode];
  222.   [opener runModalForDirectory: "/LocalLibrary/Images" file: "" types: types];
  223.   filenames = [opener filenames];
  224.   if(filenames && *filenames){
  225.     sprintf(fn,"%s/%s",[opener directory],*filenames);
  226. #ifdef DEBUG
  227.     fprintf(stderr,"Filename %s\n",fn);
  228. #endif
  229.     [underlay setImageFromFile: fn];
  230.   }
  231.   else {
  232.     [underlay setImageFromFile: "audrey5.tiff"];
  233.   }
  234. #ifdef DEBUG
  235.   fprintf(stderr,"Finishing loadTiff:\n");
  236. #endif
  237.   [mywin close];
  238.   [[underlay getImage] getSize: &asize];
  239.   [mywin sizeWindow: asize.width:asize.height];
  240.   [underlay sizeTo: asize.width:asize.height];
  241.   [self sizeTo:asize.width:asize.height];
  242.   [self display];
  243.   [mywin makeKeyAndOrderFront: self];
  244.   return self;
  245. }
  246.  
  247. - saveSettings:sender
  248. {
  249.   char *fn;
  250.   NXStream *st;
  251.   if(!saver)
  252.     saver = [SavePanel new];
  253.   [saver setRequiredFileType: "tiff"];
  254.   [saver runModal];
  255.   if(fn = [saver filename]){
  256. #ifdef DEBUG
  257.     fprintf(stderr,"Opening %s\n",fn);
  258. #endif
  259.     if(st = NXOpenMemory(NULL,0,NX_WRITEONLY)){
  260.       [[outputView getImage] writeTIFF: st]; 
  261.       NXSaveToFile(st,fn);
  262.       NXCloseMemory(st,NX_FREEBUFFER);
  263.     } else
  264.       fprintf(stderr,"Couldn't open file %s for writing!\n",fn);
  265.   }
  266.   return self;
  267. }
  268.  
  269. - getImageView:(id *) iview withOffset: (NXPoint *) apoint
  270. /* provide view & offset for compositing */
  271. {
  272.   if(mode == TIFF_MODE){
  273.     *iview = underlay;
  274.     [generator getGeneratorOffset: apoint];
  275.   } else {
  276.     *iview = sketchView;
  277.     [currentGroup getGeneratorOffset: apoint];
  278.   }
  279.   return self;
  280. }
  281.  
  282. -cut:sender { return self;};
  283. - windowDidResize: sender
  284. {
  285.   [self tesselate: self];
  286.   [sender display];
  287.   return self;
  288. }
  289. - awin { return anglewin; };
  290. - a1box { return angle1box; };
  291. - a2box { return angle2box; };
  292. - a1slide { return angle1slide; };
  293. - a2slide { return angle2slide; };
  294.  
  295. @end
  296.  
  297.