1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
|
#!/usr/bin/env ruby
require 'tk'
class Clock
def initialize(clock24 = true)
@clock = (clock24)? 24: 12
@size = 200
@cdot_size = 5
@cdot_color = 'black'
@hour_hand_color = 'black'
@minute_hand_color = 'gray25'
@second_hand_color = 'gray50'
@mark_font = 'Helvetica -14'
@mark_width = 3
@mark_color = 'black'
@submark_color = 'gray50'
@c = TkCanvas.new(:width=>2*@size, :height=>2*@size,
:scrollregion=>[-@size, -@size, @size, @size]
).pack(:fill=>:both, :expand=>true)
@tag = TkcTag.new(@c)
@hand_tag = TkcTag.new(@c)
@circle_coords = [[-0.9*@size, -0.9*@size], [0.9*@size, 0.9*@size]]
@oval = TkcOval.new(@c, @circle_coords, :fill=>'white', :tags=>[@tag])
f = TkFrame.new.pack
TkLabel.new(f, :text=>'CURRENT:').pack(:side=>:left)
@now = TkLabel.new(f, :text=>'00:00:00').pack(:side=>:left, :padx=>2)
TkLabel.new(f, :text=>' ').pack(:side=>:left)
TkLabel.new(f, :text=>' ').pack(:side=>:right)
@l = TkLabel.new(f, :text=>'00:00').pack(:side=>:right, :padx=>2)
TkLabel.new(f, :text=>'MOUSE-POINTER:').pack(:side=>:right)
cmd = proc{|x, y|
@l.text = '%02d:%02d' % coords_to_time(@c.canvasx(x), @c.canvasy(y))
}
@c.bind('Motion', cmd, '%x %y')
@tag.bind('Motion', cmd, '%x %y')
_create_hands
_create_marks
timer_proc = proc{
t = Time.now
@now.text = '%02d:%02d:%02d' % [t.hour, t.min, t.sec]
set_hands(t.hour, t.min, t.sec)
}
timer_proc.call
@timer = TkRTTimer.start(100, -1, timer_proc)
end
def _create_marks
@mark_tag = TkcTag.new(@c)
TkcLine.new(@c, 0, -0.90*@size, 0, -0.85*@size,
:tags=>[@tag, @mark_tag],
:width=>@mark_width, :fill=>@mark_color)
TkcLine.new(@c, 0.90*@size, 0, 0.85*@size, 0,
:tags=>[@tag, @mark_tag],
:width=>@mark_width, :fill=>@mark_color)
TkcLine.new(@c, 0, 0.90*@size, 0, 0.85*@size,
:tags=>[@tag, @mark_tag],
:width=>@mark_width, :fill=>@mark_color)
TkcLine.new(@c, -0.90*@size, 0, -0.85*@size, 0,
:tags=>[@tag, @mark_tag],
:width=>@mark_width, :fill=>@mark_color)
TkcText.new(@c, [0, -0.92*@size], :text=>0,
:anchor=>'s', :fill=>@mark_color)
TkcText.new(@c, [0.92*@size, 0], :text=>@clock.div(4),
:anchor=>'w', :fill=>@mark_color)
TkcText.new(@c, [0, 0.92*@size], :text=>@clock.div(2),
:anchor=>'n', :fill=>@mark_color)
TkcText.new(@c, [-0.92*@size, 0], :text=>@clock.div(4)*3,
:anchor=>'e', :fill=>@mark_color)
[30.0, 60.0].each{|angle|
rad = Math::PI * angle / 180.0
x_base = @size*Math::sin(rad)
y_base = @size*Math::cos(rad)
x1 = 0.90*x_base
y1 = 0.90*y_base
x2 = 0.85*x_base
y2 = 0.85*y_base
TkcLine.new(@c, x1, y1, x2, y2,
:tags=>[@tag, @mark_tag],
:width=>@mark_width, :fill=>@mark_color)
TkcLine.new(@c, x1, -y1, x2, -y2,
:tags=>[@tag, @mark_tag],
:width=>@mark_width, :fill=>@mark_color)
TkcLine.new(@c, -x1, y1, -x2, y2,
:tags=>[@tag, @mark_tag],
:width=>@mark_width, :fill=>@mark_color)
TkcLine.new(@c, -x1, -y1, -x2, -y2,
:tags=>[@tag, @mark_tag],
:width=>@mark_width, :fill=>@mark_color)
x3 = 0.92*x_base
y3 = 0.92*y_base
if @clock == 24
dh = angle.to_i/15
else # @clock == 12
dh = angle.to_i/30
end
TkcText.new(@c, x3, -y3, :text=>dh,
:anchor=>'sw', :fill=>@mark_color)
TkcText.new(@c, x3, y3, :text=>@clock.div(2)-dh,
:anchor=>'nw', :fill=>@mark_color)
TkcText.new(@c, -x3, y3, :text=>@clock.div(2)+dh,
:anchor=>'ne', :fill=>@mark_color)
TkcText.new(@c, -x3, -y3, :text=>@clock-dh,
:anchor=>'se', :fill=>@mark_color)
}
if @clock == 24
[15.0, 45.0, 75.0].each{|angle|
rad = Math::PI * angle / 180.0
x_base = @size*Math::sin(rad)
y_base = @size*Math::cos(rad)
x1 = 0.90*x_base
y1 = 0.90*y_base
x2 = 0.875*x_base
y2 = 0.875*y_base
TkcLine.new(@c, x1, y1, x2, y2,
:tags=>[@tag, @mark_tag],
:width=>@mark_width, :fill=>@submark_color)
TkcLine.new(@c, x1, -y1, x2, -y2,
:tags=>[@tag, @mark_tag],
:width=>@mark_width, :fill=>@submark_color)
TkcLine.new(@c, -x1, y1, -x2, y2,
:tags=>[@tag, @mark_tag],
:width=>@mark_width, :fill=>@submark_color)
TkcLine.new(@c, -x1, -y1, -x2, -y2,
:tags=>[@tag, @mark_tag],
:width=>@mark_width, :fill=>@submark_color)
}
end
end
def _create_hands
hour_hand_len = 0.55*@size
minute_hand_len = 0.85*@size
second_hand_len = 0.88*@size
hour_hand_width = 1.8*@cdot_size
minute_hand_width = 1.0*@cdot_size
second_hand_width = 1 # 0.4*@cdot_size
@hour_hand_coords = [
[0, -0.5*@cdot_size],
[hour_hand_width, -0.5*@cdot_size-hour_hand_width],
[hour_hand_width, -hour_hand_len+hour_hand_width],
[0, -hour_hand_len],
[-hour_hand_width, -hour_hand_len+hour_hand_width],
[-hour_hand_width, -0.5*@cdot_size-hour_hand_width],
]
@minute_hand_coords = [
[0, -0.5*@cdot_size],
[minute_hand_width, -0.5*@cdot_size - minute_hand_width],
[minute_hand_width, -minute_hand_len+minute_hand_width],
[0, -minute_hand_len],
[-minute_hand_width, -minute_hand_len+minute_hand_width],
[-minute_hand_width, -0.5*@cdot_size-minute_hand_width],
]
@second_hand_coords = [
[0, -0.5*@cdot_size],
[second_hand_width, -0.5*@cdot_size - second_hand_width],
[second_hand_width, -second_hand_len+second_hand_width],
[0, -second_hand_len],
[-second_hand_width, -second_hand_len+second_hand_width],
[-second_hand_width, -0.5*@cdot_size-second_hand_width],
]
@hour_hand = TkcPolygon.new(@c, @hour_hand_coords,
:tags=>[@tag, @hand_tag],
:outline=>@hour_hand_color,
:fill=>@hour_hand_color)
@minute_hand = TkcPolygon.new(@c, @minute_hand_coords,
:tags=>[@tag, @hand_tag],
:outline=>@minute_hand_color,
:fill=>@minute_hand_color)
@second_hand = TkcPolygon.new(@c, @second_hand_coords,
:tags=>[@tag, @hand_tag],
:outline=>@second_hand_color,
:fill=>@second_hand_color)
@center_dot = TkcOval.new(@c,
[-@cdot_size, -@cdot_size],
[@cdot_size, @cdot_size],
:outline=>@cdot_color, :fill=>@cdot_color)
end
private :_create_hands
def _raise_hands
@hour_hand.raise
@minute_hand.raise
@second_hand.raise
@center_dot.raise
end
private :_raise_hands
def _raise_marks
@mark_tag.raise
end
private :_raise_marks
def set_hands(hh, mm, ss)
ss_angle = Math::PI * ss / 30.0
mm_angle = Math::PI * (mm + ss/60.0) / 30.0
hh_angle = Math::PI * (hh + (mm + ss/60.0)/60.0) / (@clock.div(2))
@second_hand.coords = @second_hand_coords.collect{|x, y|
r = Math::hypot(y, x)
a = Math::atan2(y, x) + ss_angle
[Math::cos(a) * r, Math::sin(a) * r]
}
@minute_hand.coords = @minute_hand_coords.collect{|x, y|
r = Math::hypot(y, x)
a = Math::atan2(y, x) + mm_angle
[Math::cos(a) * r, Math::sin(a) * r]
}
@hour_hand.coords = @hour_hand_coords.collect{|x, y|
r = Math::hypot(y, x)
a = Math::atan2(y, x) + hh_angle
[Math::cos(a) * r, Math::sin(a) * r]
}
_raise_hands
_raise_marks
end
def coords_to_time(x, y)
return ((y < 0)? [0, 0]: [@clock.div(2), 0]) if x == 0
if @clock == 24
offset = (x<0&&y<0)? 1800.0: 360.0
m_half = 720.0
else # @clock == 12
offset = (x<0&&y<0)? 900.0: 180.0
m_half = 360.0
end
(offset + m_half*Math.atan2(y,x)/Math::PI).round.divmod(60)
end
def create_pie(hh, mm, span, color='red')
if @clock == 24
start = 90.0 - (hh*60 + mm)/4.0 # 360.0*(hh*60+mm)/(24*60)
extent = -span/4.0
else # @clock == 12
start = 90.0 - (hh*60 + mm)/2.0 # 360.0*(hh*60+mm)/(12*60)
extent = -span/2.0
end
pie = TkcArc.new(@c, @circle_coords, :tags=>[@tag],
:outline=>'black', 'fill'=>color,
:start=>start, :extent=>extent)
_raise_hands
_raise_marks
pie
end
end
sched = Clock.new
sched.create_pie(0,0, 60) # 60 minutes from 00:00
sched.create_pie(6,30, 280, 'green') # 280 minutes from 06:30
sched.create_pie(15,20, 90, 'blue') # 90 minutes from 15:20
Tk.mainloop
|