Module: Mouse

Extended by:
ArgsUpdate, Location
Defined in:
tomes/conjurations/mouse/mouse.rb

Overview

Note:

Mouse.update must be called every tick you wish to use this class.

A module for handling mouse input.

Instance Attribute Summary

Attributes included from Location

#x, #y

Class Method Summary collapse

Methods included from Location

adjust_location, location, location=

Class Method Details

.binary_signalsString

Gives an abbreviated look at which Mouse button actions are being performed.

Returns:

  • (String)

    A string of binary digits representing the following actions: “click”, “hold”, “up”.



77
78
79
80
81
82
83
# File 'tomes/conjurations/mouse/mouse.rb', line 77

def binary_signals
  click = click? ? 1 : 0
  hold = hold? ? 1 : 0
  up = up? ? 1 : 0

  format('%d%d%d', click, hold, up)
end

.click?Boolean

Check if the left mouse button was clicked this frame.

Returns:

  • (Boolean)


21
22
23
# File 'tomes/conjurations/mouse/mouse.rb', line 21

def click?
  args.inputs.mouse.click ? true : false
end

.held?Boolean Also known as: hold?

Check if the left mouse button is being held.

Returns:

  • (Boolean)


27
28
29
# File 'tomes/conjurations/mouse/mouse.rb', line 27

def held?
  args.inputs.mouse.held ? true : false
end

.on?(thing) ⇒ Boolean

Note:

I cant figure out how to resolve nil errors without a ‘rescue’ statement. DragonRuby is no help in this regard either.

Checks if the Mouse cursor is intersecting a given rectangle. This method uses ‘args` to do this calculation.

Parameters:

  • thing (Object)

    This object must respond to #rect.

Returns:

  • (Boolean)


44
45
46
47
48
# File 'tomes/conjurations/mouse/mouse.rb', line 44

def on?(thing)
  args.inputs.mouse.intersect_rect? thing.rect
rescue StandardError
  false
end

.on_something_in?(list) ⇒ Boolean

Cycles through the given list of Objects and sees if the Mouse intersects with any of them.

Parameters:

  • list (Array<Objects>)

    A list of objects which all must respond to #rect.

Returns:

  • (Boolean)


53
54
55
# File 'tomes/conjurations/mouse/mouse.rb', line 53

def on_something_in?(list)
  list.map { |thing| on?(thing) }.include? true
end

.sees(list) ⇒ Object

Checks the given list and returns the final object with which the Mouse intersects. This works in conjunction with .sees_in_list. For example, if the cursor is over a bunch of overlapping items, the topmost of them (the last one in the ‘.sees_in_list array’) will be the one returned in this method.

Parameters:

  • list (Array<Objects>)

    A list of objects which all must respond to #rect.

Returns:

  • Object This is the last element in the list for which ‘Mouse.on?’ is true.



63
64
65
# File 'tomes/conjurations/mouse/mouse.rb', line 63

def sees(list)
  sees_in_list(list).last
end

.sees_in_list(list) ⇒ Array<Objects> Also known as: sees_in

Checks the given list and returns all of the objects with which the Mouse intersects.

Parameters:

  • list (Array<Objects>)

    A list of objects which all must respond to #rect.

Returns:

  • (Array<Objects>)


70
71
72
# File 'tomes/conjurations/mouse/mouse.rb', line 70

def sees_in_list(list)
  list.map { |thing| thing if on?(thing) }.flatten.compact
end

.to_hHash

A hash that shows the current state of the Mouse.

Returns:

  • (Hash)


93
94
95
96
97
98
99
100
101
# File 'tomes/conjurations/mouse/mouse.rb', line 93

def to_h
  {
    self: 'Mouse',
    location: location,
    click: click?,
    down: hold?,
    up: up?
  }
end

.to_iInteger

The current mouse state as a combined decimal/binary integer.

Returns:

  • (Integer)

    X coordinate, Y coordinate and binary signals concatenated with no separations or leading zeros.



105
106
107
# File 'tomes/conjurations/mouse/mouse.rb', line 105

def to_i
  format('%04d%04d%s', x, y, binary_signals).to_i
end

.to_sString

A line of text that shows the current state of the Mouse.

Returns:

  • (String)

    The Mouse location and binary signals.



87
88
89
# File 'tomes/conjurations/mouse/mouse.rb', line 87

def to_s
  format('Mouse: %04d-%03d-%s', x, y, binary_signals)
end

.up?Boolean

Checks if the left mouse button was released this frame.

Returns:

  • (Boolean)


34
35
36
# File 'tomes/conjurations/mouse/mouse.rb', line 34

def up?
  args.inputs.mouse.up ? true : false
end

.update(args) ⇒ Object

see ArgsUpdate::update Updates mouse position too.



13
14
15
16
17
# File 'tomes/conjurations/mouse/mouse.rb', line 13

def update(args)
  super
  self.x = args.inputs.mouse.x.to_i
  self.y = args.inputs.mouse.y.to_i
end