Class: CyberarmEngine::Text
- Inherits:
-
Object
- Object
- CyberarmEngine::Text
- Defined in:
- lib/cyberarm_engine/text.rb
Constant Summary collapse
- CACHE =
{}
Instance Attribute Summary collapse
-
#color ⇒ Object
Returns the value of attribute color.
-
#factor_x ⇒ Object
Returns the value of attribute factor_x.
-
#factor_y ⇒ Object
Returns the value of attribute factor_y.
-
#options ⇒ Object
Returns the value of attribute options.
-
#shadow ⇒ Object
Returns the value of attribute shadow.
-
#shadow_alpha ⇒ Object
Returns the value of attribute shadow_alpha.
-
#shadow_color ⇒ Object
Returns the value of attribute shadow_color.
-
#shadow_size ⇒ Object
Returns the value of attribute shadow_size.
-
#size ⇒ Object
Returns the value of attribute size.
-
#text ⇒ Object
Returns the value of attribute text.
-
#textobject ⇒ Object
readonly
Returns the value of attribute textobject.
-
#x ⇒ Object
Returns the value of attribute x.
-
#y ⇒ Object
Returns the value of attribute y.
-
#z ⇒ Object
Returns the value of attribute z.
Instance Method Summary collapse
- #alpha ⇒ Object
- #alpha=(n) ⇒ Object
- #check_cache(size, font_name) ⇒ Object
- #draw(method = :draw_markup) ⇒ Object
- #height(text = @text) ⇒ Object
-
#initialize(text, options = {}) ⇒ Text
constructor
A new instance of Text.
- #markup_width(text = @text) ⇒ Object
- #swap_font(size, font_name = @font) ⇒ Object
- #update ⇒ Object
- #width(text = @text) ⇒ Object
Constructor Details
#initialize(text, options = {}) ⇒ Text
Returns a new instance of Text.
8 9 10 11 12 13 14 15 16 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 42 |
# File 'lib/cyberarm_engine/text.rb', line 8 def initialize(text, = {}) @text = text.to_s || "" @options = @size = [:size] || 18 @font = [:font] || Gosu.default_font_name @x = [:x] || 0 @y = [:y] || 0 @z = [:z] || 1025 @factor_x = [:factor_x] || 1 @factor_y = [:factor_y] || 1 @color = [:color] || Gosu::Color::WHITE @mode = [:mode] || :default @alignment = [:alignment] || nil @shadow = true if [:shadow] == true @shadow = false if [:shadow] == false @shadow = true if [:shadow].nil? @shadow_size = [:shadow_size] || 1 @shadow_alpha = [:shadow_alpha] || 30 @shadow_alpha = [:shadow_alpha] || 30 @shadow_color = [:shadow_color] @textobject = check_cache(@size, @font) if @alignment case @alignment when :left @x = 0 + BUTTON_PADDING when :center @x = ($window.width / 2) - (@textobject.text_width(@text) / 2) when :right @x = $window.width - BUTTON_PADDING - @textobject.text_width(@text) end end self end |
Instance Attribute Details
#color ⇒ Object
Returns the value of attribute color.
6 7 8 |
# File 'lib/cyberarm_engine/text.rb', line 6 def color @color end |
#factor_x ⇒ Object
Returns the value of attribute factor_x.
6 7 8 |
# File 'lib/cyberarm_engine/text.rb', line 6 def factor_x @factor_x end |
#factor_y ⇒ Object
Returns the value of attribute factor_y.
6 7 8 |
# File 'lib/cyberarm_engine/text.rb', line 6 def factor_y @factor_y end |
#options ⇒ Object
Returns the value of attribute options.
5 6 7 |
# File 'lib/cyberarm_engine/text.rb', line 5 def @options end |
#shadow ⇒ Object
Returns the value of attribute shadow.
6 7 8 |
# File 'lib/cyberarm_engine/text.rb', line 6 def shadow @shadow end |
#shadow_alpha ⇒ Object
Returns the value of attribute shadow_alpha.
6 7 8 |
# File 'lib/cyberarm_engine/text.rb', line 6 def shadow_alpha @shadow_alpha end |
#shadow_color ⇒ Object
Returns the value of attribute shadow_color.
6 7 8 |
# File 'lib/cyberarm_engine/text.rb', line 6 def shadow_color @shadow_color end |
#shadow_size ⇒ Object
Returns the value of attribute shadow_size.
6 7 8 |
# File 'lib/cyberarm_engine/text.rb', line 6 def shadow_size @shadow_size end |
#size ⇒ Object
Returns the value of attribute size.
5 6 7 |
# File 'lib/cyberarm_engine/text.rb', line 5 def size @size end |
#text ⇒ Object
Returns the value of attribute text.
6 7 8 |
# File 'lib/cyberarm_engine/text.rb', line 6 def text @text end |
#textobject ⇒ Object (readonly)
Returns the value of attribute textobject.
6 7 8 |
# File 'lib/cyberarm_engine/text.rb', line 6 def textobject @textobject end |
#x ⇒ Object
Returns the value of attribute x.
5 6 7 |
# File 'lib/cyberarm_engine/text.rb', line 5 def x @x end |
#y ⇒ Object
Returns the value of attribute y.
5 6 7 |
# File 'lib/cyberarm_engine/text.rb', line 5 def y @y end |
#z ⇒ Object
Returns the value of attribute z.
5 6 7 |
# File 'lib/cyberarm_engine/text.rb', line 5 def z @z end |
Instance Method Details
#alpha ⇒ Object
163 164 165 |
# File 'lib/cyberarm_engine/text.rb', line 163 def alpha @color.alpha end |
#alpha=(n) ⇒ Object
159 160 161 |
# File 'lib/cyberarm_engine/text.rb', line 159 def alpha=(n) @color = Gosu::Color.rgba(@color.red, @color.green, @color.blue, n) end |
#check_cache(size, font_name) ⇒ Object
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/cyberarm_engine/text.rb', line 44 def check_cache(size, font_name) available = false font = nil if CACHE[size] if CACHE[size][font_name] font = CACHE[size][font_name] available = true else available = false end else available = false end unless available font = Gosu::Font.new(@size, name: @font) CACHE[@size] = {} unless CACHE[@size].is_a?(Hash) CACHE[@size][@font] = font end font end |
#draw(method = :draw_markup) ⇒ Object
129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 |
# File 'lib/cyberarm_engine/text.rb', line 129 def draw(method = :draw_markup) if @shadow && !ARGV.join.include?("--no-shadow") shadow_alpha = @color.alpha <= 30 ? @color.alpha : @shadow_alpha shadow_color = @shadow_color || Gosu::Color.rgba(@color.red, @color.green, @color.blue, shadow_alpha) white = Gosu::Color::WHITE _x = @shadow_size _y = @shadow_size @rendered_shadow ||= Gosu.render((width + (shadow_size * 2)).ceil, (height + (@shadow_size * 2)).ceil) do @textobject.send(method, @text, _x - @shadow_size, _y, @z, @factor_x, @factor_y, white, :add) @textobject.send(method, @text, _x - @shadow_size, _y - @shadow_size, @z, @factor_x, @factor_y, white, :add) @textobject.send(method, @text, _x, _y - @shadow_size, @z, @factor_x, @factor_y, white, :add) @textobject.send(method, @text, _x + @shadow_size, _y - @shadow_size, @z, @factor_x, @factor_y, white, :add) @textobject.send(method, @text, _x, _y + @shadow_size, @z, @factor_x, @factor_y, white, :add) @textobject.send(method, @text, _x - @shadow_size, _y + @shadow_size, @z, @factor_x, @factor_y, white, :add) @textobject.send(method, @text, _x + @shadow_size, _y, @z, @factor_x, @factor_y, white, :add) @textobject.send(method, @text, _x + @shadow_size, _y + @shadow_size, @z, @factor_x, @factor_y, white, :add) end @rendered_shadow.draw(@x - @shadow_size, @y - @shadow_size, @z, @factor_x, @factor_y, shadow_color) end @textobject.send(method, @text, @x, @y, @z, @factor_x, @factor_y, @color, @mode) end |
#height(text = @text) ⇒ Object
125 126 127 |
# File 'lib/cyberarm_engine/text.rb', line 125 def height(text = @text) text.lines.count > 0 ? text.lines.count * textobject.height : @textobject.height end |
#markup_width(text = @text) ⇒ Object
121 122 123 |
# File 'lib/cyberarm_engine/text.rb', line 121 def markup_width(text = @text) textobject.markup_width(text) end |
#swap_font(size, font_name = @font) ⇒ Object
68 69 70 71 72 73 74 75 |
# File 'lib/cyberarm_engine/text.rb', line 68 def swap_font(size, font_name = @font) if @size != size || @font != font_name @size = size @font = font_name @textobject = check_cache(size, font_name) end end |
#update ⇒ Object
167 168 |
# File 'lib/cyberarm_engine/text.rb', line 167 def update end |
#width(text = @text) ⇒ Object
117 118 119 |
# File 'lib/cyberarm_engine/text.rb', line 117 def width(text = @text) textobject.text_width(text) end |