Class: GGLib::TextBox

Inherits:
Widget show all
Defined in:
lib/ext/widgets.rb

Constant Summary collapse

CaretColor =
0xff646464
TextColor =
0xff646464
SelColor =
0xff33FFFF
TextPadding =
5

Instance Attribute Summary

Attributes inherited from Widget

#buttonId, #defimage, #id, #name, #sleeping, #theme, #window, #zfocus

Attributes inherited from Tile

#id, #inclusive, #x1, #x2, #y1, #y2

Instance Method Summary collapse

Methods inherited from Widget

#blur, #button, #clicked?, #del, #event, #feedText, #focus, #hasFocus?, #hasStickyFocus?, #intDraw, #onClick, #onDelete, #onDrag, #onInitialize, #onKeyPress, #onRightClick, #onRightDrag, #over?, #sleep, #sleeping?, #stickFocus, #unstickFocus, #wakeUp

Methods inherited from Tile

#centerOn, #del, deleteAllInstances, deleteById, #each, #eachBorder, getAllInstances, getById, #height, #iTile, intersect?, #intersect?, #isInTile?, #move, setAllInstances, #setCoordinates, #setTile, #xTile

Constructor Details

#initialize(name, x, y, len = 12, theme = Themes::blank, w = nil, height = nil) ⇒ TextBox

Returns a new instance of TextBox.



238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
# File 'lib/ext/widgets.rb', line 238

def initialize(name, x, y, len=12, theme=Themes::blank, w=nil,height=nil)
  if width!=nil and height!=nil
    super(name, x, y, x+w, y+h, theme)
  else
    @theme = theme.newInstance(self) #Get specialized instance early
    @theme.setCoords(1,1,1,1) 
    super(name, x, y, x+@theme.width, y+@theme.height, theme)
    @theme.setCoords(@x1, @y1, @x2, @y2)
  end
  @textinput=Gosu::TextInput.new
  @offsety=((@theme.height-@theme.font.editable.height)/2).floor
  @maxlength=len
  @x=x
  @y=y
  @drawcursor=0
  @realwidth=@x + TextPadding
end

Instance Method Details

#drawObject



282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
# File 'lib/ext/widgets.rb', line 282

def draw
  text=@textinput.text.slice(0,12)
  if self.hasStickyFocus?
    pos_x = @x + @theme.font.editable.text_width(@textinput.text[0...@textinput.caret_pos]) + TextPadding
    sel_x = @x + @theme.font.editable.text_width(@textinput.text[0...@textinput.selection_start]) + TextPadding
    @realwidth+=@theme.font.editable.text_width(text)
    if pos_x > @realwidth
      pos_x=@realwidth+1 
      sel_x=@realwidth+1
      @textinput.text=text
    end
    @realwidth-=@theme.font.editable.text_width(text)
    if @drawcursor < 18
      $window.draw_line(pos_x, @y+@offsety, CaretColor, pos_x, @y+@theme.font.editable.height+@offsety, CaretColor, ZOrder::Text)
    elsif @drawcursor > 36
      @drawcursor=0
    end
    @drawcursor+=1
    $window.draw_quad(sel_x, @y+@offsety, @theme.font.selcolor, pos_x, @y+@offsety, @theme.font.selcolor, sel_x, @y+@theme.font.editable.height+@offsety, @theme.font.selcolor, pos_x, @y+@theme.font.editable.height+@offsety, @theme.font.selcolor, ZOrder::Text-0.5)
  end
  @theme.font.editable.draw(text, @x+TextPadding, @y+@offsety, ZOrder::Text, 1, 1, @theme.font.color)
end

#onMouseClickObject



261
262
263
# File 'lib/ext/widgets.rb', line 261

def onMouseClick
  stickFocus
end

#onMouseOutObject



258
259
260
# File 'lib/ext/widgets.rb', line 258

def onMouseOut
  @theme.setDefaultState unless self.hasStickyFocus?
end

#onMouseOverObject



255
256
257
# File 'lib/ext/widgets.rb', line 255

def onMouseOver
  @theme.setOverState unless self.hasStickyFocus?
end

#onStickyBlurObject



268
269
270
271
# File 'lib/ext/widgets.rb', line 268

def onStickyBlur
  @theme.setDefaultState
  $window.setTextInput(nil)
end

#onStickyFocusObject



264
265
266
267
# File 'lib/ext/widgets.rb', line 264

def onStickyFocus
  @theme.setOverState
  $window.setTextInput(@textinput)
end

#textObject



275
276
277
# File 'lib/ext/widgets.rb', line 275

def text
  return @textinput.text
end

#text=(val) ⇒ Object



278
279
280
281
# File 'lib/ext/widgets.rb', line 278

def text=(val)
  val=val.slice(0,12) if val.size>12
  @textinput.text = val
end

#widthObject



272
273
274
# File 'lib/ext/widgets.rb', line 272

def width
  return @maxlength
end