| 1 | |
|---|
| 2 | |
|---|
| 3 | #include <osg/io_utils> |
|---|
| 4 | #include <osgWidget/WindowManager> |
|---|
| 5 | #include <osgWidget/Input> |
|---|
| 6 | |
|---|
| 7 | namespace osgWidget { |
|---|
| 8 | |
|---|
| 9 | Input::Input(const std::string& name, const std::string& label, unsigned int size): |
|---|
| 10 | Label (name, label), |
|---|
| 11 | _xoff (0.0f), |
|---|
| 12 | _yoff (0.0f), |
|---|
| 13 | _index (0), |
|---|
| 14 | _size (0), |
|---|
| 15 | _cursorIndex (0), |
|---|
| 16 | _maxSize (size), |
|---|
| 17 | _cursor (new Widget("cursor")) { |
|---|
| 18 | _text->setAlignment(osgText::Text::LEFT_BOTTOM_BASE_LINE); |
|---|
| 19 | _text->setKerningType(osgText::KERNING_NONE); |
|---|
| 20 | |
|---|
| 21 | |
|---|
| 22 | _cursor->setCanClone(false); |
|---|
| 23 | _cursor->setDataVariance(osg::Object::DYNAMIC); |
|---|
| 24 | _cursor->setColor(0.0f, 0.0f, 0.0f, 1.0f); |
|---|
| 25 | |
|---|
| 26 | setEventMask( |
|---|
| 27 | |
|---|
| 28 | EVENT_MASK_FOCUS | |
|---|
| 29 | |
|---|
| 30 | EVENT_MASK_KEY | |
|---|
| 31 | |
|---|
| 32 | EVENT_MOUSE_PUSH |
|---|
| 33 | ); |
|---|
| 34 | |
|---|
| 35 | _offsets.resize(size, 0.0f); |
|---|
| 36 | |
|---|
| 37 | _text->getText().resize(size, ' '); |
|---|
| 38 | _text->update(); |
|---|
| 39 | } |
|---|
| 40 | |
|---|
| 41 | void Input::_calculateSize(const XYCoord& size) { |
|---|
| 42 | |
|---|
| 43 | |
|---|
| 44 | |
|---|
| 45 | |
|---|
| 46 | #if 0 |
|---|
| 47 | point_type width = size.x() + _cursor->getWidth(); |
|---|
| 48 | point_type height = _cursor->getHeight(); |
|---|
| 49 | |
|---|
| 50 | if(width > getWidth()) setWidth(osg::round(width)); |
|---|
| 51 | |
|---|
| 52 | if(height > getHeight()) setHeight(osg::round(height)); |
|---|
| 53 | #endif |
|---|
| 54 | } |
|---|
| 55 | |
|---|
| 56 | void Input::_calculateCursorOffsets() { |
|---|
| 57 | |
|---|
| 58 | const osgText::Text::TextureGlyphQuadMap& tgqm = _text->getTextureGlyphQuadMap(); |
|---|
| 59 | |
|---|
| 60 | const osgText::Text::TextureGlyphQuadMap::const_iterator tgqmi = tgqm.begin(); |
|---|
| 61 | |
|---|
| 62 | const osgText::Text::GlyphQuads& gq = tgqmi->second; |
|---|
| 63 | |
|---|
| 64 | osg::Vec3 pos = _text->getPosition(); |
|---|
| 65 | |
|---|
| 66 | for(unsigned int i = 0; i < _maxSize; i++) { |
|---|
| 67 | osg::Vec3 ul = gq.getTransformedCoords(0)[0 + (i * 4)]; |
|---|
| 68 | osg::Vec3 ll = gq.getTransformedCoords(0)[1 + (i * 4)]; |
|---|
| 69 | osg::Vec3 lr = gq.getTransformedCoords(0)[2 + (i * 4)]; |
|---|
| 70 | osg::Vec3 ur = gq.getTransformedCoords(0)[3 + (i * 4)]; |
|---|
| 71 | |
|---|
| 72 | _offsets[i] = lr.x() - pos.x(); |
|---|
| 73 | |
|---|
| 74 | |
|---|
| 75 | } |
|---|
| 76 | } |
|---|
| 77 | |
|---|
| 78 | bool Input::focus(WindowManager*) { |
|---|
| 79 | _cursor->setColor(0.0f, 0.0f, 0.0f, 1.0f); |
|---|
| 80 | |
|---|
| 81 | return true; |
|---|
| 82 | } |
|---|
| 83 | |
|---|
| 84 | bool Input::unfocus(WindowManager*) { |
|---|
| 85 | _cursor->setColor(0.0f, 0.0f, 0.0f, 0.0f); |
|---|
| 86 | |
|---|
| 87 | return true; |
|---|
| 88 | } |
|---|
| 89 | |
|---|
| 90 | void Input::parented(Window* parent) { |
|---|
| 91 | Label::parented(parent); |
|---|
| 92 | |
|---|
| 93 | _cursor->setSize(2.0f, _text->getCharacterHeight()); |
|---|
| 94 | |
|---|
| 95 | if(_cursorIndex) parent->getGeode()->setDrawable(_cursorIndex, _cursor.get()); |
|---|
| 96 | |
|---|
| 97 | else _cursorIndex = parent->addDrawableAndGetIndex(_cursor.get()); |
|---|
| 98 | } |
|---|
| 99 | |
|---|
| 100 | void Input::positioned() { |
|---|
| 101 | point_type ln = static_cast<point_type>(_text->getLineCount()); |
|---|
| 102 | |
|---|
| 103 | ln = ln == 0.0f ? 1.0f : ln; |
|---|
| 104 | |
|---|
| 105 | |
|---|
| 106 | |
|---|
| 107 | point_type x = getX() + _xoff; |
|---|
| 108 | point_type y = getY() + _yoff; |
|---|
| 109 | |
|---|
| 110 | |
|---|
| 111 | |
|---|
| 112 | _text->setPosition(osg::Vec3(x, y, _calculateZ(LAYER_MIDDLE))); |
|---|
| 113 | |
|---|
| 114 | point_type xoffset = _index > 0 ? _offsets[_index - 1] : 0.0f; |
|---|
| 115 | |
|---|
| 116 | _cursor->setOrigin(x + xoffset + 1.0f, y + 1.0f); |
|---|
| 117 | _cursor->setZ(_calculateZ(LAYER_MIDDLE)); |
|---|
| 118 | } |
|---|
| 119 | |
|---|
| 120 | bool Input::keyUp(int key, int mask, WindowManager*) { |
|---|
| 121 | return false; |
|---|
| 122 | } |
|---|
| 123 | |
|---|
| 124 | bool Input::keyDown(int key, int mask, WindowManager*) { |
|---|
| 125 | osgText::String& s = _text->getText(); |
|---|
| 126 | |
|---|
| 127 | if(key == osgGA::GUIEventAdapter::KEY_BackSpace) { |
|---|
| 128 | if(_index >= 1) { |
|---|
| 129 | |
|---|
| 130 | |
|---|
| 131 | s[_index - 1] = ' '; |
|---|
| 132 | |
|---|
| 133 | _text->update(); |
|---|
| 134 | |
|---|
| 135 | _calculateCursorOffsets(); |
|---|
| 136 | |
|---|
| 137 | _index--; |
|---|
| 138 | } |
|---|
| 139 | } |
|---|
| 140 | |
|---|
| 141 | else { |
|---|
| 142 | if(key > 255 || _index >= _maxSize) return false; |
|---|
| 143 | |
|---|
| 144 | |
|---|
| 145 | |
|---|
| 146 | |
|---|
| 147 | s[_index] = key; |
|---|
| 148 | |
|---|
| 149 | _text->update(); |
|---|
| 150 | |
|---|
| 151 | _calculateCursorOffsets(); |
|---|
| 152 | |
|---|
| 153 | _index++; |
|---|
| 154 | } |
|---|
| 155 | |
|---|
| 156 | |
|---|
| 157 | |
|---|
| 158 | _calculateSize(getTextSize()); |
|---|
| 159 | |
|---|
| 160 | getParent()->resize(); |
|---|
| 161 | |
|---|
| 162 | return false; |
|---|
| 163 | } |
|---|
| 164 | |
|---|
| 165 | void Input::setCursor(Widget*) { |
|---|
| 166 | } |
|---|
| 167 | |
|---|
| 168 | unsigned int Input::calculateBestYOffset(const std::string& s) { |
|---|
| 169 | const osgText::FontResolution fr(_text->getCharacterHeight(), _text->getCharacterHeight()); |
|---|
| 170 | |
|---|
| 171 | osgText::String utf(s); |
|---|
| 172 | |
|---|
| 173 | unsigned int descent = 0; |
|---|
| 174 | |
|---|
| 175 | for(osgText::String::iterator i = utf.begin(); i != utf.end(); i++) { |
|---|
| 176 | osgText::Font* font = const_cast<osgText::Font*>(_text->getFont()); |
|---|
| 177 | osgText::Font::Glyph* glyph = font->getGlyph(fr, *i); |
|---|
| 178 | unsigned int d = abs(glyph->getHorizontalBearing().y()); |
|---|
| 179 | |
|---|
| 180 | if(d > descent) descent = d; |
|---|
| 181 | } |
|---|
| 182 | |
|---|
| 183 | return descent; |
|---|
| 184 | } |
|---|
| 185 | |
|---|
| 186 | } |
|---|