Advertisement
LiTTleDRAgo

[RGSS/2/3] Mouse Gesture

Jun 29th, 2012
253
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 7.23 KB | None | 0 0
  1. #:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:
  2. # [Xp/Vx-VxA] DRG - Mouse Gesture
  3. # Version: 1.50
  4. # Author : LiTTleDRAgo
  5. #:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:
  6. module LiTTleDRAgo
  7.   module MouseGestures
  8.    
  9.   VXA = defined?(Window_BattleActor)
  10. #==============================================================================
  11. # ** CONFIGURATION
  12. #==============================================================================
  13.   ALLOWED_SCENE = ['Scene_Map','Scene_Menu','Scene_Item',
  14.                   'Scene_Skill','Scene_Equip','Scene_Status',
  15.                   'Scene_File','Scene_Load']
  16.           # Insert name scene you want to activate mouse gesture
  17.                  
  18.   # Enter all the condition and result you want here
  19.   def result_mouse_gesture(result)
  20.     case result
  21.     when ['down']  
  22.       change_scene   (Scene_Item)  
  23.     when ['right','up','left','down','right']
  24.       change_scene   (Scene_Equip)
  25.     when ['left','down','right','down','left']
  26.       change_scene   (Scene_Skill)
  27.     when ['left','down','right','down','left','up']
  28.       change_scene   (Scene_Status)
  29.     when ['downright','up','downleft']
  30.       unless [Scene_Map,Scene_Title,Scene_Gameover,Scene_Load].include?(scene)
  31.         change_scene (Scene_Map)
  32.       end
  33.     end
  34.   end
  35.    
  36.   # Enter all condition to disable mouse gesture here
  37.   def forbid_mouse_gestures
  38.     return false if !scene.is_a?(Scene_Map)
  39.     if defined?(Window_ActorCommand)            # If VX or VXA
  40.       return true if !$game_player.movable?
  41.       return false
  42.     end                                         # If XP
  43.     return true if $game_system.map_interpreter.running?
  44.     return true if $game_player.move_route_forcing
  45.     return true if $game_temp.message_window_showing
  46.   end
  47.  
  48.   # Enter all condition to ignore scene changing here
  49.   def ignore_changing_scene
  50.     return true if scene.is_a?(Scene_Battle)
  51.     return true if scene.is_a?(Scene_Load)
  52.     return true if scene.is_a?(Scene_Title)
  53.     return true if scene.is_a?(Scene_Map) && forbid_mouse_gestures
  54.   end
  55. #==============================================================================
  56. # ** CONFIG END
  57. #==============================================================================
  58.  
  59.   def update_mouse_gestures
  60.     return if forbid_mouse_gestures
  61.     clear_mouse_gesture if @mouse_gesture.nil?
  62.     if Mouse.dragging?
  63.       point = @mouse_gesture[0][@mouse_gesture[4]]
  64.       if point == nil ||  point.disposed?
  65.         @mouse_gesture[0][@mouse_gesture[4]] = Sprite.new
  66.       end
  67.       @mouse_gesture[0][@mouse_gesture[4]].x = @mouse_gesture[4] == 0 ?
  68.       Mouse.drag_coor[0] : Mouse.x
  69.       @mouse_gesture[0][@mouse_gesture[4]].y = @mouse_gesture[4] == 0 ?
  70.       Mouse.drag_coor[1] : Mouse.y
  71.       recog_mouse_gesture if @mouse_gesture[6] == Mouse.pos
  72.       @mouse_gesture[6] = Mouse.pos if @mouse_gesture[6] != Mouse.pos
  73.       @mouse_gesture[4] += 1
  74.     else
  75.       if !@mouse_gesture[0].empty?
  76.         @mouse_gesture[1].each_with_index { |s,i|
  77.         @mouse_gesture[1][i-1] = nil if i > 0 && s == @mouse_gesture[1][i-1] }
  78.         result_mouse_gesture(@mouse_gesture[1].compact)
  79.         @mouse_gesture[0].each {|i| i.dispose }
  80.         @mouse_gesture[2].each {|i| i.dispose }
  81.         clear_mouse_gesture
  82.       end
  83.     end
  84.   end
  85.  
  86.   def change_scene(some_scene=nil)
  87.     return if ignore_changing_scene
  88.     VXA ? SceneManager.call(some_scene) : $scene = some_scene.new
  89.   end
  90.   def scene() VXA ? SceneManager.scene : $scene end
  91.   def clear_mouse_gesture()  @mouse_gesture = [[],[],[],0,0,0] end
  92.   def draw_mouse_line(start_x,start_y,end_x,end_y,
  93.                 start_color,width=1,end_color=start_color)
  94.     point = @mouse_gesture[2][@mouse_gesture[3]]
  95.     if point == nil || point.disposed?
  96.       @mouse_gesture[2][@mouse_gesture[3]] = Sprite.new
  97.       @mouse_gesture[2][@mouse_gesture[3]].bitmap = Bitmap.new(640,480)
  98.       @mouse_gesture[2][@mouse_gesture[3]].z = 9999
  99.     end
  100.     distance = (start_x - end_x).abs + (start_y - end_y).abs
  101.     if end_color == start_color
  102.       (1..distance).each {|i|
  103.         x = (start_x + 1.0 * (end_x - start_x) * i / distance).to_i
  104.         y = (start_y + 1.0 * (end_y - start_y) * i / distance).to_i
  105.         @mouse_gesture[2][@mouse_gesture[3]].bitmap.fill_rect(x, y, width,
  106.         width, start_color)}
  107.     else
  108.       (1..distance).each {|i|
  109.         x = (start_x + 1.0 * (end_x - start_x) * i / distance).to_i
  110.         y = (start_y + 1.0 * (end_y - start_y) * i / distance).to_i
  111.         r = start_color.red*(distance-i)/distance+end_color.red*i/distance
  112.         g = start_color.green*(distance-i)/distance+end_color.green*i/distance
  113.         b = start_color.blue*(distance-i)/distance+end_color.blue*i/distance
  114.         a = start_color.alpha*(distance-i)/distance+end_color.alpha*i/distance
  115.         @mouse_gesture[2][@mouse_gesture[3]].bitmap.fill_rect(x, y, width,
  116.         width, Color.new(r, g, b, a))}
  117.     end
  118.     @mouse_gesture[3] += 1
  119.   end
  120.  
  121.   def recog_mouse_gesture
  122.     awal = @mouse_gesture[0][@mouse_gesture[5]]
  123.     akhir = @mouse_gesture[0][@mouse_gesture[4]]
  124.     draw_mouse_line(awal.x,awal.y, akhir.x,akhir.y, Color.new(255,0,0),3)
  125.     if awal.x - akhir.x > 50 && (awal.y - akhir.y).abs < 50
  126.       @mouse_gesture[1] << 'left'
  127.     end
  128.     if awal.x - akhir.x < -50 && (awal.y - akhir.y).abs < 50
  129.       @mouse_gesture[1] << 'right'
  130.     end
  131.     if awal.y - akhir.y > 50 && (awal.x - akhir.x).abs < 50
  132.       @mouse_gesture[1] << 'up'
  133.     end
  134.     if awal.y - akhir.y < -50 && (awal.x - akhir.x).abs < 50
  135.       @mouse_gesture[1] << 'down'
  136.     end
  137.     if awal.x - akhir.x > 50 && awal.y - akhir.y > 50
  138.       @mouse_gesture[1] << 'upleft'
  139.     end
  140.     if awal.x - akhir.x < -50 && awal.y - akhir.y > 50
  141.       @mouse_gesture[1] << 'upright'
  142.     end
  143.     if awal.x - akhir.x > 50 && awal.y - akhir.y < -50
  144.       @mouse_gesture[1] << 'downleft'
  145.     end
  146.     if awal.x - akhir.x < -50 && awal.y - akhir.y < -50
  147.       @mouse_gesture[1] << 'downright'
  148.     end
  149.     @mouse_gesture[5] = @mouse_gesture[4]
  150.   end
  151.   end
  152. end
  153. #==============================================================================
  154. # ** Scene_Something
  155. #------------------------------------------------------------------------------
  156. #  This class is definition from All Class
  157. #==============================================================================
  158. LiTTleDRAgo::MouseGestures::ALLOWED_SCENE.each {|kelas| eval "
  159. class #{kelas}
  160.  include LiTTleDRAgo::MouseGestures
  161.  alias drg_143_upd update unless method_defined?(:drg_143_upd) ||
  162.                                 !method_defined?(:update)
  163.  def update
  164.    drg_143_upd
  165.    update_mouse_gestures
  166.  end
  167. end#"}
  168. #==============================================================================
  169. # ** Input
  170. #------------------------------------------------------------------------------
  171. #  This module performs key input processing
  172. #==============================================================================
  173. module Input
  174.   class << self
  175.     if !method_defined?(:glitch_input_keys_update)
  176.       raise "This Script needs Glitchfinder's Key Input Module and "+
  177.          "Glitchfinder's Mouse Input Module"
  178.     end
  179.   end
  180. end
  181.  
  182. $drago_mouse_gestures = true
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement