Skip to content

Commit

Permalink
AeCanvasTest: Improve code via extracting test example building code …
Browse files Browse the repository at this point in the history
…to a separate class
  • Loading branch information
tinchodias committed May 15, 2024
1 parent d571a16 commit 5e542fe
Show file tree
Hide file tree
Showing 2 changed files with 143 additions and 45 deletions.
50 changes: 5 additions & 45 deletions src/Alexandrie-Canvas-Tests/AeCanvasTest.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -76,51 +76,11 @@ AeCanvasTest >> renderArbitraryFigure [
{ #category : #tests }
AeCanvasTest >> renderBackgroundAlpha: backgroundAlpha hasBorder: hasBorder outskirtsSelector: outskirtsSelector [

| aeCanvas drawFigureBlock referenceExtent |
referenceExtent := 50 asPoint.
aeCanvas := AeCanvas extent: referenceExtent.
aeCanvas clear: Color yellow.

drawFigureBlock := [ :depth |

aeCanvas restoreContextAfter: [

aeCanvas
pathTranslate: referenceExtent / -10;
pathScale: 0.75 asPoint.

aeCanvas pathFactory: [ :cairoContext |
cairoContext circleRadius: referenceExtent x / 2 ].

aeCanvas
setBackgroundWith: [
aeCanvas setSourceColor:
(depth odd
ifTrue: [ Color green ]
ifFalse: [ Color red ]) ]
alpha: backgroundAlpha.

hasBorder
ifTrue: [
aeCanvas setBorderBlock: [
aeCanvas
setSourceColor: Color black;
"setCapButt; setJoinRound;
--> Redundant: both are defaults"
setBorderWidth: referenceExtent x / 10 ].
aeCanvas perform: outskirtsSelector ]
ifFalse: [ aeCanvas setBorderOff ].

aeCanvas drawFigureAndPrepareToClip: depth odd.

depth > 0 ifTrue: [
drawFigureBlock value: depth-1 ] ] ].

"Move to initial position and draw with depth=2"
aeCanvas pathTranslate: referenceExtent / 4.
drawFigureBlock value: 2.

^ aeCanvas
^ AeTestRecursiveFiguresBuilder new
backgroundAlpha: backgroundAlpha;
hasBorder: hasBorder;
outskirtsSelector: outskirtsSelector;
newCanvas
]

{ #category : #tests }
Expand Down
138 changes: 138 additions & 0 deletions src/Alexandrie-Canvas-Tests/AeTestRecursiveFiguresBuilder.class.st
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
"
I build instances of `AeCanvas` with nested figures on it.
Browse my accessing protocol for drawing variants.
For drawing the canvas, depth 4 works good, but depth 2 is enough to show drawing glitches.
"
Class {
#name : #AeTestRecursiveFiguresBuilder,
#superclass : #Object,
#instVars : [
'referenceExtent',
'backgroundAlpha',
'hasBorder',
'borderColor',
'outskirtsSelector',
'borderCapSelector',
'borderJoinSelector'
],
#category : #'Alexandrie-Canvas-Tests-Base'
}

{ #category : #accessing }
AeTestRecursiveFiguresBuilder >> backgroundAlpha: anObject [

backgroundAlpha := anObject
]

{ #category : #accessing }
AeTestRecursiveFiguresBuilder >> borderCapSelector: anObject [

borderCapSelector := anObject
]

{ #category : #accessing }
AeTestRecursiveFiguresBuilder >> borderColor: anObject [

borderColor := anObject
]

{ #category : #accessing }
AeTestRecursiveFiguresBuilder >> borderJoinSelector: anObject [

borderJoinSelector := anObject
]

{ #category : #private }
AeTestRecursiveFiguresBuilder >> drawRecursiveFigureOf: depth on: aeCanvas [

aeCanvas restoreContextAfter: [

aeCanvas
pathTranslate: referenceExtent / -10;
pathScale: 0.75 asPoint.

aeCanvas pathFactory: [ :cairoContext |
cairoContext circleRadius: referenceExtent x / 2 ].

aeCanvas
setBackgroundWith: [
aeCanvas setSourceColor:
(depth odd
ifTrue: [ Color green ]
ifFalse: [ Color red ]) ]
alpha: backgroundAlpha.

hasBorder
ifTrue: [
aeCanvas setBorderBlock: [
aeCanvas
setSourceColor: Color black;
perform: borderCapSelector;
perform: borderJoinSelector;
setBorderWidth: referenceExtent x / 10 ].
aeCanvas perform: outskirtsSelector ]
ifFalse: [ aeCanvas setBorderOff ].

aeCanvas drawFigureAndPrepareToClip: depth odd.

"Note the recursion -if it occurs- is done before restoring the
drawing context (this is important for the clipping area)"
depth > 0 ifTrue: [
self drawRecursiveFigureOf: depth - 1 on: aeCanvas ] ]
]

{ #category : #accessing }
AeTestRecursiveFiguresBuilder >> hasBorder: aBoolean [

hasBorder := aBoolean
]

{ #category : #initialization }
AeTestRecursiveFiguresBuilder >> initialize [

super initialize.

referenceExtent := 50 asPoint.

hasBorder := true.
outskirtsSelector := #setOutskirtsCentered.
backgroundAlpha := 0.7.
borderCapSelector := #setCapButt.
borderJoinSelector:= #setJoinRound
]

{ #category : #building }
AeTestRecursiveFiguresBuilder >> newCanvas [
"It works well with depth 4 but depth 2 is enough to show drawing glitches."

^ self newCanvasOf: 2
]

{ #category : #building }
AeTestRecursiveFiguresBuilder >> newCanvasOf: depth [

| aeCanvas |

"Set up canvas"
aeCanvas := AeCanvas extent: referenceExtent.
aeCanvas clear: Color yellow.
aeCanvas pathTranslate: referenceExtent / 4.

self drawRecursiveFigureOf: depth on: aeCanvas.

^ aeCanvas
]

{ #category : #accessing }
AeTestRecursiveFiguresBuilder >> outskirtsSelector: anObject [

outskirtsSelector := anObject
]

{ #category : #accessing }
AeTestRecursiveFiguresBuilder >> referenceExtent: anObject [

referenceExtent := anObject
]

0 comments on commit 5e542fe

Please sign in to comment.