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.



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, 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
  @color    = options[:color]     || Gosu::Color::WHITE
  @mode     = options[:mode]      || :default
  @alignment = options[:alignment] || nil
  @shadow   = true  if options[:shadow] == true
  @shadow   = false if options[:shadow] == false
  @shadow   = true if options[:shadow].nil?
  @shadow_size = options[:shadow_size] || 1
  @shadow_alpha = options[:shadow_alpha] || 30
  @shadow_alpha = options[:shadow_alpha] || 30
  @shadow_color = options[: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

#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

Returns the value of attribute shadow.



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

def shadow
  @shadow
end

#shadow_alphaObject

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

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

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



153
154
155
# File 'lib/cyberarm_engine/text.rb', line 153

def alpha
  @color.alpha
end

#alpha=(n) ⇒ Object



149
150
151
# File 'lib/cyberarm_engine/text.rb', line 149

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



120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# File 'lib/cyberarm_engine/text.rb', line 120

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



116
117
118
# File 'lib/cyberarm_engine/text.rb', line 116

def height(text = @text)
  text.lines.count > 0 ? text.lines.count * textobject.height : @textobject.height
end

#markup_width(text = @text) ⇒ Object



112
113
114
# File 'lib/cyberarm_engine/text.rb', line 112

def markup_width(text = @text)
  textobject.markup_width(text)
end

#updateObject



157
158
# File 'lib/cyberarm_engine/text.rb', line 157

def update
end

#width(text = @text) ⇒ Object



108
109
110
# File 'lib/cyberarm_engine/text.rb', line 108

def width(text = @text)
  textobject.text_width(text)
end