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

#draw_image, #draw_text, #render, #value, #value=

Methods inherited from TextBlock

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

Methods inherited from CyberarmEngine::Element

#background=, #blur, #button_down, #button_up, #clicked_left_mouse_button, #content_height, #content_width, #debug_draw, #default_events, #dimensional_size, #draggable?, #draw, #enabled?, #enter, #focus, #height, #hide, #hit?, #inner_height, #inner_width, #inspect, #is_root?, #leave, #left_mouse_button, #max_scroll_height, #max_scroll_width, #noncontent_height, #noncontent_width, #outer_height, #outer_width, #render, #reposition, #root, #safe_style_fetch, #scroll_height, #scroll_width, #set_background, #set_border_color, #set_border_thickness, #set_margin, #set_padding, #set_static_position, #show, #stylize, #to_s, #toggle, #update, #update_background, #update_styles, #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, #shift_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
28
29
30
# 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

  self.choose = @choose
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
27
# File 'lib/cyberarm_engine/ui/elements/list_box.rb', line 23

def @menu.recalculate
  super

  recalculate_menu
end

Instance Method Details

#recalculateObject



75
76
77
78
79
# File 'lib/cyberarm_engine/ui/elements/list_box.rb', line 75

def recalculate
  super

  @menu.recalculate
end

#released_left_mouse_button(_sender, _x, _y) ⇒ Object



43
44
45
46
47
# File 'lib/cyberarm_engine/ui/elements/list_box.rb', line 43

def released_left_mouse_button(_sender, _x, _y)
  show_menu

  :handled
end

#show_menuObject



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/cyberarm_engine/ui/elements/list_box.rb', line 49

def show_menu
  @menu.clear

  @items.each do |item|
    btn = Button.new(
      item,
      {
        parent: @menu,
        width: 1.0,
        theme: @options[:theme],
        margin: 0,
        border_color: 0x00ffffff
      },
      proc do
        self.choose = item
        @block&.call(item)
      end
    )

    @menu.add(btn)
  end
  recalculate

  root.gui_state.show_menu(@menu)
end