home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Internet Tools 1993 July / Internet Tools.iso / RockRidge / info-service / gopher / NeXT / Tree0.5 / treeobj / MapView.m < prev    next >
Encoding:
Text File  |  1992-05-16  |  4.9 KB  |  232 lines

  1. /* TreeView.m - Copyright 1992  Steve Ludtke  All Rights Reserved       */
  2. /* This object displays each Root as a circle in the x-y plane with    */
  3. /* the current location displayed as a cross.                */
  4.  
  5. #import "MapView.h"
  6. #import <appkit/View.h>
  7. #import <appkit/Window.h>
  8. #import <appkit/Control.h>
  9. #import <appkit/Application.h>
  10. #import <strings.h>
  11. #import <dpsclient/psops.h>
  12. #import <math.h>
  13. #import <libc.h>
  14.  
  15. #define SQR(x) ((x)*(x))
  16. @implementation MapView
  17.  
  18. /* initialize */
  19. - initFrame:(NXRect *)myrect
  20. {
  21.     [super initFrame:myrect];
  22.     bgColor = NX_WHITE;
  23.     fgColor = NX_BLACK;
  24.  
  25.     xsca = NX_WIDTH(&bounds) / 2.0;  /* set scaling for display */
  26.     zsca = NX_HEIGHT(&bounds) / 2.0; /* changing the view coordinate system */
  27.     xof = NX_MIDX(&bounds);         /* would be better */
  28.     zof = NX_MIDY(&bounds);
  29.  
  30.     myx = myy = 0.0;
  31.     return self;
  32. }
  33.  
  34. /* One step in time. Just refresh the display */ 
  35. - step:(Branch *) myloc
  36. {
  37.  
  38.     [self refresh:myloc :80];
  39.     return self;
  40. }
  41.  
  42. /* We want keyboard/mouse events */
  43. - (BOOL)acceptsFirstResponder
  44. {
  45.     return YES;
  46. }
  47.  
  48. /* parse keyboard input */
  49. - keyDown:(NXEvent *)event
  50. {
  51.     char                c;
  52.  
  53.     c = event->data.key.charCode;
  54.     switch (c) {
  55.     break;
  56.     }
  57.     return (self);
  58. }
  59.  
  60. - keyUp:(NXEvent *)event
  61. {
  62.     char                c;
  63.  
  64.     c = event->data.key.charCode;
  65.     switch (c) {
  66.     }
  67.     return (self);
  68. }
  69.  
  70. /* The "meat" of the MapView */
  71. - drawSelf:(NXRect *)rects :(int)rectCount
  72. {
  73.     Root               *cur;
  74.  
  75. /* bounding box for DPSDoUserPath */
  76.     bbox[0] = bbox[1] = 50.0;
  77.     bbox[2] = bbox[3] = 51.0;
  78.  
  79.  
  80. /* clear the view */
  81.     PSsetlinewidth(0.0);
  82.     PSsetgray(NX_BLACK);
  83.     NXRectFill(&bounds);
  84.     PSsetgray(fgColor = NX_WHITE);
  85.  
  86.     cur = top;
  87.     pathc = 0;
  88.  
  89.     while (cur != NULL) {
  90.     if (cur->drawme) {        /* only draw if drawme is true */
  91.     /* draw the "map" display */
  92.         PSarc(cur->branch.x / 32768.0 * xsca, cur->branch.y / 32768.0 * zsca, 2.0, 0, 360.0);
  93.         PSstroke();
  94.     }
  95.     /* repeat for all Roots */
  96.     cur = cur->next;
  97.     }
  98.  
  99.  /* in map mode we want a + where we are */
  100.     PSmoveto(myx / 32768.0 * xsca - 4.0, myy / 32768.0 * zsca);
  101.     PSlineto(myx / 32768.0 * xsca + 4.0, myy / 32768.0 * zsca);
  102.     PSmoveto(myx / 32768.0 * xsca, myy / 32768.0 * zsca + 4.0);
  103.     PSlineto(myx / 32768.0 * xsca, myy / 32768.0 * zsca - 4.0);
  104.     PSstroke();
  105.  
  106. /* if we've used a userpath, this will draw it. Currently unused */
  107.     if (pathc != 0)
  108.     DPSDoUserPath(path, pathc * 2, dps_float, com, pathc, bbox, dps_ustroke);
  109.     return self;
  110. }
  111.  
  112. /* 2d routine to add lines to the view, for future expansion */
  113. - addline:(float)x1 :(float)y1 :(float)x2 :(float)y2
  114. {
  115.     static float        lastx, lasty;
  116.  
  117.     if (x1 != lastx || y1 != lasty) {
  118.     com[pathc] = dps_moveto;
  119.     path[pathc * 2] = x1;
  120.     path[pathc * 2 + 1] = y1;
  121.     pathc++;
  122.     if (x1 > bbox[2])
  123.         bbox[2] = x1;
  124.     if (y1 > bbox[3])
  125.         bbox[3] = y1;
  126.     if (x1 < bbox[0])
  127.         bbox[0] = x1;
  128.     if (y1 < bbox[1])
  129.         bbox[1] = y1;
  130.     }
  131.     com[pathc] = dps_lineto;
  132.     path[pathc * 2] = x2;
  133.     path[pathc * 2 + 1] = y2;
  134.     pathc++;
  135.     if (x2 > bbox[2])
  136.     bbox[2] = x2;
  137.     if (y2 > bbox[3])
  138.     bbox[3] = y2;
  139.     if (x2 < bbox[0])
  140.     bbox[0] = x2;
  141.     if (y2 < bbox[1])
  142.     bbox[1] = y2;
  143.  
  144.     if (pathc >= 1500) {
  145.     DPSDoUserPath(path, pathc * 2, dps_float, com, pathc, bbox, dps_ustroke);
  146.     bbox[0] = bbox[1] = 50.0;
  147.     bbox[2] = bbox[3] = 51.0;
  148.     pathc = 0;
  149.     }
  150.     lastx = x2;
  151.     lasty = y2;
  152.     return (self);
  153. }
  154.  
  155. /* handles movement between branches */
  156. - refresh:(Branch *) myloc :(int)speed;
  157. {
  158.     float               xs, ys, f, xls;
  159.     static Branch      *last = NULL;
  160.     float               sp2;
  161.  
  162.     if (myloc == NULL) {
  163.     myloc = &top->branch;
  164.     last = NULL;
  165.     }
  166.     if (last == NULL) {
  167.     last = myloc;
  168.     myx = myloc->x;
  169.     myy = myloc->y;
  170.     }
  171.     sp2 = 51.2;
  172.     if (speed <= 80)
  173.     sp2 = 97.0;
  174.     if (speed <= 60)
  175.     sp2 = 182.0;
  176.     if (speed <= 40)
  177.     sp2 = 273.0;
  178.     if (speed <= 20)
  179.     sp2 = 400.0;
  180.     if (speed == 0)
  181.     sp2 = 30000.0;
  182.  
  183. /* we moved !!! */
  184.     if (last != myloc) {
  185.     if (last->root != myloc->root) {
  186.     /* zoom over to the new tree */
  187.         xs = (myloc->x - last->x);
  188.         ys = (myloc->y - last->y);
  189.         xls = sqrt(SQR(xs) + SQR(ys)) / sp2;
  190.         if (xls > 200.0)
  191.         xls /= 4.0;
  192.         for (f = xls; f >= 0.0; f -= 1.0) {
  193.         myx = myloc->x - 2.0 - xs / xls * f;
  194.         myy = myloc->y - 2.0 - ys / xls * f;
  195.  
  196.         [self display];
  197.         usleep(20000);
  198.         }
  199.         myx = myloc->x;
  200.         myy = myloc->y;
  201.     }
  202.     }
  203. /* make sure we're looking at the right place */
  204.     [self display];
  205.  
  206.     last = myloc;
  207.  
  208.     return self;
  209. }
  210.  
  211. /* Receive useful pointers */
  212. - start:(Root *) Ptop :Pobject :(char *)path
  213. {
  214.     top = Ptop;
  215.     object = Pobject;
  216.     return self;
  217. }
  218.  
  219. /* no prefs yet ...*/
  220. - preferences:sender
  221. {
  222.     return self;
  223. }
  224.  
  225. /* display help */
  226. - (char *)help:window :browser
  227. {
  228.     return ("MapView - Steve Ludtke  May 1992\n\nSimple view that draws a circle for each site and a cross at the current location. Eventually I'll probably expand this one myself to support zooming, rotating, etc...  For now it's a good, simple example.");
  229. }
  230.  
  231. @end
  232.