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