Sha256: 91457f0202459270e770fbd379aefe962d0bcc5236ab1430a9b1ac440ddd6a98

Contents?: true

Size: 1.63 KB

Versions: 13

Compression:

Stored size: 1.63 KB

Contents

module Vedeu

  # Converts an index into a position for the terminal.
  #
  # When the optional offset `oy` and `ox` params are given, the they are used
  # for the starting position.
  #
  class IndexPosition

    # @param (see #initialize)
    # @return [Vedeu::Position]
    def self.[](iy, ix, oy = 1, ox = 1)
      new(iy, ix, oy, ox).[]
    end

    # Returns a new instance of Vedeu::IndexPosition.
    #
    # @param iy [Fixnum]
    # @param ix [Fixnum]
    # @param oy [Fixnum]
    # @param ox [Fixnum]
    # @return [Vedeu::IndexPosition]
    def initialize(iy, ix, oy = 1, ox = 1)
      @iy = iy
      @ix = ix
      @oy = oy
      @ox = ox
    end

    # @return [Vedeu::Position]
    def []
      Vedeu::Position[y, x]
    end
    alias_method :to_position, :[]

    # An object is equal when its values are the same.
    #
    # @param other [Vedeu::IndexPosition]
    # @return [Boolean]
    def eql?(other)
      self.class == other.class && (x == other.x && y == other.y)
    end
    alias_method :==, :eql?

    # Returns the y coordinate.
    #
    # @return [Fixnum]
    def y
      (iy <= 0) ? oy : (iy + oy)
    end
    alias_method :first, :y

    # Returns the x coordinate.
    #
    # @return [Fixnum]
    def x
      (ix <= 0) ? ox : (ix + ox)
    end
    alias_method :last, :x

    private

    # @return [Fixnum]
    def iy
      @_iy ||= (@iy <= 0) ? 0 : @iy
    end

    # @return [Fixnum]
    def ix
      @_ix ||= (@ix <= 0) ? 0 : @ix
    end

    # @return [Fixnum]
    def oy
      @_oy ||= (@oy <= 1) ? 1 : @oy
    end

    # @return [Fixnum]
    def ox
      @_ox ||= (@ox <= 1) ? 1 : @ox
    end

  end # IndexPosition

end # Vedeu

Version data entries

13 entries across 13 versions & 1 rubygems

Version Path
vedeu-0.5.3 lib/vedeu/geometry/index_position.rb
vedeu-0.5.2 lib/vedeu/geometry/index_position.rb
vedeu-0.5.1 lib/vedeu/geometry/index_position.rb
vedeu-0.5.0 lib/vedeu/geometry/index_position.rb
vedeu-0.4.65 lib/vedeu/geometry/index_position.rb
vedeu-0.4.64 lib/vedeu/geometry/index_position.rb
vedeu-0.4.63 lib/vedeu/geometry/index_position.rb
vedeu-0.4.62 lib/vedeu/geometry/index_position.rb
vedeu-0.4.61 lib/vedeu/geometry/index_position.rb
vedeu-0.4.60 lib/vedeu/geometry/index_position.rb
vedeu-0.4.59 lib/vedeu/geometry/index_position.rb
vedeu-0.4.58 lib/vedeu/geometry/index_position.rb
vedeu-0.4.57 lib/vedeu/geometry/index_position.rb