def fill_texture(self, column, row, mapper, obj_type, z_value=1) -> None: pixmap = mapper.get_pixmap_of_type(obj_type) if pixmap is None: # logger.warning("No pixmap defined for type:%s, col:%s, row:%s", obj_type, column, row) return sx, sy = scene_position(column, row) scene_utils.put_pixmap_in_tile_center(self.scene, pixmap, sx, sy, z_value)
def draw_road(self, start: (), stop: ()) -> None: logger.debug("Draw road from:%s, to:%s", start, stop) # TODO use proper icons/pixmaps road_pen = QtGui.QPen(QtGui.QColor(164, 64, 155)) road_pen.setWidth(15) path = QtGui.QPainterPath() startx, starty = scene_position(start[1], start[0]) startx = (startx + 0.5) * constants.TILE_SIZE starty = (starty + 0.5) * constants.TILE_SIZE path.moveTo(startx, starty) stopx, stopy = scene_position(stop[1], stop[0]) stopx = (stopx + 0.5) * constants.TILE_SIZE stopy = (stopy + 0.5) * constants.TILE_SIZE path.lineTo(stopx, stopy) item = self.scene.addPath(path, pen=road_pen) item.setZValue(2)
def draw_structure(self, row, column, structure) -> None: pixmap = self.scenario.get_structure_type_to_pixmap_mapper( ).get_pixmap_of_type(structure.get_type().value) if pixmap is None: logger.warning("No pixmap defined for type:%s, col:%s, row:%s", structure.get_type(), column, row) return logger.debug("Draw structure:%s, row:%s, col:%s", structure.get_type(), row, column) sx, sy = scene_position(column, row) # TODO draw same count of structures as structure level scene_utils.put_pixmap_in_tile_center(self.scene, pixmap, sx, sy, 20)
def _draw_grid_and_coords(self, columns, rows) -> None: logger.debug("_draw_grid_and_coords") # draw the grid and the coordinates for column in range(0, columns): for row in range(0, rows): sx, sy = scene_position(column, row) # item = self.scene.addRect(sx * constants.TILE_SIZE, sy * constants.TILE_SIZE, constants.TILE_SIZE, constants.TILE_SIZE) # item.setZValue(1000) text = '({},{})'.format(column, row) item = QtWidgets.QGraphicsSimpleTextItem(text) item.setBrush(QtGui.QBrush(QtCore.Qt.black)) item.setPos((sx + 0.5) * constants.TILE_SIZE - item.boundingRect().width() / 2, sy * constants.TILE_SIZE) item.setZValue(1001) self.scene.addItem(item)
def _draw_towns_and_names(self) -> None: logger.debug("_draw_towns_and_names") # draw towns and names city_pixmap = QtGui.QPixmap( constants.extend(constants.GRAPHICS_MAP_FOLDER, 'city.png')) for nation in self.scenario.server_scenario.nations(): # get all provinces of this nation provinces = self.scenario.server_scenario.provinces_of_nation( nation) for province in provinces: column, row = self.scenario.server_scenario.province_property( province, constants.ProvinceProperty.TOWN_LOCATION) sx, sy = scene_position(column, row) # center city image on center of tile scene_utils.put_pixmap_in_tile_center(self.scene, city_pixmap, sx, sy, 6) # display province name below province_name = self.scenario.server_scenario.province_property( province, constants.ProvinceProperty.NAME) item = self.scene.addSimpleText(province_name) item.setPen(qt.TRANSPARENT_PEN) item.setBrush(QtGui.QBrush(QtCore.Qt.darkRed)) x = (sx + 0.5 ) * constants.TILE_SIZE - item.boundingRect().width() / 2 y = (sy + 1) * constants.TILE_SIZE - item.boundingRect().height() item.setPos(x, y) item.setZValue(6) # display rounded rectangle below province name bx = 8 by = 4 background = QtCore.QRectF( x - bx, y - by, item.boundingRect().width() + 2 * bx, item.boundingRect().height() + 2 * by) path = QtGui.QPainterPath() path.addRoundedRect(background, 50, 50) item = self.scene.addPath(path, pen=qt.TRANSPARENT_PEN, brush=QtGui.QBrush( QtGui.QColor(128, 128, 255, 64))) item.setZValue(5)
def _draw_rivers(self) -> None: logger.debug("_draw_rivers") # draw rivers river_pen = QtGui.QPen(QtGui.QColor(64, 64, 255)) river_pen.setWidth(5) # TODO get rivers via a method (generator) for river in self.scenario.server_scenario[ constants.ScenarioProperty.RIVERS]: tiles = river['tiles'] path = QtGui.QPainterPath() for tile in tiles: sx, sy = scene_position(tile[0], tile[1]) x = (sx + 0.5) * constants.TILE_SIZE y = (sy + 0.5) * constants.TILE_SIZE if tile == tiles[0]: path.moveTo(x, y) else: path.lineTo(x, y) item = self.scene.addPath(path, pen=river_pen) item.setZValue(2)
def _draw_province_and_nation_borders(self) -> None: logger.debug("_draw_province_and_nation_borders") # draw province and nation borders # TODO the whole border drawing is a crude approximation, implement it the right way province_border_pen = QtGui.QPen(QtGui.QColor(QtCore.Qt.black)) province_border_pen.setWidth(2) nation_border_pen = QtGui.QPen() nation_border_pen.setWidth(4) for nation in self.scenario.server_scenario.nations(): # get nation color color = self.scenario.server_scenario.nation_property( nation, constants.NationProperty.COLOR) nation_color = QtGui.QColor() nation_color.setNamedColor(color) # get all provinces provinces = self.scenario.server_scenario.provinces_of_nation( nation) nation_path = QtGui.QPainterPath() # get all tiles for province in provinces: province_path = QtGui.QPainterPath() tiles = self.scenario.server_scenario.province_property( province, constants.ProvinceProperty.TILES) for column, row in tiles: sx, sy = scene_position(column, row) province_path.addRect(sx * constants.TILE_SIZE, sy * constants.TILE_SIZE, constants.TILE_SIZE, constants.TILE_SIZE) province_path = province_path.simplified() item = self.scene.addPath(province_path, pen=province_border_pen) item.setZValue(4) nation_path.addPath(province_path) nation_path = nation_path.simplified() nation_border_pen.setColor(nation_color) item = self.scene.addPath(nation_path, pen=nation_border_pen) item.setZValue(5)
def redraw(self): """ The scenario has changed or the mode has changed. Redraw the overview map. """ logger.debug('redraw') # get number of columns and rows from the scenario columns = self.scenario.server_scenario[ constants.ScenarioProperty.MAP_COLUMNS] rows = self.scenario.server_scenario[ constants.ScenarioProperty.MAP_ROWS] # compute tile size (we assume square tiles) tile_size = self.view.width() / columns # adjust view height for aspect ratio of the scenario map, assuming square tiles view_height = math.floor(tile_size * rows) self.view.setFixedHeight(view_height) # remove everything except the tracker from the scene for item in self.scene_items: self.scene.removeItem(item) self.scene_items = [] # set scene rect self.scene.setSceneRect(0, 0, columns * tile_size, rows * tile_size) self.view.fitInView(self.scene.sceneRect()) # by design there should be almost no scaling or anything else if self.mode == constants.OverviewMapMode.POLITICAL: # political mode # fill the ground layer with a neutral color item = self.scene.addRect(0, 0, columns * tile_size, rows * tile_size) item.setBrush(QtCore.Qt.lightGray) item.setPen(qt.TRANSPARENT_PEN) item.setZValue(0) self.scene_items.extend([item]) # draw the nation borders and content (non-smooth) # for all nations for nation in self.scenario.server_scenario.nations(): # get nation color color_string = self.scenario.server_scenario.nation_property( nation, constants.NationProperty.COLOR) color = QtGui.QColor() color.setNamedColor(color_string) # get all provinces provinces = self.scenario.server_scenario.provinces_of_nation( nation) tiles = [] # get all tiles for province in provinces: tiles.extend( self.scenario.server_scenario.province_property( province, constants.ProvinceProperty.TILES)) # get rectangular path for each tile path = QtGui.QPainterPath() for tile in tiles: sx, sy = scene_position(*tile) path.addRect(sx * tile_size, sy * tile_size, tile_size, tile_size) # simply (creates outline) path = path.simplified() # create a brush from the color brush = QtGui.QBrush(color) item = self.scene.addPath( path, brush=brush) # will use the default pen for outline item.setZValue(1) self.scene_items.extend([item]) elif self.mode == constants.OverviewMapMode.GEOGRAPHICAL: # fill the background with sea (blue) item = self.scene.addRect(0, 0, columns * tile_size, rows * tile_size) item.setBrush(QtCore.Qt.blue) item.setPen(qt.TRANSPARENT_PEN) item.setZValue(0) self.scene_items.extend([item]) # six terrains left, plains, hills, mountains, tundra, swamp, desert # go through each position paths = {} for t in range( 1, len(self.scenario.server_scenario.get_terrain_settings())): paths[t] = QtGui.QPainterPath() for column in range(0, columns): for row in range(0, rows): t = self.scenario.server_scenario.terrain_at(column, row) if t != 0: # not for sea sx, sy = scene_position(column, row) paths[t].addRect(sx * tile_size, sy * tile_size, tile_size, tile_size) colors = { 1: QtCore.Qt.green, 2: QtCore.Qt.darkGreen, 3: QtCore.Qt.darkGray, 4: QtCore.Qt.white, 5: QtCore.Qt.darkYellow, 6: QtCore.Qt.yellow } for t in paths: path = paths[t] path = path.simplified() brush = QtGui.QBrush(colors[t]) item = self.scene.addPath(path, brush=brush, pen=qt.TRANSPARENT_PEN) item.setZValue(1) self.scene_items.extend([item])