Sha256: f3d86608e5fee5d552d22f9b091e14d43bef220785fc7978283f2d4dd0a54d79

Contents?: true

Size: 1.71 KB

Versions: 4

Compression:

Stored size: 1.71 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

    # @param name [String] The name of the interface.
    # @param iy [Fixnum]
    # @param ix [Fixnum]
    # @return [Vedeu::Position]
    def self.from_interface(name, iy, ix)
      if Vedeu.interfaces.registered?(name)
        interface = Vedeu.interfaces.find(name)
        oy        = interface.border.by
        ox        = interface.border.bx

      else
        oy = 1
        ox = 1

      end

      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.new(y, x)
    end

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

    # @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

4 entries across 4 versions & 1 rubygems

Version Path
vedeu-0.4.13 lib/vedeu/geometry/index_position.rb
vedeu-0.4.12 lib/vedeu/geometry/index_position.rb
vedeu-0.4.11 lib/vedeu/geometry/index_position.rb
vedeu-0.4.10 lib/vedeu/geometry/index_position.rb