mirror of
https://github.com/dcs-retribution/dcs-retribution.git
synced 2025-11-10 15:41:24 +00:00
Using a singleton QObject to propagate game model update across whole app
This commit is contained in:
parent
8246c8e94b
commit
9a73c78705
@ -5,6 +5,7 @@ from PySide2.QtGui import QPixmap
|
||||
from PySide2.QtWidgets import QApplication, QLabel, QSplashScreen
|
||||
|
||||
from qt_ui import uiconstants
|
||||
from qt_ui.windows.GameUpdateSignal import GameUpdateSignal
|
||||
from qt_ui.windows.QLiberationWindow import QLiberationWindow
|
||||
from userdata import persistency
|
||||
|
||||
@ -29,6 +30,8 @@ if __name__ == "__main__":
|
||||
uiconstants.load_icons()
|
||||
app.processEvents()
|
||||
|
||||
GameUpdateSignal()
|
||||
|
||||
# Start window
|
||||
window = QLiberationWindow()
|
||||
window.setStyleSheet(css)
|
||||
|
||||
@ -5,6 +5,8 @@ from qt_ui.widgets.QBudgetBox import QBudgetBox
|
||||
from qt_ui.widgets.QTurnCounter import QTurnCounter
|
||||
|
||||
import qt_ui.uiconstants as CONST
|
||||
from qt_ui.windows.GameUpdateSignal import GameUpdateSignal
|
||||
|
||||
|
||||
class QTopPanel(QFrame):
|
||||
|
||||
@ -12,6 +14,7 @@ class QTopPanel(QFrame):
|
||||
super(QTopPanel, self).__init__()
|
||||
self.game = game
|
||||
self.init_ui()
|
||||
GameUpdateSignal.get_instance().gameupdated.connect(self.setGame)
|
||||
|
||||
def init_ui(self):
|
||||
|
||||
@ -61,4 +64,4 @@ class QTopPanel(QFrame):
|
||||
|
||||
def passTurn(self):
|
||||
self.game.pass_turn()
|
||||
self.setGame(self.game)
|
||||
GameUpdateSignal.get_instance().updateGame(self.game)
|
||||
@ -10,6 +10,7 @@ from qt_ui.widgets.map.QMapGroundObject import QMapGroundObject
|
||||
from qt_ui.widgets.map.QLiberationScene import QLiberationScene
|
||||
from dcs import Point
|
||||
|
||||
from qt_ui.windows.GameUpdateSignal import GameUpdateSignal
|
||||
from theater import ControlPoint
|
||||
from game import Game
|
||||
import qt_ui.uiconstants as CONST
|
||||
@ -36,7 +37,8 @@ class QLiberationMap(QGraphicsView):
|
||||
self.setMaximumHeight(2160)
|
||||
self._zoom = 0
|
||||
self.init_scene()
|
||||
self.loadGame(game)
|
||||
self.connectSignals()
|
||||
self.setGame(game)
|
||||
|
||||
def init_scene(self):
|
||||
scene = QLiberationScene(self)
|
||||
@ -45,19 +47,17 @@ class QLiberationMap(QGraphicsView):
|
||||
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()
|
||||
self.reload_scene()
|
||||
def connectSignals(self):
|
||||
GameUpdateSignal.get_instance().gameupdated.connect(self.setGame)
|
||||
|
||||
def setGame(self, game: Game):
|
||||
self.loadGame(game)
|
||||
self.game = game
|
||||
print("Reloading Map Canvas")
|
||||
self.reload_scene()
|
||||
|
||||
def reload_scene(self):
|
||||
scene = self.scene()
|
||||
|
||||
@ -48,11 +48,13 @@ class QMapControlPoint(QGraphicsRectItem):
|
||||
self.update()
|
||||
|
||||
def contextMenuEvent(self, event: QGraphicsSceneContextMenuEvent):
|
||||
# TODO : improve this and add contextual actions (just a placholder for now)
|
||||
menu = QMenu("Menu", self.parent)
|
||||
menu.addAction("Plan a strike on " + self.model.name + " airbase")
|
||||
menu.addAction("See available intel")
|
||||
menu.exec_(event.screenPos())
|
||||
|
||||
|
||||
@property
|
||||
def brush_color(self)->QColor:
|
||||
if self.parent.game.player_country == "USA":
|
||||
|
||||
0
qt_ui/widgets/map/QMapMission.py
Normal file
0
qt_ui/widgets/map/QMapMission.py
Normal file
0
qt_ui/windows/BriefingWindow.py
Normal file
0
qt_ui/windows/BriefingWindow.py
Normal file
19
qt_ui/windows/GameUpdateSignal.py
Normal file
19
qt_ui/windows/GameUpdateSignal.py
Normal file
@ -0,0 +1,19 @@
|
||||
from PySide2.QtCore import QObject, Signal
|
||||
from game import Game
|
||||
|
||||
|
||||
class GameUpdateSignal(QObject):
|
||||
|
||||
instance = None
|
||||
gameupdated = Signal(Game)
|
||||
|
||||
def __init__(self):
|
||||
super(GameUpdateSignal, self).__init__()
|
||||
GameUpdateSignal.instance = self
|
||||
|
||||
def updateGame(self, game: Game):
|
||||
self.gameupdated.emit(game)
|
||||
|
||||
@staticmethod
|
||||
def get_instance():
|
||||
return GameUpdateSignal.instance
|
||||
@ -8,6 +8,7 @@ from game import Game
|
||||
from qt_ui.uiconstants import URLS
|
||||
from qt_ui.widgets.QTopPanel import QTopPanel
|
||||
from qt_ui.widgets.map.QLiberationMap import QLiberationMap
|
||||
from qt_ui.windows.GameUpdateSignal import GameUpdateSignal
|
||||
from qt_ui.windows.QNewGameWizard import NewGameWizard
|
||||
from userdata import persistency
|
||||
|
||||
@ -17,17 +18,19 @@ class QLiberationWindow(QMainWindow):
|
||||
def __init__(self):
|
||||
super(QLiberationWindow, self).__init__()
|
||||
self.game = persistency.restore_game()
|
||||
self.init_ui()
|
||||
|
||||
def init_ui(self):
|
||||
self.setGeometry(300, 100, 270, 100)
|
||||
self.setWindowTitle("DCS Liberation")
|
||||
self.setWindowIcon(QIcon("../resources/icon.png"))
|
||||
self.statusBar().showMessage('Ready')
|
||||
|
||||
self.initUi()
|
||||
self.initActions()
|
||||
self.init_menubar()
|
||||
self.init_toolbar()
|
||||
self.initMenuBar()
|
||||
self.initToolbar()
|
||||
self.connectSignals()
|
||||
|
||||
def initUi(self):
|
||||
|
||||
hbox = QHBoxLayout()
|
||||
hbox.addStretch(1)
|
||||
@ -43,6 +46,9 @@ class QLiberationWindow(QMainWindow):
|
||||
central_widget.setLayout(vbox)
|
||||
self.setCentralWidget(central_widget)
|
||||
|
||||
def connectSignals(self):
|
||||
GameUpdateSignal.get_instance().gameupdated.connect(self.setGame)
|
||||
|
||||
def initActions(self):
|
||||
self.newGameAction = QAction("New Game", self)
|
||||
self.newGameAction.setIcon(QIcon(CONST.ICONS["New"]))
|
||||
@ -56,13 +62,13 @@ class QLiberationWindow(QMainWindow):
|
||||
self.showAboutDialogAction.setIcon(QIcon.fromTheme("help-about"))
|
||||
self.showAboutDialogAction.triggered.connect(self.showAboutDialog)
|
||||
|
||||
def init_toolbar(self):
|
||||
def initToolbar(self):
|
||||
self.tool_bar = self.addToolBar("File")
|
||||
self.tool_bar.addAction(self.newGameAction)
|
||||
self.tool_bar.addAction(QIcon(CONST.ICONS["Open"]), "Open")
|
||||
self.tool_bar.addAction(self.saveGameAction)
|
||||
|
||||
def init_menubar(self):
|
||||
def initMenuBar(self):
|
||||
self.menu = self.menuBar()
|
||||
|
||||
file_menu = self.menu.addMenu("File")
|
||||
@ -112,10 +118,13 @@ class QLiberationWindow(QMainWindow):
|
||||
def saveGame(self):
|
||||
print("Saving game")
|
||||
persistency.save_game(self.game)
|
||||
GameUpdateSignal.get_instance().updateGame(self.game)
|
||||
|
||||
def onGameGenerated(self):
|
||||
GameUpdateSignal.get_instance().updateGame(self.game)
|
||||
|
||||
def setGame(self, game: Game):
|
||||
self.game = game
|
||||
self.liberation_map.setGame(game)
|
||||
|
||||
def showAboutDialog(self):
|
||||
text = "<h3>DCS Liberation</h3>" + \
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user