How to find axes directions?

Dear friends,
When we use the line tool in SU and try to draw a line, it simply finds axes directions but in the following code, it is hard to find axes directions. Can you help me with it?

module MyModule
  module MyTest
    class MyTest
    
      def activate
        @st = 0  #Stack
        @input = Sketchup::InputPoint.new
        @mp = @input.position #Mouse Pointer
        @pt = @input.position #Point
      end
      
      def onLButtonDown(flags, x, y, view)
        @st = (@st - 1).abs
        @pt = @mp 
      end

      def onMouseMove(flags, x, y, view)
        @input.pick view, x, y
        @mp = @input.position
        view.invalidate
      end
      
      def draw view
        if @st == 1
          view.set_color_from_line @pt, @mp
          view.line_width = 3
          view.draw_polyline @pt, @mp
        end
      end  
    
    end
    unless file_loaded?(__FILE__)
      UI.menu("Plugins").add_item("MyTest") {
        Sketchup.active_model.select_tool(MyTest.new)
      }
      file_loaded(__FILE__)
    end
  end
end  

"The InputPoint#pick instance_method is used to get the input point at a specific screen position.

The first form just uses the screen position to compute the InputPoint. It is used when you don’t want the InputPoint to be dependent on another InputPoint.

The second form uses the screen position and another InputPoint. It will find additional inferences such as along one of the axis directions from the first point. "

Therefore for example in your activate method declare an input point to help you:

@input_help = Sketchup::InputPoint.new

in your onLButtonDown method copy the input point to the helper ip:

@input_help.copy!(@input)

so, if you have two valid ip’s you can use the above mentioned two form of #pick method to get more inferences in your onMouseMove method:

if @input_help.valid?
  @input.pick(view, x, y, @input_help)
else
  @input.pick(view, x, y)
end

I draw your attention, you can draw your ip in your draw(view) method

@input.draw(view) if @input.display?

to see similar visualization as you can see in native tools.


Last, but not least: in the API documentation (InputPoint) there is a lintk to good example on github:wink:

2 Likes

Your codes work really nice. Thank you so much.