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+)?)|(next))(?:,(#{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
}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Box

#to_frame_margins, #within_frames

Constructor Details

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

Returns a new instance of GridBox.



100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# 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

Instance Attribute Details

#xObject

The position of the element in the grid (arrays [left,right] or [top, bottom])



49
50
51
# File 'lib/ctioga2/graphics/types/grid.rb', line 49

def x
  @x
end

#yObject

The position of the element in the grid (arrays [left,right] or [top, bottom])



49
50
51
# File 'lib/ctioga2/graphics/types/grid.rb', line 49

def y
  @y
end

Class Method Details

.from_text(txt) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# 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

Instance Method Details

#classesObject



142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
# 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) ⇒ Object



77
78
79
80
81
82
83
84
85
# 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) ⇒ Object



127
128
129
130
131
132
133
134
135
136
137
138
139
140
# 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?Boolean

Returns false if the position given by @x and @y are within the grid

Returns:

  • (Boolean)


89
90
91
92
93
94
95
96
97
# 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