Class: Core::GUI::Textfield

Inherits:
Element show all
Defined in:
lib/gui/textfield.rb

Overview

Displays text in the given font and alignment

Direct Known Subclasses

Core::Game::Bubble

Instance Attribute Summary collapse

Attributes inherited from Element

#h, #w, #x, #xoff, #y, #yoff, #zoff

Instance Method Summary collapse

Methods inherited from Element

#remove?, #update

Constructor Details

#initialize(x, y, w, h, txt = "", size = 20, align = :left, fixed = true, maxw = w) ⇒ Textfield

Returns a new instance of Textfield.



8
9
10
11
12
13
14
15
16
# File 'lib/gui/textfield.rb', line 8

def initialize(x, y, w, h, txt="", size=20, align=:left, fixed=true, maxw=w)
  super(x, y, w, h)
  @bg = Core.sprite("gui/container_background")
  @font = Core.font(Core::DEFAULT_FONT, size)
  @align = align
  @fixed = fixed
  @max_width = maxw
  set_text(txt)
end

Instance Attribute Details

#max_widthObject

Returns the value of attribute max_width.



6
7
8
# File 'lib/gui/textfield.rb', line 6

def max_width
  @max_width
end

Instance Method Details

#drawObject



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/gui/textfield.rb', line 35

def draw
  @bg.draw(@x+@xoff, @y+@yoff, Core::GUI_Z + @zoff, (@w)/@bg.width.to_f, @h/@bg.height.to_f)
  y = 0
  if @text.size == 1
    y += 4
  end
  i = 0
  @text.each { |line|
    case @align
    when :center
      @font.draw(line, @x + @xoff + (@w/2) - (@fws[i]/2), @y + @yoff + (@font.height/9) + y, Core::GUI_Z + 10 + @zoff, 1, 1, Gosu::Color::BLACK)
    when :left
      @font.draw(line, @x + @xoff + 4, @y + @yoff + (@font.height/9) + y, Core::GUI_Z + 10 + @zoff, 1, 1, Gosu::Color::BLACK)
    when :right
      @font.draw(line, @x + @xoff + (@w-@fws[i]), @y + @yoff + (@font.height/9) + y, Core::GUI_Z + 10 + @zoff, 1, 1, Gosu::Color::BLACK)
    end
    y += @font.height
    i += 1
  }
end

#text=(text) ⇒ Object Also known as: set_text



18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/gui/textfield.rb', line 18

def text=(text)
  if !@fixed
    @w = @max_width
  end
  @text = Core.multiline(text, @w, @font)
  if !@fixed
    @w = 8 + @font.text_width(@text.sort_by { |line| line.length}.reverse!.first)
    @h = 8 + @text.size * (@font.height)
  end
  @fws = []
  @text.each { |line|
    @fws.push(@font.text_width(line))
  }
end