Class: Wads::TextField
- Inherits:
-
Gosu::TextInput
- Object
- Gosu::TextInput
- Wads::TextField
- Defined in:
- lib/wads/textinput.rb
Constant Summary collapse
- INACTIVE_COLOR =
Some constants that define our appearance.
0xcc666666
- ACTIVE_COLOR =
0xcc85929e
- SELECTION_COLOR =
0xcc0000ff
- CARET_COLOR =
0xffffffff
- PADDING =
5
Instance Attribute Summary collapse
-
#base_z ⇒ Object
Returns the value of attribute base_z.
-
#old_value ⇒ Object
Returns the value of attribute old_value.
-
#x ⇒ Object
readonly
Returns the value of attribute x.
-
#y ⇒ Object
readonly
Returns the value of attribute y.
Instance Method Summary collapse
- #button_down(id, mouse_x, mouse_y) ⇒ Object
- #button_up(id, mouse_x, mouse_y) ⇒ Object
- #contains_click(mouse_x, mouse_y) ⇒ Object
- #draw ⇒ Object
- #handle_key_press(id, mouse_x, mouse_y) ⇒ Object
- #handle_mouse_down(mouse_x, mouse_y) ⇒ Object
- #handle_mouse_up(mouse_x, mouse_y) ⇒ Object
- #handle_update(update_count, mouse_x, mouse_y) ⇒ Object
- #height ⇒ Object
-
#initialize(x, y, original_text, default_width) ⇒ TextField
constructor
A new instance of TextField.
-
#move_caret(mouse_x) ⇒ Object
Tries to move the caret to the position specifies by mouse_x.
-
#under_point?(mouse_x, mouse_y) ⇒ Boolean
Hit-test for selecting a text field with the mouse.
- #update(update_count, mouse_x, mouse_y) ⇒ Object
-
#width ⇒ Object
This text field grows with the text that’s being entered.
Constructor Details
#initialize(x, y, original_text, default_width) ⇒ TextField
Returns a new instance of TextField.
17 18 19 20 21 22 23 24 25 26 27 |
# File 'lib/wads/textinput.rb', line 17 def initialize(x, y, original_text, default_width) super() @x = x @y = y @font = WadsConfig.instance.current_theme.font @default_width = default_width @base_z = 0 self.text = original_text @old_value = self.text end |
Instance Attribute Details
#base_z ⇒ Object
Returns the value of attribute base_z.
14 15 16 |
# File 'lib/wads/textinput.rb', line 14 def base_z @base_z end |
#old_value ⇒ Object
Returns the value of attribute old_value.
15 16 17 |
# File 'lib/wads/textinput.rb', line 15 def old_value @old_value end |
#x ⇒ Object (readonly)
Returns the value of attribute x.
13 14 15 |
# File 'lib/wads/textinput.rb', line 13 def x @x end |
#y ⇒ Object (readonly)
Returns the value of attribute y.
13 14 15 |
# File 'lib/wads/textinput.rb', line 13 def y @y end |
Instance Method Details
#button_down(id, mouse_x, mouse_y) ⇒ Object
100 101 102 103 104 105 106 107 108 109 110 111 |
# File 'lib/wads/textinput.rb', line 100 def (id, mouse_x, mouse_y) if id == Gosu::MsLeft move_caret(mouse_x) else if @old_value == self.text # nothing to do else @old_value = self.text return WidgetResult.new(false, EVENT_TEXT_INPUT, [self.text]) end end end |
#button_up(id, mouse_x, mouse_y) ⇒ Object
113 114 115 |
# File 'lib/wads/textinput.rb', line 113 def (id, mouse_x, mouse_y) # empty base implementation end |
#contains_click(mouse_x, mouse_y) ⇒ Object
83 84 85 |
# File 'lib/wads/textinput.rb', line 83 def contains_click(mouse_x, mouse_y) under_point?(mouse_x, mouse_y) end |
#draw ⇒ Object
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/wads/textinput.rb', line 29 def draw # Depending on whether this is the currently selected input or not, change the # background's color. if WadsConfig.instance.get_window.text_input == self then background_color = ACTIVE_COLOR else background_color = INACTIVE_COLOR end WadsConfig.instance.get_window.draw_quad(x - PADDING, y - PADDING, background_color, x + width + PADDING, y - PADDING, background_color, x - PADDING, y + height + PADDING, background_color, x + width + PADDING, y + height + PADDING, background_color, @base_z + 9) # Calculate the position of the caret and the selection start. pos_x = x + @font.text_width(self.text[0...self.caret_pos]) sel_x = x + @font.text_width(self.text[0...self.selection_start]) # Draw the selection background, if any; if not, sel_x and pos_x will be # the same value, making this quad empty. WadsConfig.instance.get_window.draw_quad(sel_x, y, SELECTION_COLOR, pos_x, y, SELECTION_COLOR, sel_x, y + height, SELECTION_COLOR, pos_x, y + height, SELECTION_COLOR, @base_z + 10) # Draw the caret; again, only if this is the currently selected field. if WadsConfig.instance.get_window.text_input == self then WadsConfig.instance.get_window.draw_line(pos_x, y, CARET_COLOR, pos_x, y + height, CARET_COLOR, @base_z + 10) end # Finally, draw the text itself! @font.draw_text(self.text, x, y, @base_z + 10) end |
#handle_key_press(id, mouse_x, mouse_y) ⇒ Object
129 130 131 |
# File 'lib/wads/textinput.rb', line 129 def handle_key_press id, mouse_x, mouse_y # empty base implementation end |
#handle_mouse_down(mouse_x, mouse_y) ⇒ Object
121 122 123 |
# File 'lib/wads/textinput.rb', line 121 def handle_mouse_down mouse_x, mouse_y # empty base implementation end |
#handle_mouse_up(mouse_x, mouse_y) ⇒ Object
125 126 127 |
# File 'lib/wads/textinput.rb', line 125 def handle_mouse_up mouse_x, mouse_y # empty base implementation end |
#handle_update(update_count, mouse_x, mouse_y) ⇒ Object
133 134 135 |
# File 'lib/wads/textinput.rb', line 133 def handle_update update_count, mouse_x, mouse_y # empty base implementation end |
#height ⇒ Object
73 74 75 |
# File 'lib/wads/textinput.rb', line 73 def height @font.height end |
#move_caret(mouse_x) ⇒ Object
Tries to move the caret to the position specifies by mouse_x
88 89 90 91 92 93 94 95 96 97 98 |
# File 'lib/wads/textinput.rb', line 88 def move_caret(mouse_x) # Test character by character 1.upto(self.text.length) do |i| if mouse_x < x + @font.text_width(text[0...i]) then self.caret_pos = self.selection_start = i - 1; return end end # Default case: user must have clicked the right edge self.caret_pos = self.selection_start = self.text.length end |
#under_point?(mouse_x, mouse_y) ⇒ Boolean
Hit-test for selecting a text field with the mouse.
78 79 80 81 |
# File 'lib/wads/textinput.rb', line 78 def under_point?(mouse_x, mouse_y) mouse_x > x - PADDING and mouse_x < x + width + PADDING and mouse_y > y - PADDING and mouse_y < y + height + PADDING end |
#update(update_count, mouse_x, mouse_y) ⇒ Object
117 118 119 |
# File 'lib/wads/textinput.rb', line 117 def update(update_count, mouse_x, mouse_y) # empty base implementation end |
#width ⇒ Object
This text field grows with the text that’s being entered. (Usually one would use clip_to and scroll around on the text field.)
65 66 67 68 69 70 71 |
# File 'lib/wads/textinput.rb', line 65 def width text_width = @font.text_width(self.text) if text_width > @default_width return text_width end @default_width end |