VTK交互系统 4 自定义交互器样式2 键盘鼠标交互器设置

784 阅读1分钟

现在我们修改一下其中一个事件函数:

`void my3DCameraStyle::OnLeftButtonDown(void)
{
        vtkInteractorStyleUser::OnLeftButtonDown();
        GetLastPos(m_OldPos[0], m_OldPos[1]);
        cout << "m_OldPos = " << m_OldPos[0] << "," << m_OldPos[1] << endl;
}`

然后编译运行。可以看到每次点击屏幕,都会在屏幕上输出点击的位置。说明我们的交互器是起作用的。

突然觉得交互器样式的系列写完了。

要不要把上面几行放在上一个博客里算了?

emmmmmmm

貌似如果再写与具体功能实现有关的函数也没有什么意义,为了凑点内容,我就先写写滚轮,再写写与键盘有关的事件吧。

`void my3DCameraStyle::OnMouseWheelBackward(void)
{
        vtkInteractorStyleUser::OnMouseWheelBackward();

};`

滚轮也是没啥可写的,每次滚动的时候都在里面做点事就好了,比如把图像放大:

`vtkSmartPointer<vtkRenderWindow>renderWindow;
int width = 512;int height = 512;
int windowWidth = 600,windowHeight = 600;`

我们把renderWindow设为全局的,然后定义两个变量,之后我们改一下函数:

`void my3DCameraStyle::OnMouseWheelForward(void)
{
        vtkInteractorStyleUser::OnMouseWheelForward();
        windowWidth += 3;
        windowHeight += 3;
        renderWindow->SetSize(windowWidth, windowHeight);

};

void my3DCameraStyle::OnMouseWheelBackward(void)
{
        vtkInteractorStyleUser::OnMouseWheelBackward();
        windowWidth -= 3;
        windowHeight -= 3;
        renderWindow->SetSize(windowWidth, windowHeight);
};`

键盘:

`/**
* OnChar is triggered when an ASCII key is pressed. Some basic key presses
* are handled here ('q' for Quit, 'p' for Pick, etc)
*/
virtual void OnChar();

// OnKeyDown is triggered by pressing any key (identical to OnKeyPress()).
// An empty implementation is provided. The behavior of this function should
// be specified in the subclass.
virtual void OnKeyDown();

// OnKeyUp is triggered by releaseing any key (identical to OnKeyRelease()).
// An empty implementation is provided. The behavior of this function should
// be specified in the subclass.
virtual void OnKeyUp();

// OnKeyPress is triggered by pressing any key (identical to OnKeyDown()).
// An empty implementation is provided. The behavior of this function should
// be specified in the subclass.
virtual void OnKeyPress();

// OnKeyRelease is triggered by pressing any key (identical to OnKeyUp()).
// An empty implementation is provided. The behavior of this function should
// be specified in the subclass.
virtual void OnKeyRelease();`

这是直接从其他的默认交互器上摘抄下来的,大家复制到类里面。然后先实现第一个函数:

`void my3DCameraStyle::OnChar()
{
        vtkRenderWindowInteractor *rwi = this->Interactor;

        switch (rwi->GetKeyCode())
        {
                case 'f':
                case 'F':
                        cout << "aaaaaaa " << endl;
        }
}`

现在按 F 或者 f 的时候就会打印 aaaaaaa

根据示意,onKeyDown 和 onKeyPress是一样的。我也不知道它干嘛要搞这玩意,估计是有的系统名字叫keyDown,有的叫keyPress,VTK为了体现兼容性和用户良好性直接创建了两种名字的同一种功能的函数(瞎搞啥)。

然后我们实现该函数:

`void my3DCameraStyle::OnKeyDown()
{
        vtkRenderWindowInteractor *rwi = this->Interactor;

        switch (rwi->GetKeyCode())
        {
        case 'f':
        case 'F':
                cout << "bbbbbbbbb " << endl;
        }
}`

现在再点击 f 就会同时打印出来两行:(有意思吗?没意思。。。。。)

bbbbbbbbb

aaaaaaa

不过这个按键可以用来检测按下的其他功能,鉴于很多默认交互器都没有实现,大概实在是个不太重要的玩意,为了知识的完整性这里也还是写上吧:

我们先改改函数:

`void my3DCameraStyle::OnKeyDown()
{
        switch (this->Interactor->GetKeyCode())
        {

        }
        cout << "sssssss " << endl;
}`

可以看到如果你按了shift等非数字键就会只输出sssssss,而不会输出aaaaaaa

现在我们改为:

`void my3DCameraStyle::OnKeyDown()
{

        switch (this->Interactor->GetKeyCode())
        {
                case 0x9:
                        cout << "sssssss " << endl;
                        break;
        }
}`

键值0x9代表的是Tab键。

现在我们运行,按下Tab,就会打印ssssss

交互器样式系列就介绍到这里,一共4篇。