| 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 motion. 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 associated with Camera keyFrame paths or position. Clicking a MouseGrabber will play (resp. restore) the Camera path (resp. position).
#include "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 :
void init();
void draw();
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
setGrabsMouse((x < 80) && (abs(y-yPos()) < 6));
}
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);
}
drawText(10, cpp->yPos()-3, s.latin1(), 12);
}
}
}
void Viewer::updatePlayers()
{
for (int i=0; i<camera()->nbPaths(); ++i)
{
// Check 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);
player_ = new CameraPathPlayer*[camera()->nbPaths()];
for (int i=0; i<camera()->nbPaths(); ++i)
player_[i] = NULL;
setManipulatedFrame(new ManipulatedFrame());
help();
restoreFromFile();
}
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();
startScreenCoordinatesSystem();
displayPlayers();
stopScreenCoordinatesSystem();
}
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 which react when the mouse passes over them.<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 lower left corner. ";
text += "Note how they react to the mouse, and click to play the associated path.<br>";
text += "<i>ManipulatedFrame</i>, such as the one which defines the spiral position, are ";
text += "also <i>MouseGrabbers</i>. If 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.";
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();
}
Back to the main page