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
# 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] || "sans-serif"#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
  @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] ? options[:shadow_size] : 1
  @shadow_alpha= options[:shadow_alpha] ? options[:shadow_alpha] : 30
  @shadow_alpha= options[: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

  return 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



140
141
142
# File 'lib/cyberarm_engine/text.rb', line 140

def alpha
  @color.alpha
end

#alpha=(n) ⇒ Object



136
137
138
# File 'lib/cyberarm_engine/text.rb', line 136

def alpha=(n)
  @color = Gosu::Color.rgba(@color.red, @color.green, @color.blue, n)
end

#check_cache(size, font_name) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/cyberarm_engine/text.rb', line 43

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

  return font
end

#drawObject



109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/cyberarm_engine/text.rb', line 109

def draw
  if @shadow && !ARGV.join.include?("--no-shadow")
    shadow_alpha = @color.alpha <= 30 ? @color.alpha : @shadow_alpha
    shadow_color = @shadow_color ? @shadow_color : Gosu::Color.rgba(@color.red, @color.green, @color.blue, shadow_alpha)

    _x = @shadow_size
    _y = @shadow_size

    @rendered_shadow ||= Gosu.render((self.width+(shadow_size*2)).ceil, (self.height+(@shadow_size*2)).ceil) do
      @textobject.draw_markup(@text, _x-@shadow_size, _y, @z)
      @textobject.draw_markup(@text, _x-@shadow_size, _y-@shadow_size, @z)

      @textobject.draw_markup(@text, _x, _y-@shadow_size, @z, @factor_x)
      @textobject.draw_markup(@text, _x+@shadow_size, _y-@shadow_size, @z)

      @textobject.draw_markup(@text, _x, _y+@shadow_size, @z)
      @textobject.draw_markup(@text, _x-@shadow_size, _y+@shadow_size, @z)

      @textobject.draw_markup(@text, _x+@shadow_size, _y, @z)
      @textobject.draw_markup(@text, _x+@shadow_size, _y+@shadow_size, @z)
    end
    @rendered_shadow.draw(@x-@shadow_size, @y-@shadow_size, @z, @factor_x, @factor_y, shadow_color)
  end

  @textobject.draw_markup(@text, @x, @y, @z, @factor_x, @factor_y, @color)
end

#heightObject



105
106
107
# File 'lib/cyberarm_engine/text.rb', line 105

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

#updateObject



144
# File 'lib/cyberarm_engine/text.rb', line 144

def update; end

#widthObject



101
102
103
# File 'lib/cyberarm_engine/text.rb', line 101

def width
  textobject.text_width(@text)
end