Class: Reight::Button

Inherits:
Object
  • Object
show all
Includes:
Activatable, HasHelp, Hookable
Defined in:
lib/reight/button.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from HasHelp

#help, #set_help

Methods included from Hookable

#hook

Methods included from Activatable

#activated, #activated!, #active=, #active?

Constructor Details

#initialize(name: nil, icon: nil, label: nil, &clicked) ⇒ Button

Returns a new instance of Button.



10
11
12
13
14
15
16
17
18
# File 'lib/reight/button.rb', line 10

def initialize(name: nil, icon: nil, label: nil, &clicked)
  raise if icon && label
  @name, @icon, @label = name, icon, label
  super()

  hook :clicked
  self.clicked(&clicked) if clicked
  self.clicked {r8.flash name}
end

Instance Attribute Details

#iconObject

Returns the value of attribute icon.



20
21
22
# File 'lib/reight/button.rb', line 20

def icon
  @icon
end

#labelObject

Returns the value of attribute label.



20
21
22
# File 'lib/reight/button.rb', line 20

def label
  @label
end

#nameObject

Returns the value of attribute name.



20
21
22
# File 'lib/reight/button.rb', line 20

def name
  @name
end

Instance Method Details

#clickObject



68
# File 'lib/reight/button.rb', line 68

def click() = clicked! self

#disabled?Boolean

Returns:

  • (Boolean)


75
# File 'lib/reight/button.rb', line 75

def disabled? = !enabled?

#disabled_iconObject



87
88
89
90
91
92
93
94
# File 'lib/reight/button.rb', line 87

def disabled_icon()
  @disabled_icon ||= createGraphics(@icon.width, @icon.height).tap do |g|
    g.beginDraw {g.image @icon, 0, 0}
    g.load_pixels
    g.pixels.map! {|c| alpha(c) > 0 ? color(180) : c}
    g.update_pixels
  end
end

#drawObject



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/reight/button.rb', line 22

def draw()
  sp = sprite
  no_stroke

  if @label
    fill 210
    rect 0, pressing? ? 1 : 0, sp.w, sp.h, 2
  end

  if active?
    fill 230
    rect 0, pressing? ? 1 : 0, sp.w, sp.h, 2
  end

  if @icon
    x  = (sp.w - @icon.width)  / 2
    y  = (sp.h - @icon.height) / 2
    y += 1 if pressing?
    image enabled? ? @icon : disabled_icon, x, y
  end

  if @label
    y = pressing? ? 1 : 0
    text_size r8.project.font_size - 1
    text_align CENTER, CENTER
    fill active? ? 250 : 230
    text @label, 0, y + 1, sp.w, sp.h
    fill active? ? 70  : 50
    text @label, 0, y,     sp.w, sp.h
  end
end

#enabled?(&block) ⇒ Boolean

Returns:

  • (Boolean)


70
71
72
73
# File 'lib/reight/button.rb', line 70

def enabled?(&block)
  @enabled_block = block if block
  @enabled_block ? @enabled_block.call : true
end

#hover(x, y) ⇒ Object



64
65
66
# File 'lib/reight/button.rb', line 64

def hover(x, y)
  r8.flash help, priority: 0.5
end

#pressed(x, y) ⇒ Object



54
55
56
# File 'lib/reight/button.rb', line 54

def pressed(x, y)
  @pressing = true if enabled?
end

#pressing?Boolean

Returns:

  • (Boolean)


62
# File 'lib/reight/button.rb', line 62

def pressing? = @pressing

#released(x, y) ⇒ Object



58
59
60
# File 'lib/reight/button.rb', line 58

def released(x, y)
  @pressing = false
end

#spriteObject



77
78
79
80
81
82
83
84
85
# File 'lib/reight/button.rb', line 77

def sprite()
  @sprite ||= RubySketch::Sprite.new(physics: false).tap do |sp|
    sp.draw           {draw}
    sp.mouse_pressed  {pressed  sp.mouse_x, sp.mouse_y}
    sp.mouse_released {released sp.mouse_x, sp.mouse_y}
    sp.mouse_moved    {hover    sp.mouse_x, sp.mouse_y}
    sp.mouse_clicked  {clicked! self if enabled?}
  end
end