class Cura::Component::Listbox

A component containing a selectable list of components.

Attributes

objects[R]

Get the objects stored.

@return [Array]

selected_index[R]

Get the currently selected item's index. Returns `nil` is nothing is selected.

@return [nil, Integer]

Public Class Methods

new(attributes={}) click to toggle source
Calls superclass method
# File lib/cura/component/listbox.rb, line 40
def initialize(attributes={})
  @focusable = true
  @loopable = true
  @selected_index = 0
  @objects = []

  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 attributes [Object] :object An arbitrary object to associate with the added child in this listbox. @return [Component]

Calls superclass method
# File lib/cura/component/listbox.rb, line 107
def add_child(component_or_type, attributes={})
  object = attributes.delete(:object)
  child = super

  @objects << object

  child
end
delete_child_at(index) click to toggle source

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

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

Calls superclass method
# File lib/cura/component/listbox.rb, line 120
def delete_child_at(index)
  deleted_child = super

  @objects.delete_at(index)
  self.selected_index = @children.length - 1 if @selected_index >= @children.length

  deleted_child
end
loopable=(value) click to toggle source

Set whether this listbox is loopable or not.

@param [Object] value @return [Boolean]

# File lib/cura/component/listbox.rb, line 160
def loopable=(value)
  @loopable = !!value
end
loopable?() click to toggle source

Get whether this listbox is loopable or not.

@return [Boolean]

# File lib/cura/component/listbox.rb, line 152
def loopable?
  @loopable
end
object_at(index) click to toggle source

Get the associated object with the child at the given index.

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

# File lib/cura/component/listbox.rb, line 138
def object_at(index)
  @objects[index]
end
selected_child() click to toggle source

Get the child at the selected index.

@return [Component]

# File lib/cura/component/listbox.rb, line 80
def selected_child
  @children[@selected_index]
end
selected_child=(child) click to toggle source

Set the selected child.

@param [Component] child @return [Component]

# File lib/cura/component/listbox.rb, line 88
def selected_child=(child)
  index = @children.index(child)

  return nil if index.nil? # TODO: Raise error?
  self.selected_index = index

  selected_child
end
selected_index=(value) click to toggle source

Set the currently selected item's index. Set to `nil` if nothing is selected.

@param [nil, to_i] value @return [nil, Integer]

# File lib/cura/component/listbox.rb, line 60
def selected_index=(value)
  value = value.to_i

  if @loopable
    value = count == 0 ? 0 : value % count # Avoids value = value % 0 (divide by zero error)
  else
    value = 0 if value <= 0
    value = count - 1 if value >= count - 1
  end

  @selected_index = value

  application.dispatch_event(:selected, target: self)

  @selected_index
end
selected_object() click to toggle source

Get the object associated with the child at the selected index.

@return [Component]

# File lib/cura/component/listbox.rb, line 145
def selected_object
  @objects[@selected_index]
end

Protected Instance Methods

set_cursor_position() click to toggle source
# File lib/cura/component/listbox.rb, line 166
def set_cursor_position
  cursor.x = @children.empty? ? absolute_x : selected_child.absolute_x
  cursor.y = @children.empty? ? absolute_y : selected_child.absolute_y
end