osgkeyboard例子详解

本文详细介绍了osgkeyboard的例子,展示了如何创建osgText文本显示和事件处理类GUIEventHandler,用于在3D场景中响应键盘输入并更新文本。通过KeyboardModel类的keyChange方法处理按键状态,利用osg::Switch在按下和松开时切换按键的显示。此外,还实现了键盘事件的监听和处理,包括ASCII字符、回车、退格和删除键的处理。
摘要由CSDN通过智能技术生成

osgkeyboard例子中包含的内容:

(1)显示osgText.

(2)在viewer中添加事件处理:GUIEventHandler.

 

解析如下:

class KeyboardModel : public osg::Referenced
{
public:

        KeyboardModel() { createKeyboard(); }
       
        osg::Group* getScene() { return _scene.get(); }
       
        void keyChange(int key,int value);//每当用户按下或松开按钮时调用
       
protected:
       
        ~KeyboardModel() {}

        osg::Switch* addKey(osg::Vec3& pos, int key,const std::string& text,float width, float height);
        osg::Switch* addKey(int key,osg::Switch* sw);

        void createKeyboard();//构造函数中调用

        typedef std::map<int, osg::ref_ptr<osg::Switch> > KeyModelMap;
       
        osg::ref_ptr<osg::Group>    _scene;//场景节点
        KeyModelMap                 _keyModelMap;//map(字符ID,开关节点),如_keyModelMap["P"] = switch
        osg::ref_ptr<osgText::Text> _inputText;//显示的文本(会随着按键而改变)

};

void KeyboardModel::keyChange(int key,int value)
{
  
  // toggle the keys graphical representation on or off via osg::Swithc
    KeyModelMap::iterator itr = _keyModelMap.find(key);
    if (itr!=_keyModelMap.end())
    {
        itr->second->setSingleChildOn(value);
  //itr->second即找到的switch,例_keyModelMap["P"] = switch,则switch只打开一个(setSingleChildOn()),
  //当按下"P"时,由于KeyboardEventHandler::handle()中_keyboardModel->keyChange(ea.getKey(),1);表示显示
  //的索引值是1(即第2个红色的),否则如果按键松开,则显示的索引值是0(第一个白色的).
    }
   
    if (value)//只有当用户按下某个键时,value=1.
    {
        // when a key is pressed add the new data to the text field
       
        if (key>0 && key<256)
        {
            // just add ascii characters right now...
            _inputText->getText().push_back(key);//直接将输入的字符添加,会显示出来
            _inputText->update();
        }
        else if (key==osgGA::GUIEventAdapter::KEY_Return)
        {
            _inputText->getText().push_back('/n');//换行
            _inputText->update();
        }
        else if (key==osgGA::GUIEventAdapter::KEY_BackSpace || key==osgGA::GUIEventAdapter::KEY_Delete)
        {
            if (!_inputText->getText().empty())
            {
                _inputText->getText().pop_back();//删除
                _inputText->update();
            }
        }
       
    }
   
}

osg::Switch* KeyboardModel::addKey(osg::Vec3& pos, int key,const std::string& text,float width, float height)
{

    osg::Geode* geodeUp = new osg::Geode;//第1个字符(白色)
    {
        osgText::Text* textUp = new osgText::Text;
        textUp->setFont("fonts/arial.ttf");
        textUp->setColor(osg::Vec4(1.0f,1.0f,1.0f,1.0f));//(白色)
        textUp->setCharacterSize(height);
        textUp->setPosition(pos);
        textUp->setDrawMode(osgText::Text::TEXT/*|osgText::Text::BOUNDINGBOX*/);
        textUp->setAlignment(osgText::Text::LEFT_CENTER);
        textUp->setAxisAlignment(osgText::Text::XZ_PLANE);
        textUp->setText(text);
       
        geodeUp->addDrawable(textUp);
    }
   
    osg::Geode* geodeDown = new osg::Geode;//第2个字符(红色)
    {
        osgText::Text* textDown = new osgText::Text;
        textDown->setFont("fonts/arial.ttf");
        textDown->setColor(osg::Vec4(1.0f,0.0f,1.0f,1.0f));//(红色)
        textDown->setCharacterSize(height);
        textDown->setPosition(pos);
        textDown->setDrawMode(osgText::Text::TEXT/*||osgText::Text::BOUNDINGBOX*/);
        textDown->setAlignment(osgText::Text::LEFT_CENTER);
        textDown->setAxisAlignment(osgText::Text::XZ_PLANE);
        textDown->setText(text);
       
        geodeDown->addDrawable(textDown);
    }

    osg::Switch* model = new osg::Switch;//开关节点,添加2个Geode,且每次只能一个打开,即只显示一个
    model->addChild(geodeUp,true);
    model->addChild(geodeDown,false);
   
    _scene->addChil

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值