def render(self, size, focus=False): size = Size(*size) map = self.world.current_map map_rect = map.rect player_position = map.find(self.world.player).position if not self.viewport: # Let's pretend the map itself is the viewport, and the below logic # can adjust it as necessary. self.viewport = self.world.current_map.rect horizontal = self._adjust_viewport( self.viewport.horizontal_span, size.width, player_position.x, map.rect.horizontal_span ) vertical = self._adjust_viewport( self.viewport.vertical_span, size.height, player_position.y, map.rect.vertical_span ) self.viewport = Rectangle.from_spans(horizontal=horizontal, vertical=vertical) # viewport is from the pov of the map; negate it to get how much space # is added or removed around the map pad_left = -self.viewport.left pad_top = -self.viewport.top pad_right = (size.width - pad_left) - map_rect.width pad_bottom = (size.height - pad_top) - map_rect.height # TODO it's unclear when you're near the edge of the map, which i hate. # should either show a clear border past the map edge, or show some # kinda fade or whatever along a cut-off edge map_canvas = urwid.CompositeCanvas(CellCanvas(map)) map_canvas.pad_trim_left_right(pad_left, pad_right) map_canvas.pad_trim_top_bottom(pad_top, pad_bottom) return map_canvas
def render(self, size, focus=False): size = Size(*size) map = self.world.current_map map_rect = map.rect player_position = map.find(self.world.player).position if not self.viewport: # Let's pretend the map itself is the viewport, and the below logic # can adjust it as necessary. self.viewport = self.world.current_map.rect horizontal = self._adjust_viewport( self.viewport.horizontal_span, size.width, player_position.x, map.rect.horizontal_span, ) vertical = self._adjust_viewport( self.viewport.vertical_span, size.height, player_position.y, map.rect.vertical_span, ) self.viewport = Rectangle.from_spans(horizontal=horizontal, vertical=vertical) # viewport is from the pov of the map; negate it to get how much space # is added or removed around the map pad_left = -self.viewport.left pad_top = -self.viewport.top pad_right = (size.width - pad_left) - map_rect.width pad_bottom = (size.height - pad_top) - map_rect.height # TODO it's unclear when you're near the edge of the map, which i hate. # should either show a clear border past the map edge, or show some # kinda fade or whatever along a cut-off edge map_canvas = urwid.CompositeCanvas(CellCanvas(map)) map_canvas.pad_trim_left_right(pad_left, pad_right) map_canvas.pad_trim_top_bottom(pad_top, pad_bottom) return map_canvas