Class: AGL::TextField

Inherits:
Object
  • Object
show all
Defined in:
lib/minigl/forms.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(x, y, font, img, cursor_img = nil, text = "", margin_x = 0, margin_y = 0, max_length = 100) ⇒ TextField

Returns a new instance of TextField.



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
106
107
108
109
110
111
112
113
114
115
# File 'lib/minigl/forms.rb', line 80

def initialize x, y, font, img, cursor_img = nil, text = "", margin_x = 0, margin_y = 0, max_length = 100
  @x = x
  @y = y
  @font = font
  @img = Res.img img
  @w = @img.width
  @h = @img.height
  @cursor_img = Res.img(cursor_img) if cursor_img
  @text = text
  @text_x = x + margin_x
  @text_y = y + margin_y
  @max_length = max_length
  
  @nodes = [x + margin_x]
  @cur_node = 0
  @cursor_visible = true
  @cursor_timer = 0
  
  @k = [
    Gosu::KbA, Gosu::KbB, Gosu::KbC, Gosu::KbD, Gosu::KbE, Gosu::KbF,
    Gosu::KbG, Gosu::KbH, Gosu::KbI, Gosu::KbJ, Gosu::KbK, Gosu::KbL,
    Gosu::KbM, Gosu::KbN, Gosu::KbO, Gosu::KbP, Gosu::KbQ, Gosu::KbR,
    Gosu::KbS, Gosu::KbT, Gosu::KbU, Gosu::KbV, Gosu::KbW, Gosu::KbX,
    Gosu::KbY, Gosu::KbZ, Gosu::Kb1, Gosu::Kb2, Gosu::Kb3, Gosu::Kb4,
    Gosu::Kb5, Gosu::Kb6, Gosu::Kb7, Gosu::Kb8, Gosu::Kb9, Gosu::Kb0,
    Gosu::KbNumpad1, Gosu::KbNumpad2, Gosu::KbNumpad3, Gosu::KbNumpad4,
    Gosu::KbNumpad5, Gosu::KbNumpad6, Gosu::KbNumpad7, Gosu::KbNumpad8,
    Gosu::KbNumpad9, Gosu::KbNumpad0, Gosu::KbSpace, Gosu::KbBackspace,
    Gosu::KbDelete, Gosu::KbLeft, Gosu::KbRight, Gosu::KbHome,
    Gosu::KbEnd, Gosu::KbLeftShift, Gosu::KbRightShift,
    Gosu::KbBacktick, Gosu::KbMinus, Gosu::KbEqual, Gosu::KbBracketLeft,
    Gosu::KbBracketRight, Gosu::KbBackslash, Gosu::KbApostrophe,
    Gosu::KbComma, Gosu::KbPeriod, Gosu::KbSlash
  ]
  @chars = "abcdefghijklmnopqrstuvwxyz1234567890 ABCDEFGHIJKLMNOPQRSTUVWXYZ'-=/[]\\,.;\"_+?{}|<>:!@#$%6&*()"
end

Instance Attribute Details

#textObject

Returns the value of attribute text.



78
79
80
# File 'lib/minigl/forms.rb', line 78

def text
  @text
end

Instance Method Details

#draw(text_color = 0, selection_color = 0, alpha = 0xff) ⇒ Object



346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
# File 'lib/minigl/forms.rb', line 346

def draw text_color = 0, selection_color = 0, alpha = 0xff
  color = (alpha << 24) | 0xffffff
  text_color = (alpha << 24) | text_color
  @img.draw @x, @y, 0, 1, 1, color
  @font.draw @text, @text_x, @text_y, 0, 1, 1, text_color
  
  if @anchor1 and @anchor2
    selection_color = ((alpha / 2) << 24) | selection_color
    Game.window.draw_quad @nodes[@anchor1], @text_y, selection_color,
                          @nodes[@anchor2] + 1, @text_y, selection_color,
                          @nodes[@anchor2] + 1, @text_y + @font.height, selection_color,
                          @nodes[@anchor1], @text_y + @font.height, selection_color, 0
  end
  
  if @cursor_visible
    if @cursor_img; @cursor_img.draw @nodes[@cur_node] - @cursor_img.width / 2, @text_y, 0
    else
      cursor_color = (alpha << 24) | 0
      Game.window.draw_quad @nodes[@cur_node], @text_y, cursor_color,
                            @nodes[@cur_node] + 1, @text_y, cursor_color,
                            @nodes[@cur_node] + 1, @text_y + @font.height, cursor_color,
                            @nodes[@cur_node], @text_y + @font.height, cursor_color, 0
    end
  end
end

#insert_char(char) ⇒ Object



305
306
307
308
309
310
311
312
313
314
315
# File 'lib/minigl/forms.rb', line 305

def insert_char char
  if @text.length < @max_length
    @text.insert @cur_node, char
    @nodes.insert @cur_node + 1, @nodes[@cur_node] + @font.text_width(char)
    for i in (@cur_node + 2)..(@nodes.size - 1)
      @nodes[i] += @font.text_width(char)
    end
    @cur_node += 1
    set_cursor_visible
  end
end

#remove_char(back) ⇒ Object



335
336
337
338
339
340
341
342
343
344
# File 'lib/minigl/forms.rb', line 335

def remove_char back
  @cur_node -= 1 if back
  char_width = @font.text_width(@text[@cur_node])
  @text[@cur_node] = ""
  @nodes.delete_at @cur_node + 1
  for i in (@cur_node + 1)..(@nodes.size - 1)
    @nodes[i] -= char_width
  end
  set_cursor_visible
end

#remove_intervalObject



317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
# File 'lib/minigl/forms.rb', line 317

def remove_interval
  min = @anchor1 < @anchor2 ? @anchor1 : @anchor2
  max = min == @anchor1 ? @anchor2 : @anchor1
  interval_width = 0
  for i in min...max
    interval_width += @font.text_width(@text[i])
    @nodes.delete_at min + 1
  end
  @text[min...max] = ""
  for i in (min + 1)..(@nodes.size - 1)
    @nodes[i] -= interval_width
  end
  @cur_node = min
  @anchor1 = nil
  @anchor2 = nil
  set_cursor_visible
end

#selected_textObject



131
132
133
134
135
136
# File 'lib/minigl/forms.rb', line 131

def selected_text
  return "" if @anchor2.nil?
  min = @anchor1 < @anchor2 ? @anchor1 : @anchor2
  max = min == @anchor1 ? @anchor2 : @anchor1
  @text[min..max]
end

#set_cursor_visibleObject



285
286
287
288
# File 'lib/minigl/forms.rb', line 285

def set_cursor_visible
  @cursor_visible = true
  @cursor_timer = 0
end

#set_node_by_mouseObject



290
291
292
293
294
295
296
297
298
299
300
301
302
303
# File 'lib/minigl/forms.rb', line 290

def set_node_by_mouse
  index = @nodes.size - 1
  @nodes.each_with_index do |n, i|
    if n >= Mouse.x
      index = i
      break
    end
  end
  if index > 0
    d1 = @nodes[index] - Mouse.x; d2 = Mouse.x - @nodes[index - 1]
    index -= 1 if d1 > d2
  end
  @cur_node = index
end

#updateObject



138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
# File 'lib/minigl/forms.rb', line 138

def update
  ################################ Mouse ################################
  if Mouse.over? @x, @y, @w, @h
    if Mouse.double_click? :left
      @anchor1 = 0
      @anchor2 = @nodes.size - 1
      @cur_node = @anchor2
      set_cursor_visible
    elsif Mouse.button_pressed? :left
      set_node_by_mouse
      @anchor1 = @cur_node
      @anchor2 = nil
      set_cursor_visible
    end
  end
  if Mouse.button_down? :left
    if @anchor1
      set_node_by_mouse
      if @cur_node != @anchor1; @anchor2 = @cur_node
      else; @anchor2 = nil; end
      set_cursor_visible
    end
  elsif Mouse.button_released? :left
    if @anchor1
      if @cur_node != @anchor1; @anchor2 = @cur_node
      else; @anchor1 = nil; end
    end
  end
  
  @cursor_timer += 1
  if @cursor_timer >= 30
    @cursor_visible = (not @cursor_visible)
    @cursor_timer = 0
  end
  
  ############################### Keyboard ##############################
  shift = KB.key_down?(@k[53]) or KB.key_down?(@k[54])
  if KB.key_pressed?(@k[53]) or KB.key_pressed?(@k[54]) # shift
    @anchor1 = @cur_node if @anchor1.nil?
  elsif KB.key_released?(@k[53]) or KB.key_released?(@k[54])
    @anchor1 = nil if @anchor2.nil?
  end
  inserted = false
  for i in 0..46 # alnum
    if KB.key_pressed?(@k[i]) or KB.key_held?(@k[i])
      remove_interval if @anchor1 and @anchor2
      if i < 26
#           bool capsLock = Console.CapsLock;
        if shift
#             if (capsLock) insert_char(@chars[i]);
#             else
          insert_char @chars[i + 37]
        else
#             if (capsLock) insert_char(@chars[i + 37]);
#             else
          insert_char @chars[i]
        end
      elsif i < 36
        if shift
          insert_char @chars[i + 57]
        else; insert_char @chars[i]; end
      elsif shift
        insert_char(@chars[i + 47]);
      else; insert_char(@chars[i - 10]); end
      inserted = true
      break
    end
  end
  
  return if inserted
  for i in 55..64 # special
    if KB.key_pressed?(@k[i]) or KB.key_held?(@k[i])
      if shift; insert_char @chars[i + 18]
      else; insert_char @chars[i + 8]; end
      inserted = true
      break
    end
  end
  
  return if inserted
  if KB.key_pressed?(@k[47]) or KB.key_held?(@k[47]) # back
    if @anchor1 and @anchor2
      remove_interval
    elsif @cur_node > 0
      remove_char true
    end
  elsif KB.key_pressed?(@k[48]) or KB.key_held?(@k[48]) # del
    if @anchor1 and @anchor2
      remove_interval
    elsif @cur_node < @nodes.size - 1
      remove_char false
    end
  elsif KB.key_pressed?(@k[49]) or KB.key_held?(@k[49]) # left
    if @anchor1
      if shift
        if @cur_node > 0
          @cur_node -= 1
          @anchor2 = @cur_node
          set_cursor_visible
        end
      elsif @anchor2
        @cur_node = @anchor1 < @anchor2 ? @anchor1 : @anchor2
        @anchor1 = nil
        @anchor2 = nil
        set_cursor_visible
      end
    elsif @cur_node > 0
      @cur_node -= 1
      set_cursor_visible
    end
  elsif KB.key_pressed?(@k[50]) or KB.key_held?(@k[50]) # right
    if @anchor1
      if shift
        if @cur_node < @nodes.size - 1
          @cur_node += 1
          @anchor2 = @cur_node
          set_cursor_visible
        end
      elsif @anchor2
        @cur_node = @anchor1 > @anchor2 ? @anchor1 : @anchor2
        @anchor1 = nil
        @anchor2 = nil
        set_cursor_visible
      end
    elsif @cur_node < @nodes.size - 1
      @cur_node += 1
      set_cursor_visible
    end
  elsif KB.key_pressed?(@k[51]) # home
    @cur_node = 0
    if shift; @anchor2 = @cur_node
    else
      @anchor1 = nil
      @anchor2 = nil
    end
    set_cursor_visible
  elsif KB.key_pressed?(@k[52]) # end
    @cur_node = @nodes.size - 1
    if shift; @anchor2 = @cur_node
    else
      @anchor1 = nil
      @anchor2 = nil
    end
    set_cursor_visible
  end
end