RiveQtQuickPlugin
riveqtquickitem.h
1 // SPDX-FileCopyrightText: 2023 Jeremias Bosch <jeremias.bosch@basyskom.com>
2 // SPDX-FileCopyrightText: 2023 basysKom GmbH
3 //
4 // SPDX-License-Identifier: LGPL-3.0-or-later
5 
6 #pragma once
7 
8 #include <QElapsedTimer>
9 #include <QQuickItem>
10 #include <QQuickPaintedItem>
11 #include <QSGRenderNode>
12 #include <QSGTextureProvider>
13 #include <QImage>
14 #include <QPainter>
15 #include <QTimer>
16 #include <QtCore/QtGlobal>
17 
18 #include <rive/artboard.hpp>
19 #include <rive/listener_type.hpp>
20 
21 #include "rivestatemachineinput.h"
22 #include "datatypes.h"
23 #include "renderer/riveqtfactory.h"
24 
25 #if defined(RIVEQTQUICKITEM_LIBRARY)
26 # define RIVEQTQUICKITEM_EXPORT Q_DECL_EXPORT
27 #else
28 # define RIVEQTQUICKITEM_EXPORT Q_DECL_IMPORT
29 #endif
30 
31 class RiveQSGRenderNode;
32 class RiveQSGRHIRenderNode;
33 
34 #if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
35 class RiveQSGSoftwareRenderNode;
36 #endif
45 #if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
46 class RiveQtQuickItem : public QQuickPaintedItem
47 #else
48 class RiveQtQuickItem : public QQuickItem
49 #endif
50 {
51  Q_OBJECT
52 
73  Q_PROPERTY(QString fileSource READ fileSource WRITE setFileSource NOTIFY fileSourceChanged)
74 
75 
91  Q_PROPERTY(LoadingStatus loadingStatus READ loadingStatus NOTIFY loadingStatusChanged)
92 
93 
116  Q_PROPERTY(QVector<ArtBoardInfo> artboards READ artboards NOTIFY artboardsChanged)
117 
118 
143  Q_PROPERTY(QVector<AnimationInfo> animations READ animations NOTIFY animationsChanged)
144 
145 
168  Q_PROPERTY(QVector<StateMachineInfo> stateMachines READ stateMachines NOTIFY stateMachinesChanged)
169 
170 
186  Q_PROPERTY(int currentArtboardIndex READ currentArtboardIndex WRITE setCurrentArtboardIndex NOTIFY currentArtboardIndexChanged)
187 
188 
214  Q_PROPERTY(int currentAnimationIndex READ currentAnimationIndex WRITE triggerAnimation NOTIFY currentAnimationIndexChanged)
215 
216 
232  Q_PROPERTY(
233  int currentStateMachineIndex READ currentStateMachineIndex WRITE setCurrentStateMachineIndex NOTIFY currentStateMachineIndexChanged)
234 
235 
250  Q_PROPERTY(bool interactive READ interactive WRITE setInteractive NOTIFY interactiveChanged)
251 
252 
278  Q_PROPERTY(RiveStateMachineInput *stateMachineInterface READ stateMachineInterface WRITE setStateMachineInterface NOTIFY
279  stateMachineInterfaceChanged)
280 
281 
300  Q_PROPERTY(RiveRenderSettings::RenderQuality renderQuality READ renderQuality WRITE setRenderQuality NOTIFY renderQualityChanged)
301 
302 
320  Q_PROPERTY(RiveRenderSettings::PostprocessingMode postprocessingMode READ postprocessingMode WRITE setPostprocessingMode NOTIFY
321  postprocessingModeChanged)
322 
323 
342  Q_PROPERTY(RiveRenderSettings::FillMode fillMode READ fillMode WRITE setFillMode NOTIFY fillModeChanged)
343 
344 
361  Q_PROPERTY(int frameRate READ frameRate NOTIFY frameRateChanged)
362 
363  QML_ELEMENT
364 
365 public:
368  {
369  Idle,
370  Loading,
371  Loaded,
372  Error,
373  Unloading,
374  };
375  Q_ENUM(LoadingStatus)
376 
377  RiveQtQuickItem(QQuickItem *parent = nullptr);
378  ~RiveQtQuickItem();
379 
380  void triggerAnimation(int id);
381 
382  bool isTextureProvider() const override;
383 
384  QSGTextureProvider *textureProvider() const override;
385 
386  QString fileSource() const;
387  void setFileSource(const QString &source);
388 
390 
391  int currentAnimationIndex() const;
392  int currentArtboardIndex() const;
393  void setCurrentArtboardIndex(const int newCurrentArtboardIndex);
394 
395  const QVector<ArtBoardInfo> &artboards() const;
396  const QVector<StateMachineInfo> &stateMachines() const;
397  const QVector<AnimationInfo> &animations() const;
398 
399  int currentStateMachineIndex() const;
400  void setCurrentStateMachineIndex(const int newCurrentStateMachineIndex);
401 
403  void setStateMachineInterface(RiveStateMachineInput *stateMachineInterface);
404 
405  bool interactive() const;
406  void setInteractive(bool newInteractive);
407 
408  RiveRenderSettings::PostprocessingMode postprocessingMode() const;
409  void setPostprocessingMode(const RiveRenderSettings::PostprocessingMode mode);
410 
411  RiveRenderSettings::RenderQuality renderQuality() const;
412  void setRenderQuality(const RiveRenderSettings::RenderQuality quality);
413 
414  RiveRenderSettings::FillMode fillMode() const;
415  void setFillMode(const RiveRenderSettings::FillMode fillMode);
416 
417  int frameRate();
418 
419 #if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
420  void paint(QPainter *painter) override;
421 #endif
422 
423 signals:
424  void animationsChanged();
425  void artboardsChanged();
426  void stateMachinesChanged();
427 
428  void fileSourceChanged();
429  void loadingStatusChanged();
430 
431  void currentArtboardIndexChanged();
432  void currentAnimationIndexChanged();
433  void currentStateMachineIndexChanged();
434 
435  void interactiveChanged();
436 
437  void loadFileAfterUnloading(QString fileName);
438  void internalArtboardChanged();
439  void internalStateMachineChanged();
440  void stateMachineInterfaceChanged();
441  void stateMachineStringInterfaceChanged();
442 
443  void renderQualityChanged();
444  void postprocessingModeChanged();
445  void fillModeChanged();
446 
447  void frameRateChanged();
448 
449 protected:
450  QSGNode *updatePaintNode(QSGNode *oldNode, UpdatePaintNodeData *data) override;
451  void componentComplete() override;
452 
453 #if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
454  void geometryChange(const QRectF &newGeometry, const QRectF &oldGeometry) override;
455 #else
456  void geometryChanged(const QRectF &newGeometry, const QRectF &oldGeometry) override;
457 #endif
458 
459  void mousePressEvent(QMouseEvent *event) override;
460  void mouseReleaseEvent(QMouseEvent *event) override;
461 
462  void hoverMoveEvent(QHoverEvent *event) override;
463  void hoverEnterEvent(QHoverEvent *event) override;
464  void hoverLeaveEvent(QHoverEvent *event) override;
465 
466 private:
467  void loadRiveFile(const QString &source);
468 
469  void updateInternalArtboard();
470  void updateAnimations();
471  void updateStateMachines();
472  void updateCurrentArtboardIndex();
473  void updateCurrentStateMachineIndex();
474 
475  QRectF artboardRect();
476 
477 #if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
478  void renderOffscreen();
479 #endif
480 
481  bool hitTest(const QPointF &pos, const rive::ListenerType &type);
482 
483  QVector<ArtBoardInfo> m_artboardInfoList;
484  QVector<AnimationInfo> m_animationList;
485  QVector<StateMachineInfo> m_stateMachineList;
486 
487  std::unique_ptr<rive::File> m_riveFile;
488 
489  mutable QScopedPointer<QSGTextureProvider> m_textureProvider;
490 
491  QString m_fileSource;
492  LoadingStatus m_loadingStatus { Idle };
493 
494  std::shared_ptr<rive::ArtboardInstance> m_currentArtboardInstance { nullptr };
495  std::unique_ptr<rive::LinearAnimationInstance> m_animationInstance { nullptr };
496  std::shared_ptr<rive::StateMachineInstance> m_currentStateMachineInstance { nullptr };
497 
498  bool m_scheduleArtboardChange { false };
499  bool m_scheduleStateMachineChange { false };
500 
501  int m_currentArtboardIndex { -1 };
502  int m_currentAnimationIndex { -1 };
503  int m_initialArtboardIndex { -1 };
504  int m_currentStateMachineIndex { -1 };
505  int m_initialStateMachineIndex { -1 };
506 
507  RiveStateMachineInput *m_stateMachineInterface { nullptr };
508 
509  RiveRenderSettings m_renderSettings;
510 
511  RiveQtFactory m_riveQtFactory { m_renderSettings };
512 
513  QElapsedTimer m_elapsedTimer;
514  qint64 m_lastUpdateTime;
515  bool m_geometryChanged { true };
516 
517  bool m_hasValidRenderNode { false };
518  float m_lastMouseX { 0.f };
519  float m_lastMouseY { 0.f };
520 
521  int m_frameRate { 0 };
522 
523  RiveQSGRenderNode *m_renderNode { nullptr };
524  void updateStateMachineValues();
525 
526  bool m_loadingGuard { false };
527 #if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
528  RiveQSGSoftwareRenderNode *softwareRenderNode;
529 #endif
530 };
A quick item for Rive-based animations.
Definition: riveqtquickitem.h:50
int currentArtboardIndex
Represents the currently active artboard index.
Definition: riveqtquickitem.h:186
RiveRenderSettings::RenderQuality renderQuality
Represents the render quality setting.
Definition: riveqtquickitem.h:300
bool interactive
Indicates if the item is interactive.
Definition: riveqtquickitem.h:250
LoadingStatus
Enum representing the possible loading states.
Definition: riveqtquickitem.h:368
QVector< AnimationInfo > animations
Contains a list of available animations.
Definition: riveqtquickitem.h:143
RiveRenderSettings::FillMode fillMode
Represents the fill mode setting.
Definition: riveqtquickitem.h:342
int currentAnimationIndex
Represents the currently active animation index.
Definition: riveqtquickitem.h:214
QString fileSource
Represents the source file for the Rive animation.
Definition: riveqtquickitem.h:73
LoadingStatus loadingStatus
Indicates the loading status of the Rive file.
Definition: riveqtquickitem.h:91
RiveRenderSettings::PostprocessingMode postprocessingMode
Represents the postprocessing.
Definition: riveqtquickitem.h:321
int currentStateMachineIndex
Represents the currently active state machine index.
Definition: riveqtquickitem.h:233
int frameRate
Represents the frame rate of the animation.
Definition: riveqtquickitem.h:361
RiveStateMachineInput * stateMachineInterface
Represents the state machine property interface.
Definition: riveqtquickitem.h:279
QVector< ArtBoardInfo > artboards
Contains a list of available artboards.
Definition: riveqtquickitem.h:116
QVector< StateMachineInfo > stateMachines
Contains a list of available state machines.
Definition: riveqtquickitem.h:168
Interface for binding QML properties to Rive's state machine.
Definition: rivestatemachineinput.h:150