Map implementation

This commit is contained in:
Khopa 2019-07-04 09:33:19 +02:00
parent 2adacdba02
commit 67910bce61
6 changed files with 188 additions and 6 deletions

View File

@ -5,6 +5,7 @@ from PySide2.QtGui import QPixmap
from PySide2.QtWidgets import QApplication, QLabel, QSplashScreen
from qt_ui.windows.QLiberationWindow import QLiberationWindow
from userdata import persistency
if __name__ == "__main__":
@ -14,6 +15,7 @@ if __name__ == "__main__":
splash = QSplashScreen(pixmap)
splash.show()
persistency.setup(sys.argv[1])
sleep(2)
app.processEvents()

View File

@ -1,4 +1,5 @@
# URL for UI links
from PySide2.QtGui import QColor, QFont, QPixmap
URLS = {
"Manual": "https://github.com/shdwp/dcs_liberation/wiki/Manual",
@ -8,3 +9,31 @@ URLS = {
"ForumThread": "https://forums.eagle.ru/showthread.php?t=214834",
"Issues": "https://github.com/shdwp/dcs_liberation/issues"
}
COLORS = {
"red": QColor(255, 125, 125),
"bright_red": QColor(200, 64, 64),
"blue": QColor(164, 164, 255),
"dark_blue": QColor(45, 62, 80),
"white": QColor(255, 255, 255),
"green": QColor(128, 186, 128),
"bright_green": QColor(64, 200, 64),
"black": QColor(0, 0, 0)
}
CP_SIZE = 25
FONT = QFont("Arial", 12, weight=5, italic=True)
"""
ICONS = {
"Dawn": QPixmap("../resources/ui/daytime/dawn.png"),
"Day": QPixmap("../resources/ui/daytime/day.png"),
"Dusk": QPixmap("../resources/ui/daytime/dusk.png"),
"Night": QPixmap("../resources/ui/daytime/night.png"),
"Money": QPixmap("../resources/ui/misc/money_icon.png"),
"Ordnance": QPixmap("../resources/ui/misc/ordnance_icon.png"),
}
"""

View File

@ -0,0 +1,67 @@
from PySide2.QtCore import QLine
from PySide2.QtGui import QColor, QPainter, QCursor, QTextItem, QPen
from PySide2.QtWidgets import QGraphicsRectItem, QGraphicsSceneHoverEvent, QGraphicsSceneContextMenuEvent, QMenu
from theater import ControlPoint
import qt_ui.uiconstants as CONST
class QMapControlPoint(QGraphicsRectItem):
def __init__(self, parent, x: float, y: float, w: float, h: float, model: ControlPoint):
super(QMapControlPoint, self).__init__(x, y, w, h)
self.model = model
self.parent = parent
self.setAcceptHoverEvents(True)
self.setZValue(1)
self.setToolTip(self.model.base)
def paint(self, painter, option, widget=None):
#super(QMapControlPoint, self).paint(painter, option, widget)
painter.save()
painter.setRenderHint(QPainter.Antialiasing)
painter.setBrush(self.brush_color)
painter.setPen(self.pen_color)
if self.isUnderMouse():
painter.setBrush(CONST.COLORS["green"])
painter.setPen(self.pen_color)
painter.drawEllipse(option.rect)
r = option.rect
painter.setPen(QPen(CONST.COLORS["white"], CONST.CP_SIZE/5))
painter.setBrush(CONST.COLORS["white"])
painter.drawLine(QLine(r.x(), r.y(), r.x()+r.width(), r.y()+r.height()))
painter.restore()
def hoverEnterEvent(self, event: QGraphicsSceneHoverEvent):
self.update()
def hoverLeaveEvent(self, event: QGraphicsSceneHoverEvent):
self.update()
def contextMenuEvent(self, event: QGraphicsSceneContextMenuEvent):
menu = QMenu("Menu", self.parent)
menu.addAction("Attack")
menu.addAction("See Info")
menu.addSeparator()
menu.addAction("Show Base Info")
menu.addAction("Show Base Info")
menu.exec_(event.screenPos())
@property
def brush_color(self)->QColor:
if self.parent.game.player_country == "USA":
return self.model.captured and CONST.COLORS["blue"] or CONST.COLORS["red"]
else:
return self.model.captured and CONST.COLORS["red"] or CONST.COLORS["blue"]
@property
def pen_color(self) -> QColor:
if self.parent.game.player_country == "USA":
return self.model.captured and CONST.COLORS["dark_blue"] or CONST.COLORS["bright_red"]
else:
return self.model.captured and CONST.COLORS["bright_red"] or CONST.COLORS["dark_blue"]

View File

View File

@ -1,7 +1,13 @@
from PySide2.QtGui import QPixmap
from PySide2.QtWidgets import QWidget, QGraphicsWidget, QGraphicsView
from PySide2.QtCore import Qt
from PySide2.QtGui import QPixmap, QBrush, QColor, QWheelEvent, QPen, QFont
from PySide2.QtWidgets import QWidget, QGraphicsWidget, QGraphicsView, QFrame, QGraphicsTextItem
from qt_ui.widgets.QMapControlPoint import QMapControlPoint
from qt_ui.windows.QLiberationScene import QLiberationScene
from dcs import Point
from userdata import persistency
from game import Game
import qt_ui.uiconstants as CONST
class QLiberationMap(QGraphicsView):
@ -9,10 +15,89 @@ class QLiberationMap(QGraphicsView):
def __init__(self):
super(QLiberationMap, self).__init__()
self.setMinimumSize(800,600)
self._zoom = 0
self.init_scene()
game = persistency.restore_game()
self.loadGame(game)
def init_scene(self):
scene = QLiberationScene(self)
scene.addText("Hello World")
scene.addPixmap(QPixmap("../resources/caumap.gif"))
self.setScene(scene)
self.setTransformationAnchor(QGraphicsView.AnchorUnderMouse)
self.setResizeAnchor(QGraphicsView.AnchorUnderMouse)
#self.setVerticalScrollBarPolicy(Qt.ScrollBarAlwaysOff)
#self.setHorizontalScrollBarPolicy(Qt.ScrollBarAlwaysOff)
self.setBackgroundBrush(QBrush(QColor(30, 30, 30)))
self.setFrameShape(QFrame.NoFrame)
self.setDragMode(QGraphicsView.ScrollHandDrag)
def loadGame(self, game: Game):
self.game = game
scene = self.scene()
scene.clear()
scene.addPixmap(QPixmap("../resources/" + self.game.theater.overview_image))
for cp in self.game.theater.controlpoints:
pos = self._transform_point(cp.position)
scene.addItem(QMapControlPoint(self, pos[0]-CONST.CP_SIZE/2, pos[1]-CONST.CP_SIZE/2, CONST.CP_SIZE, CONST.CP_SIZE, cp))
#e = scene.addEllipse(pos[0]-CONST.CP_SIZE/2, pos[1]-CONST.CP_SIZE/2, CONST.CP_SIZE, CONST.CP_SIZE, QPen(brush=QBrush(color=color), width=5), brush=color)
text = scene.addText(cp.name, font=QFont("Trebuchet MS", 10, weight=5, italic=False))
text.setPos(pos[0]+CONST.CP_SIZE, pos[1]-CONST.CP_SIZE/2)
for go in cp.ground_objects:
pos = self._transform_point(go.position)
e = scene.addEllipse(pos[0] - CONST.CP_SIZE / 2, pos[1] - CONST.CP_SIZE / 2, CONST.CP_SIZE,
CONST.CP_SIZE, QPen(brush=QBrush(color=CONST.COLORS["green"]), width=5), brush=CONST.COLORS["green"])
def wheelEvent(self, event: QWheelEvent):
if event.angleDelta().y() > 0:
factor = 1.25
self._zoom += 1
else:
factor = 0.8
self._zoom -= 1
if self._zoom > -5:
self.scale(factor, factor)
else:
self._zoom = -5
def _transform_point(self, p: Point, treshold=30) -> (int, int):
point_a = list(self.game.theater.reference_points.keys())[0]
point_a_img = self.game.theater.reference_points[point_a]
point_b = list(self.game.theater.reference_points.keys())[1]
point_b_img = self.game.theater.reference_points[point_b]
Y_dist = point_a_img[0] - point_b_img[0]
lon_dist = point_a[1] - point_b[1]
X_dist = point_a_img[1] - point_b_img[1]
lat_dist = point_b[0] - point_a[0]
Y_scale = float(Y_dist) / float(lon_dist)
X_scale = float(X_dist) / float(lat_dist)
# ---
Y_offset = p.x - point_a[0]
X_offset = p.y - point_a[1]
X = point_b_img[1] + X_offset * X_scale
Y = point_a_img[0] - Y_offset * Y_scale
#X += 5
#Y += 5
return X > treshold and X or treshold, Y > treshold and Y or treshold

View File

@ -28,14 +28,13 @@ class QLiberationWindow(QMainWindow):
hbox.addWidget(okButton)
hbox.addWidget(cancelButton)
self.liberationMap = QLiberationMap()
hbox.addWidget(self.liberationMap)
self.liberation_map = QLiberationMap()
hbox.addWidget(self.liberation_map)
vbox = QVBoxLayout()
vbox.addStretch(1)
vbox.addLayout(hbox)
central_widget = QWidget()
central_widget.setLayout(vbox)
self.setCentralWidget(central_widget)