Class: Hokusai::Blocks::Button

Inherits:
Hokusai::Block show all
Defined in:
ui/src/hokusai/blocks/button.rb

Constant Summary collapse

DEFAULT_BACKGROUND =
[39, 95, 206]
DEFAULT_CLICKED_BACKGROUND =
[24, 52, 109]
DEFAULT_HOVERED_BACKGROUND =
[242, 52, 109]

Instance Attribute Summary collapse

Attributes inherited from Hokusai::Block

#node, #provides, #publisher

Instance Method Summary collapse

Methods inherited from Hokusai::Block

#children, #children?, compile, computed, computed!, #draw, #draw_with, #dump, #emit, inject, inject!, #method_missing, mount, #on_resize, provide, provides, style, styles_get, template, template_from_file, template_get, #update, use, uses

Constructor Details

#initialize(**args) ⇒ Button

Returns a new instance of Button.



93
94
95
96
97
98
99
# File 'ui/src/hokusai/blocks/button.rb', line 93

def initialize(**args)
  @button_width = 0.0
  @hovered = false
  @clicked = false

  super(**args)
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Hokusai::Block

Instance Attribute Details

#button_widthObject

Returns the value of attribute button_width.



43
44
45
# File 'ui/src/hokusai/blocks/button.rb', line 43

def button_width
  @button_width
end

Instance Method Details

#after_updatedObject



78
79
80
# File 'ui/src/hokusai/blocks/button.rb', line 78

def after_updated
  node.meta.props[:height] = button_height
end

#background_colorObject



82
83
84
# File 'ui/src/hokusai/blocks/button.rb', line 82

def background_color
  @hovered ? (@clicked ? clicked_background : hovered_background) : background
end

#button_heightObject



74
75
76
# File 'ui/src/hokusai/blocks/button.rb', line 74

def button_height
  size + padding.top + padding.bottom
end

#emit_click(event) ⇒ Object



45
46
47
48
49
50
51
# File 'ui/src/hokusai/blocks/button.rb', line 45

def emit_click(event)
  @clicked = true

  event.stop

  emit("clicked", event)
end

#render(canvas) {|canvas| ... } ⇒ Object

Yields:

  • (canvas)


86
87
88
89
90
91
# File 'ui/src/hokusai/blocks/button.rb', line 86

def render(canvas)
  canvas.height = button_height
  canvas.width = button_width

  yield canvas
end

#set_hovered(event) ⇒ Object



57
58
59
60
61
62
# File 'ui/src/hokusai/blocks/button.rb', line 57

def set_hovered(event)
  @hovered = true
  @clicked = event.left.down

  Hokusai.set_mouse_cursor(:pointer)
end

#unset_hovered(_) ⇒ Object



64
65
66
67
68
69
70
71
72
# File 'ui/src/hokusai/blocks/button.rb', line 64

def unset_hovered(_)
  @clicked = false

  if @hovered
    Hokusai.set_mouse_cursor(:default)
  end

  @hovered = false
end

#update_width(value) ⇒ Object



53
54
55
# File 'ui/src/hokusai/blocks/button.rb', line 53

def update_width(value)
  self.button_width = value + (outline.right)
end