Module: MiniGL::Mouse

Defined in:
lib/minigl/global.rb

Overview

Exposes methods for controlling mouse events.

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.xObject (readonly)

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



344
345
346
# File 'lib/minigl/global.rb', line 344

def x
  @x
end

.yObject (readonly)

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



347
348
349
# File 'lib/minigl/global.rb', line 347

def y
  @y
end

Class Method Details

.button_down?(btn) ⇒ Boolean

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

Parameters:

btn

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

Returns:

  • (Boolean)


400
401
402
# File 'lib/minigl/global.rb', line 400

def button_down?(btn)
  @down[btn]
end

.button_pressed?(btn) ⇒ Boolean

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

Returns:

  • (Boolean)


391
392
393
# File 'lib/minigl/global.rb', line 391

def button_pressed?(btn)
  @down[btn] and not @prev_down[btn]
end

.button_released?(btn) ⇒ Boolean

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

Returns:

  • (Boolean)


410
411
412
# File 'lib/minigl/global.rb', line 410

def button_released?(btn)
  @prev_down[btn] and not @down[btn]
end

.double_click?(btn) ⇒ Boolean

Returns whether the given button has just been double clicked.

Parameters:

btn

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

Returns:

  • (Boolean)


419
420
421
# File 'lib/minigl/global.rb', line 419

def double_click?(btn)
  @dbl_click[btn]
end

.initializeObject

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



351
352
353
354
355
356
# File 'lib/minigl/global.rb', line 351

def initialize
  @down = {}
  @prev_down = {}
  @dbl_click = {}
  @dbl_click_timer = {}
end

.over?(x, y, w, h) ⇒ Boolean

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.

Returns:

  • (Boolean)


430
431
432
# File 'lib/minigl/global.rb', line 430

def over?(x, y, w, h)
  @x >= x and @x < x + w and @y >= y and @y < y + h
end

.updateObject

Updates the mouse position and the state of all buttons.



359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
# File 'lib/minigl/global.rb', line 359

def update
  @prev_down = @down.clone
  @down.clear
  @dbl_click.clear

  @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

  k1 = [Gosu::MsLeft, Gosu::MsMiddle, Gosu::MsRight]
  k2 = [:left, :middle, :right]
  for i in 0..2
    if G.window.button_down? k1[i]
      @down[k2[i]] = true
      @dbl_click[k2[i]] = true if @dbl_click_timer[k2[i]]
      @dbl_click_timer.delete k2[i]
    elsif @prev_down[k2[i]]
      @dbl_click_timer[k2[i]] = 0
    end
  end

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