Class: Ray::Text

Inherits:
Drawable
  • Object
show all
Includes:
TextHelper
Defined in:
lib/ray/text.rb,
ext/text.c

Constant Summary collapse

Normal =
INT2FIX(SAY_TEXT_NORMAL)
Bold =
INT2FIX(SAY_TEXT_BOLD)
Italic =
INT2FIX(SAY_TEXT_ITALIC)
Underlined =
INT2FIX(SAY_TEXT_UNDERLINED)

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from TextHelper

#convert, #internal_string

Constructor Details

#initialize(string, opts = {}) ⇒ Text

Returns a new instance of Text.

Parameters:

  • string (String)

    The content of the text

  • opts (Hash) (defaults to: {})

    a customizable set of options

Options Hash (opts):

  • :encoding (Object) — default: "utf-8"

    The encoding for the text. Unneeded in 1.9.

  • :size (Object) — default: 12

    The character size.

  • :style (Object) — default: Font::STYLE_NORMAL

    Style of the text.

  • :at (Object) — default: (0, 0)

    Position of the text.

  • :scale (Object) — default: (1, 1)

    Scale applied to the text.

  • :zoom (Object)

    Same as :scale

  • :angle (Object) — default: 0

    Rotation applied to the text.

  • :color (Object) — default: Ray::Color.white

    The color used to draw the text

  • :font (Ray::Font, String) — default: Ray::Font.default

    Font used to draw

  • :shader (Ray::Shader) — default: nil

    Shader



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/ray/text.rb', line 17

def initialize(string, opts = {})
  opts = {
    :encoding => string.respond_to?(:encoding) ? string.encoding : "utf-8",
    :size     => 12,
    :style    => Normal,
    :at       => [0, 0],
    :angle    => 0,
    :color    => Ray::Color.white
  }.merge(opts)

  @encoding = opts[:encoding].to_s

  self.string = string
  self.size   = opts[:size]
  self.style  = opts[:style]
  self.pos    = opts[:at]
  self.scale  = opts[:scale] || opts[:zoom] || [1, 1]
  self.angle  = opts[:angle]
  self.color  = opts[:color]
  self.shader = opts[:shader]

  if font = opts[:font]
    self.font = font.is_a?(String) ? Ray::FontSet[font] : font
  end
end

Instance Attribute Details

#encodingString

Returns:

  • (String)


92
93
94
# File 'lib/ray/text.rb', line 92

def encoding
  @encoding
end

Instance Method Details

#collide?(obj) ⇒ true, false

Returns True if the receiver collides with the rect.

Returns:

  • (true, false)

    True if the receiver collides with the rect.



75
76
77
# File 'lib/ray/text.rb', line 75

def collide?(obj)
  rect.collide?(obj.to_rect)
end

#colorColor

Returns Text color.

Returns:



111
112
113
114
# File 'ext/text.c', line 111

static
VALUE ray_text_color(VALUE self) {
  return ray_col2rb(say_text_get_color(ray_rb2text(self)));
}

#color=(val) ⇒ Object

Parameters:

  • val (Color)

    New text color



120
121
122
123
124
# File 'ext/text.c', line 120

static
VALUE ray_text_set_color(VALUE self, VALUE val) {
  say_text_set_color(ray_rb2text(self), ray_rb2col(val));
  return val;
}

#fontRay::Font

Returns:



31
32
33
34
# File 'ext/text.c', line 31

static
VALUE ray_text_font(VALUE self) {
  return rb_iv_get(self, "@font");
}

#font=(font) ⇒ Object

Sets the text’s font.

Parameters:

  • font (Ray::Font)

    Font used when drawing



41
42
43
44
45
46
# File 'ext/text.c', line 41

static
VALUE ray_text_set_font(VALUE self, VALUE font) {
  say_text_set_font(ray_rb2text(self), ray_rb2font(font));
  rb_iv_set(self, "@font", font);
  return font;
}

#initialize_copy(orig) ⇒ Object



23
24
25
26
27
28
# File 'ext/text.c', line 23

static
VALUE ray_text_init_copy(VALUE self, VALUE orig) {
  say_text_copy(ray_rb2text(self), ray_rb2text(orig));
  rb_iv_set(self, "@font", rb_iv_get(orig, "@font"));
  return self;
}

#inside?(obj) ⇒ true, false

Returns True if the receiver is inside the rect. (false if they just collide).

Returns:

  • (true, false)

    True if the receiver is inside the rect. (false if they just collide)



80
81
82
# File 'lib/ray/text.rb', line 80

def inside?(obj)
  rect.inside?(obj.to_rect)
end

#inspectObject



94
95
96
# File 'lib/ray/text.rb', line 94

def inspect
  "text(#{string.inspect})"
end

#outside?(obj) ⇒ true, false

Returns True if the receiver is outside the rect.

Returns:

  • (true, false)

    True if the receiver is outside the rect.



85
86
87
# File 'lib/ray/text.rb', line 85

def outside?(obj)
  rect.outside?(obj.to_rect)
end

#rectRect Also known as: to_rect

Returns Rect occupied by the text.

Returns:

  • (Rect)

    Rect occupied by the text



129
130
131
132
# File 'ext/text.c', line 129

static
VALUE ray_text_rect(VALUE self) {
  return ray_rect2rb(say_text_get_rect(ray_rb2text(self)));
}

#sizeInteger

Returns Character size in pixels.

Returns:

  • (Integer)

    Character size in pixels



74
75
76
77
# File 'ext/text.c', line 74

static
VALUE ray_text_size(VALUE self) {
  return INT2FIX(say_text_get_size(ray_rb2text(self)));
}

#size=(val) ⇒ Object

Parameters:

  • val (Integer)

    New charcter size.



83
84
85
86
87
# File 'ext/text.c', line 83

static
VALUE ray_text_set_size(VALUE self, VALUE val) {
  say_text_set_size(ray_rb2text(self), NUM2ULONG(val));
  return val;
}

#stringString Also known as: to_s

Sets the string used by the text. In 1.9, @encoding is changed approprietly. In 1.8, it is defaulted to utf-8. Change it to whatever is the right encoding.

Returns:

  • (String)

    The string used by the text, using @encoding as the encoding.



54
55
56
57
58
59
60
61
# File 'lib/ray/text.rb', line 54

def string
  str = basic_string
  if str.respond_to? :force_encoding
    str.force_encoding InternalEncoding
  end

  convert(str, @encoding)
end

#string=(val) ⇒ Object

Sets the string used by the text. In 1.9, @encoding is changed approprietly. In 1.8, it is defaulted to utf-8. Change it to whatever is the right encoding.



66
67
68
69
70
71
72
# File 'lib/ray/text.rb', line 66

def string=(val)
  if val.respond_to? :encoding
    @encoding = val.encoding.to_s
  end

  set_basic_string internal_string(val, @encoding)
end

#styleInteger

Returns Text style (see constants Normal, Italic, Bold, and Underlined).

Returns:

  • (Integer)

    Text style (see constants Normal, Italic, Bold, and Underlined)



93
94
95
96
# File 'ext/text.c', line 93

static
VALUE ray_text_style(VALUE self) {
  return INT2FIX(say_text_get_style(ray_rb2text(self)));
}

#style=(style) ⇒ Object

Parameters:

  • style (Integer, Array<Symbol>)

    Flags for the font style. Valid symbols are :normal, :italic, :bold, and :underline.



45
46
47
# File 'lib/ray/text.rb', line 45

def style=(style)
  set_basic_style parse_style(style)
end