Class: CTioga2::Graphics::Types::GridLayout

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

Overview

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(nup = "2x2") ⇒ GridLayout

Returns a new instance of GridLayout.



212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
# 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

Instance Attribute Details

#delta_xObject

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



194
195
196
# File 'lib/ctioga2/graphics/types/grid.rb', line 194

def delta_x
  @delta_x
end

#delta_yObject

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



198
199
200
# File 'lib/ctioga2/graphics/types/grid.rb', line 198

def delta_y
  @delta_y
end

#elementsObject

The GridBox objects we’ve seen so far



210
211
212
# File 'lib/ctioga2/graphics/types/grid.rb', line 210

def elements
  @elements
end

#hscalesObject

Horizontal scales



204
205
206
# File 'lib/ctioga2/graphics/types/grid.rb', line 204

def hscales
  @hscales
end

#nupObject

The nup: an array nb horizontal, nb vertical



201
202
203
# File 'lib/ctioga2/graphics/types/grid.rb', line 201

def nup
  @nup
end

#outer_marginsObject

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



190
191
192
# File 'lib/ctioga2/graphics/types/grid.rb', line 190

def outer_margins
  @outer_margins
end

#vscalesObject

Vertical scales



207
208
209
# File 'lib/ctioga2/graphics/types/grid.rb', line 207

def vscales
  @vscales
end

Class Method Details

.current_gridObject



254
255
256
# File 'lib/ctioga2/graphics/types/grid.rb', line 254

def self.current_grid
  return @current_grid
end

.current_grid=(grid) ⇒ Object



250
251
252
# File 'lib/ctioga2/graphics/types/grid.rb', line 250

def self.current_grid=(grid)
  @current_grid = grid
end

Instance Method Details

#frame_coordinates(t, x, y) ⇒ Object

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



268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
# 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

#xsizeObject



258
259
260
# File 'lib/ctioga2/graphics/types/grid.rb', line 258

def xsize
  return @hscales.size
end

#ysizeObject



262
263
264
# File 'lib/ctioga2/graphics/types/grid.rb', line 262

def ysize
  return @vscales.size
end