home *** CD-ROM | disk | FTP | other *** search
/ Nebula / nebula.bin / SourceCode / Palettes / JoyStick / JoyStick.m < prev    next >
Text File  |  1992-10-29  |  3KB  |  187 lines

  1. #import <appkit/Application.h>
  2. #import <dpsclient/psops.h>
  3. #import <dpsclient/dpsclient.h>
  4. #include <stdlib.h>
  5. #include <stdio.h>
  6.  
  7. #import "JoyStick.h"
  8.  
  9. @implementation JoyStick
  10.  
  11. void JS_scroller(DPSTimedEntry teNumber, double now,id userData)
  12. {
  13.     NXPoint mouseLoc;
  14.  
  15.     [[userData window] getMouseLocation:&mouseLoc];
  16.     [userData convertPoint:&mouseLoc fromView:nil];
  17.     [userData setPos:(NXPoint)mouseLoc];
  18.  
  19. }
  20.  
  21. - (float)getYVal:sender
  22. {
  23.     return y*ymax;
  24. }
  25.  
  26. - (float)getXVal:sender
  27. {
  28.     return x*xmax;
  29. }
  30.  
  31. -setXMax:(float)val
  32. {
  33. xmax=2*val;
  34. return self;
  35. }
  36. -setYMax:(float)val
  37. {
  38. ymax=2*val;
  39. return self;
  40. }
  41.  
  42. - (float)yMax
  43. {
  44.     return 0.5*ymax;
  45. }
  46. - (float)xMax
  47. {
  48.     return 0.5*xmax;
  49. }
  50.  
  51. -setTrackTime:(float)val
  52.     {
  53.     tracktime=val;
  54.     return self;
  55.     }
  56. -setFadeTime:(float)val
  57.     {
  58.     fadetime=val;
  59.     return self;
  60.     }
  61. -(float)trackTime
  62.     {
  63.     return tracktime;
  64.     }
  65. -(float)fadeTime
  66.     {
  67.     return fadetime;
  68.     }
  69.  
  70. - drawSelf:(const NXRect *)rects :(int)rectCount
  71. {
  72.     PSsetgray(NX_DKGRAY);
  73.     PSrectfill(-0.55,-0.55,1.1,1.1);
  74.     PSsetgray(NX_WHITE);
  75.     PSsetlinewidth(0.02);
  76.     PSmoveto(0,-1.1);
  77.     PSlineto(0,1.1);
  78.     PSmoveto(-1.1,0);
  79.     PSlineto(1.1,0);
  80.     PSstroke();
  81.     PSsetgray(NX_BLACK);
  82.     PSrectfill(x-0.05,y-0.05,0.1,0.1);
  83.         
  84.     return self;
  85. }
  86.  
  87. - initFrame:(const NXRect *)frameRect
  88.     {    
  89.     [super initFrame:frameRect];
  90.     
  91.     [self setDrawSize:(NXCoord)1.1:(NXCoord)1.1];
  92.     [self setDrawOrigin:-(float)0.55 :-(float)0.55];
  93.  
  94.     timer = (DPSTimedEntry)-1;
  95.     tracktime=0.1;
  96.     fadetime=0.7;
  97.     track=NO;
  98.     x=y=0;
  99.     xmax=1;
  100.     ymax=1;
  101.     
  102.     return self;
  103.     }
  104.  
  105. -sizeTo:(NXCoord)width:(NXCoord)height
  106. {
  107. [super sizeTo:width :height];
  108. [self setDrawSize:(NXCoord)1.1:(NXCoord)1.1];
  109. [self setDrawOrigin:-(float)0.55 :-(float)0.55];
  110. return self;
  111. }
  112.  
  113. -free
  114. {
  115. if(timer !=  (DPSTimedEntry)-1)
  116.     DPSRemoveTimedEntry(timer);
  117. [super free];
  118. return self;
  119. }
  120. - setPos:(NXPoint)mouseLoc
  121. {
  122. if(track==YES)
  123.     {
  124.     x=    mouseLoc.x<0.5    ? mouseLoc.x     :0.5 ;
  125.     x=    x>-0.5            ? x                 :-0.5;
  126.     y=    mouseLoc.y<0.5    ? mouseLoc.y     : 0.5 ;
  127.     y=    y>-0.5            ? y                 :-0.5;
  128.     }
  129. else
  130.     {
  131.     x *=fadetime;
  132.     y *=fadetime;
  133.     if (x*x<0.0001 && y*y<0.0001 && timer != (DPSTimedEntry)-1)
  134.     {
  135.     DPSRemoveTimedEntry(timer);
  136.     timer=(DPSTimedEntry) -1;
  137.     x=y=0;
  138.     }
  139.     }
  140.     [self display];
  141.     return self;
  142. }
  143.  
  144.  
  145. - (BOOL)acceptsFirstMouse
  146. {
  147.     return YES;
  148. }
  149.  
  150. - mouseDown:(NXEvent *)theEvent
  151. {
  152.     track=YES;
  153.     if(timer== (DPSTimedEntry)-1)
  154.     timer=DPSAddTimedEntry(tracktime, (DPSTimedEntryProc)JS_scroller, (void *)self, NX_BASETHRESHOLD);
  155.     return(self);
  156. }
  157.  
  158. - mouseUp:(NXEvent *)theEvent
  159. {
  160.     track=NO;
  161.     return(self);
  162. }
  163.  
  164. -write:(NXTypedStream *)stream
  165.     {
  166.     [super write:stream];
  167.     NXWriteTypes(stream,"ffff",&xmax,&ymax,&tracktime,&fadetime);
  168.     return self;
  169.     }
  170.     
  171. -read:(NXTypedStream *)stream
  172.     {
  173.     [super read:stream];
  174.     NXReadTypes(stream,"ffff",&xmax,&ymax,&tracktime,&fadetime);
  175.     x=y=0;
  176.     timer= (DPSTimedEntry)-1;
  177.     track=NO;
  178.     return self;
  179.     }
  180.     
  181. - (const char*) inspectorName
  182.     {
  183.     return "JoyStickInspector";
  184.     }
  185.  
  186. @end
  187.