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

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

Instance Attribute Summary collapse

Attributes inherited from BasicGameObject

#options, #parent, #paused

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Helpers::InputClient

#add_inputs, #holding?, #holding_all?, #holding_any?, #input, #input=, #on_input

Methods inherited from BasicGameObject

all, create, #destroy, destroy_all, destroy_if, #draw_trait, each, each_with_index, #filename, initialize_trait, #pause!, #paused?, select, #setup, #setup_trait, trait, #trait_options, traits, #unpause!, #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
# File 'lib/chingu/text.rb', line 62

def initialize(text, options = {})      
  if text.is_a? Hash
    options = text
    text = nil
  end
 
  # We remove the :size param so it doesn't get to GameObject where it means something else
  @size = options.delete(:size) || options.delete(:height) || @@size || 15
  
  options = {:rotation_center => :top_left}.merge(options) 
  
  super(options)
    
  @text = text || options[:text] || "-No text specified-"
  @font =  options[:font] || @@font || Gosu::default_font_name()
  @line_spacing = options[:line_spacing] || 1
  @align = options[:align] || :left
  @max_width = options[:max_width]
  @padding = options[:padding] || @@padding

  @gosu_font = Gosu::Font[@font, @size]
  create_image  unless @image

  if options[:background]
    @background = GameObject.new(:image => options[:background])
    bg_update
    @no_bg_update = options[:no_bg_update] || false # always update background attribute
  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



122
123
124
125
# File 'lib/chingu/text.rb', line 122

def draw
  @background.draw  if @background    # draw our background, if any
  super                               # super -> GameObject#draw which draws out text in form of @image
end

#updateObject

Update the background attributes if necessary unless specified



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

def update
  super
  bg_update if @background and !@no_bg_update
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