diff options
author | Gonzalo Odiard <godiard@gmail.com> | 2012-03-19 01:30:32 (GMT) |
---|---|---|
committer | Gonzalo Odiard <godiard@gmail.com> | 2012-03-19 01:30:32 (GMT) |
commit | 98b216439cc9b47121d656bf5a06fa6ff051f6bc (patch) | |
tree | b1cd8726828a513640c870d143e2af00eda1d2db | |
parent | 64c89235f1a8b23d110739409c163a3576ea8315 (diff) |
Show the map in the same context in the mapview in play mode
Signed-off-by: Gonzalo Odiard <gonzalo@laptop.org>
-rw-r--r-- | mapnav.py | 11 | ||||
-rw-r--r-- | mapview.py | 204 |
2 files changed, 115 insertions, 100 deletions
@@ -14,7 +14,7 @@ import rsvg from sugar.graphics.style import Color from game_map import GameMap -from mapview import TopMapView +import mapview WIDTH_CONTROL_LINES = 2 RESIZE_HANDLE_SIZE = 10 @@ -216,6 +216,13 @@ class MapNavView(gtk.DrawingArea): event.area.height) ctx.clip() self.draw(ctx) + + if self.view_mode == self.MODE_PLAY: + position = {'x': self.x, 'y': self.y, 'direction': self.direction} + view_data = {'width': 150, 'height': 150, + 'show_position': position, 'x': rect.width - 150, 'y': 30} + mapview.draw(ctx, self._game_map, view_data) + return False def receive_update_wall_info(self, mapnav, x, y, direction): @@ -510,7 +517,7 @@ def main(): window = gtk.Window() game_map = GameMap() nav_view = MapNavView(game_map) - top_view = TopMapView(game_map, 200, 200) + top_view = mapview.TopMapView(game_map, 200, 200) top_view.show_position(nav_view.x, nav_view.y, nav_view.direction) nav_view.connect('position-changed', show_position, top_view) hbox = gtk.HBox() @@ -9,6 +9,108 @@ import gtk import cairo from game_map import GameMap +# view_data = width, height, show_position + + +def draw(ctx, game_map, view_data): + # calculate cell size + cell_width = view_data['width'] / game_map.data['max_x'] + cell_height = view_data['height'] / game_map.data['max_y'] + cell_size = min(cell_width, cell_height) + map_width = cell_size * game_map.data['max_x'] + margin_x = (view_data['width'] - map_width) / 2 + map_height = cell_size * game_map.data['max_y'] + margin_y = (view_data['height'] - map_height) / 2 + door_width = cell_size / 3 + + if 'x' in view_data: + margin_x = view_data['x'] + if 'y' in view_data: + margin_y = view_data['y'] + + # background + ctx.rectangle(margin_x, margin_y, map_width, map_height) + fill = (0, 0, 0) + stroke = (1, 1, 1) + + ctx.set_source_rgb(*fill) + ctx.fill_preserve() + ctx.set_source_rgb(*stroke) + ctx.stroke() + + # draw cells + for x in range(game_map.data['max_x']): + for y in range(game_map.data['max_y']): + # Draw North + direction = 'N' + wall_info = game_map.get_wall_info(x, y, direction) + x_pos = margin_x + x * cell_size + y_pos = margin_y + y * cell_size + if wall_info is not None: + if game_map.have_door(x, y, direction): + # draw door + ctx.move_to(x_pos, y_pos) + ctx.line_to(x_pos + door_width, y_pos) + ctx.stroke() + ctx.move_to(x_pos + door_width * 2, y_pos) + ctx.line_to(x_pos + cell_size, y_pos) + else: + ctx.move_to(x_pos, y_pos) + ctx.line_to(x_pos + cell_size, y_pos) + ctx.stroke() + + # Draw West + direction = 'W' + wall_info = game_map.get_wall_info(x, y, direction) + if wall_info is not None: + if game_map.have_door(x, y, direction): + # draw door + ctx.move_to(x_pos, y_pos) + ctx.line_to(x_pos, y_pos + door_width) + ctx.stroke() + ctx.move_to(x_pos, y_pos + door_width * 2) + ctx.line_to(x_pos, y_pos + cell_size) + else: + ctx.move_to(x_pos, y_pos) + ctx.line_to(x_pos, y_pos + cell_size) + ctx.stroke() + + if view_data['show_position'] is not None: + x = view_data['show_position']['x'] + y = view_data['show_position']['y'] + direction = view_data['show_position']['direction'] + x_pos = margin_x + x * cell_size + cell_size / 2 + y_pos = margin_y + y * cell_size + cell_size / 2 + border = 3 + if direction == 'N': + point2_x = margin_x + x * cell_size + border + point2_y = margin_y + y * cell_size + border + point3_x = margin_x + (x + 1) * cell_size - border + point3_y = margin_y + y * cell_size + border + elif direction == 'E': + point2_x = margin_x + (x + 1) * cell_size - border + point2_y = margin_y + y * cell_size + border + point3_x = margin_x + (x + 1) * cell_size - border + point3_y = margin_y + (y + 1) * cell_size - border + elif direction == 'S': + point2_x = margin_x + (x + 1) * cell_size - border + point2_y = margin_y + (y + 1) * cell_size - border + point3_x = margin_x + x * cell_size + border + point3_y = margin_y + (y + 1) * cell_size - border + elif direction == 'W': + point2_x = margin_x + x * cell_size + border + point2_y = margin_y + (y + 1) * cell_size - border + point3_x = margin_x + x * cell_size + border + point3_y = margin_y + y * cell_size + border + + ctx.move_to(x_pos, y_pos) + ctx.line_to(point2_x, point2_y) + ctx.line_to(point3_x, point3_y) + ctx.close_path() + fill = (1, 0, 0) + ctx.set_source_rgb(*fill) + ctx.fill() + class TopMapView(gtk.DrawingArea): @@ -29,112 +131,18 @@ class TopMapView(gtk.DrawingArea): self._show_position = None self.queue_draw() - def calculate_sizes(self, width, height): - # calculate cell size - cell_width = self._width / self._game_map.data['max_x'] - cell_height = self._height / self._game_map.data['max_y'] - self.cell_size = min(cell_width, cell_height) - self._map_width = self.cell_size * self._game_map.data['max_x'] - self.margin_x = (width - self._map_width) / 2 - self._map_height = self.cell_size * self._game_map.data['max_y'] - self.margin_y = (height - self._map_height) / 2 - self._door_width = self.cell_size / 3 - def expose(self, widget, event): rect = self.get_allocation() - self.calculate_sizes(rect.width, rect.height) ctx = widget.window.cairo_create() # set a clip region for the expose event ctx.rectangle(event.area.x, event.area.y, event.area.width, event.area.height) ctx.clip() - self.draw(ctx) - return False - - def draw(self, ctx): - # background - ctx.rectangle(self.margin_x, self.margin_y, self._map_width, - self._map_height) - fill = (0, 0, 0) - stroke = (1, 1, 1) - - ctx.set_source_rgb(*fill) - ctx.fill_preserve() - ctx.set_source_rgb(*stroke) - ctx.stroke() - - # draw cells - for x in range(self._game_map.data['max_x']): - for y in range(self._game_map.data['max_y']): - # Draw North - direction = 'N' - wall_info = self._game_map.get_wall_info(x, y, direction) - x_pos = self.margin_x + x * self.cell_size - y_pos = self.margin_y + y * self.cell_size - if wall_info is not None: - if self._game_map.have_door(x, y, direction): - # draw door - ctx.move_to(x_pos, y_pos) - ctx.line_to(x_pos + self._door_width, y_pos) - ctx.stroke() - ctx.move_to(x_pos + self._door_width * 2, y_pos) - ctx.line_to(x_pos + self.cell_size, y_pos) - else: - ctx.move_to(x_pos, y_pos) - ctx.line_to(x_pos + self.cell_size, y_pos) - ctx.stroke() + view_data = {'width': self._width, 'height': self._height, + 'show_position': self._show_position} - # Draw West - direction = 'W' - wall_info = self._game_map.get_wall_info(x, y, direction) - if wall_info is not None: - if self._game_map.have_door(x, y, direction): - # draw door - ctx.move_to(x_pos, y_pos) - ctx.line_to(x_pos, y_pos + self._door_width) - ctx.stroke() - ctx.move_to(x_pos, y_pos + self._door_width * 2) - ctx.line_to(x_pos, y_pos + self.cell_size) - else: - ctx.move_to(x_pos, y_pos) - ctx.line_to(x_pos, y_pos + self.cell_size) - ctx.stroke() - - if self._show_position is not None: - x = self._show_position['x'] - y = self._show_position['y'] - direction = self._show_position['direction'] - x_pos = self.margin_x + x * self.cell_size + self.cell_size / 2 - y_pos = self.margin_y + y * self.cell_size + self.cell_size / 2 - border = 3 - if direction == 'N': - point2_x = self.margin_x + x * self.cell_size + border - point2_y = self.margin_y + y * self.cell_size + border - point3_x = self.margin_x + (x + 1) * self.cell_size - border - point3_y = self.margin_y + y * self.cell_size + border - elif direction == 'E': - point2_x = self.margin_x + (x + 1) * self.cell_size - border - point2_y = self.margin_y + y * self.cell_size + border - point3_x = self.margin_x + (x + 1) * self.cell_size - border - point3_y = self.margin_y + (y + 1) * self.cell_size - border - elif direction == 'S': - point2_x = self.margin_x + (x + 1) * self.cell_size - border - point2_y = self.margin_y + (y + 1) * self.cell_size - border - point3_x = self.margin_x + x * self.cell_size + border - point3_y = self.margin_y + (y + 1) * self.cell_size - border - elif direction == 'W': - point2_x = self.margin_x + x * self.cell_size + border - point2_y = self.margin_y + (y + 1) * self.cell_size - border - point3_x = self.margin_x + x * self.cell_size + border - point3_y = self.margin_y + y * self.cell_size + border - - ctx.move_to(x_pos, y_pos) - ctx.line_to(point2_x, point2_y) - ctx.line_to(point3_x, point3_y) - ctx.close_path() - fill = (1, 0, 0) - ctx.set_source_rgb(*fill) - ctx.fill() + draw(ctx, self._game_map, view_data) + return False def main(): |