Class: OR2D::Composites::Text
- Inherits:
-
OR2D::Composite
- Object
- OR2D::Composite
- OR2D::Composites::Text
- 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.
Instance Attribute Summary
Attributes inherited from OR2D::Composite
Instance Method Summary collapse
-
#add_line(options = {}) ⇒ Object
Add a new line to the text object.
-
#append_text(text, layer_idx) ⇒ Object
Append text to a specific layer.
-
#clear(layer_idx) ⇒ Object
Clear text for a specific layer or all layers.
-
#destroy ⇒ Object
Destroy the text object.
-
#empty? ⇒ Boolean
Is the text object empty?.
-
#hide ⇒ Object
Hide the text object.
-
#initialize(options = {}) ⇒ Text
constructor
Construct a new Composite Text object.
-
#remove_layer(id, adjust: true) ⇒ Object
Remove a layer from the text object.
-
#set_text(text, layer_idx) ⇒ Object
Set the text for a specific layer.
-
#show ⇒ Object
Show the text object.
-
#update(force: false) ⇒ Object
Update the text object.
-
#x(last: false) ⇒ Integer
Get the x coordinate of the text object.
-
#x=(value) ⇒ Object
Set the x coordinate of the text object.
-
#y(last: false) ⇒ Integer
Get the y coordinate of the text object.
-
#y=(value) ⇒ Object
Set the y coordinate of the text object.
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
Constructor Details
#initialize(options = {}) ⇒ Text
Construct a new Composite Text object.
11 12 13 14 15 16 17 18 19 20 21 22 23 |
# File 'lib/or2d/composites/text.rb', line 11 def initialize( = {}) super([:id] || "TextComposite_#{SecureRandom.uuid}") @limit = [:limit] || 24 @lines = 0 @update_interval = [:update_interval] || 5 if [:lines] [:lines].each do |line_opt| add_line(line_opt) end elsif [:line] add_line([: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.
77 78 79 80 81 82 83 |
# File 'lib/or2d/composites/text.rb', line 77 def add_line( = {}) @lines += 1 unless @layers.empty? .merge!(y: OR2D.game.entities[@layers.keys.last].y + (OR2D.game.entities[@layers.keys.last].resource.size + OR2D.scale)) end add_layer() end |
#append_text(text, layer_idx) ⇒ Object
Append text to a specific layer.
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
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 |
#destroy ⇒ Object
This will also destroy the text object.
Destroy the text object. This works by destroying each layer one at a time.
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?
105 106 107 |
# File 'lib/or2d/composites/text.rb', line 105 def empty? @layers.empty? end |
#hide ⇒ Object
Hide the text object. This works by hiding each layer one at a time.
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.
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.
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 |
#show ⇒ Object
Show the text object. This works by showing each layer one at a time.
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.
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.
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.
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.
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.
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 |