有时我们需要在 VTK 窗口中增加 按钮,右上角;
实现,通过回调函数,vtkButtonCallback 获取点击:
#include <vtkVersion.h>
#include <vtkSmartPointer.h>
#include <vtkPolyDataMapper.h>
#include <vtkActor.h>
#include <vtkRenderWindow.h>
#include <vtkRenderer.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkPolyData.h>
#include <vtkImageData.h>
#include <vtkCoordinate.h>
#include <vtkSphereSource.h>
#include <vtkButtonWidget.h>
#include <vtkTexturedButtonRepresentation2D.h>
#include <QDebug>
#include <vtkPNGReader.h>
#include <vtkProp3DButtonRepresentation.h>
#include <vtkProperty.h>
#include <vtkCommand.h>
class vtkButtonCallback : public vtkCommand
{
public:
static vtkButtonCallback *New()
{
return new vtkButtonCallback;
}
virtual void Execute(vtkObject *caller, unsigned long, void*)
{
vtkButtonWidget *buttonWidget =
reinterpret_cast<vtkButtonWidget*>(caller);
vtkProp3DButtonRepresentation *rep =
reinterpret_cast<vtkProp3DButtonRepresentation *>
(buttonWidget->GetRepresentation());
int state = rep->GetState();
// this->Actor->GetProperty()->SetColor(reinterpret_cast<vtkActor *>(rep->GetButtonProp(state))->GetProperty()->GetColor());
qDebug() << "State: " << state;
}
vtkButtonCallback() :Actor(0) {}
vtkActor *Actor;
};
int main(int, char *[])
{
// Create two images for texture
vtkSmartPointer<vtkImageData> image1 =
vtkSmartPointer<vtkImageData>::New();
vtkSmartPointer<vtkImageData> image2 =
vtkSmartPointer<vtkImageData>::New();
unsigned char banana[3] = { 227, 207, 87 };
unsigned char tomato[3] = { 255, 99, 71 };
vtkSmartPointer<vtkPNGReader> pngReader1 = vtkSmartPointer<vtkPNGReader>::New();
pngReader1->SetFileName("icon-maximize-pressed.png");
pngReader1->Update();
vtkSmartPointer<vtkPNGReader> pngReader2 = vtkSmartPointer<vtkPNGReader>::New();
pngReader2->SetFileName("icon-restore-hover.png");
pngReader2->Update();
image1 = pngReader1->GetOutput();
image2 = pngReader2->GetOutput();
// CreateImage(image1, banana, tomato);
// CreateImage(image2, tomato, banana);
// Create some geometry
vtkSmartPointer<vtkSphereSource> sphereSource =
vtkSmartPointer<vtkSphereSource>::New();
sphereSource->Update();
vtkSmartPointer<vtkPolyDataMapper> mapper =
vtkSmartPointer<vtkPolyDataMapper>::New();
mapper->SetInputConnection(sphereSource->GetOutputPort());
vtkSmartPointer<vtkActor> actor =
vtkSmartPointer<vtkActor>::New();
actor->SetMapper(mapper);
// A renderer and render window
vtkSmartPointer<vtkRenderer> renderer =
vtkSmartPointer<vtkRenderer>::New();
vtkSmartPointer<vtkRenderWindow> renderWindow =
vtkSmartPointer<vtkRenderWindow>::New();
renderWindow->AddRenderer(renderer);
// An interactor
vtkSmartPointer<vtkRenderWindowInteractor> renderWindowInteractor =
vtkSmartPointer<vtkRenderWindowInteractor>::New();
renderWindowInteractor->SetRenderWindow(renderWindow);
// Create the widget and its representation
vtkSmartPointer<vtkTexturedButtonRepresentation2D> buttonRepresentation =
vtkSmartPointer<vtkTexturedButtonRepresentation2D>::New();
buttonRepresentation->SetNumberOfStates(2);
buttonRepresentation->SetButtonTexture(0, image1);
buttonRepresentation->SetButtonTexture(1, image2);
vtkSmartPointer<vtkButtonCallback> callback =
vtkSmartPointer<vtkButtonCallback>::New();
callback->Actor = actor;
vtkButtonWidget *buttonWidget = vtkButtonWidget::New();
buttonWidget->SetInteractor(renderWindowInteractor);
buttonWidget->SetRepresentation(buttonRepresentation);
buttonWidget->AddObserver(vtkCommand::StateChangedEvent, callback);
buttonWidget->SetEnabled(1);
// Add the actors to the scene
renderer->AddActor(actor);
renderer->SetBackground(.1, .2, .5);
renderWindow->SetSize(640, 480);
renderWindow->Render();
// Place the widget. Must be done after a render so that the
// viewport is defined.
// Here the widget placement is in normalized display coordinates
vtkSmartPointer<vtkCoordinate> upperRight =
vtkSmartPointer<vtkCoordinate>::New();
upperRight->SetCoordinateSystemToNormalizedDisplay();
upperRight->SetValue(1.0, 1.0);
double bds[6];
double sz = 135;
bds[0] = upperRight->GetComputedDisplayValue(renderer)[0];
bds[1] = bds[0] + sz / 2;
bds[2] = upperRight->GetComputedDisplayValue(renderer)[1];
bds[3] = bds[2] + sz / 2;
bds[4] = bds[5] = 0.0;
// Scale to 1, default is .5
// buttonRepresentation->SetPlaceFactor(1);
buttonRepresentation->PlaceWidget(bds);
buttonWidget->On();
// Begin mouse interaction
renderWindowInteractor->Start();
return EXIT_SUCCESS;
}