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:
		
							parent
							
								
									8138187f92
								
							
						
					
					
						commit
						0519abab03
					
				@ -52,6 +52,9 @@
 | 
				
			|||||||
#include <QMessageBox>
 | 
					#include <QMessageBox>
 | 
				
			||||||
#include <QParallelAnimationGroup>
 | 
					#include <QParallelAnimationGroup>
 | 
				
			||||||
#include <QPropertyAnimation>
 | 
					#include <QPropertyAnimation>
 | 
				
			||||||
 | 
					#if QT_VERSION > QT_VERSION_CHECK(5, 10, 0)
 | 
				
			||||||
 | 
					#include <QRandomGenerator> //added in Qt 5.10
 | 
				
			||||||
 | 
					#endif
 | 
				
			||||||
#include <QRegExp>
 | 
					#include <QRegExp>
 | 
				
			||||||
#include <QScrollBar>
 | 
					#include <QScrollBar>
 | 
				
			||||||
#include <QTextBoundaryFinder>
 | 
					#include <QTextBoundaryFinder>
 | 
				
			||||||
 | 
				
			|||||||
@ -2626,33 +2626,38 @@ void Courtroom::do_screenshake()
 | 
				
			|||||||
  // This way, the animation is reset in such a way that last played 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
 | 
					  // would return to its "final frame" properly. This properly resets all UI
 | 
				
			||||||
  // elements without having to bother keeping track of "origin" positions.
 | 
					  // 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->setCurrentTime(
 | 
				
			||||||
      screenshake_animation_group->duration());
 | 
					      screenshake_animation_group->duration());
 | 
				
			||||||
  screenshake_animation_group->clear();
 | 
					  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};
 | 
					                                    ui_vp_sideplayer_char, ui_vp_chatbox};
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  // I would prefer if this was its own "shake" function to be honest.
 | 
					  // 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 =
 | 
					    QPropertyAnimation *screenshake_animation =
 | 
				
			||||||
        new QPropertyAnimation(ui_element, "pos", this);
 | 
					        new QPropertyAnimation(ui_element, "pos", this);
 | 
				
			||||||
    QPoint pos_default = QPoint(ui_element->x(), ui_element->y());
 | 
					    QPoint pos_default = QPoint(ui_element->x(), ui_element->y());
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    int duration = 300; // How long does the screenshake last
 | 
					    int duration = 300; // How long does the screenshake last
 | 
				
			||||||
    int frequency = 20; // How often in ms is there a "jolt" frame
 | 
					    int frequency = 20; // How often in ms is there a "jolt" frame
 | 
				
			||||||
    int maxframes = duration / frequency;
 | 
					    // Maximum deviation from the origin position. This is 7 pixels for a 256x192 viewport,
 | 
				
			||||||
    int max_x = 7; // Max deviation from origin on x axis
 | 
					    // so we scale that value in accordance with the current viewport height so the shake
 | 
				
			||||||
    int max_y = 7; // Max deviation from origin on y axis
 | 
					    // 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);
 | 
					    screenshake_animation->setDuration(duration);
 | 
				
			||||||
    for (int frame = 0; frame < maxframes; frame++) {
 | 
					    for (int frame = 0; frame < maxframes; frame++) {
 | 
				
			||||||
      double fraction = double(frame * frequency) / duration;
 | 
					      double fraction = double(frame * frequency) / duration;
 | 
				
			||||||
      int rng = qrand(); // QRandomGenerator::global()->generate();
 | 
					#if QT_VERSION < QT_VERSION_CHECK(5, 10, 0)
 | 
				
			||||||
      int rand_x = max_x - (int(rng) % (max_x * 2));
 | 
					      int rand_x = max_deviation * (2 * (qrand() / (float)RAND_MAX) - 1) + 1;
 | 
				
			||||||
      int rand_y = max_y - (int(rng + 100) % (max_y * 2));
 | 
					      int rand_y = max_deviation * (2 * (qrand() / (float)RAND_MAX) - 1) + 1;
 | 
				
			||||||
      screenshake_animation->setKeyValueAt(
 | 
					#else
 | 
				
			||||||
          fraction, QPoint(pos_default.x() + rand_x, pos_default.y() + rand_y));
 | 
					      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->setEndValue(pos_default);
 | 
				
			||||||
    screenshake_animation->setEasingCurve(QEasingCurve::Linear);
 | 
					    screenshake_animation->setEasingCurve(QEasingCurve::Linear);
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
		Reference in New Issue
	
	Block a user