Class: Tuile::MouseEvent

Inherits:
Object
  • Object
show all
Defined in:
lib/tuile/mouse_event.rb

Overview

A mouse event.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#buttonSymbol? (readonly)

Returns one of ‘:left`, `:middle`, `:right`, `:scroll_up`, `:scroll_down`; `nil` if not known.

Returns:

  • (Symbol, nil)

    one of ‘:left`, `:middle`, `:right`, `:scroll_up`, `:scroll_down`; `nil` if not known.



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/tuile/mouse_event.rb', line 13

class MouseEvent < Data.define(:button, :x, :y)
  # @return [Point] the event's position.
  def point = Point.new(x, y)

  # Checks whether given key is a mouse event key
  # @param key [String] key read via {Keys.getkey}
  # @return [Boolean] true if it is a mouse event
  def self.mouse_event?(key)
    key.start_with?("\e[M") && key.size >= 6
  end

  # @param key [String] key read via {Keys.getkey}
  # @return [MouseEvent, nil]
  def self.parse(key)
    return nil unless mouse_event?(key)

    button = key[3].ord - 32
    # XTerm reports coordinates 1-based (column N is encoded as N + 32);
    # subtract 33 so that `x` and `y` are 0-based.
    x = key[4].ord - 33
    y = key[5].ord - 33
    button = case button
             when 0 then :left
             when 2 then :right
             when 1 then :middle
             when 64 then :scroll_up
             when 65 then :scroll_down
             end
    MouseEvent.new(button, x, y)
  end

  # @return [String]
  def self.start_tracking = "\e[?1000h"
  # @return [String]
  def self.stop_tracking = "\e[?1000l"
end

#xInteger (readonly)

Returns x coordinate, 0-based.

Returns:

  • (Integer)

    x coordinate, 0-based.



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/tuile/mouse_event.rb', line 13

class MouseEvent < Data.define(:button, :x, :y)
  # @return [Point] the event's position.
  def point = Point.new(x, y)

  # Checks whether given key is a mouse event key
  # @param key [String] key read via {Keys.getkey}
  # @return [Boolean] true if it is a mouse event
  def self.mouse_event?(key)
    key.start_with?("\e[M") && key.size >= 6
  end

  # @param key [String] key read via {Keys.getkey}
  # @return [MouseEvent, nil]
  def self.parse(key)
    return nil unless mouse_event?(key)

    button = key[3].ord - 32
    # XTerm reports coordinates 1-based (column N is encoded as N + 32);
    # subtract 33 so that `x` and `y` are 0-based.
    x = key[4].ord - 33
    y = key[5].ord - 33
    button = case button
             when 0 then :left
             when 2 then :right
             when 1 then :middle
             when 64 then :scroll_up
             when 65 then :scroll_down
             end
    MouseEvent.new(button, x, y)
  end

  # @return [String]
  def self.start_tracking = "\e[?1000h"
  # @return [String]
  def self.stop_tracking = "\e[?1000l"
end

#yInteger (readonly)

Returns y coordinate, 0-based.

Returns:

  • (Integer)

    y coordinate, 0-based.



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/tuile/mouse_event.rb', line 13

class MouseEvent < Data.define(:button, :x, :y)
  # @return [Point] the event's position.
  def point = Point.new(x, y)

  # Checks whether given key is a mouse event key
  # @param key [String] key read via {Keys.getkey}
  # @return [Boolean] true if it is a mouse event
  def self.mouse_event?(key)
    key.start_with?("\e[M") && key.size >= 6
  end

  # @param key [String] key read via {Keys.getkey}
  # @return [MouseEvent, nil]
  def self.parse(key)
    return nil unless mouse_event?(key)

    button = key[3].ord - 32
    # XTerm reports coordinates 1-based (column N is encoded as N + 32);
    # subtract 33 so that `x` and `y` are 0-based.
    x = key[4].ord - 33
    y = key[5].ord - 33
    button = case button
             when 0 then :left
             when 2 then :right
             when 1 then :middle
             when 64 then :scroll_up
             when 65 then :scroll_down
             end
    MouseEvent.new(button, x, y)
  end

  # @return [String]
  def self.start_tracking = "\e[?1000h"
  # @return [String]
  def self.stop_tracking = "\e[?1000l"
end

Class Method Details

.mouse_event?(key) ⇒ Boolean

Checks whether given key is a mouse event key

Parameters:

Returns:

  • (Boolean)

    true if it is a mouse event



20
21
22
# File 'lib/tuile/mouse_event.rb', line 20

def self.mouse_event?(key)
  key.start_with?("\e[M") && key.size >= 6
end

.parse(key) ⇒ MouseEvent?

Parameters:

Returns:



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/tuile/mouse_event.rb', line 26

def self.parse(key)
  return nil unless mouse_event?(key)

  button = key[3].ord - 32
  # XTerm reports coordinates 1-based (column N is encoded as N + 32);
  # subtract 33 so that `x` and `y` are 0-based.
  x = key[4].ord - 33
  y = key[5].ord - 33
  button = case button
           when 0 then :left
           when 2 then :right
           when 1 then :middle
           when 64 then :scroll_up
           when 65 then :scroll_down
           end
  MouseEvent.new(button, x, y)
end

.start_trackingString

Returns:

  • (String)


45
46
# File 'lib/tuile/mouse_event.rb', line 45

def self.start_tracking = "\e[?1000h"
# @return [String]

.stop_trackingString

Returns:

  • (String)


47
# File 'lib/tuile/mouse_event.rb', line 47

def self.stop_tracking = "\e[?1000l"

Instance Method Details

#pointPoint

Returns the event’s position.

Returns:

  • (Point)

    the event’s position.



15
# File 'lib/tuile/mouse_event.rb', line 15

def point = Point.new(x, y)