Class: CyberarmEngine::Element::ToggleButton

Inherits:
Button show all
Defined in:
lib/cyberarm_engine/ui/elements/toggle_button.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

Methods inherited from TextBlock

#handle_text_wrapping, #layout, #line_width, #update

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_if_size_changed, #released_left_mouse_button, #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, #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) ⇒ ToggleButton

Returns a new instance of ToggleButton.



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/cyberarm_engine/ui/elements/toggle_button.rb', line 6

def initialize(options, block = nil)
  if options.dig(:theme, :ToggleButton, :checkmark_image)
    options[:theme][:ToggleButton][:image_width] ||= options[:theme][:TextBlock][:text_size]
    super(get_image(options.dig(:theme, :ToggleButton, :checkmark_image)), options, block)
  else
    super(options[:checkmark], options, block)
  end

  @value = options[:checked] || false

  if @value
    @raw_text = @options[:checkmark]
  else
    @raw_text = ""
  end
end

Instance Attribute Details

#toggledObject (readonly)

Returns the value of attribute toggled.



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

def toggled
  @toggled
end

#valueObject

Returns the value of attribute value.



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

def value
  @value
end

Instance Method Details

#clicked_left_mouse_button(_sender, _x, _y) ⇒ Object



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

def clicked_left_mouse_button(_sender, _x, _y)
  self.value = !@value

  @block.call(self) if @block

  :handled
end

#recalculateObject



39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/cyberarm_engine/ui/elements/toggle_button.rb', line 39

def recalculate
  super
  return if @image

  _width  = dimensional_size(@style.width,  :width)
  _height = dimensional_size(@style.height, :height)

  @width  = _width  || @text.textobject.text_width(@options[:checkmark])
  @height = _height || @text.height

  update_background
end

#renderObject



31
32
33
34
35
36
37
# File 'lib/cyberarm_engine/ui/elements/toggle_button.rb', line 31

def render
  if @image
    draw_image if @value
  else
    draw_text
  end
end