15
|
1 |
"{ Package: 'stx:goodies/libcairo' }"
|
|
2 |
|
|
3 |
"{ NameSpace: Cairo }"
|
|
4 |
|
|
5 |
SimpleView subclass:#ClockView
|
17
|
6 |
instanceVariableNames:'cr updater'
|
15
|
7 |
classVariableNames:''
|
|
8 |
poolDictionaries:''
|
|
9 |
category:'Cairo-Examples'
|
|
10 |
!
|
|
11 |
|
|
12 |
|
|
13 |
!ClockView class methodsFor:'instance creation'!
|
|
14 |
|
|
15 |
new
|
|
16 |
"return an initialized instance"
|
|
17 |
|
|
18 |
^ self basicNew initialize.
|
|
19 |
! !
|
|
20 |
|
17
|
21 |
!ClockView methodsFor:'accessing-dimensions'!
|
|
22 |
|
|
23 |
preferredExtent
|
|
24 |
|
18
|
25 |
^400 @ 400
|
17
|
26 |
|
|
27 |
"Created: / 17-06-2012 / 22:37:47 / Jan Vrany <jan.vrany@fit.cvut.cz>"
|
|
28 |
! !
|
|
29 |
|
|
30 |
!ClockView methodsFor:'event handling'!
|
|
31 |
|
|
32 |
destroy
|
|
33 |
|
|
34 |
updater notNil ifTrue:[updater terminate].
|
|
35 |
super destroy.
|
|
36 |
|
|
37 |
"Created: / 17-06-2012 / 22:41:25 / Jan Vrany <jan.vrany@fit.cvut.cz>"
|
|
38 |
!
|
|
39 |
|
|
40 |
mapped
|
|
41 |
|
|
42 |
updater isNil ifTrue:[
|
|
43 |
updater := [ [ Delay waitForSeconds: 1. self invalidate ] loop ] newProcess.
|
|
44 |
updater resume.
|
|
45 |
].
|
|
46 |
super mapped.
|
|
47 |
|
|
48 |
"Created: / 17-06-2012 / 22:40:41 / Jan Vrany <jan.vrany@fit.cvut.cz>"
|
|
49 |
!
|
|
50 |
|
|
51 |
unmapped
|
|
52 |
|
|
53 |
updater notNil ifTrue:[updater terminate].
|
|
54 |
super unmapped.
|
|
55 |
|
|
56 |
"Created: / 17-06-2012 / 22:42:23 / Jan Vrany <jan.vrany@fit.cvut.cz>"
|
|
57 |
! !
|
|
58 |
|
15
|
59 |
!ClockView methodsFor:'redrawing'!
|
|
60 |
|
|
61 |
redraw
|
|
62 |
|
17
|
63 |
| time hours mins secs |
|
|
64 |
|
|
65 |
self clippingRectangle:
|
|
66 |
(Smalltalk::Rectangle left:1 top:1 width:self width height:self height).
|
|
67 |
cr isNil ifTrue:[
|
|
68 |
cr := self cairo.
|
|
69 |
].
|
|
70 |
|
|
71 |
"/ scale to unit square and translate (0, 0) to be (0.5, 0.5), i.e.
|
|
72 |
"/ the center of the window
|
|
73 |
cr save.
|
|
74 |
cr scale: self extent.
|
|
75 |
cr translate: (0.5 @ 0.5).
|
|
76 |
cr lineWidth: 0.05.
|
|
77 |
|
|
78 |
cr paint: (Color red: 33 green: 61 blue: 11).
|
|
79 |
cr paint.
|
|
80 |
|
|
81 |
cr arcX: 0 y: 0 radius: 0.42 from: 0 to: (2 * (Float pi)).
|
|
82 |
cr paint: Color white.
|
|
83 |
cr fillAndPreserve.
|
|
84 |
cr paint: Color black.
|
|
85 |
cr strokeAndPreserve.
|
|
86 |
cr clip.
|
|
87 |
|
|
88 |
"Now, clock ticks"
|
|
89 |
|
|
90 |
0 to: 11 do:[:i|
|
|
91 |
| inset |
|
|
92 |
|
|
93 |
inset := 0.05.
|
|
94 |
cr save.
|
|
95 |
|
|
96 |
cr lineCap: Cairo::LineCap LINE_CAP_ROUND.
|
|
97 |
(i \\ 3) ~~ 0 ifTrue:[
|
|
98 |
inset := inset * 0.8.
|
|
99 |
cr lineWidth: 0.03.
|
|
100 |
].
|
|
101 |
|
|
102 |
cr moveToX: (0.42 - inset) * ( i * (Float pi / 6)) cos
|
|
103 |
y: (0.42 - inset) * ( i * (Float pi / 6)) sin.
|
|
104 |
|
|
105 |
cr lineToX: (0.42 ) * ( i * (Float pi / 6)) cos
|
|
106 |
y: (0.42 ) * ( i * (Float pi / 6)) sin.
|
16
|
107 |
|
17
|
108 |
cr stroke.
|
|
109 |
|
|
110 |
cr restore.
|
|
111 |
].
|
|
112 |
|
|
113 |
"/ Not, the current time"
|
|
114 |
|
|
115 |
time := Time now.
|
|
116 |
hours := (time hours > 12 ifTrue:[time hours - 12] ifFalse:[time hours])
|
|
117 |
* (Float pi / 6).
|
|
118 |
mins := time minutes * (Float pi / 30).
|
|
119 |
secs := time seconds * (Float pi / 30).
|
|
120 |
|
|
121 |
cr save.
|
|
122 |
cr lineCap: Cairo::LineCap LINE_CAP_ROUND.
|
16
|
123 |
|
17
|
124 |
"/ draw the seconds hand
|
|
125 |
cr save.
|
|
126 |
cr lineWidth: 0.016.
|
|
127 |
cr paint: ((Color red: 70 green: 70 blue: 70) alpha: 0.8).
|
|
128 |
cr moveToX: 0.0 y: 0.0.
|
|
129 |
cr lineToX: (secs sin * (0.42 * 0.9))
|
|
130 |
y: (-1 * (secs cos * (0.42 * 0.9))).
|
|
131 |
cr stroke.
|
|
132 |
cr restore.
|
16
|
133 |
|
17
|
134 |
"/ draw th minutes
|
|
135 |
cr paint: ((Color red: 11 green: 33 blue: 61) alpha: 0.7).
|
|
136 |
cr moveToX: 0.0 y: 0.0.
|
|
137 |
cr lineToX: ((mins + (secs / 60)) sin * (0.42 * 0.8))
|
|
138 |
y: (-1 * ((mins + (secs / 60)) cos * (0.42 * 0.8))).
|
|
139 |
cr stroke.
|
|
140 |
|
|
141 |
"/ draw the hours hand
|
|
142 |
cr paint: ((Color red: 33 green: 61 blue: 11) alpha: 0.6).
|
|
143 |
cr moveToX: 0.0 y: 0.0.
|
|
144 |
cr lineToX: ((hours + (mins / 12)) sin * (0.42 * 0.5))
|
|
145 |
y: (-1 * ((hours + (mins / 12)) cos * (0.42 * 0.5))).
|
|
146 |
cr stroke.
|
|
147 |
|
|
148 |
|
|
149 |
cr restore.
|
|
150 |
cr arcX: 0 y: 0 radius: 0.01 from: 0 to: (2 * (Float pi)).
|
|
151 |
cr fill.
|
15
|
152 |
|
16
|
153 |
|
|
154 |
|
17
|
155 |
|
15
|
156 |
|
17
|
157 |
cr restore.
|
16
|
158 |
|
15
|
159 |
"Created: / 16-06-2012 / 23:25:36 / Jan Vrany <jan.vrany@fit.cvut.cz>"
|
17
|
160 |
!
|
|
161 |
|
|
162 |
redrawX: x y: y width: w height: h
|
|
163 |
|
|
164 |
self redraw
|
|
165 |
|
|
166 |
"Created: / 17-06-2012 / 21:33:48 / Jan Vrany <jan.vrany@fit.cvut.cz>"
|
15
|
167 |
! !
|
|
168 |
|
|
169 |
!ClockView class methodsFor:'documentation'!
|
|
170 |
|
|
171 |
version_SVN
|
|
172 |
^ '$Id:: $'
|
|
173 |
! !
|