Cairo__ClockView.st
author Jan Vrany <jan.vrany@fit.cvut.cz>
Wed, 24 Dec 2014 23:59:59 +0100
changeset 26 7f07a8c31e6d
parent 24 6c74dd51bff4
child 28 1bd3d147cd77
permissions -rw-r--r--
Fixed flickring of Cairo::ClockView by rendering into an off-screen image.

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

"{ NameSpace: Cairo }"

SimpleView subclass:#ClockView
	instanceVariableNames:'cr updater'
	classVariableNames:''
	poolDictionaries:''
	category:'Cairo-Examples'
!


!ClockView methodsFor:'accessing-dimensions'!

preferredExtent

    ^400 @ 400

    "Created: / 17-06-2012 / 22:37:47 / Jan Vrany <jan.vrany@fit.cvut.cz>"
! !

!ClockView methodsFor:'event handling'!

destroy

    updater notNil ifTrue:[updater terminate].
    super destroy.

    "Created: / 17-06-2012 / 22:41:25 / Jan Vrany <jan.vrany@fit.cvut.cz>"
!

mapped

    updater isNil ifTrue:[
        updater := [ [ Delay waitForSeconds: 1. self invalidate ] loop ] newProcess.
        updater resume.
    ].
    super mapped.

    "Created: / 17-06-2012 / 22:40:41 / Jan Vrany <jan.vrany@fit.cvut.cz>"
!

unmapped

    updater notNil ifTrue:[updater terminate].
    super unmapped.

    "Created: / 17-06-2012 / 22:42:23 / Jan Vrany <jan.vrany@fit.cvut.cz>"
! !

!ClockView methodsFor:'redrawing'!

redraw

    | image image_cr time hours mins secs |

    self clippingRectangle: 
        (Smalltalk::Rectangle left:1 top:1 width:self width height:self height).
    cr isNil ifTrue:[
        cr := self cairo.
    ].
    image_cr := Cairo::GraphicsContext on: (image := Cairo::Surface forImageFormatARGB32width: self width  height: self height). 

    "/ scale to unit square and translate (0, 0) to be (0.5, 0.5), i.e.
    "/ the center of the window
    image_cr save.
    image_cr scale: self extent.
    image_cr translate: (0.5 @ 0.5).
    image_cr lineWidth: 0.05.

    image_cr paint: (Color red: 33 green: 61 blue: 11).
    image_cr paint.

    image_cr arcX: 0 y: 0 radius: 0.42 from: 0 to: (2 * (Float pi)).
    image_cr paint: Color white.
    image_cr fillAndPreserve.
    image_cr paint: Color black.
    image_cr strokeAndPreserve.
    image_cr clip.

    "Now, clock ticks"

    0 to: 11 do:[:i|
        | inset |

        inset := 0.05.
        image_cr save.

        image_cr lineCap: Cairo::LineCap LINE_CAP_ROUND.
        (i \\ 3) ~~ 0 ifTrue:[
            inset := inset * 0.8.
            image_cr lineWidth: 0.03.
        ].

        image_cr moveToX: (0.42 - inset) * ( i * (Float pi / 6)) cos
                 y: (0.42 - inset) * ( i * (Float pi / 6)) sin.

        image_cr lineToX: (0.42 ) * ( i * (Float pi / 6)) cos
                 y: (0.42 ) * ( i * (Float pi / 6)) sin.

        image_cr stroke.

        image_cr restore.
    ].

    "/ Not, the current time"

    time := Time now.
    hours := (time hours > 12 ifTrue:[time hours - 12] ifFalse:[time hours])
                * (Float pi / 6).
    mins := time minutes * (Float pi / 30).
    secs := time seconds * (Float pi / 30).

    image_cr save.
    image_cr lineCap: Cairo::LineCap LINE_CAP_ROUND.

    "/ draw the seconds hand
    image_cr save.
    image_cr lineWidth: 0.016.
    image_cr paint: ((Color red: 70 green: 70 blue: 70) alpha: 0.8).
    image_cr moveToX: 0.0 y: 0.0.
    image_cr lineToX: (secs sin * (0.42 * 0.9))
             y: (-1 *  (secs cos * (0.42 * 0.9))).
    image_cr stroke.
    image_cr restore.

    "/ draw th minutes
    image_cr paint: ((Color red: 11 green: 33 blue: 61) alpha: 0.7).
    image_cr moveToX: 0.0 y: 0.0.
    image_cr lineToX: ((mins + (secs / 60)) sin * (0.42 * 0.8))
             y: (-1 * ((mins + (secs / 60)) cos * (0.42 * 0.8))).
    image_cr stroke.

    "/ draw the hours hand
    image_cr paint: ((Color red: 33 green: 61 blue: 11) alpha: 0.6).
    image_cr moveToX: 0.0 y: 0.0.
    image_cr lineToX: ((hours + (mins / 12)) sin * (0.42 * 0.5))
             y: (-1 * ((hours + (mins / 12)) cos * (0.42 * 0.5))).
    image_cr stroke.


    image_cr restore.
    image_cr arcX: 0 y: 0 radius: 0.01 from: 0 to: (2 * (Float pi)).
    image_cr fill.





    image_cr restore.

    cr setSourceSurface: image. 
    cr paint.

    "Created: / 16-06-2012 / 23:25:36 / Jan Vrany <jan.vrany@fit.cvut.cz>"
    "Modified: / 24-12-2014 / 23:56:53 / Jan Vrany <jan.vrany@fit.cvut.cz>"
!

redrawX: x y: y width: w height: h

    self redraw

    "Created: / 17-06-2012 / 21:33:48 / Jan Vrany <jan.vrany@fit.cvut.cz>"
! !

!ClockView class methodsFor:'documentation'!

version_HG
    ^ '$Changeset: <not expanded> $'
! !