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, #element_visible, #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, #value, #value=

Methods inherited from TextBlock

#handle_text_wrapping, #line_width, #update, #value, #value=

Methods inherited from CyberarmEngine::Element

#background=, #background_image=, #background_nine_slice=, #blur, #button_down, #button_up, #child_of?, #content_height, #content_width, #debug_draw, #default_events, #dimensional_size, #draggable?, #draw, #element_visible?, #enabled=, #enabled?, #enter, #focus, #focused?, #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, #parent_of?, #recalculate_if_size_changed, #reposition, #root, #safe_style_fetch, #scroll_height, #scroll_width, #set_background, #set_background_image, #set_background_nine_slice, #set_border_color, #set_border_thickness, #set_color, #set_font, #set_margin, #set_padding, #set_static_position, #show, #space_available_height, #space_available_width, #stylize, #to_s, #toggle, #update, #update_background, #update_background_image, #update_background_nine_slice, #update_styles, #value, #value=, #visible?, #width

Methods included from Common

#alt_down?, #control_down?, #current_state, #darken, #draw_rect, #fill, #find_element_by_tag, #get_asset, #get_image, #get_sample, #get_song, #lighten, #opacity, #pop_state, #previous_state, #push_state, #shift_down?, #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
31
32
# 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: self, theme: @options[:theme])
  @menu.define_singleton_method(:recalculate_menu) do
    @x = @__list_box.x
    @y = @__list_box.y + @__list_box.height

    @y = @__list_box.y - height if @y + height > window.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



25
26
27
28
29
# File 'lib/cyberarm_engine/ui/elements/list_box.rb', line 25

def @menu.recalculate
  super

  recalculate_menu
end

Instance Method Details

#clicked_left_mouse_button(_sender, _x, _y) ⇒ Object



58
59
60
61
62
# File 'lib/cyberarm_engine/ui/elements/list_box.rb', line 58

def clicked_left_mouse_button(_sender, _x, _y)
  # @block&.call(self.value) if @enabled

  :handled
end

#recalculateObject



94
95
96
97
98
# File 'lib/cyberarm_engine/ui/elements/list_box.rb', line 94

def recalculate
  super

  @menu.recalculate
end

#released_left_mouse_button(_sender, _x, _y) ⇒ Object



52
53
54
55
56
# File 'lib/cyberarm_engine/ui/elements/list_box.rb', line 52

def released_left_mouse_button(_sender, _x, _y)
  show_menu

  :handled
end

#renderObject



34
35
36
37
38
39
# File 'lib/cyberarm_engine/ui/elements/list_box.rb', line 34

def render
  super

  w = @text.textobject.text_width("")
  @text.textobject.draw_text("", @x + content_width - w, @y + @style.padding_top, @z, 1, 1, @text.color)
end

#show_menuObject



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/cyberarm_engine/ui/elements/list_box.rb', line 64

def show_menu
  @menu.clear

  @menu.style.width = width

  @items.each do |item|
    next if item == self.value

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

    @menu.add(btn)
  end
  recalculate

  root.gui_state.show_menu(@menu)
end