Class: CyberarmEngine::Element::EditLine
- Inherits:
-
Button
show all
- Defined in:
- lib/cyberarm_engine/ui/elements/edit_line.rb
Defined Under Namespace
Classes: TextInput
Constant Summary
Constants included
from Theme
Theme::THEME
Instance Attribute Summary
#background_canvas, #border_canvas, #element_visible, #event_handler, #options, #parent, #style, #tip, #x, #y, #z
Instance Method Summary
collapse
-
#begin_drag(_sender, x, _y, _button) ⇒ Object
-
#blur(_sender) ⇒ Object
-
#button_down(id) ⇒ Object
-
#caret_position ⇒ Object
-
#caret_position_under_mouse(mouse_x) ⇒ Object
-
#drag_update(_sender, x, _y, _button) ⇒ Object
-
#draggable?(button) ⇒ Boolean
-
#draw_caret ⇒ Object
-
#draw_selection ⇒ Object
-
#draw_text ⇒ Object
-
#end_drag(_sender, _x, _y, _button) ⇒ Object
-
#enter(sender) ⇒ Object
-
#focus(sender) ⇒ Object
-
#handle_keyboard_shortcuts(id) ⇒ Object
-
#initialize(text, options = {}, block = nil) ⇒ EditLine
constructor
A new instance of EditLine.
-
#keep_caret_visible ⇒ Object
-
#leave(sender) ⇒ Object
-
#left_mouse_button(sender, x, y) ⇒ Object
-
#move_caret_to_mouse(mouse_x, _mouse_y) ⇒ Object
-
#recalculate ⇒ Object
-
#render ⇒ Object
-
#selection_start_position ⇒ Object
-
#text_input_position_for(method) ⇒ Object
-
#update ⇒ Object
-
#value ⇒ Object
-
#value=(string) ⇒ Object
Methods inherited from Button
#draw_image
Methods inherited from TextBlock
#handle_text_wrapping, #line_width
#background=, #background_image=, #background_nine_slice=, #button_up, #child_of?, #clicked_left_mouse_button, #content_height, #content_width, #debug_draw, #default_events, #dimensional_size, #draw, #element_visible?, #enabled=, #enabled?, #focused?, #height, #hide, #hit?, #inner_height, #inner_width, #inspect, #is_root?, #max_scroll_height, #max_scroll_width, #noncontent_height, #noncontent_width, #outer_height, #outer_width, #parent_of?, #recalculate_if_size_changed, #released_left_mouse_button, #reposition, #root, #safe_style_fetch, #scroll_height, #scroll_width, #set_background, #set_background_image, #set_background_nine_slice, #set_border_color, #set_border_thickness, #set_color, #set_font, #set_margin, #set_padding, #set_static_position, #show, #space_available_height, #space_available_width, #stylize, #to_s, #toggle, #update_background, #update_background_image, #update_background_nine_slice, #update_styles, #visible?, #width
Methods included from Common
#alt_down?, #control_down?, #current_state, #darken, #draw_rect, #fill, #find_element_by_tag, #get_asset, #get_image, #get_sample, #get_song, #lighten, #opacity, #pop_state, #previous_state, #push_state, #shift_down?, #shift_state, #show_cursor, #show_cursor=, #window
#event, #publish, #subscribe, #unsubscribe
Methods included from Theme
#deep_merge, #default, #theme_defaults
Constructor Details
#initialize(text, options = {}, block = nil) ⇒ EditLine
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
|
# File 'lib/cyberarm_engine/ui/elements/edit_line.rb', line 18
def initialize(text, options = {}, block = nil)
@filter = options.delete(:filter)
super(text, options, block)
@type = default(:type)
@caret_width = default(:caret_width)
@caret_height = @text.textobject.height
@caret_color = default(:caret_color)
@caret_interval = default(:caret_interval)
@caret_last_interval = Gosu.milliseconds
@show_caret = true
@text_input = TextInput.new
@text_input.filter = @filter
@text_input.text = text
@last_text_value = text
@last_caret_position = @text_input.caret_pos
@offset_x = 0
@offset_y = 0
event(:begin_drag)
event(:drag_update)
event(:end_drag)
end
|
Instance Method Details
#begin_drag(_sender, x, _y, _button) ⇒ Object
263
264
265
266
267
268
269
|
# File 'lib/cyberarm_engine/ui/elements/edit_line.rb', line 263
def begin_drag(_sender, x, _y, _button)
@drag_start = x
@offset_drag_start = @offset_x
@drag_caret_position = @text_input.caret_pos
:handled
end
|
#blur(_sender) ⇒ Object
252
253
254
255
256
257
|
# File 'lib/cyberarm_engine/ui/elements/edit_line.rb', line 252
def blur(_sender)
super
window.text_input = nil
:handled
end
|
107
108
109
|
# File 'lib/cyberarm_engine/ui/elements/edit_line.rb', line 107
def button_down(id)
handle_keyboard_shortcuts(id)
end
|
#caret_position ⇒ Object
190
191
192
|
# File 'lib/cyberarm_engine/ui/elements/edit_line.rb', line 190
def caret_position
text_input_position_for(:caret_pos)
end
|
#caret_position_under_mouse(mouse_x) ⇒ Object
150
151
152
153
154
155
156
|
# File 'lib/cyberarm_engine/ui/elements/edit_line.rb', line 150
def caret_position_under_mouse(mouse_x)
1.upto(@text.text.length) do |i|
return i - 1 if mouse_x < @text.x - @offset_x + @text.width(@text.text[0...i])
end
@text_input.text.length
end
|
#drag_update(_sender, x, _y, _button) ⇒ Object
271
272
273
274
275
|
# File 'lib/cyberarm_engine/ui/elements/edit_line.rb', line 271
def drag_update(_sender, x, _y, _button)
@text_input.caret_pos = caret_position_under_mouse(x)
:handled
end
|
#draggable?(button) ⇒ Boolean
259
260
261
|
# File 'lib/cyberarm_engine/ui/elements/edit_line.rb', line 259
def draggable?(button)
button == :left
end
|
#draw_caret ⇒ Object
59
60
61
|
# File 'lib/cyberarm_engine/ui/elements/edit_line.rb', line 59
def draw_caret
Gosu.draw_rect(caret_position, @text.y, @caret_width, @caret_height, @caret_color, @z)
end
|
#draw_selection ⇒ Object
63
64
65
66
67
|
# File 'lib/cyberarm_engine/ui/elements/edit_line.rb', line 63
def draw_selection
selection_width = caret_position - selection_start_position
Gosu.draw_rect(selection_start_position, @text.y, selection_width, @text.height, default(:selection_color), @z)
end
|
#draw_text ⇒ Object
55
56
57
|
# File 'lib/cyberarm_engine/ui/elements/edit_line.rb', line 55
def draw_text
@text.draw(:draw_text)
end
|
#end_drag(_sender, _x, _y, _button) ⇒ Object
277
278
279
|
# File 'lib/cyberarm_engine/ui/elements/edit_line.rb', line 277
def end_drag(_sender, _x, _y, _button)
:handled
end
|
#enter(sender) ⇒ Object
228
229
230
231
232
233
234
235
236
237
238
|
# File 'lib/cyberarm_engine/ui/elements/edit_line.rb', line 228
def enter(sender)
if @enabled && @focus
update_styles(:active)
elsif @enabled && !@focus
update_styles(:hover)
else
update_styles(:disabled)
end
:handled
end
|
#focus(sender) ⇒ Object
218
219
220
221
222
223
224
225
226
|
# File 'lib/cyberarm_engine/ui/elements/edit_line.rb', line 218
def focus(sender)
@focus = true
window.text_input = @text_input
@text_input.caret_pos = @text_input.selection_start = @text_input.text.length
update_styles(:active)
:handled
end
|
#handle_keyboard_shortcuts(id) ⇒ Object
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
|
# File 'lib/cyberarm_engine/ui/elements/edit_line.rb', line 111
def handle_keyboard_shortcuts(id)
return unless @focus && @enabled
if Gosu.button_down?(Gosu::KB_LEFT_CONTROL) || Gosu.button_down?(Gosu::KB_RIGHT_CONTROL)
case id
when Gosu::KB_A
@text_input.selection_start = 0
@text_input.caret_pos = @text_input.text.length
when Gosu::KB_C
Gosu.clipboard = if @text_input.selection_start < @text_input.caret_pos
@text_input.text[@text_input.selection_start...@text_input.caret_pos]
else
@text_input.text[@text_input.caret_pos...@text_input.selection_start]
end
when Gosu::KB_X
chars = @text_input.text.chars
if @text_input.selection_start < @text_input.caret_pos
Gosu.clipboard = @text_input.text[@text_input.selection_start...@text_input.caret_pos]
chars.slice!(@text_input.selection_start, @text_input.caret_pos)
else
Gosu.clipboard = @text_input.text[@text_input.caret_pos...@text_input.selection_start]
chars.slice!(@text_input.caret_pos, @text_input.selection_start)
end
@text_input.text = chars.join
when Gosu::KB_V
if instance_of?(EditLine) @text_input.insert_text(Gosu.clipboard.gsub("\n", ""))
else
@text_input.insert_text(Gosu.clipboard)
end
end
end
end
|
#keep_caret_visible ⇒ Object
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
|
# File 'lib/cyberarm_engine/ui/elements/edit_line.rb', line 162
def keep_caret_visible
caret_pos = (caret_position - @text.x) + @caret_width
@last_text ||= "/\\"
@last_pos ||= -1
@last_text = @text.text
@last_pos = caret_pos
if caret_pos.between?(@offset_x, @width + @offset_x)
elsif caret_pos < @offset_x
@offset_x = if caret_pos > @width
caret_pos + @width
else
0
end
elsif caret_pos > @width
@offset_x = caret_pos - @width
else
@offset_x = 0
end
end
|
#leave(sender) ⇒ Object
240
241
242
243
244
245
246
247
248
249
250
|
# File 'lib/cyberarm_engine/ui/elements/edit_line.rb', line 240
def leave(sender)
if @enabled && @focus
update_styles(:active)
elsif @enabled && !@focus
update_styles
else
update_styles(:disabled)
end
:handled
end
|
206
207
208
209
210
211
212
213
214
215
216
|
# File 'lib/cyberarm_engine/ui/elements/edit_line.rb', line 206
def left_mouse_button(sender, x, y)
super
window.text_input = @text_input
@caret_last_interval = Gosu.milliseconds
@show_caret = true
move_caret_to_mouse(x, y)
:handled
end
|
#move_caret_to_mouse(mouse_x, _mouse_y) ⇒ Object
158
159
160
|
# File 'lib/cyberarm_engine/ui/elements/edit_line.rb', line 158
def move_caret_to_mouse(mouse_x, _mouse_y)
@text_input.caret_pos = @text_input.selection_start = caret_position_under_mouse(mouse_x)
end
|
#recalculate ⇒ Object
281
282
283
284
285
286
|
# File 'lib/cyberarm_engine/ui/elements/edit_line.rb', line 281
def recalculate
super
@width = dimensional_size(@style.width, :width) || default(:width)
update_background
end
|
#render ⇒ Object
45
46
47
48
49
50
51
52
53
|
# File 'lib/cyberarm_engine/ui/elements/edit_line.rb', line 45
def render
Gosu.clip_to(@text.x, @text.y, @width, @height) do
Gosu.translate(-@offset_x, -@offset_y) do
draw_selection
draw_caret if @focus && @show_caret
draw_text
end
end
end
|
#selection_start_position ⇒ Object
194
195
196
|
# File 'lib/cyberarm_engine/ui/elements/edit_line.rb', line 194
def selection_start_position
text_input_position_for(:selection_start)
end
|
#text_input_position_for(method) ⇒ Object
198
199
200
201
202
203
204
|
# File 'lib/cyberarm_engine/ui/elements/edit_line.rb', line 198
def text_input_position_for(method)
if @type == :password
@text.x + @text.width(default(:password_character) * @text_input.text[0...@text_input.send(method)].length)
else
@text.x + @text.width(@text_input.text[0...@text_input.send(method)]) - @style.border_thickness_left
end
end
|
#update ⇒ Object
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
|
# File 'lib/cyberarm_engine/ui/elements/edit_line.rb', line 69
def update
@style_event = :active if @focus
@text.text = if @type == :password
default(:password_character) * @text_input.text.length
else
@text_input.text
end
if @last_text_value != value
@last_text_value = value
@show_caret = true
@caret_last_interval = Gosu.milliseconds
root.gui_state.request_repaint
publish(:changed, value)
end
if @last_caret_position != @text_input.caret_pos
@last_caret_position = @text_input.caret_pos
root.gui_state.request_repaint
@show_caret = true
@caret_last_interval = Gosu.milliseconds
end
if Gosu.milliseconds >= @caret_last_interval + @caret_interval
root.gui_state.request_repaint
@caret_last_interval = Gosu.milliseconds
@show_caret = !@show_caret
end
keep_caret_visible
end
|
#value ⇒ Object
288
289
290
|
# File 'lib/cyberarm_engine/ui/elements/edit_line.rb', line 288
def value
@text_input.text
end
|
#value=(string) ⇒ Object
292
293
294
|
# File 'lib/cyberarm_engine/ui/elements/edit_line.rb', line 292
def value=(string)
@text_input.text = string
end
|