This commit is contained in:
Auric Vente 2024-07-21 05:50:25 -06:00
parent 34896e24e2
commit 3d8ff1f167
4 changed files with 67 additions and 34 deletions

View File

@ -70,16 +70,11 @@ class Ants:
@staticmethod
def prepare() -> None:
Ants.get()
Ants.fill()
@staticmethod
def hatch(num: int = 1) -> None:
from .game import Game
if len(Ants.ants) >= Config.max_ants:
Window.alert("Max ants reached\nTerminate some to hatch new ones")
return
now = Utils.now()
for _ in range(num):
@ -91,9 +86,6 @@ class Ants:
Ants.ants.append(ant)
Ants.announce_hatch(ant)
if len(Ants.ants) >= Config.max_ants:
break
Ants.save()
Game.update_info()
@ -179,28 +171,16 @@ class Ants:
@staticmethod
def get() -> None:
objs = Storage.get_ants()
changed = False
if len(objs) > Config.max_ants:
objs = objs[: Config.max_ants]
changed = True
for obj in objs:
ant = Ant()
ant.from_dict(obj)
Ants.ants.append(ant)
if changed:
Ants.save()
@staticmethod
def fill() -> None:
diff = Config.max_ants - len(Ants.ants)
if diff <= 0:
return
Ants.hatch(diff)
def populate(num: int) -> None:
Ants.clear()
Ants.hatch(num)
@staticmethod
def random_name() -> str:

View File

@ -10,7 +10,6 @@ class Config:
program: str = "cromulant"
width: int = 820
height: int = 900
max_ants: int = 100
here: Path
ants_json: Path
icon_path: Path

View File

@ -273,10 +273,19 @@ class Game:
@staticmethod
def restart() -> None:
def action() -> None:
Ants.clear()
Window.clear_view()
Ants.fill()
Game.start_loop()
opts = ["25", "50", "100", "200", "300", "400", "500"]
opts = [f"{opt} ants" for opt in opts]
size = Window.prompt_combobox("Size of the population", opts, 2)
Window.confirm("Restart the ants?", action)
if not size:
return
num = int(size.split(" ")[0])
Window.clear_view()
Ants.populate(num)
Game.start_loop()
@staticmethod
def update_size() -> None:
pass

View File

@ -5,10 +5,12 @@ from collections.abc import Callable
import signal
from PySide6.QtWidgets import QApplication # type: ignore
from PySide6.QtWidgets import QDialog
from PySide6.QtWidgets import QMainWindow
from PySide6.QtWidgets import QWidget
from PySide6.QtWidgets import QGraphicsScene
from PySide6.QtWidgets import QVBoxLayout
from PySide6.QtWidgets import QLabel
from PySide6.QtWidgets import QPushButton
from PySide6.QtWidgets import QHBoxLayout
from PySide6.QtWidgets import QScrollArea
@ -37,6 +39,39 @@ class FilterLineEdit(QLineEdit): # type: ignore
super().keyPressEvent(e)
class ComboBoxDialog(QDialog): # type: ignore
def __init__(self, message: str, options: list[str], defindex: int) -> None:
super().__init__()
self.setWindowTitle("Select Option")
self.setFixedSize(300, 150) # Set a fixed size for the dialog
self.layout = QVBoxLayout()
self.label = QLabel(message)
self.layout.addWidget(self.label)
self.combo_box = QComboBox()
self.combo_box.addItems(options)
self.combo_box.setCurrentIndex(defindex)
self.layout.addWidget(self.combo_box)
self.button_layout = QHBoxLayout()
self.ok_button = QPushButton("OK")
self.ok_button.clicked.connect(self.accept)
self.button_layout.addWidget(self.ok_button)
self.cancel_button = QPushButton("Cancel")
self.cancel_button.clicked.connect(self.reject)
self.button_layout.addWidget(self.cancel_button)
self.layout.addLayout(self.button_layout)
self.setLayout(self.layout)
def get_selection(self) -> str:
return str(self.combo_box.currentText())
class Window:
app: QApplication
window: QMainWindow
@ -107,23 +142,23 @@ class Window:
color: {Config.alt_hover_text_color};
}}
QMessageBox {{
QDialog {{
background-color: {Config.alt_background_color};
color: {Config.alt_text_color};
border: 1px solid {Config.alt_border_color};
}}
QMessageBox QLabel {{
QDialog QLabel {{
background-color: {Config.alt_background_color};
color: {Config.alt_text_color};
}}
QMessageBox QPushButton {{
QDialog QPushButton {{
background-color: {Config.alt_background_color};
color: {Config.alt_text_color};
}}
QMessageBox QPushButton:hover {{
QDialog QPushButton:hover {{
background-color: {Config.message_box_button_hover_background_color};
color: {Config.message_box_button_hover_text_color};
}}
@ -179,6 +214,7 @@ class Window:
root = QWidget()
container = QHBoxLayout()
btn_restart = QPushButton("Restart")
btn_restart.setToolTip("Restart with a new set of ants")
btn_restart.clicked.connect(Game.restart)
@ -329,3 +365,12 @@ class Window:
msg_box.setWindowTitle("Information")
msg_box.setStandardButtons(QMessageBox.Ok)
msg_box.exec()
@staticmethod
def prompt_combobox(message: str, options: list[str], defindex: int = 0) -> str:
dialog = ComboBoxDialog(message, options, defindex)
if dialog.exec() == QDialog.Accepted:
return dialog.get_selection()
return ""