diff --git a/apps/OpenSpace/ext/launcher/CMakeLists.txt b/apps/OpenSpace/ext/launcher/CMakeLists.txt index ff892f322a..f24b5c8962 100644 --- a/apps/OpenSpace/ext/launcher/CMakeLists.txt +++ b/apps/OpenSpace/ext/launcher/CMakeLists.txt @@ -26,12 +26,14 @@ set(HEADER_FILES include/assettreeitem.h include/assettreemodel.h include/camera.h + include/deltatimes.h include/filesystemaccess.h include/launcherwindow.h include/profileedit.h include/ui_addedscripts.h include/ui_assets.h include/ui_camera.h + include/ui_deltatimes.h include/ui_editorwindow.h include/ui_launcherwindow.h ) @@ -42,6 +44,7 @@ set(SOURCE_FILES src/assettreeitem.cpp src/assettreemodel.cpp src/camera.cpp + src/deltatimes.cpp src/filesystemaccess.cpp src/launcherwindow.cpp src/profileedit.cpp diff --git a/apps/OpenSpace/ext/launcher/include/deltatimes.h b/apps/OpenSpace/ext/launcher/include/deltatimes.h new file mode 100644 index 0000000000..855867c782 --- /dev/null +++ b/apps/OpenSpace/ext/launcher/include/deltatimes.h @@ -0,0 +1,100 @@ +#ifndef DELTATIMES_H +#define DELTATIMES_H + +#include +#include +#include + +QT_BEGIN_NAMESPACE +namespace Ui { +class deltaTimes; +} +QT_END_NAMESPACE + +static const int _defaultDeltaTimes[30] = { + 1, 2, 5, 10, 30, + 60, 120, 300, 600, 1800, + 3600, 7200, 10800, 21600, 43200, + 86400, 172800, 345600, 604800, 1209600, + 2592000, 5184000, 7776000, 15552000, 31536000, + 63072000, 157680000, 315360000, 630720000, 1576800000 +}; + +struct DeltaTimes { + std::vector _times; + DeltaTimes() { + _times.resize(sizeof(_defaultDeltaTimes)/sizeof(int)); + }; + DeltaTimes(std::vector& dt) { + _times = dt; + }; + void loadValues(std::vector& dt) { + for (size_t i = 0; i < dt.size(); ++i) { + _times[i] = dt[i]; + } + } + size_t size() { + auto it = find(_times.begin(), _times.end(), 0); + return std::distance(_times.begin(), it); + }; + size_t totalSize() { + return sizeof(_defaultDeltaTimes) / sizeof(int); + } +}; + +class deltaTimes : public QDialog +{ + Q_OBJECT + +public slots: + void listItemSelected(); + void saveDeltaTimeValue(); + void clearDeltaTimeValue(); + void parseSelections(); + +public: + explicit deltaTimes(DeltaTimes& _imported, QWidget *parent = nullptr); + ~deltaTimes(); + void setDeltaTimes(std::vector& dt); + QString createSummaryForDeltaTime(size_t idx, int dt, bool forListView); + struct timeIntervals { + int index; + int secondsPerInterval; + QString intervalName; + }; + + const int secondsPerYear = 31536000; + const int secondsPerMonth = 18144000; + const int secondsPerWeek = 604800; + const int secondsPerDay = 86400; + const int secondsPerHour = 3600; + const int secondsPerMinute = 60; + +private: + QString timeDescription(int value); + bool checkForTimeDescription(QString& description, QString unit, + int interval, int value); + QString checkForTimeDescription(int intervalIndex, int value); + int lastSelectableItem(); + bool isNumericalValue(QLineEdit* le); + + Ui::deltaTimes *ui; + QWidget* _parent; + + DeltaTimes& _imported; + DeltaTimes _data; + std::vector _deltaTimeStrings; + std::vector _deltaListItems; + + std::vector _timeIntervals = { + {0, 31536000, "year"}, + {1, 18144000, "month"}, + {2, 604800, "week"}, + {3, 86400, "day"}, + {4, 3600, "hour"}, + {5, 60, "minute"}, + {6, 1, "second"}, + }; +}; + +#endif // DELTATIMES_H diff --git a/apps/OpenSpace/ext/launcher/include/ui_deltatimes.h b/apps/OpenSpace/ext/launcher/include/ui_deltatimes.h new file mode 100644 index 0000000000..e98058d3d0 --- /dev/null +++ b/apps/OpenSpace/ext/launcher/include/ui_deltatimes.h @@ -0,0 +1,95 @@ +/******************************************************************************** +** Form generated from reading UI file 'deltaTimes.ui' +** +** Created by: Qt User Interface Compiler version 5.15.0 +** +** WARNING! All changes made in this file will be lost when recompiling UI file! +********************************************************************************/ + +#ifndef UI_DELTATIMES_H +#define UI_DELTATIMES_H + +#include +#include +#include +#include +#include +#include +#include +#include + +QT_BEGIN_NAMESPACE + +class Ui_deltaTimes +{ +public: + QDialogButtonBox *buttonBox; + QLabel *label_module; + QListWidget *listWidget; + QLabel *label_adjust; + QPushButton *button_save; + QPushButton *button_remove; + QLineEdit *line_seconds; + + void setupUi(QDialog *deltaTimes) + { + if (deltaTimes->objectName().isEmpty()) + deltaTimes->setObjectName(QString::fromUtf8("deltaTimes")); + deltaTimes->resize(531, 439); + buttonBox = new QDialogButtonBox(deltaTimes); + buttonBox->setObjectName(QString::fromUtf8("buttonBox")); + buttonBox->setGeometry(QRect(170, 390, 341, 32)); + buttonBox->setOrientation(Qt::Horizontal); + buttonBox->setStandardButtons(QDialogButtonBox::Cancel|QDialogButtonBox::Ok); + label_module = new QLabel(deltaTimes); + label_module->setObjectName(QString::fromUtf8("label_module")); + label_module->setGeometry(QRect(20, 20, 141, 20)); + QFont font; + font.setFamily(QString::fromUtf8("Arial")); + font.setPointSize(12); + label_module->setFont(font); + listWidget = new QListWidget(deltaTimes); + listWidget->setObjectName(QString::fromUtf8("listWidget")); + listWidget->setGeometry(QRect(20, 50, 381, 171)); + listWidget->setAutoScroll(true); + listWidget->setLayoutMode(QListView::SinglePass); + label_adjust = new QLabel(deltaTimes); + label_adjust->setObjectName(QString::fromUtf8("label_adjust")); + label_adjust->setGeometry(QRect(20, 284, 421, 20)); + label_adjust->setFont(font); + button_save = new QPushButton(deltaTimes); + button_save->setObjectName(QString::fromUtf8("button_save")); + button_save->setGeometry(QRect(20, 354, 71, 25)); + button_remove = new QPushButton(deltaTimes); + button_remove->setObjectName(QString::fromUtf8("button_remove")); + button_remove->setGeometry(QRect(20, 230, 151, 25)); + line_seconds = new QLineEdit(deltaTimes); + line_seconds->setObjectName(QString::fromUtf8("line_seconds")); + line_seconds->setGeometry(QRect(20, 310, 191, 31)); + line_seconds->setFont(font); + + retranslateUi(deltaTimes); + //QObject::connect(buttonBox, SIGNAL(accepted()), deltaTimes, SLOT(accept())); + QObject::connect(buttonBox, SIGNAL(rejected()), deltaTimes, SLOT(reject())); + + QMetaObject::connectSlotsByName(deltaTimes); + } // setupUi + + void retranslateUi(QDialog *deltaTimes) + { + deltaTimes->setWindowTitle(QCoreApplication::translate("deltaTimes", "Delta Time Steps", nullptr)); + label_module->setText(QCoreApplication::translate("deltaTimes", "Delta Times", nullptr)); + label_adjust->setText(QCoreApplication::translate("deltaTimes", "Set Delta Time for key", nullptr)); + button_save->setText(QCoreApplication::translate("deltaTimes", "Save", nullptr)); + button_remove->setText(QCoreApplication::translate("deltaTimes", "Remove Last Entry", nullptr)); + } // retranslateUi + +}; + +namespace Ui { + class deltaTimes: public Ui_deltaTimes {}; +} // namespace Ui + +QT_END_NAMESPACE + +#endif // UI_DELTATIMES_H diff --git a/apps/OpenSpace/ext/launcher/src/deltatimes.cpp b/apps/OpenSpace/ext/launcher/src/deltatimes.cpp new file mode 100644 index 0000000000..1afb7ab112 --- /dev/null +++ b/apps/OpenSpace/ext/launcher/src/deltatimes.cpp @@ -0,0 +1,155 @@ +#include "deltatimes.h" +#include "./ui_deltatimes.h" +#include + +deltaTimes::deltaTimes(DeltaTimes& imported, QWidget *parent) + : QDialog(parent) + , ui(new Ui::deltaTimes) + , _imported(imported) + , _data(imported) +{ + ui->setupUi(this); + + for (size_t d = 0; d < _data.totalSize(); ++d) { + QString summary = createSummaryForDeltaTime(d, _data._times.at(d), true); + _deltaListItems.push_back(new QListWidgetItem(summary)); + ui->listWidget->addItem(_deltaListItems[d]); + } + connect(ui->listWidget, SIGNAL(itemSelectionChanged()), this, SLOT(listItemSelected())); + connect(ui->button_save, SIGNAL(clicked()), this, SLOT(saveDeltaTimeValue())); + connect(ui->button_remove, SIGNAL(clicked()), this, SLOT(clearDeltaTimeValue())); + connect(ui->buttonBox, SIGNAL(accepted()), this, SLOT(parseSelections())); +} + +QString deltaTimes::createSummaryForDeltaTime(size_t idx, int dt, bool forListView) { + std::string s; + int k = (idx%10 == 9) ? 0 : idx%10 + 1; + k = (idx == 0) ? 1 : k; + if (idx >= 20) { + s = "shift + " + std::to_string(k); + } + else if (idx >= 10) { + s = "ctrl + " + std::to_string(k); + } + else { + s = std::to_string(k); + if (forListView) { + s += " "; + } + } + + if (dt != 0) { + s += (forListView) ? "\t" + std::to_string(dt) : ""; + } + + if (forListView) { + s += "\t"; + s += timeDescription(dt).toUtf8().constData(); + } + return QString(s.c_str()); +} + +void deltaTimes::listItemSelected() { + QListWidgetItem *item = ui->listWidget->currentItem(); + int index = ui->listWidget->row(item); + + if (index < (static_cast(_data.totalSize()) - 1)) { + if (index > lastSelectableItem() + 1) { + index = lastSelectableItem() + 1; + ui->listWidget->setCurrentRow(index); + } + } + + QString labelS = "Set Delta Time for key "; + labelS += createSummaryForDeltaTime(index, _data._times.at(index), false); + labelS += " :\t"; + + ui->label_adjust->setText(labelS); + ui->line_seconds->setText(QString::number(_data._times.at(index))); +} + +int deltaTimes::lastSelectableItem() { + int i; + for (i = _data.size() - 1; i >= 0; --i) { + if (_data._times.at(i) != 0) { + break; + } + } + + if (i < 0) { + return 0; + } + else if (i == (static_cast(_data.size()) - 1)) { + return _data.size() - 1; + } + else { + return i + 1; + } +} + +QString deltaTimes::timeDescription(int value) { + QString description; + + if (value == 0) { + return ""; + } + + size_t i; + for (i = 0; i < _timeIntervals.size(); ++i) { + if (value >= _timeIntervals[i].secondsPerInterval) { + break; + } + } + return checkForTimeDescription(i, value); +} + +QString deltaTimes::checkForTimeDescription(int intervalIndex, int value) { + float amount = static_cast(value) / + static_cast(_timeIntervals[intervalIndex].secondsPerInterval); + QString description = QString::number(amount, 'g', 2); + return description += " " + _timeIntervals[intervalIndex].intervalName + "/sec"; +} + +void deltaTimes::saveDeltaTimeValue() { + QListWidgetItem *item = ui->listWidget->currentItem(); + int index = ui->listWidget->row(item); + if (isNumericalValue(ui->line_seconds) && index <= lastSelectableItem() + 1) { + _data._times.at(index) = ui->line_seconds->text().toInt(); + QString summary = createSummaryForDeltaTime(index, _data._times.at(index), true); + _deltaListItems.at(index)->setText(summary); + } +} + +bool deltaTimes::isNumericalValue(QLineEdit* le) { + QString s = le->text(); + bool validConversion = false; + s.toInt(&validConversion); + return validConversion; +} + +void deltaTimes::clearDeltaTimeValue() { + int i; + for (i = _data.size() - 1; i >= 0; --i) { + if (_data._times.at(i) != 0) { + break; + } + } + if (i >= 0) { + _data._times.at(i) = 0; + QString summary = createSummaryForDeltaTime(i, _data._times.at(i), true); + _deltaListItems.at(i)->setText(summary); + ui->listWidget->setCurrentRow(i); + } +} + +void deltaTimes::parseSelections() { + _imported = _data; + accept(); +} + +deltaTimes::~deltaTimes() { + for (auto d : _deltaListItems) { + delete d; + } + delete ui; +} diff --git a/apps/OpenSpace/ext/launcher/src/deltatimes.ui b/apps/OpenSpace/ext/launcher/src/deltatimes.ui new file mode 100644 index 0000000000..493f07c1e7 --- /dev/null +++ b/apps/OpenSpace/ext/launcher/src/deltatimes.ui @@ -0,0 +1,167 @@ + + + deltaTimes + + + + 0 + 0 + 531 + 439 + + + + Delta Time Steps + + + + + 170 + 390 + 341 + 32 + + + + Qt::Horizontal + + + QDialogButtonBox::Cancel|QDialogButtonBox::Ok + + + + + + 20 + 20 + 141 + 20 + + + + + Arial + 12 + + + + Delta Times + + + + + + 20 + 50 + 381 + 171 + + + + true + + + QListView::SinglePass + + + + + + 20 + 284 + 421 + 20 + + + + + Arial + 12 + + + + Set Delta Time for key + + + + + + 20 + 354 + 71 + 25 + + + + Save + + + + + + 20 + 230 + 151 + 25 + + + + Remove Last Entry + + + + + + 20 + 310 + 191 + 31 + + + + + Arial + 12 + + + + <html><head/><body><p>Enter number of simulated seconds to elapse per actual second for the particular delta time</p></body></html> + + + + + + + buttonBox + accepted() + deltaTimes + accept() + + + 248 + 254 + + + 157 + 274 + + + + + buttonBox + rejected() + deltaTimes + reject() + + + 316 + 260 + + + 286 + 274 + + + + +