Class: Fidgit::ComboBox

Inherits:
Button show all
Extended by:
Forwardable
Defined in:
lib/fidgit/elements/combo_box.rb

Constant Summary collapse

ARROW_IMAGE =
"combo_arrow.png"

Constants inherited from Label

Label::ICON_POSITIONS

Constants inherited from Composite

Fidgit::Composite::DEBUG_BORDER_COLOR

Constants inherited from Element

Element::DEFAULT_SCHEMA_FILE, Element::VALID_ALIGN_H, Element::VALID_ALIGN_V

Instance Attribute Summary

Attributes inherited from Label

#background_color, #border_color, #icon_position

Attributes inherited from Packer

#spacing_h, #spacing_v

Attributes inherited from Element

#align_h, #align_v, #background_color, #border_thickness, #font, #padding_bottom, #padding_left, #padding_right, #padding_top, #parent, #tip, #z

Instance Method Summary collapse

Methods inherited from Button

#activate, #enabled=, #enter, #leave, #parent=, #text=

Methods inherited from Label

#hit_element, #icon, #icon=

Methods inherited from Container

#add, #button, #color_picker, #color_well, #combo_box, #file_browser, #grid, #group, #hit_element, #horizontal, #image_frame, #insert, #label, #list, #radio_button, #remove, #scroll_area, #scroll_window, #slider, #text_area, #to_s, #toggle_button, #update, #vertical, #write_tree, #x=, #y=

Methods inherited from Element

#default, #drag?, #draw_frame, #draw_rect, #enabled=, #enabled?, #height, #height=, #hit?, #max_height, #max_width, #min_height, #min_width, new, original_new, #outer_height, #outer_width, #recalc, schema, #to_s, #update, #width, #width=, #with, #x, #x=, #y, #y=

Methods included from Event

#events, included, new_event_handlers, #publish, #subscribe, #unsubscribe

Constructor Details

#initialize(options = {}, &block) ⇒ ComboBox

Returns a new instance of ComboBox.

Parameters:

  • text (String)

    The string to display in the label.

  • options (Hash) (defaults to: {})

    a customizable set of options

Options Hash (options):

  • [] (Object)

    :value

  • :shortcut (Symbol) — default: nil

    Adds a shortcut key for this element, that activates it. :auto takes the first letter of the text.

  • :icon (Gosu::Image, nil) — default: nil
  • :justify (:left, :right, :center) — default: :left

    Text justification.


40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/fidgit/elements/combo_box.rb', line 40

def initialize(options = {}, &block)
  options = {
    background_color: default(:background_color),
    border_color: default(:border_color),
  }.merge! options

  @value = options[:value]
  
  @hover_index = 0

  @menu = MenuPane.new(show: false) do
    subscribe :selected do |widget, value|
      self.value = value
    end
  end

  @@arrow ||= Gosu::Image[ARROW_IMAGE]

  super('', options)

  rect.height = [height, font.height + padding_top + padding_bottom].max
  rect.width = [width, font.height * 4 + padding_left + padding_right].max
end

Instance Method Details

#clearObject


92
93
94
95
96
# File 'lib/fidgit/elements/combo_box.rb', line 92

def clear
  self.text = ""
  self.icon = nil
  @menu.clear
end

#clicked_left_mouse_button(sender, x, y) ⇒ Object


84
85
86
87
88
89
90
# File 'lib/fidgit/elements/combo_box.rb', line 84

def clicked_left_mouse_button(sender, x, y)
  @menu.x = self.x
  @menu.y = self.y + height + border_thickness
  $window.game_state_manager.current_game_state.show_menu @menu

  nil
end

#drawObject


78
79
80
81
82
# File 'lib/fidgit/elements/combo_box.rb', line 78

def draw
  super
  size = height / @@arrow.width.to_f
  @@arrow.draw x + width - height, y, z, size, size
end

#indexObject


14
# File 'lib/fidgit/elements/combo_box.rb', line 14

def index; @menu.index(@value) end

#index=(index) ⇒ Object


29
30
31
32
33
34
35
# File 'lib/fidgit/elements/combo_box.rb', line 29

def index=(index)
  if index.between?(0, @menu.size - 1)
    self.value = @menu[index].value
  end

  index
end

#item(text, value, options = {}, &block) ⇒ Object


64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/fidgit/elements/combo_box.rb', line 64

def item(text, value, options = {}, &block)
  item = @menu.item(text, value, options, &block)

  # Force text to be updated if the item added has the same value.
  if item.value == @value
    self.text = item.text
    self.icon = item.icon
  end

  recalc

  item
end

#valueObject


15
# File 'lib/fidgit/elements/combo_box.rb', line 15

def value; @value; end

#value=(value) ⇒ Object


17
18
19
20
21
22
23
24
25
26
27
# File 'lib/fidgit/elements/combo_box.rb', line 17

def value=(value)
  if @value != value
    @value = value
    item = @menu.find(@value)
    self.text = item.text
    self.icon = item.icon
    publish :changed, @value
  end

  value
end