本文主要是介绍为Vega Prime 2.0窗口添加鼠标函数,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
//自定义一个类
#include <vpInputMouse.h>
class WxpMouseInputObserver: public vpInputSourceBoolean::Subscriber,
public vpInputSourceFloat::Subscriber,
public vpInputSourceInteger::Subscriber,
public vsChannel::Subscriber
{
public:
WxpMouseInputObserver::WxpMouseInputObserver(void)
{
ValX=0;
ValY=0;
LeftButtonDown=false;
RightButtonDown=false;
}
WxpMouseInputObserver::~WxpMouseInputObserver(void)
{
}
private:
int ValX; // X坐标
int ValY; // Y坐标
bool LeftButtonDown; // 左键按下
bool RightButtonDown; // 右键按下
void notify(vsChannel::Event event, const vsChannel *channel, vrDrawContext *context)
{}
virtual void notify(vsChannel::Event, const vsChannel *,vsTraversalCull *)
{
}
/**
* notify method to catch boolean source subscriber events
*/
void notify(vpInputSourceBoolean::Event event, vpInputSourceBoolean *source)
{
//PublicMember::TPS_pObject_observer->setTranslateZ(2,true);
CString name=source->getName();
int val=source->getValue();
if(name=="vpInputMouse::SOURCE_BOOLEAN_BUTTON_LEFT")
{
if(val==1)
LeftButtonDown=true;
if(val==0)
LeftButtonDown=false;
}
//
if(name=="vpInputMouse::SOURCE_BOOLEAN_BUTTON_RIGHT")
{
if(val==1)
RightButtonDown=true;
if(val==0)
RightButtonDown=false;
}
}
/**
* notify method to catch float source subscriber events
*/
void notify(vpInputSourceFloat::Event, vpInputSourceFloat *source)
{
}
/**
* notify method to catch integer source subscriber events
*/
void notify(vpInputSourceInteger::Event, vpInputSourceInteger *source)
{
char tt[200];
sprintf(tt,"name:%s \nvalue:%f\n", source->getName(), source->getValue());
//AfxMessageBox(tt);
CString name=source->getName();
int val=source->getValue();
if(val>1740)
return ;
//左键控制 y z面的位置
if ( (name=="vpInputMouse::SOURCE_INTEGER_POSITION_X") && LeftButtonDown )
{
if(val>ValX)
{
PublicMember::TPS_pObject_observer->setTranslateX(-1,true);
}
if(val<ValX)
{
PublicMember::TPS_pObject_observer->setTranslateX(1,true);
}
ValX=val;
}
if ( (name=="vpInputMouse::SOURCE_INTEGER_POSITION_Y") && LeftButtonDown )
{
if(val>ValY)
{
PublicMember::TPS_pObject_observer->setTranslateZ(-1,true);
}
if(val<ValY)
{
PublicMember::TPS_pObject_observer->setTranslateZ(1,true);
}
ValY=val;
}
//右键控制H P 方向的转动
if ( (name=="vpInputMouse::SOURCE_INTEGER_POSITION_X") && RightButtonDown )
{
if(val>ValX)
{
PublicMember::TPS_pObject_observer->setRotateR(-0.20,true);
}
if(val<ValX)
{
PublicMember::TPS_pObject_observer->setRotateR(0.20,true);
}
ValX=val;
}
if ( (name=="vpInputMouse::SOURCE_INTEGER_POSITION_Y") && RightButtonDown )
{
if(val>ValY)
{
PublicMember::TPS_pObject_observer->setRotateP(-0.20,true);
}
if(val<ValY)
{
PublicMember::TPS_pObject_observer->setRotateP(0.20,true);
}
ValY=val;
}
//中键滑动控制远近
if (name=="vpInputMouse::SOURCE_INTEGER_WHEEL_DIRECTION")
if(val==1)
{
PublicMember::TPS_pObject_observer->setTranslateY(-10,true);
}
if(val==-1)
{
PublicMember::TPS_pObject_observer->setTranslateY(10,true);
}
}
//end of class
};
//配置鼠标
//设置窗体
vpWindow * vpWin= * vpWindow::begin();
vpWin->setParent(TPS_RunningWindow);
vpWin->setBorderEnable(false);
vpWin->setFullScreenEnable(true);
//设置键盘
vpWin->setInputEnable(true);
vpWin->setKeyboardFunc((vrWindow::KeyboardFunc)TPS_KeyboardMotor,NULL);
//设置鼠标函数
WxpMouseInputObserver * wang=new WxpMouseInputObserver();
vpChannel *chan = *vpChannel::begin();
chan->addSubscriber(vsChannel::EVENT_POST_DRAW,wang);
// grab our mouse input from the first window
vpWindow* window = vpWindow::empty() ? NULL : *vpWindow::begin();
assert(window);
vpChannel* channel = vpChannel::empty() ? NULL : *vpChannel::begin();
assert(channel);
// create a mouse
vpInputMouse *m_mouse;
m_mouse = new vpInputMouse();
m_mouse->setWindow(window);
m_mouse->setChannel(channel);
m_mouse->ref();
// add subscribers to the input sources so we know when a value
// has changed
vpInputMouse::const_iterator_source_boolean bit;
for (bit=m_mouse->begin_source_boolean();
bit!=m_mouse->end_source_boolean();++bit) {
(*bit)->addSubscriber(
vpInputSourceBoolean::EVENT_VALUE_CHANGED, wang);
}
vpInputMouse::const_iterator_source_float fit;
for (fit=m_mouse->begin_source_float();
fit!=m_mouse->end_source_float();++fit) {
(*fit)->addSubscriber(
vpInputSourceFloat::EVENT_VALUE_CHANGED, wang);
}
vpInputMouse::const_iterator_source_integer iit;
for (iit=m_mouse->begin_source_integer();
iit!=m_mouse->end_source_integer();++iit) {
(*iit)->addSubscriber(
vpInputSourceInteger::EVENT_VALUE_CHANGED, wang);
}
这篇关于为Vega Prime 2.0窗口添加鼠标函数的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!