Class: Reight::Button
- Inherits:
-
Object
- Object
- Reight::Button
- Includes:
- Activatable, HasHelp, Hookable
- Defined in:
- lib/reight/button.rb
Direct Known Subclasses
MapEditor::Tool, SoundEditor::Tool, SpriteEditor::Color, SpriteEditor::Tool
Instance Attribute Summary collapse
-
#icon ⇒ Object
Returns the value of attribute icon.
-
#label ⇒ Object
Returns the value of attribute label.
-
#name ⇒ Object
Returns the value of attribute name.
Instance Method Summary collapse
- #click ⇒ Object
- #disabled? ⇒ Boolean
- #disabled_icon ⇒ Object
- #draw ⇒ Object
- #enabled?(&block) ⇒ Boolean
- #hover(x, y) ⇒ Object
-
#initialize(name: nil, icon: nil, label: nil, &clicked) ⇒ Button
constructor
A new instance of Button.
- #pressed(x, y) ⇒ Object
- #pressing? ⇒ Boolean
- #released(x, y) ⇒ Object
- #sprite ⇒ Object
Methods included from HasHelp
Methods included from Hookable
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
#icon ⇒ Object
Returns the value of attribute icon.
20 21 22 |
# File 'lib/reight/button.rb', line 20 def icon @icon end |
#label ⇒ Object
Returns the value of attribute label.
20 21 22 |
# File 'lib/reight/button.rb', line 20 def label @label end |
#name ⇒ Object
Returns the value of attribute name.
20 21 22 |
# File 'lib/reight/button.rb', line 20 def name @name end |
Instance Method Details
#click ⇒ Object
67 |
# File 'lib/reight/button.rb', line 67 def click() = clicked! self |
#disabled? ⇒ Boolean
74 |
# File 'lib/reight/button.rb', line 74 def disabled? = !enabled? |
#disabled_icon ⇒ Object
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 |
#draw ⇒ Object
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
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
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 |
#sprite ⇒ Object
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 |