Cairo__Examples1.st
author Jan Vrany <jan.vrany@fit.cvut.cz>
Sun, 28 Feb 2016 16:36:53 +0000
changeset 53 57718b3ac316
parent 51 5293f2b851ab
child 57 2c9a342e1f2a
permissions -rw-r--r--
Examples: added more examples demostraint drawing/filling rectangles and how coordinates relates to line width.

"{ Package: 'stx:goodies/libcairo' }"

"{ NameSpace: Cairo }"

TestCase subclass:#Examples1
	instanceVariableNames:'view surface'
	classVariableNames:''
	poolDictionaries:''
	category:'Cairo-Examples'
!

!Examples1 class methodsFor:'accessing'!

isTestSelector:aSelector
    | method |

    aSelector isNil ifTrue:[ ^ false ].
    method := self lookupMethodFor: aSelector.
    ^ method notNil 
        and:[(method hasAnnotation: #example:) or:[ method hasAnnotation: #example:category: ] ]

    "
    Cairo::Examples1 isTestSelector: #example01:
    "

    "Created: / 26-02-2016 / 21:57:20 / Jan Vrany <jan.vrany@fit.cvut.cz>"
! !

!Examples1 class methodsFor:'private'!

testSelectors
        "The API method is allTestSelectors which now includes #shouldInheritSelectors and so handles all cases.  Unlike that method, this does not guarantee to return a sorted ordered collection."

        ^self sunitSelectors select: [:each | self isTestSelector: each ]

    "Created: / 26-02-2016 / 22:40:48 / Jan Vrany <jan.vrany@fit.cvut.cz>"
! !

!Examples1 methodsFor:'examples'!

example01: cr <example: '01 - arc'>
| xc yc radius angle1 angle2 |

xc := 128.
yc := 128.
radius := 100.
angle1 := 45 * (Float pi / 180).
angle2 := 180 * (Float pi / 180).

cr sourceR: 1.0 G: 1.0 B: 1.0.
cr lineWidth: 10.
cr arcX: xc y: yc radius: radius from: angle1 to: angle2.
cr stroke.

    "Created: / 26-02-2016 / 21:53:02 / Jan Vrany <jan.vrany@fit.cvut.cz>"
!

example21: cr <example: '21 - Display PNG from file'>
| png |

png := Cairo::Surface newPNGWithFile: (Smalltalk getBitmapFileName:'circle1.png' forPackage:#'stx:goodies/libcairo') pathName.

cr sourceR: 1 G: 1 B: 1.
cr rectangleX: 0 y: 0 width: 32 height: 32.
cr fill.

cr sourceR: 0 G: 0 B: 0.
cr rectangleX: 32 y: 32 width: 32 height: 32.
cr fill.

cr sourceSurface: png x: 0 y: 0.
cr paint.

    "Created: / 28-02-2016 / 07:56:02 / Jan Vrany <jan.vrany@fit.cvut.cz>"
!

example22: cr <example: '22 - Rectangles - fill and stroke'>
cr antialias: Cairo::Antialias CAIRO_ANTIALIAS_NONE.

"/ Draw reference lines...
cr lineWidth: 1.
cr sourceR: 0 G: 0 B: 0.
cr moveToX: 1   y: 15.
cr lineToX: 130 y: 15.
cr moveToX: 15  y: 1.
cr lineToX: 15  y: 130.
cr moveToX: 1   y: 115.
cr lineToX: 130 y: 115.
cr moveToX: 115 y: 1.
cr lineToX: 115 y: 130.
cr stroke.

cr lineWidth: 10.
cr sourceR: 1 G: 0 B: 0.
cr rectangleX: 15 y: 15 width: 100 height: 100.
cr stroke.

cr sourceR: 0 G: 0 B: 1.
cr rectangleX: 15 y: 65 width: 50 height: 50.
cr fill.

    "Created: / 28-02-2016 / 15:09:30 / Jan Vrany <jan.vrany@fit.cvut.cz>"
! !

!Examples1 methodsFor:'private'!

performTest
    | cr |

    cr := Cairo::GraphicsContext onSurface: surface.
    [
        self perform: testSelector sunitAsSymbol with: cr.
    ] ensure:[ 
        cr release
    ].

    "Created: / 26-02-2016 / 22:01:46 / Jan Vrany <jan.vrany@fit.cvut.cz>"
! !

!Examples1 methodsFor:'running'!

setUp
    Display notNil ifTrue:[ 
        | top |

        top := StandardSystemView new.
        top origin: 10@10 extent: 200@100.       
        view := SimpleView origin: 0.0 @ 0.0 corner: 1.0 @ 1.0 in: top.
        top openAndWait.
        surface := view cairoSurface.

    ].

    "Created: / 26-02-2016 / 22:15:15 / Jan Vrany <jan.vrany@fit.cvut.cz>"
!

tearDown
    view notNil ifTrue:[ 
        view topView close.
    ].
    surface notNil ifTrue:[ 
        surface release.
    ].

    "Created: / 26-02-2016 / 22:18:42 / Jan Vrany <jan.vrany@fit.cvut.cz>"
! !