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



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

def click() = clicked! self

#disabled?Boolean

Returns:

  • (Boolean)


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

def disabled? = !enabled?

#disabled_iconObject



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

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
# 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_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)


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

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

#hover(x, y) ⇒ Object



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

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

#pressed(x, y) ⇒ Object



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

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

#pressing?Boolean

Returns:

  • (Boolean)


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

def pressing? = @pressing

#released(x, y) ⇒ Object



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

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

#spriteObject



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

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