summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorTom Black <[email protected]>2018-10-05 22:44:13 -0700
committerTom Black <[email protected]>2018-10-08 17:34:17 -0700
commit4a4df9831003e0c9f51278b968a78d1900b9a3d9 (patch)
tree9c3ea4774243a48e07ee75f18da77b2ba1c1bf6f
parent13821e171daa52e2b9c4d35320eee0492b727bb7 (diff)
downloadruby2d-4a4df9831003e0c9f51278b968a78d1900b9a3d9.tar.gz
ruby2d-4a4df9831003e0c9f51278b968a78d1900b9a3d9.zip
Reorganize Image, Sprite, and Text args
Start with the required argument first as positional, followed by optional ones as keyword arguments Co-Authored-By: Andrew Havens <[email protected]>
-rw-r--r--lib/ruby2d/image.rb4
-rw-r--r--lib/ruby2d/sprite.rb2
-rw-r--r--lib/ruby2d/text.rb4
-rw-r--r--test/circle_spec.rb2
-rw-r--r--test/contains.rb4
-rw-r--r--test/controller.rb2
-rw-r--r--test/image_spec.rb6
-rw-r--r--test/testcard.rb30
-rw-r--r--test/text_spec.rb20
9 files changed, 37 insertions, 37 deletions
diff --git a/lib/ruby2d/image.rb b/lib/ruby2d/image.rb
index 9386f6b..5a57953 100644
--- a/lib/ruby2d/image.rb
+++ b/lib/ruby2d/image.rb
@@ -7,8 +7,8 @@ module Ruby2D
attr_reader :path, :color
attr_accessor :x, :y, :width, :height, :rotate, :data
- def initialize(opts = {})
- @path = opts[:path]
+ def initialize(path, opts = {})
+ @path = path
unless RUBY_ENGINE == 'opal'
unless File.exists? @path
diff --git a/lib/ruby2d/sprite.rb b/lib/ruby2d/sprite.rb
index a00617f..35c3c90 100644
--- a/lib/ruby2d/sprite.rb
+++ b/lib/ruby2d/sprite.rb
@@ -4,7 +4,7 @@ module Ruby2D
class Sprite
include Renderable
- attr_reader :x, :y, :width, :height
+ attr_reader :x, :y, :width, :height
attr_accessor :rotate, :loop, :clip_x, :clip_y, :clip_width, :clip_height, :data
def initialize(path, opts = {})
diff --git a/lib/ruby2d/text.rb b/lib/ruby2d/text.rb
index a8df8b5..5f942cd 100644
--- a/lib/ruby2d/text.rb
+++ b/lib/ruby2d/text.rb
@@ -7,11 +7,11 @@ module Ruby2D
attr_reader :text, :size, :width, :height, :font, :color
attr_accessor :x, :y, :rotate, :data
- def initialize(opts = {})
+ def initialize(text, opts = {})
@x = opts[:x] || 0
@y = opts[:y] || 0
@z = opts[:z] || 0
- @text = (opts[:text] || "Hello Ruby!").to_s
+ @text = text.to_s
@size = opts[:size] || 20
@rotate = opts[:rotate] || 0
@font = opts[:font] || Font.default
diff --git a/test/circle_spec.rb b/test/circle_spec.rb
index dd49e2c..0c88b7f 100644
--- a/test/circle_spec.rb
+++ b/test/circle_spec.rb
@@ -5,7 +5,7 @@ RSpec.describe Ruby2D::Circle do
describe "#new" do
it "creates a white circle by default" do
circle = Circle.new
- expect(circle.color).to be_a(Ruby2D::Color)
+ expect(circle.color).to be_a(Ruby2D::Color)
expect(circle.color.r).to eq(1)
expect(circle.color.g).to eq(1)
expect(circle.color.b).to eq(1)
diff --git a/test/contains.rb b/test/contains.rb
index c5c6686..b24b898 100644
--- a/test/contains.rb
+++ b/test/contains.rb
@@ -17,8 +17,8 @@ objects.push Quad.new(x1: 350, y1: 50, x2: 500, y2: 75, x3: 450, y3: 150, x4: 37
objects.push Triangle.new(x1: 550, y1: 50, x2: 600, y2: 125, x3: 500, y3: 150)
objects.push Line.new(x1: 225, y1: 175, x2: 375, y2: 225, width: 20)
objects.push Circle.new(x: 225, y: 275, radius: 50)
-objects.push Image.new(x: 50, y: 200, path: "#{media}/colors.png")
-objects.push Text.new(x: 450, y: 200, text: "Hello", size: 50, font: font)
+objects.push Image.new("#{media}/colors.png", x: 50, y: 200)
+objects.push Text.new("Hello", x: 450, y: 200, size: 50, font: font)
on :key_down do |event|
close if event.key == 'escape'
diff --git a/test/controller.rb b/test/controller.rb
index 5035ebb..b6e3348 100644
--- a/test/controller.rb
+++ b/test/controller.rb
@@ -4,7 +4,7 @@ set title: "Ruby 2D — Controller", width: 600, height: 425
set diagnostics: true
# Controller outline image
-controller = Image.new(path: 'media/controller.png')
+controller = Image.new('media/controller.png')
scale = 80
diff --git a/test/image_spec.rb b/test/image_spec.rb
index f175a01..e2ef37e 100644
--- a/test/image_spec.rb
+++ b/test/image_spec.rb
@@ -4,18 +4,18 @@ RSpec.describe Ruby2D::Image do
describe "#new" do
it "raises exception if image file doesn't exist" do
- expect { Image.new(path: "bad_image.png") }.to raise_error(Ruby2D::Error)
+ expect { Image.new("bad_image.png") }.to raise_error(Ruby2D::Error)
end
end
describe "#contains?" do
it "returns true if point is inside the image" do
- image = Image.new(path: "test/media/image.bmp")
+ image = Image.new("test/media/image.bmp")
expect(image.contains?(50, 50)).to be true
end
it "returns true if point is outside the image" do
- image = Image.new(path: "test/media/image.bmp")
+ image = Image.new("test/media/image.bmp")
expect(image.contains?(-50, 50)).to be false
expect(image.contains?(50, -50)).to be false
expect(image.contains?(50, 150)).to be false
diff --git a/test/testcard.rb b/test/testcard.rb
index 2b182cf..21b627b 100644
--- a/test/testcard.rb
+++ b/test/testcard.rb
@@ -206,25 +206,25 @@ Circle.new(x: 575, y: 225, radius: 17, sectors: 16, color: [0, 0, 0, 0.6])
rotate = false
# Images
-img_png = Image.new(x: 600, y: 180, path: "#{media}/image.png")
-img_jpg = Image.new(x: 600, y: 290, path: "#{media}/image.jpg")
-img_bmp = Image.new(x: 600, y: 400, path: "#{media}/image.bmp")
-img_r = Image.new(x: 400, y: 200, width: 50, height: 25, path: "#{media}/colors.png")
+img_png = Image.new("#{media}/image.png", x: 600, y: 180)
+img_jpg = Image.new("#{media}/image.jpg", x: 600, y: 290)
+img_bmp = Image.new("#{media}/image.bmp", x: 600, y: 400)
+img_r = Image.new("#{media}/colors.png", x: 400, y: 200, width: 50, height: 25)
img_r.color = [1.0, 0.3, 0.3, 1.0]
-img_g = Image.new(x: 400, y: 225, path: "#{media}/colors.png")
+img_g = Image.new("#{media}/colors.png", x: 400, y: 225)
img_g.width, img_g.height = 25, 25
img_g.color = [0.3, 1.0, 0.3, 1.0]
-img_b = Image.new(x: 425, y: 225, path: "#{media}/colors.png")
+img_b = Image.new("#{media}/colors.png", x: 425, y: 225)
img_b.width, img_b.height = 25, 25
img_b.color = [0.3, 0.3, 1.0, 1.0]
# Text
-txt_r = Text.new(x: 44, y: 202, text: "R", font: font, color: [1.0, 0.0, 0.0, 1.0])
-txt_b = Text.new(x: 92, y: 202, text: "G", font: font, color: [0.0, 1.0, 0.0, 1.0])
-txt_g = Text.new(x: 144, y: 202, text: "B", font: font, color: [0.0, 0.0, 1.0, 1.0])
+txt_r = Text.new("R", x: 44, y: 202, font: font, color: [1.0, 0.0, 0.0, 1.0])
+txt_b = Text.new("G", x: 92, y: 202, font: font, color: [0.0, 1.0, 0.0, 1.0])
+txt_g = Text.new("B", x: 144, y: 202, font: font, color: [0.0, 0.0, 1.0, 1.0])
# Frames per second
-fps = Text.new(x: 10, y: 470, text: "", font: font)
+fps = Text.new("", x: 10, y: 470, font: font)
# Sprites
spr = Sprite.new(
@@ -244,24 +244,24 @@ flash = 0
time_start = Time.now
# Default font for text
-Text.new(x: 150, y: 470, text: "Default font", size: 20)
+Text.new("Default font", x: 150, y: 470, size: 20)
# Text size
-created_text = Text.new(x: 10, y: 270, text: "Created text", font: font)
+created_text = Text.new("Created text", x: 10, y: 270, font: font)
created_text_background = Rectangle.new(
x: created_text.x - 10,
y: created_text.y - 10,
width: created_text.width + 20,
height: created_text.height + 20,
- color: "red"
+ color: 'red'
)
created_text.remove
created_text.add
updated_text = Text.new(
+ "Updated text",
x: 20 + created_text_background.x2,
y: 270,
- text: "Updated text",
font: font
)
updated_text_background = Rectangle.new(
@@ -269,7 +269,7 @@ updated_text_background = Rectangle.new(
y: updated_text.y - 10,
width: updated_text.width + 20,
height: updated_text.height + 20,
- color: "blue"
+ color: 'blue'
)
updated_text.remove
updated_text.add
diff --git a/test/text_spec.rb b/test/text_spec.rb
index 67afe0e..63acec7 100644
--- a/test/text_spec.rb
+++ b/test/text_spec.rb
@@ -4,24 +4,24 @@ RSpec.describe Ruby2D::Text do
describe "#new" do
it "raises exception if font file doesn't exist" do
- expect { Text.new(font: "bad_font.ttf") }.to raise_error(Ruby2D::Error)
+ expect { Text.new("hello", font: "bad_font.ttf") }.to raise_error(Ruby2D::Error)
end
it "uses the system default font if one is not provided" do
- t = Text.new
+ t = Text.new("hello")
expect(t.font).to eq(Font.default)
end
end
describe "#text=" do
it "maps Time to string" do
- t = Text.new(font: "test/media/bitstream_vera/vera.ttf")
+ t = Text.new("hello", font: "test/media/bitstream_vera/vera.ttf")
t.text = Time.new(1, 1, 1, 1, 1, 1, 1)
expect(t.text).to eq("0001-01-01 01:01:01 +0000")
end
it "maps Number to string" do
- t = Text.new(font: "test/media/bitstream_vera/vera.ttf")
+ t = Text.new("hello", font: "test/media/bitstream_vera/vera.ttf")
t.text = 0
expect(t.text).to eq("0")
end
@@ -29,12 +29,12 @@ RSpec.describe Ruby2D::Text do
describe "#width" do
it "is known after creation" do
- t = Text.new(font: "test/media/bitstream_vera/vera.ttf")
+ t = Text.new("Hello Ruby!", font: "test/media/bitstream_vera/vera.ttf")
expect(t.width).to be_between(110, 120)
end
it "is known after updating" do
- t = Text.new(font: "test/media/bitstream_vera/vera.ttf")
+ t = Text.new("hello", font: "test/media/bitstream_vera/vera.ttf")
t.text = "Hello!"
expect(t.width).to eq(59)
end
@@ -42,12 +42,12 @@ RSpec.describe Ruby2D::Text do
describe "#height" do
it "is known after creation" do
- t = Text.new(font: "test/media/bitstream_vera/vera.ttf")
+ t = Text.new("hello", font: "test/media/bitstream_vera/vera.ttf")
expect(t.height).to eq(24)
end
it "is known after updating" do
- t = Text.new(font: "test/media/bitstream_vera/vera.ttf")
+ t = Text.new("hello", font: "test/media/bitstream_vera/vera.ttf")
t.text = "Good morning world!"
expect(t.height).to eq(24)
end
@@ -55,13 +55,13 @@ RSpec.describe Ruby2D::Text do
describe "#contains?" do
it "returns true if point is inside the text" do
- t = Text.new(font: "test/media/bitstream_vera/vera.ttf")
+ t = Text.new("hello", font: "test/media/bitstream_vera/vera.ttf")
t.text = "Hello world!"
expect(t.contains?(t.width / 2, t.height / 2)).to be true
end
it "returns false if point is outside the text" do
- t = Text.new(font: "test/media/bitstream_vera/vera.ttf")
+ t = Text.new("hello", font: "test/media/bitstream_vera/vera.ttf")
t.text = "Hello world!"
expect(t.contains?( - t.width / 2, t.height / 2)).to be false
expect(t.contains?( t.width / 2, - t.height / 2)).to be false