gosling 2.3.0 → 2.3.2

Sign up to get free protection for your applications and to get access to all the features.
data/spec/rect_spec.rb CHANGED
@@ -1,87 +1,87 @@
1
- describe Gosling::Rect do
2
- before(:all) do
3
- @window = Gosu::Window.new(640, 480, false)
4
- @rect = Gosling::Rect.new(@window)
5
- end
6
-
7
- it 'starts with width and height 1' do
8
- expect(@rect.width).to be == 1
9
- expect(@rect.height).to be == 1
10
- end
11
-
12
- describe '#width' do
13
- it 'returns our width' do
14
- expect(@rect.width).to be_kind_of(Numeric)
15
- end
16
- end
17
-
18
- describe '#set_width' do
19
- it 'alters our width, updating our vertices' do
20
- rect = Gosling::Rect.new(@window)
21
- rect.width = 10
22
- expect(rect.width).to be == 10
23
- end
24
-
25
- it 'cannot be less than one' do
26
- rect = Gosling::Rect.new(@window)
27
- expect { rect.width = 0 }.to raise_error(ArgumentError)
28
- expect { rect.width = -100 }.to raise_error(ArgumentError)
29
- end
30
-
31
- it 'updates our vertices' do
32
- expected_vertices = [
33
- Snow::Vec3[ 0, 0, 0],
34
- Snow::Vec3[11, 0, 0],
35
- Snow::Vec3[11, 1, 0],
36
- Snow::Vec3[ 0, 1, 0]
37
- ]
38
-
39
- rect = Gosling::Rect.new(@window)
40
- rect.width = 11
41
- expect(rect.get_vertices).to be == expected_vertices
42
- end
43
- end
44
-
45
- describe '#height' do
46
- it 'returns our height' do
47
- expect(@rect.height).to be_kind_of(Numeric)
48
- end
49
- end
50
-
51
- describe '#set_height' do
52
- it 'alters our height, updating our vertices' do
53
- rect = Gosling::Rect.new(@window)
54
- rect.height = 15
55
- expect(rect.height).to be == 15
56
- end
57
-
58
- it 'cannot be less than one' do
59
- rect = Gosling::Rect.new(@window)
60
- expect { rect.height = 0 }.to raise_error(ArgumentError)
61
- expect { rect.height = -200 }.to raise_error(ArgumentError)
62
- end
63
-
64
- it 'updates our vertices' do
65
- expected_vertices = [
66
- Snow::Vec3[0, 0, 0],
67
- Snow::Vec3[1, 0, 0],
68
- Snow::Vec3[1, 40, 0],
69
- Snow::Vec3[0, 40, 0]
70
- ]
71
-
72
- rect = Gosling::Rect.new(@window)
73
- rect.height = 40
74
- expect(rect.get_vertices).to be == expected_vertices
75
- end
76
- end
77
-
78
- it 'does not allow set_vertices to be called directly' do
79
- vertices = [
80
- Snow::Vec3[0, 0, 0],
81
- Snow::Vec3[1, 10, 0],
82
- Snow::Vec3[2, 20, 0],
83
- Snow::Vec3[3, 40, 0]
84
- ]
85
- expect { @rect.set_vertices(vertices) }.to raise_error(NoMethodError)
86
- end
1
+ describe Gosling::Rect do
2
+ before(:all) do
3
+ @window = Gosu::Window.new(640, 480, false)
4
+ @rect = Gosling::Rect.new(@window)
5
+ end
6
+
7
+ it 'starts with width and height 1' do
8
+ expect(@rect.width).to be == 1
9
+ expect(@rect.height).to be == 1
10
+ end
11
+
12
+ describe '#width' do
13
+ it 'returns our width' do
14
+ expect(@rect.width).to be_kind_of(Numeric)
15
+ end
16
+ end
17
+
18
+ describe '#set_width' do
19
+ it 'alters our width, updating our vertices' do
20
+ rect = Gosling::Rect.new(@window)
21
+ rect.width = 10
22
+ expect(rect.width).to be == 10
23
+ end
24
+
25
+ it 'cannot be less than one' do
26
+ rect = Gosling::Rect.new(@window)
27
+ expect { rect.width = 0 }.to raise_error(ArgumentError)
28
+ expect { rect.width = -100 }.to raise_error(ArgumentError)
29
+ end
30
+
31
+ it 'updates our vertices' do
32
+ expected_vertices = [
33
+ Snow::Vec3[ 0, 0, 0],
34
+ Snow::Vec3[11, 0, 0],
35
+ Snow::Vec3[11, 1, 0],
36
+ Snow::Vec3[ 0, 1, 0]
37
+ ]
38
+
39
+ rect = Gosling::Rect.new(@window)
40
+ rect.width = 11
41
+ expect(rect.get_vertices).to be == expected_vertices
42
+ end
43
+ end
44
+
45
+ describe '#height' do
46
+ it 'returns our height' do
47
+ expect(@rect.height).to be_kind_of(Numeric)
48
+ end
49
+ end
50
+
51
+ describe '#set_height' do
52
+ it 'alters our height, updating our vertices' do
53
+ rect = Gosling::Rect.new(@window)
54
+ rect.height = 15
55
+ expect(rect.height).to be == 15
56
+ end
57
+
58
+ it 'cannot be less than one' do
59
+ rect = Gosling::Rect.new(@window)
60
+ expect { rect.height = 0 }.to raise_error(ArgumentError)
61
+ expect { rect.height = -200 }.to raise_error(ArgumentError)
62
+ end
63
+
64
+ it 'updates our vertices' do
65
+ expected_vertices = [
66
+ Snow::Vec3[0, 0, 0],
67
+ Snow::Vec3[1, 0, 0],
68
+ Snow::Vec3[1, 40, 0],
69
+ Snow::Vec3[0, 40, 0]
70
+ ]
71
+
72
+ rect = Gosling::Rect.new(@window)
73
+ rect.height = 40
74
+ expect(rect.get_vertices).to be == expected_vertices
75
+ end
76
+ end
77
+
78
+ it 'does not allow set_vertices to be called directly' do
79
+ vertices = [
80
+ Snow::Vec3[0, 0, 0],
81
+ Snow::Vec3[1, 10, 0],
82
+ Snow::Vec3[2, 20, 0],
83
+ Snow::Vec3[3, 40, 0]
84
+ ]
85
+ expect { @rect.set_vertices(vertices) }.to raise_error(NoMethodError)
86
+ end
87
87
  end
data/spec/spec_helper.rb CHANGED
@@ -1,3 +1,3 @@
1
- require_relative '../lib/gosling.rb'
2
-
3
- RSpec::Expectations.configuration.on_potential_false_positives = :nothing
1
+ require_relative '../lib/gosling.rb'
2
+
3
+ RSpec::Expectations.configuration.on_potential_false_positives = :nothing
data/spec/sprite_spec.rb CHANGED
@@ -1,45 +1,45 @@
1
- describe Gosling::Sprite do
2
- before(:all) do
3
- @window = Gosu::Window.new(640, 480, false)
4
- @local_path = File.dirname(__FILE__)
5
- @image = Gosling::ImageLibrary.get(File.join(@local_path, 'images/key.png'))
6
-
7
- @sprite = Gosling::Sprite.new(@window)
8
- end
9
-
10
- describe '#get_image' do
11
- it 'returns our image' do
12
- get_sprite = Gosling::Sprite.new(@window)
13
- expect(get_sprite.get_image).to be == nil
14
- get_sprite.set_image(@image)
15
- expect(get_sprite.get_image).to be_instance_of(Gosu::Image)
16
- end
17
- end
18
-
19
- describe '#set_image' do
20
- before(:each) do
21
- @image_sprite = Gosling::Sprite.new(@window)
22
- @image_sprite.set_image(@image)
23
- end
24
-
25
- it 'complains if given something other than an image' do
26
- expect { Gosling::Sprite.new(@window).set_image(:foo) }.to raise_error(ArgumentError)
27
- end
28
-
29
- it 'sets our image' do
30
- expect(@image_sprite.get_image).to be == @image
31
- end
32
-
33
- it 'automatically updates our width and height' do
34
- expect(@image_sprite.width).to be == @image.width
35
- expect(@image_sprite.height).to be == @image.height
36
- expect(@image_sprite.width).to be_kind_of(Numeric)
37
- expect(@image_sprite.height).to be_kind_of(Numeric)
38
- end
39
- end
40
-
41
- it 'does not allow set_width or set_height to be called directly' do
42
- expect { @sprite.set_width(10) }.to raise_error(NoMethodError)
43
- expect { @sprite.set_height(20) }.to raise_error(NoMethodError)
44
- end
45
- end
1
+ describe Gosling::Sprite do
2
+ before(:all) do
3
+ @window = Gosu::Window.new(640, 480, false)
4
+ @local_path = File.dirname(__FILE__)
5
+ @image = Gosling::ImageLibrary.get(File.join(@local_path, 'images/key.png'))
6
+
7
+ @sprite = Gosling::Sprite.new(@window)
8
+ end
9
+
10
+ describe '#get_image' do
11
+ it 'returns our image' do
12
+ get_sprite = Gosling::Sprite.new(@window)
13
+ expect(get_sprite.get_image).to be == nil
14
+ get_sprite.set_image(@image)
15
+ expect(get_sprite.get_image).to be_instance_of(Gosu::Image)
16
+ end
17
+ end
18
+
19
+ describe '#set_image' do
20
+ before(:each) do
21
+ @image_sprite = Gosling::Sprite.new(@window)
22
+ @image_sprite.set_image(@image)
23
+ end
24
+
25
+ it 'complains if given something other than an image' do
26
+ expect { Gosling::Sprite.new(@window).set_image(:foo) }.to raise_error(ArgumentError)
27
+ end
28
+
29
+ it 'sets our image' do
30
+ expect(@image_sprite.get_image).to be == @image
31
+ end
32
+
33
+ it 'automatically updates our width and height' do
34
+ expect(@image_sprite.width).to be == @image.width
35
+ expect(@image_sprite.height).to be == @image.height
36
+ expect(@image_sprite.width).to be_kind_of(Numeric)
37
+ expect(@image_sprite.height).to be_kind_of(Numeric)
38
+ end
39
+ end
40
+
41
+ it 'does not allow set_width or set_height to be called directly' do
42
+ expect { @sprite.set_width(10) }.to raise_error(NoMethodError)
43
+ expect { @sprite.set_height(20) }.to raise_error(NoMethodError)
44
+ end
45
+ end
@@ -1,479 +1,479 @@
1
- class TransformableThing
2
- include Gosling::Transformable
3
-
4
- attr_reader :is_dirty
5
- attr_reader :translation, :translate_is_dirty
6
- attr_reader :center, :center_is_dirty
7
- attr_reader :scale, :scale_is_dirty
8
- end
9
-
10
- describe Gosling::Transformable do
11
- before(:all) do
12
- @read_only_tf = TransformableThing.new
13
- end
14
-
15
- it 'has a center' do
16
- expect(@read_only_tf.center).to be_instance_of(Snow::Vec3)
17
- end
18
-
19
- it 'has a scale' do
20
- expect(@read_only_tf.scale).to be_instance_of(Snow::Vec2)
21
- end
22
-
23
- it 'has a rotation' do
24
- expect(@read_only_tf.rotation).to be_kind_of(Numeric)
25
- end
26
-
27
- it 'has a translation' do
28
- expect(@read_only_tf.translation).to be_instance_of(Snow::Vec3)
29
- end
30
-
31
- it 'has methods for getting its x/y position' do
32
- expect(@read_only_tf.x).to be_kind_of(Numeric)
33
- expect(@read_only_tf.y).to be_kind_of(Numeric)
34
- end
35
-
36
- it 'has methods for setting its x/y position' do
37
- @read_only_tf.x = 13
38
- @read_only_tf.y = -7
39
- expect(@read_only_tf.x).to be == 13
40
- expect(@read_only_tf.y).to be == -7
41
- end
42
-
43
- it 'has methods for getting its x/y centerpoint' do
44
- expect(@read_only_tf.center_x).to be_kind_of(Numeric)
45
- expect(@read_only_tf.center_y).to be_kind_of(Numeric)
46
- end
47
-
48
- it 'has methods for setting its x/y centerpoint' do
49
- @read_only_tf.center_x = 5
50
- @read_only_tf.center_y = 15
51
- expect(@read_only_tf.center_x).to be == 5
52
- expect(@read_only_tf.center_y).to be == 15
53
- end
54
-
55
- it 'has methods for getting its x/y scaling' do
56
- expect(@read_only_tf.scale_x).to be_kind_of(Numeric)
57
- expect(@read_only_tf.scale_y).to be_kind_of(Numeric)
58
- end
59
-
60
- it 'has methods for setting its x/y scaling' do
61
- @read_only_tf.scale_x = 2
62
- @read_only_tf.scale_y = 3
63
- expect(@read_only_tf.scale_x).to be == 2
64
- expect(@read_only_tf.scale_y).to be == 3
65
- end
66
-
67
- it 'has a method for getting its rotation' do
68
- expect(@read_only_tf.rotation).to be_kind_of(Numeric)
69
- end
70
-
71
- it 'has a method for setting its rotation' do
72
- @read_only_tf.rotation = Math::PI
73
- expect(@read_only_tf.rotation).to be == Math::PI
74
- end
75
-
76
- describe '#center=' do
77
- it 'accepts a size 3 vector' do
78
- tf = TransformableThing.new
79
- expect { tf.center = Snow::Vec3[0, 0, 0] }.not_to raise_error
80
- expect { tf.center = [0, 0, 0] }.not_to raise_error
81
- expect { tf.center = :foo }.to raise_error(ArgumentError)
82
- end
83
-
84
- it 'sets the center transform' do
85
- v = Snow::Vec3[10.to_r, 20.to_r, 1.to_r]
86
- tf = TransformableThing.new
87
- tf.center = v
88
- expect(tf.center).to be == v
89
- end
90
-
91
- it 'does not alter the z value, which should always be 1' do
92
- tf = TransformableThing.new
93
- [
94
- Snow::Vec3[1, 1, 1],
95
- Snow::Vec3[1, 1, 13],
96
- Snow::Vec3[1, 1, 7],
97
- Snow::Vec3[1, 1, 29373],
98
- Snow::Vec3[1, 1, -1],
99
- Snow::Vec3[1, 1, 0],
100
- Snow::Vec3[1, 1, -328.7],
101
- ].each do |v|
102
- tf.center = v
103
- expect(tf.center[2]).to be == 1
104
- end
105
- end
106
-
107
- context 'when given a block' do
108
- it 'yields the center vector directly to the block' do
109
- tf = TransformableThing.new
110
- tf.center = [3, -7]
111
-
112
- center_ref = nil
113
- area = nil
114
- tf.set_center do |c|
115
- center_ref = c
116
- area = c.x * c.y
117
- end
118
- expect(center_ref).to be_equal(tf.center)
119
- expect(area).to eq(-21)
120
- end
121
-
122
- it 'marks the transformation as dirty' do
123
- tf = TransformableThing.new
124
- tf.set_center { |t| area = t.x * t.y }
125
- expect(tf.center_is_dirty).to be true
126
- expect(tf.is_dirty).to be true
127
- end
128
- end
129
- end
130
-
131
- describe '#set_scale' do
132
- it 'accepts a size 2 vector, an equivalent, or a single numeric value' do
133
- tf = TransformableThing.new
134
- expect { tf.scale = Snow::Vec2[1, 1] }.not_to raise_error
135
- expect { tf.scale = [1, 1] }.not_to raise_error
136
- expect { tf.scale = 1 }.not_to raise_error
137
- expect { tf.set_scale([1, 1]) }.not_to raise_error
138
- expect { tf.set_scale(1, 1) }.not_to raise_error
139
- expect { tf.set_scale(1) }.not_to raise_error
140
- expect { tf.scale = :foo }.to raise_error(ArgumentError)
141
- end
142
-
143
- it 'sets the scale transform' do
144
- v = Snow::Vec2[2.to_r, 0.5.to_r]
145
- tf = TransformableThing.new
146
- tf.scale = v
147
- expect(tf.scale).to be == v
148
- end
149
-
150
- context 'when given a single value' do
151
- it 'sets the x and y scaling' do
152
- tf = TransformableThing.new
153
- tf.scale = 2.7
154
- expect(tf.scale.x).to eq(2.7)
155
- expect(tf.scale.y).to eq(2.7)
156
- end
157
- end
158
-
159
- context 'when given a block' do
160
- it 'yields the scaling vector directly to the block' do
161
- tf = TransformableThing.new
162
- tf.scale = [3, 2]
163
-
164
- scale_ref = nil
165
- area = nil
166
- tf.set_scale do |s|
167
- scale_ref = s
168
- area = s.x * s.y
169
- end
170
- expect(scale_ref).to be_equal(tf.scale)
171
- expect(area).to eq(6)
172
- end
173
-
174
- it 'marks the transformation as dirty' do
175
- tf = TransformableThing.new
176
- tf.set_scale { |s| area = s.x * s.y }
177
- expect(tf.scale_is_dirty).to be true
178
- expect(tf.is_dirty).to be true
179
- end
180
- end
181
- end
182
-
183
- describe '#set_rotation' do
184
- it 'sets the rotation transform' do
185
- r = Math::PI / 2
186
- tf = TransformableThing.new
187
- tf.rotation = r
188
- expect(tf.rotation).to be == r
189
- end
190
-
191
- it 'does not allow non-finite floats' do
192
- tf = TransformableThing.new
193
- expect { tf.rotation = Float::NAN }.to raise_error(ArgumentError)
194
- expect { tf.rotation = Float::INFINITY }.to raise_error(ArgumentError)
195
- end
196
- end
197
-
198
- describe '#set_translation' do
199
- it 'accepts a size 3 vector' do
200
- tf = TransformableThing.new
201
- expect { tf.translation = Snow::Vec3[0, 0, 0] }.not_to raise_error
202
- expect { tf.translation = :foo }.to raise_error(ArgumentError)
203
- end
204
-
205
- it 'sets the translation transform' do
206
- v = Snow::Vec3[1024.to_r, 768.to_r, 1.to_r]
207
- tf = TransformableThing.new
208
- tf.translation = v
209
- expect(tf.translation).to be == v
210
- end
211
-
212
- it 'does not alter the z value, which should always be 1' do
213
- tf = TransformableThing.new
214
- [
215
- Snow::Vec3[1, 1, 1],
216
- Snow::Vec3[1, 1, 13],
217
- Snow::Vec3[1, 1, 7],
218
- Snow::Vec3[1, 1, 29373],
219
- Snow::Vec3[1, 1, -1],
220
- Snow::Vec3[1, 1, 0],
221
- Snow::Vec3[1, 1, -328.7],
222
- ].each do |v|
223
- tf.translation = v
224
- expect(tf.translation[2]).to be == 1
225
- end
226
- end
227
-
228
- context 'when given a block' do
229
- it 'yields the transform vector directly to the block' do
230
- tf = TransformableThing.new
231
- tf.translation = [7, 11]
232
-
233
- translation_ref = nil
234
- area = nil
235
- tf.set_translation do |t|
236
- translation_ref = t
237
- area = t.x * t.y
238
- end
239
- expect(translation_ref).to be_equal(tf.translation)
240
- expect(area).to eq(77)
241
- end
242
-
243
- it 'marks the transformation as dirty' do
244
- tf = TransformableThing.new
245
- tf.set_translation { |t| area = t.x * t.y }
246
- expect(tf.translate_is_dirty).to be true
247
- expect(tf.is_dirty).to be true
248
- end
249
- end
250
- end
251
-
252
- describe '#to_matrix' do
253
- it 'returns a matrix' do
254
- expect(@read_only_tf.to_matrix).to be_instance_of(Snow::Mat3)
255
- end
256
- end
257
-
258
- it 'centers correctly' do
259
- tf = TransformableThing.new
260
- tf.center = Snow::Vec3[10.to_r, 20.to_r, 0.to_r]
261
- expected_matrix = Snow::Mat3[
262
- 1, 0, -10,
263
- 0, 1, -20,
264
- 0, 0, 1
265
- ]
266
- expect(tf.to_matrix).to be == expected_matrix
267
- end
268
-
269
- it 'scales correctly' do
270
- tf = TransformableThing.new
271
- tf.scale = Snow::Vec2[2.to_r, 0.5.to_r]
272
- expected_matrix = Snow::Mat3[
273
- 2, 0, 0,
274
- 0, 0.5, 0,
275
- 0, 0, 1
276
- ]
277
- expect(tf.to_matrix).to be == expected_matrix
278
- end
279
-
280
- it 'rotates correctly' do
281
- tf = TransformableThing.new
282
- tf.rotation = Math::PI / 2
283
- expected_matrix = Snow::Mat3[
284
- 0, 1, 0,
285
- -1, 0, 0,
286
- 0, 0, 1
287
- ]
288
- expect(tf.to_matrix).to be == expected_matrix
289
- end
290
-
291
- it 'translates correctly' do
292
- tf = TransformableThing.new
293
- tf.translation = Snow::Vec3[1024.to_r, 768.to_r, 0.to_r]
294
- expected_matrix = Snow::Mat3[
295
- 1, 0, 1024,
296
- 0, 1, 768,
297
- 0, 0, 1
298
- ]
299
- expect(tf.to_matrix).to be == expected_matrix
300
- end
301
-
302
- it 'applies all transforms in the correct order' do
303
- tf = TransformableThing.new
304
- tf.center = Snow::Vec3[10.to_r, 20.to_r, 0.to_r]
305
- tf.scale = Snow::Vec2[2.to_r, 0.5.to_r]
306
- tf.rotation = Math::PI / 2
307
- tf.translation = Snow::Vec3[1024.to_r, 768.to_r, 0.to_r]
308
-
309
- expected_matrix = Snow::Mat3[
310
- 0.0, 0.5, 1014.0,
311
- -2.0, 0.0, 788.0,
312
- 0.0, 0.0, 1.0
313
- ]
314
- expect(tf.to_matrix).to be == expected_matrix
315
-
316
- v = Snow::Vec3[0.to_r, -50.to_r, 1.to_r]
317
- v_transformed = Snow::Vec3[989.to_r, 788.to_r, 1.to_r]
318
- expect(tf.to_matrix * v).to be == v_transformed
319
-
320
- v = Snow::Vec3[100.to_r, -50.to_r, 1.to_r]
321
- v_transformed = Snow::Vec3[989.to_r, 588.to_r, 1.to_r]
322
- expect(tf.to_matrix * v).to be == v_transformed
323
-
324
- v = Snow::Vec3[100.to_r, 50.to_r, 1.to_r]
325
- v_transformed = Snow::Vec3[1039.to_r, 588.to_r, 1.to_r]
326
- expect(tf.to_matrix * v).to be == v_transformed
327
-
328
- v = Snow::Vec3[0.to_r, 50.to_r, 1.to_r]
329
- v_transformed = Snow::Vec3[1039.to_r, 788.to_r, 1.to_r]
330
- expect(tf.to_matrix * v).to be == v_transformed
331
- end
332
-
333
- describe '#transform_point' do
334
- it 'expects a length 3 vector' do
335
- out = Snow::Vec3.new
336
- expect { @read_only_tf.transform_point(Snow::Vec3[1, 0, 1], out) }.not_to raise_error
337
- expect { @read_only_tf.transform_point(:foo, out) }.to raise_error
338
- expect { @read_only_tf.transform_point(nil, out) }.to raise_error
339
- end
340
-
341
- it 'returns a length 3 vector' do
342
- out = Snow::Vec3.new
343
- result = @read_only_tf.transform_point(Snow::Vec3[1, 0, 1], out)
344
- expect(result).to be_instance_of(Snow::Vec3)
345
- expect(result).to equal(out)
346
- end
347
-
348
- it 'always returns a z value of 0' do
349
- out = Snow::Vec3.new
350
- [
351
- Snow::Vec3[1, 1, 1],
352
- Snow::Vec3[-1, -1, -1],
353
- Snow::Vec3[-22, -22, 0],
354
- Snow::Vec3[-11, 13, 34],
355
- Snow::Vec3[37, -4, -15],
356
- Snow::Vec3[34, 39, -16],
357
- Snow::Vec3[-48, 23, -32],
358
- Snow::Vec3[24, -39, 42],
359
- Snow::Vec3[49, 44, -15],
360
- Snow::Vec3[27, 23, 42],
361
- Snow::Vec3[33, -25, -20],
362
- Snow::Vec3[-46, -18, 48],
363
- ].each do |v|
364
- expect(@read_only_tf.transform_point(v, out)[2]).to be == 0
365
- expect(out.z).to eq(0)
366
- end
367
- end
368
-
369
- it 'transforms the point correctly' do
370
- tf = TransformableThing.new
371
- tf.center = Snow::Vec3[5.to_r, 20.to_r, 0.to_r]
372
- tf.scale = Snow::Vec2[2.to_r, 0.5.to_r]
373
- tf.rotation = Math::PI / 2
374
- tf.translation = Snow::Vec3[1024.to_r, 768.to_r, 0.to_r]
375
-
376
- out = Snow::Vec3.new
377
- [
378
- [0, 0],
379
- [10, -20],
380
- [-1024, -768],
381
- [-5, 999],
382
- [5, 20]
383
- ].each do |pt|
384
- x, y = pt
385
- expected = Snow::Vec3[(y - 20) * 0.5 + 1024, (x - 5) * -2 + 768, 0]
386
- expect(tf.transform_point(Snow::Vec3[x, y, 0], out)).to be == expected
387
- expect(out).to be == expected
388
- end
389
- end
390
- end
391
-
392
- describe '#untransform_point' do
393
- it 'expects a length 3 vector' do
394
- out = Snow::Vec3.new
395
- expect { @read_only_tf.untransform_point(Snow::Vec3[1, 0, 1], out) }.not_to raise_error
396
- expect { @read_only_tf.untransform_point(:foo, out) }.to raise_error
397
- expect { @read_only_tf.untransform_point(nil, out) }.to raise_error
398
- end
399
-
400
- it 'returns a length 3 vector' do
401
- out = Snow::Vec3.new
402
- result = @read_only_tf.untransform_point(Snow::Vec3[1, 0, 1], out)
403
- expect(result).to be_instance_of(Snow::Vec3)
404
- expect(out).to equal(result)
405
- end
406
-
407
- it 'always returns a z value of 0' do
408
- out = Snow::Vec3.new
409
- [
410
- Snow::Vec3[1, 1, 1],
411
- Snow::Vec3[-1, -1, -1],
412
- Snow::Vec3[-22, -22, 0],
413
- Snow::Vec3[-11, 13, 34],
414
- Snow::Vec3[37, -4, -15],
415
- Snow::Vec3[34, 39, -16],
416
- Snow::Vec3[-48, 23, -32],
417
- Snow::Vec3[24, -39, 42],
418
- Snow::Vec3[49, 44, -15],
419
- Snow::Vec3[27, 23, 42],
420
- Snow::Vec3[33, -25, -20],
421
- Snow::Vec3[-46, -18, 48],
422
- ].each do |v|
423
- expect(@read_only_tf.untransform_point(v, out)[2]).to be == 0
424
- expect(out.z).to be == 0
425
- end
426
- end
427
-
428
- it 'untransforms the point correctly' do
429
- tf = TransformableThing.new
430
- tf.center = Snow::Vec3[5.to_r, 20.to_r, 0.to_r]
431
- tf.scale = Snow::Vec2[2.to_r, 0.5.to_r]
432
- tf.rotation = Math::PI / 2
433
- tf.translation = Snow::Vec3[1024.to_r, 768.to_r, 0.to_r]
434
-
435
- out = Snow::Vec3.new
436
- [
437
- [1014, 778],
438
- [1004, 758],
439
- [630, 2826],
440
- [788, 3027],
441
- [768, 1024]
442
- ].each do |pt|
443
- x, y = pt
444
- expected = Snow::Vec3[(y - 768) * -0.5 + 5, (x - 1024) * 2 + 20, 0]
445
- expect(tf.untransform_point(Snow::Vec3[x, y, 0], out)).to be == expected
446
- expect(out).to be == expected
447
- end
448
- end
449
-
450
- it 'undoes the results of transform_point' do
451
- tf = TransformableThing.new
452
- tf.center = Snow::Vec3[5.to_r, 20.to_r, 0.to_r]
453
- tf.scale = Snow::Vec2[2.to_r, 0.5.to_r]
454
- tf.rotation = Math::PI / 2
455
- tf.translation = Snow::Vec3[1024.to_r, 768.to_r, 0.to_r]
456
-
457
- out = Snow::Vec3.new
458
- [
459
- Snow::Vec3[1, 1, 0],
460
- Snow::Vec3[-1, -1, 0],
461
- Snow::Vec3[-22, -22, 0],
462
- Snow::Vec3[-11, 13, 0],
463
- Snow::Vec3[37, -4, 0],
464
- Snow::Vec3[34, 39, 0],
465
- Snow::Vec3[-48, 23, 0],
466
- Snow::Vec3[24, -39, 0],
467
- Snow::Vec3[49, 44, 0],
468
- Snow::Vec3[27, 23, 0],
469
- Snow::Vec3[33, -25, 0],
470
- Snow::Vec3[-46, -18, 0],
471
- ].each do |v|
472
- expect(tf.untransform_point(tf.transform_point(v, out), out)).to be == v
473
- expect(out).to be == v
474
- expect(tf.transform_point(tf.untransform_point(v, out), out)).to be == v
475
- expect(out).to be == v
476
- end
477
- end
478
- end
479
- end
1
+ class TransformableThing
2
+ include Gosling::Transformable
3
+
4
+ attr_reader :is_dirty
5
+ attr_reader :translation, :translate_is_dirty
6
+ attr_reader :center, :center_is_dirty
7
+ attr_reader :scale, :scale_is_dirty
8
+ end
9
+
10
+ describe Gosling::Transformable do
11
+ before(:all) do
12
+ @read_only_tf = TransformableThing.new
13
+ end
14
+
15
+ it 'has a center' do
16
+ expect(@read_only_tf.center).to be_instance_of(Snow::Vec3)
17
+ end
18
+
19
+ it 'has a scale' do
20
+ expect(@read_only_tf.scale).to be_instance_of(Snow::Vec2)
21
+ end
22
+
23
+ it 'has a rotation' do
24
+ expect(@read_only_tf.rotation).to be_kind_of(Numeric)
25
+ end
26
+
27
+ it 'has a translation' do
28
+ expect(@read_only_tf.translation).to be_instance_of(Snow::Vec3)
29
+ end
30
+
31
+ it 'has methods for getting its x/y position' do
32
+ expect(@read_only_tf.x).to be_kind_of(Numeric)
33
+ expect(@read_only_tf.y).to be_kind_of(Numeric)
34
+ end
35
+
36
+ it 'has methods for setting its x/y position' do
37
+ @read_only_tf.x = 13
38
+ @read_only_tf.y = -7
39
+ expect(@read_only_tf.x).to be == 13
40
+ expect(@read_only_tf.y).to be == -7
41
+ end
42
+
43
+ it 'has methods for getting its x/y centerpoint' do
44
+ expect(@read_only_tf.center_x).to be_kind_of(Numeric)
45
+ expect(@read_only_tf.center_y).to be_kind_of(Numeric)
46
+ end
47
+
48
+ it 'has methods for setting its x/y centerpoint' do
49
+ @read_only_tf.center_x = 5
50
+ @read_only_tf.center_y = 15
51
+ expect(@read_only_tf.center_x).to be == 5
52
+ expect(@read_only_tf.center_y).to be == 15
53
+ end
54
+
55
+ it 'has methods for getting its x/y scaling' do
56
+ expect(@read_only_tf.scale_x).to be_kind_of(Numeric)
57
+ expect(@read_only_tf.scale_y).to be_kind_of(Numeric)
58
+ end
59
+
60
+ it 'has methods for setting its x/y scaling' do
61
+ @read_only_tf.scale_x = 2
62
+ @read_only_tf.scale_y = 3
63
+ expect(@read_only_tf.scale_x).to be == 2
64
+ expect(@read_only_tf.scale_y).to be == 3
65
+ end
66
+
67
+ it 'has a method for getting its rotation' do
68
+ expect(@read_only_tf.rotation).to be_kind_of(Numeric)
69
+ end
70
+
71
+ it 'has a method for setting its rotation' do
72
+ @read_only_tf.rotation = Math::PI
73
+ expect(@read_only_tf.rotation).to be == Math::PI
74
+ end
75
+
76
+ describe '#center=' do
77
+ it 'accepts a size 3 vector' do
78
+ tf = TransformableThing.new
79
+ expect { tf.center = Snow::Vec3[0, 0, 0] }.not_to raise_error
80
+ expect { tf.center = [0, 0, 0] }.not_to raise_error
81
+ expect { tf.center = :foo }.to raise_error(ArgumentError)
82
+ end
83
+
84
+ it 'sets the center transform' do
85
+ v = Snow::Vec3[10.to_r, 20.to_r, 1.to_r]
86
+ tf = TransformableThing.new
87
+ tf.center = v
88
+ expect(tf.center).to be == v
89
+ end
90
+
91
+ it 'does not alter the z value, which should always be 1' do
92
+ tf = TransformableThing.new
93
+ [
94
+ Snow::Vec3[1, 1, 1],
95
+ Snow::Vec3[1, 1, 13],
96
+ Snow::Vec3[1, 1, 7],
97
+ Snow::Vec3[1, 1, 29373],
98
+ Snow::Vec3[1, 1, -1],
99
+ Snow::Vec3[1, 1, 0],
100
+ Snow::Vec3[1, 1, -328.7],
101
+ ].each do |v|
102
+ tf.center = v
103
+ expect(tf.center[2]).to be == 1
104
+ end
105
+ end
106
+
107
+ context 'when given a block' do
108
+ it 'yields the center vector directly to the block' do
109
+ tf = TransformableThing.new
110
+ tf.center = [3, -7]
111
+
112
+ center_ref = nil
113
+ area = nil
114
+ tf.set_center do |c|
115
+ center_ref = c
116
+ area = c.x * c.y
117
+ end
118
+ expect(center_ref).to be_equal(tf.center)
119
+ expect(area).to eq(-21)
120
+ end
121
+
122
+ it 'marks the transformation as dirty' do
123
+ tf = TransformableThing.new
124
+ tf.set_center { |t| area = t.x * t.y }
125
+ expect(tf.center_is_dirty).to be true
126
+ expect(tf.is_dirty).to be true
127
+ end
128
+ end
129
+ end
130
+
131
+ describe '#set_scale' do
132
+ it 'accepts a size 2 vector, an equivalent, or a single numeric value' do
133
+ tf = TransformableThing.new
134
+ expect { tf.scale = Snow::Vec2[1, 1] }.not_to raise_error
135
+ expect { tf.scale = [1, 1] }.not_to raise_error
136
+ expect { tf.scale = 1 }.not_to raise_error
137
+ expect { tf.set_scale([1, 1]) }.not_to raise_error
138
+ expect { tf.set_scale(1, 1) }.not_to raise_error
139
+ expect { tf.set_scale(1) }.not_to raise_error
140
+ expect { tf.scale = :foo }.to raise_error(ArgumentError)
141
+ end
142
+
143
+ it 'sets the scale transform' do
144
+ v = Snow::Vec2[2.to_r, 0.5.to_r]
145
+ tf = TransformableThing.new
146
+ tf.scale = v
147
+ expect(tf.scale).to be == v
148
+ end
149
+
150
+ context 'when given a single value' do
151
+ it 'sets the x and y scaling' do
152
+ tf = TransformableThing.new
153
+ tf.scale = 2.7
154
+ expect(tf.scale.x).to eq(2.7)
155
+ expect(tf.scale.y).to eq(2.7)
156
+ end
157
+ end
158
+
159
+ context 'when given a block' do
160
+ it 'yields the scaling vector directly to the block' do
161
+ tf = TransformableThing.new
162
+ tf.scale = [3, 2]
163
+
164
+ scale_ref = nil
165
+ area = nil
166
+ tf.set_scale do |s|
167
+ scale_ref = s
168
+ area = s.x * s.y
169
+ end
170
+ expect(scale_ref).to be_equal(tf.scale)
171
+ expect(area).to eq(6)
172
+ end
173
+
174
+ it 'marks the transformation as dirty' do
175
+ tf = TransformableThing.new
176
+ tf.set_scale { |s| area = s.x * s.y }
177
+ expect(tf.scale_is_dirty).to be true
178
+ expect(tf.is_dirty).to be true
179
+ end
180
+ end
181
+ end
182
+
183
+ describe '#set_rotation' do
184
+ it 'sets the rotation transform' do
185
+ r = Math::PI / 2
186
+ tf = TransformableThing.new
187
+ tf.rotation = r
188
+ expect(tf.rotation).to be == r
189
+ end
190
+
191
+ it 'does not allow non-finite floats' do
192
+ tf = TransformableThing.new
193
+ expect { tf.rotation = Float::NAN }.to raise_error(ArgumentError)
194
+ expect { tf.rotation = Float::INFINITY }.to raise_error(ArgumentError)
195
+ end
196
+ end
197
+
198
+ describe '#set_translation' do
199
+ it 'accepts a size 3 vector' do
200
+ tf = TransformableThing.new
201
+ expect { tf.translation = Snow::Vec3[0, 0, 0] }.not_to raise_error
202
+ expect { tf.translation = :foo }.to raise_error(ArgumentError)
203
+ end
204
+
205
+ it 'sets the translation transform' do
206
+ v = Snow::Vec3[1024.to_r, 768.to_r, 1.to_r]
207
+ tf = TransformableThing.new
208
+ tf.translation = v
209
+ expect(tf.translation).to be == v
210
+ end
211
+
212
+ it 'does not alter the z value, which should always be 1' do
213
+ tf = TransformableThing.new
214
+ [
215
+ Snow::Vec3[1, 1, 1],
216
+ Snow::Vec3[1, 1, 13],
217
+ Snow::Vec3[1, 1, 7],
218
+ Snow::Vec3[1, 1, 29373],
219
+ Snow::Vec3[1, 1, -1],
220
+ Snow::Vec3[1, 1, 0],
221
+ Snow::Vec3[1, 1, -328.7],
222
+ ].each do |v|
223
+ tf.translation = v
224
+ expect(tf.translation[2]).to be == 1
225
+ end
226
+ end
227
+
228
+ context 'when given a block' do
229
+ it 'yields the transform vector directly to the block' do
230
+ tf = TransformableThing.new
231
+ tf.translation = [7, 11]
232
+
233
+ translation_ref = nil
234
+ area = nil
235
+ tf.set_translation do |t|
236
+ translation_ref = t
237
+ area = t.x * t.y
238
+ end
239
+ expect(translation_ref).to be_equal(tf.translation)
240
+ expect(area).to eq(77)
241
+ end
242
+
243
+ it 'marks the transformation as dirty' do
244
+ tf = TransformableThing.new
245
+ tf.set_translation { |t| area = t.x * t.y }
246
+ expect(tf.translate_is_dirty).to be true
247
+ expect(tf.is_dirty).to be true
248
+ end
249
+ end
250
+ end
251
+
252
+ describe '#to_matrix' do
253
+ it 'returns a matrix' do
254
+ expect(@read_only_tf.to_matrix).to be_instance_of(Snow::Mat3)
255
+ end
256
+ end
257
+
258
+ it 'centers correctly' do
259
+ tf = TransformableThing.new
260
+ tf.center = Snow::Vec3[10.to_r, 20.to_r, 0.to_r]
261
+ expected_matrix = Snow::Mat3[
262
+ 1, 0, -10,
263
+ 0, 1, -20,
264
+ 0, 0, 1
265
+ ]
266
+ expect(tf.to_matrix).to be == expected_matrix
267
+ end
268
+
269
+ it 'scales correctly' do
270
+ tf = TransformableThing.new
271
+ tf.scale = Snow::Vec2[2.to_r, 0.5.to_r]
272
+ expected_matrix = Snow::Mat3[
273
+ 2, 0, 0,
274
+ 0, 0.5, 0,
275
+ 0, 0, 1
276
+ ]
277
+ expect(tf.to_matrix).to be == expected_matrix
278
+ end
279
+
280
+ it 'rotates correctly' do
281
+ tf = TransformableThing.new
282
+ tf.rotation = Math::PI / 2
283
+ expected_matrix = Snow::Mat3[
284
+ 0, 1, 0,
285
+ -1, 0, 0,
286
+ 0, 0, 1
287
+ ]
288
+ expect(tf.to_matrix).to be == expected_matrix
289
+ end
290
+
291
+ it 'translates correctly' do
292
+ tf = TransformableThing.new
293
+ tf.translation = Snow::Vec3[1024.to_r, 768.to_r, 0.to_r]
294
+ expected_matrix = Snow::Mat3[
295
+ 1, 0, 1024,
296
+ 0, 1, 768,
297
+ 0, 0, 1
298
+ ]
299
+ expect(tf.to_matrix).to be == expected_matrix
300
+ end
301
+
302
+ it 'applies all transforms in the correct order' do
303
+ tf = TransformableThing.new
304
+ tf.center = Snow::Vec3[10.to_r, 20.to_r, 0.to_r]
305
+ tf.scale = Snow::Vec2[2.to_r, 0.5.to_r]
306
+ tf.rotation = Math::PI / 2
307
+ tf.translation = Snow::Vec3[1024.to_r, 768.to_r, 0.to_r]
308
+
309
+ expected_matrix = Snow::Mat3[
310
+ 0.0, 0.5, 1014.0,
311
+ -2.0, 0.0, 788.0,
312
+ 0.0, 0.0, 1.0
313
+ ]
314
+ expect(tf.to_matrix).to be == expected_matrix
315
+
316
+ v = Snow::Vec3[0.to_r, -50.to_r, 1.to_r]
317
+ v_transformed = Snow::Vec3[989.to_r, 788.to_r, 1.to_r]
318
+ expect(tf.to_matrix * v).to be == v_transformed
319
+
320
+ v = Snow::Vec3[100.to_r, -50.to_r, 1.to_r]
321
+ v_transformed = Snow::Vec3[989.to_r, 588.to_r, 1.to_r]
322
+ expect(tf.to_matrix * v).to be == v_transformed
323
+
324
+ v = Snow::Vec3[100.to_r, 50.to_r, 1.to_r]
325
+ v_transformed = Snow::Vec3[1039.to_r, 588.to_r, 1.to_r]
326
+ expect(tf.to_matrix * v).to be == v_transformed
327
+
328
+ v = Snow::Vec3[0.to_r, 50.to_r, 1.to_r]
329
+ v_transformed = Snow::Vec3[1039.to_r, 788.to_r, 1.to_r]
330
+ expect(tf.to_matrix * v).to be == v_transformed
331
+ end
332
+
333
+ describe '#transform_point' do
334
+ it 'expects a length 3 vector' do
335
+ out = Snow::Vec3.new
336
+ expect { @read_only_tf.transform_point(Snow::Vec3[1, 0, 1], out) }.not_to raise_error
337
+ expect { @read_only_tf.transform_point(:foo, out) }.to raise_error
338
+ expect { @read_only_tf.transform_point(nil, out) }.to raise_error
339
+ end
340
+
341
+ it 'returns a length 3 vector' do
342
+ out = Snow::Vec3.new
343
+ result = @read_only_tf.transform_point(Snow::Vec3[1, 0, 1], out)
344
+ expect(result).to be_instance_of(Snow::Vec3)
345
+ expect(result).to equal(out)
346
+ end
347
+
348
+ it 'always returns a z value of 0' do
349
+ out = Snow::Vec3.new
350
+ [
351
+ Snow::Vec3[1, 1, 1],
352
+ Snow::Vec3[-1, -1, -1],
353
+ Snow::Vec3[-22, -22, 0],
354
+ Snow::Vec3[-11, 13, 34],
355
+ Snow::Vec3[37, -4, -15],
356
+ Snow::Vec3[34, 39, -16],
357
+ Snow::Vec3[-48, 23, -32],
358
+ Snow::Vec3[24, -39, 42],
359
+ Snow::Vec3[49, 44, -15],
360
+ Snow::Vec3[27, 23, 42],
361
+ Snow::Vec3[33, -25, -20],
362
+ Snow::Vec3[-46, -18, 48],
363
+ ].each do |v|
364
+ expect(@read_only_tf.transform_point(v, out)[2]).to be == 0
365
+ expect(out.z).to eq(0)
366
+ end
367
+ end
368
+
369
+ it 'transforms the point correctly' do
370
+ tf = TransformableThing.new
371
+ tf.center = Snow::Vec3[5.to_r, 20.to_r, 0.to_r]
372
+ tf.scale = Snow::Vec2[2.to_r, 0.5.to_r]
373
+ tf.rotation = Math::PI / 2
374
+ tf.translation = Snow::Vec3[1024.to_r, 768.to_r, 0.to_r]
375
+
376
+ out = Snow::Vec3.new
377
+ [
378
+ [0, 0],
379
+ [10, -20],
380
+ [-1024, -768],
381
+ [-5, 999],
382
+ [5, 20]
383
+ ].each do |pt|
384
+ x, y = pt
385
+ expected = Snow::Vec3[(y - 20) * 0.5 + 1024, (x - 5) * -2 + 768, 0]
386
+ expect(tf.transform_point(Snow::Vec3[x, y, 0], out)).to be == expected
387
+ expect(out).to be == expected
388
+ end
389
+ end
390
+ end
391
+
392
+ describe '#untransform_point' do
393
+ it 'expects a length 3 vector' do
394
+ out = Snow::Vec3.new
395
+ expect { @read_only_tf.untransform_point(Snow::Vec3[1, 0, 1], out) }.not_to raise_error
396
+ expect { @read_only_tf.untransform_point(:foo, out) }.to raise_error
397
+ expect { @read_only_tf.untransform_point(nil, out) }.to raise_error
398
+ end
399
+
400
+ it 'returns a length 3 vector' do
401
+ out = Snow::Vec3.new
402
+ result = @read_only_tf.untransform_point(Snow::Vec3[1, 0, 1], out)
403
+ expect(result).to be_instance_of(Snow::Vec3)
404
+ expect(out).to equal(result)
405
+ end
406
+
407
+ it 'always returns a z value of 0' do
408
+ out = Snow::Vec3.new
409
+ [
410
+ Snow::Vec3[1, 1, 1],
411
+ Snow::Vec3[-1, -1, -1],
412
+ Snow::Vec3[-22, -22, 0],
413
+ Snow::Vec3[-11, 13, 34],
414
+ Snow::Vec3[37, -4, -15],
415
+ Snow::Vec3[34, 39, -16],
416
+ Snow::Vec3[-48, 23, -32],
417
+ Snow::Vec3[24, -39, 42],
418
+ Snow::Vec3[49, 44, -15],
419
+ Snow::Vec3[27, 23, 42],
420
+ Snow::Vec3[33, -25, -20],
421
+ Snow::Vec3[-46, -18, 48],
422
+ ].each do |v|
423
+ expect(@read_only_tf.untransform_point(v, out)[2]).to be == 0
424
+ expect(out.z).to be == 0
425
+ end
426
+ end
427
+
428
+ it 'untransforms the point correctly' do
429
+ tf = TransformableThing.new
430
+ tf.center = Snow::Vec3[5.to_r, 20.to_r, 0.to_r]
431
+ tf.scale = Snow::Vec2[2.to_r, 0.5.to_r]
432
+ tf.rotation = Math::PI / 2
433
+ tf.translation = Snow::Vec3[1024.to_r, 768.to_r, 0.to_r]
434
+
435
+ out = Snow::Vec3.new
436
+ [
437
+ [1014, 778],
438
+ [1004, 758],
439
+ [630, 2826],
440
+ [788, 3027],
441
+ [768, 1024]
442
+ ].each do |pt|
443
+ x, y = pt
444
+ expected = Snow::Vec3[(y - 768) * -0.5 + 5, (x - 1024) * 2 + 20, 0]
445
+ expect(tf.untransform_point(Snow::Vec3[x, y, 0], out)).to be == expected
446
+ expect(out).to be == expected
447
+ end
448
+ end
449
+
450
+ it 'undoes the results of transform_point' do
451
+ tf = TransformableThing.new
452
+ tf.center = Snow::Vec3[5.to_r, 20.to_r, 0.to_r]
453
+ tf.scale = Snow::Vec2[2.to_r, 0.5.to_r]
454
+ tf.rotation = Math::PI / 2
455
+ tf.translation = Snow::Vec3[1024.to_r, 768.to_r, 0.to_r]
456
+
457
+ out = Snow::Vec3.new
458
+ [
459
+ Snow::Vec3[1, 1, 0],
460
+ Snow::Vec3[-1, -1, 0],
461
+ Snow::Vec3[-22, -22, 0],
462
+ Snow::Vec3[-11, 13, 0],
463
+ Snow::Vec3[37, -4, 0],
464
+ Snow::Vec3[34, 39, 0],
465
+ Snow::Vec3[-48, 23, 0],
466
+ Snow::Vec3[24, -39, 0],
467
+ Snow::Vec3[49, 44, 0],
468
+ Snow::Vec3[27, 23, 0],
469
+ Snow::Vec3[33, -25, 0],
470
+ Snow::Vec3[-46, -18, 0],
471
+ ].each do |v|
472
+ expect(tf.untransform_point(tf.transform_point(v, out), out)).to be == v
473
+ expect(out).to be == v
474
+ expect(tf.transform_point(tf.untransform_point(v, out), out)).to be == v
475
+ expect(out).to be == v
476
+ end
477
+ end
478
+ end
479
+ end