Class: TT::Gizmo::RotateGizmo

Inherits:
Object
  • Object
show all
Defined in:
TT_Lib2/gizmo_manipulator.rb

Overview

Since:

  • 2.7.0

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(parent, origin, direction, color, active_color) ⇒ RotateGizmo

Returns a new instance of RotateGizmo

Parameters:

  • origin (Geom::Point3d)
  • direction (Geom::Vector3d)
  • color (Sketchup::Color)
  • active_color (Sketchup::Color)

Since:

  • 2.7.0



857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
# File 'TT_Lib2/gizmo_manipulator.rb', line 857

def initialize( parent, origin, direction, color, active_color )
  @parent = parent
  @origin = origin.clone
  @direction = direction.clone
  @color = color
  @active_color = active_color
  
  @selected = false
  @interacting = false
  
  # Cache of the axis origin and orientation. Cached on onLButtonDown
  @old_origin = nil
  @old_vector = nil
  @old_axis = nil # [pt1, pt2]
  
  # User InputPoint
  @pt_screen_start = nil
  @pt_screen_mouse = nil
  
  @pt_start = nil
  @pt_mouse = nil
  
  # Event callbacks
  @callback = nil
  @callback_start = nil
  @callback_end = nil
end

Instance Attribute Details

#directionObject

Since:

  • 2.7.0



849
850
851
# File 'TT_Lib2/gizmo_manipulator.rb', line 849

def direction
  @direction
end

#originObject

Since:

  • 2.7.0



849
850
851
# File 'TT_Lib2/gizmo_manipulator.rb', line 849

def origin
  @origin
end

Instance Method Details

#active?Boolean

Returns:

  • (Boolean)

Since:

  • 2.7.0



902
903
904
# File 'TT_Lib2/gizmo_manipulator.rb', line 902

def active?
  @interacting == true
end

#cancelObject

Since:

  • 2.7.0



1172
1173
1174
1175
# File 'TT_Lib2/gizmo_manipulator.rb', line 1172

def cancel
  @selected = false
  @interacting = false
end

#draw(view) ⇒ Nil

Parameters:

  • view (Sketchup::View)

Returns:

  • (Nil)

Since:

  • 2.7.0



1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
# File 'TT_Lib2/gizmo_manipulator.rb', line 1071

def draw( view )
  points = rotation_segment( view )
  screen_pts = screen_points( points, view )
  
  view.line_stipple = ''
  view.line_width = 2
  view.drawing_color = (@selected) ? @active_color : @color
  view.draw2d( GL_LINE_STRIP, screen_pts )
  
  if @interacting && @pt_start && @pt_mouse
    angle = @angle

    view.line_stipple = ''
    view.line_width = 1

    # Account for snapping
    start_vector = @origin.vector_to( @pt_start )
    tr = Geom::Transformation.rotation( @origin, @direction, angle )
    mouse_vector = start_vector.transform( tr )
    
    # Start Line
    view.drawing_color = @color
    view.draw( GL_LINES, [@origin, @pt_start] )
    
    # End Line
    view.drawing_color = @color
    #view.draw( GL_LINES, [@origin, @pt_mouse] )
    pt2 = @origin.offset( mouse_vector )
    view.draw( GL_LINES, [@origin, pt2] )
    
    # Rotation Pie
    #start_vector = @origin.vector_to( @pt_start )
    #mouse_vector = @origin.vector_to( @pt_mouse )
    clockwise = ( start_vector * mouse_vector % @direction ) < 0

    # Generate ticks
    unit_options = view.model.options['UnitsOptions']
    if unit_options['AngleSnapEnabled']
      radius = view.pixels_to_model( 150, @origin )
      tick_length = view.pixels_to_model( 150 / 12, @origin )
      steps = 360.0 / unit_options['SnapAngle']
      ticks = TT::Geom3d.circle2d( ORIGIN, X_AXIS, radius, steps )
      ticks.map! { |pt| [ pt, pt.offset( pt.vector_to(ORIGIN), tick_length ) ] }
      ticks.flatten!
      yaxis = start_vector * @direction
      tr = Geom::Transformation.axes( @origin, start_vector, yaxis, @direction )
      for pt in ticks
        pt.transform!( tr )
      end
      view.draw( GL_LINES, ticks )
    end
    
    # Rotation Pie
    segments = TT::Geom3d.arc_segments( angle, 64 )

    if TT::SketchUp.support?( TT::SketchUp::COLOR_ALPHA ) && @selected
      fill = Sketchup::Color.new( *@color.to_a )
      radius = view.pixels_to_model( 150, @origin )

      # Rotated Segment
      fill.alpha = 0.4
      view.drawing_color = fill
      arc = TT::Geom3d.arc( @origin, start_vector, @direction, radius, 0, angle, segments )
      polygon = arc + [@origin]
      view.draw( GL_POLYGON, polygon )

      # Unrotated Segment
      # First 180 - because OpenGL only draws convex shapes.
      half = ( clockwise ) ? -180.degrees : 180.degrees
      rest_angle = ( half - angle ) * -1
      half = half * -1 if rest_angle.abs == 360.degrees # Edge case
      fill.alpha = 0.2
      view.drawing_color = fill
      segments = TT::Geom3d.arc_segments( half, 64 )
      arc = TT::Geom3d.arc( @origin, mouse_vector, @direction, radius, 0, half, segments )
      polygon = arc + [@origin]
      view.draw( GL_POLYGON, polygon )
      # Remainding Segment
      #rest_angle = ( half - angle ) * -1
      if rest_angle.abs != 360.degrees
        segments = TT::Geom3d.arc_segments( rest_angle, 64 )
        arc = TT::Geom3d.arc( @origin, start_vector, @direction, radius, 0, rest_angle, segments )
        polygon = arc + [@origin]
        view.draw( GL_POLYGON, polygon )
      end
    end
    
    view.line_width = 1
    view.drawing_color = @color
    segments = TT::Geom3d.arc_segments( angle, 64 )
    
    for offset in [ 50, 75, 100, 125 ]
      radius = view.pixels_to_model( offset, @origin )
      arc = TT::Geom3d.arc( @origin, start_vector, @direction, radius, 0, angle, segments )
      view.draw( GL_LINE_STRIP, arc )
    end

  end
end

#mouse_active?Boolean

Returns:

  • (Boolean)

Since:

  • 2.7.0



908
909
910
# File 'TT_Lib2/gizmo_manipulator.rb', line 908

def mouse_active?
  @mouse_active == true
end

#on_transform(&block) ⇒ Object

Since:

  • 2.7.0



886
887
888
# File 'TT_Lib2/gizmo_manipulator.rb', line 886

def on_transform( &block )
  @callback = block
end

#on_transform_end(&block) ⇒ Object

Since:

  • 2.7.0



896
897
898
# File 'TT_Lib2/gizmo_manipulator.rb', line 896

def on_transform_end( &block )
  @callback_end = block
end

#on_transform_start(&block) ⇒ Object

Since:

  • 2.7.0



891
892
893
# File 'TT_Lib2/gizmo_manipulator.rb', line 891

def on_transform_start( &block )
  @callback_start = block
end

#onLButtonDown(flags, x, y, view) ⇒ Boolean

Parameters:

  • flags (Integer)
  • x (Integer)
  • y (Integer)
  • view (Sketchup::View)

Returns:

  • (Boolean)

Since:

  • 2.7.0



924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
# File 'TT_Lib2/gizmo_manipulator.rb', line 924

def onLButtonDown( flags, x, y, view )
  if @selected
    
    # Cache the origin for use in onMouseMove to work out the distance
    # moved.
    @old_origin = @origin.clone
    @old_axis = [ @origin.clone, @direction.clone ] # Line (3D)
    
    @pt_screen_start = Geom::Point3d.new( x, y, 0 )
    @pt_screen_mouse = @pt_screen_start.clone
    
    segment = rotation_segment( view )
    @pt_start = project_to_segment( view, x, y, segment )
    if @pt_start
      @pt_mouse = @pt_start.clone
    else
      @pt_mouse = nil
      return true
    end
    
    @last_vector = @origin.vector_to( @pt_start )
    @angle = 0
    @last_angle = 0
    
    @interacting = true
    @callback_start.call( self, 'Rotate' ) unless @callback_start.nil?
    true
  else
    false
  end
end

#onLButtonUp(flags, x, y, view) ⇒ Boolean

Parameters:

  • flags (Integer)
  • x (Integer)
  • y (Integer)
  • view (Sketchup::View)

Returns:

  • (Boolean)

Since:

  • 2.7.0



963
964
965
966
967
968
969
970
971
972
# File 'TT_Lib2/gizmo_manipulator.rb', line 963

def onLButtonUp( flags, x, y, view )
  if @interacting
    @callback_end.call( self, 'Rotate' ) unless @callback_end.nil?
    @interacting = false
    true
  else
    @interacting = false
    false
  end
end

#onMouseMove(flags, x, y, view) ⇒ Boolean

Parameters:

  • flags (Integer)
  • x (Integer)
  • y (Integer)
  • view (Sketchup::View)

Returns:

  • (Boolean)

Since:

  • 2.7.0



981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
# File 'TT_Lib2/gizmo_manipulator.rb', line 981

def onMouseMove( flags, x, y, view )
  if @interacting
    @mouse_active = true
    
    segment = rotation_segment( view )
    screen_point = Geom::Point3d.new( x, y, 0 )
    
    @pt_mouse = project_to_segment( view, x, y, segment )
    return false unless @pt_mouse
    
    start_vector = @origin.vector_to( @pt_start )
    mouse_vector = @origin.vector_to( @pt_mouse )

    clockwise = ( start_vector * mouse_vector % @direction ) < 0

    total_angle = start_vector.angle_between( mouse_vector )

    if clockwise
      total_angle = total_angle * -1
    end

    # Snapping
    a = total_angle
    unit_options = view.model.options['UnitsOptions']
    if unit_options['AngleSnapEnabled']
      snap_angle = unit_options['SnapAngle'].degrees
      size = view.pixels_to_model( 150, @origin ) # Protractor size
      # Closest snap
      diff = a % snap_angle
      if diff > snap_angle / 2.0
        nearest_angle = a + (snap_angle - diff)
      else
        nearest_angle = a - diff
      end
      # Snapping behaviour depends if the cursor is within the Gizmo radius.
      mouse_ray = view.pickray( x, y )
      plane = [ @origin, @direction ]
      plane_point = Geom::intersect_line_plane( mouse_ray, plane )
      mouse_plane_vector = @origin.vector_to( plane_point )
      #if mouse_vector.length < size
      if mouse_plane_vector.length < size
        # Within the Gizmo radius - Full snap
        a = nearest_angle
      else
        # Outside the Gizmo radius - Proximity snap
        #rotation = (direction < 0.0) ? -nearest_angle : nearest_angle
        #rotation = (clockwise) ? -nearest_angle : nearest_angle
        rotation = nearest_angle
        tr = Geom::Transformation.rotation( @origin, @direction, rotation )
        line = [ @origin, @pt_start.transform(tr) ]
        
        nx = view.pixels_to_model( 5, @pt_mouse )
        ny = @pt_mouse.project_to_line(line).distance( @pt_mouse )

        a = nearest_angle if ny < nx # Snap!
      end
      # Adjust increment snap.
      if a != total_angle
        total_angle = a
      end
    end # if snapping
    @angle = total_angle
    
    t_total = Geom::Transformation.rotation( @origin, @direction, total_angle )
    
    increment_angle = total_angle - @last_angle
    t_increment = Geom::Transformation.rotation( @origin, @direction, increment_angle )
    
    angle_formatted = Sketchup.format_angle( total_angle )
    Sketchup.vcb_label = 'Angle'
    Sketchup.vcb_value = angle_formatted
    view.tooltip = "Rotate: #{angle_formatted}"
    
    @pt_screen_mouse = screen_point # (?) Unused?
    @last_angle = total_angle

    data = [ :rotate, @parent.origin, @direction, total_angle ]
    
    @callback.call( self, t_increment, t_total, data ) unless @callback.nil?
    true
  else
    @selected = mouse_over?( x, y, view )
    @mouse_active = @selected
  end
end

#tooltipObject

Since:

  • 2.7.0



913
914
915
# File 'TT_Lib2/gizmo_manipulator.rb', line 913

def tooltip
  'Rotate'
end