barotrauma-sunken-tapes/gui/tape_editor_widgets.py

333 lines
12 KiB
Python

from widgets import *
def change_font_size(label: QLabel | QLineEdit, factor: float) -> QLabel:
f = label.font()
f.setPointSizeF(f.pointSizeF() * factor)
label.setFont(f)
return label
class IdentifierEditWidget(QLineEdit):
def __init__(self, tape: dict):
super().__init__()
self.tape = tape
self.setFont("Consolas")
self.setStyleSheet("font-weight: bold; background-color: #cfd8dc;")
self.setTextMargins(2, 0, 2, 0)
self.setFrame(QFrame.Shape.NoFrame)
self.setText(self.tape["identifier"])
change_font_size(self, 1.41)
self.load()
self.textChanged.connect(self.save)
def load(self):
self.setText(self.tape["identifier"])
def save(self):
self.tape["identifier"] = self.text()
class CoverEditWidget(QLabel):
def __init__(self, tape: dict):
super().__init__()
self.tape = tape
pix = QPixmap(f"./source/images/{self.tape['identifier']}.png")
self.pixmap = pix.scaled(350, 350,
Qt.AspectRatioMode.KeepAspectRatio,
Qt.TransformationMode.SmoothTransformation)
self.resize(350, 0)
self.setPixmap(self.pixmap)
class NameEditWidget(QLineEdit):
def __init__(self, tape: dict):
super().__init__()
self.tape = tape
self.load()
self.textChanged.connect(self.save)
def load(self):
self.setText(self.tape["name"])
def save(self):
self.tape["name"] = self.text()
class SourceEditWidget(UrlWidget):
def __init__(self, tape: dict):
super().__init__()
self.tape = tape
self.load()
self.url_lineedit.textChanged.connect(self.save)
def load(self):
self.url_lineedit.setText(self.tape["source"])
def save(self):
self.tape["source"] = self.url_lineedit.text()
class ProcessEditWidget(QWidget):
def __init__(self, tape: dict):
super().__init__()
self.tape = tape
self.cbox_clip_start = QCheckBox("Clip at start")
self.cbox_clip_end = QCheckBox("Clip at end")
self.cbox_volume_factor = QCheckBox("Volume factor")
self.clip_start = TimeEditWithoutWheel()
self.clip_start.setDisplayFormat("hh:mm:ss.z")
self.clip_end = TimeEditWithoutWheel()
self.clip_end.setDisplayFormat("hh:mm:ss.z")
self.volume_factor = SpinBoxWithoutWheel()
self.volume_factor.setSuffix(" dB")
self.volume_factor.setMinimum(-30)
self.volume_factor.setMaximum(30)
layout = QGridLayout(self)
layout.setContentsMargins(5, 0, 5, 0)
layout.setColumnStretch(3, 1)
layout.addWidget(self.cbox_clip_start, 0, 0)
layout.addWidget(self.cbox_clip_end, 1, 0)
layout.addWidget(self.cbox_volume_factor, 2, 0)
layout.addWidget(self.clip_start, 0, 1)
layout.addWidget(self.clip_end, 1, 1)
layout.addWidget(self.volume_factor, 2, 1)
self.load()
self.clip_start.timeChanged.connect(self.save)
self.clip_end.timeChanged.connect(self.save)
self.volume_factor.valueChanged.connect(self.save)
self.cbox_clip_start.toggled.connect(self.save)
self.cbox_clip_end.toggled.connect(self.save)
self.cbox_volume_factor.toggled.connect(self.save)
self.save()
def load(self):
clip_start, clip_end = self.tape["process"]["start"], self.tape["process"]["end"]
start, end = QTime(), QTime()
if clip_start is not None:
start_time = datetime.datetime.strptime(clip_start, "%H:%M:%S.%f")
start.setHMS(start_time.hour, start_time.minute, start_time.second, ms=start_time.microsecond * 1e-3)
self.cbox_clip_start.setChecked(clip_start != "00:00:00.0")
self.clip_start.setTime(start)
if clip_end is not None:
end_time = datetime.datetime.strptime(clip_end, "%H:%M:%S.%f")
end.setHMS(end_time.hour, end_time.minute, end_time.second, ms=end_time.microsecond * 1e-3)
self.cbox_clip_end.setChecked(clip_end != "00:00:00.0")
self.clip_end.setTime(end)
volume_factor = self.tape["process"]["volume"]
if volume_factor is not None:
self.volume_factor.setValue(volume_factor)
self.cbox_volume_factor.setChecked(volume_factor != 0)
def save(self):
start = self.clip_start.time()
start = f"{start.hour():02}:{start.minute():02}:{start.second():02}.{start.msec():03}"
end = self.clip_end.time()
end = f"{end.hour():02}:{end.minute():02}:{end.second():02}.{end.msec():03}"
volume = self.volume_factor.value()
self.tape["process"]["start"] = start if self.cbox_clip_start.isChecked() else None
self.tape["process"]["end"] = end if self.cbox_clip_end.isChecked() else None
self.tape["process"]["volume"] = volume if self.cbox_volume_factor.isChecked() else None
class EconomyEditWidget(QWidget):
def __init__(self, tape: dict):
super().__init__()
self.tape = tape
self.spinbox_price = SpinBoxWithoutWheel()
self.spinbox_price.setRange(10, 100000)
self.spinbox_price.setSuffix(" mk")
self.spinbox_price.setSizePolicy(QSizePolicy.Policy.Maximum, QSizePolicy.Policy.Maximum)
layout = QGridLayout(self)
layout.setContentsMargins(5, 0, 5, 0)
layout.addWidget(self.spinbox_price, 1, 0)
layout.setColumnStretch(4, 1)
for i, text in enumerate(["Price", "Sold in", "Factor", " Local price"]):
layout.addWidget(change_font_size(QLabel(text), 0.8), 0, i)
self.economy = {"outpost": {},
"city": {},
"research": {},
"military": {},
"mine": {}}
for i, (location, editors) in enumerate(self.economy.items()):
editors["cbox"] = QCheckBox(location.capitalize())
editors["float"] = DoubleSpinBoxWithoutWheel()
editors["float"].setSingleStep(0.05)
editors["label"] = QLabel()
editors["label"].setFont("consolas")
layout.addWidget(editors["cbox"], int(i) + 1, 1)
layout.addWidget(editors["float"], int(i) + 1, 2)
layout.addWidget(editors["label"], int(i) + 1, 3)
self.load()
self.update_labels()
self.spinbox_price.valueChanged.connect(self.save)
for editors in self.economy.values():
editors["cbox"].toggled.connect(self.save)
editors["float"].valueChanged.connect(self.save)
def load(self):
self.spinbox_price.setValue(self.tape["price"])
for locale in self.tape["economy"]:
self.economy[locale["location"]]["cbox"].setChecked(locale["sold"])
self.economy[locale["location"]]["float"].setValue(locale["factor"])
def save(self):
self.tape["price"] = self.spinbox_price.value()
self.tape["economy"] = [{"location": location,
"factor": editors["float"].value(),
"sold": editors["cbox"].isChecked()} for location, editors in self.economy.items()]
self.update_labels()
def update_labels(self):
for editors in self.economy.values():
editors["label"].setText(f" {self.tape['price'] * editors['float'].value():4.0f} mk")
class SpawnEditWidget(QWidget):
def __init__(self, tape: dict):
super().__init__()
self.tape = tape
layout = QGridLayout(self)
layout.setContentsMargins(5, 0, 5, 0)
layout.setColumnStretch(2, 1)
for i, text in enumerate(["Place", "Probability"]):
layout.addWidget(change_font_size(QLabel(text), 0.8), 0, i)
self.spawn = {"outpostcrewcabinet": {"label": QLabel("Outpost Crew Cabinet"),
"float": ProbabilitySpinBox()},
"wreckstorage": {"label": QLabel("Wreck Storage"),
"float": ProbabilitySpinBox()},
"abandonedcrewcab": {"label": QLabel("Abandoned Crew Cabinet"),
"float": ProbabilitySpinBox()},
"abandonedstoragecab": {"label": QLabel("Abandoned Storage Cabinet"),
"float": ProbabilitySpinBox()}}
for i, (location, editors) in enumerate(self.spawn.items()):
layout.addWidget(editors["label"], int(i) + 1, 0)
layout.addWidget(editors["float"], int(i) + 1, 1)
self.load()
for i, (location, editors) in enumerate(self.spawn.items()):
editors["float"].valueChanged.connect(self.save)
def load(self):
for locale in self.tape["spawn"]:
self.spawn[locale["location"]]["float"].setValue(locale["probability"])
def save(self):
self.tape["spawn"] = [{"location": location,
"probability": editors["float"].value()} for location, editors in self.spawn.items() if
editors["float"].value() > 0]
# TODO: afflictions with shorter range for walkman songs.
class CraftingEditWidget(QWidget):
def __init__(self, tape: dict):
super().__init__()
self.tape = tape
self.int_uses = SpinBoxWithoutWheel()
self.int_uses.setMinimum(1)
self.int_uses.setMaximum(1000)
self.description = QPlainTextEdit("One use is duration of the tape")
self.description.setEnabled(False)
self.description.setMaximumHeight(QFontMetrics(self.description.font()).height() * 1.7)
layout = QHBoxLayout(self)
layout.addWidget(QLabel("Number of uses:"))
layout.addWidget(self.int_uses)
layout.addWidget(self.description)
layout.addStretch()
layout.setContentsMargins(5, 0, 5, 0)
self.load()
self.int_uses.valueChanged.connect(self.save)
def load(self):
self.int_uses.setValue(self.tape["no_of_uses"])
def save(self):
self.tape["no_of_uses"] = self.int_uses.value()
class AfflictionsEditWidget(QWidget):
def __init__(self, tape: dict):
super().__init__()
self.tape = tape
self.description = QPlainTextEdit("Factor 1.0 means that the affliction will reach the "
"highest level after being exposed to the sound of the "
"tape for its play duration.")
self.description.setEnabled(False)
self.description.setMaximumWidth(212)
layout = QGridLayout(self)
layout.setContentsMargins(5, 0, 5, 0)
layout.addWidget(change_font_size(QLabel("Affliction"), 0.8), 0, 0)
layout.addWidget(change_font_size(QLabel("Factor"), 0.8), 0, 1)
layout.addWidget(self.description, 1, 2, 3, 1)
layout.setColumnStretch(3, 1)
self.afflictions = {"strengthen": {},
"haste": {},
"psychosis": {}}
for i, (affliction, editors) in enumerate(self.afflictions.items()):
editors["cbox"] = QCheckBox(affliction.capitalize())
editors["float"] = DoubleSpinBoxWithoutWheel()
editors["float"].setSingleStep(0.01)
layout.addWidget(editors["cbox"], int(i) + 1, 0)
layout.addWidget(editors["float"], int(i) + 1, 1)
self.load()
for editors in self.afflictions.values():
editors["cbox"].toggled.connect(self.save)
editors["float"].valueChanged.connect(self.save)
def load(self):
if self.tape["afflictions"]:
for affliction in self.tape["afflictions"]:
self.afflictions[affliction["name"]]["cbox"].setChecked(True)
self.afflictions[affliction["name"]]["float"].setValue(affliction["factor"])
def save(self):
self.tape["afflictions"] = [{"name": affliction, "factor": editors["float"].value()} for affliction, editors in
self.afflictions.items() if editors["cbox"].isChecked()]