#        Name : adapted from Rotix 1.1 by John Clements
# Description : rotate objects with 90, custom and random degrees
#      Author : TBD
#       Usage : select from the Plugins menu, hover mouse over object (SHIFT to add to current selection)
#               and use left/right for 90 degrees, up/down to rotate with VCB value, left click to random rotate
#        Date : 25.Jul.2oo4
#        Type : tool
#    Modified : 19.Aug.2004 by Glenn M. Lewis - hit 'Home' or 'End' key to change the axis of rotation

class Rotator

  # initialize on tool activation
  def activate
    @entities = []
    # show VCB and status info
    Sketchup::set_status_text("Rotator::Right-click Object  > Left-Click Enity Info to select object, Left/Right for 90 degrees, Up/Down for VCB value,", SB_PROMPT)
    Sketchup::set_status_text("Z:Angle", SB_VCB_LABEL)
    @v = Geom::Vector3d.new(0,0,1)
    # default value
    @angle = 1
    @add_selection = 0
  end
 
  
  #perform rotation
  def rotate(angle)
    ss= Sketchup.active_model.selection
    ss.each do |e|  
      pt = e.bounds.center
      tr = Geom::Transformation.rotation(pt,@v,angle.degrees)
      Sketchup.active_model.active_entities.transform_entities(tr,e)
    end
  end


   # process keyboard
  def onKeyDown(key, repeat, flags, view)
    case key
     when VK_LEFT then rotate(90)
     when VK_RIGHT then rotate(-90)
     when VK_UP then rotate(-@angle)
     when VK_DOWN then rotate(@angle)
    end
  end
  
  # on user text in VCB modify angle
  def onUserText(text,view)
    begin
      value = text.to_i
    rescue
      # Error parsing the text
      UI.beep
      value = nil
      Sketchup::set_status_text "", SB_VCB_VALUE
    end
    return if !value
    
    @angle = value
  end

end # class Rotator

#----------------------------------------------------------------------------
# add menu items
if( not $Rotator_menu_loaded )
  add_separator_to_menu("Tools")
  UI.menu("Tools").add_item("Rotator[TBD]") { Sketchup.active_model.select_tool Rotator.new }
end

$rotator_menu_loaded = true 


