Class: CyberarmEngine::Text
- Inherits:
-
Object
- Object
- CyberarmEngine::Text
- Defined in:
- lib/cyberarm_engine/text.rb
Constant Summary collapse
- CACHE =
{}
Instance Attribute Summary collapse
-
#border ⇒ Object
Returns the value of attribute border.
-
#border_alpha ⇒ Object
Returns the value of attribute border_alpha.
-
#border_color ⇒ Object
Returns the value of attribute border_color.
-
#border_size ⇒ Object
Returns the value of attribute border_size.
-
#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
readonly
Returns the value of attribute shadow.
-
#shadow_alpha ⇒ Object
readonly
Returns the value of attribute shadow_alpha.
-
#shadow_color ⇒ Object
readonly
Returns the value of attribute shadow_color.
-
#shadow_size ⇒ Object
readonly
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.
- #invalidate_cache! ⇒ Object
- #markup_width(text = @text) ⇒ Object
- #swap_font(size, font_name = @font) ⇒ Object
- #text_width(text = @text) ⇒ Object
- #update ⇒ Object
- #width(text = @text) ⇒ Object
Constructor Details
#initialize(text, options = {}) ⇒ Text
Returns a new instance of Text.
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 43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/cyberarm_engine/text.rb', line 10 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 if [:color] @color = [:color].is_a?(Gosu::Color) ? [:color] : Gosu::Color.new([:color]) else @color = Gosu::Color::WHITE end @mode = [:mode] || :default @alignment = [:alignment] || nil @border = [:border] @border = true if [:border].nil? @border_size = [:border_size] || 1 @border_alpha = [:border_alpha] || 30 @border_color = [:border_color] || Gosu::Color::BLACK @shadow = [:shadow] @shadow_size = [:shadow_size] || 2 @shadow_alpha = [:shadow_alpha] || 30 @shadow_color = [:shadow_color] || Gosu::Color::BLACK @static = [:static] || ([:static].nil? || [:static] == false ? false : true) @textobject = check_cache(@size, @font) if @alignment case @alignment when :left @x = 0 + BUTTON_PADDING when :center @x = (CyberarmEngine::Window.instance.width / 2) - (@textobject.text_width(@text) / 2) when :right @x = CyberarmEngine::Window.instance.width - BUTTON_PADDING - @textobject.text_width(@text) end end end |
Instance Attribute Details
#border ⇒ Object
Returns the value of attribute border.
6 7 8 |
# File 'lib/cyberarm_engine/text.rb', line 6 def border @border end |
#border_alpha ⇒ Object
Returns the value of attribute border_alpha.
6 7 8 |
# File 'lib/cyberarm_engine/text.rb', line 6 def border_alpha @border_alpha end |
#border_color ⇒ Object
Returns the value of attribute border_color.
6 7 8 |
# File 'lib/cyberarm_engine/text.rb', line 6 def border_color @border_color end |
#border_size ⇒ Object
Returns the value of attribute border_size.
6 7 8 |
# File 'lib/cyberarm_engine/text.rb', line 6 def border_size @border_size end |
#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 (readonly)
Returns the value of attribute shadow.
6 7 8 |
# File 'lib/cyberarm_engine/text.rb', line 6 def shadow @shadow end |
#shadow_alpha ⇒ Object (readonly)
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 (readonly)
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 (readonly)
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
247 248 249 |
# File 'lib/cyberarm_engine/text.rb', line 247 def alpha @color.alpha end |
#alpha=(n) ⇒ Object
243 244 245 |
# File 'lib/cyberarm_engine/text.rb', line 243 def alpha=(n) @color = Gosu::Color.rgba(@color.red, @color.green, @color.blue, n) end |
#check_cache(size, font_name) ⇒ Object
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/cyberarm_engine/text.rb', line 55 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
174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 |
# File 'lib/cyberarm_engine/text.rb', line 174 def draw(method = :draw_markup) if @static if @border && !@cached_text_border_image _x = @border_size _y = @border_size _width = method == :draw_markup ? text_width : markup_width img = Gosu::Image.send(:"from_#{method.to_s.split("_").last}", @text, @size, font: @font) @cached_text_border_image = Gosu.render((_width + (@border_size * 2)).ceil, (height + (@border_size * 2)).ceil) do img.draw(-_x, 0, @z, @factor_x, @factor_y, @border_color, @mode) img.draw(-_x, -_y, @z, @factor_x, @factor_y, @border_color, @mode) img.draw(0, -_y, @z, @factor_x, @factor_y, @border_color, @mode) img.draw(_x, -_y, @z, @factor_x, @factor_y, @border_color, @mode) img.draw(_x, 0, @z, @factor_x, @factor_y, @border_color, @mode) img.draw(_x, _y, @z, @factor_x, @factor_y, @border_color, @mode) img.draw(0, _y, @z, @factor_x, @factor_y, @border_color, @mode) img.draw(-_x, _y, @z, @factor_x, @factor_y, @border_color, @mode) end end @cached_text_shadow_image ||= Gosu::Image.send(:"from_#{method.to_s.split("_").last}", @text, @size, font: @font) if @shadow @gosu_cached_text_image ||= Gosu::Image.send(:"from_#{method.to_s.split("_").last}", @text, @size, font: @font) @cached_text_border_image.draw(@x, @y, @z, @factor_x, @factor_y, @border_color, @mode) if @border @cached_text_shadow_image.draw(@x + @shadow_size, @y + @shadow_size, @z, @factor_x, @factor_y, @shadow_color, @mode) if @shadow @gosu_cached_text_image.draw(@x, @y, @z, @factor_x, @factor_y, @color, @mode) else if @border && !ARGV.join.include?("--no-border") border_alpha = @color.alpha <= 30 ? @color.alpha : @border_alpha border_color = @border_color || Gosu::Color.rgba(@color.red, @color.green, @color.blue, border_alpha) white = Gosu::Color::WHITE _x = @border_size _y = @border_size _width = method == :draw_markup ? text_width : markup_width @cached_text_border_image ||= Gosu.render((_width + (border_size * 2)).ceil, (height + (@border_size * 2)).ceil) do @textobject.send(method, @text, _x - @border_size, _y, @z, @factor_x, @factor_y, white, @mode) @textobject.send(method, @text, _x - @border_size, _y - @border_size, @z, @factor_x, @factor_y, white, @mode) @textobject.send(method, @text, _x, _y - @border_size, @z, @factor_x, @factor_y, white, @mode) @textobject.send(method, @text, _x + @border_size, _y - @border_size, @z, @factor_x, @factor_y, white, @mode) @textobject.send(method, @text, _x, _y + @border_size, @z, @factor_x, @factor_y, white, @mode) @textobject.send(method, @text, _x - @border_size, _y + @border_size, @z, @factor_x, @factor_y, white, @mode) @textobject.send(method, @text, _x + @border_size, _y, @z, @factor_x, @factor_y, white, @mode) @textobject.send(method, @text, _x + @border_size, _y + @border_size, @z, @factor_x, @factor_y, white, @mode) end @cached_text_border_image.draw(@x - @border_size, @y - @border_size, @z, @factor_x, @factor_y, border_color) end if @shadow shadow_color = @shadow_color || Gosu::Color.rgba(@color.red, @color.green, @color.blue, @shadow_alpha) @textobject.send(method, @text, @x + @shadow_size, @y + @shadow_size, @z, @factor_x, @factor_y, shadow_color, @mode) end @textobject.send(method, @text, @x, @y, @z, @factor_x, @factor_y, @color, @mode) end end |
#height(text = @text) ⇒ Object
166 167 168 169 170 171 172 |
# File 'lib/cyberarm_engine/text.rb', line 166 def height(text = @text) if text.lines.count > 0 text.lines.count * textobject.height + @border_size + @shadow_size else @textobject.height + @border_size + @shadow_size end end |
#invalidate_cache! ⇒ Object
254 255 256 257 258 |
# File 'lib/cyberarm_engine/text.rb', line 254 def invalidate_cache! @cached_text_border_image = nil @cached_text_shadow_image = nil @gosu_cached_text_image = nil end |
#markup_width(text = @text) ⇒ Object
152 153 154 155 156 157 158 159 160 161 162 163 164 |
# File 'lib/cyberarm_engine/text.rb', line 152 def markup_width(text = @text) text = text.to_s spacing = 0 spacing += @border_size if @border spacing += @shadow_size if @shadow if text == @text && @static && @gosu_cached_text_image @gosu_cached_text_image&.width + spacing else textobject.markup_width(text) + spacing end end |
#swap_font(size, font_name = @font) ⇒ Object
79 80 81 82 83 84 85 86 87 |
# File 'lib/cyberarm_engine/text.rb', line 79 def swap_font(size, font_name = @font) if @size != size || @font != font_name @size = size @font = font_name invalidate_cache! @textobject = check_cache(size, font_name) end end |
#text_width(text = @text) ⇒ Object
140 141 142 143 144 145 146 147 148 149 150 |
# File 'lib/cyberarm_engine/text.rb', line 140 def text_width(text = @text) spacing = 0 spacing += @border_size if @border spacing += @shadow_size if @shadow if text == @text && @static && @gosu_cached_text_image @gosu_cached_text_image&.width + spacing else textobject.text_width(text) + spacing end end |
#update ⇒ Object
251 252 |
# File 'lib/cyberarm_engine/text.rb', line 251 def update end |
#width(text = @text) ⇒ Object
136 137 138 |
# File 'lib/cyberarm_engine/text.rb', line 136 def width(text = @text) markup_width(text) end |