class Cura::Component::Pack

A component with children which moves and optionally resizes them. TODO: Expand attribute - See: www.pygtk.org/pygtk2tutorial/sec-DetailsOfBoxes.html TODO: I think the only time it needs to pack_children is right before drawing? Would that get messy?

Attributes

spacing[R]

Get the spacing between children.

@return [Integer]

Public Class Methods

new(attributes={}) click to toggle source
Calls superclass method Cura::Attributes::HasOrientation::new
# File lib/cura/component/pack.rb, line 14
def initialize(attributes={})
  @fill = false
  @spacing = 0

  super
end

Public Instance Methods

add_child(component_or_type, attributes={}) click to toggle source

Add a child to this group.

@param [#to_sym, Component] component_or_type

A Symbol representing the child component type or a {Component::Base} instance.
When a Symbol is given, a new child component will be initialized of that type. See {Component::Base.type}.

@param [#to_h] attributes

When component_or_type is a Symbol, then these attributes will be used to initialize the child component.
When component_or_type is a {Component::Base}, then these attributes will be used to update the child component.

@option options [#to_i] :expand

The new child is to be given extra space. The extra space will be divided evenly between all children that use this option.

@option options [#to_i] :fill

The space given to child by the expand option is actually allocated to child, rather than just padding it.
This parameter has no effect if expand is set to false.

@return [Component]

Calls superclass method Cura::Component::Group#add_child
# File lib/cura/component/pack.rb, line 53
def add_child(component_or_type, attributes={})
  # TODO: :expand and :fill attributes?
  child = super

  pack_children

  child
end
delete_child_at(index) click to toggle source

Remove a child from this object's children at the given index.

@param [#to_i] index @return [Component]

Calls superclass method Cura::Component::Group#delete_child_at
# File lib/cura/component/pack.rb, line 66
def delete_child_at(index)
  child = super

  pack_children

  child
end
draw() click to toggle source

Draw this pack.

@return [Pack]

Calls superclass method Cura::Component::Group#draw
# File lib/cura/component/pack.rb, line 115
def draw
  pack_children # TODO: Should be in #update?

  super
end
fill=(value) click to toggle source

Set whether children will be filled. Must be truthy or falsey.

If set to a truthy value, children will be resized to fit the space available to it. For example, if orientation is set to :horizontal, then all of the children's width attributes will be set to this instance's width.

@param [Object] value @return [Boolean]

# File lib/cura/component/pack.rb, line 92
def fill=(value)
  @fill = !!value
end
fill?() click to toggle source

Get whether children will be filled. If this pack's orientation is set to :vertical, then the children's width will be set to this pack's width. If this pack's orientation is set to :horizontal, then the children's height will be set to this pack's width.

@return [Boolean]

# File lib/cura/component/pack.rb, line 79
def fill?
  @fill
end
height=(value) click to toggle source

Set the height dimension of this pack.

Calls superclass method
# File lib/cura/component/pack.rb, line 31
def height=(value)
  result = super

  pack_children

  result
end
spacing=(value) click to toggle source

Set the spacing between children.

@param [#to_i] value @return [Integer]

# File lib/cura/component/pack.rb, line 105
def spacing=(value)
  value = value.to_i
  value = 0 if value < 0

  @spacing = value
end
width=(value) click to toggle source

Set the width dimension of this pack.

Calls superclass method
# File lib/cura/component/pack.rb, line 22
def width=(value)
  result = super

  pack_children

  result
end

Protected Instance Methods

pack_children() click to toggle source

Position and resize this pack's children based on it's attributes.

# File lib/cura/component/pack.rb, line 124
def pack_children
  child_x = 0
  child_y = 0

  children.each do |child|
    if horizontal?
      child.x = child_x
      child_x += child.width + child.offsets.width + spacing

      child.height = height if fill?
    elsif vertical?
      child.y = child_y if vertical?
      child_y += child.height + child.offsets.height + spacing

      child.width = width if fill?
    end
  end
end