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