Class: BTetrisKp::TextField
- Inherits:
-
Gosu::TextInput
- Object
- Gosu::TextInput
- BTetrisKp::TextField
- Defined in:
- lib/btetris_kp/gui/textfield.rb
Overview
class representing input box for IP adress / port / text
Instance Attribute Summary collapse
-
#x ⇒ Object
readonly
Returns the value of attribute x.
-
#y ⇒ Object
readonly
Returns the value of attribute y.
Instance Method Summary collapse
-
#draw ⇒ Object
draws textfield on window (gosu).
-
#filter(text) ⇒ Object
filters text of characters specified in regexp.
-
#initialize(window, font, x, y, text, filter, width, maxlen) ⇒ TextField
constructor
A new instance of TextField.
-
#mouse_over?(mouse_x, mouse_y) ⇒ Boolean
returns true if mouse is over textfield.
-
#move_caret(mouse_x) ⇒ Object
moves the caret to the position specified by mouse.
-
#update ⇒ Object
updates textfield, limits length of text to @maxlen.
Constructor Details
#initialize(window, font, x, y, text, filter, width, maxlen) ⇒ TextField
Returns a new instance of TextField.
9 10 11 12 13 14 15 16 17 18 19 |
# File 'lib/btetris_kp/gui/textfield.rb', line 9 def initialize(window, font, x, y, text, filter, width, maxlen) super() @window = window @font = font @x = x @y = y @width = width @filter = filter @maxlen = maxlen self.text = text end |
Instance Attribute Details
#x ⇒ Object (readonly)
Returns the value of attribute x.
7 8 9 |
# File 'lib/btetris_kp/gui/textfield.rb', line 7 def x @x end |
#y ⇒ Object (readonly)
Returns the value of attribute y.
7 8 9 |
# File 'lib/btetris_kp/gui/textfield.rb', line 7 def y @y end |
Instance Method Details
#draw ⇒ Object
draws textfield on window (gosu)
32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/btetris_kp/gui/textfield.rb', line 32 def draw @window.draw_line(@x - Const::BORDER_GAP, @y + @font.height, Const::CARET_CLR, @x + @width + 2 * Const::BORDER_GAP, @y + @font.height, Const::CARET_CLR) unless text.nil? # draws the caret if textfield is selected pos_x = @x + @font.text_width(text[0...caret_pos]) if @window.text_input == self @window.draw_line(pos_x, @y, Const::CARET_CLR, pos_x, @y + @font.height, Const::CARET_CLR, 0) end @font.draw(text, @x, @y, 0) end end |
#filter(text) ⇒ Object
filters text of characters specified in regexp
22 23 24 |
# File 'lib/btetris_kp/gui/textfield.rb', line 22 def filter(text) text.upcase.gsub(@filter, '') end |
#mouse_over?(mouse_x, mouse_y) ⇒ Boolean
returns true if mouse is over textfield
48 49 50 51 52 53 |
# File 'lib/btetris_kp/gui/textfield.rb', line 48 def mouse_over?(mouse_x, mouse_y) mouse_x >= x - Const::BORDER_GAP && mouse_x <= x + @width + Const::BORDER_GAP && mouse_y >= y - Const::BORDER_GAP && mouse_y <= y + @font.height + Const::BORDER_GAP end |
#move_caret(mouse_x) ⇒ Object
moves the caret to the position specified by mouse
56 57 58 59 60 61 62 63 64 |
# File 'lib/btetris_kp/gui/textfield.rb', line 56 def move_caret(mouse_x) 1.upto(text.length) do |i| if mouse_x < x + @font.text_width(text[0...i]) self.caret_pos = self.selection_start = i - 1 return end end self.caret_pos = self.selection_start = text.length end |
#update ⇒ Object
updates textfield, limits length of text to @maxlen
27 28 29 |
# File 'lib/btetris_kp/gui/textfield.rb', line 27 def update self.text = text.slice(0...@maxlen) if text.size > @maxlen end |