root/OpenSceneGraph/trunk/src/osgWidget/Input.cpp @ 9450

Revision 9450, 4.9 kB (checked in by robert, 6 years ago)

From Sergey Leontyev, "Some minor(but important) bug fixes for osgWidget:


1. In WindowManager?.cpp childRemoved method the second parameter "end" is misleading and

should be named "numChildren" instead. It represents how many children to remove. As a result, the method was implemented incorrectly and failed to remove UI objects. So I fixed it.


replaced this:


void WindowManager::childRemoved(unsigned int start, unsigned int end) {

while(start < end) {

Window* window = getByIndex(start);

if(!window) continue;

if(_remove(window)) {

window->_index = -1;

window->unmanaged(this);

}

start++;

}

}


with this:

void WindowManager::childRemoved(unsigned int start, unsigned int numChildren) {

for (unsigned int i = start; i < start+numChildren; i++)
{

Window* window = getByIndex(i);

if(!window) continue;

if(_remove(window)) {

window->_index = -1;

window->unmanaged(this);

}

}

}


2. in Input.cpp


The cursor in Input did not get positioned correctly, probably as a left over from the TOP LEFT origin system which is now BOTTOM LEFT.

in method positioned()

replaced this:

_cursor->setOrigin(x + xoffset + 1.0f, y - _cursor->getHeight() + 1.0f);


with this:

_cursor->setOrigin(x + xoffset + 1.0f, y + 1.0f);


Now it is placed correctly.

"

Line 
1// -*-c++-*- osgWidget - Code by: Jeremy Moles (cubicool) 2007-2008
2
3#include <osg/io_utils>
4#include <osgWidget/WindowManager>
5#include <osgWidget/Input>
6
7namespace osgWidget {
8
9Input::Input(const std::string& name, const std::string& label, unsigned int size):
10Label        (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    // Make the cursor un-copyable.
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        // For showing/hiding the "cursor."
28        EVENT_MASK_FOCUS |
29        // For keypresses, obviously.
30        EVENT_MASK_KEY |
31        // For "click" focusing.
32        EVENT_MOUSE_PUSH
33    );
34
35    _offsets.resize(size, 0.0f);
36
37    _text->getText().resize(size, ' ');
38    _text->update();
39}
40
41void Input::_calculateSize(const XYCoord& size) {
42    // An Input cannot currently set it's own size RELIABLY until the osgText implementation
43    // is dratiscally improved. I'm getting wildly crazy results. :(
44    // point_type height = size.y() > _cursor->getHeight() ? size.y() : _cursor->getHeight();
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
56void Input::_calculateCursorOffsets() {
57    // Determine the "offset"
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        // warn() << "vb: " << gq.getGlyphs()[i]->getHorizontalBearing() << std::endl;
75    }
76}
77
78bool Input::focus(WindowManager*) {
79    _cursor->setColor(0.0f, 0.0f, 0.0f, 1.0f);
80
81    return true;
82}
83
84bool Input::unfocus(WindowManager*) {
85    _cursor->setColor(0.0f, 0.0f, 0.0f, 0.0f);
86
87    return true;
88}
89
90void 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
100void Input::positioned() {
101    point_type ln = static_cast<point_type>(_text->getLineCount());
102
103    ln = ln == 0.0f ? 1.0f : ln;
104
105    // point_type th = (_text->getCharacterHeight() * ln) + (_text->getLineSpacing() * (ln - 1.0f));
106
107    point_type x = getX() + _xoff;
108    point_type y = getY() + _yoff;
109
110    // XYCoord size = getTextSize();
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
120bool Input::keyUp(int key, int mask, WindowManager*) {
121    return false;
122}
123
124bool 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            // s.erase(s.begin() + (_index - 1));
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        // else if(_index < s.size()) s.insert(s.begin() + _index, key);
145        // else if(_index == s.size()) s.push_back(key);
146
147        s[_index] = key;
148       
149        _text->update();
150       
151        _calculateCursorOffsets();
152
153        _index++;
154    }
155
156    // _text->update();
157
158    _calculateSize(getTextSize());
159
160    getParent()->resize();
161
162    return false;
163}
164
165void Input::setCursor(Widget*) {
166}
167
168unsigned 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}
Note: See TracBrowser for help on using the browser.