Fix multiple issues with screenshake code (#812)

* Major cleanup of screenshake code

* Add pre-5.10 support for screenshake math

* more compat, uglier too

* add surprise tool

* we don't need inline functions
This commit is contained in:
Rosemary Witchaven 2022-07-23 10:19:06 -05:00 committed by GitHub
parent 8138187f92
commit 0519abab03
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 19 additions and 11 deletions

View File

@ -52,6 +52,9 @@
#include <QMessageBox>
#include <QParallelAnimationGroup>
#include <QPropertyAnimation>
#if QT_VERSION > QT_VERSION_CHECK(5, 10, 0)
#include <QRandomGenerator> //added in Qt 5.10
#endif
#include <QRegExp>
#include <QScrollBar>
#include <QTextBoundaryFinder>

View File

@ -2626,33 +2626,38 @@ void Courtroom::do_screenshake()
// This way, the animation is reset in such a way that last played screenshake
// would return to its "final frame" properly. This properly resets all UI
// elements without having to bother keeping track of "origin" positions.
// Works great wit the chat text being detached from the chat box!
// Works great with the chat text being detached from the chat box!
screenshake_animation_group->setCurrentTime(
screenshake_animation_group->duration());
screenshake_animation_group->clear();
QList<QWidget *> affected_list = {ui_vp_background, ui_vp_player_char,
const QList<QWidget *> &affected_list = {ui_vp_background, ui_vp_player_char,
ui_vp_sideplayer_char, ui_vp_chatbox};
// I would prefer if this was its own "shake" function to be honest.
foreach (QWidget *ui_element, affected_list) {
for (QWidget *ui_element : affected_list) {
QPropertyAnimation *screenshake_animation =
new QPropertyAnimation(ui_element, "pos", this);
QPoint pos_default = QPoint(ui_element->x(), ui_element->y());
int duration = 300; // How long does the screenshake last
int frequency = 20; // How often in ms is there a "jolt" frame
int maxframes = duration / frequency;
int max_x = 7; // Max deviation from origin on x axis
int max_y = 7; // Max deviation from origin on y axis
// Maximum deviation from the origin position. This is 7 pixels for a 256x192 viewport,
// so we scale that value in accordance with the current viewport height so the shake
// is roughly the same intensity regardless of viewport size. Done as a float operation for maximum accuracy.
int max_deviation = 7 * (float(ui_viewport->height()) / 192);
int maxframes = 15; // duration / frequency;
screenshake_animation->setDuration(duration);
for (int frame = 0; frame < maxframes; frame++) {
double fraction = double(frame * frequency) / duration;
int rng = qrand(); // QRandomGenerator::global()->generate();
int rand_x = max_x - (int(rng) % (max_x * 2));
int rand_y = max_y - (int(rng + 100) % (max_y * 2));
screenshake_animation->setKeyValueAt(
fraction, QPoint(pos_default.x() + rand_x, pos_default.y() + rand_y));
#if QT_VERSION < QT_VERSION_CHECK(5, 10, 0)
int rand_x = max_deviation * (2 * (qrand() / (float)RAND_MAX) - 1) + 1;
int rand_y = max_deviation * (2 * (qrand() / (float)RAND_MAX) - 1) + 1;
#else
int rand_x = QRandomGenerator::system()->bounded(-max_deviation, max_deviation);
int rand_y = QRandomGenerator::system()->bounded(-max_deviation, max_deviation);
#endif
screenshake_animation->setKeyValueAt(fraction, QPoint(pos_default.x() + rand_x, pos_default.y() + rand_y));
}
screenshake_animation->setEndValue(pos_default);
screenshake_animation->setEasingCurve(QEasingCurve::Linear);