12.3. How to use Drag event and Recognizer

Example 12.4, “ DragGestureRecognizer creates and registers a SoWinTouchScreen SoWinTouchScreen in order to release Windows touch events. It creates an SoDragGestureRecognizer and adds it into the recognizer list. A SoEventCallback SoEventCallback SoEventCallback is created to manipulate SoDragGestureEvent events.

Example 12.4.  DragGestureRecognizer

#include <Inventor/Qt/SoQt.h>
#include <Inventor/nodes/SoOrthographicCamera.h>
#include <Inventor/nodes/SoSeparator.h>
#include <Inventor/nodes/SoEventCallback.h>
#include <Inventor/nodes/SoDirectionalLight.h>
#include <Inventor/nodes/SoCone.h>
#include <Inventor/touch/devices/SoQtTouchScreen.h>
#include "SoDragGestureRecognizer.h"
#include <Inventor/nodes/SoMaterial.h>

SoOrthographicCamera *myCamera;
SoQtRenderArea *myRenderArea;
SoMaterial* color;

void myDragCB(void *userData, SoEventCallback *eventCB) {
  SoDragGestureEvent *dragEvent = (SoDragGestureEvent*)eventCB->getEvent();
  SbColor init = color->diffuseColor[0];
  SbVec2f dist = dragEvent->getDraggedDistance();

  color->diffuseColor.setValue(init[0] + dist[0]/10000, init[1] + dist[1]/10000, init[2]);
}

int main(int, char **argv)
{
  SoDragGestureRecognizer dragGestureRecognizer;

  Widget appWindow = SoQt::init(argv[0]);
  if (appWindow == NULL) exit(1);

  // Create and set up the root node
  SoSeparator *root = new SoSeparator;
  root->ref();

  // Add a camera
  myCamera = new SoOrthographicCamera;
  root->addChild(myCamera);
  root->addChild(new SoDirectionalLight);
  
  color = new SoMaterial;
  root->addChild(color);
  SoCone* cone = new SoCone;
  root->addChild(cone);

  myRenderArea = new SoQtRenderArea(appWindow);

  SoEventCallback *myEventCB = new SoEventCallback;

  SoDragGestureEvent::initClass();

  myEventCB->addEventCallback(SoDragGestureEvent::getClassTypeId(), myDragCB, myRenderArea->getSceneManager()->getSceneGraph());
  root->addChild(myEventCB);

  // Register the touch screen device.
  SoQtTouchScreen touchScreenDevice(appWindow);
  myRenderArea->registerDevice(&touchScreenDevice);

  touchScreenDevice.getTouchManager()->addRecognizer(&dragGestureRecognizer);

  myRenderArea->setSceneGraph(root);
  myRenderArea->setTitle("DragGestureRecognizer");

  // Make the camera see the whole scene
  SbViewportRegion viewportRegion = myRenderArea->getViewportRegion();
  myCamera->viewAll(root, viewportRegion, 2.0f);
  
  // Show our application window, and loop forever...
  myRenderArea->show();
  SoQt::show(appWindow);
  SoQt::mainLoop();

  root->unref();

  SoDragGestureEvent::exitClass();

  delete myRenderArea;
  SoQt::finish();

  return 0;
}