Show
Ignore:
Timestamp:
03/25/03 13:44:50 (12 years ago)
Author:
robert
Message:

Added text input field to the osgkeyboard demo.

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • OpenSceneGraph/trunk/examples/osgkeyboard/osgkeyboard.cpp

    r1748 r1750  
    2929        ~KeyboardModel() {} 
    3030 
    31         void addKey(osg::Vec3& pos, int key,const std::string& text,float width, float height); 
     31        osg::Switch* addKey(osg::Vec3& pos, int key,const std::string& text,float width, float height); 
     32        osg::Switch* addKey(int key,osg::Switch* sw); 
    3233 
    3334        void createKeyboard(); 
     
    3738        osg::ref_ptr<osg::Group>    _scene; 
    3839        KeyModelMap                 _keyModelMap; 
     40        osg::ref_ptr<osgText::Text> _inputText; 
    3941 
    4042}; 
     
    4244void KeyboardModel::keyChange(int key,int value) 
    4345{ 
    44     std::cout << std::hex << key << "\t"<< value << std::dec  << std::endl; 
    45  
     46    std::cout << "key value change, code="<<std::hex << key << "\t value="<< value << std::dec  << std::endl; 
     47 
     48    // toggle the keys graphical representation on or off via osg::Swithc 
    4649    KeyModelMap::iterator itr = _keyModelMap.find(key); 
    4750    if (itr!=_keyModelMap.end()) 
     
    4952        itr->second->setSingleChildOn(value); 
    5053    } 
     54     
     55    if (value) 
     56    { 
     57        // when a key is pressed add the new data to the text field 
     58         
     59        if (key>0 && key<256) 
     60        { 
     61            // just add ascii characters right now... 
     62            _inputText->getText().push_back(key); 
     63            _inputText->update(); 
     64        } 
     65        else if (key==osgGA::GUIEventAdapter::KEY_Return) 
     66        { 
     67            _inputText->getText().push_back('\n'); 
     68            _inputText->update(); 
     69        } 
     70        else if (key==osgGA::GUIEventAdapter::KEY_BackSpace || key==osgGA::GUIEventAdapter::KEY_Delete)  
     71        { 
     72            if (!_inputText->getText().empty()) 
     73            { 
     74                _inputText->getText().pop_back(); 
     75                _inputText->update(); 
     76            } 
     77        } 
     78         
     79    } 
     80     
    5181} 
    5282 
    53 void KeyboardModel::addKey(osg::Vec3& pos, int key,const std::string& text,float width, float height) 
     83osg::Switch* KeyboardModel::addKey(osg::Vec3& pos, int key,const std::string& text,float width, float height) 
    5484{ 
    5585 
     
    94124    pos.x() += width; 
    95125     
     126    return model; 
     127     
    96128} 
    97129 
     130osg::Switch* KeyboardModel::addKey(int key,osg::Switch* sw) 
     131{ 
     132    _keyModelMap[key] = sw; 
     133    return sw; 
     134} 
     135 
    98136void KeyboardModel::createKeyboard() 
    99137{ 
    100138    _scene = new osg::Group; 
    101139     
    102     osg::Vec3 pos(0.0f,0.0f,0.0f); 
     140    osg::Vec3 origin(0.0f,0.0f,0.0f); 
     141    osg::Vec3 pos=origin; 
    103142     
    104143    addKey(pos,osgGA::GUIEventAdapter::KEY_Control_L,"Ctrl",2.0f,0.5f); 
     
    118157    addKey(pos,osgGA::GUIEventAdapter::KEY_Shift_L,"Shift",2.0f,0.5f); 
    119158    addKey(pos,'\\',"\\",1.0f,1.0f); 
    120     addKey(pos,'z',"Z",1.0f,1.0f); 
    121     addKey(pos,'x',"X",1.0f,1.0f); 
    122     addKey(pos,'c',"C",1.0f,1.0f); 
    123     addKey(pos,'v',"V",1.0f,1.0f); 
    124     addKey(pos,'b',"B",1.0f,1.0f); 
    125     addKey(pos,'n',"N",1.0f,1.0f); 
    126     addKey(pos,'m',"M",1.0f,1.0f); 
    127     addKey(pos,',',",",1.0f,1.0f); 
    128     addKey(pos,'.',".",1.0f,1.0f); 
    129     addKey(pos,'/',"/",1.0f,1.0f); 
     159    addKey('Z',addKey(pos,'z',"Z",1.0f,1.0f)); 
     160    addKey('X',addKey(pos,'x',"X",1.0f,1.0f)); 
     161    addKey('C',addKey(pos,'c',"C",1.0f,1.0f)); 
     162    addKey('V',addKey(pos,'v',"V",1.0f,1.0f)); 
     163    addKey('B',addKey(pos,'b',"B",1.0f,1.0f)); 
     164    addKey('N',addKey(pos,'n',"N",1.0f,1.0f)); 
     165    addKey('M',addKey(pos,'m',"M",1.0f,1.0f)); 
     166    addKey('<',addKey(pos,',',",",1.0f,1.0f)); 
     167    addKey('>',addKey(pos,'.',".",1.0f,1.0f)); 
     168    addKey('?',addKey(pos,'/',"/",1.0f,1.0f)); 
    130169    addKey(pos,osgGA::GUIEventAdapter::KEY_Shift_R,"Shift",2.0f,0.5f); 
    131170 
     
    134173     
    135174    addKey(pos,osgGA::GUIEventAdapter::KEY_Caps_Lock,"Caps",2.0f,0.5f); 
    136     addKey(pos,'a',"A",1.0f,1.0f); 
    137     addKey(pos,'s',"S",1.0f,1.0f); 
    138     addKey(pos,'d',"D",1.0f,1.0f); 
    139     addKey(pos,'f',"F",1.0f,1.0f); 
    140     addKey(pos,'g',"G",1.0f,1.0f); 
    141     addKey(pos,'h',"H",1.0f,1.0f); 
    142     addKey(pos,'j',"J",1.0f,1.0f); 
    143     addKey(pos,'k',"K",1.0f,1.0f); 
    144     addKey(pos,'l',"L",1.0f,1.0f); 
    145     addKey(pos,';',";",1.0f,1.0f); 
    146     addKey(pos,'\'',"'",1.0f,1.0f); 
    147     addKey(pos,'#',"#",1.0f,1.0f); 
     175    addKey('A',addKey(pos,'a',"A",1.0f,1.0f)); 
     176    addKey('S',addKey(pos,'s',"S",1.0f,1.0f)); 
     177    addKey('D',addKey(pos,'d',"D",1.0f,1.0f)); 
     178    addKey('F',addKey(pos,'f',"F",1.0f,1.0f)); 
     179    addKey('G',addKey(pos,'g',"G",1.0f,1.0f)); 
     180    addKey('H',addKey(pos,'h',"H",1.0f,1.0f)); 
     181    addKey('J',addKey(pos,'j',"J",1.0f,1.0f)); 
     182    addKey('K',addKey(pos,'k',"K",1.0f,1.0f)); 
     183    addKey('L',addKey(pos,'l',"L",1.0f,1.0f)); 
     184    addKey(':',addKey(pos,';',";",1.0f,1.0f)); 
     185    addKey('@',addKey(pos,'\'',"'",1.0f,1.0f)); 
     186    addKey('~',addKey(pos,'#',"#",1.0f,1.0f)); 
    148187    addKey(pos,osgGA::GUIEventAdapter::KEY_Return,"Return",4.0f,0.5f); 
    149188 
     
    152191     
    153192    addKey(pos,osgGA::GUIEventAdapter::KEY_Tab,"Tab",2.0f,0.5f); 
    154     addKey(pos,'q',"Q",1.0f,1.0f); 
    155     addKey(pos,'w',"W",1.0f,1.0f); 
    156     addKey(pos,'e',"E",1.0f,1.0f); 
    157     addKey(pos,'r',"R",1.0f,1.0f); 
    158     addKey(pos,'t',"T",1.0f,1.0f); 
    159     addKey(pos,'y',"Y",1.0f,1.0f); 
    160     addKey(pos,'u',"U",1.0f,1.0f); 
    161     addKey(pos,'i',"I",1.0f,1.0f); 
    162     addKey(pos,'o',"O",1.0f,1.0f); 
    163     addKey(pos,'p',"P",1.0f,1.0f); 
    164     addKey(pos,'[',"[",1.0f,1.0f); 
    165     addKey(pos,']',"]",1.0f,1.0f); 
     193    addKey('Q',addKey(pos,'q',"Q",1.0f,1.0f)); 
     194    addKey('W',addKey(pos,'w',"W",1.0f,1.0f)); 
     195    addKey('E',addKey(pos,'e',"E",1.0f,1.0f)); 
     196    addKey('R',addKey(pos,'r',"R",1.0f,1.0f)); 
     197    addKey('T',addKey(pos,'t',"T",1.0f,1.0f)); 
     198    addKey('Y',addKey(pos,'y',"Y",1.0f,1.0f)); 
     199    addKey('U',addKey(pos,'u',"U",1.0f,1.0f)); 
     200    addKey('I',addKey(pos,'i',"I",1.0f,1.0f)); 
     201    addKey('O',addKey(pos,'o',"O",1.0f,1.0f)); 
     202    addKey('P',addKey(pos,'p',"P",1.0f,1.0f)); 
     203    addKey('{',addKey(pos,'[',"[",1.0f,1.0f)); 
     204    addKey('}',addKey(pos,']',"]",1.0f,1.0f)); 
    166205 
    167206    pos.x() = 0.0f; 
     
    271310    addKey(pos,osgGA::GUIEventAdapter::KEY_KP_Multiply,"*",1.0f,1.0f); 
    272311    addKey(pos,osgGA::GUIEventAdapter::KEY_KP_Subtract,"-",1.0f,1.0f); 
     312     
     313    float totalWidth = pos.x()-origin.x(); 
     314    pos = origin; 
     315    pos.z() += -1.5f; 
     316 
     317    osg::Geode* geodeInput = new osg::Geode; 
     318    { 
     319        _inputText = new osgText::Text; 
     320        _inputText->setFont("fonts/arial.ttf"); 
     321        _inputText->setColor(osg::Vec4(1.0f,1.0f,0.0f,1.0f)); 
     322        _inputText->setCharacterSize(1.0f); 
     323        _inputText->setMaximumWidth(totalWidth); 
     324        _inputText->setPosition(pos); 
     325        _inputText->setDrawMode(osgText::Text::TEXT/*||osgText::Text::BOUNDINGBOX*/); 
     326        _inputText->setAlignment(osgText::Text::BASE_LINE); 
     327        _inputText->setAxisAlignment(osgText::Text::XZ_PLANE); 
     328        _inputText->setText("Press some keys..."); 
     329         
     330        geodeInput->addDrawable(_inputText.get()); 
     331         
     332        _scene->addChild(geodeInput); 
     333    } 
    273334 
    274335}