Class: Vedeu::Move

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/vedeu/cursor/move.rb

Overview

Note:

The cursor may not be visible, but it will still move if requested. The cursor will not exceed the border or boundary of the interface. The cursor will move freely within the bounds of the interface,

irrespective of content.

Adjusts the position of the cursor. To use this class, call the appropriate event:

Examples:

Vedeu.trigger(:_cursor_down_)

Vedeu.trigger(:_cursor_left_) # When a name is not given, the cursor in
                              # the interface which is currently in focus
                              # should move to the left.

Vedeu.trigger(:_cursor_left_, 'my_interface')
                              # When a name is given, the cursor instance
                              # belonging to this interface moves to the
                              # left.

Vedeu.trigger(:_cursor_right_)
Vedeu.trigger(:_cursor_up_)

Vedeu.trigger(:_cursor_origin_)                 # /or/
Vedeu.trigger(:_cursor_origin_, 'my_interface')
                              # Moves the cursor to the top left of the
                              # named or current interface in focus.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, dy = 0, dx = 0) ⇒ Move

Returns an instance of Vedeu::Move.

Parameters:

  • name (String)

    The name of the cursor.

  • dy (Fixnum) (defaults to: 0)

    Move up (-1), or down (1), or no action (0).

  • dx (Fixnum) (defaults to: 0)

    Move left (-1), or right (1), or no action (0).



47
48
49
50
51
# File 'lib/vedeu/cursor/move.rb', line 47

def initialize(name, dy = 0, dx = 0)
  @name = name
  @dy   = dy
  @dx   = dx
end

Instance Attribute Details

#dxFixnum (readonly, protected)

Returns:

  • (Fixnum)


124
125
126
# File 'lib/vedeu/cursor/move.rb', line 124

def dx
  @dx
end

#dyFixnum (readonly, protected)

Returns:

  • (Fixnum)


128
129
130
# File 'lib/vedeu/cursor/move.rb', line 128

def dy
  @dy
end

#nameString (readonly, protected)

Returns:

  • (String)


132
133
134
# File 'lib/vedeu/cursor/move.rb', line 132

def name
  @name
end

Class Method Details

.by_name(direction, name = nil) ⇒ Cursor

Move the named cursor, or that which is currently in focus in the specified direction.

Parameters:

  • direction (Symbol)

    The direction of travel. Directions include: (:down, :left, :right, :up, :origin). When ‘:origin’ the cursor is moved to the top left of the interface.

  • name (String|NilClass) (defaults to: nil)

    The name of the interface/cursor to be moved; when not given, the interface currently in focus determines which cursor instance to move.

Returns:



63
64
65
66
67
# File 'lib/vedeu/cursor/move.rb', line 63

def self.by_name(direction, name = nil)
  name = name ? name : Vedeu.focus

  Vedeu::Move.send(direction, name)
end

.down(name) ⇒ Cursor

Moves the cursor down by one row.

Parameters:

  • name (String)

Returns:



73
74
75
# File 'lib/vedeu/cursor/move.rb', line 73

def self.down(name)
  new(name, 1, 0).move
end

.left(name) ⇒ Cursor

Moves the cursor left by one column.

Parameters:

  • name (String)

Returns:



81
82
83
# File 'lib/vedeu/cursor/move.rb', line 81

def self.left(name)
  new(name, 0, -1).move
end

.origin(name) ⇒ Cursor

Moves the cursor to the top left coordinate of the interface.

Parameters:

  • name (String)

Returns:



105
106
107
# File 'lib/vedeu/cursor/move.rb', line 105

def self.origin(name)
  new(name, -2000, -2000).move
end

.right(name) ⇒ Cursor

Moves the cursor right by one column.

Parameters:

  • name (String)

Returns:



89
90
91
# File 'lib/vedeu/cursor/move.rb', line 89

def self.right(name)
  new(name, 0, 1).move
end

.up(name) ⇒ Cursor

Moves the cursor up by one row.

Parameters:

  • name (String)

Returns:



97
98
99
# File 'lib/vedeu/cursor/move.rb', line 97

def self.up(name)
  new(name, -1, 0).move
end

Instance Method Details

#attributesHash<Symbol => Fixnum, String> (private)

Retrieve the attributes of the cursor.

Returns:

  • (Hash<Symbol => Fixnum, String>)


139
140
141
# File 'lib/vedeu/cursor/move.rb', line 139

def attributes
  cursor.attributes
end

#borderObject (private)

Retrieve the border for the named view: this will provide the geometry for the cursor, ensuring it displays within the interface boundaries.



182
183
184
# File 'lib/vedeu/cursor/move.rb', line 182

def border
  @border ||= Vedeu.borders.by_name(name)
end

#coordinateCoordinate (private)

Returns:



187
188
189
# File 'lib/vedeu/cursor/move.rb', line 187

def coordinate
  @coordinate ||= Vedeu::Coordinate.new(height, width, x, y)
end

#cursorVedeu::Cursor (private)

Returns:



192
193
194
# File 'lib/vedeu/cursor/move.rb', line 192

def cursor
  @cursor ||= Vedeu.cursors.by_name(name)
end

#moveCursor

Returns a newly positioned and stored Cursor.

Returns:



112
113
114
115
116
117
118
# File 'lib/vedeu/cursor/move.rb', line 112

def move
  cursor = Vedeu::Cursor.new(attributes.merge!(new_attributes)).store

  Vedeu.trigger(:_refresh_cursor_, name)

  cursor
end

#new_attributesHash<Symbol => Fixnum> (private)

Provide the new attributes for the cursor.

Returns:

  • (Hash<Symbol => Fixnum>)


146
147
148
149
150
151
152
153
# File 'lib/vedeu/cursor/move.rb', line 146

def new_attributes
  {
    x:  validator.x,
    y:  validator.y,
    ox: ox,
    oy: oy,
  }
end

#oxFixnum (private)

Apply the direction amount to the cursor offset. If the offset is less than 0, correct to 0.

Returns:

  • (Fixnum)


200
201
202
203
204
# File 'lib/vedeu/cursor/move.rb', line 200

def ox
  ox = cursor.ox + dx
  ox = 0 if ox < 0
  ox
end

#oyFixnum (private)

Apply the direction amount to the cursor offset. If the offset is less than 0, correct to 0.

Returns:

  • (Fixnum)


210
211
212
213
214
# File 'lib/vedeu/cursor/move.rb', line 210

def oy
  oy = cursor.oy + dy
  oy = 0 if oy < 0
  oy
end

#validatorPositionValidator (private)

Validates the new position of the cursor.

Returns:



158
159
160
161
162
# File 'lib/vedeu/cursor/move.rb', line 158

def validator
  @validator ||= Vedeu::PositionValidator.validate(name,
                                                   x_position,
                                                   y_position)
end

#x_positionFixnum (private)

Returns the cursors x position based on its current offset.

Returns:

  • (Fixnum)


167
168
169
# File 'lib/vedeu/cursor/move.rb', line 167

def x_position
  coordinate.x_position(ox)
end

#y_positionFixnum (private)

Returns the cursors y position based on its current offset.

Returns:

  • (Fixnum)


174
175
176
# File 'lib/vedeu/cursor/move.rb', line 174

def y_position
  coordinate.y_position(oy)
end