Class: Chingu::Text

Inherits:
GameObject show all
Defined in:
lib/chingu/text.rb

Overview

Text is a class to give the use of Gosu::Font more rubyish feel and fit it better into Chingu. Pure Gosu:

@font = Gosu::Font.new($window, "verdana", 30)
@font.draw("A Text", 200, 50, 55, 2.0)

Chingu

@text = Chingu::Text.new("A Text", :x => 200, :y => 50, :zorder => 55, :factor_x => 2.0)
@text.draw  # usually not needed as Text is a GameObject and therefore autodrawn

Constant Summary collapse

@@size =
nil
@@font =
nil
@@padding =
5

Instance Attribute Summary collapse

Attributes inherited from GameObject

#angle, #center, #center_x, #center_y, #color, #factor, #factor_x, #factor_y, #height, #image, #mode, #x, #y, #zorder

Attributes inherited from BasicGameObject

#options, #parent, #paused, #visible

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from GameObject

#alpha, #alpha=, #attributes, #attributes=, #distance_to, #draw_at, #draw_relative, #filename, #hide!, #inside_window?, #outside_window?, #show!, #size=, #visible?

Methods included from Helpers::RotationCenter

#rotation_center, #rotation_center=

Methods included from Helpers::InputClient

#holding?, #input, #input=

Methods inherited from BasicGameObject

all, create, #destroy, destroy_all, destroy_if, #draw_trait, initialize_trait, #pause!, #paused?, #setup, #setup_trait, trait, #trait_options, traits, #unpause!, #update, #update_trait

Methods included from Helpers::ClassInheritableAccessor

included

Constructor Details

#initialize(text, options = {}) ⇒ Text

Takes the standard GameObject-hash-arguments but also:

:text               - a string of text
:font_name|:font    - Name of a system font, or a filename to a TTF file (must contain ? does not work on Linux). 
:height|:size       - Height of the font in pixels. 
:line_spacing       - Spacing between two lines of text in pixels. 
:max_width          - Width of the bitmap that will be returned. Text will be split into multiple lines to avoid drawing over the right border. When a single word is too long, it will be truncated.
:align              - One of :left, :right, :center or :justify.

if :max_width is given the text is drawn using :line_spacing, :align and :max_width



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/chingu/text.rb', line 62

def initialize(text, options = {})      
  if text.is_a? Hash
    options = text
    text = nil
  end
 
  super(options)
  
  @text = text || options[:text] || "-No text specified-"
  @font =  options[:font] || @@font || Gosu::default_font_name()
  @size = options[:size] || options[:height] || @@size || 15
  @line_spacing = options[:line_spacing] || 1
  @align = options[:align] || :left
  @max_width = options[:max_width]
  @padding = options[:padding] || @@padding
  self.rotation_center = :top_left

  @gosu_font = Gosu::Font.new($window, @font, @size)
  create_image  unless @image

  if options[:background]
    @background = GameObject.new(:image => options[:background])
    @background.attributes = self.attributes
    @background.color = Color::WHITE
    @background.zorder -= 1
    @background.x -= @padding
    @background.y -= @padding
    @background.width = self.width + @padding * 2
    @background.height = self.height + @padding * 2
  end
  
  self.height = options[:height]  if options[:height]
end

Instance Attribute Details

#alignObject (readonly)

Returns the value of attribute align.



37
38
39
# File 'lib/chingu/text.rb', line 37

def align
  @align
end

#backgroundObject (readonly)

Returns the value of attribute background.



37
38
39
# File 'lib/chingu/text.rb', line 37

def background
  @background
end

#gosu_fontObject (readonly)

Returns the value of attribute gosu_font.



37
38
39
# File 'lib/chingu/text.rb', line 37

def gosu_font
  @gosu_font
end

#line_spacingObject (readonly)

Returns the value of attribute line_spacing.



37
38
39
# File 'lib/chingu/text.rb', line 37

def line_spacing
  @line_spacing
end

#max_widthObject (readonly)

Returns the value of attribute max_width.



37
38
39
# File 'lib/chingu/text.rb', line 37

def max_width
  @max_width
end

#sizeObject (readonly)

Returns the value of attribute size.



37
38
39
# File 'lib/chingu/text.rb', line 37

def size
  @size
end

#textObject

Returns the value of attribute text.



36
37
38
# File 'lib/chingu/text.rb', line 36

def text
  @text
end

Class Method Details

.fontObject



42
# File 'lib/chingu/text.rb', line 42

def self.font; @@font; end

.font=(value) ⇒ Object



43
# File 'lib/chingu/text.rb', line 43

def self.font=(value); @@font = value; end

.heightObject



46
# File 'lib/chingu/text.rb', line 46

def self.height; @@size; end

.height=(value) ⇒ Object



47
# File 'lib/chingu/text.rb', line 47

def self.height=(value); @@size = value; end

.paddingObject



48
# File 'lib/chingu/text.rb', line 48

def self.padding; @@padding; end

.padding=(value) ⇒ Object



49
# File 'lib/chingu/text.rb', line 49

def self.padding=(value); @@padding = value; end

.sizeObject



44
# File 'lib/chingu/text.rb', line 44

def self.size; @@size; end

.size=(value) ⇒ Object



45
# File 'lib/chingu/text.rb', line 45

def self.size=(value); @@size = value; end

Instance Method Details

#drawObject

Draws @background if present + our text in @image



114
115
116
117
# File 'lib/chingu/text.rb', line 114

def draw
  @background.draw  if @background    # draw our background, if any

  super                               # super -> GameObject#draw which draws out text in form of @image

end

#widthObject

Returns the width, in pixels, the given text would occupy if drawn.



107
108
109
# File 'lib/chingu/text.rb', line 107

def width
  @gosu_font.text_width(@text, @factor_x)
end