--- a/Cairo__ClockView.st Sun Jun 17 14:49:30 2012 +0000
+++ b/Cairo__ClockView.st Sun Jun 17 20:44:52 2012 +0000
@@ -3,7 +3,7 @@
"{ NameSpace: Cairo }"
SimpleView subclass:#ClockView
- instanceVariableNames:'cr'
+ instanceVariableNames:'cr updater'
classVariableNames:''
poolDictionaries:''
category:'Cairo-Examples'
@@ -18,30 +18,152 @@
^ self basicNew initialize.
! !
+!ClockView methodsFor:'accessing-dimensions'!
+
+preferredExtent
+
+ ^200@200
+
+ "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
- |area oldClip|
+ | time hours mins secs |
+
+ self clippingRectangle:
+ (Smalltalk::Rectangle left:1 top:1 width:self width height:self height).
+ cr isNil ifTrue:[
+ cr := self cairo.
+ ].
+
+ "/ scale to unit square and translate (0, 0) to be (0.5, 0.5), i.e.
+ "/ the center of the window
+ cr save.
+ cr scale: self extent.
+ cr translate: (0.5 @ 0.5).
+ cr lineWidth: 0.05.
+
+ cr paint: (Color red: 33 green: 61 blue: 11).
+ cr paint.
+
+ cr arcX: 0 y: 0 radius: 0.42 from: 0 to: (2 * (Float pi)).
+ cr paint: Color white.
+ cr fillAndPreserve.
+ cr paint: Color black.
+ cr strokeAndPreserve.
+ cr clip.
+
+ "Now, clock ticks"
+
+ 0 to: 11 do:[:i|
+ | inset |
+
+ inset := 0.05.
+ cr save.
+
+ cr lineCap: Cairo::LineCap LINE_CAP_ROUND.
+ (i \\ 3) ~~ 0 ifTrue:[
+ inset := inset * 0.8.
+ cr lineWidth: 0.03.
+ ].
+
+ cr moveToX: (0.42 - inset) * ( i * (Float pi / 6)) cos
+ y: (0.42 - inset) * ( i * (Float pi / 6)) sin.
+
+ cr lineToX: (0.42 ) * ( i * (Float pi / 6)) cos
+ y: (0.42 ) * ( i * (Float pi / 6)) sin.
- area := Smalltalk::Rectangle left:1 top:1 width:self width height:self height.
- oldClip := clipRect.
- self clippingRectangle:area.
+ cr stroke.
+
+ 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).
+
+ cr save.
+ cr lineCap: Cairo::LineCap LINE_CAP_ROUND.
- self clearView.
- self displayLineFromX:1 y:1 toX:self width y: self height.
+ "/ draw the seconds hand
+ cr save.
+ cr lineWidth: 0.016.
+ cr paint: ((Color red: 70 green: 70 blue: 70) alpha: 0.8).
+ cr moveToX: 0.0 y: 0.0.
+ cr lineToX: (secs sin * (0.42 * 0.9))
+ y: (-1 * (secs cos * (0.42 * 0.9))).
+ cr stroke.
+ cr restore.
- cr isNil ifTrue:[cr := self cairo].
+ "/ draw th minutes
+ cr paint: ((Color red: 11 green: 33 blue: 61) alpha: 0.7).
+ cr moveToX: 0.0 y: 0.0.
+ cr lineToX: ((mins + (secs / 60)) sin * (0.42 * 0.8))
+ y: (-1 * ((mins + (secs / 60)) cos * (0.42 * 0.8))).
+ cr stroke.
+
+ "/ draw the hours hand
+ cr paint: ((Color red: 33 green: 61 blue: 11) alpha: 0.6).
+ cr moveToX: 0.0 y: 0.0.
+ cr lineToX: ((hours + (mins / 12)) sin * (0.42 * 0.5))
+ y: (-1 * ((hours + (mins / 12)) cos * (0.42 * 0.5))).
+ cr stroke.
+
+
+ cr restore.
+ cr arcX: 0 y: 0 radius: 0.01 from: 0 to: (2 * (Float pi)).
+ cr fill.
- cr paint: (Color red alpha: 1.0).
- cr rectangleX: 1 y: 1 width: (self width) height: (self height).
- cr stroke.
+
- self clippingRectangle:oldClip.
+ cr restore.
"Created: / 16-06-2012 / 23:25:36 / 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'!