Cleanups in map object UI.

* Fix the context menu
* Remove unnecessary overrides
* Clean up formatting/naming
* Factor out base class for shared behavior
This commit is contained in:
Dan Albert 2020-09-13 22:51:41 -07:00
parent dcaa8d4e96
commit 80f2b7a1db
3 changed files with 107 additions and 106 deletions

View File

@ -1,29 +1,32 @@
from PySide2.QtCore import QRect, Qt from typing import Optional
from PySide2.QtGui import QColor, QPainter
from PySide2.QtWidgets import QGraphicsRectItem, QGraphicsSceneHoverEvent, QGraphicsSceneContextMenuEvent, QMenu, \
QAction, QGraphicsSceneMouseEvent
import qt_ui.uiconstants as CONST from PySide2.QtGui import QColor, QPainter
from PySide2.QtWidgets import (
QAction,
QGraphicsSceneContextMenuEvent,
QMenu,
)
import qt_ui.uiconstants as const
from game import Game from game import Game
from qt_ui.windows.basemenu.QBaseMenu2 import QBaseMenu2 from qt_ui.windows.basemenu.QBaseMenu2 import QBaseMenu2
from theater import ControlPoint, db from theater import ControlPoint
from .QMapObject import QMapObject
class QMapControlPoint(QGraphicsRectItem): class QMapControlPoint(QMapObject):
def __init__(self, parent, x: float, y: float, w: float, h: float, model: ControlPoint, game: Game): def __init__(self, parent, x: float, y: float, w: float, h: float,
super(QMapControlPoint, self).__init__(x, y, w, h) model: ControlPoint, game: Game) -> None:
super().__init__(x, y, w, h)
self.model = model self.model = model
self.game = game self.game = game
self.parent = parent self.parent = parent
self.setAcceptHoverEvents(True)
self.setZValue(1) self.setZValue(1)
self.setToolTip(self.model.name) self.setToolTip(self.model.name)
self.base_details_dialog: Optional[QBaseMenu2] = None
def paint(self, painter, option, widget=None) -> None:
def paint(self, painter, option, widget=None):
#super(QMapControlPoint, self).paint(painter, option, widget)
if self.parent.get_display_rule("cp"): if self.parent.get_display_rule("cp"):
painter.save() painter.save()
painter.setRenderHint(QPainter.Antialiasing) painter.setRenderHint(QPainter.Antialiasing)
@ -32,69 +35,43 @@ class QMapControlPoint(QGraphicsRectItem):
if self.model.has_runway(): if self.model.has_runway():
if self.isUnderMouse(): if self.isUnderMouse():
painter.setBrush(CONST.COLORS["white"]) painter.setBrush(const.COLORS["white"])
painter.setPen(self.pen_color) painter.setPen(self.pen_color)
r = option.rect r = option.rect
painter.drawEllipse(r.x(), r.y(), r.width(), r.height()) painter.drawEllipse(r.x(), r.y(), r.width(), r.height())
# TODO: Draw sunk carriers differently.
#gauge = QRect(r.x(), # Either don't draw them at all, or perhaps use a sunk ship icon.
# r.y()+CONST.CP_SIZE/2 + 2,
# r.width(),
# CONST.CP_SIZE / 4)
#painter.setBrush(CONST.COLORS["bright_red"])
#painter.setPen(CONST.COLORS["black"])
#painter.drawRect(gauge)
#gauge2 = QRect(r.x(),
# r.y() + CONST.CP_SIZE / 2 + 2,
# r.width()*self.model.base.strength,
# CONST.CP_SIZE / 4)
#painter.setBrush(CONST.COLORS["green"])
#painter.drawRect(gauge2)
else:
# TODO : not drawing sunk carriers. Can be improved to display sunk carrier.
pass
painter.restore() painter.restore()
def hoverEnterEvent(self, event: QGraphicsSceneHoverEvent): def contextMenuEvent(self, event: QGraphicsSceneContextMenuEvent) -> None:
self.update()
self.setCursor(Qt.PointingHandCursor)
def mouseMoveEvent(self, event:QGraphicsSceneMouseEvent):
self.update()
def hoverLeaveEvent(self, event: QGraphicsSceneHoverEvent):
self.update()
def mousePressEvent(self, event:QGraphicsSceneMouseEvent):
self.openBaseMenu()
#self.contextMenuEvent(event)
def contextMenuEvent(self, event: QGraphicsSceneContextMenuEvent):
if self.model.captured: if self.model.captured:
openBaseMenu = QAction("Open base menu") text = "Open base menu"
else: else:
openBaseMenu = QAction("Open intel menu") text = "Open intel menu"
openBaseMenu.triggered.connect(self.openBaseMenu) open_menu = QAction(text)
open_menu.triggered.connect(self.on_click)
menu = QMenu("Menu", self.parent) menu = QMenu("Menu", self.parent)
menu.addAction(openBaseMenu) menu.addAction(open_menu)
menu.exec_(event.screenPos()) menu.exec_(event.screenPos())
@property @property
def brush_color(self)->QColor: def brush_color(self) -> QColor:
return self.model.captured and CONST.COLORS["blue"] or CONST.COLORS["super_red"] if self.model.captured:
return const.COLORS["blue"]
else:
return const.COLORS["super_red"]
@property @property
def pen_color(self) -> QColor: def pen_color(self) -> QColor:
return self.model.captured and CONST.COLORS["white"] or CONST.COLORS["white"] return const.COLORS["white"]
def openBaseMenu(self): def on_click(self) -> None:
self.baseMenu = QBaseMenu2(self.window(), self.model, self.game) self.base_details_dialog = QBaseMenu2(
self.baseMenu.show() self.window(),
self.model,
self.game
)
self.base_details_dialog.show()

View File

@ -1,33 +1,37 @@
from PySide2.QtCore import QPoint, QRect, QPointF, Qt from typing import List, Optional
from PySide2.QtGui import QPainter, QBrush
from PySide2.QtWidgets import QGraphicsRectItem, QGraphicsItem, QGraphicsSceneHoverEvent, QGraphicsSceneMouseEvent
import qt_ui.uiconstants as CONST from PySide2.QtCore import QRect
from game import db, Game from PySide2.QtGui import QBrush
from PySide2.QtWidgets import QGraphicsItem
import qt_ui.uiconstants as const
from game import Game
from game.data.building_data import FORTIFICATION_BUILDINGS from game.data.building_data import FORTIFICATION_BUILDINGS
from qt_ui.windows.groundobject.QGroundObjectMenu import QGroundObjectMenu from qt_ui.windows.groundobject.QGroundObjectMenu import QGroundObjectMenu
from theater import TheaterGroundObject, ControlPoint from theater import TheaterGroundObject, ControlPoint
from .QMapObject import QMapObject
class QMapGroundObject(QGraphicsRectItem): class QMapGroundObject(QMapObject):
def __init__(self, parent, x: float, y: float, w: float, h: float,
def __init__(self, parent, x: float, y: float, w: float, h: float, cp: ControlPoint, model: TheaterGroundObject, game:Game, buildings=[]): cp: ControlPoint, model: TheaterGroundObject, game: Game,
super(QMapGroundObject, self).__init__(x, y, w, h) buildings: Optional[List[TheaterGroundObject]] = None) -> None:
super().__init__(x, y, w, h)
self.model = model self.model = model
self.cp = cp self.cp = cp
self.parent = parent self.parent = parent
self.game = game self.game = game
self.setAcceptHoverEvents(True)
self.setZValue(2) self.setZValue(2)
self.buildings = buildings self.buildings = buildings if buildings is not None else []
self.setFlag(QGraphicsItem.ItemIgnoresTransformations, False) self.setFlag(QGraphicsItem.ItemIgnoresTransformations, False)
self.ground_object_dialog: Optional[QGroundObjectMenu] = None
if len(self.model.groups) > 0: if len(self.model.groups) > 0:
units = {} units = {}
for g in self.model.groups: for g in self.model.groups:
print(g) print(g)
for u in g.units: for u in g.units:
if u.type in units.keys(): if u.type in units:
units[u.type] = units[u.type]+1 units[u.type] = units[u.type]+1
else: else:
units[u.type] = 1 units[u.type] = 1
@ -42,14 +46,9 @@ class QMapGroundObject(QGraphicsRectItem):
tooltip = tooltip + str(building.dcs_identifier) + "\n" tooltip = tooltip + str(building.dcs_identifier) + "\n"
self.setToolTip(tooltip[:-1]) self.setToolTip(tooltip[:-1])
def mousePressEvent(self, event:QGraphicsSceneMouseEvent): def paint(self, painter, option, widget=None) -> None:
self.openEditionMenu() player_icons = "_blue"
enemy_icons = ""
def paint(self, painter, option, widget=None):
#super(QMapControlPoint, self).paint(painter, option, widget)
playerIcons = "_blue"
enemyIcons = ""
if self.parent.get_display_rule("go"): if self.parent.get_display_rule("go"):
painter.save() painter.save()
@ -58,7 +57,8 @@ class QMapGroundObject(QGraphicsRectItem):
if cat == "aa" and self.model.sea_object: if cat == "aa" and self.model.sea_object:
cat = "ship" cat = "ship"
rect = QRect(option.rect.x()+2,option.rect.y(),option.rect.width()-2,option.rect.height()) rect = QRect(option.rect.x() + 2, option.rect.y(),
option.rect.width() - 2, option.rect.height())
is_dead = self.model.is_dead is_dead = self.model.is_dead
for building in self.buildings: for building in self.buildings:
@ -67,16 +67,16 @@ class QMapGroundObject(QGraphicsRectItem):
break break
if not is_dead and not self.cp.captured: if not is_dead and not self.cp.captured:
painter.drawPixmap(rect, CONST.ICONS[cat + enemyIcons]) painter.drawPixmap(rect, const.ICONS[cat + enemy_icons])
elif not is_dead: elif not is_dead:
painter.drawPixmap(rect, CONST.ICONS[cat + playerIcons]) painter.drawPixmap(rect, const.ICONS[cat + player_icons])
else: else:
painter.drawPixmap(rect, CONST.ICONS["destroyed"]) painter.drawPixmap(rect, const.ICONS["destroyed"])
self.drawHealthGauge(painter, option) self.draw_health_gauge(painter, option)
painter.restore() painter.restore()
def drawHealthGauge(self, painter, option): def draw_health_gauge(self, painter, option) -> None:
units_alive = 0 units_alive = 0
units_dead = 0 units_dead = 0
@ -97,22 +97,18 @@ class QMapGroundObject(QGraphicsRectItem):
if units_dead + units_alive > 0: if units_dead + units_alive > 0:
ratio = float(units_alive)/(float(units_dead) + float(units_alive)) ratio = float(units_alive)/(float(units_dead) + float(units_alive))
bar_height = ratio * option.rect.height() bar_height = ratio * option.rect.height()
painter.fillRect(option.rect.x(), option.rect.y(), 2, option.rect.height(), QBrush(CONST.COLORS["dark_red"])) painter.fillRect(option.rect.x(), option.rect.y(), 2,
painter.fillRect(option.rect.x(), option.rect.y(), 2, bar_height, QBrush(CONST.COLORS["green"])) option.rect.height(),
QBrush(const.COLORS["dark_red"]))
painter.fillRect(option.rect.x(), option.rect.y(), 2, bar_height,
def hoverEnterEvent(self, event: QGraphicsSceneHoverEvent): QBrush(const.COLORS["green"]))
self.update()
self.setCursor(Qt.PointingHandCursor)
def mouseMoveEvent(self, event:QGraphicsSceneMouseEvent):
self.update()
self.setCursor(Qt.PointingHandCursor)
def hoverLeaveEvent(self, event: QGraphicsSceneHoverEvent):
self.update()
def openEditionMenu(self):
self.editionMenu = QGroundObjectMenu(self.window(), self.model, self.buildings, self.cp, self.game)
self.editionMenu.show()
def on_click(self) -> None:
self.ground_object_dialog = QGroundObjectMenu(
self.window(),
self.model,
self.buildings,
self.cp,
self.game
)
self.ground_object_dialog.show()

View File

@ -0,0 +1,28 @@
"""Common base for objects drawn on the game map."""
from PySide2.QtCore import Qt
from PySide2.QtWidgets import (
QGraphicsRectItem,
QGraphicsSceneHoverEvent,
QGraphicsSceneMouseEvent,
)
class QMapObject(QGraphicsRectItem):
"""Base class for objects drawn on the game map.
Game map objects have an on_click behavior that triggers on left click, and
change the mouse cursor on hover.
"""
def __init__(self, x: float, y: float, w: float, h: float):
super().__init__(x, y, w, h)
self.setAcceptHoverEvents(True)
def hoverEnterEvent(self, event: QGraphicsSceneHoverEvent):
self.setCursor(Qt.PointingHandCursor)
def mousePressEvent(self, event: QGraphicsSceneMouseEvent):
if event.button() == Qt.LeftButton:
self.on_click()
def on_click(self) -> None:
raise NotImplementedError