#include #include #include #include #include #include #include #include #include #include #include #ifdef WIN32 #include #else #endif #if __cplusplus > 199711L #include #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; #if __cplusplus <= 199711L this->CurrentClickTime = time(0) * 1000; #else this->CurrentClickTime = std::chrono::duration_cast( std::chrono::system_clock::now().time_since_epoch() ); #endif } else { #if __cplusplus <= 199711L this->CurrentClickTime = time(0) * 1000; #else this->CurrentClickTime = std::chrono::duration_cast( std::chrono::system_clock::now().time_since_epoch() ); #endif } //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; #if __cplusplus <= 199711L seconds = difftime(this->CurrentClickTime, this->LastClickTime); #else seconds = this->CurrentClickTime - this->LastClickTime; #endif if (seconds <= 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 #if __cplusplus <= 199711L time_t LastClickTime; time_t CurrentClickTime; #else std::chrono::milliseconds LastClickTime; std::chrono::milliseconds CurrentClickTime; #endif }; vtkStandardNewMacro(MouseInteractorStyleDoubleClick); int main(int, char *[]) { vtkSmartPointer sphereSource = vtkSmartPointer::New(); sphereSource->SetCenter(0.0, 0.0, 0.0); sphereSource->SetRadius(5.0); sphereSource->Update(); vtkSmartPointer mapper = vtkSmartPointer::New(); mapper->SetInputConnection(sphereSource->GetOutputPort()); vtkSmartPointer actor = vtkSmartPointer::New(); actor->SetMapper(mapper); vtkSmartPointer renderer = vtkSmartPointer::New(); renderer->SetBackground(1, 1, 1); // Background color white renderer->AddActor(actor); vtkSmartPointer renderWindow = vtkSmartPointer::New(); renderWindow->AddRenderer(renderer); vtkSmartPointer renderWindowInteractor = vtkSmartPointer::New(); renderWindowInteractor->SetRenderWindow(renderWindow); vtkSmartPointer style = vtkSmartPointer::New(); renderWindowInteractor->SetInteractorStyle(style); renderWindowInteractor->Initialize(); renderWindowInteractor->Start(); return EXIT_SUCCESS; }