Home | Documentation | Download | Screenshots | Developer |
Illustration of the MouseGrabber
class.
A MouseGrabber
is an abstract class which defines objects that are sensitive to the mouse.
When activated, they grab the mouse events. Possible applications are interactive 2D or 3D GUI,
object auto-selection, auto drop-down menu and much more.
In this example, MouseGrabbers are defined and associated with Camera keyFrame paths or position. Clicking a MouseGrabber will play (resp. restore) the Camera path (resp. position).
#include <QGLViewer/qglviewer.h> class CameraPathPlayer : public qglviewer::MouseGrabber { public: CameraPathPlayer(int nb) : pathNb(nb) {}; void checkIfGrabsMouse(int x, int y, const qglviewer::Camera* const camera); int yPos() { return 25*pathNb + 25; }; protected: void mousePressEvent(QMouseEvent* const, qglviewer::Camera* const camera) { camera->playKeyFramePath(pathNb); }; private: int pathNb; }; class Viewer : public QGLViewer { protected : virtual void init(); virtual void draw(); virtual QString helpString() const; void displayPlayers(); void updatePlayers(); private: CameraPathPlayer** player_; };
#include "mouseGrabber.h" using namespace qglviewer; using namespace std; void CameraPathPlayer::checkIfGrabsMouse(int x, int y, const Camera* const) { // Rectangular activation array - May have to be tune depending on your default font size setGrabsMouse((x < 80) && (y<yPos()) && ((yPos()-y) < 16)); } void Viewer::displayPlayers() { for (int i=0; i<camera()->nbPaths(); ++i) { CameraPathPlayer* cpp = player_[i]; if (cpp) { QString s; if (cpp->grabsMouse()) { glColor3f(1,1,1); if (camera()->keyFrameInterpolator(i)->numberOfKeyFrames() > 1) s = "Play path F" + QString::number(i+1); else s = "Restore pos F" + QString::number(i+1); } else { glColor3f(0.6, 0.6, 0.6); if (camera()->keyFrameInterpolator(i)->numberOfKeyFrames() > 1) s = "Path F" + QString::number(i+1); else s = "Pos F" + QString::number(i+1); } glDisable(GL_LIGHTING); drawText(10, cpp->yPos()-3, s); } } } void Viewer::updatePlayers() { for (int i=0; i<camera()->nbPaths(); ++i) { // Check if CameraPathPlayer is still valid if ((player_[i]) && (!camera()->keyFrameInterpolator(i))) { delete player_[i]; player_[i] = NULL; } // Or add it if needed if ((camera()->keyFrameInterpolator(i)) && (!player_[i])) player_[i] = new CameraPathPlayer(i); } } void Viewer::init() { // Absolutely needed for MouseGrabber setMouseTracking(true); // In order to make the manipulatedFrame displacements clearer setDrawAxis(); // Initialize the CameraPathPlayer MouseGrabber array player_ = new CameraPathPlayer*[camera()->nbPaths()]; for (int i=0; i<camera()->nbPaths(); ++i) player_[i] = NULL; setManipulatedFrame(new ManipulatedFrame()); restoreFromFile(); help(); } void Viewer::draw() { glMultMatrixd(manipulatedFrame()->matrix()); // Visual feedback for the manipulatedFrame grab. if (manipulatedFrame()->grabsMouse()) drawAxis(0.9); else drawAxis(0.7); // Draw a spiral const float nbSteps = 500.0; glBegin(GL_QUAD_STRIP); for (float i=0; i<nbSteps; ++i) { float ratio = i/nbSteps; float angle = 21.0*ratio; float c = cos(angle); float s = sin(angle); float r1 = 1.0 - 0.8*ratio; float r2 = 0.8 - 0.8*ratio; float alt = ratio - 0.5; const float nor = .5; const float up = sqrt(1.0-nor*nor); glColor3f(1-ratio, .2 , ratio); glNormal3f(nor*c, up, nor*s); glVertex3f(r1*c, alt, r1*s); glVertex3f(r2*c, alt+0.05, r2*s); } glEnd(); updatePlayers(); displayPlayers(); } QString Viewer::helpString() const { QString text("<h2>M o u s e G r a b b e r </h2>"); text += "This example illustrates the use of <i>MouseGrabber</i>, which is an abstract "; text += "class for objects that react (usually when the mouse hovers over them).<br><br>"; text += "Define new camera paths (or positions) using <b>Alt</b>+[<b>F1</b>-<b>F12</b>]. "; text += "New <i>MouseGrabbers</i> are created and displayed in the left corner. "; text += "Note how they react to the mouse, and click them to play the associated path.<br><br>"; text += "<i>ManipulatedFrame</i>, such as the one which defines the spiral position, are "; text += "also <i>MouseGrabbers</i>. When the mouse is close to the axis origin, the <i>ManipulatedFrame</i> "; text += "will react to mouse click, as if the <b>Control</b> key was pressed. This is very convenient "; text += "to intuitively move scene objects (such as lights) without any key or GUI interaction.<br><br>"; text += "Note that <i>setMouseTracking()</i> must be enabled to enable <i>MouseGrabbers</i>."; return text; }
#include "mouseGrabber.h" #include <qapplication.h> int main(int argc, char** argv) { // Read command lines arguments. QApplication application(argc,argv); // Instantiate the viewer, show it on screen. Viewer viewer; viewer.show(); // Set the viewer as the application main widget. application.setMainWidget(&viewer); // Run main loop. return application.exec(); }
Go back to the examples main page