mirror of
https://github.com/dcs-liberation/dcs_liberation.git
synced 2025-11-10 14:22:26 +00:00
Allow toggling player/AI pilot state.
https://github.com/dcs-liberation/dcs_liberation/issues/276
This commit is contained in:
parent
b38d271f10
commit
a52043ef29
@ -452,6 +452,12 @@ class SquadronModel(QAbstractListModel):
|
||||
"""Returns the pilot located at the given index."""
|
||||
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:
|
||||
"""A model for the Game object.
|
||||
|
||||
@ -1,13 +1,17 @@
|
||||
import logging
|
||||
|
||||
from PySide2.QtCore import (
|
||||
QItemSelectionModel,
|
||||
QModelIndex,
|
||||
Qt,
|
||||
QItemSelection,
|
||||
)
|
||||
from PySide2.QtWidgets import (
|
||||
QAbstractItemView,
|
||||
QDialog,
|
||||
QListView,
|
||||
QVBoxLayout,
|
||||
QPushButton,
|
||||
)
|
||||
|
||||
from game.squadrons import Pilot
|
||||
@ -61,6 +65,7 @@ class SquadronDialog(QDialog):
|
||||
|
||||
def __init__(self, squadron_model: SquadronModel, parent) -> None:
|
||||
super().__init__(parent)
|
||||
self.squadron_model = squadron_model
|
||||
|
||||
self.setMinimumSize(1000, 440)
|
||||
self.setWindowTitle(squadron_model.squadron.name)
|
||||
@ -69,4 +74,42 @@ class SquadronDialog(QDialog):
|
||||
layout = QVBoxLayout()
|
||||
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)
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user