I am trying to add Mouse input for the menus of my VXAce game so that users can click on things like "New Game", "Items" etc...
I am using Ayesha for the Title Screen and Monogatari for the in-game menu.
As well as that I have Fullscreen++ and Basic Window Resizer to make the window bigger, as well as Cidiomar R. Dias Junior's input script, as well as a custom mouse helper script I threw together
I have also used this debug script to show the mouse position, and to display the currently pressed key, and I see `:LBUTTON` when clicking.
Here's my current script I currently have, I can see it drawing the red debug boxes in the correct place, but not responding to mouse hover or mouse click events
Any help would be much appreciated!
I am using Ayesha for the Title Screen and Monogatari for the in-game menu.
As well as that I have Fullscreen++ and Basic Window Resizer to make the window bigger, as well as Cidiomar R. Dias Junior's input script, as well as a custom mouse helper script I threw together
Ruby:
module Mouse
extend self
GET_CURSOR_POS = Win32API.new('user32', 'GetCursorPos', ['P'], 'I')
SCREEN_TO_CLIENT = Win32API.new('user32', 'ScreenToClient', ['L', 'P'], 'I')
FIND_WINDOW = Win32API.new('user32', 'FindWindowA', ['P', 'P'], 'L')
GET_KEY_STATE = Win32API.new('user32', 'GetAsyncKeyState', ['I'], 'S')
HWND = FIND_WINDOW.call(nil, "Game")
VK_LBUTTON = 0x01
@x = 0
@y = 0
@clicked = false
@down = false
def update
update_position
update_click_state
end
def x; @x; end
def y; @y; end
def click?
@clicked
end
private
def update_position
pos = [0, 0].pack('ll')
GET_CURSOR_POS.call(pos)
SCREEN_TO_CLIENT.call(HWND, pos)
@x, @y = pos.unpack('ll')
end
def update_click_state
state = GET_KEY_STATE.call(VK_LBUTTON)
pressed = (state & 0x8000 != 0)
if pressed
unless @down
@clicked = true
@down = true
else
@clicked = false
end
else
@clicked = false
@down = false
end
end
end
# Hook Mouse.update into Input.update
class << Input
alias_method :_mouse_update_input, :update
def update
_mouse_update_input
Mouse.update
end
end
I have also used this debug script to show the mouse position, and to display the currently pressed key, and I see `:LBUTTON` when clicking.
Ruby:
class Scene_MouseDebug < Scene_Base
def start
super
create_info_window
end
def update
super
update_input
end
def create_info_window
@window = Window_Base.new(0, 0, 544, 416)
@window.contents.font.size = 20
end
def update_input
mx = Mouse.x
my = Mouse.y
@window.contents.clear
@window.contents.draw_text(0, 0, 520, 24, "Mouse Position: #{mx}, #{my}")
if Input.trigger?(:LBUTTON)
puts "[DEBUG] Input.trigger?(:LBUTTON) = TRUE"
@window.contents.draw_text(0, 24, 520, 24, "LBUTTON: Clicked (symbol)")
end
if Input.trigger?(0x01)
puts "[DEBUG] Input.trigger?(0x01) = TRUE"
@window.contents.draw_text(0, 48, 520, 24, "VK 0x01: Clicked (raw)")
end
draw_pressed_keys
end
def draw_pressed_keys
pressed_keys = []
0.upto(255) do |keycode|
if Input.press?(keycode)
# Try to find a matching symbol
symbol = Input::KEYMAP.key(keycode)
label = symbol ? "#{symbol} (0x#{keycode.to_s(16)})" : "VK 0x#{keycode.to_s(16)}"
pressed_keys << label
end
end
if pressed_keys.empty?
@window.contents.draw_text(0, 80, 520, 24, "No keys currently pressed.")
else
@window.contents.draw_text(0, 80, 520, 24, "Keys Pressed:")
pressed_keys.each_with_index do |label, i|
@window.contents.draw_text(16, 104 + i * 20, 500, 24, "- #{label}")
end
end
end
end
Here's my current script I currently have, I can see it drawing the red debug boxes in the correct place, but not responding to mouse hover or mouse click events
Ruby:
#==============================================================================
# ▼ Ayesha Mouse Mode
#==============================================================================
AYESHA_MOUSE_DEBUG = true
class Scene_Title
alias ayesha_mouse_setup execute_setup
def execute_setup
ayesha_mouse_setup
@mouse_hovering = false
create_debug_boxes if AYESHA_MOUSE_DEBUG
@screen_size = [Graphics.width, Graphics.height]
end
alias ayesha_mouse_update update
def update
check_resolution_change
ayesha_mouse_update
update_mouse_input
update_debug_boxes if AYESHA_MOUSE_DEBUG
end
alias ayesha_mouse_dispose dispose
def dispose
dispose_debug_boxes if AYESHA_MOUSE_DEBUG
ayesha_mouse_dispose
end
#--------------------------------------------------------------------------
# ● Mouse Hover + Click Handling
#--------------------------------------------------------------------------
def update_mouse_input
return unless @com
mx, my = Mouse.x, Mouse.y
@com.each_with_index do |command, index|
next unless command_visible?(command)
if in_rect_sprite?(command, mx, my)
if @com_index != index
@com_index = index
refresh_index
update_commands
puts "[Mouse Hover] Set index to #{index}" if AYESHA_MOUSE_DEBUG
end
if Input.trigger?(0x01) # Left click
puts "[Mouse Click] Select command at index #{index}" if AYESHA_MOUSE_DEBUG
Sound.play_ok
select_command
end
break
end
end
end
#--------------------------------------------------------------------------
# ● Accurate Hitbox from Sprite
#--------------------------------------------------------------------------
def in_rect_sprite?(sprite, mx, my)
return false unless sprite && sprite.bitmap && sprite.visible
return false if sprite.opacity < 200
sx = sprite.x - sprite.ox
sy = sprite.y - sprite.oy
sw = sprite.bitmap.width
sh = sprite.bitmap.height
mx >= sx && mx < sx + sw && my >= sy && my < sy + sh
end
def command_visible?(command)
command && command.visible && command.bitmap && command.opacity > 100
end
#--------------------------------------------------------------------------
# ● Watch for screen resolution change (fullscreen toggle)
#--------------------------------------------------------------------------
def check_resolution_change
if @screen_size != [Graphics.width, Graphics.height]
@screen_size = [Graphics.width, Graphics.height]
reset_debug_boxes if AYESHA_MOUSE_DEBUG
end
end
#--------------------------------------------------------------------------
# ● Debug Red Boxes
#--------------------------------------------------------------------------
def create_debug_boxes
@debug_boxes = []
return unless @com
@com.each do |sprite|
box = Sprite.new
box.bitmap = Bitmap.new(sprite.bitmap.width, sprite.bitmap.height)
box.bitmap.fill_rect(box.bitmap.rect, Color.new(255, 0, 0, 100))
box.z = 999
@debug_boxes << box
end
end
def update_debug_boxes
return unless @debug_boxes && @com
@debug_boxes.each_with_index do |box, i|
sprite = @com[i]
next unless sprite
box.x = sprite.x - sprite.ox
box.y = sprite.y - sprite.oy
box.visible = sprite.visible
box.opacity = sprite.opacity
end
end
def dispose_debug_boxes
return unless @debug_boxes
@debug_boxes.each do |box|
box.bitmap.dispose
box.dispose
end
@debug_boxes.clear
end
def reset_debug_boxes
dispose_debug_boxes
create_debug_boxes
end
end
#==============================================================================
# ▼ Patch Title_Commands to Return Accurate Rect
# Used only for debug drawing
#==============================================================================
class Title_Commands < Sprite
def rect
Rect.new(self.x - self.ox, self.y - self.oy, self.bitmap.width, self.bitmap.height)
end
end
Any help would be much appreciated!