Class: Ruby2D::Text

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

Instance Attribute Summary collapse

Attributes included from Renderable

#z

Instance Method Summary collapse

Methods included from Renderable

#add, #opacity, #opacity=, #remove

Constructor Details

#initialize(opts = {}) ⇒ Text

Returns a new instance of Text.



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/ruby2d/text.rb', line 10

def initialize(opts = {})
  @type_id = 6

  @x = opts[:x] || 0
  @y = opts[:y] || 0
  @z = opts[:z] || 0
  @text = (opts[:text] || "Hello World!").to_s
  @size = opts[:size] || 20

  @font = opts[:font]

  unless RUBY_ENGINE == 'opal'
    unless File.exists? @font
      raise Error, "Cannot find font file `#{@font}`"
    end
  end

  self.color = opts[:color] || 'white'
  ext_text_init
  add
end

Instance Attribute Details

#colorObject

Returns the value of attribute color.



8
9
10
# File 'lib/ruby2d/text.rb', line 8

def color
  @color
end

#dataObject

Returns the value of attribute data.



7
8
9
# File 'lib/ruby2d/text.rb', line 7

def data
  @data
end

#fontObject (readonly)

Returns the value of attribute font.



8
9
10
# File 'lib/ruby2d/text.rb', line 8

def font
  @font
end

#heightObject (readonly)

Returns the value of attribute height.



8
9
10
# File 'lib/ruby2d/text.rb', line 8

def height
  @height
end

#sizeObject (readonly)

Returns the value of attribute size.



8
9
10
# File 'lib/ruby2d/text.rb', line 8

def size
  @size
end

#textObject

Returns the value of attribute text.



8
9
10
# File 'lib/ruby2d/text.rb', line 8

def text
  @text
end

#widthObject (readonly)

Returns the value of attribute width.



8
9
10
# File 'lib/ruby2d/text.rb', line 8

def width
  @width
end

#xObject

Returns the value of attribute x.



7
8
9
# File 'lib/ruby2d/text.rb', line 7

def x
  @x
end

#yObject

Returns the value of attribute y.



7
8
9
# File 'lib/ruby2d/text.rb', line 7

def y
  @y
end

Instance Method Details

#contains?(x, y) ⇒ Boolean

Returns:

  • (Boolean)


41
42
43
# File 'lib/ruby2d/text.rb', line 41

def contains?(x, y)
  @x < x and @x + @width > x and @y < y and @y + @height > y
end

#ext_text_initObject



208
209
210
211
212
# File 'ext/ruby2d/ruby2d-opal.rb', line 208

def ext_text_init
  `#{self}.data = S2D.CreateText(#{self}.font, #{self}.text, #{self}.size);`
  @width  = `#{self}.data.width;`
  @height = `#{self}.data.height;`
end

#ext_text_set(msg) ⇒ Object



214
215
216
217
218
# File 'ext/ruby2d/ruby2d-opal.rb', line 214

def ext_text_set(msg)
  `S2D.SetText(#{self}.data, #{msg});`
  @width  = `#{self}.data.width;`
  @height = `#{self}.data.height;`
end