Class: GTK::Layout

Inherits:
Object show all
Defined in:
dragon/layout.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(w, h) ⇒ Layout

Returns a new instance of Layout.


192
193
194
195
196
197
# File 'dragon/layout.rb', line 192

def initialize w, h
  @w = w
  @h = h
  @rect_cache = {}
  init_device @w, @h
end

Instance Attribute Details

#hObject (readonly)

Returns the value of attribute h


190
191
192
# File 'dragon/layout.rb', line 190

def h
  @h
end

#rect_cacheObject (readonly)

Returns the value of attribute rect_cache


190
191
192
# File 'dragon/layout.rb', line 190

def rect_cache
  @rect_cache
end

#wObject (readonly)

Returns the value of attribute w


190
191
192
# File 'dragon/layout.rb', line 190

def w
  @w
end

Instance Method Details

#cell_heightObject


295
296
297
# File 'dragon/layout.rb', line 295

def cell_height
  device.control_area.cell_size
end

#cell_widthObject


299
300
301
# File 'dragon/layout.rb', line 299

def cell_width
  device.control_area.cell_size
end

#col_countObject


279
280
281
# File 'dragon/layout.rb', line 279

def col_count
  device.grid_area.col_count
end

#control_rectObject


268
269
270
271
272
273
# File 'dragon/layout.rb', line 268

def control_rect
  @control_rect ||= { x: device.control_area.margin.left,
                      y: device.control_area.margin.top,
                      w: device.control_area.w,
                      h: device.control_area.h }
end

#deviceObject


431
432
433
# File 'dragon/layout.rb', line 431

def device
  @device
end

#font_pt_to_px(pt) ⇒ Object


222
223
224
# File 'dragon/layout.rb', line 222

def font_pt_to_px pt
  pt * 1.333333
end

#font_px_to_pt(px) ⇒ Object


218
219
220
# File 'dragon/layout.rb', line 218

def font_px_to_pt px
  (px / 1.33333).floor
end

#font_relative_size_enum(size_enum) ⇒ Object


210
211
212
213
214
215
216
# File 'dragon/layout.rb', line 210

def font_relative_size_enum size_enum
  base_line_logical = 22
  base_line_actual = font_size_med
  target_logical = size_enum
  target_logical = 1 if target_logical <= 0
  (base_line_actual / base_line_logical) * target_logical
end

#font_sizeObject


250
251
252
# File 'dragon/layout.rb', line 250

def font_size
  font_size_cell * 0.7
end

#font_size_cellObject


226
227
228
# File 'dragon/layout.rb', line 226

def font_size_cell
  (cell_height / 1.33333)
end

#font_size_lgObject


234
235
236
# File 'dragon/layout.rb', line 234

def font_size_lg
  font_size_cell * 0.8
end

#font_size_medObject


238
239
240
# File 'dragon/layout.rb', line 238

def font_size_med
  font_size_cell * 0.7
end

#font_size_smObject


242
243
244
# File 'dragon/layout.rb', line 242

def font_size_sm
  font_size_cell * 0.6
end

#font_size_xlObject


230
231
232
# File 'dragon/layout.rb', line 230

def font_size_xl
  font_size_cell
end

#font_size_xsObject


246
247
248
# File 'dragon/layout.rb', line 246

def font_size_xs
  font_size_cell * 0.5
end

#gutter_heightObject


283
284
285
# File 'dragon/layout.rb', line 283

def gutter_height
  device.grid_area.gutter
end

#gutter_widthObject


287
288
289
# File 'dragon/layout.rb', line 287

def gutter_width
  device.grid_area.gutter
end

#init_device(w, h) ⇒ Object


435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
# File 'dragon/layout.rb', line 435

def init_device w, h
  @device      = Device.new
  @device.w    = w
  @device.h    = h
  @device.name = "Device"
  @device.aspect.w = (u_for_16x9 w, h)[:w]
  @device.aspect.h = (u_for_16x9 w, h)[:h]
  @device.aspect.u = (u_for_16x9 w, h)[:u]
  @device.safe_area.w             = @device.aspect.u * 16
  @device.safe_area.h             = @device.aspect.u * 9
  @device.safe_area.margin.left   = ((@device.w - @device.safe_area.w).fdiv 2).floor
  @device.safe_area.margin.right  = ((@device.w - @device.safe_area.w).fdiv 2).floor
  @device.safe_area.margin.top    = ((@device.h - @device.safe_area.h).fdiv 2).floor
  @device.safe_area.margin.bottom = ((@device.h - @device.safe_area.h).fdiv 2).floor
  @device.grid_area.outer_gutter  = @device.w / 80
  @device.grid_area.gutter        = @device.w / 160

  @device.grid_area.w = @device.safe_area.w - (@device.grid_area.outer_gutter * 2)
  @device.grid_area.h = @device.safe_area.h - (@device.grid_area.outer_gutter * 2)

  @device.grid_area.margin.left   = ((@device.w - @device.grid_area.w).fdiv 2).floor
  @device.grid_area.margin.right  = ((@device.w - @device.grid_area.w).fdiv 2).floor
  @device.grid_area.margin.top    = ((@device.h - @device.grid_area.h).fdiv 2).floor
  @device.grid_area.margin.bottom = ((@device.h - @device.grid_area.h).fdiv 2).floor

  @device.grid_area.col_count = 24
  @device.grid_area.row_count = 12
  @device.grid_area.cell_w = ((@device.aspect.w - (@device.grid_area.outer_gutter * 2)) - ((@device.grid_area.col_count - 1) * @device.grid_area.gutter)).fdiv @device.grid_area.col_count
  @device.grid_area.cell_h = ((@device.aspect.h - (@device.grid_area.outer_gutter * 2)) - ((@device.grid_area.row_count - 1) * @device.grid_area.gutter)).fdiv @device.grid_area.row_count

  @device.control_area.cell_size = @device.grid_area.cell_w
  @device.control_area.cell_size = @device.grid_area.cell_h if @device.grid_area.cell_h < @device.grid_area.cell_w && @device.grid_area.cell_h > 0
  @device.control_area.cell_size = @device.control_area.cell_size.floor
  @device.control_area.w = (@device.control_area.cell_size * @device.grid_area.col_count) + (@device.grid_area.gutter * (@device.grid_area.col_count - 1))
  @device.control_area.h = (@device.control_area.cell_size * @device.grid_area.row_count) + (@device.grid_area.gutter * (@device.grid_area.row_count - 1))
  @device.control_area.margin.left  = (@device.w - @device.control_area.w).fdiv 2
  @device.control_area.margin.right  = (@device.w - @device.control_area.w).fdiv 2
  @device.control_area.margin.top  = (@device.h - @device.control_area.h).fdiv 2
  @device.control_area.margin.bottom  = (@device.h - @device.control_area.h).fdiv 2
  @device
end

#inspectObject


483
484
485
# File 'dragon/layout.rb', line 483

def inspect
  serialize.to_s
end

#logical_rectObject


254
255
256
257
258
259
# File 'dragon/layout.rb', line 254

def logical_rect
  @logical_rect ||= { x: 0,
                      y: 0,
                      w: @w,
                      h: @h }
end

#outer_gutterObject


291
292
293
# File 'dragon/layout.rb', line 291

def outer_gutter
  device.grid_area.outer_gutter
end

#rect(opts) ⇒ Object


315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
# File 'dragon/layout.rb', line 315

def rect opts
  opts = rect_defaults.merge opts
  result = send opts[:rect]
  if opts[:row] && opts[:col] && opts[:w] && opts[:h]
    col = rect_col opts[:col], opts[:w]
    row = rect_row opts[:row], opts[:h]
    result = control_rect.merge x: col.x,
                                y: row.y,
                                w: col.w,
                                h: row.h
  elsif opts[:row] && !opts[:col]
    result = rect_row opts[:row], opts[:h]
  elsif !opts[:row] && opts[:col]
    result = rect_col opts[:col], opts[:w]
  else
    raise "LayoutTheory::rect unable to process opts #{opts}."
  end

  if opts[:max_height] && opts[:max_height] >= 0
    if result[:h] > opts[:max_height]
      delta = (result[:h] - opts[:max_height]) * 2
      result[:y] += delta
      result[:h] = opts[:max_height]
    end
  end

  if opts[:max_width] && opts[:max_width] >= 0
    if result[:w] > opts[:max_width]
      delta = (result[:w] - opts[:max_width]) * 2
      result[:x] += delta
      result[:w] = opts[:max_width]
    end
  end

  result[:x] += opts[:dx]
  result[:y] += opts[:dy]

  if opts[:include_row_gutter]
    result[:x] -= device.grid_area.gutter
    result[:w] += device.grid_area.gutter * 2
  end

  if opts[:include_col_gutter]
    result[:y] -= device.grid_area.gutter
    result[:h] += device.grid_area.gutter * 2
  end

  result[:x] += opts[:dx] if opts[:dx]
  result[:y] += opts[:dy] if opts[:dy]
  result[:w] += opts[:dw] if opts[:dw]
  result[:h] += opts[:dh] if opts[:dh]
  result[:row] = opts[:row]
  result[:col] = opts[:col]

  if $gtk.args.grid.name == :center
    result[:x] -= 640
    result[:y] -= 360
  end

  result
end

#rect_center(reference, target) ⇒ Object


377
378
379
380
381
# File 'dragon/layout.rb', line 377

def rect_center reference, target
  delta_x = (reference.w - target.w).fdiv 2
  delta_y = (reference.h - target.h).fdiv 2
  [target.x - delta_x, target.y - delta_y, target.w, target.h]
end

#rect_col(index, w) ⇒ Object


408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
# File 'dragon/layout.rb', line 408

def rect_col index, w
  @rect_cache[:col] ||= {}
  @rect_cache[:col][index] ||= {}
  return @rect_cache[:col][index][w] if @rect_cache[:col][index][w]
  col_x = (control_rect.x) +
          (device.grid_area.gutter * index) +
          (device.control_area.cell_size * index)

  col_x = col_x.to_i
  col_x -= 1 if col_x.odd? && (index + 1) < @device.grid_area.col_count.half
  col_x += 1 if col_x.odd? && (index + 1) >= @device.grid_area.col_count.half

  col_w = (device.grid_area.gutter * (w - 1)) +
          (device.control_area.cell_size * w)

  col_w = col_w.to_i
  col_w -= 1 if col_w.odd?

  result = control_rect.merge x: col_x, w: col_w
  @rect_cache[:col][index][w] = result
  @rect_cache[:col][index][w]
end

#rect_defaultsObject


303
304
305
306
307
308
309
310
311
312
313
# File 'dragon/layout.rb', line 303

def rect_defaults
  {
    row: nil,
    col: nil,
    h:   1,
    w:   1,
    dx:  0,
    dy:  0,
    rect: :control_rect
  }
end

#rect_row(index, h) ⇒ Object


383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
# File 'dragon/layout.rb', line 383

def rect_row index, h
  @rect_cache[:row] ||= {}
  @rect_cache[:row][index] ||= {}
  return @rect_cache[:row][index][h] if @rect_cache[:row][index][h]
  row_h = (device.grid_area.gutter * (h - 1)) +
          (device.control_area.cell_size * h)

  row_h = row_h.to_i
  row_h -= 1 if row_h.odd?

  row_y = (control_rect.y) +
          (device.grid_area.gutter * index) +
          (device.control_area.cell_size * index)

  row_y = row_y.to_i
  row_y += 1 if row_y.odd? && (index + 1) > @device.grid_area.row_count.half
  row_y += 1 if row_y.odd? && (index + 1) <= @device.grid_area.row_count.half

  row_y = device.h - row_y - row_h

  result = control_rect.merge y: row_y, h: row_h
  @rect_cache[:row][index][h] = result
  @rect_cache[:row][index][h]
end

#row_countObject


275
276
277
# File 'dragon/layout.rb', line 275

def row_count
  device.grid_area.row_count
end

#safe_rectObject


261
262
263
264
265
266
# File 'dragon/layout.rb', line 261

def safe_rect
  @safe_rect ||= { x: 0,
                   y: 0,
                   w: @w,
                   h: @h }
end

#serializeObject


477
478
479
480
481
# File 'dragon/layout.rb', line 477

def serialize
  {
    device: @device.serialize,
  }
end

#to_sObject


487
488
489
# File 'dragon/layout.rb', line 487

def to_s
  serialize.to_s
end

#u_for_16x9(w, h) ⇒ Object


199
200
201
202
203
204
205
206
207
208
# File 'dragon/layout.rb', line 199

def u_for_16x9 w, h
  u = (w.fdiv 16).floor
  u = (h.fdiv 9).floor if (u * 9) > h

  {
    u: u,
    w: u * 16,
    h: u * 9
  }
end