Class: Cura::Component::Listbox

Inherits:
Pack show all
Defined in:
lib/cura/component/listbox.rb

Overview

A component containing a selectable list of components.

Instance Attribute Summary collapse

Attributes inherited from Pack

#spacing

Attributes included from Attributes::HasOrientation

#orientation

Attributes included from Attributes::HasAncestry

#parent

Attributes included from Attributes::HasOffsets

#offsets

Attributes included from Attributes::HasEvents

#event_handler

Instance Method Summary collapse

Methods inherited from Pack

#draw, #fill=, #fill?, #height=, #width=

Methods included from Attributes::HasOrientation

#horizontal?, #vertical?

Methods included from Attributes::HasAttributes

included, #update_attributes

Methods inherited from Group

#draw, #height, #update, #width

Methods included from Attributes::HasChildren

#add_children, #children, #children?, #delete_child, #delete_children, #each

Methods inherited from Base

#application, #background, #contains_coordinates?, #cursor, #draw, #focus, #focused?, #foreground, #inspect, #pencil, #update

Methods included from Attributes::HasRelativeCoordinates

#absolute_x, #absolute_y

Methods included from Attributes::HasCoordinates

#x, #x=, #y, #y=

Methods included from Attributes::HasAncestry

#ancestors, #parent?

Methods included from Attributes::HasOffsets

#border, #border=, #margin, #margin=, #padding, #padding=

Methods included from Attributes::HasColors

#background, #background=, #foreground, #foreground=

Methods included from Attributes::HasFocusability

#focusable=, #focusable?

Methods included from Attributes::HasEvents

included, #on_event

Methods included from Attributes::HasDimensions

#height, #height=, #resize, #width, #width=

Constructor Details

#initialize(attributes = {}) ⇒ Listbox

Returns a new instance of Listbox.



42
43
44
45
46
47
48
49
# File 'lib/cura/component/listbox.rb', line 42

def initialize(attributes={})
  @focusable = true
  @loopable = true
  @selected_index = 0
  @objects = []
  
  super
end

Instance Attribute Details

#selected_indexnil, Integer

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

Returns:

  • (nil, Integer)


55
56
57
# File 'lib/cura/component/listbox.rb', line 55

def selected_index
  @selected_index
end

Instance Method Details

#add_child(component, object = nil) ⇒ Component

Add a child to this group.

Parameters:

  • component (Component)
  • object (Object) (defaults to: nil)

    An arbitrary object to associate with the added child in this listbox.

Returns:



91
92
93
94
95
96
97
# File 'lib/cura/component/listbox.rb', line 91

def add_child(component, object=nil)
  child = super(component)

  @objects << object

  child
end

#delete_child_at(index) ⇒ Component

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

Parameters:

  • index (#to_i)

Returns:



103
104
105
106
107
108
109
110
# File 'lib/cura/component/listbox.rb', line 103

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) ⇒ Boolean

Set whether this listbox is loopable or not.

Parameters:

  • value (Object)

Returns:

  • (Boolean)


138
139
140
# File 'lib/cura/component/listbox.rb', line 138

def loopable=(value)
  @loopable = !!value
end

#loopable?Boolean

Get whether this listbox is loopable or not.

Returns:

  • (Boolean)


130
131
132
# File 'lib/cura/component/listbox.rb', line 130

def loopable?
  @loopable
end

#object_at(index) ⇒ Object

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

Parameters:

  • index (#to_i)

Returns:

  • (Object)


116
117
118
# File 'lib/cura/component/listbox.rb', line 116

def object_at(index)
  @objects[index]
end

#selected_childComponent

Get the child at the selected index.

Returns:



82
83
84
# File 'lib/cura/component/listbox.rb', line 82

def selected_child
  @children[@selected_index]
end

#selected_objectComponent

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

Returns:



123
124
125
# File 'lib/cura/component/listbox.rb', line 123

def selected_object
  @objects[@selected_index]
end