home *** CD-ROM | disk | FTP | other *** search
/ BURKS 2 / BURKS_AUG97.ISO / BURKS / LANGUAGE / SMALTALK / TEXTBOOK / AP25.ST (.txt) < prev    next >
Text File  |  1997-04-22  |  4KB  |  170 lines

  1.  
  2. 'Smalltalk Textbook Appendix 25'!
  3.  
  4.  
  5.  
  6.  
  7.  
  8. EngiGeometric subclass: #EngiVertex
  9.     instanceVariableNames: 'vertexPoint vertexExtent vertexState '
  10.     classVariableNames: ''
  11.     poolDictionaries: ''
  12.     category: 'Engi-Graph'!
  13. EngiVertex comment:
  14. '
  15.  
  16. Engi 0.07 (24 March 1994)
  17. Copyright (C) 1994 by Atsushi Aoki
  18.  
  19. '!
  20.  
  21.  
  22. !EngiVertex methodsFor: 'initialize-release'!
  23.  
  24. initialize
  25.     super initialize.
  26.     vertexPoint := Point zero.
  27.     vertexExtent := 16 @ 16.
  28.     ^self! !
  29.  
  30. !EngiVertex methodsFor: 'testing'!
  31.  
  32. containsPoint: aPoint 
  33.     ^self bounds containsPoint: aPoint!
  34.  
  35. isBranch
  36.     ^false!
  37.  
  38. isVertex
  39.     ^true! !
  40.  
  41. !EngiVertex methodsFor: 'bounds accessing'!
  42.  
  43. computeBounds
  44.     | bounds margin expand |
  45.     bounds := self vertexPoint extent: self vertexExtent.
  46.     margin := (self lineWidth / 2) asInteger.
  47.     expand := margin @ margin corner: margin + 1 @ (margin + 1).
  48.     bounds := bounds rounded expandedBy: expand.
  49.     ^bounds! !
  50.  
  51. !EngiVertex methodsFor: 'point accessing'!
  52.  
  53. vertexPoint
  54.     ^vertexPoint!
  55.  
  56. vertexPoint: aPoint 
  57.     vertexPoint := aPoint.
  58.     self flushBounds! !
  59.  
  60. !EngiVertex methodsFor: 'extent accessing'!
  61.  
  62. vertexExtent
  63.     ^vertexExtent!
  64.  
  65. vertexExtent: sizePoint 
  66.     vertexExtent := sizePoint.
  67.     self flushBounds! !
  68.  
  69. !EngiVertex methodsFor: 'state accessing'!
  70.  
  71. vertexState
  72.     ^vertexState!
  73.  
  74. vertexState: aState 
  75.     vertexState := aState! !
  76.  
  77. !EngiVertex methodsFor: 'transforming'!
  78.  
  79. rotatedBy: angleDegree 
  80.     self vertexPoint: (self class rotate: self vertexPoint by: angleDegree).
  81.     self flushBounds!
  82.  
  83. scaledBy: scalePoint 
  84.     self vertexPoint: (self class scale: self vertexPoint by: scalePoint).
  85.     self flushBounds!
  86.  
  87. translatedBy: amountPoint 
  88.     self vertexPoint: (self class translate: self vertexPoint by: amountPoint).
  89.     self flushBounds! !
  90.  
  91. !EngiVertex methodsFor: 'displaying'!
  92.  
  93. displayFilledOn: graphicsContext at: aPoint 
  94.     | aRectangle |
  95.     self fillColor isNil ifFalse: [((self bounds translatedBy: aPoint)
  96.             intersects: graphicsContext clippingBounds)
  97.             ifTrue: 
  98.                 [aRectangle := self vertexPoint extent: self vertexExtent.
  99.                 aRectangle := (aRectangle translatedBy: aPoint) rounded.
  100.                 graphicsContext paint: self fillColor.
  101.                 graphicsContext lineWidth: self lineWidth.
  102.                 graphicsContext
  103.                     displayWedgeBoundedBy: (aRectangle insetBy: (1 @ 1 corner: 1 @ 1))
  104.                     startAngle: 0
  105.                     sweepAngle: 360]]!
  106.  
  107. displayStrokedOn: graphicsContext at: aPoint 
  108.     | aRectangle |
  109.     self strokeColor isNil ifFalse: [((self bounds translatedBy: aPoint)
  110.             intersects: graphicsContext clippingBounds)
  111.             ifTrue: 
  112.                 [aRectangle := self vertexPoint extent: self vertexExtent.
  113.                 aRectangle := (aRectangle translatedBy: aPoint) rounded.
  114.                 graphicsContext paint: self strokeColor.
  115.                 graphicsContext lineWidth: self lineWidth.
  116.                 graphicsContext
  117.                     displayArcBoundedBy: (aRectangle insetBy: (1 @ 1 corner: 2 @ 2))
  118.                     startAngle: 0
  119.                     sweepAngle: 360]]! !
  120. "-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- "!
  121.  
  122. EngiVertex class
  123.     instanceVariableNames: ''!
  124.  
  125.  
  126. !EngiVertex class methodsFor: 'instance creation'!
  127.  
  128. new
  129.     ^super new initialize! !
  130.  
  131. !EngiVertex class methodsFor: 'examples'!
  132.  
  133. example1
  134.     "EngiVertex example1."
  135.  
  136.     | aVertex activeWindow activeSensor previousPoint currentPoint |
  137.     aVertex := EngiVertex new.
  138.     activeWindow := aVertex class activeWindow.
  139.     activeWindow clear.
  140.     activeSensor := activeWindow sensor.
  141.     previousPoint := nil.
  142.     [activeSensor anyButtonPressed]
  143.         whileFalse: 
  144.             [currentPoint := activeSensor cursorPoint.
  145.             previousPoint = currentPoint
  146.                 ifFalse: 
  147.                     [previousPoint := currentPoint.
  148.                     aVertex vertexPoint: currentPoint.
  149.                     aVertex strokeColor: aVertex class sampleColor.
  150.                     aVertex fillColor: aVertex class sampleColor.
  151.                     aVertex display]].
  152.     activeWindow display.
  153.     ^aVertex!
  154.  
  155. example2
  156.     "EngiVertex example2."
  157.  
  158.     | aVertex visualTransporter |
  159.     aVertex := EngiVertex new.
  160.     aVertex strokeColor: ColorValue red.
  161.     aVertex fillColor: ColorValue green.
  162.     aVertex lineWidth: 3.
  163.     aVertex vertexPoint: 100 @ 100.
  164.     visualTransporter := EngiVisualTransporter load: aVertex.
  165.     visualTransporter
  166.         follow: [self activeSensor cursorPoint]
  167.         while: [self activeSensor noButtonPressed]
  168.         on: self activeGraphicsContext.
  169.     ^aVertex! !
  170.