[code] Timer class

[code] Timer class

Postby MartinRinehart » Fri Jan 15, 2010 6:01 pm

I wrote a Timer class. I'm setting it free here, hoping that newbie developers will find it useful or educational.

The code here is more test code than Timer code. The Timer class is more doc than code. The test code here moves a selected component 100 times by [1,1,1]. (Set frames_per_second to your own taste.)

If you don't know how to move components, look at the move() routine in the Timer_test class.

Code: Select all
# timer.rb - a Timer class

require 'sketchup'

class Timer
=begin
Written and placed in public domain by Martin Rinehart, January, 2010.

Thinking back to my Java days, I thought that UI.start/stop_timer() methods should really be replaced by a Timer class. You can do three things with a timer:
 
timer = Timer.new( pause_length, runnable )
timer.start()
timer.stop()

A Timer is initialized with a pause_length (seconds, including fractions of a second, often 1.0/frames_per_second) and a Runnable object. An object is Runnable if it has a run() method that returns a boolean (true==keep running, false==stop running).

After the Timer's start() method is called, the Runnable's run() is called every pause_length seconds until the Timer's stop() method is called or the Runnable's run() method returns false.
=end

    # attr_reader :pause_length, :runnable, :running

    def initialize( pause_length, runnable )
        @pause_length = pause_length
        @runnable = runnable
        @running = false
    end

    def start()
        @id = UI.start_timer( @pause_length, true ) {
            @running =  @runnable.run()
            stop() unless @running
        }
    end

    def stop()
        UI.stop_timer( @id )
        @running = false
    end

end # of class Timer

class Timer_test # implements Runnable

    attr_reader :valid

    def initialize()
        model = Sketchup.active_model
        sels = model.selection()
        @valid = false
        if sels.length > 0
            @sel = sels[0]
            @valid = @sel.is_a?( Sketchup::ComponentInstance )
        end
        @moves = 0
    end
   
    def move( r, g, b )
        trans = @sel.transformation().to_a()
        trans[12] += r; trans[13] += g; trans[14] += b
        trans2 = Geom::Transformation.new( trans )
        @sel.move!( trans2 )
        Sketchup.active_model().active_view().invalidate()
    end
   
    def run()
        move( 1, 1, 1 )
        @moves += 1
        return @moves < 100
    end
   
end # of Timer_test

fps = 10 # frames per second

tt = Timer_test.new()
if tt.valid()
    t = Timer.new( 1.0/fps, tt )
    t.start()
else
    UI.messagebox( 'Please select a component.' )
end

# end of timer.rb


To use this code, save it into /some/dir, not Plugins. Select a component in any model and make sure you can still see it [100,100,100] away from its current location. In the Ruby Console load '/some/dir/timer.rb'.
0
Author, Edges to Rubies - The Complete SketchUp Tutorial at http://www.MartinRinehart.com/models/tutorial.

MartinRinehart 
 

Re: [code] Timer class

Postby cjthompson » Fri Jan 15, 2010 8:08 pm

Just a very small suggestion: instead of having
Code: Select all
t = Timer.new( 1.0/fps, tt )


you might want to put:
Code: Select all
t = Timer.new( 1/fps.to_f, tt )


That way, people experimenting with the code can easily change the numbers, without having to make sure that it's a decimal.

EDIT: Although, now that I think of it, they would only change the fps, not the 1. :oops:
0

cjthompson 
 

Re: [code] Timer class

Postby MartinRinehart » Fri Jan 15, 2010 8:23 pm

cjthompson wrote:you might want to put:
Code: Select all
t = Timer.new( 1/fps.to_f, tt )



Matz made some brilliant decisions, and some other decisions. The decision not to coerce to float was, maybe, one of the other decisions. How many times are you really helped by 9/2 returning 4?
0
Author, Edges to Rubies - The Complete SketchUp Tutorial at http://www.MartinRinehart.com/models/tutorial.

MartinRinehart 
 

SketchUcation One-Liner Adverts

by Ad Machine » 5 minutes ago



Ad Machine 
Robot
 



 

Return to Developers' Forum

Who is online

Users browsing this forum: No registered users and 12 guests

Visit our sponsors: