#==============================================================================
# ** Mouse Input Module (Revised)
#------------------------------------------------------------------------------
# by DerVVulfman
# version 1.2
# 08-18-2007
#------------------------------------------------------------------------------
# Based on...
# Mouse Input Module
# by Near Fantastica
#------------------------------------------------------------------------------
# Set_Pos feature by
# Freakboy
#------------------------------------------------------------------------------
#
# THE CALLS:
#
# Mouse.click?
# This returns a true/false value when you test whether a button is clicked.
# The values you pass are 1 (for the left mouse button), 2 (for the right) or
# 3 (for the middle button).
#
# Mouse.press?
# This returns a true/false value when you test whether a button is pressed
# and kept depressed. The values you pass are 1 (for the left mouse button),
# 2 (for the right mouse button), or 3 (for the middle).
#
# Mouse.pixels
# This returns the mouse's screen coordinates in pixels. Based on a screen
# with a 640x480 dimension, this returns an array of the mouse's position in
# index values. Calling Mouse.pixels returns both x & y positions in a sin-
# gle string, but calling Mouse.pixels[0] returns the x position (0-639) and
# calling Mouse.pixels[1] returns the y position (0-439). If the mouse is
# outside of the game's window region, this call returns nil.
#
# Mouse.tiles
# This returns the mouse's screen coordinates in map tiles. Based on the
# system's 20x15 tile size, this returns it in index values (a 0-19 width &
# a 0-14 height). This functions the same manner as Mouse.pixels.
#
# Mouse.set_pos
# This allows you to forcefully position the mouse at an x/y position within
# the game screen by pixel coordinates. Given the game's normal screen width
# of 640x480, adding: Mouse.set_pos(320,240) should position the mouse dead
# center of the gaming window.
#
# Mouse.update
# Add this routine into your update routines to update the mouse position.
# It must be called otherwise you won't get valid mouse coordinates.
#
#==============================================================================
module Mouse
@mouse_menu = 0
#--------------------------------------------------------------------------
# * Mouse Click
# button : button
#--------------------------------------------------------------------------
def Mouse.click?(button)
return true if @keys.include?(button)
return false
end
#--------------------------------------------------------------------------
# * Mouse Pressed
# button : button
#--------------------------------------------------------------------------
def Mouse.press?(button)
return true if @press.include?(button)
return false
end
#--------------------------------------------------------------------------
# * Mouse Pressed
# button : button
#--------------------------------------------------------------------------
def Mouse.area?(x, y, width=32, height=32)
return false if @pos == nil
return true if @pos[0] >= x and @pos[0] <= (x+width) and @pos[1] >= y and @pos[1] <= (y+height)
return false
end
#--------------------------------------------------------------------------
# * Mouse Pixel Position
#--------------------------------------------------------------------------
def Mouse.pixels
return @pos == nil ? [0, 0] : @pos
end
#--------------------------------------------------------------------------
# * Mouse Tile Position
#--------------------------------------------------------------------------
def Mouse.tiles
return nil if @pos == nil
x = @pos[0] / 32
y = @pos[1] / 32
return [x, y]
end
#--------------------------------------------------------------------------
# * Set Mouse Position
#--------------------------------------------------------------------------
def Mouse.set_pos(x_pos=0, y_pos=0)
width, height = Mouse.client_size
if (x_pos.between?(0, width) && y_pos.between?(0, height))
x = Mouse.client_pos[0] + x_pos; y = Mouse.client_pos[1] + y_pos
Win32API.new('user32', 'SetCursorPos', 'NN', 'N').call(x, y)
end
end
#--------------------------------------------------------------------------
# * Mouse Update
#--------------------------------------------------------------------------
def Mouse.update
@pos = Mouse.pos
@keys, @press = [], []
@keys.push(1) if Win32API.new("user32","GetAsyncKeyState",['i'],'i').call(1) & 0X01 == 1
@keys.push(2) if Win32API.new("user32","GetAsyncKeyState",['i'],'i').call(2) & 0X01 == 1
@keys.push(3) if Win32API.new("user32","GetAsyncKeyState",['i'],'i').call(4) & 0X01 == 1
@press.push(1) if Win32API.new("user32","GetKeyState",['i'],'i').call(1) & 0X01 == 1
@press.push(2) if Win32API.new("user32","GetKeyState",['i'],'i').call(2) & 0X01 == 1
@press.push(3) if Win32API.new("user32","GetKeyState",['i'],'i').call(4) & 0X01 == 1
end
#--------------------------------------------------------------------------
# * Automatic functions below
#--------------------------------------------------------------------------
#
#--------------------------------------------------------------------------
# * Obtain Mouse position in screen
#--------------------------------------------------------------------------
def Mouse.global_pos
pos = [0, 0].pack('ll')
if Win32API.new('user32', 'GetCursorPos', 'p', 'i').call(pos) != 0
return pos.unpack('ll')
else
return nil
end
end
#--------------------------------------------------------------------------
# * Return Screen mouse position within game window
#--------------------------------------------------------------------------
def Mouse.pos
x, y = Mouse.screen_to_client(*Mouse.global_pos)
width, height = Mouse.client_size
begin
if (x >= 0 and y >= 0 and x < width and y < height)
return x, y
else
return nil
end
rescue
return nil
end
end
#--------------------------------------------------------------------------
# * Pass Screen to Game System
#--------------------------------------------------------------------------
def Mouse.screen_to_client(x, y)
return nil unless x and y
pos = [x, y].pack('ll')
if Win32API.new('user32', 'ScreenToClient', %w(l p), 'i').call(Mouse.hwnd, pos) != 0
return pos.unpack('ll')
else
return nil
end
end
#--------------------------------------------------------------------------
# * Get Screen Window Handle
#--------------------------------------------------------------------------
def Mouse.hwnd
game_name = "\0" * 256
Win32API.new('kernel32', 'GetPrivateProfileStringA', %w(p p p p l p), 'l').call('Game','Title','',game_name,255,".\\Game.ini")
game_name.delete!("\0")
return Win32API.new('user32', 'FindWindowA', %w(p p), 'l').call('RGSS Player',game_name)
end
#--------------------------------------------------------------------------
# * Get Game Window Size
#--------------------------------------------------------------------------
def Mouse.client_size
rect = [0, 0, 0, 0].pack('l4')
Win32API.new('user32', 'GetClientRect', %w(l p), 'i').call(Mouse.hwnd, rect)
right, bottom = rect.unpack('l4')[2..3]
return right, bottom
end
#--------------------------------------------------------------------------
# * Get Window Position (RGSS Player)
#--------------------------------------------------------------------------
def Mouse.client_pos
rect = [0, 0, 0, 0].pack('l4')
Win32API.new('user32', 'GetWindowRect', %w(l p), 'i').call(Mouse.hwnd, rect)
left, upper = rect.unpack('l4')[0..1]
return left+4, upper+30
end
end