Class: CyberarmEngine::Element::Button

Inherits:
TextBlock show all
Defined in:
lib/cyberarm_engine/ui/elements/button.rb

Direct Known Subclasses

EditLine, ListBox, MenuItem, Slider::Handle, ToggleButton

Constant Summary

Constants included from Theme

Theme::THEME

Instance Attribute Summary

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 TextBlock

#handle_text_wrapping, #line_width, #update

Methods inherited from CyberarmEngine::Element

#background=, #background_image=, #background_nine_slice=, #blur, #button_down, #button_up, #child_of?, #clicked_left_mouse_button, #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, #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(text_or_image, options = {}, block = nil) ⇒ Button

Returns a new instance of Button.



4
5
6
7
8
9
10
11
12
13
14
# File 'lib/cyberarm_engine/ui/elements/button.rb', line 4

def initialize(text_or_image, options = {}, block = nil)
  @image = nil
  @scale_x = 1
  @scale_y = 1

  @image = text_or_image if text_or_image.is_a?(Gosu::Image)

  super(text_or_image, options, block)

  @style.background_canvas.background = @style.background
end

Instance Method Details

#draw_imageObject



24
25
26
27
28
29
30
31
# File 'lib/cyberarm_engine/ui/elements/button.rb', line 24

def draw_image
  @image.draw(
    @style.border_thickness_left + @style.padding_left + @x,
    @style.border_thickness_top + @style.padding_top + @y,
    @z + 2,
    @scale_x, @scale_y, @text.color
  )
end

#draw_textObject



33
34
35
# File 'lib/cyberarm_engine/ui/elements/button.rb', line 33

def draw_text
  @text.draw
end

#layoutObject



37
38
39
40
41
42
43
44
45
46
47
48
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
74
# File 'lib/cyberarm_engine/ui/elements/button.rb', line 37

def layout
  unless @enabled
    @style.background_canvas.background = @style.disabled[:background]
    @text.color = @style.disabled[:color]
  else
    @style.background_canvas.background = @style.background
    @text.color = @style.color
  end

  if @image
    @width = 0
    @height = 0

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

    if _width && _height
      @scale_x = _width.to_f / @image.width
      @scale_y = _height.to_f / @image.height
    elsif _width
      @scale_x = _width.to_f / @image.width
      @scale_y = @scale_x
    elsif _height
      @scale_y = _height.to_f / @image.height
      @scale_x = @scale_y
    else
      @scale_x = 1
      @scale_y = 1
    end

    @width = _width || @image.width.floor * @scale_x
    @height = _height || @image.height.floor * @scale_y

    update_background
  else
    super
  end
end

#renderObject



16
17
18
19
20
21
22
# File 'lib/cyberarm_engine/ui/elements/button.rb', line 16

def render
  if @image
    draw_image
  else
    draw_text
  end
end

#valueObject



76
77
78
# File 'lib/cyberarm_engine/ui/elements/button.rb', line 76

def value
  @image || super
end

#value=(value) ⇒ Object



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/cyberarm_engine/ui/elements/button.rb', line 80

def value=(value)
  if value.is_a?(Gosu::Image)
    @image = value
  else
    super
  end

  old_width = width
  old_height = height

  if old_width != width || old_height != height
    root.gui_state.request_recalculate
  else
    recalculate
  end

  publish(:changed, self.value)
end