gosu_enhanced 0.1.0 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 8c8c0ff47fba5b897c2f6066b9f4306cdae9f42b
4
- data.tar.gz: ad4815c8c03d03ab0483b7b77f094e68a7a26eed
3
+ metadata.gz: c5dd0948ca7db7ec8a520b70f29b267aa8883eaf
4
+ data.tar.gz: 8a160117c73e5d9d67bc751b63ad44784bdda3f3
5
5
  SHA512:
6
- metadata.gz: 197ec5637526f565b3594ed2e3653812c753bdcf45336cfecd0e05ed28936fc32ce239541052835934f738f2ecbde49c3fc8d34d9bc806a49942f37bf64976d7
7
- data.tar.gz: d50c833a17e528fca792fa1ea0c3a9ac656fa9ec0b274833b7b74a99e0d10953c70196a3893db943dfe5fa42bc647e56b9d8b54522a5235eb0fecdcf106a5d15
6
+ metadata.gz: e13b5c1e84a17fb7a7c6323e13cf2d957e37a495245d3025c2bf24a6bc1dae1469163ce79e6db3081ec47da64efec9adb8834d35bc8c305233dde60b74019f64
7
+ data.tar.gz: bc0fe6bc7545d2cfe6efe5a567b7aabd0adda888bf4a5b09dbdaf74581d793b7d944117ad5444f278cdc2adb2ea040752d62d91100d1dcb2e0b4eb92417d124a
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- gosu_enhanced (0.1.0)
4
+ gosu_enhanced (0.2.0)
5
5
  gosu (~> 0.7)
6
6
 
7
7
  GEM
data/README.md CHANGED
@@ -4,23 +4,28 @@ Some added classes for using Gosu.
4
4
 
5
5
  ## Point
6
6
 
7
- Holds a set of (x, y) co-ordinates. Allows for offsetting and moving by another Point
8
- or a pair of co-ordinates.
7
+ Holds a set of (x, y) co-ordinates. Allows for offsetting and moving by another
8
+ Point or a pair of co-ordinates.
9
9
 
10
10
  ## Size
11
11
 
12
- Holds a (width, height) size. Allows for inflation / deflation with another size or
13
- a pair of sizes.
12
+ Holds a (width, height) size. Allows for inflation / deflation with another
13
+ size or a pair of sizes.
14
+
15
+ ## Region
16
+
17
+ Holds a position and size. Delegates movement and size change to its
18
+ constituents.
14
19
 
15
20
  ## Gosu::Window
16
21
 
17
- Added #draw_rectangle which draws a simple rectangle specified with a Point and Size
18
- in one colour.
22
+ Added #draw_rectangle which draws a simple rectangle specified with a Point
23
+ and Size in one colour.
19
24
 
20
25
  ## Gosu::Font
21
26
 
22
- Added #measure and #centred_in which measure a piece of text and centre it in a
23
- given Size.
27
+ Added #measure and #centred_in which measure a piece of text and centre it in
28
+ a given Size.
24
29
 
25
30
  ## Installation
26
31
 
@@ -9,11 +9,11 @@ Gem::Specification.new do |spec|
9
9
  spec.authors = ["Julian Nicholls"]
10
10
  spec.email = ["juliannicholls29@gmail.com"]
11
11
  spec.summary = %q{Enhanced versions of some Gosu classes.}
12
- spec.description = %q{Point and Size classes to hold pixel addresses and
12
+ spec.description = %q{Point, Size, and Region classes to hold pixel addresses and
13
13
  rectangle sizes respectively. Updated window class to draw rectangle in a single
14
14
  colour more easily. Updated font class to measure text and return co-ordinates
15
15
  to centre a piece of text in a rectangle size.}
16
- spec.homepage = ""
16
+ spec.homepage = "https://github.com/JulianNicholls/gosu_enhanced-gem"
17
17
  spec.license = "MIT"
18
18
 
19
19
  spec.files = `git ls-files -z`.split("\x0")
data/lib/gosu_enhanced.rb CHANGED
@@ -4,4 +4,5 @@ require "gosu_enhanced/version"
4
4
 
5
5
  require 'gosu_enhanced/point'
6
6
  require 'gosu_enhanced/size'
7
+ require 'gosu_enhanced/region'
7
8
  require 'gosu_enhanced/enhanced'
@@ -0,0 +1,44 @@
1
+ require 'forwardable'
2
+
3
+ module GosuEnhanced
4
+ # Hold a rectangular region specified by a Point and a Size
5
+ # Most functions are delegated to the constituent Point and Size
6
+ class Region
7
+ extend Forwardable
8
+
9
+ def_delegators :@position, :move_by!, :move_to!, :x, :y
10
+ def_delegators :@size, :inflate!, :deflate!, :width, :height
11
+
12
+ attr_reader :position, :size
13
+
14
+ def initialize( pos, size )
15
+ @position, @size = pos.dup, size.dup
16
+ end
17
+
18
+ def contains?( col, row = nil )
19
+ if col.respond_to? :x
20
+ col.x.between?( left, left + width - 1 ) &&
21
+ col.y.between?( top, top + height - 1 )
22
+ else
23
+ col.between?( left, left + width - 1 ) &&
24
+ row.between?( top, top + height - 1 )
25
+ end
26
+ end
27
+
28
+ def top
29
+ position.y
30
+ end
31
+
32
+ def left
33
+ position.x
34
+ end
35
+
36
+ def dup
37
+ Region.new( position.dup, size.dup )
38
+ end
39
+
40
+ def draw( surface, z, colour )
41
+ surface.draw_rectangle( position, size, z, colour )
42
+ end
43
+ end
44
+ end
@@ -1,9 +1,16 @@
1
1
  module GosuEnhanced
2
2
  # Hold a 2-dimensional size and allow for inflation / deflation
3
- class Size < Struct.new( :width, :height )
4
- # Inflation with a negative value or deflation is not allowed to make a
5
- # dimension negative.
6
-
3
+ class Size
4
+ attr_reader :width, :height
5
+
6
+ # Neither dimension can be negative, since that doesn't make sense.
7
+ # The values are checked on inflation / deflation.
8
+
9
+ def initialize( w, h )
10
+ @width, @height = w, h
11
+ validate( 0, 0 )
12
+ end
13
+
7
14
  def inflate( by_w, by_h = nil )
8
15
  if by_w.respond_to? :width
9
16
  validate( by_w.width, by_w.height )
@@ -28,13 +35,13 @@ module GosuEnhanced
28
35
  if by_w.respond_to? :width
29
36
  validate( by_w.width, by_w.height )
30
37
 
31
- self.width += by_w.width
32
- self.height += by_w.height
38
+ @width += by_w.width
39
+ @height += by_w.height
33
40
  else
34
41
  validate( by_w, by_h )
35
42
 
36
- self.width += by_w
37
- self.height += by_h
43
+ @width += by_w
44
+ @height += by_h
38
45
  end
39
46
  end
40
47
 
@@ -46,6 +53,9 @@ module GosuEnhanced
46
53
  end
47
54
  end
48
55
 
56
+ def ==( other )
57
+ width == other.width && height == other.height
58
+ end
49
59
  private
50
60
 
51
61
  def validate( by_w, by_h )
@@ -1,3 +1,3 @@
1
1
  module GosuEnhanced
2
- VERSION = "0.1.0"
2
+ VERSION = "0.2.0"
3
3
  end
data/spec/point_spec.rb CHANGED
@@ -2,11 +2,25 @@ require 'spec_helper'
2
2
 
3
3
  describe GosuEnhanced::Point do
4
4
  describe '#initialize' do
5
- it 'should work with two values' do
5
+ it 'should work with two positive values' do
6
6
  point = GosuEnhanced::Point.new( 10, 20 )
7
7
  expect( point.x ).to eq 10
8
8
  expect( point.y ).to eq 20
9
9
  end
10
+
11
+ it 'should work with negative values' do
12
+ point = GosuEnhanced::Point.new( -10, 20 )
13
+ expect( point.x ).to eq -10
14
+ expect( point.y ).to eq 20
15
+
16
+ point = GosuEnhanced::Point.new( 10, -20 )
17
+ expect( point.x ).to eq 10
18
+ expect( point.y ).to eq -20
19
+
20
+ point = GosuEnhanced::Point.new( -10, -20 )
21
+ expect( point.x ).to eq -10
22
+ expect( point.y ).to eq -20
23
+ end
10
24
  end
11
25
 
12
26
  describe '#offset' do
@@ -0,0 +1,152 @@
1
+ require 'spec_helper'
2
+
3
+ describe GosuEnhanced::Region do
4
+ let( :point ) { GosuEnhanced::Point.new( 10, 20 ) }
5
+ let( :size ) { GosuEnhanced::Size.new( 30, 40 ) }
6
+
7
+ describe '#y' do
8
+ it 'should return the top (y) co-ordinate' do |variable|
9
+ reg = GosuEnhanced::Region.new( point, size )
10
+ expect( reg.y ).to eq 20
11
+ end
12
+ end
13
+
14
+ describe '#x' do
15
+ it 'should return the left (x) co-ordinate' do |variable|
16
+ reg = GosuEnhanced::Region.new( point, size )
17
+ expect( reg.x ).to eq 10
18
+ end
19
+ end
20
+
21
+ describe '#width' do
22
+ it 'should return the width' do |variable|
23
+ reg = GosuEnhanced::Region.new( point, size )
24
+ expect( reg.width ).to eq 30
25
+ end
26
+ end
27
+
28
+ describe '#height' do
29
+ it 'should return the height' do |variable|
30
+ reg = GosuEnhanced::Region.new( point, size )
31
+ expect( reg.height ).to eq 40
32
+ end
33
+ end
34
+
35
+ describe '#move_by!' do
36
+ it 'should work with a Point' do
37
+ reg = GosuEnhanced::Region.new( point, size )
38
+ reg.move_by!( GosuEnhanced::Point.new( 30, 40 ) )
39
+ expect( reg.left ).to eq 40
40
+ expect( reg.top ).to eq 60
41
+
42
+ expect( point.x ).to eq 10
43
+ expect( point.y ).to eq 20
44
+ end
45
+
46
+ it 'should work with two positive values' do
47
+ reg = GosuEnhanced::Region.new( point, size )
48
+ reg.move_by!( 40, 50 )
49
+ expect( reg.left ).to eq 50
50
+ expect( reg.top ).to eq 70
51
+ end
52
+
53
+ it 'should work with two negative values' do
54
+ reg = GosuEnhanced::Region.new( point, size )
55
+ reg.move_by!( -5, -10 )
56
+ expect( reg.left ).to eq 5
57
+ expect( reg.top ).to eq 10
58
+ end
59
+
60
+ it 'should work with values that result in negative co-ordinates' do
61
+ reg = GosuEnhanced::Region.new( point, size )
62
+ reg.move_by!( -80, -150 )
63
+ expect( reg.left ).to eq -70
64
+ expect( reg.top ).to eq -130
65
+ end
66
+ end
67
+
68
+ describe '#move_to!' do
69
+ it 'should work with a Point' do
70
+ reg = GosuEnhanced::Region.new( point, size )
71
+ reg.move_to!( GosuEnhanced::Point.new( 30, 40 ) )
72
+ expect( reg.left ).to eq 30
73
+ expect( reg.top ).to eq 40
74
+
75
+ expect( point.x ).to eq 10
76
+ expect( point.y ).to eq 20
77
+ end
78
+
79
+ it 'should work with two positive values' do
80
+ reg = GosuEnhanced::Region.new( point, size )
81
+ reg.move_to!( 40, 50 )
82
+ expect( reg.left ).to eq 40
83
+ expect( reg.top ).to eq 50
84
+ end
85
+
86
+ it 'should work with two negative values' do
87
+ reg = GosuEnhanced::Region.new( point, size )
88
+ reg.move_to!( -5, -10 )
89
+ expect( reg.left ).to eq -5
90
+ expect( reg.top ).to eq -10
91
+ end
92
+
93
+ describe '#inflate!' do
94
+ it 'should work with a Size' do
95
+ reg = GosuEnhanced::Region.new( point, size )
96
+ reg.inflate!( GosuEnhanced::Size.new( 30, 40 ) )
97
+ expect( reg.width ).to eq 60
98
+ expect( reg.height ).to eq 80
99
+
100
+ expect( size.width ).to eq 30
101
+ expect( size.height ).to eq 40
102
+ end
103
+
104
+ it 'should work with two values' do
105
+ reg = GosuEnhanced::Region.new( point, size )
106
+ reg.inflate!( 40, 50 )
107
+ expect( reg.width ).to eq 70
108
+ expect( reg.height ).to eq 90
109
+ end
110
+
111
+ it 'should work with negative values' do
112
+ reg = GosuEnhanced::Region.new( point, size )
113
+ reg.inflate!( -20, -35 )
114
+ expect( reg.width ).to eq 10
115
+ expect( reg.height ).to eq 5
116
+ end
117
+
118
+ it 'should raise an error with out-of-range values' do
119
+ reg = GosuEnhanced::Region.new( point, size )
120
+ expect { reg.inflate!( -50, -20 ) }.to raise_error Exception
121
+ expect { reg.inflate!( -25, -60 ) }.to raise_error Exception
122
+ expect { reg.inflate!( -50, -70 ) }.to raise_error Exception
123
+ end
124
+ end
125
+
126
+ describe '#deflate!' do
127
+ it 'should work with a Size' do
128
+ reg = GosuEnhanced::Region.new( point, size )
129
+ reg.deflate!( GosuEnhanced::Size.new( 20, 35 ) )
130
+ expect( reg.width ).to eq 10
131
+ expect( reg.height ).to eq 5
132
+
133
+ expect( size.width ).to eq 30
134
+ expect( size.height ).to eq 40
135
+ end
136
+
137
+ it 'should work with two values' do
138
+ reg = GosuEnhanced::Region.new( point, size )
139
+ reg.deflate!( 10, 25 )
140
+ expect( reg.width ).to eq 20
141
+ expect( reg.height ).to eq 15
142
+ end
143
+
144
+ it 'should raise an error for out-of-range values' do
145
+ reg = GosuEnhanced::Region.new( point, size )
146
+ expect { reg.deflate!( 25, 50 ) }.to raise_error Exception
147
+ expect { reg.deflate!( 60, 35 ) }.to raise_error Exception
148
+ expect { reg.deflate!( 80, 70 ) }.to raise_error Exception
149
+ end
150
+ end
151
+ end
152
+ end
@@ -0,0 +1,82 @@
1
+ require 'spec_helper'
2
+
3
+ describe GosuEnhanced::Region do
4
+ let( :point ) { GosuEnhanced::Point.new( 10, 20 ) }
5
+ let( :size ) { GosuEnhanced::Size.new( 30, 40 ) }
6
+
7
+ describe '#initialize' do
8
+ it 'should work with a Point and a Size' do
9
+ reg = GosuEnhanced::Region.new( point, size )
10
+ end
11
+ end
12
+
13
+ describe '#position' do
14
+ it 'should return the position as a Point' do
15
+ reg = GosuEnhanced::Region.new( point, size )
16
+ expect( reg.position ).to eq GosuEnhanced::Point.new( 10, 20 )
17
+ end
18
+ end
19
+
20
+ describe '#top' do
21
+ it 'should return the top co-ordinate' do |variable|
22
+ reg = GosuEnhanced::Region.new( point, size )
23
+ expect( reg.top ).to eq 20
24
+ end
25
+ end
26
+
27
+ describe '#left' do
28
+ it 'should return the left co-ordinate' do |variable|
29
+ reg = GosuEnhanced::Region.new( point, size )
30
+ expect( reg.left ).to eq 10
31
+ end
32
+ end
33
+
34
+ describe '#size' do
35
+ it 'should return the size as a Size' do
36
+ reg = GosuEnhanced::Region.new( point, size )
37
+ expect( reg.size ).to eq GosuEnhanced::Size.new( 30, 40 )
38
+ end
39
+ end
40
+
41
+ describe '#contains?' do
42
+ it 'should work with two co-ordinates' do
43
+ reg = GosuEnhanced::Region.new( point, size )
44
+ expect( reg.contains?( 10, 20 ) ).to be true
45
+ expect( reg.contains?( 20, 35 ) ).to be true
46
+ expect( reg.contains?( 29, 59 ) ).to be true
47
+ expect( reg.contains?( 30, 60 ) ).to be false
48
+ end
49
+
50
+ it 'should work with a Point' do
51
+ reg = GosuEnhanced::Region.new( point, size )
52
+ expect( reg.contains?( GosuEnhanced::Point.new( 10, 20 ) ) ).to be true
53
+ expect( reg.contains?( GosuEnhanced::Point.new( 20, 35 ) ) ).to be true
54
+ expect( reg.contains?( GosuEnhanced::Point.new( 29, 59 ) ) ).to be true
55
+ expect( reg.contains?( GosuEnhanced::Point.new( 30, 60 ) ) ).to be false
56
+ end
57
+ end
58
+
59
+ # Unlike the Point and Size, a Region needs a dup override to actually return
60
+ # a duplicate correctly.
61
+
62
+ describe '#dup' do
63
+ it 'should duplicate a region' do
64
+ reg = GosuEnhanced::Region.new( point, size )
65
+ new_reg = reg.dup
66
+
67
+ expect( reg.position ).to eq GosuEnhanced::Point.new( 10, 20 )
68
+ expect( reg.size ).to eq GosuEnhanced::Size.new( 30, 40 )
69
+
70
+ expect( new_reg.position ).to eq GosuEnhanced::Point.new( 10, 20 )
71
+ expect( new_reg.size ).to eq GosuEnhanced::Size.new( 30, 40 )
72
+
73
+ new_reg.move_to!( 20, 30 )
74
+ new_reg.inflate!( 10, 10 )
75
+ expect( new_reg.position ).to eq GosuEnhanced::Point.new( 20, 30 )
76
+ expect( new_reg.size ).to eq GosuEnhanced::Size.new( 40, 50 )
77
+
78
+ expect( reg.position ).to eq GosuEnhanced::Point.new( 10, 20 )
79
+ expect( reg.size ).to eq GosuEnhanced::Size.new( 30, 40 )
80
+ end
81
+ end
82
+ end
data/spec/size_spec.rb CHANGED
@@ -7,6 +7,12 @@ describe GosuEnhanced::Size do
7
7
  expect( size.width ).to eq 10
8
8
  expect( size.height ).to eq 20
9
9
  end
10
+
11
+ it 'should reject negative values' do
12
+ expect { size = GosuEnhanced::Size.new( -10, 20 ) }.to raise_error Exception
13
+ expect { size = GosuEnhanced::Size.new( 10, -20 ) }.to raise_error Exception
14
+ expect { size = GosuEnhanced::Size.new( -10, -20 ) }.to raise_error Exception
15
+ end
10
16
  end
11
17
 
12
18
  describe '#inflate' do
@@ -24,7 +30,7 @@ describe GosuEnhanced::Size do
24
30
  expect( esize.height ).to eq 70
25
31
  end
26
32
 
27
- it 'should work with two explicitly negative values' do
33
+ it 'should work with negative values' do
28
34
  size = GosuEnhanced::Size.new( 40, 50 )
29
35
  esize = size.inflate( -30, -20 )
30
36
  expect( esize.width ).to eq 10
@@ -77,7 +83,7 @@ describe GosuEnhanced::Size do
77
83
  expect( size.height ).to eq 70
78
84
  end
79
85
 
80
- it 'should work with two explicitly negative values' do
86
+ it 'should work with negative values' do
81
87
  size = GosuEnhanced::Size.new( 40, 50 )
82
88
  size.inflate!( -30, -20 )
83
89
  expect( size.width ).to eq 10
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: gosu_enhanced
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Julian Nicholls
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-04-12 00:00:00.000000000 Z
11
+ date: 2014-04-18 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: gosu
@@ -94,10 +94,10 @@ dependencies:
94
94
  - - "~>"
95
95
  - !ruby/object:Gem::Version
96
96
  version: '0.9'
97
- description: "Point and Size classes to hold pixel addresses and \nrectangle sizes
98
- respectively. Updated window class to draw rectangle in a single \ncolour more easily.
99
- Updated font class to measure text and return co-ordinates \nto centre a piece of
100
- text in a rectangle size."
97
+ description: "Point, Size, and Region classes to hold pixel addresses and \nrectangle
98
+ sizes respectively. Updated window class to draw rectangle in a single \ncolour
99
+ more easily. Updated font class to measure text and return co-ordinates \nto centre
100
+ a piece of text in a rectangle size."
101
101
  email:
102
102
  - juliannicholls29@gmail.com
103
103
  executables: []
@@ -114,12 +114,15 @@ files:
114
114
  - lib/gosu_enhanced.rb
115
115
  - lib/gosu_enhanced/enhanced.rb
116
116
  - lib/gosu_enhanced/point.rb
117
+ - lib/gosu_enhanced/region.rb
117
118
  - lib/gosu_enhanced/size.rb
118
119
  - lib/gosu_enhanced/version.rb
119
120
  - spec/point_spec.rb
121
+ - spec/region_delegation_spec.rb
122
+ - spec/region_spec.rb
120
123
  - spec/size_spec.rb
121
124
  - spec/spec_helper.rb
122
- homepage: ''
125
+ homepage: https://github.com/JulianNicholls/gosu_enhanced-gem
123
126
  licenses:
124
127
  - MIT
125
128
  metadata: {}
@@ -145,5 +148,7 @@ specification_version: 4
145
148
  summary: Enhanced versions of some Gosu classes.
146
149
  test_files:
147
150
  - spec/point_spec.rb
151
+ - spec/region_delegation_spec.rb
152
+ - spec/region_spec.rb
148
153
  - spec/size_spec.rb
149
154
  - spec/spec_helper.rb