Class: CyberarmEngine::Element::Button

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

Direct Known Subclasses

EditLine, ListBox, Slider::Handle, ToggleButton

Constant Summary

Constants included from Theme

Theme::THEME

Instance Attribute Summary

Attributes inherited from CyberarmEngine::Element

#background_canvas, #border_canvas, #enabled, #event_handler, #options, #parent, #style, #tip, #x, #y, #z

Instance Method Summary collapse

Methods inherited from Label

#handle_text_wrapping, #line_width

Methods inherited from CyberarmEngine::Element

#background=, #button_down, #button_up, #content_height, #content_width, #default_events, #dimensional_size, #draggable?, #draw, #enabled?, #height, #hide, #hit?, #inner_height, #inner_width, #is_root?, #noncontent_height, #noncontent_width, #outer_height, #outer_width, #reposition, #root, #set_background, #set_border_color, #set_border_thickness, #set_margin, #set_padding, #set_static_position, #show, #stylize, #to_s, #toggle, #update, #update_background, #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, #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 = default(:background)
end

Instance Method Details

#blur(_sender) ⇒ Object



79
80
81
82
83
# File 'lib/cyberarm_engine/ui/elements/button.rb', line 79

def blur(_sender)
  @focus = false

  :handled
end

#clicked_left_mouse_button(_sender, _x, _y) ⇒ Object



66
67
68
69
70
# File 'lib/cyberarm_engine/ui/elements/button.rb', line 66

def clicked_left_mouse_button(_sender, _x, _y)
  @block.call(self) if @block

  :handled
end

#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

#enter(_sender) ⇒ Object



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

def enter(_sender)
  @focus = false unless window.button_down?(Gosu::MsLeft)

  if @focus
    @style.background_canvas.background = default(:active, :background)
    @text.color = default(:active, :color)
  else
    @style.background_canvas.background = default(:hover, :background)
    @text.color = default(:hover, :color)
  end

  :handled
end

#leave(_sender) ⇒ Object



72
73
74
75
76
77
# File 'lib/cyberarm_engine/ui/elements/button.rb', line 72

def leave(_sender)
  @style.background_canvas.background = default(:background)
  @text.color = default(:color)

  :handled
end

#left_mouse_button(_sender, _x, _y) ⇒ Object



51
52
53
54
55
56
57
58
# File 'lib/cyberarm_engine/ui/elements/button.rb', line 51

def left_mouse_button(_sender, _x, _y)
  @focus = true
  @style.background_canvas.background = default(:active, :background)
  window.current_state.focus = self
  @text.color = default(:active, :color)

  :handled
end

#recalculateObject



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/cyberarm_engine/ui/elements/button.rb', line 85

def recalculate
  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.round * @scale_x
    @height = _height || @image.height.round * @scale_y

    update_background
  else
    super
  end
end

#released_left_mouse_button(sender, _x, _y) ⇒ Object



60
61
62
63
64
# File 'lib/cyberarm_engine/ui/elements/button.rb', line 60

def released_left_mouse_button(sender, _x, _y)
  enter(sender)

  :handled
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



116
117
118
# File 'lib/cyberarm_engine/ui/elements/button.rb', line 116

def value
  @image || super
end

#value=(value) ⇒ Object



120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/cyberarm_engine/ui/elements/button.rb', line 120

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

  old_width = width
  old_height = height
  recalculate

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

  publish(:changed, self.value)
end