Class: Fidgit::ComboBox

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

Constant Summary

Constants inherited from Label

Label::VALID_JUSTIFICATION

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, #color, #icon, #text

Attributes inherited from Element

#align_h, #align_v, #background_color, #border_thickness, #font_size, #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

#draw_foreground

Methods inherited from Element

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

Methods included from Event

#events, included, #publish, #subscribe

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 (Fidgit::Thumbnail, Gosu::Image, nil) — default: nil
  • :justify (:left, :right, :center) — default: :left

    Text justification.



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

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["combo_arrow.png"]

  super('', options)

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

Instance Method Details

#clearObject



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

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

#clicked_left_mouse_button(sender, x, y) ⇒ Object



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

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



73
74
75
76
77
# File 'lib/fidgit/elements/combo_box.rb', line 73

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

#indexObject



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

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

#index=(index) ⇒ Object



26
27
28
29
30
31
32
# File 'lib/fidgit/elements/combo_box.rb', line 26

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

  index
end

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



61
62
63
64
65
66
67
68
69
70
71
# File 'lib/fidgit/elements/combo_box.rb', line 61

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

  item
end

#valueObject



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

def value; @value; end

#value=(value) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
# File 'lib/fidgit/elements/combo_box.rb', line 14

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