]> Creatis software - cpPlugins.git/blob - appli/examples/example_Test_DoubleClick.cxx
Merge branch 'master' of ssh://git.creatis.insa-lyon.fr/cpPlugins
[cpPlugins.git] / appli / examples / example_Test_DoubleClick.cxx
1 #include <vtkRenderWindow.h>
2 #include <vtkRenderWindowInteractor.h>
3 #include <vtkRenderer.h>
4 #include <vtkSphereSource.h>
5 #include <vtkPolyDataMapper.h>
6 #include <vtkActor.h>
7 #include <vtkSmartPointer.h>
8 #include <vtkPointPicker.h>
9 #include <vtkCamera.h>
10 #include <vtkInteractorStyleTrackballCamera.h>
11 #include <vtkObjectFactory.h>
12
13 #ifdef WIN32
14 #include <windows.h>
15 #else
16 #endif
17
18 // Define interaction style
19 class MouseInteractorStyleDoubleClick : public vtkInteractorStyleTrackballCamera
20 {
21 public:
22
23         static MouseInteractorStyleDoubleClick* New();
24         vtkTypeMacro(MouseInteractorStyleDoubleClick, vtkInteractorStyleTrackballCamera);
25
26         MouseInteractorStyleDoubleClick() : NumberOfClicks(0), ResetPixelDistance(5), DoubleClickTolerance(1000)
27         {
28                 this->PreviousPosition[0] = 0;
29                 this->PreviousPosition[1] = 0;
30 #ifdef WIN32
31                 this->DoubleClickTolerance = GetDoubleClickTime();
32 #else
33                 // I dont have any idea if this has an equivalent for linux
34                 //this->DoubleClickTolerance = GetDoubleClickTime();
35 #endif
36         }
37
38         virtual void OnLeftButtonDown()
39         {
40                 //std::cout << "Pressed left mouse button." << std::endl;
41                 this->NumberOfClicks++;
42
43                 if (this->NumberOfClicks > 1)
44                 {
45                         this->LastClickTime = this->CurrentClickTime;
46                         this->CurrentClickTime = time(0);
47                 } else
48                 {
49                         this->CurrentClickTime = time(0);
50                 }
51
52                 //std::cout << "NumberOfClicks = " << this->NumberOfClicks << std::endl;
53                 int pickPosition[2];
54                 this->GetInteractor()->GetEventPosition(pickPosition);
55
56                 int xdist = pickPosition[0] - this->PreviousPosition[0];
57                 int ydist = pickPosition[1] - this->PreviousPosition[1];
58
59                 this->PreviousPosition[0] = pickPosition[0];
60                 this->PreviousPosition[1] = pickPosition[1];
61
62                 int moveDistance = (int)sqrt((double)(xdist*xdist + ydist*ydist));
63
64                 // Reset numClicks - If mouse moved further than resetPixelDistance
65                 if (moveDistance > this->ResetPixelDistance)
66                 {
67                         this->NumberOfClicks = 1;
68                 }
69
70
71                 if (this->NumberOfClicks == 2)
72                 {
73                         double  seconds = difftime(this->CurrentClickTime, this->LastClickTime);
74                         if (seconds * 1000 <= this->DoubleClickTolerance)
75                         {
76                                 std::cout << "Double clicked. with " << seconds << " of diference" << std::endl;
77                                 this->NumberOfClicks = 0;
78                         } else
79                         {
80                                 std::cout << "Too slow to be considered as a double click" << std::endl;
81                                 this->NumberOfClicks = 1;
82                         }                       
83                         
84                 }
85                 // forward events
86                 vtkInteractorStyleTrackballCamera::OnLeftButtonDown();
87         }
88
89 private:
90         unsigned int NumberOfClicks;
91         int PreviousPosition[2];
92         int ResetPixelDistance;
93         double DoubleClickTolerance; // time in miliseconds
94         time_t LastClickTime;
95         time_t CurrentClickTime;
96 };
97 vtkStandardNewMacro(MouseInteractorStyleDoubleClick);
98
99 int main(int, char *[])
100 {
101         vtkSmartPointer<vtkSphereSource> sphereSource =
102                 vtkSmartPointer<vtkSphereSource>::New();
103         sphereSource->SetCenter(0.0, 0.0, 0.0);
104         sphereSource->SetRadius(5.0);
105         sphereSource->Update();
106
107         vtkSmartPointer<vtkPolyDataMapper> mapper =
108                 vtkSmartPointer<vtkPolyDataMapper>::New();
109         mapper->SetInputConnection(sphereSource->GetOutputPort());
110
111         vtkSmartPointer<vtkActor> actor =
112                 vtkSmartPointer<vtkActor>::New();
113         actor->SetMapper(mapper);
114
115         vtkSmartPointer<vtkRenderer> renderer =
116                 vtkSmartPointer<vtkRenderer>::New();
117         renderer->SetBackground(1, 1, 1); // Background color white
118         renderer->AddActor(actor);
119
120         vtkSmartPointer<vtkRenderWindow> renderWindow =
121                 vtkSmartPointer<vtkRenderWindow>::New();
122         renderWindow->AddRenderer(renderer);
123
124         vtkSmartPointer<vtkRenderWindowInteractor> renderWindowInteractor =
125                 vtkSmartPointer<vtkRenderWindowInteractor>::New();
126         renderWindowInteractor->SetRenderWindow(renderWindow);
127
128         vtkSmartPointer<MouseInteractorStyleDoubleClick> style =
129                 vtkSmartPointer<MouseInteractorStyleDoubleClick>::New();
130         renderWindowInteractor->SetInteractorStyle(style);
131
132         renderWindowInteractor->Initialize();
133         renderWindowInteractor->Start();
134
135         return EXIT_SUCCESS;
136 }