Class: Vedeu::MoveCursor

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/vedeu/cursor/move_cursor.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(cursor, interface, dy = 0, dx = 0) ⇒ MoveCursor

Returns an instance of MoveCursor.

Parameters:

  • cursor (Cursor)
  • interface (Interface)
  • 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).



56
57
58
59
60
61
# File 'lib/vedeu/cursor/move_cursor.rb', line 56

def initialize(cursor, interface, dy = 0, dx = 0)
  @cursor    = cursor
  @dy        = dy || 0
  @dx        = dx || 0
  @interface = interface
end

Instance Attribute Details

#cursorObject (readonly, private)

Returns the value of attribute cursor.



148
149
150
# File 'lib/vedeu/cursor/move_cursor.rb', line 148

def cursor
  @cursor
end

#dxObject (readonly, private)

Returns the value of attribute dx.



148
149
150
# File 'lib/vedeu/cursor/move_cursor.rb', line 148

def dx
  @dx
end

#dyObject (readonly, private)

Returns the value of attribute dy.



148
149
150
# File 'lib/vedeu/cursor/move_cursor.rb', line 148

def dy
  @dy
end

#interfaceObject (readonly, private)

Returns the value of attribute interface.



148
149
150
# File 'lib/vedeu/cursor/move_cursor.rb', line 148

def interface
  @interface
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:



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/vedeu/cursor/move_cursor.rb', line 73

def self.by_name(direction, name = nil)
  if name
    cursor     = Vedeu.cursors.by_name(name)
    interface  = Vedeu.interfaces.find(name)
    new_cursor = MoveCursor.send(direction, cursor, interface)
    Refresh.by_name(name)

  else
    cursor     = Vedeu.cursor
    interface  = Vedeu.interfaces.current
    new_cursor = MoveCursor.send(direction, cursor, interface)
    Refresh.by_focus

  end
  new_cursor
end

.down(cursor, interface) ⇒ Cursor

Moves the cursor down by one row.

Parameters:

Returns:



95
96
97
# File 'lib/vedeu/cursor/move_cursor.rb', line 95

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

.left(cursor, interface) ⇒ Cursor

Moves the cursor left by one column.

Parameters:

Returns:



104
105
106
107
108
# File 'lib/vedeu/cursor/move_cursor.rb', line 104

def self.left(cursor, interface)
  return cursor unless cursor.ox > 0

  new(cursor, interface, 0, -1).move
end

.origin(cursor, interface) ⇒ Cursor

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

Parameters:

Returns:



135
136
137
# File 'lib/vedeu/cursor/move_cursor.rb', line 135

def self.origin(cursor, interface)
  new(cursor, interface, (0 - cursor.y), (0 - cursor.x)).move
end

.right(cursor, interface) ⇒ Cursor

Moves the cursor right by one column.

Parameters:

Returns:



115
116
117
# File 'lib/vedeu/cursor/move_cursor.rb', line 115

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

.up(cursor, interface) ⇒ Cursor

Moves the cursor up by one row.

Parameters:

Returns:



124
125
126
127
128
# File 'lib/vedeu/cursor/move_cursor.rb', line 124

def self.up(cursor, interface)
  return cursor unless cursor.oy > 0

  new(cursor, interface, -1, 0).move
end

Instance Method Details

#bordered_heightFixnum (private)

Return the height of the interface, minus any borders.

Returns:



198
199
200
201
202
# File 'lib/vedeu/cursor/move_cursor.rb', line 198

def bordered_height
  return border.height if border?

  height
end

#bordered_widthFixnum (private)

Return the width of the interface, minus any borders.

Returns:



207
208
209
210
211
# File 'lib/vedeu/cursor/move_cursor.rb', line 207

def bordered_width
  return border.width if border?

  width
end

#coordinateCoordinate (private)

Returns:



188
189
190
191
192
193
# File 'lib/vedeu/cursor/move_cursor.rb', line 188

def coordinate
  @coordinate ||= Vedeu::Coordinate.new(bordered_height,
                                        bordered_width,
                                        left,
                                        top)
end

#moveCursor

Returns a newly positioned and stored Cursor.

Returns:



142
143
144
# File 'lib/vedeu/cursor/move_cursor.rb', line 142

def move
  Cursor.new(cursor.attributes.merge!(moved_attributes)).store
end

#moved_attributesHash (private)

Returns:

  • (Hash)


151
152
153
154
155
156
157
158
# File 'lib/vedeu/cursor/move_cursor.rb', line 151

def moved_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:



171
172
173
174
175
# File 'lib/vedeu/cursor/move_cursor.rb', line 171

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:



181
182
183
184
185
# File 'lib/vedeu/cursor/move_cursor.rb', line 181

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

#validatorPositionValidator (private)

Returns:



161
162
163
164
165
# File 'lib/vedeu/cursor/move_cursor.rb', line 161

def validator
  @validator ||= Vedeu::PositionValidator.validate(interface,
                                                  coordinate.x_position(ox),
                                                  coordinate.y_position(oy))
end