panda_canvas 0.2.0 → 0.3.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.
data/lib/panda_canvas.rb CHANGED
@@ -3,34 +3,26 @@ $:.unshift(libdir) unless $:.include?(libdir)
3
3
 
4
4
  require 'texplay'
5
5
  require 'panda_canvas/canvas'
6
+ require 'panda_canvas/clean_room'
6
7
 
8
+ # Panda Canvas is an educational 2D drawing canvas.
7
9
  module PandaCanvas
8
10
 
9
11
  class << self
10
12
 
13
+ # Reader for the Canvas instance.
14
+ # Used to access the underlying TexPlay image.
11
15
  attr_reader :canvas
12
16
 
17
+ # Takes a +block+ with drawing code.
18
+ # The code is then drawn in a window with dimensions +width+ and +height+.
13
19
  def draw(width=640, height=480, &block)
14
- @canvas = Canvas.new(width, height, Fiber.new(&block))
20
+ clean_room = CleanRoom.new
21
+ clean_room.instance_eval(&block)
22
+ @canvas = Canvas.new(width, height, clean_room.calls)
15
23
  @canvas.show
16
24
  end
17
25
 
18
26
  end # class << self
19
27
 
20
- end # PandaCanvas
21
-
22
- def method_missing(sym, *args)
23
- found = false
24
- if PandaCanvas.canvas
25
- @panda_canvas_image ||= PandaCanvas.canvas.image
26
- if @panda_canvas_image.respond_to? sym
27
- pci = @panda_canvas_image
28
- self.class.instance_eval do
29
- define_method(sym) {|*args| pci.send sym, *args }
30
- end
31
- self.send sym, *args
32
- found = true
33
- end
34
- end
35
- super(sym, *args) unless found
36
- end
28
+ end # PandaCanvas
@@ -1,25 +1,35 @@
1
- require 'texplay'
2
- require 'fiber'
3
-
4
1
  module PandaCanvas
5
2
 
3
+ # Canvas is a subclassed Gosu::Window that is used for drawing.
6
4
  class Canvas < Gosu::Window
7
5
 
6
+ # TexPlay image, which is drawn in the window.
8
7
  attr_reader :image
9
8
 
10
- def initialize(width, height, fiber)
9
+ # Creates a new canvas window with dimensions +width+ and +height+.
10
+ # A list of +calls+ in the form
11
+ # [:method, *args]
12
+ # is passed to be executed.
13
+ def initialize(width, height, calls)
11
14
  super(width, height, false)
12
15
  self.caption = 'Panda Canvas'
13
16
  @image = TexPlay.create_image(self, width, height)
14
- @fiber = fiber
17
+ @calls = calls
15
18
  end
16
19
 
20
+ # Draws the image in memory.
17
21
  def draw
18
22
  @image.draw(0, 0, 0)
19
23
  end
20
24
 
25
+ # Runs a range of commands until the next flush.
21
26
  def update
22
- @fiber.resume if @fiber.alive?
27
+ unless @calls.empty?
28
+ @calls.slice!(0...@calls.index(CleanRoom::FLUSH_SIGNATURE)).each do |call|
29
+ @image.send call[0], *call[1..-1]
30
+ end
31
+ @calls.shift
32
+ end
23
33
  end
24
34
 
25
35
  end # Canvas
@@ -0,0 +1,32 @@
1
+ module PandaCanvas
2
+
3
+ # CleanRoom is used to capture and store method calls for delayed execution.
4
+ class CleanRoom
5
+
6
+ # Signature for the +flush+ method.
7
+ # This method is used to stop calculation and draw the frame.
8
+ FLUSH_SIGNATURE = [:flush].freeze
9
+
10
+ # Returns an array of captured method calls.
11
+ # A +flush+ is appended at the end.
12
+ def calls
13
+ @calls + [FLUSH_SIGNATURE]
14
+ end
15
+
16
+ # Initializes the clean object for method call capturing.
17
+ def initialize
18
+ @calls = []
19
+ end
20
+
21
+ # Capures and stores all missing method calls in the instance.
22
+ def method_missing(sym, *args)
23
+ if sym == :flush
24
+ @calls << FLUSH_SIGNATURE
25
+ else
26
+ @calls << [sym, *args]
27
+ end
28
+ end
29
+
30
+ end # CleanRoom
31
+
32
+ end # PandaCanvas
@@ -1,6 +1,6 @@
1
1
  class PandaCanvas
2
2
 
3
3
  # PandaCanvas version.
4
- VERSION = '0.2.0'
4
+ VERSION = '0.3.0'
5
5
 
6
6
  end # end PandaCanvas
metadata CHANGED
@@ -4,9 +4,9 @@ version: !ruby/object:Gem::Version
4
4
  prerelease: false
5
5
  segments:
6
6
  - 0
7
- - 2
7
+ - 3
8
8
  - 0
9
- version: 0.2.0
9
+ version: 0.3.0
10
10
  platform: ruby
11
11
  authors:
12
12
  - Dimitry Solovyov
@@ -57,6 +57,7 @@ extra_rdoc_files: []
57
57
 
58
58
  files:
59
59
  - lib/panda_canvas/canvas.rb
60
+ - lib/panda_canvas/clean_room.rb
60
61
  - lib/panda_canvas/version.rb
61
62
  - lib/panda_canvas.rb
62
63
  - LICENSE