module MiniGL::Mouse

Exposes methods for controlling mouse events.

Attributes

x[R]

The current x-coordinate of the mouse cursor in the screen.

y[R]

The current y-coordinate of the mouse cursor in the screen.

Public Class Methods

button_down?(btn) click to toggle source

Returns whether the given button is down in the current frame.

Parameters:

btn

Button to be checked. Valid values are :left, :middle and :right

# File lib/minigl/global.rb, line 453
def button_down?(btn)
  @down[btn]
end
button_pressed?(btn) click to toggle source

Returns whether the given button is down in the current frame and was not down in the frame before.

Parameters:

btn

Button to be checked. Valid values are :left, :middle and :right

# File lib/minigl/global.rb, line 444
def button_pressed?(btn)
  @down[btn] and not @prev_down[btn]
end
button_released?(btn) click to toggle source

Returns whether the given button is not down in the current frame, but was down in the frame before.

Parameters:

btn

Button to be checked. Valid values are :left, :middle and :right

# File lib/minigl/global.rb, line 463
def button_released?(btn)
  @prev_down[btn] and not @down[btn]
end
double_click?(btn) click to toggle source

Returns whether the given button has just been double clicked.

Parameters:

btn

Button to be checked. Valid values are :left, :middle and :right

# File lib/minigl/global.rb, line 472
def double_click?(btn)
  @dbl_click[btn]
end
initialize() click to toggle source

This is called by GameWindow.initialize. Don’t call it explicitly.

# File lib/minigl/global.rb, line 397
def initialize
  @down = {}
  @prev_down = {}
  @next_down = {}
  @dbl_click = {}
  @dbl_click_timer = {}
end
over?(x, y = nil, w = nil, h = nil) click to toggle source

Returns whether the mouse cursor is currently inside the given area.

Parameters:

x

The x-coordinate of the top left corner of the area.

y

The y-coordinate of the top left corner of the area.

w

The width of the area.

h

The height of the area.

Alternate syntax

over?(rectangle)

Parameters:

rectangle

A rectangle representing the area to be checked.

# File lib/minigl/global.rb, line 490
def over?(x, y = nil, w = nil, h = nil)
  return @x >= x.x && @x < x.x + x.w && @y >= x.y && @y < x.y + x.h if x.is_a? Rectangle
  @x >= x && @x < x + w && @y >= y && @y < y + h
end
update() click to toggle source

Updates the mouse position and the state of all buttons.

# File lib/minigl/global.rb, line 406
def update
  @prev_down = @down.clone
  @down = @next_down.clone
  @next_down.delete_if { |_, v| v.zero? }
  @dbl_click.clear

  if @click
    @click[:action].call
    @click = nil
  end

  @dbl_click_timer.each do |k, v|
    if v < G.double_click_delay
      @dbl_click_timer[k] += 1
    else
      @dbl_click_timer.delete(k)
    end
  end

  %i[left middle right].each do |key|
    if @down[key]
      @dbl_click[key] = true if @dbl_click_timer[key]
      @dbl_click_timer.delete(key)
    elsif @prev_down[key]
      @dbl_click_timer[key] = 0
    end
  end

  @x = G.window.mouse_x.round
  @y = G.window.mouse_y.round
end