panda_canvas 0.4.3 → 0.5.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.
@@ -0,0 +1,40 @@
1
+ module PandaCanvas
2
+
3
+ # AnimationCanvas is a subclassed Gosu::Window that is used for animation.
4
+ class AnimationCanvas < Gosu::Window
5
+
6
+ include DrawingMethods
7
+
8
+ # TexPlay image, which is drawn in the window.
9
+ attr_reader :image
10
+
11
+ # Creates a new canvas window with dimensions +width+ and +height+.
12
+ # A +block+ is passed to be executed.
13
+ def initialize(width, height, &block)
14
+ super(width, height, false)
15
+ self.caption = 'Panda Canvas'
16
+ @block = block
17
+ @image = TexPlay.create_image(self, width, height)
18
+ @clean_room = AnimationCleanRoom.new(@image)
19
+ @clean_room.canvas_calls = []
20
+ @clean_room.frame = 0
21
+ end
22
+
23
+ # Draws the image in memory.
24
+ def draw
25
+ @image.draw(0, 0, 0)
26
+ @clean_room.canvas_calls.each {|call| send call[0], *call[1..-1] }
27
+ end
28
+
29
+ # Runs an animation block.
30
+ def update
31
+ @image.rect 0, 0, width, height, :color => :black, :fill => true
32
+ DrawingMethods::CANVAS_UPDATE.each {|call| send call[0], *call[1..-1] }
33
+ @clean_room.canvas_calls = []
34
+ @clean_room.frame += 1
35
+ @clean_room.instance_eval &@block
36
+ end
37
+
38
+ end
39
+
40
+ end # PandaCanvas
@@ -0,0 +1,31 @@
1
+ module PandaCanvas
2
+
3
+ # AnimationCleanRoom is used to capture and store method calls for delayed execution.
4
+ class AnimationCleanRoom
5
+
6
+ # Array of methods that need to be passed to canvas.
7
+ attr_accessor :canvas_calls
8
+
9
+ # Current frame number.
10
+ attr_accessor :frame
11
+
12
+ # Initializes the clean object for method call capturing.
13
+ # TexPlay +image+ is used for TexPlay drawing methods.
14
+ def initialize(image)
15
+ @image = image
16
+ end
17
+
18
+ # Capures and stores all missing method calls in the instance.
19
+ def method_missing(sym, *args)
20
+ if DrawingMethods::CANVAS_CALLS.include? sym
21
+ @canvas_calls << [sym, *args]
22
+ elsif Gosu::Image.public_instance_methods.include? sym
23
+ @image.send sym, *args
24
+ else
25
+ super
26
+ end
27
+ end
28
+
29
+ end # CleanRoom
30
+
31
+ end # PandaCanvas
@@ -0,0 +1,47 @@
1
+ module PandaCanvas
2
+
3
+ # DrawingCanvas is a subclassed Gosu::Window that is used for drawing.
4
+ class DrawingCanvas < Gosu::Window
5
+
6
+ include DrawingMethods
7
+
8
+ # TexPlay image, which is drawn in the window.
9
+ attr_reader :image
10
+
11
+ # Creates a new canvas window with dimensions +width+ and +height+.
12
+ # A +block+ is passed to be executed.
13
+ def initialize(width, height, &block)
14
+ super(width, height, false)
15
+ self.caption = 'Panda Canvas'
16
+ @image = TexPlay.create_image(self, width, height)
17
+ clean_room = DrawingCleanRoom.new
18
+ clean_room.instance_eval(&block)
19
+ @calls = clean_room.calls
20
+ @canvas_calls = []
21
+ end
22
+
23
+ # Draws the image in memory.
24
+ def draw
25
+ @image.draw(0, 0, 0)
26
+ @canvas_calls.each {|call| send call[0], *call[1..-1] }
27
+ end
28
+
29
+ # Runs a range of commands until the next flush.
30
+ def update
31
+ DrawingMethods::CANVAS_UPDATE.each {|call| send call[0], *call[1..-1] }
32
+ unless @calls.empty?
33
+ flush_index = @calls.index(DrawingCleanRoom::FLUSH)
34
+ @calls.slice!(0...flush_index).each do |call|
35
+ if DrawingMethods::CANVAS_CALLS.include? call[0]
36
+ @canvas_calls << call
37
+ else
38
+ @image.send call[0], *call[1..-1]
39
+ end
40
+ end
41
+ @calls.shift
42
+ end
43
+ end
44
+
45
+ end # Canvas
46
+
47
+ end # PandaCanvas
@@ -1,18 +1,12 @@
1
1
  module PandaCanvas
2
2
 
3
- # CleanRoom is used to capture and store method calls for delayed execution.
4
- class CleanRoom
3
+ # DrawingCleanRoom is used to capture and store method calls for delayed execution.
4
+ class DrawingCleanRoom
5
5
 
6
6
  # Signature for the +flush+ method.
7
7
  # This method is used to stop calculation and draw the frame.
8
8
  FLUSH = [:flush].freeze
9
9
 
10
- # Signature set for defaults that are executed in each update event.
11
- CANVAS_UPDATE = [[:font, 12]]
12
-
13
- # Names of calls that need to be sent directly to Canvas instead of the TexPlay image on draw.
14
- CANVAS_CALLS = [:font, :text, :text_rel].freeze
15
-
16
10
  # Returns an array of captured method calls.
17
11
  # A +flush+ is appended at the end.
18
12
  def calls
@@ -28,7 +22,8 @@ module PandaCanvas
28
22
  def method_missing(sym, *args)
29
23
  if sym == :flush
30
24
  @_calls << FLUSH
31
- elsif Gosu::Image.public_instance_methods.include? sym
25
+ elsif (DrawingMethods::CANVAS_CALLS.include? sym) ||
26
+ (Gosu::Image.public_instance_methods.include? sym)
32
27
  @_calls << [sym, *args]
33
28
  else
34
29
  super
@@ -1,31 +1,17 @@
1
1
  module PandaCanvas
2
2
 
3
- # Canvas is a subclassed Gosu::Window that is used for drawing.
4
- class Canvas < Gosu::Window
3
+ module DrawingMethods
5
4
 
6
- # TexPlay image, which is drawn in the window.
7
- attr_reader :image
5
+ # Signature set for defaults that are executed in each update event.
6
+ CANVAS_UPDATE = [[:font, 12]]
8
7
 
9
- # Creates a new canvas window with dimensions +width+ and +height+.
10
- # A list of +calls+ in the form +[:method, *args]+ is passed to be executed.
11
- def initialize(width, height, calls)
12
- super(width, height, false)
13
- self.caption = 'Panda Canvas'
14
- @image = TexPlay.create_image(self, width, height)
15
- @calls = calls
16
- @canvas_calls = []
17
- @used_fonts = {}
18
- end
19
-
20
- # Draws the image in memory.
21
- def draw
22
- @image.draw(0, 0, 0)
23
- @canvas_calls.each {|call| send call[0], *call[1..-1] }
24
- end
8
+ # Names of calls that need to be sent directly to Canvas instead of the TexPlay image on draw.
9
+ CANVAS_CALLS = [:font, :text, :text_rel].freeze
25
10
 
26
11
  # Sets the font with name +font_name+ and +height+ in pixels to be used when drawing text.
27
12
  # All subsequent text drawing calls will use the given font.
28
13
  def font(height, font_name=Gosu::default_font_name)
14
+ @used_fonts ||= {}
29
15
  key = [font_name, height]
30
16
  if @used_fonts.include? key
31
17
  @font = @used_fonts[key]
@@ -59,21 +45,6 @@ module PandaCanvas
59
45
  @font.draw_rel(s, x, y, 0, rel_x, rel_y, 1, 1, color)
60
46
  end
61
47
 
62
- # Runs a range of commands until the next flush.
63
- def update
64
- CleanRoom::CANVAS_UPDATE.each {|call| send call[0], *call[1..-1] }
65
- unless @calls.empty?
66
- @calls.slice!(0...@calls.index(CleanRoom::FLUSH)).each do |call|
67
- if CleanRoom::CANVAS_CALLS.include? call[0]
68
- @canvas_calls << call
69
- else
70
- @image.send call[0], *call[1..-1]
71
- end
72
- end
73
- @calls.shift
74
- end
75
- end
76
-
77
- end # Canvas
48
+ end # DrawingMethods
78
49
 
79
50
  end # PandaCanvas
@@ -1,6 +1,6 @@
1
1
  module PandaCanvas
2
2
 
3
3
  # PandaCanvas version.
4
- VERSION = '0.4.3'
4
+ VERSION = '0.5.0'
5
5
 
6
6
  end # end PandaCanvas
data/lib/panda_canvas.rb CHANGED
@@ -2,8 +2,11 @@ libdir = File.dirname(__FILE__)
2
2
  $:.unshift(libdir) unless $:.include?(libdir)
3
3
 
4
4
  require 'texplay'
5
- require 'panda_canvas/canvas'
6
- require 'panda_canvas/clean_room'
5
+ require 'panda_canvas/drawing_methods'
6
+ require 'panda_canvas/animation_canvas'
7
+ require 'panda_canvas/animation_clean_room'
8
+ require 'panda_canvas/drawing_canvas'
9
+ require 'panda_canvas/drawing_clean_room'
7
10
 
8
11
  # Panda Canvas is an educational 2D drawing canvas.
9
12
  module PandaCanvas
@@ -17,9 +20,14 @@ module PandaCanvas
17
20
  # Takes a +block+ with drawing code.
18
21
  # The code is then drawn in a window with dimensions +width+ and +height+.
19
22
  def draw(width=640, height=480, &block)
20
- clean_room = CleanRoom.new
21
- clean_room.instance_eval(&block)
22
- @canvas = Canvas.new(width, height, clean_room.calls)
23
+ @canvas = DrawingCanvas.new(width, height, &block)
24
+ @canvas.show
25
+ end
26
+
27
+ # Takes a +block+ with animation code.
28
+ # The code is then drawn in a window with dimensions +width+ and +height+.
29
+ def animate(width=640, height=480, &block)
30
+ @canvas = AnimationCanvas.new(width, height, &block)
23
31
  @canvas.show
24
32
  end
25
33
 
metadata CHANGED
@@ -4,9 +4,9 @@ version: !ruby/object:Gem::Version
4
4
  prerelease: false
5
5
  segments:
6
6
  - 0
7
- - 4
8
- - 3
9
- version: 0.4.3
7
+ - 5
8
+ - 0
9
+ version: 0.5.0
10
10
  platform: ruby
11
11
  authors:
12
12
  - Dimitry Solovyov
@@ -14,7 +14,7 @@ autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
16
 
17
- date: 2010-10-21 00:00:00 +03:00
17
+ date: 2010-10-24 00:00:00 +03:00
18
18
  default_executable:
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency
@@ -56,8 +56,11 @@ extensions: []
56
56
  extra_rdoc_files: []
57
57
 
58
58
  files:
59
- - lib/panda_canvas/canvas.rb
60
- - lib/panda_canvas/clean_room.rb
59
+ - lib/panda_canvas/animation_canvas.rb
60
+ - lib/panda_canvas/animation_clean_room.rb
61
+ - lib/panda_canvas/drawing_canvas.rb
62
+ - lib/panda_canvas/drawing_clean_room.rb
63
+ - lib/panda_canvas/drawing_methods.rb
61
64
  - lib/panda_canvas/version.rb
62
65
  - lib/panda_canvas.rb
63
66
  - LICENSE