Class: Chingu::Text
- Inherits:
-
GameObject
- Object
- BasicGameObject
- GameObject
- Chingu::Text
- 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
-
#align ⇒ Object
readonly
Returns the value of attribute align.
-
#background ⇒ Object
readonly
Returns the value of attribute background.
-
#gosu_font ⇒ Object
readonly
Returns the value of attribute gosu_font.
-
#line_spacing ⇒ Object
readonly
Returns the value of attribute line_spacing.
-
#max_width ⇒ Object
readonly
Returns the value of attribute max_width.
-
#size ⇒ Object
readonly
Returns the value of attribute size.
-
#text ⇒ Object
Returns the value of attribute text.
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
- .font ⇒ Object
- .font=(value) ⇒ Object
- .height ⇒ Object
- .height=(value) ⇒ Object
- .padding ⇒ Object
- .padding=(value) ⇒ Object
- .size ⇒ Object
- .size=(value) ⇒ Object
Instance Method Summary collapse
-
#draw ⇒ Object
Draws @background if present + our text in @image.
-
#initialize(text, options = {}) ⇒ Text
constructor
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).
-
#width ⇒ Object
Returns the width, in pixels, the given text would occupy if drawn.
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
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
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, = {}) if text.is_a? Hash = text text = nil end super() @text = text || [:text] || "-No text specified-" @font = [:font] || @@font || Gosu::default_font_name() @size = [:size] || [:height] || @@size || 15 @line_spacing = [:line_spacing] || 1 @align = [:align] || :left @max_width = [:max_width] @padding = [:padding] || @@padding self.rotation_center = :top_left @gosu_font = Gosu::Font.new($window, @font, @size) create_image unless @image if [:background] @background = GameObject.new(:image => [: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 = [:height] if [:height] end |
Instance Attribute Details
#align ⇒ Object (readonly)
Returns the value of attribute align.
37 38 39 |
# File 'lib/chingu/text.rb', line 37 def align @align end |
#background ⇒ Object (readonly)
Returns the value of attribute background.
37 38 39 |
# File 'lib/chingu/text.rb', line 37 def background @background end |
#gosu_font ⇒ Object (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_spacing ⇒ Object (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_width ⇒ Object (readonly)
Returns the value of attribute max_width.
37 38 39 |
# File 'lib/chingu/text.rb', line 37 def max_width @max_width end |
#size ⇒ Object (readonly)
Returns the value of attribute size.
37 38 39 |
# File 'lib/chingu/text.rb', line 37 def size @size end |
#text ⇒ Object
Returns the value of attribute text.
36 37 38 |
# File 'lib/chingu/text.rb', line 36 def text @text end |
Class Method Details
.font ⇒ Object
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 |
.height ⇒ Object
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 |
.padding ⇒ Object
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 |
.size ⇒ Object
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
#draw ⇒ Object
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 |
#width ⇒ Object
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 |