Mouse Script Version: 1.0
By: Near
Introduction
This script adds a mouse input functionality to your game,
and you can add a cursor image also. Call with: Mouse.mouse_press?
Features
Screenshots
No screens, sorry.
Demo
Working on a demo.
Script
Instructions
Most of this is self explanatory, but for those of you who
would like to know how to make a window or sprite or what-
ever clickable, here you go:
All you do is check the position of the mouse(Mouse.pos)
and make sure it is within the rectangle of your window.
Ex:
For sprites, you might use the same rectangle method, or if
you need it to be pixel perfect, you would have to ask som-
one else.
Also, for the cursor Graphic, it has to be in './Graphics/System/' folder.
The configuration is on line 6 of the first part.
The pathfinding script is not necessary, unless you are using the point
and click movement.
FAQ
Waiting for questions.
Compatibility
No known compatibility issues yet.
Credits and Thanks
I have to thank Near_Fantastica, because I looked a bit
as his module to figure how the win32apis were called.
Author's Notes
I like pancakes.
Terms and Conditions
Credit is appreciated, but not necessary.
By: Near
Introduction
This script adds a mouse input functionality to your game,
and you can add a cursor image also. Call with: Mouse.mouse_press?
Features
- Mouse Input
- Mouse graphic
- Can make virtually anything clickable.
Screenshots
No screens, sorry.
Demo
Working on a demo.
Script
Code:
Â
class Game_Character
 attr_accessor :path_finding  # Finding a path?
 attr_accessor :path_x     # X coordinate to find.
 attr_accessor :path_y     # Y coordinate to find.
 alias npath_init initialize
 alias npath_update update
 def initialize
  @path_finding = false
  @path_x = 0
  @path_y = 0
  npath_init
 end
 def update
  find_path if @path_finding
  npath_update
 end
 # Start finding a path
 def start_path(x,y)
  @path_x = x
  @path_y = y
  @path_finding = true
  return
 end
 # Execute Pathfinding
 def find_path
  if (@x == @path_x && @y == @path_y) || !passable?(@path_x,@path_y)
   clear_path
   return
  end
  if @path_x < @x && @path_y > @y
   path_lower_left
  elsif @path_x > @x && @path_y > @y
   path_lower_right
  elsif @path_x < @x && @path_y < @y
   path_upper_left
  elsif @path_x > @x && @path_y < @y
   path_upper_right
  elsif @path_y > @y
   path_down
  elsif @path_y < @y
   path_up
  elsif @path_x > @x
   path_right
  elsif @path_x < @x
   path_left
  else
   return
  end
 end
 # Clear Path
 def clear_path
  @path_finding = false
  @path_x = 0
  @path_y = 0
  return
 end
 #-------------------  Paths  ------------------#
 def path_lower_left
  if passable?(@x-1,@y+1)
   move_lower_left
  end
  return
 end
 def path_lower_right
   if passable?(@x+1,@y+1)
   move_lower_right
  end
  return
 end
 def path_upper_left
  if passable?(@x-1,@y-1)
   move_upper_left
  end
  return
 end
 def path_upper_right
  if passable?(@x+1,@y-1)
   move_upper_right
  end
  return
 end
 def path_down
  if passable?(@x,@y+1)
   move_down
  end
  return
 end
 def path_up
  if passable?(@x,@y-1)
   move_up
  end
  return
 end
 def path_right
  if passable?(@x+1,@y)
   move_right
  end
  return
 end
 def path_left
  if passable?(@x-1,@y)
   move_left
  end
  return
 end
end
Â
Â
Code:
Â
module Near
 module Library
  module WIN32
   GSM = Win32API.new('user32', 'GetSystemMetrics', 'i', 'i')
   GetCursorPos = Win32API.new('user32', 'GetCursorPos', 'p', 'i')
   ScreenToClient = Win32API.new('user32', 'ScreenToClient', %w(l p), 'i')
   GetClientRect = Win32API.new('user32', 'GetClientRect', %w(l p), 'i')
   GetPrivateString = Win32API.new('kernel32', 'GetPrivateProfileStringA', %w(p p p p l p), 'l')
   FindWindow = Win32API.new('user32', 'FindWindowA', %w(p p), 'l')
   ShowCursor = Win32API.new('user32', 'ShowCursor', 'l', 'l')
  end
 end
 module KeyCode
  Mouse_Left = 1
  Mouse_Right = 2
  Mouse_Middle = 4
 end
end
Â
Code:
Â
module Mouse
 #--------------------------------------------------------------------------
 # * Config - Customize Here!
 #--------------------------------------------------------------------------
 Icon = "Arrow6" # File must be in .\Graphics\System\<File>
 #--------------------------------------------------------------------------
 # * End of Config - Don't Customize Here!
 #--------------------------------------------------------------------------
 n = (true ? 0 : 1)
 Near::Library::WIN32::ShowCursor.call(n)
 module_function
 def self.tile
  return nil if pos == nil
  return [0,0] unless $scene.is_a?(Scene_Map)
  ox = $game_map.display_x / 4
  oy = $game_map.display_y / 4
  x = (pos[0] + ox) / 32
  y = (pos[1] + oy) / 32
  return [x, y]
 end
 def self.position
  return pos == nil ? [0, 0] : pos
 end
 def self.global_pos
  pos = [0, 0].pack('ll')
  if Near::Library::WIN32::GetCursorPos.call(pos) != 0
   return pos.unpack('ll')
  else
   return nil
  end
 end
 def self.pos
  x, y = self.screen_to_client(*Mouse.global_pos)
  width, height = self.client_size
  begin
   if (x >= 0 and y >= 0 and x < width and y < height)
    return x, y
   else
    return [0,0]
   end
  rescue
   return [0,0]
  end
 end
 def self.screen_to_client(x, y)
  return nil unless x and y
  pos = [x, y].pack('ll')
  if Near::Library::WIN32::ScreenToClient.call(self.hwnd, pos) != 0
   return pos.unpack('ll')
  else
   return nil
  end
 end
 def self.hwnd
  game_name = "\0" * 256
  Near::Library::WIN32::GetPrivateString.call('Game','Title','',game_name,255,".\\Game.ini")
  game_name.delete!("\0")
  return Near::Library::WIN32::FindWindow.call('RGSS Player',game_name)
 end
 def self.client_size
  rect = [0, 0, 0, 0].pack('l4')
  Near::Library::WIN32::GetClientRect.call(self.hwnd, rect)
  right, bottom = rect.unpack('l4')[2..3]
  return right, bottom
 end
 def self.update
  if mouse_press?
   self.check_event(tile[0],tile[1])
  end
 end
 def self.mouse_press?
   if Input.press?(Near::KeyCode::Mouse_Left)
    return true
   end
  return false
 end
 def self.right_press?
  if Input.press?(Near::KeyCode::Mouse_Right)
   return true
  end
  return false
 end
end
Â
class << Input
 #--------------------------------------------------------------------------
 # ◠Update old input calls
 #--------------------------------------------------------------------------
 alias old_update update unless $@
 def Input.update
  old_update
  $mouse.update
 end
 #--------------------------------------------------------------------------
 # ◠Update old input triggers
 #   num : A, B, C
 #--------------------------------------------------------------------------
 alias old_trigger? trigger? unless $@
 def Input.trigger?(num)
  return old_trigger?(num) if Mouse.pos == nil
  case num
  when Input::B
   return (old_trigger?(num) or Mouse.right_press?)
  when Input::C
   return (old_trigger?(num) or Mouse.mouse_press?)
  else
   return old_trigger?(num)
  end
 end
end
Â
Â
class Sprite_Mouse < Sprite
 #--------------------------------------------------------------------------
 # ◠Initialize the mouse
 #--------------------------------------------------------------------------
 def initialize
  super
  self.bitmap = Cache.system(Mouse::Icon)
  @true_x = 0
  @true_y = 0
  self.z = 10001
  self.ox, self.oy = [16, 0]
  self.visible = false
  self.src_rect.set(0, 0, 32, 32)
  update
 end
 #--------------------------------------------------------------------------
 # ◠Dispose of the mouse
 #--------------------------------------------------------------------------
 def dispose
  if self.bitmap != nil
   self.bitmap.dispose
  end
  super
 end
 #--------------------------------------------------------------------------
 # ◠Update the mouse
 #--------------------------------------------------------------------------
 def update
  super
  #$mouse_icon = "Arrow6" if $mouse_icon == nil
  self.bitmap = Cache.system(Mouse::Icon)  Â
  self.visible = true if self.visible == false and $scene != nil
  self.x = Mouse.pos[0]
  self.y = Mouse.pos[1]
  @true_x = Mouse.tile[0]
  @true_y = Mouse.tile[1]
  if Mouse.mouse_press?
   self.src_rect.set(0, 0, 32, 32)  Â
  end
  return
 end
Â
end
Â
$mouse = Sprite_Mouse.new
Â
Â
Code:
Â
class Window_Selectable < Window_Base
 alias mouse_initialize initialize
 def initialize(x, y, width, height, spacing = 32)
  mouse_initialize(x, y, width, height, spacing)
  @mouse = false
  @scroll_wait = 0
 end
 alias mouse_update update
 def update
  mouse_update
  mouse_operation if self.active || @mouse
 end
 def mouse_operation
  mx = Mouse.pos[0] - (self.x - self.ox + 16)
  my = Mouse.pos[1] - (self.y - self.oy + 16)
  width = self.width / @column_max - 32
  height = 32
Â
  for index in 0...@item_max
   x = index % @column_max * (width + 32)
   y = index / @column_max * 32
   if mx > x and
     mx < x + width and
     my > y and
     my < y + height
    mouse_cursor(index)
    break
   end
  end
 end
 def mouse_cursor(index)
  return if @index == index
  @scroll_wait -= 1 if @scroll_wait > 0
  row1 = @index / @column_max
  row2 = index / @column_max
  bottom = self.top_row + (self.page_row_max - 1)
  if row1 == self.top_row and row2 < self.top_row
   return if @scroll_wait > 0
   @index = [@index - @column_max, 0].max
   @scroll_wait = 4
  elsif row1 == bottom and row2 > bottom
   return if @scroll_wait > 0
   @index = [@index + @column_max, @item_max - 1].min
   @scroll_wait = 4
  else
   @index = index
  end
  Sound.play_cursor
 end
end
Â
class Window_MenuStatus < Window_Selectable
 def mouse_operation Â
  return if @index < 0
  mx = Mouse.pos[0] - (self.x - self.ox + 16)
  my = Mouse.pos[1] - (self.y - self.oy + 16)
  x = 0
  width = self.width - 32
  height = 96
  for index in 0...@item_max
   y = index * 116
   if mx > x and
     mx < x + width and
     my > y and
     my < y + height
    mouse_cursor(index)
    break
   end
  end
 end
end
Â
class Window_Target < Window_Selectable
 def mouse_operation
  return if @index <= -1
  mx = Mouse.pos[0] - (self.x - self.ox + 16)
  my = Mouse.pos[1] - (self.y - self.oy + 16)
  x = 0
  width = self.width - 32
  height = 96
  for index in 0...@item_max
   y = index * 116
   if mx > x and
     mx < x + width and
     my > y and
     my < y + height
    mouse_cursor(index)
    break
   end
  end
 end
end
Â
class Window_NameInput < Window_Base
 alias mouse_update update
 def update
  mouse_update
  mouse_operation if self.active
 end
 def mouse_operation
  last_index = @index
  mx = Mouse.pos[0] - (self.x - self.ox + 16)
  my = Mouse.pos[1] - (self.y - self.oy + 16)
  width = 28
  height = 32
  for index in 0...180
   x = 4 + index / 5 / 9 * 152 + index % 5 * 28
   y = index / 5 % 9 * 32
   if mx > x and
     mx < x + width and
     my > y and
     my < y + height
    @index = index
    break
   end
  end
  x = 544
  y = 9 * 32
  width = 64
  if mx > x and
    mx < x + width and
    my > y and
    my < y + height
   @index = 180
  end
  Sound.play_cursor unless @index == index
 end
end
Â
class Window_Message < Window_Selectable
 def mouse_operation
  mx = Mouse.pos[0] - (self.x - self.ox + 16)
  my = Mouse.pos[1] - (self.y - self.oy + 16)
  x = 8
  width = 128
  height = 32
  for index in 0...@item_max
   y = ($game_temp.choice_start + index) * 32
    Â
   if mx > x and mx < x + width and my > y and my < y + height
    mouse_cursor(index)
    break
   end
  end
 end
end
Â
class Window_PartyCommand < Window_Selectable
 def mouse_operation
  mx = Mouse.pos[0] - (self.x - self.ox + 16)
  my = Mouse.pos[1] - (self.y - self.oy + 16)
  y = 0
  width = 128
  height = 32
  for index in 0...@item_max
   x = 160 + index * 160
   if mx > x and
     mx < x + width and
     my > y and
     my < y + height
    mouse_cursor(index)
    break
   end
  end
 end
end
Â
class Window_MenuPosition < Window_Selectable
 def mouse_operation
  mx = Mouse.pos[0] - (self.x - self.ox + 16)
  my = Mouse.pos[1] - (self.y - self.oy + 16)
  y = 0
  width = self.contents.width / @item_max - 10
  height = 32
  for index in 0...@item_max
   x = self.contents.width / (@item_max) * index + 4
   if mx > x and
     mx < x + width and
     my > y and
     my < y + height
    mouse_cursor(index)
    break
   end
  end
 end
end
Â
class Scene_File
 alias mouse_update update
 def update
  mouse_update
  save = false
  mx, my = Mouse.pos
  x = 0
  width = (save ? 160 : 640)
  height = 90
  for index in 0...4
   y = 56 + index % 4 * 104
   if mx > x and
     mx < x + width and
     my > y and
     my < y + height
    break if @index == index
    @savefile_windows[@index].selected = false
    @index = index
    @savefile_windows[@index].selected = true
    Sound.play_cursor
    break
   end
  end
 end
end
Â
Â
Code:
Â
class Game_Player
 alias mouse_update update
 def update
  if Mouse.pos != nil and Mouse.tile != nil
  if Mouse.mouse_press?
   x = Mouse.tile[0]
   y = Mouse.tile[1]
   if passable?(x,y)
    start_path(x,y)
   end
  end
  end
  mouse_update
 end
end
Â
Most of this is self explanatory, but for those of you who
would like to know how to make a window or sprite or what-
ever clickable, here you go:
All you do is check the position of the mouse(Mouse.pos)
and make sure it is within the rectangle of your window.
Ex:
Code:
Â
return false if Mouse.pos[0] > @window.x + @window.width || Mouse.pos[1] > @window.y + @window.height
return false if Mouse.pos[0] < @window.x || Mouse.pos[1] < @window.y
return true
Â
For sprites, you might use the same rectangle method, or if
you need it to be pixel perfect, you would have to ask som-
one else.
Also, for the cursor Graphic, it has to be in './Graphics/System/' folder.
The configuration is on line 6 of the first part.
The pathfinding script is not necessary, unless you are using the point
and click movement.
FAQ
Waiting for questions.
Compatibility
No known compatibility issues yet.
Credits and Thanks
I have to thank Near_Fantastica, because I looked a bit
as his module to figure how the win32apis were called.
Author's Notes
I like pancakes.
Terms and Conditions
Credit is appreciated, but not necessary.