Class: Ruby2D::Text

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

Overview

A text string drawn with a specified font and size

Constant Summary collapse

STYLE_FLAGS =

Font style name → TTF style flag bit (mirrors SDL3_ttf's TTF_STYLE_*).

{
  normal: 0, bold: 1, italic: 2, underline: 4, strikethrough: 8
}.freeze
NUL =

Frozen NUL string for the content scan below. A bare "\0" literal would allocate a throwaway string on every content= — 360×/frame under a dynamic-text HUD — since the library doesn't enable frozen string literals.

"\0".freeze

Constants included from Interactive

Interactive::OBJECT_EVENTS, Interactive::OBJECT_EVENT_FILTER_PREDICATES

Instance Attribute Summary collapse

Attributes included from Renderable

#color, #height, #padding_bottom, #padding_left, #padding_right, #padding_top, #visible, #width, #x, #x_align, #y, #y_align, #z

Instance Method Summary collapse

Methods included from Renderable

#_alignment_anchor_dx, #_alignment_anchor_dy, #_apply_padding, #_extract_alignment, #_point_in_polygon?, #_point_on_segment?, #_require_numeric_position, #_resolve_alignment, #_unrotate, #_validate_dimensions, #add, #colour=, #contains?, flatten_color, flatten_per_vertex, flatten_points, flatten_resolved_color, #hide, #opacity, #opacity=, #padding=, #remove, resolve_color_or_default, resolve_single_color, #show

Methods included from Interactive

#_fire_event, #interactive?, #off, #on

Constructor Details

#initialize(content, size: 20, style: nil, font: Font.default, x: 0, y: 0, z: 0, rotate: 0, rx: nil, ry: nil, color: nil, colour: nil, opacity: nil, add: true, visible: true, padding: nil, padding_top: nil, padding_right: nil, padding_bottom: nil, padding_left: nil) ⇒ Text

Create a text object



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/ruby2d/text.rb', line 58

def initialize(content, size: 20, style: nil, font: Font.default,
               x: 0, y: 0, z: 0,
               rotate: 0, rx: nil, ry: nil, color: nil, colour: nil,
               opacity: nil, add: true, visible: true,
               padding: nil, padding_top: nil, padding_right: nil,
               padding_bottom: nil, padding_left: nil)
  x, y = _extract_alignment(x, y)
  _apply_padding(padding, padding_top, padding_right, padding_bottom, padding_left)
  @x = x
  @y = y
  @z = z
  @content = validate_content(content).dup.freeze
  @size = validate_size(size)
  @rotate = rotate
  @_user_rx = rx
  @_user_ry = ry
  @style = style
  @style_flags = compute_style_flags(style)
  @font = normalize_font_path(font)

  self.color = color || colour || 'white'
  self.opacity = opacity unless opacity.nil?
  Ext.text_create(self)

  @visible = visible
  self.add if add
end

Instance Attribute Details

#contentObject

Returns the value of attribute content.



18
19
20
# File 'lib/ruby2d/text.rb', line 18

def content
  @content
end

#fontObject

Returns the value of attribute font.



18
19
20
# File 'lib/ruby2d/text.rb', line 18

def font
  @font
end

#rotateObject

Returns the value of attribute rotate.



19
20
21
# File 'lib/ruby2d/text.rb', line 19

def rotate
  @rotate
end

#sizeObject

Returns the value of attribute size.



18
19
20
# File 'lib/ruby2d/text.rb', line 18

def size
  @size
end

#styleObject

Returns the value of attribute style.



18
19
20
# File 'lib/ruby2d/text.rb', line 18

def style
  @style
end

Instance Method Details

#_render_sceneObject

Scene-graph draw hook (see Renderable#_render_scene): render with no overrides, minus its keyword handling — a zero-arg call into a keyword-heavy render still pays microseconds of keyword setup on wasm mruby.



173
174
175
176
# File 'lib/ruby2d/text.rb', line 173

def _render_scene
  _resolve_alignment
  Ext.text_draw(self, rx, ry)
end

#render(x: nil, y: nil, rotate: nil, color: nil, colour: nil, opacity: nil) ⇒ Object

Render the text. Called with overrides for one-shot rendering inside a render block; with no arguments it draws the same frame the scene graph does (delegating to _render_scene).



137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
# File 'lib/ruby2d/text.rb', line 137

def render(x: nil, y: nil, rotate: nil, color: nil, colour: nil, opacity: nil)
  if x.nil? && y.nil? && rotate.nil? && color.nil? && colour.nil? && opacity.nil?
    return _render_scene
  end

  Window.render_ready_check

  saved_x, saved_y = @x, @y
  saved_rotate = @rotate
  saved_color = @color

  @x = x if x
  @y = y if y
  @rotate = rotate if rotate

  c = color || colour
  if c || opacity
    @color = c ? Color.new(c) : Color.new(saved_color)
    @color.opacity = opacity if opacity
  end

  begin
    Ext.text_draw(self, rx, ry)
  ensure
    @x, @y = saved_x, saved_y
    @rotate = saved_rotate
    @color = saved_color
  end
end

#rxObject

Get the rotation center x coordinate



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

def rx
  @_user_rx.nil? ? @x + @width / 2.0 : @_user_rx
end

#rx=(val) ⇒ Object

Set the rotation center x coordinate



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

def rx=(val)
  @_user_rx = val
end

#ryObject

Get the rotation center y coordinate



43
44
45
# File 'lib/ruby2d/text.rb', line 43

def ry
  @_user_ry.nil? ? @y + @height / 2.0 : @_user_ry
end

#ry=(val) ⇒ Object

Set the rotation center y coordinate



53
54
55
# File 'lib/ruby2d/text.rb', line 53

def ry=(val)
  @_user_ry = val
end

#x=(value) ⇒ Object

Set the x position. Pass a symbol (:left, :center, :right) to set alignment intent — resolved at draw time against the window.



23
24
25
26
27
# File 'lib/ruby2d/text.rb', line 23

def x=(value)
  return self.x_align = value if value.is_a?(Symbol)
  @x_align = nil unless @_resolving_alignment
  @x = value
end

#y=(value) ⇒ Object

Set the y position. Pass a symbol (:top, :center, :bottom) to set alignment intent — resolved at draw time against the window.



31
32
33
34
35
# File 'lib/ruby2d/text.rb', line 31

def y=(value)
  return self.y_align = value if value.is_a?(Symbol)
  @y_align = nil unless @_resolving_alignment
  @y = value
end