'From Cuis 1.0 of 6 April 2009 [latest update: #169] on 13 April 2009 at 3:22:59 pm'!

!PolygonMorph commentStamp: '<historical>' prior: 0!
This class combines the old Polygon and Curve classes.

The 1-bit fillForm to make display and containment tests reasonably fast.  However, this functionality is in the process of being supplanted by balloon capabilities, which should eventually provide anti-aliasing as well.

Warning: Balloon was removed from Cuis.!


!Pen methodsFor: 'operations' stamp: 'jmv 4/13/2009 14:45'!
arrowHead
	"Put an arrowhead on the previous pen stroke"
	" | pen | pen _ Pen new. 20 timesRepeat: [pen turn: 360//20; go: 20; arrowHead]."

	penDown ifTrue:
		[self arrowHeadFrom: (direction degreeCos @ direction degreeSin) * -40 + location 
			to: location
			arrowSpec: 5 @ 4]! !

!Pen methodsFor: 'operations' stamp: 'jmv 4/13/2009 14:46'!
arrowHeadFrom: prevPt to: newPt arrowSpec: anArrowSpec
	"Put an arrowhead on the pen stroke from oldPt to newPt
	Warning: anArrowSpec is currently ignored"

	| pm af myColor finalPt delta |
	myColor _ self color.
	delta _ newPt - prevPt.
	delta r <= 2 "pixels" ifTrue: [^ self].
	finalPt _ newPt + (Point r: sourceForm width degrees: delta degrees).	"in same direction"
	pm _ PolygonMorph vertices: (Array with: prevPt asIntegerPoint with: finalPt asIntegerPoint)  
		color: myColor  "not used"
		borderWidth: sourceForm width borderColor: myColor.
	pm makeOpen; makeForwardArrow.
	"anArrowSpec ifNotNil: [pm arrowSpec: anArrowSpec]."
	af _ pm arrowForms first.
	"render it onto the destForm"
	(FormCanvas on: destForm "Display") stencil: af at: af offset + (1@1)
		color: myColor! !


!PolygonMorph methodsFor: 'menu' stamp: 'jmv 4/13/2009 14:41'!
addCustomMenuItems: aMenu hand: aHandMorph
	| lineName |
	super addCustomMenuItems: aMenu hand: aHandMorph.
	aMenu addUpdating: #handlesShowingPhrase target: self action: #showOrHideHandles.
	vertices size > 2 ifTrue:
		[aMenu addUpdating: #openOrClosePhrase target: self action: #makeOpenOrClosed.
		lineName _ (closed ifTrue: ['outline'] ifFalse: ['line']) translated.
		self isCurve
			ifTrue: [aMenu add: ('make segmented {1}' translated format: {lineName}) action: #toggleSmoothing]
			ifFalse: [aMenu add: ('make smooth {1}' translated format: {lineName}) action: #toggleSmoothing]]. 
	aMenu add: 'specify dashed line' translated action:  #specifyDashedLine.

	self isOpen ifTrue: [
		aMenu addLine.
		aMenu addWithLabel: '---' enablement: [self isOpen and: [arrows ~~ #none]] action:  #makeNoArrows.
		aMenu addWithLabel: '-->' enablement: [self isOpen and: [arrows ~~ #forward]] action:  #makeForwardArrow.
		aMenu addWithLabel: '<--' enablement: [self isOpen and: [arrows ~~ #back]] action:  #makeBackArrow.
		aMenu addWithLabel: '<->' enablement: [self isOpen and: [arrows ~~ #both]] action:  #makeBothArrows ]! !

!PolygonMorph methodsFor: 'private' stamp: 'di 6/13/97 07:21'!
computeArrowFormAt: endPoint from: priorPoint
	"Compute a triangle oriented along the line from priorPoint to endPoint.
	Then draw those lines in a form and return that form, with appropriate offset"
	| d v p1 pts box arrowForm bb origin angle |
	d _ borderWidth max: 1.
	v _ endPoint - priorPoint.
	angle _ v theta radiansToDegrees.
	pts _ Array with: (endPoint + (borderWidth//2) + (Point r: d*5 degrees: angle))
				with: (endPoint + (borderWidth//2) + (Point r: d*4 degrees: angle + 135.0))
				with: (endPoint + (borderWidth//2) + (Point r: d*4 degrees: angle - 135.0)).

	box _ ((pts first rect: pts last) encompass: (pts at: 2)) expandBy: 1.
	arrowForm _ ColorForm extent: box extent asIntegerPoint.
	bb _ (BitBlt toForm: arrowForm) sourceForm: nil; fillColor: Color black;
			combinationRule: Form over; width: 1; height: 1.
	origin _ box topLeft.
	p1 _ pts last - origin.
	pts do: [:p | bb drawFrom: p1 to: p-origin.  p1 _ p-origin].
	arrowForm convexShapeFill: Color black.
	^ arrowForm offset: box topLeft! !


!PolygonMorph class methodsFor: 'instance creation' stamp: 'jmv 4/13/2009 14:34'!
shapeFromPen: penBlock color: c borderWidth: bw borderColor: bc
	"World addMorph: (PolygonMorph
		shapeFromPen: [:p | p hilbert: 4 side: 5. p go: 5.
						p hilbert: 4 side: 5. p go: 5]
		color: Color red borderWidth: 1 borderColor: Color black)"

	| pen |
	penBlock value: (pen _ PenPointRecorder new).
	^ self vertices: pen points asArray color: c borderWidth: bw borderColor: bc! !

PolygonMorph removeSelector: #arrowBoundsAt:from:!
PolygonMorph removeSelector: #arrowSpec:!
PolygonMorph removeSelector: #customizeArrows:!
PolygonMorph removeSelector: #quickFill:!
PolygonMorph removeSelector: #standardArrows!
