Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

added FontStyle and WrappedScreen#print, using that in Label behavior

  • Loading branch information...
commit 03a9b6a2a958656d5044ab17fb096461920cd4eb 1 parent 699e9f1
@karlin karlin authored
View
1  .gitignore
@@ -12,3 +12,4 @@ tags
ctags
.bundle
*.lock
+vendor
View
29 lib/gamebox/actors/label.rb
@@ -4,30 +4,38 @@
setup do
# will define attributes and set their values if no one else has
- actor.has_attributes text: "",
+ actor.has_attributes text: "",
+ font_size: 30,
font_name: "Asimov.ttf",
color: [250,250,250,255],
- font_size: 30,
width: 0,
height: 0,
layer: 1
- font = resource_manager.load_font actor.font_name, actor.font_size
- actor.has_attributes font: font
+
+ font_style = FontStyle.new resource_manager, actor.font_name, actor.font_size, actor.color
+ actor.has_attributes font_style: font_style
actor.when :font_size_changed do
- actor.font = resource_manager.load_font actor.font_name, actor.font_size
+ actor.font_style.reload
+ recalculate_size
end
actor.when :font_name_changed do
- actor.font = resource_manager.load_font actor.font_name, actor.font_size
+ actor.font_style.reload
+ recalculate_size
end
actor.when :text_changed do
- actor.width = actor.font.text_width actor.text
- actor.height = [actor.width, font.height]
+ recalculate_size
end
end
+ helpers do
+ def recalculate_size
+ actor.width = actor.font_style.calc_width actor.text
+ actor.height = actor.font_style.height
+ end
+ end
end
Actor.define :label do
@@ -36,10 +44,7 @@
view do
draw do |target,x_off,y_off,z|
- @converted_color ||= target.convert_color(actor.color)
- actor.font.draw actor.text, actor.x, actor.y, z,
- 1,1, # x factor, y factor
- @converted_color
+ target.print actor.text, actor.x, actor.y, z, actor.font_style
end
end
View
26 lib/gamebox/core/font_style.rb
@@ -0,0 +1,26 @@
+
+class FontStyle
+ attr_accessor :font, :name, :size, :color, :x_scale, :y_scale
+
+ def initialize(resource_manager, name, size, color, x_scale=1, y_scale=1)
+ @name = name
+ @size = size
+ @color = color
+ @x_scale = x_scale
+ @y_scale = y_scale
+ @resource_manager = resource_manager
+ reload
+ end
+
+ def calc_width(text)
+ @font.text_width text
+ end
+
+ def height
+ @font.height
+ end
+
+ def reload
+ @font = @resource_manager.load_font name, size
+ end
+end
View
17 lib/gamebox/core/wrapped_screen.rb
@@ -118,19 +118,8 @@ def convert_color(color)
c
end
- def size_text(text, font_file, font_size)
- @font_cache ||= {}
- @font_cache[font_file] ||= {}
- font = @font_cache[font_file][font_size] ||= Font.new(@screen, font_file, font_size)
-
- return [font.text_width(text),font.height]
- end
-
- def render_text(text, font_file, font_size, color)
- @font_cache ||= {}
- @font_cache[font_file] ||= {}
- font = @font_cache[font_file][font_size] ||= Font.new(@screen, font_file, font_size)
-
- DelayedText.new font, text
+ def print(text, x, y, z, font_style)
+ font_style.font.draw text, x, y, z, font_style.x_scale, font_style.y_scale, convert_color(font_style.color)
end
end
+
View
44 spec/core/font_style_spec.rb
@@ -0,0 +1,44 @@
+require 'helper'
+
+describe FontStyle do
+ let(:resource_manager) { mock }
+ let(:font) { mock }
+
+ subject do
+ FontStyle.new resource_manager, "FooFace", 24, :aquamarine
+ end
+
+ before do
+ resource_manager.expects(:load_font).with("FooFace", 24).returns font
+ end
+
+ it 'constructs a font from style specifications' do
+ subject.name.should == "FooFace"
+ subject.size.should == 24
+ subject.color.should == :aquamarine
+ subject.x_scale.should == 1
+ subject.y_scale.should == 1
+ end
+
+ it 'calculates the width of the given text' do
+ font.expects(:text_width).with("Blah").returns 56
+
+ subject.calc_width("Blah").should == 56
+ end
+
+ it 'has a height' do
+ font.expects(:height).returns 42
+ subject.height.should == 42
+ end
+
+ it 'updates the font when the face or size change' do
+ subject.name = "Bob"
+ subject.size = 21
+ resource_manager.expects(:load_font).with("Bob", 21).returns :newfont
+
+ subject.reload
+ subject.font.should == :newfont
+ end
+end
+
+
Please sign in to comment.
Something went wrong with that request. Please try again.