class CTioga2::Graphics::Types::GridBox
The position of a single element in a GridLayout
todo add the possibility to override one element of the final positions.
Constants
- 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.
- GridBoxRE
- OptionHashRE
Attributes
x[RW]
The position of the element in the grid (arrays [left,right] or [top, bottom])
y[RW]
The position of the element in the grid (arrays [left,right] or [top, bottom])
Public Class Methods
from_text(txt)
click to toggle source
# File lib/ctioga2/graphics/types/grid.rb, line 51 def self.from_text(txt) if txt =~ GridBoxRE if $3 # next x = 0 y = 0 grd = GridLayout.current_grid lastel = grd.elements.last if lastel x = lastel.x.max + 1 y = lastel.y.max if x >= grd.xsize x = 0 y += 1 end end return GridBox.new(grd, x, y, $4) else return GridBox.new(GridLayout.current_grid, $1, $2, $4) # The latter being to remove # the initial comma end else raise "#{txt} is not a grid box." end end
new(grid, x, y, options = {})
click to toggle source
# File lib/ctioga2/graphics/types/grid.rb, line 100 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 || {} if ! within_grid? raise "Grid element #{x},#{y} is outside grid boundaries (#{@grid.xsize}x#{@grid.ysize})" end @grid.elements << self end
Public Instance Methods
classes()
click to toggle source
# File lib/ctioga2/graphics/types/grid.rb, line 142 def classes rv = [] hsh = { 'left' => @x.min == 0, 'right' => @x.max + 1 == @grid.xsize, 'top' => @y.min == 0, 'bottom' => @y.max + 1 == @grid.ysize } for k, v in hsh if v rv << "grid-#{k}" else rv << "grid-non-#{k}" end end xv = nil yv = nil if @x.min == @x.max xv = @x.min rv << "grid-column-#{xv}" if xv.even? rv << "grid-even-column" else rv << "grid-odd-column" end end if @y.min == @y.max yv = @y.min rv << "grid-row-#{yv}" if yv.even? rv << "grid-even-row" else rv << "grid-odd-row" end end if xv && yv rv << "grid-#{xv}-#{yv}" end return rv end
parse_range(str)
click to toggle source
# File lib/ctioga2/graphics/types/grid.rb, line 77 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)
click to toggle source
# File lib/ctioga2/graphics/types/grid.rb, line 127 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
within_grid?()
click to toggle source
Returns false if the position given by @x and @y are within the grid
# File lib/ctioga2/graphics/types/grid.rb, line 89 def within_grid? if @x.min < 0 || @x.max >= @grid.xsize return false end if @y.min < 0 || @y.max >= @grid.ysize return false end return true end