/* * (c) LSIIT, UMR CNRS/UdS * Authors: O. Génevaux, F. Larue. * * See licence.txt for additional information. */ #ifndef DISPLAYERFACTORYINTERFACE_H #define DISPLAYERFACTORYINTERFACE_H //#include #include "SelectionTool.h" #include "Box.h" #include "UIParam.h" #include "ToolCategory.h" #include "UIContext.h" #include "UIData.h" class UIParamSet; class SelectionManager; enum DisplayDoF { DISPLAY_DOF_NONE = 0x00, DISPLAY_DOF_2D = 0x01, DISPLAY_DOF_3D = 0x02, DISPLAY_DOF_BOTH = DISPLAY_DOF_2D | DISPLAY_DOF_3D }; class DisplayableInterface : public QObject { Q_OBJECT private: GLViewer *m_Viewer; GenericUIData *m_Data; bool m_IsAnimationPaused; bool m_IsSelected; QString concatenateResourcesContent( const QStringList &resourceNames ) const { QString res; for( auto &rscName : resourceNames ) res += getResourceContent( rscName ); return res; } protected: inline QString getResourceContent( const QString &resourceName ) const { QString content; QFile rscFile( resourceName ); if( !rscFile.open( QIODevice::ReadOnly | QIODevice::Text ) ) qDebug() << "Unable to load resource \"" << resourceName << "\"."; else content = QTextStream( &rscFile ).readAll(); return content; } inline bool shaderFromResources( GPU::Shader &shader, const QString &vshResourceName, const QString &fshResourceName, std::string *logs = NULL ) const { return shaderFromResources( shader, QStringList(vshResourceName), QStringList(fshResourceName), logs ); } inline bool shaderFromResources( GPU::Shader &shader, const QStringList &vshResourceNames, const QStringList &fshResourceNames, std::string *logs = NULL ) const { std::string vshSrc = concatenateResourcesContent(vshResourceNames).toStdString(); std::string fshSrc = concatenateResourcesContent(fshResourceNames).toStdString(); return GPU::CreateShaderFromSources( shader, vshSrc, fshSrc, logs ); } inline bool shaderFromResources( GPU::Shader &shader, const QString &vshResourceName, const QString &gshResourceName, const QString &fshResourceName, std::string *logs = NULL ) const { return shaderFromResources( shader, QStringList(vshResourceName), QStringList(gshResourceName), QStringList(fshResourceName), logs ); } inline bool shaderFromResources( GPU::Shader &shader, const QStringList &vshResourceNames, const QStringList &gshResourceNames, const QStringList &fshResourceNames, std::string *logs = NULL ) const { std::string vshSrc = concatenateResourcesContent(vshResourceNames).toStdString(); std::string gshSrc = concatenateResourcesContent(gshResourceNames).toStdString(); std::string fshSrc = concatenateResourcesContent(fshResourceNames).toStdString(); return GPU::CreateShaderFromSources( shader, vshSrc, gshSrc, fshSrc, logs ); } public: inline DisplayableInterface( GenericUIData *m, GLViewer *v ) : QObject(), m_Data(m), m_Viewer(v), m_IsAnimationPaused(false) {} virtual ~DisplayableInterface() {} /** Recover the current viewer to which this Displayable is associated. */ inline GLViewer* getViewer() const { return m_Viewer; } /** Recover the source data from which this Displayable has been created. */ inline GenericUIData* getSource() const { return m_Data; } /** Recover the current state of the set of display parameters. */ inline const UIParamSet& getParameters() const { return *m_Data->GetDisplayOptions(); } /** Function called once, after the creation of the displayer object but before its first display. * The OpenGL context is active, which means that any OpenGL operation can be done here. */ virtual void initialize( UIParamSet ¶ms ) {} /** Function called once, before the destruction of the displayer object. * The OpenGL context is active, which means that any OpenGL operation can be done here. */ virtual void release( UIParamSet ¶ms ) {} /** Recover the bounding box of the displayed item in locam frame. */ virtual void boundingBox( Box3f &box ) const = 0; /** Recover the transformation matrix of the displayed item from local to global frame. */ virtual void transformation( QMatrix4x4 &tr ) const { tr.setToIdentity(); } /** Recover the label to be displayed. */ virtual QString label() const { return QString(); } /** Recover parameters describing the display options of the current item. */ virtual void declareParameters( UIParamSet ¶ms ) {} /** Function called when the value of a parameter has changed. */ virtual void onUpdatingParameter( UIParamSet ¶ms, UIParam *p ) {} /** Function that effectively performs the display. */ virtual void onDisplay( UIParamSet ¶ms ) = 0; /** Called when the viewport of the GL widget into which this object is displayed is resized. */ virtual void onReshape( UIParamSet ¶ms ) {} /** Specify if picking is enabled for this item. */ virtual bool isPickable( UIParamSet ¶ms ) { return false; } /** Function that performs a rendering pass dedicated to point picking. */ virtual void onPicking( UIParamSet ¶ms, GPU::Shader &pickingShader, const std::string& pickingShaderVertexAttrib ) {} /** Specify if the item is currently selected. */ inline bool isSelected() const { return m_IsSelected; } /** Specify if this item can be animated. */ virtual bool isAnimated() { return false; } /** Specify a fixed timestep (in sec.), if needed. In this case, the onAnimationStep function is called only when elapsed time since the last time step exceeds the specified duration. */ virtual float animationTimeStepValue() { return 0.0f; } /** Function called when the animation is started. */ virtual void onAnimationStart() {} /** Function called when the animation is stopped. */ virtual void onAnimationStop() {} /** Function called at each animation frame, providing the time step since the last call. */ virtual void onAnimationStep( float elapsedTimeSec ) {} /** Function called when the button "Step forward" is clicked. */ virtual void onAnimationStepForward() {} /** Function called when the button "Step backward" is clicked. */ virtual void onAnimationStepBackward() {} inline bool isAnimationPaused() const { return m_IsAnimationPaused; } /** Recover the managers describing the selection behaviour for every kind of selectable entity. */ virtual void selectionManagers( QVector &mngr ) {} /** Apply a functor to every selected entity. */ virtual void processSelection( SelectionManager *mngr, BaseSelectionProcessor &proc ) {} /** Specify if this item can be grabbed and moved in the 3D viewer. */ virtual bool isGrabbable() const { return false; } /** Function called when the item grabbing motion has been validated in the 3D viewer. */ virtual void onUpdatingTransformation( const QMatrix4x4 &tr ) {} signals: void registerAnimation( DisplayableInterface *d ); void unregisterAnimation( DisplayableInterface *d ); void displayNeedUpdate(); public slots: inline void setSelected( bool selected ) { m_IsSelected = selected; } inline void updateParameter( UIParam* p ) { onUpdatingParameter(*p->Group(),p); } inline void startAnimation() { m_IsAnimationPaused = false; emit registerAnimation(this); } inline void pauseAnimation() { m_IsAnimationPaused = true; } inline void stopAnimation() { m_IsAnimationPaused = false; emit unregisterAnimation(this); } inline void animationStepForward() { onAnimationStepForward(); emit displayNeedUpdate(); } inline void animationStepBackward() { onAnimationStepBackward(); emit displayNeedUpdate(); } }; class DisplayableFactoryInterface { public: virtual ~DisplayableFactoryInterface() {} /** Type identifiers of the data that can be managed by this displayer. */ virtual void acceptedDataTypes( QVector &types ) const = 0; /** Determines the display order of all objects with respect to each other. Higher priorities are * displayed after lower ones. May be useful when transparent objects have to be displayed. */ virtual unsigned int displayPriority() const { return 0; } /** Defines the degrees of freedom (2D or 3D) required for the viewer to display this object. */ virtual DisplayDoF viewerDoF() const = 0; /** Creates an instance of a displayer object with the correct type. */ virtual DisplayableInterface* newDisplayer( GenericUIData *m, GLViewer *v ) const = 0; inline void allocateDisplayOptions( GenericUIData *d ) const { DisplayableInterface *disp = newDisplayer( d, NULL ); UIParamSet *params = new UIParamSet( d->GetBaseName(), NULL ); disp->declareParameters( *params ); params->updateChildrenLists( d ); d->SetDisplayOptions( params ); delete disp; } }; //Q_DECLARE_INTERFACE( DisplayableInterface, "PluginsInterface.Displayable" ) Q_DECLARE_INTERFACE( DisplayableFactoryInterface, "PluginsInterface.DisplayableFactory" ) #endif // DISPLAYERFACTORYINTERFACE_H