Methods
#
C
N
S
U
Class Public methods
new(clock24 = true)
# File ext/tk/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 Public methods
_create_marks()
# File ext/tk/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)
# File ext/tk/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')
# File ext/tk/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)
# File ext/tk/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
update(h, m, s)
# File sample/observ.rb, line 25
def update(h, m, s)
  printf "\e[8D%02d:%02d:%02d", h, m, s
  STDOUT.flush
end