panda_canvas 0.3.0 → 0.4.0
Sign up to get free protection for your applications and to get access to all the features.
- data/README.rdoc +5 -1
- data/lib/panda_canvas/canvas.rb +32 -5
- data/lib/panda_canvas/clean_room.rb +50 -31
- data/lib/panda_canvas/version.rb +1 -1
- metadata +3 -3
data/README.rdoc
CHANGED
@@ -8,4 +8,8 @@ It is created as a teaching tool for the {Computer Club n.a. 8-bit Panda}[http:/
|
|
8
8
|
|
9
9
|
Panda Canvas is available as a gem:
|
10
10
|
|
11
|
-
$ gem install panda_canvas
|
11
|
+
$ gem install panda_canvas
|
12
|
+
|
13
|
+
== More
|
14
|
+
|
15
|
+
Take a look at the {wiki}[http://github.com/8bitpanda/panda_canvas/wiki].
|
data/lib/panda_canvas/canvas.rb
CHANGED
@@ -7,26 +7,53 @@ module PandaCanvas
|
|
7
7
|
attr_reader :image
|
8
8
|
|
9
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.
|
10
|
+
# A list of +calls+ in the form +[:method, *args]+ is passed to be executed.
|
13
11
|
def initialize(width, height, calls)
|
14
12
|
super(width, height, false)
|
15
13
|
self.caption = 'Panda Canvas'
|
16
14
|
@image = TexPlay.create_image(self, width, height)
|
17
15
|
@calls = calls
|
16
|
+
@canvas_calls = []
|
17
|
+
@used_fonts = {}
|
18
18
|
end
|
19
19
|
|
20
20
|
# Draws the image in memory.
|
21
21
|
def draw
|
22
22
|
@image.draw(0, 0, 0)
|
23
|
+
@canvas_calls.each {|call| send call[0], *call[1..-1] }
|
24
|
+
end
|
25
|
+
|
26
|
+
# Sets the font with name +font_name+ and +height+ in pixels to be used when drawing text.
|
27
|
+
def font(font_name, height)
|
28
|
+
key = [font_name, height]
|
29
|
+
if @used_fonts.include? key
|
30
|
+
@font = @used_fonts[key]
|
31
|
+
else
|
32
|
+
@font = @used_fonts[key] = Gosu::Font.new(self, font_name, height)
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
# Draws text +s+ in coordinates +x+ and +y+ with a given +color+.
|
37
|
+
def text(s, x, y, color)
|
38
|
+
if color.is_a? Symbol
|
39
|
+
rgb = TexPlay::Colors.const_get(color.capitalize)[0..2].map! do |c|
|
40
|
+
c * 255
|
41
|
+
end
|
42
|
+
color = Gosu::Color.new(255, *rgb)
|
43
|
+
end
|
44
|
+
@font.draw(s, x, y, 0, 1, 1, color)
|
23
45
|
end
|
24
46
|
|
25
47
|
# Runs a range of commands until the next flush.
|
26
48
|
def update
|
49
|
+
CleanRoom::CANVAS_UPDATE.each {|call| send call[0], *call[1..-1] }
|
27
50
|
unless @calls.empty?
|
28
|
-
@calls.slice!(0...@calls.index(CleanRoom::
|
29
|
-
|
51
|
+
@calls.slice!(0...@calls.index(CleanRoom::FLUSH)).each do |call|
|
52
|
+
if CleanRoom::CANVAS_CALLS.include? call[0]
|
53
|
+
@canvas_calls << call
|
54
|
+
else
|
55
|
+
@image.send call[0], *call[1..-1]
|
56
|
+
end
|
30
57
|
end
|
31
58
|
@calls.shift
|
32
59
|
end
|
@@ -1,32 +1,51 @@
|
|
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
|
-
|
9
|
-
|
10
|
-
#
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
#
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
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 = [:flush].freeze
|
9
|
+
|
10
|
+
# Signature set for defaults that are executed in each update event.
|
11
|
+
CANVAS_UPDATE = [[:font, Gosu::default_font_name, 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].freeze
|
15
|
+
|
16
|
+
# Returns an array of captured method calls.
|
17
|
+
# A +flush+ is appended at the end.
|
18
|
+
def calls
|
19
|
+
@_calls + [FLUSH]
|
20
|
+
end
|
21
|
+
|
22
|
+
# Initializes the clean object for method call capturing.
|
23
|
+
def initialize
|
24
|
+
@_calls = []
|
25
|
+
end
|
26
|
+
|
27
|
+
# Capures and stores all missing method calls in the instance.
|
28
|
+
def method_missing(sym, *args)
|
29
|
+
if sym == :flush
|
30
|
+
@_calls << FLUSH
|
31
|
+
else
|
32
|
+
@_calls << [sym, *args]
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
# Adds a font change call to +font_name+ with +height+ in pixels.
|
37
|
+
# Uses default typeface, if +font_name+ is +nil+.
|
38
|
+
# All subsequent text drawing calls will use the given font.
|
39
|
+
def font(height, font_name=nil)
|
40
|
+
@_calls << [:font, font_name || Gosu::default_font_name, height]
|
41
|
+
end
|
42
|
+
|
43
|
+
# Adds text +s+ to be drawn on screen with current font and +color+.
|
44
|
+
# Text is placed in +x+ and +y+ coordinates.
|
45
|
+
def text(s, x, y, color=0xffffffff)
|
46
|
+
@_calls << [:text, s, x, y, color]
|
47
|
+
end
|
48
|
+
|
49
|
+
end # CleanRoom
|
50
|
+
|
32
51
|
end # PandaCanvas
|
data/lib/panda_canvas/version.rb
CHANGED
metadata
CHANGED
@@ -4,9 +4,9 @@ version: !ruby/object:Gem::Version
|
|
4
4
|
prerelease: false
|
5
5
|
segments:
|
6
6
|
- 0
|
7
|
-
-
|
7
|
+
- 4
|
8
8
|
- 0
|
9
|
-
version: 0.
|
9
|
+
version: 0.4.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-
|
17
|
+
date: 2010-10-18 00:00:00 +03:00
|
18
18
|
default_executable:
|
19
19
|
dependencies:
|
20
20
|
- !ruby/object:Gem::Dependency
|