From bb32f47b8ce643b1ac373a196d8f6861e7aa56bc Mon Sep 17 00:00:00 2001 From: Khopa Date: Sat, 6 Jul 2019 13:21:03 +0200 Subject: [PATCH] Implemented basic base menu to buy units --- qt_ui/stylesheets/style.css | 5 ++ qt_ui/widgets/QTopPanel.py | 8 +- qt_ui/widgets/map/QLiberationMap.py | 6 +- qt_ui/widgets/map/QMapControlPoint.py | 8 +- qt_ui/windows/QBaseMenu.py | 118 +++++++++++++++++++++++--- qt_ui/windows/QLiberationWindow.py | 16 ++-- 6 files changed, 136 insertions(+), 25 deletions(-) diff --git a/qt_ui/stylesheets/style.css b/qt_ui/stylesheets/style.css index 203cca28..861a8912 100644 --- a/qt_ui/stylesheets/style.css +++ b/qt_ui/stylesheets/style.css @@ -16,4 +16,9 @@ QPushButton[style="btn-primary"]{ /*color:white;*/ /*padding: 21px 5px 21px 5px; margin-top: 6px;*/ +} + +QBaseMenu{ + background-color:#699245; + color:white; } \ No newline at end of file diff --git a/qt_ui/widgets/QTopPanel.py b/qt_ui/widgets/QTopPanel.py index 6738abd9..571af2a1 100644 --- a/qt_ui/widgets/QTopPanel.py +++ b/qt_ui/widgets/QTopPanel.py @@ -19,10 +19,7 @@ class QTopPanel(QFrame): def init_ui(self): self.turnCounter = QTurnCounter() - self.turnCounter.setCurrentTurn(self.game.turn, self.game.current_day) - self.budgetBox = QBudgetBox() - self.budgetBox.setBudget(self.game.budget, self.game.budget_reward_amount) self.passTurnButton = QPushButton("Pass Turn") self.passTurnButton.setIcon(CONST.ICONS["PassTurn"]) @@ -53,8 +50,9 @@ class QTopPanel(QFrame): def setGame(self, game:Game): self.game = game - self.turnCounter.setCurrentTurn(self.game.turn, self.game.current_day) - self.budgetBox.setBudget(self.game.budget, self.game.budget_reward_amount) + if game is not None: + self.turnCounter.setCurrentTurn(self.game.turn, self.game.current_day) + self.budgetBox.setBudget(self.game.budget, self.game.budget_reward_amount) def openSettings(self): QMessageBox.information(self, "Settings", "Todo open game settings") diff --git a/qt_ui/widgets/map/QLiberationMap.py b/qt_ui/widgets/map/QLiberationMap.py index f21c394e..58697d24 100644 --- a/qt_ui/widgets/map/QLiberationMap.py +++ b/qt_ui/widgets/map/QLiberationMap.py @@ -57,7 +57,9 @@ class QLiberationMap(QGraphicsView): def setGame(self, game: Game): self.game = game print("Reloading Map Canvas") - self.reload_scene() + print(self.game) + if self.game is not None: + self.reload_scene() def reload_scene(self): scene = self.scene() @@ -67,7 +69,7 @@ class QLiberationMap(QGraphicsView): 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)) + CONST.CP_SIZE, cp, self.game)) # 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) diff --git a/qt_ui/widgets/map/QMapControlPoint.py b/qt_ui/widgets/map/QMapControlPoint.py index 2949b88e..cc68956b 100644 --- a/qt_ui/widgets/map/QMapControlPoint.py +++ b/qt_ui/widgets/map/QMapControlPoint.py @@ -5,14 +5,16 @@ from PySide2.QtWidgets import QGraphicsRectItem, QGraphicsSceneHoverEvent, QGrap from qt_ui.windows.QBaseMenu import QBaseMenu from theater import ControlPoint +from game import Game import qt_ui.uiconstants as CONST class QMapControlPoint(QGraphicsRectItem): - def __init__(self, parent, x: float, y: float, w: float, h: float, model: ControlPoint): + def __init__(self, parent, x: float, y: float, w: float, h: float, model: ControlPoint, game: Game): super(QMapControlPoint, self).__init__(x, y, w, h) self.model = model + self.game = game self.parent = parent self.setAcceptHoverEvents(True) self.setZValue(1) @@ -77,5 +79,5 @@ class QMapControlPoint(QGraphicsRectItem): return self.model.captured and CONST.COLORS["bright_red"] or CONST.COLORS["dark_blue"] def openBaseMenu(self): - window = QBaseMenu(self.window(), self.model) - window.show() \ No newline at end of file + self.baseMenu = QBaseMenu(self.window(), self.model, self.game) + self.baseMenu.show() \ No newline at end of file diff --git a/qt_ui/windows/QBaseMenu.py b/qt_ui/windows/QBaseMenu.py index 6fa0d3db..b8ea65e7 100644 --- a/qt_ui/windows/QBaseMenu.py +++ b/qt_ui/windows/QBaseMenu.py @@ -1,23 +1,121 @@ from PySide2.QtCore import Qt from PySide2.QtGui import QWindow -from PySide2.QtWidgets import QHBoxLayout, QLabel, QWidget +from PySide2.QtWidgets import QHBoxLayout, QLabel, QWidget, QFrame, QDialog, QVBoxLayout, QGridLayout, QPushButton +from dcs.unittype import UnitType -from theater import ControlPoint +from theater import ControlPoint, CAP, Embarking, AirDefence, CAS, PinpointStrike, db +from game import Game -class QBaseMenu(QWidget): +class QBaseMenu(QDialog): - def __init__(self, parent, controlPoint: ControlPoint): + def __init__(self, parent, controlPoint: ControlPoint, game: Game): super(QBaseMenu, self).__init__(parent) self.cp = controlPoint + self.game = game + self.deliveryEvent = self.game.units_delivery_event(self.cp) self.setWindowFlags(Qt.WindowStaysOnTopHint) self.initUi() def initUi(self): - layout = QHBoxLayout() - layout.addWidget(QLabel("TODO : This will be the base menu")) - layout.addWidget(QLabel(self.cp.name + " Base Info")) - central_widget = QWidget() - central_widget.setLayout(layout) - self.setLayout(layout) \ No newline at end of file + self.setWindowTitle(self.cp.name) + + self.topLayout = QHBoxLayout() + self.topLayout.setProperty("style", "baseMenuHeader") + self.topLayout.addWidget(QLabel("" + self.cp.name + "")) + self.topLayout.addWidget( + QLabel("{} / {} / {}".format(self.cp.base.total_planes, self.cp.base.total_armor, self.cp.base.total_aa))) + + units = { + CAP: db.find_unittype(CAP, self.game.player_name), + Embarking: db.find_unittype(Embarking, self.game.player_name), + AirDefence: db.find_unittype(AirDefence, self.game.player_name), + CAS: db.find_unittype(CAS, self.game.player_name), + PinpointStrike: db.find_unittype(PinpointStrike, self.game.player_name), + } + + self.mainLayout = QVBoxLayout() + self.mainLayout.addLayout(self.topLayout) + + print(units) + tasks = list(units.keys()) + tasks_per_column = 3 + + purchaseLayout = QGridLayout() + self.bought_amount_labels = {} + + if not self.cp.captured: + column = 0 + for i, tasks_column in [(i, tasks[idx:idx+tasks_per_column]) for i, idx in enumerate(range(0, len(tasks), tasks_per_column))]: + row = 2 + + def purchase_row(unit_type, unit_price): + layout = QHBoxLayout() + existing_units = self.cp.base.total_units_of_type(unit_type) + scheduled_units = self.deliveryEvent.units.get(unit_type, 0) + + unitName = QLabel(db.unit_type_name(unit_type)) + amountBought = QLabel("{} ({})".format(existing_units, scheduled_units)) + self.bought_amount_labels[unit_type] = amountBought + + price = QLabel("{}m".format(unit_price)) + + buy = QPushButton("+") + buy.clicked.connect(lambda: self.buy(unit_type)) + + sell = QPushButton("-") + sell.clicked.connect(lambda: self.sell(unit_type)) + + layout.addWidget(unitName) + layout.addWidget(amountBought) + layout.addWidget(price) + + layout.addWidget(buy) + layout.addWidget(sell) + + return layout + + for task_type in tasks_column: + QLabel("{}".format(db.task_name(task_type))) + row += 1 + + units_column = list(set(units[task_type])) + units_column.sort(key=lambda x: db.PRICES[x]) + for unit_type in units_column: + layout = purchase_row(unit_type, db.PRICES[unit_type]) + self.mainLayout.addLayout(layout) + + column += 5 + + + self.setLayout(self.mainLayout) + + + def _update_count_label(self, unit_type: UnitType): + self.bought_amount_labels[unit_type].setText("({}{})".format( + self.cp.base.total_units_of_type(unit_type), + unit_type in self.deliveryEvent.units and ", bought {}".format(self.deliveryEvent.units[unit_type]) or "" + )) + + def buy(self, unit_type): + price = db.PRICES[unit_type] + if self.game.budget >= price: + self.deliveryEvent.deliver({unit_type: 1}) + self.game.budget -= price + + self._update_count_label(unit_type) + + def sell(self, unit_type): + if self.deliveryEvent.units.get(unit_type, 0) > 0: + price = db.PRICES[unit_type] + self.game.budget += price + self.deliveryEvent.units[unit_type] = self.deliveryEvent.units[unit_type] - 1 + if self.deliveryEvent.units[unit_type] == 0: + del self.deliveryEvent.units[unit_type] + elif self.cp.base.total_units_of_type(unit_type) > 0: + price = db.PRICES[unit_type] + self.game.budget += price + self.cp.base.commit_losses({unit_type: 1}) + + self._update_count_label(unit_type) \ No newline at end of file diff --git a/qt_ui/windows/QLiberationWindow.py b/qt_ui/windows/QLiberationWindow.py index 11fc8ee5..67f47cde 100644 --- a/qt_ui/windows/QLiberationWindow.py +++ b/qt_ui/windows/QLiberationWindow.py @@ -17,11 +17,12 @@ class QLiberationWindow(QMainWindow): def __init__(self): super(QLiberationWindow, self).__init__() - self.game = persistency.restore_game() + + self.setGame(persistency.restore_game()) self.setGeometry(300, 100, 270, 100) self.setWindowTitle("DCS Liberation") - self.setWindowIcon(QIcon("../resources/icon.png")) + self.setWindowIcon(QIcon("./resources/icon.png")) self.statusBar().showMessage('Ready') self.initUi() @@ -29,6 +30,8 @@ class QLiberationWindow(QMainWindow): self.initMenuBar() self.initToolbar() self.connectSignals() + self.onGameGenerated(self.game) + def initUi(self): @@ -40,7 +43,8 @@ class QLiberationWindow(QMainWindow): vbox = QVBoxLayout() vbox.setMargin(0) vbox.addWidget(QTopPanel(self.game)) - vbox.addLayout(hbox) + vbox.addWidget(self.liberation_map) + #vbox.addLayout(hbox) central_widget = QWidget() central_widget.setLayout(vbox) @@ -113,14 +117,16 @@ class QLiberationWindow(QMainWindow): def newGame(self): wizard = NewGameWizard(self) wizard.show() - wizard.accepted.connect(lambda: self.setGame(wizard.generatedGame)) + wizard.accepted.connect(lambda: self.onGameGenerated(wizard.generatedGame)) def saveGame(self): print("Saving game") persistency.save_game(self.game) GameUpdateSignal.get_instance().updateGame(self.game) - def onGameGenerated(self): + def onGameGenerated(self, game: Game): + print("On Game generated") + self.game = game GameUpdateSignal.get_instance().updateGame(self.game) def setGame(self, game: Game):