Class: Wx::SF::ShapeList

Inherits:
Object
  • Object
show all
Includes:
Enumerable, FIRM::Serializable
Defined in:
lib/wx/shapes/shape_list.rb

Overview

This class implements an indexed container for unique, non-nil, shapes (no duplicates).

Instance Method Summary collapse

Constructor Details

#initialize(enum = nil) ⇒ ShapeList

Constructor.

Parameters:

  • enum (ShapeList, ::Enumerable, Shape, nil) (defaults to: nil)

    shape container to copy, single shape to add or nil



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/wx/shapes/shape_list.rb', line 19

def initialize(enum = nil)
  if enum
    if enum.is_a?(ShapeList)
      @list = enum.instance_variable_get('@list').dup
    elsif enum.is_a?(::Enumerable)
      @list = ::Set.new
      enum.each { |elem| self << elem }
    else
      @list = ::Set.new
      self << enum
    end
  else
    @list = ::Set.new
  end
end

Instance Method Details

#all(collection = []) ⇒ Object

Recursively collects shapes and returns collection.

Parameters:

  • collection (Array<Shape>) (defaults to: [])

    container to return collected shapes in



52
53
54
# File 'lib/wx/shapes/shape_list.rb', line 52

def all(collection = [])
  @list.inject(collection.concat(@list.to_a)) { |list, shape| shape.instance_variable_get('@child_shapes').all(list) }
end

#append(shape) ⇒ self Also known as: push, <<

Appends a new shape to the list if not yet in list. Does not perform a recursive check.

Parameters:

  • shape (Shape)

    shape to add

Returns:

  • (self)


73
74
75
76
77
78
# File 'lib/wx/shapes/shape_list.rb', line 73

def append(shape)
  unless @list.include?(check_elem(shape))
    @list << shape
  end
  self
end

#clearself

Empties the shape list.

Returns:

  • (self)


64
65
66
67
# File 'lib/wx/shapes/shape_list.rb', line 64

def clear
  @list.clear
  self
end

#delete(shape) ⇒ Shape?

Removes the given shape from the list and returns that.

Parameters:

  • shape (Shape)

    shape to match

Returns:

  • (Shape, nil)

    removed shape or nil if none matched



85
86
87
88
89
90
91
92
# File 'lib/wx/shapes/shape_list.rb', line 85

def delete(shape)
  if @list.include?(check_elem(shape))
    @list.delete(shape)
    shape
  else
    nil
  end
end

#each {|shape| ... } ⇒ self

Iterates over contained shapes. When a block given, passes each successive shape to the block. Allows the array to be modified during iteration. When no block given, returns a new ::Enumerator.

Yield Parameters:

Returns:

  • (self)


41
42
43
44
45
46
47
48
# File 'lib/wx/shapes/shape_list.rb', line 41

def each(&block)
  if block_given?
    @list.each(&block)
    self
  else
    @list.each
  end
end

#empty?Boolean

Returns true if no shapes are contained, false otherwise.

Returns:

  • (Boolean)


58
59
60
# File 'lib/wx/shapes/shape_list.rb', line 58

def empty?
  @list.empty?
end

#include?(shape, recursive = false) ⇒ Boolean

Returns true if the given shape is included in the list. Performs a recursive search in case :recursive is true.

Parameters:

  • shape (Shape)

    shape to match

  • recursive (Boolean) (defaults to: false)

    pass true to search recursively, false for non-recursive

Returns:

  • (Boolean)


99
100
101
102
# File 'lib/wx/shapes/shape_list.rb', line 99

def include?(shape, recursive = false)
  found = @list.include?(check_elem(shape))
  found || (recursive && @list.any? { |child| child.include_child_shape?(shape, recursive) })
end