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

#auto_centerRay::Vector2?

Returns Auto-centering ratio. Nil when disabled.

Returns:

  • (Ray::Vector2, nil)

    Auto-centering ratio. Nil when disabled.



145
146
147
148
149
150
151
152
153
# File 'ext/text.c', line 145

static
VALUE ray_text_auto_center(VALUE self) {
  say_text *text = ray_rb2text(self);

  if (say_text_auto_center(text))
    return ray_vector2_to_rb(say_text_get_auto_center_ratio(text));
  else
    return Qnil;
}

#auto_center=(val) ⇒ Object

Sets the auto centering ratio. It’s a vector2 containing values between 0 and 1 (typically, at least) to determine the center of the text when it is resized. (0.5, 1), for example, would set the origin to (middle, bottom).

Parameters:

  • val (Ray::Vector2, nil)

    New auto centering ratio. If nil, disables auto-centering.



164
165
166
167
168
169
170
171
172
173
174
# File 'ext/text.c', line 164

static
VALUE ray_text_set_auto_center(VALUE self, VALUE center) {
  say_text *text = ray_rb2text(self);

  if (NIL_P(center))
    say_text_disable_auto_center(text);
  else
    say_text_enable_auto_center(text, ray_convert_to_vector2(center));

  return center;
}

#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:



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

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



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

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:



39
40
41
42
# File 'ext/text.c', line 39

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



49
50
51
52
53
54
# File 'ext/text.c', line 49

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



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

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"));
  ray_drawable_copy_attr(self, orig);
  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

#pretty_print(q, other_attributes = []) ⇒ Object



100
101
102
103
104
105
106
107
108
# File 'lib/ray/text.rb', line 100

def pretty_print(q, other_attributes = [])
  attr = [
          "string",
          "font", "color", "size", "style",
          "rect", "auto_center"
         ]

  super q, (attr + other_attributes)
end

#rectRect Also known as: to_rect

Returns Rect occupied by the text.

Returns:

  • (Rect)

    Rect occupied by the text



137
138
139
140
# File 'ext/text.c', line 137

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



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

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.



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

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)



101
102
103
104
# File 'ext/text.c', line 101

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