Class: Reflex::Button

Inherits:
View
  • Object
show all
Defined in:
lib/reflex/button.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from View

#capturing?, #categories, #category, #category=, #children, #collision, #collision=, #delay, #find_child, has_model, #interval, #on_contact, #on_contact_begin, #on_contact_end, #remove_self, #shapes, #style, #styles, #timeout

Methods included from HasTags

#clear_tags, #tag, #tag=, #tags, #untag

Methods included from HasFrame

#inset_by, #move_by, #move_to, #resize_by, #resize_to

Methods included from Hookable

#hook

Constructor Details

#initialize(*args, &block) ⇒ Button

Returns a new instance of Button.



16
17
18
19
20
# File 'lib/reflex/button.rb', line 16

def initialize (*args, &block)
  self.data = false
  super
  self.text = self.name unless self.text
end

Instance Attribute Details

#textObject

Returns the value of attribute text.



14
15
16
# File 'lib/reflex/button.rb', line 14

def text
  @text
end

Instance Method Details

#content_boundsObject



22
23
24
25
# File 'lib/reflex/button.rb', line 22

def content_bounds ()
  f = window.painter.font
  return f.width(@text) + 2, f.height + 2
end

#on_data_update(e) ⇒ Object



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

def on_data_update (e)
  on_press({}) if e.data
end

#on_draw(e) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/reflex/button.rb', line 30

def on_draw (e)
  e.painter.push fill: (pressing? ? :white : :none), stroke: :white do |p|
    p.rect e.bounds

    if @text
      p.fill pressing? ? :black : :white
      x = (e.bounds.w - p.font.w(@text)) / 2
      y = (e.bounds.h - p.font.h) / 2
      p.text @text, x, y
    end

    p.fill :none
    p.rect e.bounds
  end
end

#on_pointer(e) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/reflex/button.rb', line 46

def on_pointer (e)
  case e.type
  when :down
    self.capture += [:pointer]
    redraw
  when :up
    if pressing?
      self.capture -= [:pointer]
      if frame.move_to(0, 0).include? e.position
        self.data = true
        self.data = false
      end
      redraw
    end
  end
end

#on_press(e) ⇒ Object



27
28
# File 'lib/reflex/button.rb', line 27

def on_press (e)
end