Envision, Create, Share

Welcome to HBGames, a leading amateur game development forum and Discord server. All are welcome, and amongst our ranks you will find experts in their field from all aspects of video game design and development.

Mouse control

Credits to Cybersam and Astro_mech!
No need to credit me. Create a new script above all scripts adn paste the below code:
Code:
#==============================================================================
#
# Mouse Script v1  created by: Cybersam, edited by Astro_mech
#
#==============================================================================
module Mouse
  $mx = $my = 0
  gsm = Win32API.new('user32', 'GetSystemMetrics', 'i', 'i')

  @cursor_pos = Win32API.new('user32', 'GetCursorPos', 'p', 'i')
  module_function

  def mouse_global_pos
    pos = [0, 0].pack('ll')
    if @cursor_pos.call(pos) != 0
      return pos.unpack('ll')
    else
      return nil
    end
  end

  def mouse_pos(catch_anywhere = false)
    x, y = screen_to_client(*mouse_global_pos)
    width, height = client_size
    if catch_anywhere or (x >= 0 and y >= 0 and x < width and y < height)
      return $mx = x, $my = y
    else
      return $mx, $my
    end
  end
  
  def del
    if @oldcursor == nil
      return
    else
      @SetClassLong.call(handel ,-12, @oldcursor)
      @oldcursor = nil
    end
  end
  
end

$scr2cli = Win32API.new('user32', 'ScreenToClient', %w(l p), 'i')
$client_rect = Win32API.new('user32', 'GetClientRect', %w(l p), 'i')
$readini = Win32API.new('kernel32', 'GetPrivateProfileStringA', %w(p p p p l p), 'l')
$findwindow = Win32API.new('user32', 'FindWindowA', %w(p p), 'l')

def screen_to_client(x, y)
  return nil unless x and y
  pos = [x, y].pack('ll')
  if $scr2cli.call(hwnd, pos) != 0
    return pos.unpack('ll')
  else
    return nil
  end
end

def hwnd
  game_name = "\0" * 256
  $readini.call('Game','Title','',game_name,255,".\\Game.ini")
  game_name.delete!("\0")
  return $findwindow.call('RGSS Player',game_name)
end

def client_size
  rect = [0, 0, 0, 0].pack('l4')
  $client_rect.call(hwnd, rect)
  right, bottom = rect.unpack('l4')[2..3]
  return right, bottom
end
In this script you should constantly call Mouse.mouse_pos to store the mouse positioning in the $mx and $my global variables. But i hate global variables and that method name lol xD So i created a small modification to ease the work and make it more appealing, paste it just below the code above in the same script slot:
Code:
module Mouse
  @x = @y = 0
  class << self
    attr_reader :x, :y
  end
  def self.update
    @x, @y = Mouse.mouse_pos
  end

  def mouse_pos(catch_anywhere = false)
    x, y = screen_to_client(*mouse_global_pos)
    width, height = client_size
    if catch_anywhere or (x >= 0 and y >= 0 and x < width and y < height)
      return @x = x, @y = y
    else
      return @x, @y
    end
  end
end

So you should call Mouse.update to update the mouse's positioning and reference Mouse.x and Mouse.y to get the mouse's actual position. There?s no mystery ^^

An example of an image of a mouse pointer... Test it (use this only with all the scripts added, you can copy/paste it anywhere below the scripts above):
Code:
module Mouse
  @sprite = Sprite.new 
  @sprite.bitmap = RPG::Cache.icon("001-Weapon01")

  alias old_mouse_update update
  def self.update
    old_mouse_update
    @sprite.x, @sprite.y = @x, @y
  end
end

The code is given as is, it wasn?t tested.
 
This is Cybersam?s mouse module, and as far as i know, it doesn?t have nothing to do with Mr. Mo?s scripts >.<. You can put it in any game, it soudn?t interfer in nothing. Just remember to update the mouse positioning on each frame, by calling Mouse.update from any updateable class (like Scene_Map, for example, although i recommend to update it together with Graphics and Audio update on the main module of all scenes, so you could use mouse anywhere). Also, read my post again, i made some modifications.

EDIT: What exactly do you want? Simply learn how to use a mouse or use the mouse for something more specific?
 
yeahe basicly just use the mouse. (Little curoser moving around on the game screen) Is there a demo to your script? Or any other script?(besides mr.mo's since his script does not work in the menu.
 

Thank you for viewing

HBGames is a leading amateur video game development forum and Discord server open to all ability levels. Feel free to have a nosey around!

Discord

Join our growing and active Discord server to discuss all aspects of game making in a relaxed environment. Join Us

Content

  • Our Games
  • Games in Development
  • Emoji by Twemoji.
    Top