lib/vedeu/cursors/cursor.rb in vedeu-0.6.30 vs lib/vedeu/cursors/cursor.rb in vedeu-0.6.31

- old
+ new

@@ -15,14 +15,10 @@ :bx, :bxn, :by, :byn - # @!attribute [r] attributes - # @return [Hash] - attr_reader :attributes - # @!attribute [r] name # @return [String|Symbol] attr_reader :name # @!attribute [w] ox @@ -41,11 +37,11 @@ # @return [Fixnum] attr_writer :y # @param (see #initialize) # @return [Vedeu::Cursors::Cursor] - def self.store(attributes) + def self.store(attributes = {}) new(attributes).store end # Returns a new instance of Vedeu::Cursors::Cursor. # @@ -62,17 +58,28 @@ # the cursor. # @option attributes y [Fixnum] The terminal y coordinate for # the cursor. # @return [Vedeu::Cursors::Cursor] def initialize(attributes = {}) - @attributes = defaults.merge!(attributes) - - @attributes.each do |key, value| + defaults.merge!(attributes).each do |key, value| instance_variable_set("@#{key}", value) end end + # @return [Hash] + def attributes + { + name: @name, + ox: ox, + oy: oy, + repository: @repository, + visible: @visible, + x: x, + y: y, + } + end + # An object is equal when its values are the same. # # @param other [Vedeu::Cursors::Cursor] # @return [Boolean] def eql?(other) @@ -86,63 +93,67 @@ # Vedeu.trigger(:_cursor_down_, Vedeu.focus) # # @return [Vedeu::Cursors::Cursor] def move_down @oy += 1 + @y = coordinate(oy, :y).y - Vedeu::Cursors::Cursor.store( - new_attributes(coordinate.y_position, x, oy, ox)) + store end # Moves the cursor left by one column. # # Vedeu.trigger(:_cursor_left_, name) # Vedeu.trigger(:_cursor_left_, Vedeu.focus) # # @return [Vedeu::Cursors::Cursor] def move_left @ox -= 1 + @x = coordinate(ox, :x).x - Vedeu::Cursors::Cursor.store( - new_attributes(y, coordinate.x_position, oy, ox)) + store end # Moves the cursor to the top left of the named interface. # # Vedeu.trigger(:_cursor_origin_, name) # Vedeu.trigger(:_cursor_origin_, Vedeu.focus) # # @return [Vedeu::Cursors::Cursor] def move_origin - Vedeu::Cursors::Cursor.store( - attributes.merge!(x: bx, y: by, ox: 0, oy: 0)) + @x = bx + @y = by + @ox = 0 + @oy = 0 + + store end # Moves the cursor right by one column. # # Vedeu.trigger(:_cursor_right_, name) # Vedeu.trigger(:_cursor_right_, Vedeu.focus) # # @return [Vedeu::Cursors::Cursor] def move_right @ox += 1 + @x = coordinate(ox, :x).x - Vedeu::Cursors::Cursor.store( - new_attributes(y, coordinate.x_position, oy, ox)) + store end # Moves the cursor up by one row. # # Vedeu.trigger(:_cursor_up_, name) # Vedeu.trigger(:_cursor_up_, Vedeu.focus) # # @return [Vedeu::Cursors::Cursor] def move_up @oy -= 1 + @y = coordinate(oy, :y).y - Vedeu::Cursors::Cursor.store( - new_attributes(coordinate.y_position, x, oy, ox)) + store end # Renders the cursor. # # @return [Array<Vedeu::Models::Escape>] @@ -158,13 +169,14 @@ # @param new_ox [Fixnum] The column/character position. # @return [Vedeu::Cursors::Cursor] def reposition(new_oy, new_ox) @oy = new_oy @ox = new_ox + @y = coordinate(oy, :y).y + @x = coordinate(ox, :x).x - Vedeu::Cursors::Cursor.store( - new_attributes(coordinate.y_position, coordinate.x_position, oy, ox)) + store end # @return [Array<Fixnum>] def to_a position.to_a @@ -198,18 +210,16 @@ render end # @return [Fixnum] def ox - @ox = 0 if @ox < 0 - @ox + @ox < 0 ? 0 : @ox end # @return [Fixnum] def oy - @oy = 0 if @oy < 0 - @oy + @oy < 0 ? 0 : @oy end # Return the position of this cursor. # # @return [Vedeu::Geometry::Position] @@ -233,38 +243,34 @@ render end # @return [Fixnum] The column/character coordinate. def x - @x = bx if @x < bx - @x = bxn if @x > bxn - - @attributes[:x] = @x - + @x = (@x < bx) ? bx : @x + @x = (@x > bxn) ? bxn : @x @x end # @return [Fixnum] The row/line coordinate. def y - @y = by if @y < by - @y = byn if @y > byn - - @attributes[:y] = @y - + @y = (@y < by) ? by : @y + @y = (@y > byn) ? byn : @y @y end private # @see Vedeu::Borders::Repository#by_name def border @border ||= Vedeu.borders.by_name(name) end + # Determine correct x and y related coordinates. + # # @return [Vedeu::Geometry::Coordinate] - def coordinate - @coordinate ||= Vedeu::Geometry::Coordinate.new(name, oy, ox) + def coordinate(offset, type) + Vedeu::Geometry::Coordinate.new(name: name, offset: offset, type: type) end # The default values for a new instance of this class. # # @return [Hash] @@ -311,10 +317,12 @@ # @!method toggle_cursor # @see Vedeu::Toggleable::ClassMethods#toggle def_delegators Vedeu::Cursors::Cursor, :hide_cursor, :show_cursor, :toggle_cursor + # :nocov: + # See {file:docs/events/visibility.md#\_hide_cursor_} Vedeu.bind(:_hide_cursor_) { |name| Vedeu.hide_cursor(name) } # Vedeu.bind_alias(:_cursor_hide_, :_hide_cursor_) # See {file:docs/events/visibility.md#\_show_cursor_} @@ -369,7 +377,9 @@ Vedeu.trigger(:_clear_view_, name) Vedeu.trigger(:_refresh_view_, name) Vedeu.trigger(:_refresh_cursor_, name) end + + # :nocov: end # Vedeu