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
# File 'lib/ruby2d/text.rb', line 10

def initialize(opts = {})
  @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_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)


39
40
41
# File 'lib/ruby2d/text.rb', line 39

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

#ext_initObject



195
196
197
198
199
200
201
# File 'ext/ruby2d/ruby2d-opal.rb', line 195

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

#ext_renderObject



211
212
213
214
215
216
217
218
219
220
221
222
223
# File 'ext/ruby2d/ruby2d-opal.rb', line 211

def ext_render
  `
  #{self}.data.x = #{self}.x;
  #{self}.data.y = #{self}.y;

  #{self}.data.color.r = #{self}.color.r;
  #{self}.data.color.g = #{self}.color.g;
  #{self}.data.color.b = #{self}.color.b;
  #{self}.data.color.a = #{self}.color.a;

  S2D.DrawText(#{self}.data);
  `
end

#ext_set(msg) ⇒ Object



203
204
205
206
207
208
209
# File 'ext/ruby2d/ruby2d-opal.rb', line 203

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