ShadowInfoObserver trigger only when the slider stop

Hello, happy new year.

when I create a “shadowInfos” observer, and I select type ==0 (the slider), It fires continuously while I move the cursor. It’s an “oninput event”.
For this project, I would like the trigger to be of the “onchange event” type; the trigger only takes place when the cursor is stopped, to avoid slowing down the system by too much calculation.

I tried using sleep 1 function,
But the code is synchronous and not asynchronous.

class MyShadowInfoObserver < Sketchup::ShadowInfoObserver
  def initialize()
    @last=0
    @time=0
  end

  def onShadowInfoChanged(shadow_info, type)
    if type == 0
      @last += 1
      test = @last
      sleep 1 # wait 1 second
      
      if test == @last
        @time = shadow_info["ShadowTime"]
        puts "#{@last} => #{@time}"
      else
        puts "#{test}"
      end
    end
  end
end

If you have any ideas, I’m interested.
Otherwise, I would like to redo a slider, but I haven’t found how to define shadow_info[“ShadowTime”] = new date

Thanks for your help

Kernel#sleep does not work well within a Ruby process embedded within an application.

Use the API’s UI.start_timer instead.

shadowinfo["ShadowTime"].class
#=> Time

The Time class is a core Ruby class.

The only way I think is to save the time the first call to the observer change callback, and then only recalculate after a specific interval.

class MyShadowInfoObserver < Sketchup::ShadowInfoObserver

  INTERVAL ||= 5 # Recalculation interval in seconds

  def initialize
    @elapsed = 0
    @start = 0
  end

  def onShadowInfoChanged(shadow_info, type)
    if type == 0 || type == 4
      if @start = 0 # first call
        @start = Time.now.to_i
        # First calculation:
        calculate(shadow_info["ShadowTime"])
      else # a subsequent call
        @elapsed = Time.now.to_i - @start
        if @elapsed >= INTERVAL
          # Recalculate and reset:
          calculate(shadow_info["ShadowTime"])
          @elapsed = 0
          @start = Time.now.to_i
        end
      end
    end # if type
  end

  def calculate(time)
    # Calculation code here ...
  end

end # observer subclass
1 Like

Thanks Dan
I mistakenly thought it was a DateTime and not a Time Class.
Now that I can edit, it might be easier to redo a slider.
I’m going to try anyway.

Thank you for the help

1 Like