Wavelength
Privacy-focused, cross-platform, and open-source communication application
Loading...
Searching...
No Matches
floating_energy_sphere_widget.h
Go to the documentation of this file.
1#ifndef FLOATING_ENERGY_SPHERE_WIDGET_H
2#define FLOATING_ENERGY_SPHERE_WIDGET_H
3
4#include <deque>
5#include <QAudioFormat>
6#include <QElapsedTimer>
7#include <QMatrix4x4>
8#include <QMediaPlayer>
9#include <QOpenGLBuffer>
10#include <QOpenGLWidget>
11#include <QVector3D>
12#include <QOpenGLFunctions_3_3_Core>
13#include <QOpenGLVertexArrayObject>
14#include <QTimer>
15#include <QtMultimedia/QAudioDecoder>
16#include <QtMultimedia/QAudioBuffer>
17
19#define MAX_IMPACTS 5
20
21class QOpenGLShaderProgram;
25struct ImpactInfo {
27 QVector3D point = QVector3D(0, 0, 0);
29 float start_time = -1.0f;
31 bool active = false;
32};
33
45class FloatingEnergySphereWidget final : public QOpenGLWidget, protected QOpenGLFunctions_3_3_Core {
46 Q_OBJECT
47
48public:
56 explicit FloatingEnergySphereWidget(bool is_first_time, QWidget *parent = nullptr);
57
63
69 void SetClosable(bool closable);
70
71signals:
74
77
80
81protected:
86 void initializeGL() override;
87
94 void resizeGL(int w, int h) override;
95
101 void paintGL() override;
102
107 void mousePressEvent(QMouseEvent *event) override;
108
114 void mouseMoveEvent(QMouseEvent *event) override;
115
120 void mouseReleaseEvent(QMouseEvent *event) override;
121
126 void wheelEvent(QWheelEvent *event) override;
127
133 void closeEvent(QCloseEvent *event) override;
134
139 void keyPressEvent(QKeyEvent *event) override;
140
141private slots:
147 void UpdateAnimation();
148
153 void ProcessAudioBuffer();
154
158 void AudioDecodingFinished() const;
159
163 void HandleMediaPlayerError() const;
164
169
173 void PlayKonamiHint() const;
174
181 void HandlePlayerStateChanged(QMediaPlayer::State state);
182
189
193 void SimulateClick();
194
195private:
202 void SetupSphereGeometry(int rings, int sectors);
203
207 void SetupShaders();
208
213 void SetupAudio();
214
222 float CalculateRMSAmplitude(const QAudioBuffer &buffer) const;
223
230 void TriggerImpactAtScreenPos(const QPoint &screen_position);
231
234
236 QTimer timer_;
239
241 QOpenGLShaderProgram *shader_program_;
243 QOpenGLBuffer vbo_;
245 QOpenGLVertexArrayObject vao_;
246
250 QMatrix4x4 view_matrix_;
252 QMatrix4x4 model_matrix_;
253
258
264 QQuaternion rotation_;
265
267 std::vector<GLfloat> vertices_;
270
273
278
280 QElapsedTimer elapsed_timer_;
283
285 QMediaPlayer *media_player_;
287 QAudioDecoder *audio_decoder_;
289 std::vector<float> audio_amplitudes_;
291 QAudioFormat audio_format_;
300
302 std::vector<ImpactInfo> impacts_;
305
307 std::deque<int> key_sequence_;
309 const std::vector<int> konami_code_;
311 QTimer *hint_timer_;
312
318 const QString kGoodbyeSoundFilename = "goodbye.wav";
319
322};
323
324#endif // FLOATING_ENERGY_SPHERE_WIDGET_H
float damping_factor_
Damping factor applied to angular velocity for inertia decay.
Definition floating_energy_sphere_widget.h:277
QMatrix4x4 view_matrix_
View matrix transforming world space to view space.
Definition floating_energy_sphere_widget.h:250
void UpdateAnimation()
Slot called periodically by timer_ to update animation state. Advances time, updates audio amplitude ...
Definition floating_energy_sphere_widget.cpp:476
void AudioDecodingFinished() const
Slot called by the audio decoder when it finishes decoding the entire source file.
Definition floating_energy_sphere_widget.cpp:640
QMediaPlayer * media_player_
Media player for playing audio files (initial sound, hint, destruction).
Definition floating_energy_sphere_widget.h:285
QElapsedTimer elapsed_timer_
Timer used for calculating delta time between animation frames.
Definition floating_energy_sphere_widget.h:280
void widgetClosed()
Emitted when the widget is closed normally (not during destruction).
float current_audio_amplitude_
Current audio amplitude value used for rendering (smoothed).
Definition floating_energy_sphere_widget.h:295
QVector3D angular_velocity_
Current angular velocity used for inertial rotation.
Definition floating_energy_sphere_widget.h:275
void HandleAudioDecoderError()
Slot called when the QAudioDecoder encounters an error. Logs the error.
Definition floating_energy_sphere_widget.cpp:652
std::vector< GLfloat > vertices_
Raw vertex data (positions) for the sphere points.
Definition floating_energy_sphere_widget.h:267
QTimer * hint_timer_
Timer triggering the Konami code audio hints after inactivity.
Definition floating_energy_sphere_widget.h:311
void SetupShaders()
Compiles and links the vertex and fragment shaders into shader_program_.
Definition floating_energy_sphere_widget.cpp:358
int next_impact_index_
Index of the next slot to use in the impacts_ vector (circular).
Definition floating_energy_sphere_widget.h:304
float time_value_
Time value passed to shaders, incremented in UpdateAnimation.
Definition floating_energy_sphere_widget.h:238
void HandleMediaPlayerError() const
Slot called when the QMediaPlayer encounters an error. Logs the error.
Definition floating_energy_sphere_widget.cpp:644
std::deque< int > key_sequence_
Stores the sequence of recently pressed keys for Konami code detection.
Definition floating_energy_sphere_widget.h:307
bool audio_ready_
Flag indicating if audio data has been successfully decoded and is ready for visualization.
Definition floating_energy_sphere_widget.h:299
float camera_distance_
Distance from the camera to the origin (center of the sphere). Used for zooming.
Definition floating_energy_sphere_widget.h:257
void keyPressEvent(QKeyEvent *event) override
Handles key press events. Detects the Konami code sequence.
Definition floating_energy_sphere_widget.cpp:712
void PlayKonamiHint() const
Slot called by hint_timer_ after a period of inactivity to play the Konami code audio hint.
Definition floating_energy_sphere_widget.cpp:812
void TriggerImpactAtScreenPos(const QPoint &screen_position)
Triggers a visual impact effect at a point on the sphere corresponding to a screen position....
Definition floating_energy_sphere_widget.cpp:865
FloatingEnergySphereWidget(bool is_first_time, QWidget *parent=nullptr)
Constructs a FloatingEnergySphereWidget. Initializes OpenGL settings, timers, audio components,...
Definition floating_energy_sphere_widget.cpp:158
void SimulateClick()
Slot called periodically by click_simulation_timer_ to trigger random impacts on the sphere.
Definition floating_energy_sphere_widget.cpp:930
void mouseMoveEvent(QMouseEvent *event) override
Handles mouse move events. Rotates the sphere based on mouse dragging when the left button is pressed...
Definition floating_energy_sphere_widget.cpp:665
std::vector< ImpactInfo > impacts_
Stores information about active impact effects.
Definition floating_energy_sphere_widget.h:302
void closeEvent(QCloseEvent *event) override
Handles the widget close event. Stops timers and audio, emits widgetClosed() if allowed (closable_ is...
Definition floating_energy_sphere_widget.cpp:846
QQuaternion rotation_
Current rotation of the sphere as a quaternion.
Definition floating_energy_sphere_widget.h:264
qint64 audio_buffer_duration_ms_
Duration of a single audio buffer processed by the decoder, in milliseconds.
Definition floating_energy_sphere_widget.h:293
void paintGL() override
Renders the OpenGL scene. Called whenever the widget needs to be repainted. Binds shaders and VAO,...
Definition floating_energy_sphere_widget.cpp:426
QAudioFormat audio_format_
Format of the decoded audio data (sample rate, channels, etc.).
Definition floating_energy_sphere_widget.h:291
void mousePressEvent(QMouseEvent *event) override
Handles mouse press events. Accepts the event, but the primary logic is in mouseMoveEvent.
Definition floating_energy_sphere_widget.cpp:661
QVector3D camera_position_
Position of the camera in world space.
Definition floating_energy_sphere_widget.h:255
QTimer timer_
Timer driving the main animation loop (UpdateAnimation).
Definition floating_energy_sphere_widget.h:236
QAudioDecoder * audio_decoder_
Decoder for extracting audio data from files for amplitude analysis.
Definition floating_energy_sphere_widget.h:287
QOpenGLVertexArrayObject vao_
OpenGL Vertex Array Object managing vertex buffer state.
Definition floating_energy_sphere_widget.h:245
QTimer * click_simulation_timer_
Timer triggering simulated random clicks/impacts on the sphere.
Definition floating_energy_sphere_widget.h:321
const QString kGoodbyeSoundFilename
Filename of the audio played during the destruction sequence.
Definition floating_energy_sphere_widget.h:318
QPoint last_mouse_position_
Stores the last mouse position during dragging for rotation calculation.
Definition floating_energy_sphere_widget.h:260
float destruction_progress_
Progress of the destruction animation (0.0 to 1.0).
Definition floating_energy_sphere_widget.h:316
void resizeGL(int w, int h) override
Handles resizing of the OpenGL viewport and updates the projection matrix. Calculates the field of vi...
Definition floating_energy_sphere_widget.cpp:405
void ProcessAudioBuffer()
Slot called by the audio decoder when a new buffer of decoded audio data is ready....
Definition floating_energy_sphere_widget.cpp:535
float target_audio_amplitude_
Target audio amplitude based on the currently playing audio buffer.
Definition floating_energy_sphere_widget.h:297
bool is_destroying_
Flag indicating if the destruction animation is currently active.
Definition floating_energy_sphere_widget.h:314
QOpenGLBuffer vbo_
OpenGL Vertex Buffer Object storing sphere vertex data.
Definition floating_energy_sphere_widget.h:243
QOpenGLShaderProgram * shader_program_
The compiled and linked OpenGL shader program.
Definition floating_energy_sphere_widget.h:241
void SetupSphereGeometry(int rings, int sectors)
Creates the vertex data (positions) for the sphere geometry using rings and sectors....
Definition floating_energy_sphere_widget.cpp:383
~FloatingEnergySphereWidget() override
Destructor. Cleans up OpenGL resources (VBO, VAO, shader), stops timers, and stops audio playback/dec...
Definition floating_energy_sphere_widget.cpp:229
void initializeGL() override
Initializes OpenGL resources (functions, shaders, geometry, VAO/VBO) and audio setup....
Definition floating_energy_sphere_widget.cpp:247
float last_frame_time_secs_
Timestamp of the last animation frame in seconds.
Definition floating_energy_sphere_widget.h:282
void mouseReleaseEvent(QMouseEvent *event) override
Handles mouse release events. Stops direct rotation tracking and allows inertia to take over.
Definition floating_energy_sphere_widget.cpp:689
QMatrix4x4 model_matrix_
Model matrix transforming model space to world space (includes rotation).
Definition floating_energy_sphere_widget.h:252
int vertex_count_
Total number of vertices in the sphere geometry.
Definition floating_energy_sphere_widget.h:269
void SetupAudio()
Initializes audio components (QMediaPlayer, QAudioDecoder), connects signals, selects the appropriate...
Definition floating_energy_sphere_widget.cpp:289
QMatrix4x4 projection_matrix_
Projection matrix transforming view space to clip space.
Definition floating_energy_sphere_widget.h:248
bool closable_
Flag indicating if the widget can be closed.
Definition floating_energy_sphere_widget.h:272
bool is_first_time_
Flag indicating if this is the first time the widget/app is run (affects initial audio).
Definition floating_energy_sphere_widget.h:233
const std::vector< int > konami_code_
The target key sequence for the Konami code.
Definition floating_energy_sphere_widget.h:309
void destructionSequenceFinished()
Emitted when the destruction animation sequence (visual and audio) completes.
void StartDestructionSequence()
Initiates the destruction animation sequence. Sets the is_destroying_ flag, resets progress,...
Definition floating_energy_sphere_widget.cpp:749
void wheelEvent(QWheelEvent *event) override
Handles mouse wheel events. Zooms the camera in or out by adjusting camera_distance_.
Definition floating_energy_sphere_widget.cpp:698
std::vector< float > audio_amplitudes_
Stores calculated RMS amplitude values for each decoded audio buffer.
Definition floating_energy_sphere_widget.h:289
void SetClosable(bool closable)
Sets whether the widget can be closed by the user or programmatically. Used to prevent closing during...
Definition floating_energy_sphere_widget.cpp:243
void konamiCodeEntered()
Emitted when the Konami code sequence is successfully entered. (Deprecated, destruction starts direct...
void HandlePlayerStateChanged(QMediaPlayer::State state)
Slot called when the QMediaPlayer's state changes (e.g., Playing, Stopped). Used to detect the end of...
Definition floating_energy_sphere_widget.cpp:782
bool mouse_pressed_
Flag indicating if the left mouse button is currently pressed.
Definition floating_energy_sphere_widget.h:262
float CalculateRMSAmplitude(const QAudioBuffer &buffer) const
Calculates the Root Mean Square (RMS) amplitude for a given audio buffer. Used to quantify the energy...
Definition floating_energy_sphere_widget.cpp:571
Structure holding information about a single impact effect on the sphere.
Definition floating_energy_sphere_widget.h:25
float start_time
Time (from time_value_) when the impact started. -1.0f if inactive.
Definition floating_energy_sphere_widget.h:29
QVector3D point
Point of impact in local sphere coordinates (normalized).
Definition floating_energy_sphere_widget.h:27
bool active
Flag indicating if this impact slot is currently active.
Definition floating_energy_sphere_widget.h:31