Class: TextField
- Inherits:
-
Gosu::TextInput
- Object
- Gosu::TextInput
- TextField
- Defined in:
- lib/external/gosu/text.rb
Overview
Input text boxes
Constant Summary collapse
- FONT =
Gosu::Font.new(60)
- WIDTH =
800- LENGTH_LIMIT =
20- PADDING =
5- INACTIVE_COLOR =
0xcc_666666- ACTIVE_COLOR =
0xcc_555555- SELECTION_COLOR =
0xcc_444444- CARET_COLOR =
0xff_ffffff
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(z) ⇒ Object
-
#filter(new_text) ⇒ Object
In this example, we use the filter method to prevent the user from entering a text that exceeds the length limit.
- #height ⇒ Object
-
#initialize(window, x, y) ⇒ TextField
constructor
A new instance of TextField.
-
#under_mouse? ⇒ Boolean
Hit-test for selecting a text field with the mouse.
Constructor Details
#initialize(window, x, y) ⇒ TextField
Returns a new instance of TextField.
21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/external/gosu/text.rb', line 21 def initialize(window, x, y) # It's important to call the inherited constructor. super() @window = window @x = x @y = y # Start with a self-explanatory text in each field. self.text = 'Click to edit' end |
Instance Attribute Details
#x ⇒ Object (readonly)
Returns the value of attribute x.
19 20 21 |
# File 'lib/external/gosu/text.rb', line 19 def x @x end |
#y ⇒ Object (readonly)
Returns the value of attribute y.
19 20 21 |
# File 'lib/external/gosu/text.rb', line 19 def y @y end |
Instance Method Details
#draw(z) ⇒ Object
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/external/gosu/text.rb', line 40 def draw(z) # Change the background colour if this is the currently selected text field. color = if @window.text_input == self ACTIVE_COLOR else INACTIVE_COLOR end # ChillerDragon's epic shadow to at least have edited the stolen sample a lil bit Gosu.draw_rect (x - PADDING) + 5, (y - PADDING) + 5, WIDTH + 2 * PADDING, height + 2 * PADDING, INACTIVE_COLOR, z Gosu.draw_rect x - PADDING, y - PADDING, WIDTH + 2 * PADDING, height + 2 * PADDING, color, z Gosu.draw_rect x - PADDING, y - PADDING, WIDTH + 2 * PADDING, height + 2 * PADDING, color, z # Calculate the position of the caret and the selection start. pos_x = x + FONT.text_width(text[0...caret_pos]) sel_x = x + FONT.text_width(text[0...selection_start]) sel_w = pos_x - sel_x # Draw the selection background, if any. (If not, sel_x and pos_x will be # the same value, making this a no-op call.) Gosu.draw_rect sel_x, y, sel_w, height, SELECTION_COLOR, z # Draw the caret if this is the currently selected field. Gosu.draw_line pos_x, y, CARET_COLOR, pos_x, y + height, CARET_COLOR, z if @window.text_input == self # Finally, draw the text itself! FONT.draw_text text, x, y, z end |
#filter(new_text) ⇒ Object
In this example, we use the filter method to prevent the user from entering a text that exceeds the length limit. However, you can also use this to blacklist certain characters, etc.
35 36 37 38 |
# File 'lib/external/gosu/text.rb', line 35 def filter(new_text) allowed_length = [LENGTH_LIMIT - text.length, 0].max new_text[0, allowed_length] end |
#height ⇒ Object
68 69 70 |
# File 'lib/external/gosu/text.rb', line 68 def height FONT.height end |
#under_mouse? ⇒ Boolean
Hit-test for selecting a text field with the mouse.
73 74 75 76 |
# File 'lib/external/gosu/text.rb', line 73 def under_mouse? @window.mouse_x > x - PADDING and @window.mouse_x < x + WIDTH + PADDING and @window.mouse_y > y - PADDING and @window.mouse_y < y + height + PADDING end |