Allow toggling player/AI pilot state.

https://github.com/dcs-liberation/dcs_liberation/issues/276
This commit is contained in:
Dan Albert 2021-05-26 20:09:51 -07:00
parent b38d271f10
commit a52043ef29
2 changed files with 50 additions and 1 deletions

View File

@ -452,6 +452,12 @@ class SquadronModel(QAbstractListModel):
"""Returns the pilot located at the given index.""" """Returns the pilot located at the given index."""
return self.squadron.pilot_at_index(index.row()) return self.squadron.pilot_at_index(index.row())
def toggle_ai_state(self, index: QModelIndex) -> None:
pilot = self.pilot_at_index(index)
self.beginResetModel()
pilot.player = not pilot.player
self.endResetModel()
class GameModel: class GameModel:
"""A model for the Game object. """A model for the Game object.

View File

@ -1,13 +1,17 @@
import logging
from PySide2.QtCore import ( from PySide2.QtCore import (
QItemSelectionModel, QItemSelectionModel,
QModelIndex, QModelIndex,
Qt, Qt,
QItemSelection,
) )
from PySide2.QtWidgets import ( from PySide2.QtWidgets import (
QAbstractItemView, QAbstractItemView,
QDialog, QDialog,
QListView, QListView,
QVBoxLayout, QVBoxLayout,
QPushButton,
) )
from game.squadrons import Pilot from game.squadrons import Pilot
@ -61,6 +65,7 @@ class SquadronDialog(QDialog):
def __init__(self, squadron_model: SquadronModel, parent) -> None: def __init__(self, squadron_model: SquadronModel, parent) -> None:
super().__init__(parent) super().__init__(parent)
self.squadron_model = squadron_model
self.setMinimumSize(1000, 440) self.setMinimumSize(1000, 440)
self.setWindowTitle(squadron_model.squadron.name) self.setWindowTitle(squadron_model.squadron.name)
@ -69,4 +74,42 @@ class SquadronDialog(QDialog):
layout = QVBoxLayout() layout = QVBoxLayout()
self.setLayout(layout) self.setLayout(layout)
layout.addWidget(PilotList(squadron_model)) self.pilot_list = PilotList(squadron_model)
self.pilot_list.selectionModel().selectionChanged.connect(
self.on_selection_changed
)
layout.addWidget(self.pilot_list)
self.toggle_ai_button = QPushButton()
self.reset_button_state(self.pilot_list.currentIndex())
self.toggle_ai_button.setProperty("style", "start-button")
self.toggle_ai_button.clicked.connect(self.toggle_ai)
layout.addWidget(self.toggle_ai_button, alignment=Qt.AlignRight)
def toggle_ai(self) -> None:
index = self.pilot_list.currentIndex()
if not index.isValid():
logging.error("Cannot toggle player/AI: no pilot is selected")
return
self.squadron_model.toggle_ai_state(index)
def reset_button_state(self, index: QModelIndex) -> None:
if not self.squadron_model.squadron.aircraft.flyable:
self.toggle_ai_button.setText("Not flyable")
self.toggle_ai_button.setDisabled(True)
return
if not index.isValid():
self.toggle_ai_button.setText("No pilot selected")
self.toggle_ai_button.setDisabled(True)
return
self.toggle_ai_button.setEnabled(True)
pilot = self.squadron_model.pilot_at_index(index)
self.toggle_ai_button.setText(
"Convert to AI" if pilot.player else "Convert to player"
)
def on_selection_changed(
self, selected: QItemSelection, _deselected: QItemSelection
) -> None:
index = selected.indexes()[0]
self.reset_button_state(index)