### Freehand Tool
#
module Tools2D
class FreehandTool2D

def db(string)
  dir=File.dirname(__FILE__) ### adjust folder to suit tool
  toolname="2Dtools"  ### adjust xxx to suit tool...
  locale=Sketchup.get_locale.upcase
  path=File.join(dir, toolname+locale+".lingvo")
  unless File.exist?(path)
     return string
  else
    deBabelizer(string,path)
  end 
end#def

def getExtents
    bbox=Sketchup.active_model.bounds
    bbox.add(@ip.position)if @ip and @ip.valid?
    bbox.add(@ip1.position)if @ip1 and @ip1.valid?
    bbox.add(@ip2.position)if @ip2 and @ip2.valid?
    bbox.add(@ip3.position)if @ip3 and @ip3.valid?
    return bbox
end

@@cursor=nil
def initialize()
    @ip1 = nil
    @ip = nil
    @iplast= nil
	@@cursor=nil
    curpath=File.join(File.dirname(__FILE__), "Icons", "2Dfreehand_cursor.png")
    @@cursor=UI::create_cursor(curpath, 0, 31) if File.exist?(curpath)
end

def activate
    @segment_length=1.0.inch if not @segment_length
    @ip1 = Sketchup::InputPoint.new
    @ip = Sketchup::InputPoint.new
    @iplast= Sketchup::InputPoint.new
    @points=[]
    self.reset(nil)
end

def deactivate(view)
    view.invalidate if @drawn
    Sketchup::set_status_text("",SB_PROMPT)
    Sketchup::set_status_text("",SB_VCB_LABEL)
    Sketchup::set_status_text("",SB_VCB_VALUE)
end

def resume(view)
  view.invalidate
  if not @points[0]
    Sketchup::set_status_text((db("2D Freehand:"))+@zmsg+(db(" Pick Start Point. Hold down mouse-button to draw. To Exit press <Enter> or <Esc>")),SB_PROMPT)
    Sketchup::set_status_text((db("2D Freehand Seg'")),SB_VCB_LABEL)
    Sketchup::set_status_text(@segment_length.to_s,SB_VCB_VALUE)
  else
    Sketchup::set_status_text((db("2D Freehand:"))+@zmsg+(db(" Release mouse-button to end. Move near to start to auto-close loop. To Cancel press <Esc>")),SB_PROMPT)
    Sketchup::set_status_text((db("2D Freehand Seg'")),SB_VCB_LABEL)
    Sketchup::set_status_text(@segment_length.to_s,SB_VCB_VALUE)
  end
end

def onSetCursor()
    #return nil if RUBY_PLATFORM =~ /darwin/
	UI::set_cursor(@@cursor)if @@cursor
end

def onMouseMove(flags, x, y, view)
        if not @points[0]
          Sketchup::set_status_text((db("2D Freehand:"))+@zmsg+(db(" Pick Start Point. Hold down mouse-button to draw. To Exit press <Enter> or <Esc>")),SB_PROMPT)
          Sketchup::set_status_text((db("2D Freehand Seg'")),SB_VCB_LABEL)
          Sketchup::set_status_text(@segment_length.to_s,SB_VCB_VALUE)
        else
          Sketchup::set_status_text((db("2D Freehand:"))+@zmsg+(db(" Release mouse-button to end. Move near to start to auto-close loop. To Cancel press <Esc>")),SB_PROMPT)
          Sketchup::set_status_text((db("2D Freehand Seg'")),SB_VCB_LABEL)
          Sketchup::set_status_text(@segment_length.to_s,SB_VCB_VALUE)
        end
        @ip.pick(view,x,y)
        if @ip.valid?
            @ip1.copy!(@ip)
            view.tooltip = @ip1.tooltip
            view.invalidate if @iplast.display? or @ip1.display?
            p1=@ip1.position;p1.z=@z
            if p1 and @points.last and p1.distance(@points.last)>=@segment_length
              @points<<p1
            end
            if @points[2] and @points.last.distance(@points.first)<=@segment_length
              Sketchup::set_status_text((db("Closed Loop")),SB_VCB_VALUE)
            else
              Sketchup::set_status_text(@segment_length.to_s,SB_VCB_VALUE)
            end
            begin
               view.drawing_color = "black"
               view.draw_polyline(@points)
            rescue
              ###
            end
            @iplast=@ip1
            @ip1.clear
        end#if
        view.invalidate
end

def onLButtonDown(flags, x, y, view)
    @ip.pick view, x, y
    if @ip.valid?
      view.tooltip=@ip.tooltip
      p1=@ip.position;p1.z=@z
      @points<<p1
      @iplast=@ip
      view.invalidate
    end
end

def onLButtonUp(flags, x, y, view)
    self.create_geometry(@points,view)### end this loop
    self.reset(view) ### start another
end

def onKeyDown(key, repeat, flags, view)
    ###
end

def onKeyUp(key, repeat, flags, view)
    ###
end

def enableVCB?
   return true
end

def onUserText(text, view)
    begin
        value = text.to_l
    rescue # Error parsing the text
        UI.beep
        puts (db("Cannot convert "))+text+(db(" to a Length"))
        value = nil
    end
    return if !value
    @segment_length=value
    Sketchup::set_status_text(@segment_length.to_s,SB_VCB_VALUE)
    view.invalidate
end

def draw(view)
    if @ip.valid?
       view.drawing_color = "black"
       begin
        view.draw_polyline(@points)
       rescue
        ###
       end
    end
    view.invalidate
end

def onCancel(flag, view)
    if @points[1]
      view.invalidate
      self.reset(view)
    else
      view.invalidate
      Sketchup::set_status_text("",SB_PROMPT)
      Sketchup::set_status_text("",SB_VCB_LABEL)
      Sketchup::set_status_text("",SB_VCB_VALUE)
      Sketchup.send_action("selectSelectionTool:")
      return nil
    end#if
end

def onReturn(view)
    view.invalidate
    Sketchup::set_status_text("",SB_PROMPT)
    Sketchup::set_status_text("",SB_VCB_LABEL)
    Sketchup::set_status_text("",SB_VCB_VALUE)
    Sketchup.send_action("selectSelectionTool:")
    return nil
end

def reset(view)
    ### get/set reference plane 'z'
    @z=Sketchup.active_model.get_attribute("2Dtools","z",nil)
    Sketchup.active_model.set_attribute("2Dtools","z",0.0.mm)if not @z
    @z=Sketchup.active_model.get_attribute("2Dtools","z",nil)
    @zmsg=" [ Z="+@z.to_s+" ] "
    ###
    Sketchup::set_status_text((db("2D Freehand:"))+@zmsg+(db(" Pick Start Point. Hold down mouse-button to draw. To Exit press <Enter> or <Esc>")),SB_PROMPT)
    Sketchup::set_status_text((db("2D Freehand Seg'")),SB_VCB_LABEL)
    Sketchup::set_status_text(@segment_length.to_s,SB_VCB_VALUE)
    @ip1.clear
    @ip.clear
    @iplast.clear
    @points=[]
    if view
        view.tooltip = nil
        view.invalidate
    end
end

def create_geometry(points, view)
    points<<@points.first if points[2] and points.last.distance(points.first)<=@segment_length
    begin
      view.model.start_operation(db("2D Freehand"))
       view.model.active_entities.add_curve(points)
      view.model.commit_operation
    rescue
      ###
    end#if
end

end # class

end#mod
#-----------------------------------------------------------------------------
# shortcut
#def freehandtool2D
    #Sketchup.active_model.select_tool FreehandTool2D.new
#end
######-----------------------------------------------

