Class: OR2D::Composites::Text

Inherits:
OR2D::Composite show all
Includes:
Animations::CompositeAnimations
Defined in:
lib/or2d/composites/text.rb

Overview

The Composite Text class is a composite entity that can be used to display multiple lines of text. It displays each line of text on a separate layer. It displays sentences character by character.

Since:

  • 2023-04-26

Instance Attribute Summary

Attributes inherited from OR2D::Composite

#id, #layers

Instance Method Summary collapse

Methods included from Animations::CompositeAnimations

#fade, #fade_in, #fade_out, #grow, #grow_entity, #grow_text_composite, #rotation, #shake, #shrink, #shrink_entity, #shrink_text

Methods inherited from OR2D::Composite

#toggle

Constructor Details

#initialize(options = {}) ⇒ Text

Construct a new Composite Text object.

Parameters:

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

    the options to create the Composite Text object with

Options Hash (options):

  • :limit (Integer) — default: 24

    the maximum number of characters that can be displayed on a single line

  • :lines (Integer) — default: 0

    the number of lines that can be displayed

  • :update_interval (Integer) — default: 5

    the number of frames between each update

Since:

  • 2023-04-26



11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/or2d/composites/text.rb', line 11

def initialize(options = {})
  super(options[:id] || "TextComposite_#{SecureRandom.uuid}")
  @limit = options[:limit] || 24
  @lines = 0
  @update_interval = options[:update_interval] || 5
  if options[:lines]
    options[:lines].each do |line_opt|
      add_line(line_opt)
    end
  elsif options[:line]
    add_line(options[:line])
  end
end

Instance Method Details

#add_line(options = {}) ⇒ Object

Add a new line to the text object. If there are no layers, the line will be added in place. If there are layers, the line will be added below the last layer.

Parameters:

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

    the options to create the layer with

Since:

  • 2023-04-26



77
78
79
80
81
82
83
# File 'lib/or2d/composites/text.rb', line 77

def add_line(options = {})
  @lines += 1
  unless @layers.empty?
    options.merge!(y: OR2D.game.entities[@layers.keys.last].y + (OR2D.game.entities[@layers.keys.last].resource.size + OR2D.scale))
  end
  add_layer(options)
end

#append_text(text, layer_idx) ⇒ Object

Append text to a specific layer.

Parameters:

  • text (String)

    the text to append

  • layer_idx (Integer, Symbol)

    the index of the layer to append the text to

Since:

  • 2023-04-26



96
97
98
99
100
101
# File 'lib/or2d/composites/text.rb', line 96

def append_text(text, layer_idx)
  return if @layers[@layers.keys[layer_idx]][:buffer].length >= @limit

  @layers[@layers.keys[layer_idx]][:buffer] += text
  @layers[@layers.keys[layer_idx]][:buffer] = @layers[@layers.keys[layer_idx]][:buffer].slice(0, @limit)
end

#clear(layer_idx) ⇒ Object

Clear text for a specific layer or all layers

Parameters:

  • layer_idx (Integer, Symbol)

    the index of the layer to clear or :all to clear all layers

Since:

  • 2023-04-26



63
64
65
66
67
68
69
70
71
72
73
# File 'lib/or2d/composites/text.rb', line 63

def clear(layer_idx)
  if layer_idx == :all
    @layers.each do |id, properties|
      properties[:buffer] = String.new
      properties[:cursor] = 0
    end
  else
    @layers[@layers.keys[layer_idx]][:buffer] = String.new
    @layers[@layers.keys[layer_idx]][:cursor] = 0
  end
end

#destroyObject

Note:

This will also destroy the text object.

Destroy the text object. This works by destroying each layer one at a time.

Since:

  • 2023-04-26



150
151
152
153
154
155
# File 'lib/or2d/composites/text.rb', line 150

def destroy
  @lines.times do |index|
    OR2D.game.entities[@layers.keys[index]].destroy
  end
  super
end

#empty?Boolean

Is the text object empty?

Returns:

  • (Boolean)

    whether the text object is empty

Since:

  • 2023-04-26



105
106
107
# File 'lib/or2d/composites/text.rb', line 105

def empty?
  @layers.empty?
end

#hideObject

Hide the text object. This works by hiding each layer one at a time.

Since:

  • 2023-04-26



142
143
144
145
146
# File 'lib/or2d/composites/text.rb', line 142

def hide
  @lines.times do |index|
    OR2D.game.entities[@layers.keys[index]].hide
  end
end

#remove_layer(id, adjust: true) ⇒ Object

Remove a layer from the text object. This works by removing the layer and adjusting the y coordinates of the remaining layers.

Since:

  • 2023-04-26



131
132
133
134
135
136
137
138
139
# File 'lib/or2d/composites/text.rb', line 131

def remove_layer(id, adjust: true)
  if adjust && @lines > 1
    @lines.times do |index|
      OR2D.game.entities[@layers.keys[index - 1]].y -= OR2D.game.entities[id].resource.size + OR2D.scale
    end
  end
  @lines -= 1
  super(id)
end

#set_text(text, layer_idx) ⇒ Object

Set the text for a specific layer.

Parameters:

  • text (String)

    the text to set

  • layer_idx (Integer, Symbol)

    the index of the layer to set the text for

Since:

  • 2023-04-26



88
89
90
91
# File 'lib/or2d/composites/text.rb', line 88

def set_text(text, layer_idx)
  @layers[@layers.keys[layer_idx]][:buffer] = text.slice(0, @limit)
  @layers[@layers.keys[layer_idx]][:cursor] = 0
end

#showObject

Show the text object. This works by showing each layer one at a time.

Since:

  • 2023-04-26



120
121
122
123
124
125
126
127
128
# File 'lib/or2d/composites/text.rb', line 120

def show
  @lines.times do |iteration|
    if iteration == 1
      OR2D.game.entities[@layers.keys[0]].show if @layers[@layers.keys[0]][:show]
    elsif @layers[@layers.keys[iteration - 1]][:show]
      OR2D.game.entities[@layers.keys[iteration - 1]].show
    end
  end
end

#update(force: false) ⇒ Object

Update the text object. This works by updating the text for each layer one character at a time only on key frames determined by the update interval.

Since:

  • 2023-04-26



110
111
112
113
114
115
116
117
# File 'lib/or2d/composites/text.rb', line 110

def update(force: false)
  return unless force || OR2D.game.key_frame?(@update_interval)

  @layers.each do |id, properties|
    OR2D.game.entities[id].resource.text = properties[:buffer].slice(0, properties[:cursor])
    properties[:cursor] += 1 if properties[:cursor] < properties[:buffer].length
  end
end

#x(last: false) ⇒ Integer

Get the x coordinate of the text object.

Parameters:

  • last (Boolean) (defaults to: false)

    whether to get the x coordinate of the last layer or the first layer

Returns:

  • (Integer)

    the x coordinate of the text object

Since:

  • 2023-04-26



28
29
30
# File 'lib/or2d/composites/text.rb', line 28

def x(last: false)
  OR2D.game.entities[last ? @layers.keys.last : @layers.keys.first].x
end

#x=(value) ⇒ Object

Set the x coordinate of the text object.

Parameters:

  • value (Integer)

    the x coordinate to set

Since:

  • 2023-04-26



55
56
57
58
59
# File 'lib/or2d/composites/text.rb', line 55

def x=(value)
  @layers.each_key do |key|
    OR2D.game.entities[key].x = value
  end
end

#y(last: false) ⇒ Integer

Get the y coordinate of the text object.

Parameters:

  • last (Boolean) (defaults to: false)

    whether to get the y coordinate of the last layer or the first layer

Returns:

  • (Integer)

    the y coordinate of the text object

Since:

  • 2023-04-26



35
36
37
# File 'lib/or2d/composites/text.rb', line 35

def y(last: false)
  OR2D.game.entities[last ? @layers.keys.last : @layers.keys.first].y
end

#y=(value) ⇒ Object

Set the y coordinate of the text object.

Parameters:

  • value (Integer)

    the y coordinate to set

Since:

  • 2023-04-26



41
42
43
44
45
46
47
48
49
50
51
# File 'lib/or2d/composites/text.rb', line 41

def y=(value)
  @layers.each_key do |key|
    offset = if key == @layers.keys.first
               0
             else
               previous = @layers.keys[@layers.keys.index(key) - 1]
               OR2D.game.entities[previous].y + (OR2D.game.entities[previous].resource.size / OR2D.scale)
             end
    OR2D.game.entities[key].y = value + offset
  end
end