Web · Wiki · Activities · Blog · Lists · Chat · Meeting · Bugs · Git · Translate · Archive · People · Donate

Commit 98b216439cc9b47121d656bf5a06fa6ff051f6bc

Show the map in the same context in the mapview in play mode

Signed-off-by: Gonzalo Odiard <gonzalo@laptop.org>
mapnav.py
(9 / 2)
  
1414from sugar.graphics.style import Color
1515
1616from game_map import GameMap
17from mapview import TopMapView
17import mapview
1818
1919WIDTH_CONTROL_LINES = 2
2020RESIZE_HANDLE_SIZE = 10
216216 event.area.height)
217217 ctx.clip()
218218 self.draw(ctx)
219
220 if self.view_mode == self.MODE_PLAY:
221 position = {'x': self.x, 'y': self.y, 'direction': self.direction}
222 view_data = {'width': 150, 'height': 150,
223 'show_position': position, 'x': rect.width - 150, 'y': 30}
224 mapview.draw(ctx, self._game_map, view_data)
225
219226 return False
220227
221228 def receive_update_wall_info(self, mapnav, x, y, direction):
517517 window = gtk.Window()
518518 game_map = GameMap()
519519 nav_view = MapNavView(game_map)
520 top_view = TopMapView(game_map, 200, 200)
520 top_view = mapview.TopMapView(game_map, 200, 200)
521521 top_view.show_position(nav_view.x, nav_view.y, nav_view.direction)
522522 nav_view.connect('position-changed', show_position, top_view)
523523 hbox = gtk.HBox()
mapview.py
(106 / 98)
  
99import cairo
1010from game_map import GameMap
1111
12# view_data = width, height, show_position
1213
14
15def draw(ctx, game_map, view_data):
16 # calculate cell size
17 cell_width = view_data['width'] / game_map.data['max_x']
18 cell_height = view_data['height'] / game_map.data['max_y']
19 cell_size = min(cell_width, cell_height)
20 map_width = cell_size * game_map.data['max_x']
21 margin_x = (view_data['width'] - map_width) / 2
22 map_height = cell_size * game_map.data['max_y']
23 margin_y = (view_data['height'] - map_height) / 2
24 door_width = cell_size / 3
25
26 if 'x' in view_data:
27 margin_x = view_data['x']
28 if 'y' in view_data:
29 margin_y = view_data['y']
30
31 # background
32 ctx.rectangle(margin_x, margin_y, map_width, map_height)
33 fill = (0, 0, 0)
34 stroke = (1, 1, 1)
35
36 ctx.set_source_rgb(*fill)
37 ctx.fill_preserve()
38 ctx.set_source_rgb(*stroke)
39 ctx.stroke()
40
41 # draw cells
42 for x in range(game_map.data['max_x']):
43 for y in range(game_map.data['max_y']):
44 # Draw North
45 direction = 'N'
46 wall_info = game_map.get_wall_info(x, y, direction)
47 x_pos = margin_x + x * cell_size
48 y_pos = margin_y + y * cell_size
49 if wall_info is not None:
50 if game_map.have_door(x, y, direction):
51 # draw door
52 ctx.move_to(x_pos, y_pos)
53 ctx.line_to(x_pos + door_width, y_pos)
54 ctx.stroke()
55 ctx.move_to(x_pos + door_width * 2, y_pos)
56 ctx.line_to(x_pos + cell_size, y_pos)
57 else:
58 ctx.move_to(x_pos, y_pos)
59 ctx.line_to(x_pos + cell_size, y_pos)
60 ctx.stroke()
61
62 # Draw West
63 direction = 'W'
64 wall_info = game_map.get_wall_info(x, y, direction)
65 if wall_info is not None:
66 if game_map.have_door(x, y, direction):
67 # draw door
68 ctx.move_to(x_pos, y_pos)
69 ctx.line_to(x_pos, y_pos + door_width)
70 ctx.stroke()
71 ctx.move_to(x_pos, y_pos + door_width * 2)
72 ctx.line_to(x_pos, y_pos + cell_size)
73 else:
74 ctx.move_to(x_pos, y_pos)
75 ctx.line_to(x_pos, y_pos + cell_size)
76 ctx.stroke()
77
78 if view_data['show_position'] is not None:
79 x = view_data['show_position']['x']
80 y = view_data['show_position']['y']
81 direction = view_data['show_position']['direction']
82 x_pos = margin_x + x * cell_size + cell_size / 2
83 y_pos = margin_y + y * cell_size + cell_size / 2
84 border = 3
85 if direction == 'N':
86 point2_x = margin_x + x * cell_size + border
87 point2_y = margin_y + y * cell_size + border
88 point3_x = margin_x + (x + 1) * cell_size - border
89 point3_y = margin_y + y * cell_size + border
90 elif direction == 'E':
91 point2_x = margin_x + (x + 1) * cell_size - border
92 point2_y = margin_y + y * cell_size + border
93 point3_x = margin_x + (x + 1) * cell_size - border
94 point3_y = margin_y + (y + 1) * cell_size - border
95 elif direction == 'S':
96 point2_x = margin_x + (x + 1) * cell_size - border
97 point2_y = margin_y + (y + 1) * cell_size - border
98 point3_x = margin_x + x * cell_size + border
99 point3_y = margin_y + (y + 1) * cell_size - border
100 elif direction == 'W':
101 point2_x = margin_x + x * cell_size + border
102 point2_y = margin_y + (y + 1) * cell_size - border
103 point3_x = margin_x + x * cell_size + border
104 point3_y = margin_y + y * cell_size + border
105
106 ctx.move_to(x_pos, y_pos)
107 ctx.line_to(point2_x, point2_y)
108 ctx.line_to(point3_x, point3_y)
109 ctx.close_path()
110 fill = (1, 0, 0)
111 ctx.set_source_rgb(*fill)
112 ctx.fill()
113
114
13115class TopMapView(gtk.DrawingArea):
14116
15117 def __init__(self, game_map, width, height):
131131 self._show_position = None
132132 self.queue_draw()
133133
134 def calculate_sizes(self, width, height):
135 # calculate cell size
136 cell_width = self._width / self._game_map.data['max_x']
137 cell_height = self._height / self._game_map.data['max_y']
138 self.cell_size = min(cell_width, cell_height)
139 self._map_width = self.cell_size * self._game_map.data['max_x']
140 self.margin_x = (width - self._map_width) / 2
141 self._map_height = self.cell_size * self._game_map.data['max_y']
142 self.margin_y = (height - self._map_height) / 2
143 self._door_width = self.cell_size / 3
144
145134 def expose(self, widget, event):
146135 rect = self.get_allocation()
147 self.calculate_sizes(rect.width, rect.height)
148136 ctx = widget.window.cairo_create()
149137 # set a clip region for the expose event
150138 ctx.rectangle(event.area.x, event.area.y, event.area.width,
151139 event.area.height)
152140 ctx.clip()
153 self.draw(ctx)
154 return False
141 view_data = {'width': self._width, 'height': self._height,
142 'show_position': self._show_position}
155143
156 def draw(self, ctx):
157 # background
158 ctx.rectangle(self.margin_x, self.margin_y, self._map_width,
159 self._map_height)
160 fill = (0, 0, 0)
161 stroke = (1, 1, 1)
162
163 ctx.set_source_rgb(*fill)
164 ctx.fill_preserve()
165 ctx.set_source_rgb(*stroke)
166 ctx.stroke()
167
168 # draw cells
169 for x in range(self._game_map.data['max_x']):
170 for y in range(self._game_map.data['max_y']):
171 # Draw North
172 direction = 'N'
173 wall_info = self._game_map.get_wall_info(x, y, direction)
174 x_pos = self.margin_x + x * self.cell_size
175 y_pos = self.margin_y + y * self.cell_size
176 if wall_info is not None:
177 if self._game_map.have_door(x, y, direction):
178 # draw door
179 ctx.move_to(x_pos, y_pos)
180 ctx.line_to(x_pos + self._door_width, y_pos)
181 ctx.stroke()
182 ctx.move_to(x_pos + self._door_width * 2, y_pos)
183 ctx.line_to(x_pos + self.cell_size, y_pos)
184 else:
185 ctx.move_to(x_pos, y_pos)
186 ctx.line_to(x_pos + self.cell_size, y_pos)
187 ctx.stroke()
188
189 # Draw West
190 direction = 'W'
191 wall_info = self._game_map.get_wall_info(x, y, direction)
192 if wall_info is not None:
193 if self._game_map.have_door(x, y, direction):
194 # draw door
195 ctx.move_to(x_pos, y_pos)
196 ctx.line_to(x_pos, y_pos + self._door_width)
197 ctx.stroke()
198 ctx.move_to(x_pos, y_pos + self._door_width * 2)
199 ctx.line_to(x_pos, y_pos + self.cell_size)
200 else:
201 ctx.move_to(x_pos, y_pos)
202 ctx.line_to(x_pos, y_pos + self.cell_size)
203 ctx.stroke()
204
205 if self._show_position is not None:
206 x = self._show_position['x']
207 y = self._show_position['y']
208 direction = self._show_position['direction']
209 x_pos = self.margin_x + x * self.cell_size + self.cell_size / 2
210 y_pos = self.margin_y + y * self.cell_size + self.cell_size / 2
211 border = 3
212 if direction == 'N':
213 point2_x = self.margin_x + x * self.cell_size + border
214 point2_y = self.margin_y + y * self.cell_size + border
215 point3_x = self.margin_x + (x + 1) * self.cell_size - border
216 point3_y = self.margin_y + y * self.cell_size + border
217 elif direction == 'E':
218 point2_x = self.margin_x + (x + 1) * self.cell_size - border
219 point2_y = self.margin_y + y * self.cell_size + border
220 point3_x = self.margin_x + (x + 1) * self.cell_size - border
221 point3_y = self.margin_y + (y + 1) * self.cell_size - border
222 elif direction == 'S':
223 point2_x = self.margin_x + (x + 1) * self.cell_size - border
224 point2_y = self.margin_y + (y + 1) * self.cell_size - border
225 point3_x = self.margin_x + x * self.cell_size + border
226 point3_y = self.margin_y + (y + 1) * self.cell_size - border
227 elif direction == 'W':
228 point2_x = self.margin_x + x * self.cell_size + border
229 point2_y = self.margin_y + (y + 1) * self.cell_size - border
230 point3_x = self.margin_x + x * self.cell_size + border
231 point3_y = self.margin_y + y * self.cell_size + border
232
233 ctx.move_to(x_pos, y_pos)
234 ctx.line_to(point2_x, point2_y)
235 ctx.line_to(point3_x, point3_y)
236 ctx.close_path()
237 fill = (1, 0, 0)
238 ctx.set_source_rgb(*fill)
239 ctx.fill()
144 draw(ctx, self._game_map, view_data)
145 return False
240146
241147
242148def main():