Class: CTioga2::Graphics::Types::GridBox

Inherits:
Box
  • Object
show all
Defined in:
lib/ctioga2/graphics/types/grid.rb

Overview

The position of a single element in a GridLayout

todo add the possibility to override one element of the final positions.

Constant Summary collapse

OptionHashRE =
/([\w-]+)\s*=\s*([^,]+),?\s*/
GridBoxRE =
/^\s*grid:(\d+)\s*,\s*(\d+)(?:,(#{OptionHashRE}+))?\s*$/
FrameCoordsOverride =

This hash helps to convert from a hash-based representation of frame coordinates to the array-based one.

todo I should either use existing code or refactor into something globally useful.

{ 'xl' => 0,
  'yt' => 1,
  'xr' => 2,
  'yb' => 3
}

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Box

#to_frame_margins

Constructor Details

#initialize(grid, x, y, options = {}) ⇒ GridBox

Returns a new instance of GridBox.



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/ctioga2/graphics/types/grid.rb', line 59

def initialize(grid, x, y, options = {})
  if options.is_a? String
    str = options
    options = {}
    str.split(/\s*,\s*/).map { |s|
      s =~ OptionHashRE
      options[$1] = 
      BaseCoordinate.from_text($2,if FrameCoordsOverride[$1] % 2 == 0
                                    :x
                                  else
                                    :y
                                  end, :frame)
    }
  end
  @x = x.to_i
  @grid = grid
  @y = y.to_i
  @overrides = options || {}
end

Class Method Details

.from_text(txt) ⇒ Object



49
50
51
52
53
54
55
56
57
# File 'lib/ctioga2/graphics/types/grid.rb', line 49

def self.from_text(txt)
  if txt =~ GridBoxRE
    return GridBox.new(GridLayout.current_grid, $1.to_i, $2.to_i, 
                       $3) # The latter being to remove
                                  # the initial comma
  else
    raise "#{txt} is not a grid box."
  end
end

Instance Method Details

#to_frame_coordinates(t) ⇒ Object



79
80
81
82
83
84
85
86
87
# File 'lib/ctioga2/graphics/types/grid.rb', line 79

def to_frame_coordinates(t)
  a = @grid.frame_coordinates(t, @x, @y)
  ## \todo write a framework for manipulating this !
  for k,v in @overrides
    next unless FrameCoordsOverride.key?(k)
    a[FrameCoordsOverride[k]] = v.to_frame(t)
  end
  return a
end