Class: CyberarmEngine::Element::ListBox

Inherits:
Button show all
Defined in:
lib/cyberarm_engine/ui/elements/list_box.rb

Constant Summary

Constants included from Theme

Theme::THEME

Instance Attribute Summary collapse

Attributes inherited from CyberarmEngine::Element

#background_canvas, #border_canvas, #enabled, #event_handler, #options, #parent, #style, #tip, #x, #y, #z

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Button

#blur, #clicked_left_mouse_button, #draw_image, #draw_text, #enter, #leave, #left_mouse_button, #render, #value, #value=

Methods inherited from Label

#clicked_left_mouse_button, #handle_text_wrapping, #line_width, #render, #value, #value=

Methods inherited from CyberarmEngine::Element

#background=, #button_down, #button_up, #content_height, #content_width, #default_events, #dimensional_size, #draggable?, #draw, #enabled?, #height, #hide, #hit?, #inner_height, #inner_width, #is_root?, #noncontent_height, #noncontent_width, #outer_height, #outer_width, #render, #reposition, #root, #set_background, #set_border_color, #set_border_thickness, #set_margin, #set_padding, #set_static_position, #show, #stylize, #to_s, #toggle, #update, #update_background, #value, #value=, #visible?, #width

Methods included from Common

#current_state, #darken, #draw_rect, #fill, #get_asset, #get_image, #get_sample, #get_song, #lighten, #opacity, #pop_state, #previous_state, #push_state, #show_cursor, #show_cursor=, #window

Methods included from CyberarmEngine::Event

#event, #publish, #subscribe, #unsubscribe

Methods included from Theme

#deep_merge, #default, #theme_defaults

Constructor Details

#initialize(options = {}, block = nil) ⇒ ListBox

Returns a new instance of ListBox.



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/cyberarm_engine/ui/elements/list_box.rb', line 7

def initialize(options = {}, block = nil)
  @items = options[:items] || []
  @choose = options[:choose] || @items.first

  super(@choose, options, block)

  @style.background_canvas.background = default(:background)

  # TODO: "Clean Up" into own class?
  @menu = Stack.new(parent: parent, width: @options[:width], theme: @options[:theme])
  @menu.define_singleton_method(:recalculate_menu) do
    @x = @__list_box.x
    @y = @__list_box.y + @__list_box.height
  end
  @menu.instance_variable_set(:"@__list_box", self)

  def @menu.recalculate
    super
    recalculate_menu
  end
end

Instance Attribute Details

#chooseObject

Returns the value of attribute choose.



5
6
7
# File 'lib/cyberarm_engine/ui/elements/list_box.rb', line 5

def choose
  @choose
end

#itemsObject

Returns the value of attribute items.



4
5
6
# File 'lib/cyberarm_engine/ui/elements/list_box.rb', line 4

def items
  @items
end

Class Method Details

.recalculateObject



23
24
25
26
# File 'lib/cyberarm_engine/ui/elements/list_box.rb', line 23

def @menu.recalculate
  super
  recalculate_menu
end

Instance Method Details

#recalculateObject



61
62
63
64
65
# File 'lib/cyberarm_engine/ui/elements/list_box.rb', line 61

def recalculate
  super

  @menu.recalculate
end

#released_left_mouse_button(_sender, _x, _y) ⇒ Object



40
41
42
43
44
# File 'lib/cyberarm_engine/ui/elements/list_box.rb', line 40

def released_left_mouse_button(_sender, _x, _y)
  show_menu

  :handled
end

#show_menuObject



46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/cyberarm_engine/ui/elements/list_box.rb', line 46

def show_menu
  @menu.clear
  @items.each do |item|
    [@block]
    block = proc { self.choose = item; @block.call(item) if @block }
    b = Button.new(item,
                   { parent: @menu, width: 1.0, theme: @options[:theme], margin: 0, border_color: 0x00ffffff }, block)

    @menu.add(b)
  end
  recalculate

  root.gui_state.show_menu(@menu)
end