activate grid button and widget size

This commit is contained in:
AndreaRigoni
2026-03-17 22:56:56 +00:00
parent 553bb7fd61
commit 92a06f6274
7 changed files with 210 additions and 2 deletions

View File

@@ -38,6 +38,13 @@
#include <vtkObjectFactory.h>
#include <vtkTextProperty.h>
#include <vtkRenderWindow.h>
#include <vtkButtonWidget.h>
#include <vtkTexturedButtonRepresentation2D.h>
#include <vtkImageData.h>
#include <vtkImageCanvasSource2D.h>
#include <vtkCallbackCommand.h>
#include <vtkCoordinate.h>
#include <vtkRenderer.h>
#include "uLibVtkViewer.h"
@@ -82,6 +89,9 @@ void Viewer::InstallPipe() {
// Common setup
Viewport::SetupPipeline(renderWindowInteractor);
// Setup native grid button
SetupGridButton();
// BUT we want to override the style with our custom NoSpin version
vtkSmartPointer<vtkInteractorStyleNoSpin> style =
@@ -114,6 +124,89 @@ Viewer::MakeCameraOrientationWidget(vtkRenderWindowInteractor *interactor,
return widget;
}
void Viewer::SetupGridButton() {
if (!m_RenderWindow || !m_RenderWindow->GetInteractor()) return;
// Create procedural textures for the button using canvas
vtkNew<vtkImageCanvasSource2D> canvas;
canvas->SetScalarTypeToUnsignedChar();
canvas->SetNumberOfScalarComponents(4);
canvas->SetExtent(0, 63, 0, 63, 0, 0);
// State 0: OFF (Gray circle, transparent background)
canvas->SetDrawColor(0, 0, 0, 0);
canvas->FillBox(0, 63, 0, 63);
canvas->SetDrawColor(120, 120, 120, 255);
canvas->DrawCircle(32, 32, 25);
canvas->Update();
vtkNew<vtkImageData> imgOff;
imgOff->DeepCopy(canvas->GetOutput());
// State 1: ON (White circle, transparent background)
canvas->SetDrawColor(0, 0, 0, 0);
canvas->FillBox(0, 63, 0, 63);
canvas->SetDrawColor(255, 255, 255, 255);
canvas->DrawCircle(32, 32, 25);
canvas->Update();
vtkNew<vtkImageData> imgOn;
imgOn->DeepCopy(canvas->GetOutput());
vtkNew<vtkTexturedButtonRepresentation2D> rep;
rep->SetNumberOfStates(2);
rep->SetButtonTexture(0, imgOff);
rep->SetButtonTexture(1, imgOn);
m_GridButton = vtkSmartPointer<vtkButtonWidget>::New();
m_GridButton->SetInteractor(m_RenderWindow->GetInteractor());
m_GridButton->SetRepresentation(rep);
// Position it initially
UpdateGridButtonPosition();
// Callback for resize (ModifiedEvent on RenderWindow)
vtkNew<vtkCallbackCommand> resizeCallback;
resizeCallback->SetClientData(this);
resizeCallback->SetCallback([](vtkObject*, unsigned long, void* clientdata, void*){
auto* v = static_cast<Viewer*>(clientdata);
v->UpdateGridButtonPosition();
});
m_RenderWindow->AddObserver(vtkCommand::ModifiedEvent, resizeCallback);
// Callback for state change
vtkNew<vtkCallbackCommand> stateCallback;
stateCallback->SetClientData(this);
stateCallback->SetCallback([](vtkObject* caller, unsigned long, void* clientdata, void*){
auto* btn = vtkButtonWidget::SafeDownCast(caller);
auto* v = static_cast<Viewer*>(clientdata);
auto* r = vtkTexturedButtonRepresentation2D::SafeDownCast(btn->GetRepresentation());
v->SetGridVisible(r->GetState() == 1);
});
m_GridButton->AddObserver(vtkCommand::StateChangedEvent, stateCallback);
m_GridButton->On();
// Set initial state
rep->SetState(GetGridVisible() ? 1 : 0);
}
void Viewer::UpdateGridButtonPosition() {
if (!m_GridButton || !m_RenderWindow) return;
auto* rep = vtkTexturedButtonRepresentation2D::SafeDownCast(m_GridButton->GetRepresentation());
if (!rep) return;
int *sz = m_RenderWindow->GetSize();
if (sz[0] == 0 || sz[1] == 0) return; // Window not yet sized or hidden
int margin_rigth = 23;
int margin_top = 170;
int btnSz = 100; // Button size in display coordinates
double bds[6] = { (double)sz[0] - btnSz - margin_rigth, (double)sz[0] - margin_rigth,
(double)sz[1] - margin_top - btnSz/2.0, (double)sz[1] - margin_top + btnSz/2.0, 0, 0 };
rep->PlaceWidget(bds);
}
void Viewer::Start() { m_RenderWindow->GetInteractor()->Start(); }
vtkRenderWindow *Viewer::GetRenderWindow() { return m_RenderWindow; }