Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
branch: master
Fetching contributors…

Cannot retrieve contributors at this time

127 lines (92 sloc) 2.482 kb

flexible time interval

I found that we can change the loop time interval of animate() and every() freely.

# sample72.rb
Shoes.app :width => 200, :height => 100 do
    time_interval = [0, 5, 1, 5, 2, 10, 1]
    a = animate 100 do
      ti = time_interval.shift
      para ti
      sleep ti
      a.remove if time_interval.empty?
    end
end

sample72.png

sample72.png

Show the executed time interval.

# sample72-1.rb
Shoes.app :width => 200, :height => 100 do
    time_interval = [5, 1, 5, 2, 10, 1]
    @ti = 0
    a = animate 100 do
      m = time_interval.shift
      para m
      sleep @ti
      @ti = m
      a.remove if time_interval.empty?
    end
end

sample72-1.png

sample72-1.png

Show the executing time interval.


But the above two snippets stop everything during executing sleep.
So, it's better to use Thread. Look at the sample72-2.

# sample72-2.rb
Shoes.app :width => 200, :height => 100 do
  Thread.start do
    [0, 5, 1, 5, 2, 10, 1].each do |ti|
      para ti
      sleep ti
    end
  end
end

sample72-2.png
The same as sample72.png

Note 1
sample72-3 doesn't work as I expected.... I'm not sure why....

# sample72-3.rb
Shoes.app :width => 200, :height => 100 do
    time_interval = [0, 5, 1, 5, 2, 10, 1]
    Thread.new do
      a = animate 100 do
        ti = time_interval.shift
        para ti
        sleep ti
        a.remove if time_interval.empty?
      end
    end
end

sample72-3.png
The same as sample72.png

Note 2
Another solution using mixin Observable module. It's interesting, although there is no reason to have to use this library. :-P

# sample72-4.rb
require 'observer'

class TimeServer
  include Observable

  def initialize
    Thread.start do
      [0, 5, 1, 5, 2, 10, 1].each do |ti|
        sleep ti
        changed
        notify_observers ti
      end
    end
  end
end

module TimeWatcher
  def update ti
    @msg.text += ti.to_s
  end
end

Shoes.app :width => 200, :height => 100 do
  @msg = para
  extend TimeWatcher
  TimeServer.new.add_observer self
end

sample72-4.png
The same as sample72.png

Policeman

All sample72.rb to sample72-4.rb worked with Shoes-0.r1263 as same as Raisins.

Jump to Line
Something went wrong with that request. Please try again.