class Cura::Component::Listbox
A component containing a selectable list of components.
Attributes
Get the objects stored.
@return [Array]
Get the currently selected item's index. Returns `nil` is nothing is selected.
@return [nil, Integer]
Public Class Methods
# 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 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]
# 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
Remove a child from this listbox's children at the given index.
@param [#to_i] index @return [Component]
# 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
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
Get whether this listbox is loopable or not.
@return [Boolean]
# File lib/cura/component/listbox.rb, line 152 def loopable? @loopable end
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
Get the child at the selected index.
@return [Component]
# File lib/cura/component/listbox.rb, line 80 def selected_child @children[@selected_index] end
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
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
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
# 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