edu_draw 1.0.1 → 2.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/README.md +67 -2
- data/examples/animations.rb +26 -0
- data/examples/areas.rb +22 -0
- data/lib/edu_draw.rb +1 -0
- data/lib/edu_draw/animation_pen.rb +65 -0
- data/lib/edu_draw/pen.rb +95 -20
- data/lib/edu_draw/sheet.rb +40 -19
- data/test/edu_draw/animation_pen_test.rb +16 -0
- data/test/edu_draw/basic_pen_tests.rb +71 -0
- data/test/edu_draw/pen_test.rb +7 -53
- data/test/edu_draw/sheet_test.rb +7 -1
- data/test/test_helper.rb +5 -0
- metadata +20 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: dfc3b98dd75cc8bba96e5c4232b6fd63af037220
|
4
|
+
data.tar.gz: 7ad1c99a69f9146c9ec8b9c2fdc8d3d58c1591ea
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 94cdaeefbc9b682466f99041b32632e0fcb167f92140cd4d363d6cb67c6926e52b8a25f14d7191a7bf636207213aa7c4e23839bbf1a0f8bc1da7b0bea93d3851
|
7
|
+
data.tar.gz: 1516ae2a65123eebbe63a900f9500d744635c320cf71c79e08b34ce00d17c59f085cce7a65d6afd42ce9c25613e7818e790e1ac89254e79cadff63cc1d6ac10e
|
data/README.md
CHANGED
@@ -41,10 +41,10 @@ When you are done drawing, you have to make one final call to the sheet, to actu
|
|
41
41
|
|
42
42
|
The whole program looks like this:
|
43
43
|
|
44
|
-
```
|
44
|
+
```ruby
|
45
45
|
require "edu_draw"
|
46
46
|
|
47
|
-
sheet = EduDraw::Sheet.new x: 500, y: 600, title: "
|
47
|
+
sheet = EduDraw::Sheet.new x: 500, y: 600, title: "A beautiful rectangle"
|
48
48
|
green_pen = sheet.new_pen(x: 100, y: 20)
|
49
49
|
|
50
50
|
green_pen.move 100
|
@@ -58,6 +58,71 @@ green_pen.move 80
|
|
58
58
|
sheet.show
|
59
59
|
```
|
60
60
|
|
61
|
+
### Areas / Filled shapes
|
62
|
+
You can also draw filled or party-filled shapes. Here is an short example, that can be also found in the examples folder:
|
63
|
+
```ruby
|
64
|
+
require "edu_draw"
|
65
|
+
|
66
|
+
sheet = EduDraw::Sheet.new(x: 150, y: 150)
|
67
|
+
pen = sheet.new_pen(x: 52, y: 10)
|
68
|
+
|
69
|
+
# Every move in the fill-block will create a filled a shape
|
70
|
+
pen.fill do
|
71
|
+
5.times do
|
72
|
+
pen.move 40
|
73
|
+
pen.turn_right 36
|
74
|
+
end
|
75
|
+
end
|
76
|
+
|
77
|
+
#Every move out of the fill block just draws a line
|
78
|
+
# Those 10 moves result in a half-filled decagon
|
79
|
+
5.times do
|
80
|
+
pen.move 40
|
81
|
+
pen.turn_right 36
|
82
|
+
end
|
83
|
+
|
84
|
+
|
85
|
+
sheet.show
|
86
|
+
```
|
87
|
+
|
88
|
+
### Animations
|
89
|
+
You can even draw animated shapes. Here is an short example, that can be also found in the examples folder:
|
90
|
+
```ruby
|
91
|
+
require "edu_draw"
|
92
|
+
|
93
|
+
sheet = EduDraw::Sheet.new(x: 150, y: 150)
|
94
|
+
|
95
|
+
# Instead of using sheet.new_pen, now you create a new_animation_pen
|
96
|
+
pen = sheet.new_animation_pen(x: 75, y: 75)
|
97
|
+
|
98
|
+
# Here you define what happens in the beginning of each frame
|
99
|
+
pen.each_frame do
|
100
|
+
pen.turn_right 1
|
101
|
+
end
|
102
|
+
|
103
|
+
# Now you define what the pen should draw each frame
|
104
|
+
# It is important to leave the pen in a state, where
|
105
|
+
# it can start drawing on the next frame, that's why
|
106
|
+
# the pens moves back, even though it does not draw
|
107
|
+
# any actual lines.
|
108
|
+
pen.draw_frame do
|
109
|
+
pen.move 50
|
110
|
+
pen.move -50
|
111
|
+
end
|
112
|
+
|
113
|
+
|
114
|
+
# You're set. No just show the sheet as usual and enjoy
|
115
|
+
# the wonderfully spinning line you just drew.
|
116
|
+
sheet.show
|
117
|
+
```
|
118
|
+
|
119
|
+
## Changelog
|
120
|
+
* 2.0.0
|
121
|
+
* Adds ability to fill shapes. See `examples/areas.rb`
|
122
|
+
* Adds ability to draw animated shapes. See `examples/animations.rb`
|
123
|
+
* Makes state of pen writable
|
124
|
+
* Greatly improves documentation
|
125
|
+
|
61
126
|
## Contributing
|
62
127
|
Feel free to contribute anything, may it be better tests, better documentation or more features. As you can see, I tried to keep the code tidy so it is appreciated if you'd do the same. Just add a pull request and we will make it work. Oh, and don't forget to have fun ;)
|
63
128
|
|
@@ -0,0 +1,26 @@
|
|
1
|
+
require "edu_draw"
|
2
|
+
|
3
|
+
sheet = EduDraw::Sheet.new(x: 150, y: 150)
|
4
|
+
|
5
|
+
# Instead of using sheet.new_pen, now you create a new_animation_pen
|
6
|
+
pen = sheet.new_animation_pen(x: 75, y: 75)
|
7
|
+
|
8
|
+
# Here you define what happens in the beginning of each frame
|
9
|
+
pen.each_frame do
|
10
|
+
pen.turn_right 1
|
11
|
+
end
|
12
|
+
|
13
|
+
# Now you define what the pen should draw each frame
|
14
|
+
# It is important to leave the pen in a state, where
|
15
|
+
# it can start drawing on the next frame, that's why
|
16
|
+
# the pens moves back, even though it does not draw
|
17
|
+
# any actual lines.
|
18
|
+
pen.draw_frame do
|
19
|
+
pen.move 50
|
20
|
+
pen.move -50
|
21
|
+
end
|
22
|
+
|
23
|
+
|
24
|
+
# You're set. No just show the sheet as usual and enjoy
|
25
|
+
# the wonderfully spinning line you just drew.
|
26
|
+
sheet.show
|
data/examples/areas.rb
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
require "edu_draw"
|
2
|
+
|
3
|
+
sheet = EduDraw::Sheet.new(x: 150, y: 150)
|
4
|
+
pen = sheet.new_pen(x: 52, y: 10)
|
5
|
+
|
6
|
+
# Every move in the fill-block will create a filled a shape
|
7
|
+
pen.fill do
|
8
|
+
5.times do
|
9
|
+
pen.move 40
|
10
|
+
pen.turn_right 36
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
#Every move out of the fill block just draws a line
|
15
|
+
# Those 10 moves result in a half-filled decagon
|
16
|
+
5.times do
|
17
|
+
pen.move 40
|
18
|
+
pen.turn_right 36
|
19
|
+
end
|
20
|
+
|
21
|
+
|
22
|
+
sheet.show
|
data/lib/edu_draw.rb
CHANGED
@@ -0,0 +1,65 @@
|
|
1
|
+
module EduDraw
|
2
|
+
# An AnimatedPen is a special {Pen} that is used for drawing animated shapes
|
3
|
+
class AnimationPen < Pen
|
4
|
+
|
5
|
+
# @private
|
6
|
+
# Creates a new AnimationPen
|
7
|
+
# @overload initialize(x, y, angle, color)
|
8
|
+
# @param (see Pen#initialize)
|
9
|
+
def initialize(*args)
|
10
|
+
super
|
11
|
+
@update_block = -> {}
|
12
|
+
@draw_block = -> {}
|
13
|
+
end
|
14
|
+
|
15
|
+
# Defines what needs to be done every frame before drawing
|
16
|
+
#
|
17
|
+
# @example
|
18
|
+
# # This makes the pen turn around slowly
|
19
|
+
# pen.each_frame do
|
20
|
+
# pen.turn_right 1
|
21
|
+
# end
|
22
|
+
def each_frame &block
|
23
|
+
@update_block = block
|
24
|
+
end
|
25
|
+
|
26
|
+
# Defines what the pen needs to draw every frame
|
27
|
+
#
|
28
|
+
# @note Every {#move} outside this block will draw only in the first frame
|
29
|
+
#
|
30
|
+
# @note Remember to set the state of the pen so that it can start drawing the next frame.
|
31
|
+
# In the given example the last `pen.turn_right 90` is not neccessary for the shape,
|
32
|
+
# but without it, the pen would draw the rectangle in a different direction on the next frame.
|
33
|
+
# Maybe, this is what you need, but it is still recommended to do these changes in the
|
34
|
+
# {#each_frame}-block.
|
35
|
+
#
|
36
|
+
# @example
|
37
|
+
# # Draw a rectangle
|
38
|
+
# pen.draw_frame do
|
39
|
+
# pen.move 20
|
40
|
+
# pen.turn_right 90
|
41
|
+
# pen.move 10
|
42
|
+
# pen.turn_right 90
|
43
|
+
# pen.move 20
|
44
|
+
# pen.turn_right 90
|
45
|
+
# pen.move 10
|
46
|
+
# pen.turn_right 90
|
47
|
+
# end
|
48
|
+
def draw_frame &block
|
49
|
+
@draw_block = block
|
50
|
+
end
|
51
|
+
|
52
|
+
# @private
|
53
|
+
def update
|
54
|
+
empty_shapes
|
55
|
+
@update_block.call
|
56
|
+
@draw_block.call
|
57
|
+
end
|
58
|
+
|
59
|
+
private
|
60
|
+
|
61
|
+
def empty_shapes
|
62
|
+
shapes.clear
|
63
|
+
end
|
64
|
+
end
|
65
|
+
end
|
data/lib/edu_draw/pen.rb
CHANGED
@@ -2,30 +2,39 @@ module EduDraw
|
|
2
2
|
# A Pen is a drawing tool that provides basic drawing functionalities on a 2d {Sheet}.
|
3
3
|
class Pen
|
4
4
|
|
5
|
+
# Tolerance with which to consider two points the same
|
6
|
+
PIXEL_TOLERANCE = 0.2
|
7
|
+
private_constant :PIXEL_TOLERANCE
|
8
|
+
|
9
|
+
# Shapes to be drawn by sheet
|
10
|
+
# @private
|
11
|
+
attr_reader :shapes
|
12
|
+
|
5
13
|
# @return [Fixnum] x-coordinate of position in pixel where left is 0
|
6
|
-
|
14
|
+
attr_accessor :x
|
7
15
|
|
8
16
|
# @return [Fixnum] y-coordinate of position in pixel where top is 0
|
9
|
-
|
17
|
+
attr_accessor :y
|
10
18
|
|
11
19
|
# @return [Fixnum] Direction of the pen in degree where 0 points to the right
|
12
|
-
|
20
|
+
attr_accessor :angle
|
13
21
|
|
14
22
|
# @return [Gosu::Color] Color of the pen
|
15
|
-
|
23
|
+
attr_accessor :color
|
16
24
|
|
17
|
-
# Creates a new
|
25
|
+
# Creates a new Pen
|
18
26
|
#
|
19
|
-
# @param x [Fixnum]
|
20
|
-
# @param y [Fixnum]
|
21
|
-
# @param angle [Fixnum]
|
22
|
-
# @param color [Gosu::Color]
|
23
|
-
def initialize(
|
27
|
+
# @param x [Fixnum] Initial value of {#x}
|
28
|
+
# @param y [Fixnum] Initial value of {#y}
|
29
|
+
# @param angle [Fixnum] Initial value of {#angle}
|
30
|
+
# @param color [Gosu::Color] Initial value of {#color}
|
31
|
+
def initialize(x: 0, y: 0, angle: 0, color: Gosu::Color::GREEN)
|
24
32
|
@x = x
|
25
33
|
@y = y
|
26
34
|
@angle = angle
|
27
35
|
@color = color
|
28
|
-
@
|
36
|
+
@shapes = []
|
37
|
+
@to_be_filled = []
|
29
38
|
|
30
39
|
down!
|
31
40
|
end
|
@@ -42,7 +51,7 @@ module EduDraw
|
|
42
51
|
# Changes the direction of self to be turned to the right
|
43
52
|
# The {#angle} stays within 0..364
|
44
53
|
#
|
45
|
-
# @param
|
54
|
+
# @param (see #turn_left)
|
46
55
|
def turn_right(degree)
|
47
56
|
turn_left -degree
|
48
57
|
end
|
@@ -52,23 +61,23 @@ module EduDraw
|
|
52
61
|
@up
|
53
62
|
end
|
54
63
|
|
55
|
-
# If the pen is
|
64
|
+
# If the pen is down, it touches the {Sheet} and is drawing when moved.
|
56
65
|
def down?
|
57
66
|
!up?
|
58
67
|
end
|
59
68
|
|
60
69
|
# Lifts the pen from the {Sheet}
|
61
70
|
#
|
62
|
-
# @see
|
63
|
-
# @see
|
71
|
+
# @see #up?
|
72
|
+
# @see #down?
|
64
73
|
def up!
|
65
74
|
@up = true
|
66
75
|
end
|
67
76
|
|
68
77
|
# Sticks the pen to the {Sheet}
|
69
78
|
#
|
70
|
-
# @see
|
71
|
-
# @see
|
79
|
+
# @see #up?
|
80
|
+
# @see #down?
|
72
81
|
def down!
|
73
82
|
@up = false
|
74
83
|
end
|
@@ -80,9 +89,7 @@ module EduDraw
|
|
80
89
|
def move(length)
|
81
90
|
target_x = x + Math.cos(angle_in_rad) * length
|
82
91
|
target_y = y + Math.sin(angle_in_rad) * length
|
83
|
-
|
84
|
-
@sheet.shapes << [:draw_line, x, y, color, target_x, target_y, color]
|
85
|
-
end
|
92
|
+
handle_movement x, y, target_x, target_y
|
86
93
|
@x = target_x
|
87
94
|
@y = target_y
|
88
95
|
end
|
@@ -91,5 +98,73 @@ module EduDraw
|
|
91
98
|
def angle_in_rad
|
92
99
|
angle * Math::PI / 180.0
|
93
100
|
end
|
101
|
+
|
102
|
+
# Draws filled shapes.
|
103
|
+
#
|
104
|
+
# @note If the pen does not end where it started, the
|
105
|
+
# pen will assume a connection between start and end point.
|
106
|
+
# This does not affect the actual position and direction
|
107
|
+
# of the pen.
|
108
|
+
#
|
109
|
+
# @example
|
110
|
+
# # This draws a triangle
|
111
|
+
# pen.fill do
|
112
|
+
# pen.move 10
|
113
|
+
# pen.turn_right 90
|
114
|
+
# pen.move 10
|
115
|
+
# end
|
116
|
+
def fill(&block)
|
117
|
+
@to_be_filled << [x,y]
|
118
|
+
yield
|
119
|
+
if starting_point_is_end_point?
|
120
|
+
@to_be_filled.delete_at(0)
|
121
|
+
end
|
122
|
+
origin_x, origin_y = @to_be_filled.first
|
123
|
+
@to_be_filled[1..@to_be_filled.length-1].each_cons(2) do |point_a, point_b|
|
124
|
+
@shapes << [:draw_triangle,
|
125
|
+
origin_x, origin_y, color,
|
126
|
+
point_a[0], point_a[1], color,
|
127
|
+
point_b[0], point_b[1], color]
|
128
|
+
end
|
129
|
+
@to_be_filled.clear
|
130
|
+
end
|
131
|
+
|
132
|
+
# Hook method
|
133
|
+
# @private
|
134
|
+
def update
|
135
|
+
# nothing to do since nothing ever changes
|
136
|
+
end
|
137
|
+
|
138
|
+
private
|
139
|
+
|
140
|
+
|
141
|
+
# True, if pen is currently in a {#fill}-block
|
142
|
+
def fill_mode?
|
143
|
+
@to_be_filled.length > 0
|
144
|
+
end
|
145
|
+
|
146
|
+
# Does whatever needs to be done upon movement.
|
147
|
+
# This depends on the state of the pen.
|
148
|
+
# up or down? fill mode or regular mode?
|
149
|
+
def handle_movement(x, y, target_x, target_y)
|
150
|
+
if down?
|
151
|
+
if fill_mode?
|
152
|
+
@to_be_filled << [target_x, target_y]
|
153
|
+
else
|
154
|
+
@shapes << [:draw_line, x, y, color, target_x, target_y, color]
|
155
|
+
end
|
156
|
+
end
|
157
|
+
end
|
158
|
+
|
159
|
+
# Checks whether the pen came back to its starting point during
|
160
|
+
# the {#fill}-block.
|
161
|
+
def starting_point_is_end_point?
|
162
|
+
start_x = @to_be_filled.first[0]
|
163
|
+
start_y = @to_be_filled.first[1]
|
164
|
+
end_x = @to_be_filled.last[0]
|
165
|
+
end_y = @to_be_filled.last[1]
|
166
|
+
(start_x - end_x).abs <= PIXEL_TOLERANCE and
|
167
|
+
(start_y - end_y).abs <= PIXEL_TOLERANCE
|
168
|
+
end
|
94
169
|
end
|
95
170
|
end
|
data/lib/edu_draw/sheet.rb
CHANGED
@@ -3,7 +3,9 @@ module EduDraw
|
|
3
3
|
class Sheet < Gosu::Window
|
4
4
|
|
5
5
|
# @private
|
6
|
-
|
6
|
+
# All pens created by and for this sheet
|
7
|
+
attr_reader :pens
|
8
|
+
private :pens
|
7
9
|
|
8
10
|
# Creates a new Sheet
|
9
11
|
#
|
@@ -13,39 +15,58 @@ module EduDraw
|
|
13
15
|
def initialize(x: 100, y: 100, title: "A blank sheet")
|
14
16
|
super x, y, false
|
15
17
|
self.caption = title
|
16
|
-
@
|
18
|
+
@pens = []
|
17
19
|
end
|
18
20
|
|
19
|
-
# @private
|
20
|
-
def needs_cursor?
|
21
|
-
true
|
22
|
-
end
|
23
21
|
|
24
22
|
# Creates a new {Pen} that draws on self
|
25
23
|
#
|
26
|
-
# @see
|
24
|
+
# @see Pen
|
27
25
|
#
|
28
26
|
# @param x [Fixnum] x-coordinate of starting position. Left is 0.
|
29
27
|
# @param y [Fixnum] y-coordinate of starting position. Top is 0.
|
30
28
|
# @param angle [Fixnum] Direction of pen in degree. 0 points to the right.
|
31
29
|
# @param color [Gosu::Color] Color of the pen
|
32
30
|
def new_pen(x: 0, y: 0, angle: 0, color: Gosu::Color::GREEN)
|
33
|
-
Pen.new(
|
31
|
+
pen = Pen.new(x: x, y: y, angle: angle, color: color)
|
32
|
+
pens << pen
|
33
|
+
pen
|
34
34
|
end
|
35
35
|
|
36
|
-
#
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
36
|
+
# Create a new {AnimationPen} that draws something different each frame
|
37
|
+
#
|
38
|
+
# @see AnimationPen
|
39
|
+
#
|
40
|
+
# @param (see #new_pen)
|
41
|
+
def new_animation_pen(x: 0, y: 0, angle: 0, color: Gosu::Color::GREEN)
|
42
|
+
pen = AnimationPen.new(x: x, y: y, angle: angle, color: color)
|
43
|
+
pens << pen
|
44
|
+
pen
|
42
45
|
end
|
43
46
|
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
47
|
+
private
|
48
|
+
# Makes gosu display the system mouse cursor
|
49
|
+
def needs_cursor?
|
50
|
+
true
|
51
|
+
end
|
52
|
+
|
53
|
+
# Gosu hook method for drawing window content
|
54
|
+
def draw
|
55
|
+
pens.map(&:shapes).each do |shapes|
|
56
|
+
shapes.each do |shape|
|
57
|
+
method,*args = shape
|
58
|
+
send method, *args
|
59
|
+
end
|
60
|
+
end
|
61
|
+
end
|
62
|
+
|
63
|
+
# Gosu hook method for updating game state before drawing
|
64
|
+
# This is called once per frame
|
65
|
+
def update
|
66
|
+
pens.each &:update
|
67
|
+
if button_down? Gosu::KbEscape
|
68
|
+
close
|
69
|
+
end
|
48
70
|
end
|
49
|
-
end
|
50
71
|
end
|
51
72
|
end
|
@@ -0,0 +1,16 @@
|
|
1
|
+
require_relative "../test_helper"
|
2
|
+
module EduDraw
|
3
|
+
class AnimationPenTest < Minitest::Unit::TestCase
|
4
|
+
include BasicPenTests
|
5
|
+
|
6
|
+
def setup
|
7
|
+
@pen = AnimationPen.new(x: 10, y: 20, angle: 25, color: Gosu::Color::BLACK)
|
8
|
+
end
|
9
|
+
|
10
|
+
def test_update_removes_shapes
|
11
|
+
pen_shapes << Object.new
|
12
|
+
@pen.send :update
|
13
|
+
assert_empty pen_shapes
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
@@ -0,0 +1,71 @@
|
|
1
|
+
module EduDraw
|
2
|
+
module BasicPenTests
|
3
|
+
def pen_shapes
|
4
|
+
@pen.instance_variable_get :@shapes
|
5
|
+
end
|
6
|
+
|
7
|
+
def test_initialize
|
8
|
+
assert_equal 10, @pen.x
|
9
|
+
assert_equal 20, @pen.y
|
10
|
+
assert_equal 25, @pen.angle
|
11
|
+
assert_equal Gosu::Color::BLACK, @pen.color
|
12
|
+
end
|
13
|
+
|
14
|
+
def test_move
|
15
|
+
@pen.move 100
|
16
|
+
expected_x = 10 + Math.cos(25 * Math::PI / 180.0) * 100
|
17
|
+
expected_y = 20 + Math.sin(25 * Math::PI / 180.0) * 100
|
18
|
+
assert_equal expected_x, @pen.x # x changed
|
19
|
+
assert_equal expected_y, @pen.y # y changed
|
20
|
+
assert_equal 25, @pen.angle # angle unchanged
|
21
|
+
assert_equal Gosu::Color::BLACK, @pen.color # color unchanged
|
22
|
+
end
|
23
|
+
|
24
|
+
def test_doesnt_draws_when_up
|
25
|
+
@pen.up!
|
26
|
+
@pen.move 100
|
27
|
+
assert_empty pen_shapes
|
28
|
+
end
|
29
|
+
|
30
|
+
def test_up_and_down
|
31
|
+
assert @pen.down?
|
32
|
+
refute @pen.up?
|
33
|
+
|
34
|
+
@pen.up!
|
35
|
+
assert @pen.up?
|
36
|
+
refute @pen.down?
|
37
|
+
|
38
|
+
@pen.down!
|
39
|
+
assert @pen.down?
|
40
|
+
refute @pen.up?
|
41
|
+
end
|
42
|
+
|
43
|
+
def test_turning
|
44
|
+
assert_equal 25, @pen.angle
|
45
|
+
|
46
|
+
@pen.turn_left 25
|
47
|
+
assert_equal 0, @pen.angle
|
48
|
+
|
49
|
+
@pen.turn_left 10
|
50
|
+
assert_equal 350, @pen.angle
|
51
|
+
|
52
|
+
@pen.turn_right 20
|
53
|
+
assert_equal 10, @pen.angle
|
54
|
+
end
|
55
|
+
|
56
|
+
def test_angle_in_rad
|
57
|
+
assert_equal 25 * Math::PI / 180.0, @pen.angle_in_rad
|
58
|
+
end
|
59
|
+
|
60
|
+
def test_area
|
61
|
+
assert_respond_to @pen, :fill
|
62
|
+
@pen.fill do
|
63
|
+
4.times do
|
64
|
+
@pen.move 10
|
65
|
+
@pen.turn_right 90
|
66
|
+
end
|
67
|
+
end
|
68
|
+
assert_equal 2, pen_shapes.length # 2 rectangles
|
69
|
+
end
|
70
|
+
end
|
71
|
+
end
|
data/test/edu_draw/pen_test.rb
CHANGED
@@ -1,62 +1,16 @@
|
|
1
1
|
require_relative "../test_helper"
|
2
2
|
module EduDraw
|
3
3
|
class PenTest < Minitest::Unit::TestCase
|
4
|
-
|
5
|
-
@sheet_stub = stub shapes: []
|
6
|
-
@pen = Pen.new(@sheet_stub, x: 10, y: 20, angle: 25, color: Gosu::Color::BLACK)
|
7
|
-
end
|
8
|
-
|
9
|
-
def test_initialize
|
10
|
-
assert_equal 10, @pen.x
|
11
|
-
assert_equal 20, @pen.y
|
12
|
-
assert_equal 25, @pen.angle
|
13
|
-
assert_equal Gosu::Color::BLACK, @pen.color
|
14
|
-
end
|
15
|
-
|
16
|
-
def test_move
|
17
|
-
@pen.move 100
|
18
|
-
expected_x = 10 + Math.cos(25 * Math::PI / 180.0) * 100
|
19
|
-
expected_y = 20 + Math.sin(25 * Math::PI / 180.0) * 100
|
20
|
-
assert_equal expected_x, @pen.x # x changed
|
21
|
-
assert_equal expected_y, @pen.y # y changed
|
22
|
-
assert_equal 25, @pen.angle # angle unchanged
|
23
|
-
assert_equal Gosu::Color::BLACK, @pen.color # color unchanged
|
24
|
-
end
|
25
|
-
|
26
|
-
def test_doesnt_draws_when_up
|
27
|
-
@sheet_stub.expects(:shapes).times(0)
|
28
|
-
@pen.up!
|
29
|
-
@pen.move 100
|
30
|
-
end
|
4
|
+
include BasicPenTests
|
31
5
|
|
32
|
-
def
|
33
|
-
|
34
|
-
refute @pen.up?
|
35
|
-
|
36
|
-
@pen.up!
|
37
|
-
assert @pen.up?
|
38
|
-
refute @pen.down?
|
39
|
-
|
40
|
-
@pen.down!
|
41
|
-
assert @pen.down?
|
42
|
-
refute @pen.up?
|
43
|
-
end
|
44
|
-
|
45
|
-
def test_turning
|
46
|
-
assert_equal 25, @pen.angle
|
47
|
-
|
48
|
-
@pen.turn_left 25
|
49
|
-
assert_equal 0, @pen.angle
|
50
|
-
|
51
|
-
@pen.turn_left 10
|
52
|
-
assert_equal 350, @pen.angle
|
53
|
-
|
54
|
-
@pen.turn_right 20
|
55
|
-
assert_equal 10, @pen.angle
|
6
|
+
def setup
|
7
|
+
@pen = Pen.new(x: 10, y: 20, angle: 25, color: Gosu::Color::BLACK)
|
56
8
|
end
|
57
9
|
|
58
|
-
def
|
59
|
-
|
10
|
+
def test_update_does_not_remove_shapes
|
11
|
+
pen_shapes << Object.new
|
12
|
+
@pen.send :update
|
13
|
+
refute_empty pen_shapes
|
60
14
|
end
|
61
15
|
end
|
62
16
|
end
|
data/test/edu_draw/sheet_test.rb
CHANGED
@@ -12,7 +12,7 @@ module EduDraw
|
|
12
12
|
end
|
13
13
|
|
14
14
|
def test_cursor_visible
|
15
|
-
assert @sheet.needs_cursor?
|
15
|
+
assert @sheet.send :needs_cursor?
|
16
16
|
end
|
17
17
|
|
18
18
|
def test_window
|
@@ -26,8 +26,14 @@ module EduDraw
|
|
26
26
|
|
27
27
|
def test_new_pen_creates_new_instance
|
28
28
|
Pen.expects(:new).once
|
29
|
+
AnimationPen.expects(:new).never
|
29
30
|
@sheet.new_pen
|
30
31
|
end
|
31
32
|
|
33
|
+
def test_new_animation_pen
|
34
|
+
AnimationPen.expects(:new).never
|
35
|
+
Pen.expects(:new).once
|
36
|
+
@sheet.new_animation_pen
|
37
|
+
end
|
32
38
|
end
|
33
39
|
end
|
data/test/test_helper.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: edu_draw
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version:
|
4
|
+
version: 2.0.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Marvin Ede
|
@@ -38,6 +38,20 @@ dependencies:
|
|
38
38
|
- - "~>"
|
39
39
|
- !ruby/object:Gem::Version
|
40
40
|
version: '1.1'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: minitest-focus
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '1.1'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '1.1'
|
41
55
|
description: Simple object oriented API for opening a window and drawing in it. Based
|
42
56
|
on gosu. It's meant to teach beginners programming as a visualization of code can
|
43
57
|
help.
|
@@ -49,10 +63,15 @@ files:
|
|
49
63
|
- LICENSE
|
50
64
|
- README.md
|
51
65
|
- Rakefile
|
66
|
+
- examples/animations.rb
|
67
|
+
- examples/areas.rb
|
52
68
|
- examples/lines.rb
|
53
69
|
- lib/edu_draw.rb
|
70
|
+
- lib/edu_draw/animation_pen.rb
|
54
71
|
- lib/edu_draw/pen.rb
|
55
72
|
- lib/edu_draw/sheet.rb
|
73
|
+
- test/edu_draw/animation_pen_test.rb
|
74
|
+
- test/edu_draw/basic_pen_tests.rb
|
56
75
|
- test/edu_draw/pen_test.rb
|
57
76
|
- test/edu_draw/sheet_test.rb
|
58
77
|
- test/test_helper.rb
|