home *** CD-ROM | disk | FTP | other *** search
/ Nebula 1995 August / NEBULA.mdf / SourceCode / GameKit / gamekit-1 / GKTextActor.m < prev    next >
Encoding:
Text File  |  1994-06-07  |  4.4 KB  |  169 lines

  1.  
  2. // Handles putting temporary text in the game field.
  3.  
  4. #import <gamekit/gamekit.h>
  5. #import <daymisckit/daymisckit.h>
  6. #import <dpsclient/psops.h>
  7.  
  8. static BOOL classInitialized = NO;
  9.  
  10. @implementation GKTextActor
  11.  
  12. + initialize
  13. {
  14.     id ret = [super initialize];
  15.     // we want to make sure that the text pswraps have been initialized.
  16.     if ((self == [GKTextActor class]) && (!classInitialized)) {
  17.         GKTextSetUp();
  18.         classInitialized = YES;
  19.     }
  20.     return ret;
  21. }
  22.  
  23. - init
  24. {
  25.     NXPoint zero = { 0.0, 0.0 };
  26.     return [self initAtPoint:&zero pointSize:GK_DEFAULTPOINTSIZE];
  27. }
  28.  
  29. - initAtPoint:(const NXPoint *)aPoint pointSize:(float)points
  30. {    // shouldn't ever really use this, but then again, might as well
  31.     // have it set up a useful text string!
  32.     return [self initWithString:GK_DEFAULTTEXT
  33.             atPoint:aPoint pointSize:points];
  34. }
  35.  
  36. - initWithInt:(int)aNumber atPoint:(const NXPoint *)aPoint
  37.         pointSize:(float)points
  38. {
  39.     char tempString[32];
  40.     sprintf(tempString, "%d", aNumber);
  41.     return [self initWithString:tempString atPoint:aPoint pointSize:points];
  42. }
  43.  
  44. - initWithString:(const char *)aString atPoint:(const NXPoint *)aPoint
  45.         pointSize:(float)points
  46. {
  47.     NXRect tempRect;
  48.     id ret = [super init];
  49.     doDraw = YES; visible = YES; blinking = NO; doErase = NO;
  50.     timeLeftOnScreen = 48; blinkPeriod = 4;
  51.     pointSize = points;
  52.     if (!text) text = [[DAYString alloc] init];
  53.     if (!fontName) fontName = [[DAYString alloc] init];
  54.     [fontName setStringValue:"Helvetica"];
  55.     [text setStringValue:aString];
  56.     GK_COPY_VECTORS(&(boundingBox.origin), aPoint);
  57.     // calculate the text size (our bounding box)
  58.     [self getBoundingBox:&tempRect];
  59.     return ret;
  60. }
  61.  
  62. - (int)timeLeftOnScreen { return timeLeftOnScreen; }
  63. - (int)blinkPeriod { return blinkPeriod; }
  64. - (BOOL)blinking { return blinking; }
  65. - (const char *)fontName { return [fontName stringValue]; }
  66.  
  67. - getBoundingBox:(NXRect *)box        // obtain the true bounding box
  68. {    // we have to do it this way 'cause descenders in the text can shift
  69.     // the box down from the drawing origin...
  70.     NXRect tempRect = { { 0.0, 0.0 }, { 0.0, 0.0 } };
  71.     float x, y, width, height;
  72.     GKborderedTextBoundingBox((char *)[text stringValue],
  73.             &x, &y, &width, &height);
  74.     NXSetRect(&tempRect, x + NX_X(&boundingBox),
  75.             y + NX_Y(&boundingBox), width, height);
  76.     NX_WIDTH(&boundingBox) = width;
  77.     NX_HEIGHT(&boundingBox) = height;
  78.     GK_COPY_RECT(box, &tempRect);
  79.     return self;
  80. }
  81.  
  82. - setFontName:(const char *)name
  83. {
  84.     [fontName setStringValue:name];
  85.     return self;
  86. }
  87.  
  88. - setTimeLeftOnScreen:(int)anInt
  89. {
  90.     timeLeftOnScreen = anInt;
  91.     return self;
  92. }
  93.  
  94. - setBlinkPeriod:(int)anInt
  95. {
  96.     blinkPeriod = anInt;
  97.     [self setBlinking:(anInt != 0)];
  98.     return self;
  99. }
  100.  
  101. - setBlinking:(BOOL)anInt
  102. {
  103.     blinking = anInt;
  104.     visible = YES;
  105.     doDraw = YES;
  106.     return self;
  107. }
  108.  
  109. - eraseInDirtPile:dirtPile
  110. { // no need to erase us yet.
  111.     if (timeLeftOnScreen < 1) {
  112.         // tell the stage to remove us after this cycle; we're going away
  113.         [self leaveTheStage];
  114.         doErase = YES;
  115.     }
  116.     if (doErase) { // do the erase if we're blinking
  117.         float x, y, width, height;
  118.         GKborderedTextBoundingBox((char *)[text stringValue],
  119.                 &x, &y, &width, &height);
  120.         doErase = NO;
  121.         [dirtPile addRegion:(x + GK_VECTOR_X(&lastDrawnLocation))
  122.                     :(y + GK_VECTOR_Y(&lastDrawnLocation)) :width :height];
  123.         return self;
  124.     }
  125.     return self;
  126. }
  127.  
  128. - markInDirtPile:dirtPile
  129. { // only need to mark the firt time we draw since we are effectively static
  130.     if (doDraw) {
  131.         float x, y, width, height;
  132.         GKborderedTextBoundingBox((char *)[text stringValue],
  133.                 &x, &y, &width, &height);
  134.         doErase = NO;
  135.         [dirtPile addRegion:(x + GK_VECTOR_X(&lastDrawnLocation))
  136.                     :(y + GK_VECTOR_Y(&lastDrawnLocation)) :width :height];
  137.         return self;
  138.     }
  139.     return self;
  140. }
  141.  
  142. - moveOneFrame
  143. {    // Decrease the time left on screen every time we "move".
  144.     [super moveOneFrame];
  145.     timeLeftOnScreen--;
  146.     if (!(timeLeftOnScreen % blinkPeriod)) {
  147.         visible = !visible; // turn it on or off
  148.         doDraw = YES;    // make sure the new state is redrawn
  149.         if (!visible) doErase = YES;
  150.     }
  151.     return self;
  152. }
  153.  
  154. - drawActorWithOffset:(NXPoint *)offset    // draw the actor
  155. {
  156.     if (timeLeftOnScreen < 1) return self;  // don't draw; we're gone
  157.     doDraw = NO;
  158.     if (!visible) return self;
  159.     // We always render, even though we don't always mark the DirtPile,
  160.     // because some other actor may have overwritten us...
  161.     // render the text; white text with a black outline around it.
  162.     PSselectfont([fontName stringValue], pointSize);
  163.     GKdrawBorderedText((offset->x + NX_X(&boundingBox)),
  164.             (offset->y + NX_Y(&boundingBox)), (char *)[text stringValue]);
  165.     return self;
  166. }
  167.  
  168. @end
  169.