123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
# Requires module Mouse
# Requires module Input
#===============================================================================
# Mouse input class written by Luka S.J. to enable the usage of the native
# mouse module in Essentials.
# Please give credit if used.
#===============================================================================
class Game_Mouse
attr_reader :visible
attr_reader :x
attr_reader :y
# replace nil with a valid path to a graphics location to display a sprite
# for the mouse
@@graphics_path = nil
# starts up the mouse and determines initial co-ordinate
def initialize
@mouse = Mouse
@position = @mouse.getMousePos
@cursor = Win32API.new("user32", "ShowCursor", "i", "i" )
@visible = false
if @position.nil?
@x = 0
@y = 0
else
@x = @position[0]
@y = @position[1]
end
@static_x = @x
@static_y = @y
@object_ox = nil
@object_oy = nil
# used to make on screen mouse sprite (if a graphics path is defined)
@viewport = Viewport.new(0,0,Graphics.width,Graphics.height)
@viewport.z = 0x3FFFFFFF
@sprite = Sprite.new(@viewport)
if !@@graphics_path.nil?
@sprite.bitmap = BitmapCache.load_bitmap(@@graphics_path)
self.show
end
@sprite.visible = @visible
@sprite.x = @x
@sprite.y = @y
# ===================================================================
end
# updates the mouse (update placed in Input.update)
def update
@position = @mouse.getMousePos
if !@position.nil?
@x = @position[0]
@y = @position[1]
else
@x = -5000
@y = -5000
end
@sprite.visible = @visible
@sprite.x = @x
@sprite.y = @y
end
# manipulation of the visibility of the mouse sprite
def show
@cursor.call(0)
@visible=true
end
def hide
@cursor.call(1)
@visible=false
end
# checks if mouse is over a sprite (can define custom width and height)
def over?(object=nil,width=-1,height=-1)
return false if object.nil?
params=self.getObjectParams(object)
x=params[0]
y=params[1]
width=params[2] if width < 0
height=params[3] if height < 0
return true if @x >= x && @x <= (x + width) and @y >= y && @y <= (y + height)
return false
end
# special method to check whether the mouse is over sprites with special shapes
def overPixel?(sprite)
return false if !sprite.bitmap
bitmap=sprite.bitmap
return false if !self.over?(sprite)
bx=@x-sprite.x
by=@y-sprite.y
if defined?(sprite.viewport) && sprite.viewport
bx-=sprite.viewport.rect.x
by-=sprite.viewport.rect.y
end
bx+=sprite.src_rect.x
by+=sprite.src_rect.y
pixel=bitmap.get_pixel(bx,by)
return true if pixel.alpha>0
return false
end
# checks if mouse is left clicking a sprite (can define custom width and height)
def leftClick?(object=nil,width=-1,height=-1)
if object.nil?
return Input.triggerex?(Input::Mouse_Left)
else
return (self.over?(object,width,height) && Input.triggerex?(Input::Mouse_Left))
end
end
# checks if mouse pressed left a sprite (can define custom width and height)
def leftPressed?(object=nil,width=-1,height=-1)
if object.nil?
return Input.releaseex?(Input::Mouse_Left)
else
return (self.over?(object,width,height) && Input.releaseex?(Input::Mouse_Left))
end
end
# checks if mouse is right clicking a sprite (can define custom width and height)
def rightClick?(object=nil,width=-1,height=-1)
if object.nil?
return Input.triggerex?(Input::Mouse_Right)
else
return (self.over?(object,width,height) && Input.triggerex?(Input::Mouse_Right))
end
end
# checks if mouse is left clicking a sprite / continuous (can define custom width and height)
def leftPress?(object=nil,width=-1,height=-1)
if object.nil?
return Input.pressed(Input::Mouse_Left)
else
return (self.over?(object,width,height) && Input.pressed(Input::Mouse_Left))
end
end
# checks if mouse is right clicking a sprite / continuous (can define custom width and height)
def rightPress?(object=nil,width=-1,height=-1)
if object.nil?
return Input.pressed(Input::Mouse_Left)
else
return (self.over?(object,width,height) && Input.pressed(Input::Mouse_Right))
end
end
# checks if the mouse is in a certain area of the App window
def inArea?(x,y,width,height)
rect=Rect.new(x,y,width,height)
return self.over?(rect)
end
# checks if the mouse is clicking in a certain area of the App window
# click can either be "left" or "right", to specify which mouse button is clicked
def inAreaClick?(x,y,width,height,click="left")
case click
when "left"
return (self.inArea?(x,y,width,height) && Input.triggerex?(Input::Mouse_Left))
when "right"
return (self.inArea?(x,y,width,height) && Input.triggerex?(Input::Mouse_Right))
else
return false
end
end
# checks if the mouse is pressing in a certain area of the App window
# click can either be "left" or "right", to specify which mouse button is pressed
def inAreaPress?(x,y,width,height,click="left")
case click
when "left"
return (self.inArea?(x,y,width,height) && Input.pressed(Input::Mouse_Left))
when "right"
return (self.inArea?(x,y,width,height) && Input.pressed(Input::Mouse_Right))
else
return false
end
end
# retained for compatibility
def inAreaLeft?(x,y,width,height)
return inAreaPress?(x,y,width,height,"left")
end
def inAreaRight?(x,y,width,height)
return inAreaPress?(x,y,width,height,"right")
end
# checks if the mouse is idle/ not moving around
def isStatic?
ret=false
ret=true if @static_x==@x && @static_y==@y
if !(@static_x==@x) or !(@static_y==@y)
@static_x=@x
@static_y=@y
end
return ret
end
# moves a targeted object with the mouse.x when left clicked (range of movement can be specified in terms of position and dimensions in the App window)
def drag_x(object,limit_x=nil,limit_width=nil)\
return false if !defined?(object.x)
if self.leftPress?(object)
if @object_ox.nil?
@object_ox = @x - object.x
end
object.x = @x - @object_ox
object.x = limit_x if limit_x && object.xlimit_width
else
@object_ox=nil
end
end
# moves a targeted object with the mouse.y when left clicked (range of movement can be specified in terms of position and dimensions in the App window)
def drag_y(object,limit_y=nil,limit_height=nil)
return false if !defined?(object.y)
if self.leftPress?(object)
if @object_oy.nil?
@object_oy = @y - object.y
end
object.y = @y - @object_oy
object.y = limit_y if limit_y && object.ylimit_height
else
@object_oy=nil
end
end
# moves a targeted object with the mouse when left clicked (range of movement can be specified in terms of position and dimensions in the App window)
def drag_xy(object,limit_x=nil,limit_y=nil,limit_width=nil,limit_height=nil)
return false if !defined?(object.x) or !defined?(object.y)
if self.leftPress?(object)
if @object_ox.nil?
@object_ox = @x - object.x
end
if @object_oy.nil?
@object_oy = @y - object.y
end
object.x = @x - @object_ox
object.x = limit_x if limit_x && object.xlimit_width
object.y = @y - @object_oy
object.y = limit_y if limit_y && object.ylimit_height
else
@object_ox=nil
@object_oy=nil
end
end
def getObjectParams(object)
params=[0,0,0,0]
if object.is_a?(Sprite)
params[0]=(object.x)
params[1]=(object.y)
if defined?(object.viewport) && object.viewport
params[0]+=object.viewport.rect.x
params[1]+=object.viewport.rect.y
end
params[2]=(object.bitmap.width*object.zoom_x) if object.bitmap
params[3]=(object.bitmap.height*object.zoom_y) if object.bitmap
if defined?(object.src_rect)
params[2]=(object.src_rect.width*object.zoom_x) if object.bitmap && object.src_rect.width != object.bitmap.width
params[3]=(object.src_rect.height*object.zoom_y) if object.bitmap && object.src_rect.height != object.bitmap.height
end
elsif object.is_a?(Viewport)
params=[object.rect.x,object.rect.y,object.rect.width,object.rect.height]
else
params[0]=(object.x) if object.x
params[1]=(object.y) if object.y
if defined?(object.viewport) && object.viewport
params[0]+=object.viewport.rect.x
params[1]+=object.viewport.rect.y
end
params[2]=(object.width) if object.width
params[3]=(object.height) if object.height
end
return params
end
end
#===============================================================================
# Initializes the Game_Mouse class
#===============================================================================
$mouse = Game_Mouse.new
#===============================================================================
# Mouse input methods for the Input module
#===============================================================================
module Input
Mouse_Left = 1
Mouse_Right = 2
Mouse_Middle = 4
class << Input
alias update_org update
end
def self.update
$mouse.update if defined?($mouse) && $mouse
update_org
end
def self.pressed(key)
return true unless Win32API.new("user32","GetKeyState",['i'],'i').call(key).between?(0, 1)
return false
end
end