zyps 0.6.3 → 0.7.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,152 @@
1
+ # Copyright 2007 Jay McGavren, jay@mcgavren.com.
2
+ #
3
+ # This file is part of Zyps.
4
+ #
5
+ # Zyps is free software; you can redistribute it and/or modify
6
+ # it under the terms of the GNU Lesser General Public License as published by
7
+ # the Free Software Foundation; either version 3 of the License, or
8
+ # (at your option) any later version.
9
+ #
10
+ # This program is distributed in the hope that it will be useful,
11
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
12
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
+ # GNU General Public License for more details.
14
+ #
15
+ # You should have received a copy of the GNU Lesser General Public License
16
+ # along with this program. If not, see <http://www.gnu.org/licenses/>.
17
+
18
+
19
+ require 'gtk2'
20
+
21
+
22
+ module Zyps
23
+
24
+
25
+ #Called by View objects for use in Ruby-GNOME2 applications.
26
+ #Assign an instance to a View, then add the drawing_area attribute to a GUI container object.
27
+ #The drawing area will be updated whenever the View is.
28
+ class GTK2Canvas
29
+
30
+
31
+ #A Gtk::DrawingArea that will be painted on.
32
+ attr_reader :drawing_area
33
+ #Dimensions of the drawing area.
34
+ #Control should normally be left to the owner View object.
35
+ attr_reader :width, :height
36
+
37
+ def initialize
38
+
39
+ #Will be resized later.
40
+ @width = 1
41
+ @height = 1
42
+
43
+ #Create a drawing area.
44
+ @drawing_area = Gtk::DrawingArea.new
45
+ #Set to correct size.
46
+ resize
47
+
48
+ #Whenever the drawing area needs updating...
49
+ @drawing_area.signal_connect("expose_event") do
50
+ #Copy buffer bitmap to canvas.
51
+ @drawing_area.window.draw_drawable(
52
+ @drawing_area.style.fg_gc(@drawing_area.state), #Gdk::GC (graphics context) to use when drawing.
53
+ buffer, #Gdk::Drawable source to copy onto canvas.
54
+ 0, 0, #Pull from upper left of source.
55
+ 0, 0, #Copy to upper left of canvas.
56
+ -1, -1 #-1 width and height signals to copy entire source over.
57
+ )
58
+ end
59
+
60
+ end
61
+
62
+ def width= (pixels) #:nodoc:
63
+ @width = pixels
64
+ resize
65
+ end
66
+
67
+ def height= (pixels) #:nodoc:
68
+ @height = pixels
69
+ resize
70
+ end
71
+
72
+
73
+ #Takes a hash with these keys and defaults:
74
+ # :color => nil
75
+ # :border_width => 1
76
+ # :filled => true
77
+ # :x => nil
78
+ # :y => nil
79
+ # :width => nil
80
+ # :height => nil
81
+ def draw_rectangle(options = {})
82
+ options = {
83
+ :filled => true
84
+ }.merge(options)
85
+ graphics_context = Gdk::GC.new(buffer)
86
+ graphics_context.rgb_fg_color = convert_color(options[:color])
87
+ buffer.draw_rectangle(
88
+ graphics_context,
89
+ options[:filled],
90
+ options[:x], options[:y],
91
+ options[:width], options[:height]
92
+ )
93
+ end
94
+
95
+
96
+ #Takes a hash with these keys and defaults:
97
+ # :color => nil
98
+ # :width => nil
99
+ # :x1 => nil
100
+ # :y1 => nil
101
+ # :x2 => nil
102
+ # :y2 => nil
103
+ def draw_line(options = {})
104
+ graphics_context = Gdk::GC.new(buffer)
105
+ graphics_context.rgb_fg_color = convert_color(options[:color])
106
+ graphics_context.set_line_attributes(
107
+ options[:width].ceil,
108
+ Gdk::GC::LINE_SOLID,
109
+ Gdk::GC::CAP_ROUND,
110
+ Gdk::GC::JOIN_MITER #Only used for polygons.
111
+ )
112
+ buffer.draw_line(
113
+ graphics_context,
114
+ options[:x1], options[:y1],
115
+ options[:x2], options[:y2]
116
+ )
117
+ end
118
+
119
+
120
+ #Draw all objects to the drawing area.
121
+ def render
122
+ @drawing_area.queue_draw_area(0, 0, @width, @height)
123
+ end
124
+
125
+
126
+ private
127
+
128
+ #Converts a Zyps Color to a Gdk::Color.
129
+ def convert_color(color)
130
+ Gdk::Color.new(
131
+ color.red * 65535,
132
+ color.green * 65535,
133
+ color.blue * 65535
134
+ )
135
+ end
136
+
137
+
138
+ def resize
139
+ @drawing_area.set_size_request(@width, @height)
140
+ @buffer = nil #Causes buffer to reset its size next time it's accessed.
141
+ end
142
+
143
+
144
+ def buffer
145
+ @buffer ||= Gdk::Pixmap.new(@drawing_area.window, @width, @height, -1)
146
+ end
147
+
148
+
149
+ end
150
+
151
+
152
+ end #module Zyps
@@ -0,0 +1,191 @@
1
+ # Copyright 2007 Jay McGavren, jay@mcgavren.com.
2
+ #
3
+ # This file is part of Zyps.
4
+ #
5
+ # Zyps is free software; you can redistribute it and/or modify
6
+ # it under the terms of the GNU Lesser General Public License as published by
7
+ # the Free Software Foundation; either version 3 of the License, or
8
+ # (at your option) any later version.
9
+ #
10
+ # This program is distributed in the hope that it will be useful,
11
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
12
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
+ # GNU General Public License for more details.
14
+ #
15
+ # You should have received a copy of the GNU Lesser General Public License
16
+ # along with this program. If not, see <http://www.gnu.org/licenses/>.
17
+
18
+
19
+ require 'wx'
20
+
21
+
22
+ module Zyps
23
+
24
+
25
+ #Called by View objects for use in wxRuby applications.
26
+ #Assign an instance to a View, then add the drawing_area attribute to a GUI container object.
27
+ #The drawing area will be updated whenever the View is.
28
+ class WxCanvas
29
+
30
+
31
+ #A wxWidgets window that will be painted on.
32
+ attr_reader :drawing_area
33
+ #Dimensions of the drawing area.
34
+ #Control should normally be left to the owner View object.
35
+ attr_reader :width, :height
36
+
37
+ #Takes the wxRuby GUI object that will be its parent.
38
+ def initialize (parent)
39
+
40
+ #Will be resized later.
41
+ @width = 1
42
+ @height = 1
43
+
44
+ #Create a drawing area.
45
+ @drawing_area = Wx::Window.new(parent)
46
+ #Set to correct size.
47
+ resize
48
+
49
+ #Whenever the drawing area needs updating...
50
+ @drawing_area.evt_paint do |event|
51
+ render
52
+ end
53
+
54
+ #Arrays of shapes that will be painted when render() is called.
55
+ @rectangle_queue = []
56
+ @line_queue = []
57
+
58
+ #Hash of Wx::Pens used to draw in various colors and widths.
59
+ @pens = Hash.new {|h, k| h[k] = Hash.new}
60
+ #Hash of Wx::Brushes for various colors.
61
+ @brushes = Hash.new
62
+
63
+ end
64
+
65
+ def width= (pixels) #:nodoc:
66
+ @width = pixels
67
+ resize
68
+ end
69
+
70
+ def height= (pixels) #:nodoc:
71
+ @height = pixels
72
+ resize
73
+ end
74
+
75
+
76
+ #Takes a hash with these keys and defaults:
77
+ # :color => nil
78
+ # :border_width => 1
79
+ # :filled => true
80
+ # :x => nil
81
+ # :y => nil
82
+ # :width => nil
83
+ # :height => nil
84
+ def draw_rectangle(options = {})
85
+ options = {
86
+ :filled => true,
87
+ :border_width => 1
88
+ }.merge(options)
89
+ @rectangle_queue << options
90
+ end
91
+
92
+
93
+
94
+ #Takes a hash with these keys and defaults:
95
+ # :color => nil
96
+ # :width => nil
97
+ # :x1 => nil
98
+ # :y1 => nil
99
+ # :x2 => nil
100
+ # :y2 => nil
101
+ def draw_line(options = {})
102
+ @line_queue << options
103
+ end
104
+
105
+
106
+ #Draw all objects to the drawing area.
107
+ def render
108
+ buffer.draw do |surface|
109
+ #Draw all queued rectangles.
110
+ render_rectangles(surface)
111
+ #Draw all queued lines.
112
+ render_lines(surface)
113
+ end
114
+ #Copy offscreen bitmap to screen.
115
+ @drawing_area.paint do |dc|
116
+ #Copy the buffer to the viewable window.
117
+ dc.draw_bitmap(buffer, 0, 0, false)
118
+ end
119
+ end
120
+
121
+
122
+ private
123
+
124
+
125
+ #Converts a Zyps Color to the toolkit's color class.
126
+ def convert_color(color)
127
+ Wx::Colour.new(
128
+ (color.red * 255).floor,
129
+ (color.green * 255).floor,
130
+ (color.blue * 255).floor
131
+ )
132
+ end
133
+
134
+
135
+ #Resize buffer and drawing area.
136
+ def resize
137
+ @drawing_area.set_size(Wx::Size.new(@width, @height))
138
+ @buffer = nil #Causes buffer to reset its size next time it's accessed.
139
+ end
140
+
141
+
142
+ #The Wx::Bitmap to draw to.
143
+ def buffer
144
+ @buffer ||= Wx::Bitmap.new(@width, @height)
145
+ end
146
+
147
+
148
+ #Draw all queued rectangles to the given GC.
149
+ def render_rectangles(surface)
150
+ while options = @rectangle_queue.shift do
151
+ surface.pen = get_pen(options[:color], options[:border_width]) #Used for border.
152
+ if options[:filled]
153
+ surface.brush = get_brush(options[:color])
154
+ else
155
+ surface.brush = Wx::TRANSPARENT_BRUSH
156
+ end
157
+ surface.draw_rectangle(
158
+ options[:x], options[:y],
159
+ options[:width], options[:height]
160
+ )
161
+ end
162
+ end
163
+
164
+
165
+ #Draw all queued lines to the given GC.
166
+ def render_lines(surface)
167
+ surface.pen.cap = Wx::CAP_ROUND
168
+ while options = @line_queue.shift do
169
+ surface.pen = get_pen(options[:color], options[:width])
170
+ surface.draw_line(
171
+ options[:x1].floor, options[:y1].floor,
172
+ options[:x2].floor, options[:y2].floor
173
+ )
174
+ end
175
+ end
176
+
177
+
178
+ def get_pen(color, width)
179
+ @pens[[color.red, color.green, color.blue]][width] ||= Wx::Pen.new(convert_color(color), width.ceil)
180
+ end
181
+
182
+
183
+ def get_brush(color)
184
+ @brushes[[color.red, color.green, color.blue]] ||= Wx::Brush.new(convert_color(color), Wx::SOLID)
185
+ end
186
+
187
+
188
+ end
189
+
190
+
191
+ end #module Zyps
@@ -16,7 +16,7 @@
16
16
  # along with this program. If not, see <http://www.gnu.org/licenses/>.
17
17
 
18
18
 
19
- require 'gtk2'
19
+ require 'zyps'
20
20
 
21
21
 
22
22
  module Zyps
@@ -25,39 +25,44 @@ module Zyps
25
25
  #A view of game objects.
26
26
  class TrailsView
27
27
 
28
- #A Gtk::DrawingArea to render objects on.
28
+ #A GUI toolkit-specific drawing area that will be used to render the view.
29
+ #See WxCanvas and GTK2Canvas.
29
30
  attr_reader :canvas
30
31
  #Dimensions of the view.
31
32
  attr_reader :width, :height
32
33
  #Number of line segments to draw for each object.
33
34
  attr_accessor :trail_length
35
+ #Whether view should be erased before re-drawing.
36
+ attr_accessor :erase_flag
34
37
 
35
- def initialize (width = 600, height = 400, trail_length = 5)
38
+ #Takes a hash with these keys and defaults:
39
+ # :canvas => nil
40
+ # :width => 600
41
+ # :height => 400
42
+ # :trail_length => 5
43
+ # :erase_flag => true
44
+ def initialize (options = {})
36
45
 
37
- @width, @height, @trail_length, = width, height, trail_length
38
-
39
- #Create a drawing area.
40
- @canvas = Gtk::DrawingArea.new
41
- #Set to correct size.
42
- resize
43
-
44
- #Whenever the drawing area needs updating...
45
- @canvas.signal_connect("expose_event") do
46
- #Copy buffer bitmap to canvas.
47
- @canvas.window.draw_drawable(
48
- @canvas.style.fg_gc(@canvas.state), #Gdk::GC (graphics context) to use when drawing.
49
- buffer, #Gdk::Drawable source to copy onto canvas.
50
- 0, 0, #Pull from upper left of source.
51
- 0, 0, #Copy to upper left of canvas.
52
- -1, -1 #-1 width and height signals to copy entire source over.
53
- )
54
- end
46
+ options = {
47
+ :width => 600,
48
+ :height => 400,
49
+ :trail_length => 5,
50
+ :erase_flag => true
51
+ }.merge(options)
52
+ @width = options[:width]
53
+ @height = options[:height]
54
+ @trail_length = options[:trail_length]
55
+ @erase_flag = options[:erase_flag]
56
+ @canvas = options[:canvas]
55
57
 
58
+ #Set canvas's size to match view's.
59
+ resize if @canvas
60
+
56
61
  #Track a list of locations for each object.
57
62
  @locations = Hash.new {|h, k| h[k] = Array.new}
58
63
 
59
64
  end
60
-
65
+
61
66
  def width= (pixels) #:nodoc:
62
67
  @width = pixels
63
68
  resize
@@ -66,7 +71,12 @@ class TrailsView
66
71
  @height = pixels
67
72
  resize
68
73
  end
69
-
74
+ def canvas= (canvas) #:nodoc:
75
+ @canvas = canvas
76
+ resize
77
+ end
78
+
79
+
70
80
  #Takes an Environment, and draws it to the canvas.
71
81
  #Tracks the position of each GameObject over time so it can draw a trail behind it.
72
82
  #The head will match the object's Color exactly, fading to black at the tail.
@@ -74,14 +84,14 @@ class TrailsView
74
84
  def update(environment)
75
85
 
76
86
  #Clear the background on the buffer.
77
- graphics_context = Gdk::GC.new(buffer)
78
- graphics_context.rgb_fg_color = Gdk::Color.new(0, 0, 0)
79
- buffer.draw_rectangle(
80
- graphics_context,
81
- true, #Filled.
82
- 0, 0, #Lower-left corner.
83
- @width, @height #Upper-right corner.
84
- )
87
+ if @erase_flag
88
+ @canvas.draw_rectangle(
89
+ :color => Color.new(0, 0, 0),
90
+ :filled => true,
91
+ :x => 0, :y => 0,
92
+ :width => @width, :height => @height
93
+ )
94
+ end
85
95
 
86
96
  #For each GameObject in the environment:
87
97
  environment.objects.each do |object|
@@ -91,7 +101,7 @@ class TrailsView
91
101
  #Add the object's current location to the list.
92
102
  @locations[object.identifier] << [object.location.x, object.location.y]
93
103
  #If the list is larger than the number of tail segments, delete the first position.
94
- @locations[object.identifier].shift if @locations[object.identifier].length > @trail_length
104
+ @locations[object.identifier].shift while @locations[object.identifier].length > @trail_length
95
105
 
96
106
  #For each location in this object's list:
97
107
  @locations[object.identifier].each_with_index do |location, index|
@@ -102,50 +112,37 @@ class TrailsView
102
112
  #Divide the current segment number by trail segment count to get the multiplier to use for brightness and width.
103
113
  multiplier = index.to_f / @locations[object.identifier].length.to_f
104
114
 
105
- #Set the drawing color to use the object's colors, adjusted by the multiplier.
106
- graphics_context.rgb_fg_color = Gdk::Color.new( #Don't use Gdk::GC.foreground= here, as that requires a color to be in the color map already.
107
- object.color.red * multiplier * 65535,
108
- object.color.green * multiplier * 65535,
109
- object.color.blue * multiplier * 65535
110
- )
111
-
112
- #Multiply the actual drawing width by the current multiplier to get the current drawing width.
113
- graphics_context.set_line_attributes(
114
- (object_radius * 2 * multiplier).ceil,
115
- Gdk::GC::LINE_SOLID,
116
- Gdk::GC::CAP_ROUND, #Line ends drawn as semicircles.
117
- Gdk::GC::JOIN_MITER #Only used for polygons.
118
- )
119
-
120
115
  #Get previous location so we can draw a line from it.
121
116
  previous_location = @locations[object.identifier][index - 1]
122
117
 
123
- #Draw a line with the current width from the prior location to the current location.
124
- buffer.draw_line(
125
- graphics_context,
126
- previous_location[0], previous_location[1],
127
- location[0], location[1]
118
+ @canvas.draw_line(
119
+ :color => Color.new(
120
+ object.color.red * multiplier,
121
+ object.color.green * multiplier,
122
+ object.color.blue * multiplier
123
+ ),
124
+ :width => object_radius * 2 * multiplier,
125
+ :x1 => previous_location[0], :y1 => previous_location[1],
126
+ :x2 => location[0], :y2 => location[1]
128
127
  )
129
128
 
130
129
  end
131
130
 
132
131
  end
133
132
 
134
- @canvas.queue_draw_area(0, 0, @width, @height)
133
+ @canvas.render
135
134
 
136
135
  end
137
136
 
138
137
 
139
138
  private
140
139
 
140
+
141
141
  def resize
142
- @canvas.set_size_request(@width, @height)
143
- @buffer = nil #Causes buffer to reset its size next time it's accessed.
144
- end
145
-
146
- def buffer
147
- @buffer ||= Gdk::Pixmap.new(@canvas.window, @width, @height, -1)
142
+ @canvas.width = @width
143
+ @canvas.height = @height
148
144
  end
145
+
149
146
 
150
147
  end
151
148