Class: Ruby2D::Text

Inherits:
Object
  • Object
show all
Includes:
Renderable
Defined in:
lib/ruby2d/text.rb

Overview

Text string drawn using the specified font and size

Instance Attribute Summary collapse

Attributes included from Renderable

#color, #height, #width, #z

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Renderable

#add, #contains?, #remove

Constructor Details

#initialize(text, size: 20, style: nil, font: Font.default, x: 0, y: 0, z: 0, rotate: 0, color: nil, colour: nil, opacity: nil, show: true) ⇒ Text

Create a text string

Parameters:

  • text

    The text to show

  • size (Numeric) (defaults to: 20)

    The font size

  • font (String) (defaults to: Font.default)

    Path to font file to use to draw the text

  • style (String) (defaults to: nil)

    Font style

  • x (Numeric) (defaults to: 0)
  • y (Numeric) (defaults to: 0)
  • z (Numeric) (defaults to: 0)
  • rotate (Numeric) (defaults to: 0)

    Angle, default is 0

  • color (Numeric) (defaults to: nil)

    or colour Colour the text when rendering

  • opacity (Numeric) (defaults to: nil)

    Opacity of the image when rendering

  • show (true, false) (defaults to: true)

    If true the Text is added to Window automatically.



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/ruby2d/text.rb', line 25

def initialize(text, size: 20, style: nil, font: Font.default,
               x: 0, y: 0, z: 0,
               rotate: 0, color: nil, colour: nil,
               opacity: nil, show: true)
  @x = x
  @y = y
  @z = z
  @text = text.to_s
  @size = size
  @rotate = rotate
  @style = style
  self.color = color || colour || 'white'
  self.color.opacity = opacity unless opacity.nil?
  @font_path = font
  @texture = nil
  create_font
  create_texture

  add if show
end

Instance Attribute Details

#dataObject

Returns the value of attribute data.



11
12
13
# File 'lib/ruby2d/text.rb', line 11

def data
  @data
end

#rotateObject

Returns the value of attribute rotate.



11
12
13
# File 'lib/ruby2d/text.rb', line 11

def rotate
  @rotate
end

#sizeObject

Returns the value of attribute size.



10
11
12
# File 'lib/ruby2d/text.rb', line 10

def size
  @size
end

#textObject

Returns the value of attribute text.



10
11
12
# File 'lib/ruby2d/text.rb', line 10

def text
  @text
end

#xObject

Returns the value of attribute x.



11
12
13
# File 'lib/ruby2d/text.rb', line 11

def x
  @x
end

#yObject

Returns the value of attribute y.



11
12
13
# File 'lib/ruby2d/text.rb', line 11

def y
  @y
end

Class Method Details

.create_texture(text, size: 20, style: nil, font: Font.default) ⇒ Object

Create a texture for the specified text

Parameters:

  • text

    The text to show

  • size (Numeric) (defaults to: 20)

    The font size

  • font (String) (defaults to: Font.default)

    Path to font file to use to draw the text

  • style (String) (defaults to: nil)

    Font style



77
78
79
80
# File 'lib/ruby2d/text.rb', line 77

def create_texture(text, size: 20, style: nil, font: Font.default)
  font = Font.load(font, size, style)
  Texture.new(*Text.ext_load_text(font.ttf_font, text))
end

Instance Method Details

#draw(x:, y:, color:, rotate:) ⇒ Object



62
63
64
65
66
67
68
69
# File 'lib/ruby2d/text.rb', line 62

def draw(x:, y:, color:, rotate:)
  Window.render_ready_check

  x ||= @rotate
  color ||= [1.0, 1.0, 1.0, 1.0]

  render(x: x, y: y, color: Color.new(color), rotate: rotate)
end

#fontObject

Returns the path of the font as a string



47
48
49
# File 'lib/ruby2d/text.rb', line 47

def font
  @font_path
end