### Sets Z-plane for 2D Tools.
#
module Tools2D
class Zplane2D

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
    @@cursor=nil
    curpath=File.join(File.dirname(__FILE__), "Icons", "2Dzplane_cursor.png")
    @@cursor=UI::create_cursor(curpath, 0, 31) if File.exist?(curpath)
end

def activate
    @z=Sketchup.active_model.get_attribute("2Dtools","z",nil)
    Sketchup.active_model.set_attribute("2Dtools","z",0.0.mm) unless @z
		@z=Sketchup.active_model.get_attribute("2Dtools","z",0.0.mm)
    @ip = Sketchup::InputPoint.new
    @ip1 = Sketchup::InputPoint.new
    Sketchup::set_status_text((db("2D Tools Z-plane: Pick 3D-point, Type absolute Z-height or +NN/--NN=relative...")), SB_PROMPT)
    Sketchup::set_status_text("Z=#{@z.to_l} ",SB_VCB_LABEL)
    self.reset(nil)
end

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

def onMouseMove(flags, x, y, view)
    view.lock_inference
    @ip.pick view, x, y
    if @ip.valid?
        view.invalidate if @ip.display?
        @ip1.copy! @ip
        view.tooltip=(db("Z-height: "))+@ip1.tooltip
        Sketchup::set_status_text("Z=#{@z.to_l} ",SB_VCB_LABEL)
        Sketchup::set_status_text(@ip1.position.z.to_l.to_s, SB_VCB_VALUE)
    end
end

def resume(view)
  view.invalidate
  Sketchup::set_status_text((db("2D Tools Z-plane: Pick 3D-point, Type absolute Z-height or +NN/--NN=relative...")), SB_PROMPT)
  Sketchup::set_status_text("Z=#{@z.to_l} ",SB_VCB_LABEL)
end

def draw(view)
  if @ip1.valid?
    view.guess_target
    view.line_width=4
    view.draw_points([@ip.position],10,4,"orangered")
  end#if
end

def onLButtonDown(flags, x, y, view)
    @ip1.pick view, x, y
    if @ip1.valid?
        Sketchup::set_status_text("Z=#{@z.to_l} ",SB_VCB_LABEL)
        Sketchup::set_status_text(@ip1.position.z.to_l.to_s,SB_VCB_VALUE)
    end
end

def onLButtonUp(flags,x,y,view)
    Sketchup::set_status_text("", SB_PROMPT)
    self.set_zplane(@ip1.position.z)
end

def deactivate(view)
    Sketchup::set_status_text("",SB_PROMPT)
    Sketchup::set_status_text("",SB_VCB_LABEL)
    Sketchup::set_status_text("",SB_VCB_VALUE)
end

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

def enableVCB?
   return true
end

def onUserText(text, view)
    #return if not @ip1.valid?
    ### check for relative value + or --
    if text=~/^\+/
      text=text[1..-1]
      add=true
    elsif text=~/^--/
      text=text[2..-1]
      sub=true
    else
      add=false
      sub=false
    end#if
    ###
    begin
        value=text.to_l
    rescue
        # Error parsing the text
        UI.beep
        puts (db("Cannot convert "))+text+(db(" to a Z-height"))
        value=nil
        Sketchup::set_status_text("",SB_VCB_VALUE)
    end
    ###
    return nil if not value
    if add
      value=@z+value
    elsif sub
      value=@z-value
    end
    self.set_zplane(value)
    return nil
end

def reset(view)
    ### get/set reference plane 'z'
    @z=Sketchup.active_model.get_attribute("2Dtools","z",nil)
    unless @z
			Sketchup.active_model.set_attribute("2Dtools","z",0.0.mm)
			@z=Sketchup.active_model.get_attribute("2Dtools","z",0.0.mm)
		end
    #@tools=Sketchup.active_model.tools
    puts db("Setting Z-plane.")
end

def set_zplane(z)
    puts "="+z.to_l.to_s
    Sketchup.active_model.set_attribute("2Dtools","z",z)
    Sketchup::set_status_text("",SB_VCB_VALUE)
    Sketchup::set_status_text("",SB_VCB_LABEL)
    Sketchup::set_status_text("",SB_PROMPT)
    Sketchup.send_action("selectSelectionTool:")
    return nil
end

end # class

end#module
#-----------------------------------------------------------------------------
# shortcut
#def zplane2D
    #Sketchup.active_model.select_tool Zplane2D.new
#end
######-----------------------------------------------