Advertisement
leequangson

Mouse Controller

Aug 12th, 2013
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 12.07 KB | None | 0 0
  1. #:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=
  2. # Mouse Controller by Blizzard
  3. # Version: 2.0b
  4. # Type: Custom Input System
  5. # Date: 9.10.2009
  6. # Date v2.0b: 22.7.2010
  7. #:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=
  8. #  
  9. #  This work is protected by the following license:
  10. # #----------------------------------------------------------------------------
  11. # #  
  12. # #  Creative Commons - Attribution-NonCommercial-ShareAlike 3.0 Unported
  13. # #  ( http://creativecommons.org/licenses/by-nc-sa/3.0/ )
  14. # #  
  15. # #  You are free:
  16. # #  
  17. # #  to Share - to copy, distribute and transmit the work
  18. # #  to Remix - to adapt the work
  19. # #  
  20. # #  Under the following conditions:
  21. # #  
  22. # #  Attribution. You must attribute the work in the manner specified by the
  23. # #  author or licensor (but not in any way that suggests that they endorse you
  24. # #  or your use of the work).
  25. # #  
  26. # #  Noncommercial. You may not use this work for commercial purposes.
  27. # #  
  28. # #  Share alike. If you alter, transform, or build upon this work, you may
  29. # #  distribute the resulting work only under the same or similar license to
  30. # #  this one.
  31. # #  
  32. # #  - For any reuse or distribution, you must make clear to others the license
  33. # #    terms of this work. The best way to do this is with a link to this web
  34. # #    page.
  35. # #  
  36. # #  - Any of the above conditions can be waived if you get permission from the
  37. # #    copyright holder.
  38. # #  
  39. # #  - Nothing in this license impairs or restricts the author's moral rights.
  40. # #  
  41. # #----------------------------------------------------------------------------
  42. #
  43. #:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=
  44. #
  45. # Compatibility:
  46. #
  47. #   90% compatible with SDK v1.x. 80% compatible with SDK v2.x. May cause
  48. #   incompatibility issues with other custom input Systems. Works with "Custom
  49. #   Game Controls" from Tons of Add-ons and Blizz-ABS's custom controls.
  50. #   This script is not meant to be used as a standalone but rather in
  51. #   combination with special menus that are properly adapted to support a mouse
  52. #   controller system.
  53. #  
  54. #  
  55. # Features:
  56. #
  57. #   - fully automated mouse control in game
  58. #   - can be enhanced with "Custom Game Controls" from Tons of Add-ons
  59. #   - can be enhanced with "Blizz-ABS Controls"
  60. #   - can be enhanced with "RMX-OS"
  61. #
  62. # new in 2.0b:
  63. #
  64. #   - added option to hide Windows' cursor
  65. #   - added possibility to hide and show the ingame cursor during the game
  66. #   - added possibility to change the cursor icon
  67. #   - added several new options
  68. #   - optimized
  69. #
  70. #
  71. # Instructions:
  72. #
  73. # - Explanation:
  74. #
  75. #   This script can work as a stand-alone for window option selections. To be
  76. #   able to use the mouse buttons, you need a custom Input module. The
  77. #   supported systems are "Custom Game Controls" from Tons of Add-ons,
  78. #   Blizz-ABS Custom Controls and RMX-OS Custom Controls. This script will
  79. #   automatically detect and apply the custom input modules' configuration
  80. #   which is optional.
  81. #  
  82. # - Configuration:
  83. #
  84. #   MOUSE_ICON          - the default filename of the icon located in the
  85. #                         Graphics/Pictures folder
  86. #   APPLY_BORDERS       - defines whether the ingame cursor can go beyond the
  87. #                         game window borders
  88. #   WINDOW_WIDTH        - defines the window width, required only when using
  89. #                         APPLY_BORDER
  90. #   WINDOW_HEIGHT       - defines the window height, required only when using
  91. #                         APPLY_BORDER
  92. #   HIDE_WINDOWS_CURSOR - hides the Windows Cursor on the window by default
  93. #   AUTO_CONFIGURE      - when using "Custom Game Controls" from Tons of
  94. #                         Add-ons, Blizz-ABS or RMX-OS, this option will
  95. #                         automatically add the left mouse button as
  96. #                         confirmation button
  97. #  
  98. # - Script Calls:
  99. #  
  100. #   You can use a few script calls to manipulate the cursor. Keep in mind that
  101. #   these changes are not being saved with the save file.
  102. #  
  103. #   To hide the ingame Mouse Cursor, use following call.
  104. #  
  105. #     $mouse.hide
  106. #  
  107. #   To show the ingame Mouse Cursor, use following call.
  108. #  
  109. #     $mouse.show
  110. #  
  111. #   To change the cursor image, use following call. Make sure your image is
  112. #  
  113. #     $mouse.set_cursor('IMAGE_NAME')
  114. #  
  115. #  
  116. # Additional Information:
  117. #  
  118. #   Even though there is an API call to determine the size of the window, API
  119. #   calls are CPU expensive so the values for the window size need to be
  120. #   configured manually in this script.
  121. #  
  122. #
  123. # If you find any bugs, please report them here:
  124. # http://forum.chaos-project.com
  125. #:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=
  126.  
  127. $mouse_controller = 2.0
  128.  
  129. #===============================================================================
  130. # Mouse
  131. #===============================================================================
  132.  
  133. class Mouse
  134.  
  135. #::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
  136. # START Configuration
  137. #::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
  138.   MOUSE_ICON = 'cursor'
  139.   AUTO_CONFIGURE = true
  140.   APPLY_BORDERS = true
  141.   WINDOW_WIDTH = 640
  142.   WINDOW_HEIGHT = 480
  143.   HIDE_WINDOWS_CURSOR = false
  144. #::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
  145. # END Configuration
  146. #::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
  147.  
  148.   if HIDE_WINDOWS_CURSOR
  149.     Win32API.new('user32', 'ShowCursor', 'i', 'i').call(0)
  150.   end
  151.  
  152.   SCREEN_TO_CLIENT = Win32API.new('user32', 'ScreenToClient', %w(l p), 'i')
  153.   READ_INI = Win32API.new('kernel32', 'GetPrivateProfileStringA', %w(p p p p l p), 'l')
  154.   FIND_WINDOW = Win32API.new('user32', 'FindWindowA', %w(p p), 'l')
  155.   CURSOR_POSITION = Win32API.new('user32', 'GetCursorPos', 'p', 'i')
  156.  
  157.   def initialize
  158.     @cursor = Sprite.new
  159.     @cursor.z = 1000000
  160.     self.set_cursor(MOUSE_ICON)
  161.     update
  162.   end
  163.  
  164.   def update
  165.     @cursor.x, @cursor.y = self.position
  166.   end
  167.  
  168.   def x
  169.     return @cursor.x
  170.   end
  171.  
  172.   def y
  173.     return @cursor.y
  174.   end
  175.  
  176.   def position
  177.     x, y = self.get_client_position
  178.     if APPLY_BORDERS
  179.       if x < 0
  180.         x = 0
  181.       elsif x >= WINDOW_WIDTH
  182.         x = WINDOW_WIDTH - 1
  183.       end
  184.       if y < 0
  185.         y = 0
  186.       elsif y >= WINDOW_HEIGHT
  187.         y = WINDOW_HEIGHT - 1
  188.       end
  189.     end
  190.     return x, y
  191.   end
  192.  
  193.   def get_client_position
  194.     pos = [0, 0].pack('ll')
  195.     CURSOR_POSITION.call(pos)
  196.     SCREEN_TO_CLIENT.call(WINDOW, pos)
  197.     return pos.unpack('ll')
  198.   end
  199.  
  200.   def set_cursor(image)
  201.     @cursor.bitmap = RPG::Cache.picture(image)
  202.   end
  203.  
  204.   def show
  205.     @cursor.visible = true
  206.   end
  207.  
  208.   def hide
  209.     @cursor.visible = false
  210.   end
  211.  
  212.   def self.find_window
  213.     game_name = "\0" * 256
  214.     READ_INI.call('Game', 'Title', '', game_name, 255, '.\\Game.ini')
  215.     game_name.delete!("\0")
  216.     return FIND_WINDOW.call('RGSS Player', game_name)
  217.   end
  218.  
  219.   WINDOW = self.find_window
  220.  
  221. end
  222.  
  223. $mouse = Mouse.new
  224.  
  225. #==============================================================================
  226. # module Input
  227. #==============================================================================
  228.  
  229. module Input
  230.  
  231.   class << Input
  232.     alias update_mousecontroller_later update
  233.   end
  234.  
  235.   def self.update
  236.     $mouse.update
  237.     update_mousecontroller_later
  238.   end
  239.  
  240.   if Mouse::AUTO_CONFIGURE
  241.     if $BlizzABS
  242.       C.push(Input::Key['Mouse Left']) if !C.include?(Input::Key['Mouse Left'])
  243.       if !Attack.include?(Input::Key['Mouse Right'])
  244.         Attack.push(Input::Key['Mouse Right'])
  245.       end
  246.     elsif $tons_version != nil && $tons_version >= 6.4 &&
  247.         TONS_OF_ADDONS::CUSTOM_CONTROLS || defined?(RMXOS)
  248.       C.push(Input::Key['Mouse Left']) if !C.include?(Input::Key['Mouse Left'])
  249.     end
  250.   end
  251.  
  252. end
  253.  
  254. #===============================================================================
  255. # Rect
  256. #===============================================================================
  257.  
  258. class Rect
  259.  
  260.   def covers?(x, y)
  261.     return !(x < self.x || x >= self.x + self.width ||
  262.         y < self.y || y >= self.y + self.height)
  263.   end
  264.  
  265. end
  266.  
  267. #===============================================================================
  268. # Sprite
  269. #===============================================================================
  270.  
  271. class Sprite
  272.  
  273.   def mouse_in_area?
  274.     return false if self.bitmap == nil
  275.     return ($mouse.x >= self.x && $mouse.x < self.x + self.src_rect.width &&
  276.         $mouse.y >= self.y && $mouse.y < self.y + self.src_rect.height)
  277.   end
  278.  
  279. end
  280.  
  281. #===============================================================================
  282. # Window_Base
  283. #===============================================================================
  284.  
  285. class Window_Base
  286.  
  287.   def mouse_in_area?
  288.     return ($mouse.x >= self.x && $mouse.x < self.x + self.width &&
  289.         $mouse.y >= self.y && $mouse.y < self.y + self.height)
  290.   end
  291.  
  292.   def mouse_in_inner_area?
  293.     return ($mouse.x >= self.x + 16 && $mouse.x < self.x + self.width - 16 &&
  294.         $mouse.y >= self.y + 16 && $mouse.y < self.y + self.height - 16)
  295.   end
  296.  
  297. end
  298.  
  299. #===============================================================================
  300. # Window_Selectable
  301. #===============================================================================
  302.  
  303. class Window_Selectable
  304.  
  305.   alias contents_is_mousecontroller_later contents=
  306.   def contents=(bitmap)
  307.     contents_is_mousecontroller_later(bitmap)
  308.     begin
  309.       update_selections
  310.       update_mouse if self.active
  311.     rescue
  312.     end
  313.   end
  314.  
  315.   alias index_is_mousecontroller_later index=
  316.   def index=(value)
  317.     index_is_mousecontroller_later(value)
  318.     update_selections
  319.   end
  320.  
  321.   alias active_is_mousecontroller_later active=
  322.   def active=(value)
  323.     active_is_mousecontroller_later(value)
  324.     update_cursor_rect
  325.   end
  326.  
  327.   def update_selections
  328.     @selections = []
  329.     index, ox, oy = self.index, self.ox, self.oy
  330.     (0...@item_max).each {|i|
  331.         @index = i
  332.         update_cursor_rect
  333.         rect = self.cursor_rect.clone
  334.         rect.x += self.ox
  335.         rect.y += self.oy
  336.         @selections.push(rect)}
  337.     @index, self.ox, self.oy = index, ox, oy
  338.     self.cursor_rect.empty
  339.   end
  340.  
  341.   alias update_mousecontroller_later update
  342.   def update
  343.     update_mouse if self.active
  344.     update_mousecontroller_later
  345.   end
  346.  
  347.   def update_mouse
  348.     if self.mouse_in_inner_area?
  349.       update_mouse_selection
  350.       return
  351.     end
  352.     self.index = -1
  353.     if self.contents != nil && @selections.size > 0 && self.mouse_in_area?
  354.       update_mouse_scrolling
  355.     end
  356.   end
  357.  
  358.   def update_mouse_selection
  359.     update_selections if @selections.size != @item_max
  360.     @selections.each_index {|i|
  361.         if @selections[i].covers?($mouse.x - self.x - 16 + self.ox,
  362.             $mouse.y - self.y - 16 + self.oy)
  363.           self.index = i if self.index != i
  364.           return
  365.         end}
  366.     self.index = -1
  367.   end
  368.  
  369.   def update_mouse_scrolling
  370.     if Input.repeat?(Input::C)
  371.       if $mouse.x < self.x + 16
  372.         if self.ox > 0
  373.           $game_system.se_play($data_system.cursor_se)
  374.           self.ox -= @selections[0].width
  375.           self.ox = 0 if self.ox < 0
  376.         end
  377.       elsif $mouse.x >= self.x + self.width - 16
  378.         max_ox = self.contents.width - self.width + 32
  379.         if self.ox <= max_ox
  380.           $game_system.se_play($data_system.cursor_se)
  381.           self.ox += @selections[0].width
  382.           self.ox = max_ox if self.ox >= max_ox
  383.         end
  384.       elsif $mouse.y < self.y + 16
  385.         if self.oy > 0
  386.           $game_system.se_play($data_system.cursor_se)
  387.           self.oy -= @selections[0].height
  388.           self.oy = 0 if self.oy < 0
  389.         end
  390.       elsif $mouse.y >= self.y + self.height - 16
  391.         max_oy = self.contents.height - self.height + 32
  392.         if self.oy <= max_oy
  393.           $game_system.se_play($data_system.cursor_se)
  394.           self.oy += @selections[0].height
  395.           self.oy = max_oy if self.oy >= max_oy
  396.         end
  397.       end
  398.     end
  399.   end
  400.  
  401. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement