Tools and Inference

Can anyone explain what exactly the code of these two methods are doing, I can’t find anything on this constant called “CONSTRAIN_MODIFIER_KEY”:

def onKeyDown(key, rpt, flags, view)
    if( key == CONSTRAIN_MODIFIER_KEY && rpt == 1 )
        @shift_down_time = Time.now
        
        # if we already have an inference lock, then unlock it
        if( view.inference_locked? )
            view.lock_inference
        elsif( @state == 0 )
            view.lock_inference @ip
        elsif( @state == 1 )
            view.lock_inference @ip, @ip1
        end
    end
end

def onKeyUp(key, rpt, flags, view)
    if( key == CONSTRAIN_MODIFIER_KEY &&
        view.inference_locked? &&
        (Time.now - @shift_down_time) > 0.5 )
        view.lock_inference
    end
end

CONSTRAIN_MODIFIER_KEY is the shift key.

It’s basically unlocking the inference if the shift key is held down for more than half a second, or unlocking the inference if you press shift again. I can’t explain exactly what the methods are doing without knowing what the @state, @ip, @ip1 instance variables are for.

Agreed. Which is also the same as VK_SHIFT.
Both are 16 on Windows but might differ on Mac so it is safer to use the constants.

CONSTRAIN_MODIFIER_KEY is a SketchUp specific constant name based upon what SketchUp uses the SHIFT key for. Ie, toggling inferences which are usually constraints.

Likewise the VK_CONTROL is usually used for tool “copy mode” and why the API defines the COPY_MODIFIER_KEY constant.

The @state is simply an instance variable used as a counter to keep track of what tool state the tool is in. This looks like code from the "linetool.rb" example and the states would be “ready for first click” (0) and “ready for second click” (1).

The @ip, and @ip1 are Sketchup::InputPoint object references used to help get the points where the user clicks.

1 Like

Yes, I started with the linetool.rb quite a while back as my template and have modified various forms of that original tool to suit my needs.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.