Class: CyberarmEngine::Text

Inherits:
Object
  • Object
show all
Defined in:
lib/cyberarm_engine/text.rb

Constant Summary collapse

CACHE =
{}

Instance Attribute Summary collapse

Instance Method Summary collapse

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, options = {})
  @text = text.to_s || ""
  @options = options
  @size = options[:size] || 18
  @font = options[:font] || Gosu.default_font_name
  @x = options[:x] || 0
  @y = options[:y] || 0
  @z = options[:z] || 1025
  @factor_x = options[:factor_x]  || 1
  @factor_y = options[:factor_y]  || 1
  if options[:color]
    @color = options[:color].is_a?(Gosu::Color) ? options[:color] : Gosu::Color.new(options[:color])
  else
    @color = Gosu::Color::WHITE
  end
  @mode      = options[:mode]      || :default
  @alignment = options[:alignment] || nil

  @border   = options[:border]
  @border   = true if options[:border].nil?
  @border_size = options[:border_size] || 1
  @border_alpha = options[:border_alpha] || 30
  @border_color = options[:border_color] || Gosu::Color::BLACK

  @shadow = options[:shadow]
  @shadow_size = options[:shadow_size] || 2
  @shadow_alpha = options[:shadow_alpha] || 30
  @shadow_color = options[:shadow_color] || Gosu::Color::BLACK

  @static = options[:static] || (options[:static].nil? || options[: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

#borderObject

Returns the value of attribute border.



6
7
8
# File 'lib/cyberarm_engine/text.rb', line 6

def border
  @border
end

#border_alphaObject

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_colorObject

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_sizeObject

Returns the value of attribute border_size.



6
7
8
# File 'lib/cyberarm_engine/text.rb', line 6

def border_size
  @border_size
end

#colorObject

Returns the value of attribute color.



6
7
8
# File 'lib/cyberarm_engine/text.rb', line 6

def color
  @color
end

#factor_xObject

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_yObject

Returns the value of attribute factor_y.



6
7
8
# File 'lib/cyberarm_engine/text.rb', line 6

def factor_y
  @factor_y
end

#optionsObject

Returns the value of attribute options.



5
6
7
# File 'lib/cyberarm_engine/text.rb', line 5

def options
  @options
end

#shadowObject (readonly)

Returns the value of attribute shadow.



6
7
8
# File 'lib/cyberarm_engine/text.rb', line 6

def shadow
  @shadow
end

#shadow_alphaObject (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_colorObject (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_sizeObject (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

#sizeObject

Returns the value of attribute size.



5
6
7
# File 'lib/cyberarm_engine/text.rb', line 5

def size
  @size
end

#textObject

Returns the value of attribute text.



6
7
8
# File 'lib/cyberarm_engine/text.rb', line 6

def text
  @text
end

#textobjectObject (readonly)

Returns the value of attribute textobject.



6
7
8
# File 'lib/cyberarm_engine/text.rb', line 6

def textobject
  @textobject
end

#xObject

Returns the value of attribute x.



5
6
7
# File 'lib/cyberarm_engine/text.rb', line 5

def x
  @x
end

#yObject

Returns the value of attribute y.



5
6
7
# File 'lib/cyberarm_engine/text.rb', line 5

def y
  @y
end

#zObject

Returns the value of attribute z.



5
6
7
# File 'lib/cyberarm_engine/text.rb', line 5

def z
  @z
end

Instance Method Details

#alphaObject



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

#updateObject



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