]> Creatis software - cpPlugins.git/commitdiff
double click example test
authorjose guzman <jose@gmail.com>
Wed, 30 Sep 2015 14:16:32 +0000 (16:16 +0200)
committerjose guzman <jose@gmail.com>
Wed, 30 Sep 2015 14:16:32 +0000 (16:16 +0200)
appli/examples/CMakeLists.txt
appli/examples/example_Test_DoubleClick.cxx [new file with mode: 0644]

index a4fb5d7995ea8a760cdb9c184002cbb17627edfe..d7e157911476bfb870e4d389934bef5f85d17b66 100644 (file)
@@ -34,6 +34,7 @@ ENDFOREACH(prog)
 SET(
   NOPLUGINS_EXAMPLES_PROGRAMS
   example_ContourWidget
+  example_Test_DoubleClick
   ## example_ExtractDICOMSeries
   ## example_ImageGaussianModelEstimator
   ## example_ReadQuadEdgeMeshWithoutPlugins
diff --git a/appli/examples/example_Test_DoubleClick.cxx b/appli/examples/example_Test_DoubleClick.cxx
new file mode 100644 (file)
index 0000000..bf357d2
--- /dev/null
@@ -0,0 +1,136 @@
+#include <vtkRenderWindow.h>
+#include <vtkRenderWindowInteractor.h>
+#include <vtkRenderer.h>
+#include <vtkSphereSource.h>
+#include <vtkPolyDataMapper.h>
+#include <vtkActor.h>
+#include <vtkSmartPointer.h>
+#include <vtkPointPicker.h>
+#include <vtkCamera.h>
+#include <vtkInteractorStyleTrackballCamera.h>
+#include <vtkObjectFactory.h>
+
+#ifdef WIN32
+#include <windows.h>
+#else
+#endif
+
+// Define interaction style
+class MouseInteractorStyleDoubleClick : public vtkInteractorStyleTrackballCamera
+{
+public:
+
+       static MouseInteractorStyleDoubleClick* New();
+       vtkTypeMacro(MouseInteractorStyleDoubleClick, vtkInteractorStyleTrackballCamera);
+
+       MouseInteractorStyleDoubleClick() : NumberOfClicks(0), ResetPixelDistance(5), DoubleClickTolerance(1000)
+       {
+               this->PreviousPosition[0] = 0;
+               this->PreviousPosition[1] = 0;
+#ifdef WIN32
+               this->DoubleClickTolerance = GetDoubleClickTime();
+#else
+               // I dont have any idea if this has an equivalent for linux
+               //this->DoubleClickTolerance = GetDoubleClickTime();
+#endif
+       }
+
+       virtual void OnLeftButtonDown()
+       {
+               //std::cout << "Pressed left mouse button." << std::endl;
+               this->NumberOfClicks++;
+
+               if (this->NumberOfClicks > 1)
+               {
+                       this->LastClickTime = this->CurrentClickTime;
+                       this->CurrentClickTime = time(0);
+               } else
+               {
+                       this->CurrentClickTime = time(0);
+               }
+
+               //std::cout << "NumberOfClicks = " << this->NumberOfClicks << std::endl;
+               int pickPosition[2];
+               this->GetInteractor()->GetEventPosition(pickPosition);
+
+               int xdist = pickPosition[0] - this->PreviousPosition[0];
+               int ydist = pickPosition[1] - this->PreviousPosition[1];
+
+               this->PreviousPosition[0] = pickPosition[0];
+               this->PreviousPosition[1] = pickPosition[1];
+
+               int moveDistance = (int)sqrt((double)(xdist*xdist + ydist*ydist));
+
+               // Reset numClicks - If mouse moved further than resetPixelDistance
+               if (moveDistance > this->ResetPixelDistance)
+               {
+                       this->NumberOfClicks = 1;
+               }
+
+
+               if (this->NumberOfClicks == 2)
+               {
+                       double  seconds = difftime(this->CurrentClickTime, this->LastClickTime);
+                       if (seconds * 1000 <= this->DoubleClickTolerance)
+                       {
+                               std::cout << "Double clicked. with " << seconds << " of diference" << std::endl;
+                               this->NumberOfClicks = 0;
+                       } else
+                       {
+                               std::cout << "Too slow to be considered as a double click" << std::endl;
+                               this->NumberOfClicks = 1;
+                       }                       
+                       
+               }
+               // forward events
+               vtkInteractorStyleTrackballCamera::OnLeftButtonDown();
+       }
+
+private:
+       unsigned int NumberOfClicks;
+       int PreviousPosition[2];
+       int ResetPixelDistance;
+       double DoubleClickTolerance; // time in miliseconds
+       time_t LastClickTime;
+       time_t CurrentClickTime;
+};
+vtkStandardNewMacro(MouseInteractorStyleDoubleClick);
+
+int main(int, char *[])
+{
+       vtkSmartPointer<vtkSphereSource> sphereSource =
+               vtkSmartPointer<vtkSphereSource>::New();
+       sphereSource->SetCenter(0.0, 0.0, 0.0);
+       sphereSource->SetRadius(5.0);
+       sphereSource->Update();
+
+       vtkSmartPointer<vtkPolyDataMapper> mapper =
+               vtkSmartPointer<vtkPolyDataMapper>::New();
+       mapper->SetInputConnection(sphereSource->GetOutputPort());
+
+       vtkSmartPointer<vtkActor> actor =
+               vtkSmartPointer<vtkActor>::New();
+       actor->SetMapper(mapper);
+
+       vtkSmartPointer<vtkRenderer> renderer =
+               vtkSmartPointer<vtkRenderer>::New();
+       renderer->SetBackground(1, 1, 1); // Background color white
+       renderer->AddActor(actor);
+
+       vtkSmartPointer<vtkRenderWindow> renderWindow =
+               vtkSmartPointer<vtkRenderWindow>::New();
+       renderWindow->AddRenderer(renderer);
+
+       vtkSmartPointer<vtkRenderWindowInteractor> renderWindowInteractor =
+               vtkSmartPointer<vtkRenderWindowInteractor>::New();
+       renderWindowInteractor->SetRenderWindow(renderWindow);
+
+       vtkSmartPointer<MouseInteractorStyleDoubleClick> style =
+               vtkSmartPointer<MouseInteractorStyleDoubleClick>::New();
+       renderWindowInteractor->SetInteractorStyle(style);
+
+       renderWindowInteractor->Initialize();
+       renderWindowInteractor->Start();
+
+       return EXIT_SUCCESS;
+}
\ No newline at end of file