Commit 815a8065 authored by Julien Finet's avatar Julien Finet Committed by Marcus D. Hanwell

Add vtkContextInteractorStyle for scene interaction

Change-Id: I5b13db434760301e92b665dd5894e255dd48815a
parent acf133c2
......@@ -36,6 +36,7 @@ SET(Kit_SRCS
vtkContextBufferId.cxx
vtkContextClip.cxx
vtkContextDevice2D.cxx
vtkContextInteractorStyle.cxx
vtkContextItem.cxx
vtkContextMapper2D.cxx
vtkContextMouseEvent.h
......
/*=========================================================================
Program: Visualization Toolkit
Module: vtkContextInteractorStyle.cxx
Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
All rights reserved.
See Copyright.txt or http://www.kitware.com/Copyright.htm for details.
This software is distributed WITHOUT ANY WARRANTY; without even
the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
PURPOSE. See the above copyright notice for more information.
=========================================================================*/
#include "vtkContextInteractorStyle.h"
#include "vtkContextMouseEvent.h"
#include "vtkContextScene.h"
#include "vtkCallbackCommand.h"
#include "vtkCommand.h"
#include "vtkObjectFactory.h"
#include "vtkRenderWindow.h"
#include "vtkRenderWindowInteractor.h"
#include <cassert>
vtkStandardNewMacro(vtkContextInteractorStyle);
//--------------------------------------------------------------------------
vtkContextInteractorStyle::vtkContextInteractorStyle()
{
this->Scene = NULL;
this->ProcessingEvents = 0;
this->SceneCallbackCommand = vtkCallbackCommand::New();
this->SceneCallbackCommand->SetClientData(this);
this->SceneCallbackCommand->SetCallback(
vtkContextInteractorStyle::ProcessSceneEvents);
this->LastSceneRepaintMTime = 0;
}
//--------------------------------------------------------------------------
vtkContextInteractorStyle::~vtkContextInteractorStyle()
{
if (this->SceneCallbackCommand)
{
this->SceneCallbackCommand->Delete();
this->SceneCallbackCommand = 0;
}
}
//--------------------------------------------------------------------------
void vtkContextInteractorStyle::PrintSelf(ostream& os, vtkIndent indent)
{
this->Superclass::PrintSelf(os, indent);
os << indent << "Scene: " << this->Scene << endl;
if (this->Scene)
{
this->Scene->PrintSelf(os, indent.GetNextIndent());
}
}
//--------------------------------------------------------------------------
void vtkContextInteractorStyle::SetScene(vtkContextScene* scene)
{
if (this->Scene == scene)
{
return;
}
if (this->Scene)
{
this->Scene->RemoveObserver(this->SceneCallbackCommand);
}
this->Scene = scene;
if (this->Scene)
{
this->Scene->AddObserver(vtkCommand::ModifiedEvent,
this->SceneCallbackCommand,
this->Priority);
}
this->Modified();
}
//----------------------------------------------------------------------------
void vtkContextInteractorStyle::ProcessSceneEvents(vtkObject* object,
unsigned long event,
void* clientdata,
void* calldata)
{
vtkContextInteractorStyle* self =
reinterpret_cast<vtkContextInteractorStyle *>( clientdata );
switch (event)
{
case vtkCommand::ModifiedEvent:
self->OnSceneModified();
break;
default:
break;
}
}
//----------------------------------------------------------------------------
void vtkContextInteractorStyle::OnSceneModified()
{
if (!this->Scene
|| !this->Scene->GetDirty()
|| this->ProcessingEvents
|| this->Scene->GetMTime() == this->LastSceneRepaintMTime)
{
return;
}
this->BeginProcessingEvent();
this->LastSceneRepaintMTime = this->Scene->GetMTime();
this->Interactor->GetRenderWindow()->Render();
this->EndProcessingEvent();
}
//----------------------------------------------------------------------------
void vtkContextInteractorStyle::BeginProcessingEvent()
{
++this->ProcessingEvents;
}
//----------------------------------------------------------------------------
void vtkContextInteractorStyle::EndProcessingEvent()
{
--this->ProcessingEvents;
assert(this->ProcessingEvents >= 0);
if (this->ProcessingEvents == 0)
{
this->OnSceneModified();
}
}
//----------------------------------------------------------------------------
void vtkContextInteractorStyle::OnMouseMove()
{
this->BeginProcessingEvent();
bool eatEvent = false;
if (this->Scene)
{
int x = this->Interactor->GetEventPosition()[0];
int y = this->Interactor->GetEventPosition()[1];
eatEvent = this->Scene->MouseMoveEvent(x, y);
}
if (!eatEvent)
{
this->Superclass::OnMouseMove();
}
this->EndProcessingEvent();
}
//----------------------------------------------------------------------------
void vtkContextInteractorStyle::OnLeftButtonDown()
{
this->BeginProcessingEvent();
bool eatEvent = false;
if (this->Scene)
{
int x = this->Interactor->GetEventPosition()[0];
int y = this->Interactor->GetEventPosition()[1];
if (this->Interactor->GetRepeatCount())
{
eatEvent =
this->Scene->DoubleClickEvent(vtkContextMouseEvent::LEFT_BUTTON, x, y);
}
else
{
eatEvent =
this->Scene->ButtonPressEvent(vtkContextMouseEvent::LEFT_BUTTON, x, y);
}
}
if (!eatEvent)
{
this->Superclass::OnLeftButtonDown();
}
this->EndProcessingEvent();
}
//--------------------------------------------------------------------------
void vtkContextInteractorStyle::OnLeftButtonUp()
{
this->BeginProcessingEvent();
bool eatEvent = false;
if (this->Scene)
{
int x = this->Interactor->GetEventPosition()[0];
int y = this->Interactor->GetEventPosition()[1];
eatEvent =
this->Scene->ButtonReleaseEvent(vtkContextMouseEvent::LEFT_BUTTON, x, y);
}
if (!eatEvent)
{
this->Superclass::OnLeftButtonUp();
}
this->EndProcessingEvent();
}
//----------------------------------------------------------------------------
void vtkContextInteractorStyle::OnMiddleButtonDown()
{
this->BeginProcessingEvent();
bool eatEvent = false;
if (this->Scene)
{
int x = this->Interactor->GetEventPosition()[0];
int y = this->Interactor->GetEventPosition()[1];
if (this->Interactor->GetRepeatCount())
{
eatEvent =
this->Scene->DoubleClickEvent(vtkContextMouseEvent::MIDDLE_BUTTON, x, y);
}
else
{
eatEvent =
this->Scene->ButtonPressEvent(vtkContextMouseEvent::MIDDLE_BUTTON, x, y);
}
}
if (!eatEvent)
{
this->Superclass::OnMiddleButtonDown();
}
this->EndProcessingEvent();
}
//--------------------------------------------------------------------------
void vtkContextInteractorStyle::OnMiddleButtonUp()
{
this->BeginProcessingEvent();
bool eatEvent = false;
if (this->Scene)
{
int x = this->Interactor->GetEventPosition()[0];
int y = this->Interactor->GetEventPosition()[1];
eatEvent =
this->Scene->ButtonReleaseEvent(vtkContextMouseEvent::MIDDLE_BUTTON, x, y);
}
if (!eatEvent)
{
this->Superclass::OnMiddleButtonUp();
}
this->EndProcessingEvent();
}
//----------------------------------------------------------------------------
void vtkContextInteractorStyle::OnRightButtonDown()
{
this->BeginProcessingEvent();
bool eatEvent = false;
if (this->Scene)
{
int x = this->Interactor->GetEventPosition()[0];
int y = this->Interactor->GetEventPosition()[1];
if (this->Interactor->GetRepeatCount())
{
eatEvent =
this->Scene->DoubleClickEvent(vtkContextMouseEvent::RIGHT_BUTTON, x, y);
}
else
{
eatEvent =
this->Scene->ButtonPressEvent(vtkContextMouseEvent::RIGHT_BUTTON, x, y);
}
}
if (!eatEvent)
{
this->Superclass::OnRightButtonDown();
}
this->EndProcessingEvent();
}
//--------------------------------------------------------------------------
void vtkContextInteractorStyle::OnRightButtonUp()
{
this->BeginProcessingEvent();
bool eatEvent = false;
if (this->Scene)
{
int x = this->Interactor->GetEventPosition()[0];
int y = this->Interactor->GetEventPosition()[1];
eatEvent =
this->Scene->ButtonReleaseEvent(vtkContextMouseEvent::RIGHT_BUTTON, x, y);
}
if (!eatEvent)
{
this->Superclass::OnRightButtonUp();
}
this->EndProcessingEvent();
}
//----------------------------------------------------------------------------
void vtkContextInteractorStyle::OnMouseWheelForward()
{
this->BeginProcessingEvent();
bool eatEvent = false;
if (this->Scene)
{
int x = this->Interactor->GetEventPosition()[0];
int y = this->Interactor->GetEventPosition()[1];
eatEvent =
this->Scene->MouseWheelEvent(+1.0 * this->MouseWheelMotionFactor, x, y);
}
if (!eatEvent)
{
this->Superclass::OnMouseWheelForward();
}
this->EndProcessingEvent();
}
//--------------------------------------------------------------------------
void vtkContextInteractorStyle::OnMouseWheelBackward()
{
this->BeginProcessingEvent();
bool eatEvent = false;
if (this->Scene)
{
int x = this->Interactor->GetEventPosition()[0];
int y = this->Interactor->GetEventPosition()[1];
eatEvent =
this->Scene->MouseWheelEvent(-1.0 * this->MouseWheelMotionFactor , x, y);
}
if (!eatEvent)
{
this->Superclass::OnMouseWheelBackward();
}
this->EndProcessingEvent();
}
//--------------------------------------------------------------------------
void vtkContextInteractorStyle::OnSelection(unsigned int rect[5])
{
this->BeginProcessingEvent();
if (this->Scene)
{
this->Scene->ProcessSelectionEvent(rect);
}
this->EndProcessingEvent();
}
/*=========================================================================
Program: Visualization Toolkit
Module: vtkContextInteractorStyle.h
Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
All rights reserved.
See Copyright.txt or http://www.kitware.com/Copyright.htm for details.
This software is distributed WITHOUT ANY WARRANTY; without even
the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
PURPOSE. See the above copyright notice for more information.
=========================================================================*/
// .NAME vtkContextInteractorStyle - An interactor for chart views
// It observes the user events (mouse events) and propagates them
// to the scene. If the scene doesn't eat the event, it is propagated
// to the interactor style superclass.
//
// .SECTION Description
#ifndef __vtkContextInteractorStyle_h
#define __vtkContextInteractorStyle_h
#include "vtkInteractorStyle.h"
class vtkContextScene;
class VTK_CHARTS_EXPORT vtkContextInteractorStyle : public vtkInteractorStyle
{
public:
static vtkContextInteractorStyle *New();
vtkTypeMacro(vtkContextInteractorStyle, vtkInteractorStyle);
void PrintSelf(ostream& os, vtkIndent indent);
// Description:
// Set the scene to forward user events to.
// Refresh the view when the scene is dirty and no event is being processed.
// The scene is observed (vtkCommand::ModifiedEvent) and a refresh on the
// view is called appropriately: scene is dirty and no event is being
// processed.
void SetScene(vtkContextScene* scene);
// Description:
// Return the observed scene.
vtkGetObjectMacro(Scene, vtkContextScene);
// Description:
// Called when the scene is modified. Refresh the scene if needed.
virtual void OnSceneModified();
// Description:
// Called when the user moves the mouse
// Default behavior forwards the event to the observed scene.
virtual void OnMouseMove();
// Description:
// Called when the user clicks the mouse left button.
// Default behavior forwards the event to the observed scene.
virtual void OnLeftButtonDown();
// Description:
// Called when the user releases the mouse left button.
// Default behavior forwards the event to the observed scene.
virtual void OnLeftButtonUp();
// Description:
// Called when the user clicks the mouse middle button.
// Default behavior forwards the event to the observed scene.
virtual void OnMiddleButtonDown();
// Description:
// Called when the user releases the mouse middle button.
// Default behavior forwards the event to the observed scene.
virtual void OnMiddleButtonUp();
// Description:
// Called when the user clicks the mouse right button.
// Default behavior forwards the event to the observed scene.
virtual void OnRightButtonDown();
// Description:
// Called when the user releases the mouse right button.
// Default behavior forwards the event to the observed scene.
virtual void OnRightButtonUp();
// Description:
// Called when the user moves the mouse wheel forward.
// Default behavior forwards the event to the observed scene.
virtual void OnMouseWheelForward();
// Description:
// Called when the user moves the mouse wheel backward.
// Default behavior forwards the event to the observed scene.
virtual void OnMouseWheelBackward();
// Description:
// Place holder for future implementation.
// Default behavior forwards the event to the observed scene.
virtual void OnSelection(unsigned int rect[5]);
protected:
vtkContextInteractorStyle();
~vtkContextInteractorStyle();
static void ProcessSceneEvents(vtkObject* object, unsigned long event,
void* clientdata, void* calldata);
// Description:
// Inform the interactor style that an event is being processed.
// That way is knows to not refresh the view (the view will eventually be
// refreshed at the end.
void BeginProcessingEvent();
// Description:
// Inform the interactor style that an event is finished to be processed.
// If no other event is being processed it check if the scene needs to be
// rendered (scene is dirty)
void EndProcessingEvent();
vtkContextScene* Scene;
vtkCallbackCommand* SceneCallbackCommand;
int ProcessingEvents;
unsigned long int LastSceneRepaintMTime;
private:
vtkContextInteractorStyle(const vtkContextInteractorStyle&); // Not implemented
void operator=(const vtkContextInteractorStyle&); // Not implemented
};
#endif
This diff is collapsed.
......@@ -31,8 +31,8 @@ class vtkAbstractContextItem;
class vtkTransform2D;
class vtkContextMouseEvent;
class vtkContextScenePrivate;
class vtkContextInteractorStyle;
class vtkInteractorStyle;
class vtkAnnotationLink;
class vtkRenderer;
......@@ -130,10 +130,6 @@ public:
vtkGetMacro(ScaleTiles, bool);
vtkBooleanMacro(ScaleTiles, bool);
// Description:
// Add the scene as an observer on the supplied interactor style.
void SetInteractorStyle(vtkInteractorStyle *interactor);
// Description:
// This should not be necessary as the context view should take care of
// rendering.
......@@ -144,6 +140,7 @@ public:
// scene. This should only be used by the vtkContextItem derived objects in
// a scene in their event handlers.
void SetDirty(bool isDirty);
bool GetDirty()const;
//BTX
// Description:
......@@ -178,11 +175,6 @@ protected:
vtkContextScene();
~vtkContextScene();
// Description:
// Protected function called after any event to check if a repaint of the
// scene is required. Called by the Command object after interaction events.
void CheckForRepaint();
// Description:
// Called to process events - figure out what child(ren) to propagate events
// to.
......@@ -191,27 +183,27 @@ protected:
// Description:
// Process a rubber band selection event.
virtual void ProcessSelectionEvent(vtkObject* caller, void* callData);
virtual bool ProcessSelectionEvent(unsigned int rect[5]);
// Description:
// Process a mouse move event.
virtual void MouseMoveEvent(int x, int y);
virtual bool MouseMoveEvent(int x, int y);
// Description:
// Process a mouse button press event.
virtual void ButtonPressEvent(int button, int x, int y);
virtual bool ButtonPressEvent(int button, int x, int y);
// Description:
// Process a mouse button release event.
virtual void ButtonReleaseEvent(int button, int x, int y);
virtual bool ButtonReleaseEvent(int button, int x, int y);
// Description:
// Process a mouse button double click event.
virtual void DoubleClickEvent(int button, int x, int y);
virtual bool DoubleClickEvent(int button, int x, int y);
// Description:
// Process a mouse wheel event where delta is the movement forward or back.
virtual void MouseWheelEvent(int delta, int x, int y);
virtual bool MouseWheelEvent(int delta, int x, int y);
// Description:
// Paint the scene in a special mode to build a cache for picking.
......@@ -244,9 +236,10 @@ protected:
// Description:
// The command object for the charts.
class Command;
friend class Command;
Command *Observer;
//class Command;
//friend class Command;
//Command *Observer;
friend class vtkContextInteractorStyle;
// Description:
// Private storage object - where we hide all of our STL objects...
......@@ -282,7 +275,7 @@ private:
void operator=(const vtkContextScene &); // Not implemented.
typedef bool (vtkAbstractContextItem::* MouseEvents)(const vtkContextMouseEvent&);
void ProcessItem(vtkAbstractContextItem* cur,
bool ProcessItem(vtkAbstractContextItem* cur,
const vtkContextMouseEvent& event,
MouseEvents eventPtr);
//ETX
......
......@@ -24,7 +24,7 @@
#include "vtkRenderWindowInteractor.h"
#include "vtkInteractorStyle.h"
#include "vtkContextActor.h"
#include "vtkInteractorStyleRubberBand2D.h"
#include "vtkContextInteractorStyle.h"
#include "vtkObjectFactory.h"
......@@ -48,9 +48,9 @@ vtkContextView::vtkContextView()
// Should not need to do this...
this->Scene->SetRenderer(this->Renderer);
vtkInteractorStyleRubberBand2D* style = vtkInteractorStyleRubberBand2D::New();
vtkContextInteractorStyle* style = vtkContextInteractorStyle::New();
style->SetScene(this->Scene);
this->GetInteractor()->SetInteractorStyle(style);
this->Scene->SetInteractorStyle(style);
style->Delete();
// Single color background by default.
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment