Module: OrangeZest::Input

Defined in:
lib/orange_zest/input.rb

Overview

Holds some input state so that it can be globally accessed.

In Gosu, the mouse cursor position is only available to the ‘Gosu::Window`, which makes it tricky to break the UI into components. This module stores and shares the cursor position for global access.

Class Method Summary collapse

Class Method Details

.button_down(id) ⇒ Object

Should be called from your ‘Gosu::Window#button_down` implementation. Updates `.click?`.

Parameters:

  • id (Integer)

    The keycode pressed.



10
11
12
# File 'lib/orange_zest/input.rb', line 10

def self.button_down(id)
  @click = (id == Gosu::MS_LEFT)
end

.clear_clickObject

Clears the click registered this frame, if any. This can be used to ensure that a click is only seen by one UI element.



33
34
35
# File 'lib/orange_zest/input.rb', line 33

def self.clear_click
  @click = false
end

.click?Boolean

Whether the left mouse button was clicked this frame. Even if the mouse is held down, this will only be ‘true` for the first frame of the click.

Returns:

  • (Boolean)


27
28
29
# File 'lib/orange_zest/input.rb', line 27

def self.click?
  @click
end

.cursorObject

The mouse cursor position within the window.



21
22
23
# File 'lib/orange_zest/input.rb', line 21

def self.cursor
  @cursor
end

.update(window) ⇒ Object

Should be called from your ‘Gosu::Window’#update` implementation. Updates ‘.cursor`.

Parameters:

  • window (Gosu::Window)

    The window this is being called from.



16
17
18
# File 'lib/orange_zest/input.rb', line 16

def self.update(window)
  @cursor = Point.new(window.mouse_x, window.mouse_y)
end