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

Instance Method Summary collapse

Methods inherited from Button

#draw_image, #draw_text, #layout, #value, #value=

Methods inherited from TextBlock

#handle_text_wrapping, #layout, #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?, #layout, #leave, #left_mouse_button, #max_scroll_height, #max_scroll_width, #noncontent_height, #noncontent_width, #outer_height, #outer_width, #parent_of?, #recalculate, #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
# 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)

  @menu = Menu.new(parent: self, theme: @options[:theme])

  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

Instance Method Details

#clicked_left_mouse_button(_sender, _x, _y) ⇒ Object



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

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

  :handled
end

#released_left_mouse_button(_sender, _x, _y) ⇒ Object



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

def released_left_mouse_button(_sender, _x, _y)
  show_menu

  :handled
end

#renderObject



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

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



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/cyberarm_engine/ui/elements/list_box.rb', line 56

def show_menu
  @menu.clear do

    @menu.style.width = width

    @items.each do |item|
      # prevent already selected item from appearing in list
      # NOTE: Remove this? Might be kinda confusing...
      next if item == self.value

      root.gui_state.menu_item(item, width: 1.0, margin: 0, border_color: 0x00ffffff) do
        self.choose = item
        @block&.call(self.value)
      end
    end
  end

  recalculate

  @menu.show
end