Class: Reight::Text
- Inherits:
-
Object
- Object
- Reight::Text
- Includes:
- Activatable, HasHelp, Hookable
- Defined in:
- lib/reight/text.rb
Instance Attribute Summary collapse
-
#align ⇒ Object
Returns the value of attribute align.
-
#editable ⇒ Object
(also: #editable?)
Returns the value of attribute editable.
-
#label ⇒ Object
Returns the value of attribute label.
-
#value ⇒ Object
Returns the value of attribute value.
Instance Method Summary collapse
- #clicked(x, y) ⇒ Object
- #draw ⇒ Object
- #focus=(focus) ⇒ Object
- #focus? ⇒ Boolean
- #hit?(x, y) ⇒ Boolean
-
#initialize(text = '', editable: false, align: LEFT, label: nil, regexp: nil, &changed) ⇒ Text
constructor
A new instance of Text.
- #key_pressed(key, code) ⇒ Object
- #revert ⇒ Object
- #sprite ⇒ Object
- #valid?(value = self.value, ignore_regexp: focus?) ) ⇒ Boolean
Methods included from HasHelp
Methods included from Hookable
Methods included from Activatable
#activated, #activated!, #active=, #active?
Constructor Details
#initialize(text = '', editable: false, align: LEFT, label: nil, regexp: nil, &changed) ⇒ Text
Returns a new instance of Text.
10 11 12 13 14 15 16 17 18 19 |
# File 'lib/reight/text.rb', line 10 def initialize(text = '', editable: false, align: LEFT, label: nil, regexp: nil, &changed) hook :changed super() @editable, @align, @label, @regexp = editable, align, label, regexp @shake = 0 self.changed(&changed) if changed self.value = text end |
Instance Attribute Details
#align ⇒ Object
Returns the value of attribute align.
21 22 23 |
# File 'lib/reight/text.rb', line 21 def align @align end |
#editable ⇒ Object Also known as: editable?
Returns the value of attribute editable.
21 22 23 |
# File 'lib/reight/text.rb', line 21 def editable @editable end |
#label ⇒ Object
Returns the value of attribute label.
21 22 23 |
# File 'lib/reight/text.rb', line 21 def label @label end |
#value ⇒ Object
Returns the value of attribute value.
23 24 25 |
# File 'lib/reight/text.rb', line 23 def value @value end |
Instance Method Details
#clicked(x, y) ⇒ Object
100 101 102 103 104 105 106 107 108 109 |
# File 'lib/reight/text.rb', line 100 def clicked(x, y) if focus? return if hit? x, y self.value = @old_value if value == '' && !valid?(ignore_regexp: false) self.focus = false elsif editable? self.focus = true @old_value, @value = @value.dup, '' end end |
#draw ⇒ Object
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 |
# File 'lib/reight/text.rb', line 62 def draw() sp = sprite no_stroke if @shake != 0 translate rand(-@shake.to_f..@shake.to_f), 0 @shake *= rand(0.7..0.9) @shake = 0 if @shake.abs < 0.1 end fill focus? ? 230 : 200 rect 0, 0, sp.w, sp.h, 3 show_old = value == '' text = show_old ? @old_value : value text = label.to_s + text unless focus? x = 2 fill show_old ? 200 : 50 text_align @align, CENTER text text, x, 0, sp.w - x * 2, sp.h if focus? && (frame_count % 60) < 30 fill 100 bounds = text_font.text_bounds value xx = (@align == LEFT ? x + bounds.w : (sp.w + bounds.w) / 2) - 1 rect xx, (sp.h - bounds.h) / 2, 2, bounds.h end end |
#focus=(focus) ⇒ Object
32 33 34 35 36 37 38 39 40 |
# File 'lib/reight/text.rb', line 32 def focus=(focus) return if focus && !editable? return if focus == focus? sprite.capture = focus unless focus revert unless valid? value changed! value, self if value != @old_value end end |
#focus? ⇒ Boolean
42 43 44 |
# File 'lib/reight/text.rb', line 42 def focus?() sprite.capturing? end |
#hit?(x, y) ⇒ Boolean
111 112 113 114 |
# File 'lib/reight/text.rb', line 111 def hit?(x, y) sp = sprite (0...sp.w).include?(x) && (0...sp.h).include?(y) end |
#key_pressed(key, code) ⇒ Object
91 92 93 94 95 96 97 98 |
# File 'lib/reight/text.rb', line 91 def key_pressed(key, code) case code when ESC then self.value = @old_value; self.focus = false when ENTER then self.focus = false when DELETE, BACKSPACE then self.value = value.split('').tap {_1.pop}.join else self.value += key if key && valid?(key) end end |
#revert ⇒ Object
27 28 29 30 |
# File 'lib/reight/text.rb', line 27 def revert() self.value = @old_value @shake = 6 end |
#sprite ⇒ Object
116 117 118 119 120 121 122 |
# File 'lib/reight/text.rb', line 116 def sprite() @sprite ||= RubySketch::Sprite.new(physics: false).tap do |sp| sp.draw {draw} sp.key_pressed {key_pressed sp.key, sp.key_code} sp.mouse_clicked {clicked sp.mouse_x, sp.mouse_y} end end |
#valid?(value = self.value, ignore_regexp: focus?) ) ⇒ Boolean
53 54 55 56 57 58 59 60 |
# File 'lib/reight/text.rb', line 53 def valid?(value = self.value, ignore_regexp: focus?) case when !value then false when ignore_regexp then true when !@regexp then true else value =~ @regexp end end |