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 BasicGameObject
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).
-
#update ⇒ Object
Update the background attributes if necessary unless specified.
-
#width ⇒ Object
Returns the width, in pixels, the given text would occupy if drawn.
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
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, = {}) if text.is_a? Hash = text text = nil end # We remove the :size param so it doesn't get to GameObject where it means something else @size = .delete(:size) || .delete(:height) || @@size || 15 = {:rotation_center => :top_left}.merge() super() @text = text || [:text] || "-No text specified-" @font = [:font] || @@font || Gosu::default_font_name() @line_spacing = [:line_spacing] || 1 @align = [:align] || :left @max_width = [:max_width] @padding = [:padding] || @@padding @gosu_font = Gosu::Font[@font, @size] create_image unless @image if [:background] @background = GameObject.new(:image => [:background]) bg_update @no_bg_update = [:no_bg_update] || false # always update background attribute 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
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 |
#update ⇒ Object
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 |
#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 |