Class: Clock

Inherits:
Object show all
Defined in:
sample/24hr_clock.rb

Instance Method Summary collapse

Constructor Details

#initialize(clock24 = true) ⇒ Clock

Returns a new instance of Clock.



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
# File 'sample/24hr_clock.rb', line 7

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

Instance Method Details

#_create_marksObject



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
# File 'sample/24hr_clock.rb', line 60

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

#coords_to_time(x, y) ⇒ Object



252
253
254
255
256
257
258
259
260
261
262
# File 'sample/24hr_clock.rb', line 252

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

#create_pie(hh, mm, span, color = 'red') ⇒ Object



264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
# File 'sample/24hr_clock.rb', line 264

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

#set_hands(hh, mm, ss) ⇒ Object



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
# File 'sample/24hr_clock.rb', line 225

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