class CTioga2::Graphics::Types::GridLayout

This class provides a grid-like layout through the use of a grid setup command and a grid box specification.

Attributes

delta_x[RW]

The X offset to go from the right-hand side of one element to the left-hand-side of the next

delta_y[RW]

The Y offset to go from the bottom of one element to the top of the next.

elements[RW]

The GridBox objects we've seen so far

hscales[RW]

Horizontal scales

nup[RW]

The nup: an array nb horizontal, nb vertical

outer_margins[RW]

The margins (left, right, top, bottom) around the whole grid

vscales[RW]

Vertical scales

Public Class Methods

current_grid() click to toggle source
# File lib/ctioga2/graphics/types/grid.rb, line 254
def self.current_grid
  return @current_grid
end
current_grid=(grid) click to toggle source
# File lib/ctioga2/graphics/types/grid.rb, line 250
def self.current_grid=(grid)
  @current_grid = grid
end
new(nup = "2x2") click to toggle source
# File lib/ctioga2/graphics/types/grid.rb, line 212
def initialize(nup = "2x2")
  if nup.respond_to?(:split)
    if nup =~ /,/
      @hscales, @vscales = nup.split(/\s*x\s*/).map { |x| 
        x.split(/\s*,\s*/).map { |y| y.to_f }
      }
      if @hscales.size == 1
        @hscales = [1] * @hscales[0].to_i
      elsif @vscales.size == 1
        @vscales = [1] * @vscales[0].to_i
      end
      @nup = [@hscales.size, @vscales.size]
    else
      @nup = nup.split(/\s*x\s*/).map { |s| s.to_i }
    end
  else
    @nup = nup.dup
  end

  # Initialize with the given
  @outer_margins = {
    'left' =>  Dimension.new(:dy, 2.5, :x),
    'right' => Dimension.new(:bp, 6, :x),
    'bottom' =>  Dimension.new(:dy, 2.5, :y),
    'top' => Dimension.new(:dy, 2.5, :y)
  }
  @delta_x = Dimension.new(:dy, 2.5, :x)
  @delta_y = Dimension.new(:dy, 2.5, :y)

  @hscales ||= [1] * @nup[0]
  @vscales ||= [1] * @nup[1]

  @elements = []
end

Public Instance Methods

frame_coordinates(t, x, y) click to toggle source

Compute the frame coordinates fo the x,y element of the grid. They are numbered from the top,left element.

# File lib/ctioga2/graphics/types/grid.rb, line 268
def frame_coordinates(t, x, y)
  compute_lengths(t)
  xo = if x > 0
         @hscales[0..(x-1)].inject(0,:+) * @wbase
       else
         0
       end
  xl = @outer_margins['left'].to_frame(t, :x) + xo + 
    x * @delta_x.to_frame(t, :x)
  yo = if y > 0
         @vscales[0..(y-1)].inject(0,:+) * @hbase
       else
         0
       end
  yt = 1 - (@outer_margins['top'].to_frame(t, :y) + yo +
            y * @delta_y.to_frame(t, :y))
  return [xl, yt, 
          xl + @wbase * @hscales[x], 
          yt - @hbase * @vscales[y]]
end
xsize() click to toggle source
# File lib/ctioga2/graphics/types/grid.rb, line 258
def xsize
  return @hscales.size
end
ysize() click to toggle source
# File lib/ctioga2/graphics/types/grid.rb, line 262
def ysize
  return @vscales.size
end

Protected Instance Methods

compute_lengths(t) click to toggle source

Compute the necessary variables in frame coordinates

# File lib/ctioga2/graphics/types/grid.rb, line 292
def compute_lengths(t)
  return if (@wbase && @hbase)
  @wbase = (1 - 
    (@outer_margins['left'].to_frame(t, :x) + 
     @outer_margins['right'].to_frame(t, :x) + 
     @delta_x.to_frame(t, :x) * (@nup[0]-1)))/@hscales.inject(0,:+)
  @hbase = (1 - 
    (@outer_margins['top'].to_frame(t, :y) + 
     @outer_margins['bottom'].to_frame(t, :y) + 
     @delta_y.to_frame(t, :y) * (@nup[1]-1)))/@vscales.inject(0,:+)
end