-
Notifications
You must be signed in to change notification settings - Fork 0
/
textfield.rb
76 lines (63 loc) · 2.6 KB
/
textfield.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
class TextField < Gosu::TextInput
# Some constants that define our appearance.
INACTIVE_COLOR = 0xcc666666
ACTIVE_COLOR = 0xccff6666
SELECTION_COLOR = 0xcc0000ff
CARET_COLOR = 0xffffffff
PADDING = 5
attr_reader :x, :y, :z
def initialize(window, font, x, y, z)
# TextInput's constructor doesn't expect any arguments.
super()
@window, @font, @x, @y, @z = window, font, x, y, z
# Start with a self-explanatory text in each field.
self.text = ""
end
# Example filter method. You can truncate the text to employ a length limit (watch out
# with Ruby 1.8 and UTF-8!), limit the text to certain characters etc.
def filter text
text#.upcase
end
def draw
# Depending on whether this is the currently selected input or not, change the
# background's color.
if @window.text_input == self then
background_color = ACTIVE_COLOR
else
background_color = INACTIVE_COLOR
end
@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, z - 1)
# 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.
@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, z - 1)
# Draw the caret; again, only if this is the currently selected field.
if @window.text_input == self then
@window.draw_line(pos_x, y, CARET_COLOR,
pos_x, y + height, CARET_COLOR, z - 1)
end
# Finally, draw the text itself!
@font.draw(self.text, x, y, z)
end
# This text field grows with the text that's being entered.
# (Usually one would use clip_to and scroll around on the text field.)
def width
@font.text_width(self.text)
end
def height
@font.height
end
# Hit-test for selecting a text field with the mouse.
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
end