### PolyLine Edit Tool
#
module Tools2D
class PolylineeditTool2D

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()
    @ip = nil
    @ip1 = nil
    @ip2 = nil
    @ip3 = nil
    @state=0
    @edge=nil
    @vert=nil
    @verts=[]
    @points=[]
	###
	@@cursor0=nil
    curpath0=File.join(File.dirname(__FILE__), "Icons", "2Dpolylineedit_cursor0.png")
    @@cursor0=UI::create_cursor(curpath0, 0, 31) if File.exist?(curpath0)
	@@cursor1=nil
    curpath1=File.join(File.dirname(__FILE__), "Icons", "2Dpolylineedit_cursor1.png")
    @@cursor1=UI::create_cursor(curpath1, 0, 31) if File.exist?(curpath1)
	@@cursor2=nil
    curpath2=File.join(File.dirname(__FILE__), "Icons", "2Dpolylineedit_cursor2.png")
    @@cursor2=UI::create_cursor(curpath2, 0, 31) if File.exist?(curpath2)
	###
end

def activate
    ###
    @z=Sketchup.active_model.get_attribute("2Dtools","z",nil)
    Sketchup.active_model.set_attribute("2Dtools","z",0.0.mm)if not @z
    @zmsg=" [ Z="+@z.to_s+" ] "
    @ip = Sketchup::InputPoint.new
    @ip1 = Sketchup::InputPoint.new
    @edge=nil
    @vert=nil
    @verts=[]
    @points=[]
    Sketchup::set_status_text((db("Pick Arc/Polyline/Welded-Curve to be Edited:  Esc/<Enter> to Exit...")), SB_PROMPT)
    Sketchup::set_status_text("",SB_VCB_LABEL)
    Sketchup::set_status_text("",SB_VCB_VALUE)
    @state=0
    self.reset(nil)
end

def onSetCursor()
    #return nil if RUBY_PLATFORM =~ /darwin/
	if @state<1
      UI::set_cursor(@@cursor0)if @@cursor0
    elsif @state==1
      UI::set_cursor(@@cursor1)if @@cursor1
    else
      UI::set_cursor(@@cursor2)if @@cursor2
    end#if    
end

def onMouseMove(flags, x, y, view)
    #view.lock_inference
    @ip.pick view, x, y
    case @state 
     when 0
       Sketchup::set_status_text((db("Pick Arc/Polyline/Welded-Curve to be Edited:  Esc/<Enter> to Exit...")), SB_PROMPT)
      Sketchup::set_status_text("",SB_VCB_LABEL)
      Sketchup::set_status_text("",SB_VCB_VALUE)
      if @ip.valid?
        view.invalidate if @ip.display?
        @ip1.copy! @ip
        view.tooltip=db("Pick Polyline")
      end
     when 1
      Sketchup::set_status_text((db("Pick a Polyline Vertex to Move:  Esc to Exit:  Press <Enter> to make changes...")), SB_PROMPT)
      Sketchup::set_status_text("",SB_VCB_LABEL)
      Sketchup::set_status_text("",SB_VCB_VALUE)
      if @ip.valid?
        view.invalidate if @ip.display?
        @ip2.copy! @ip
        view.tooltip=db("Vertex to Move")
      end
     when 2
      @ip.pick view, x, y, @ip2
      view.lock_inference @ip
      Sketchup::set_status_text((db("Pick New Vertex Location:  Esc to Exit:  Press <Enter> to make changes...")), SB_PROMPT)
      Sketchup::set_status_text("",SB_VCB_LABEL)
      Sketchup::set_status_text("",SB_VCB_VALUE)
      if @ip.valid?
        view.invalidate if @ip.display?
        @ip3.copy! @ip
        view.tooltip=db("New Vertex Location")
      end
    end#case state
end

def onLButtonDown(flags, x, y, view)
    @ip.pick view, x, y
    case @state
     when 0
      @ip1.copy! @ip
      ### check if edge
      ph=view.pick_helper
      ph.do_pick(x,y)
      picked_edge=ph.picked_edge
      @edge=nil
      @edge=picked_edge if picked_edge
      if @edge
        if @edge.curve
          @state=1
          self.edit(view)
        else
          UI.beep
          puts db("The Edge not part of a Curve.")
        end#if
      else
        puts db("Pick an Edge in a Curve !")
        UI.beep
      end#if
      Sketchup::set_status_text((db("Pick a Polyline Vertex to Move:  Esc to Exit:  Press <Enter> to make changes...")), SB_PROMPT)
     when 1
      @ip2.copy! @ip
      ### check if edge >>> vertex
      ph=view.pick_helper
      ph.do_pick(x,y)
      picked_edge=ph.picked_edge
      edge=nil
      edge=picked_edge if picked_edge
      ### get vertex nearest picked point
      if edge
        v0=edge.start;v1=edge.end
        @vert=v0
        @vert=v1 if v0.position.distance(@ip2.position)>v1.position.distance(@ip2.position)
        if @vert and @verts.include?(@vert)
          @state=2
        else
          UI.beep
          puts (db("Not a good Vertex."))
        end#if vert
        Sketchup::set_status_text((db("Pick New Vertex Location:  Esc to Exit:  Press <Enter> to make changes...")), SB_PROMPT)
      end#if edge
     when 2
      @ip3.copy! @ip
      vert_position=@ip3.position
      vert_position.z=@vert.position.z ### ???
      indx=@verts.index(@vert)
      @points[indx]=vert_position
      @ip2.clear
      @ip3.clear
      @state=1
      Sketchup::set_status_text((db("Pick a Polyline Vertex to Move:  Esc to Exit:  Press <Enter> to make changes...")), SB_PROMPT)
      self.reset(view)
    end#case
end

def onLButtonUp(flags,x,y,view)
    ###
end

def onKeyDown(key, repeat, flags, view)
    if key == CONSTRAIN_MODIFIER_KEY && repeat == 1
        @shift_down_time = Time.now
        if view.inference_locked?
            view.lock_inference
        elsif @state == 0 && @ip1.valid?
            view.lock_inference @ip1
        elsif @state == 1 && @ip2.valid?
            view.lock_inference @ip2, @ip1
        end
    end
end

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

def resume(view)
  view.invalidate
end

def draw(view)
    return if not @state>0
    if @looped
      @points[-1]=@points.first 
    end#if
    @opoints=[]
    @verts.each{|v|@opoints<<v.position}
    view.line_width=4
    view.draw_points(@opoints, 8, 4, "black")
    view.drawing_color="orange"
    view.draw_polyline(@points)
    view.draw_points(@points, 8, 4, "orangered")
    view.draw_points([@vert.position], 12, 4, "green")if @vert and @state==2
    view.draw_points([@ip3.position], 12, 4, "orangered")if @ip3 and @state==2
    if @state==2 and @vert and @ip3 and @ip3.valid?
      view.invalidate
      view.line_width=1
      view.line_stipple="_"
      view.set_color_from_line(@vert.position,@ip3.position)
      view.draw_polyline([@vert.position,@ip3.position])
      if @verts.include?(@vert)
        #view.drawing_color="black"
        indx=@verts.index(@vert)
        pt1=@points[indx]
        if @looped
          if indx==0
            pt0=@points[-2]
            pt2=@points[1]
          elsif indx==@points.length-1
            pt0=@points[1]
            pt2=@points[-2]
          else
            pt0=@points[indx-1]
            pt2=@points[indx+1]
          end#if
        else
          if indx==0
            pt0=@points[1]
            pt2=@points[1]
          elsif indx==@points.length-1
            pt0=@points[-2]
            pt2=@points[-2]
          else
            pt0=@points[indx-1]
            pt2=@points[indx+1]
          end#if
        end#if
        view.line_stipple="_"
        view.draw_polyline([pt0,@ip3.position,pt2])
      end#if
    end#if
    view.invalidate
end#if

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

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

def enableVCB?
   return false
end

def onUserText(text, view)
    ###
end

def reset(view)
    @ip2=Sketchup::InputPoint.new
    @ip3=Sketchup::InputPoint.new
end

def onReturn(view)
  if @state==0
    self.deactivate(view)
    return nil
  else
    model=view.model
   model.start_operation(db("Edit Polyline Vertices"))
    if @looped
      @points[-1]=@points.first 
    end#if
    @edge.curve.move_vertices(@points)
   model.commit_operation
    self.activate()
    return nil
  end#if
end

def edit(view)
    @verts=@edge.curve.vertices
    if @verts.first.position==@verts.last.position ###==looped
      @looped=true
    else
      @looped=false
    end#if
    @points=[]
    @verts.each{|v|@points<<v.position}
    @opoints=@points
    view.invalidate
end

end # class

end#mod
#-----------------------------------------------------------------------------
# shortcut
#def polylineedittool2D
    #Sketchup.active_model.select_tool PolylineeditTool2D.new
#end
######-----------------------------------------------