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+(?:-\d+)?)\s*,\s*(\d+(?:-\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.



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/ctioga2/graphics/types/grid.rb', line 67

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
  
  @grid = grid
  @x = parse_range(x).sort
  @y = parse_range(y).sort
  @overrides = options || {}
end

Class Method Details

.from_text(txt) ⇒ Object



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

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

Instance Method Details

#parse_range(str) ⇒ Object



57
58
59
60
61
62
63
64
65
# File 'lib/ctioga2/graphics/types/grid.rb', line 57

def parse_range(str)
  if str.is_a? Array
    return str
  elsif str =~ /(\d+)\s*-\s*(\d+)/
    return [$1.to_i, $2.to_i]
  else
    return [str.to_i, str.to_i]
  end
end

#to_frame_coordinates(t) ⇒ Object



88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/ctioga2/graphics/types/grid.rb', line 88

def to_frame_coordinates(t)
  a = @grid.frame_coordinates(t, @x[0], @y[0])
  ov = @grid.frame_coordinates(t, @x[1], @y[1])
  # Override with the right-bottom element.
  a[2] = ov[2]
  a[3] = ov[3]

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