From Jean-Sebastien Guay, "osgWidget::WindowManager did nothing in its keyUp event, and in particular didn't call any callbacks. Since I wanted to have callbacks on keyUp, I copied what it does on keyDown, which works for me. I could have just used keyDown and saved myself the trouble, but you know me... :-)

osgWidget::Input:

[Functional changes]
- Previously, the field would be filled with spaces up to its max length, and typing would just replace the spaces. Also, there was a _textLength variable that kept track of the real length of text in the field, since the osgText::Text's length just reflected the length of spaces+text entered. This was not great, as you could still select the spaces with the mouse and it just feels hacky. So I changed it to only contain the text entered, no spaces, and _textLength was removed since it's now redundant (the osgText::Text's length is used instead).
- Fixed the selection size which (visually only) showed one more character selected than what was really selected.
- Fixed selection by dragging the mouse, it would sometimes not select the last character of the string.
- Cursor will now accurately reflect whether insert mode is activated (block cursor) or we're in normal mode (line cursor) like in most editors.
- Implemented Ctrl-X (cut)
- Added a new clear() method that allows the field to be emptied correctly. Useful for a command line interface, for example (hint, hint).
- Mouse and keyboard event handler methods would always return false, which meant selecting with the mouse would also rotate the trackball, and typing an 's' would turn on stats.

[Code cleanup]
- Renamed the (local) _selectionMin and _selectionMax variables which are used in a lot of places, as the underscores would lead to think they were members. Either I called them selection{Min|Max} or delete{Min|Max} where it made more sense.
- Fixed some indenting which was at 3 spaces (inconsistently), I'm sure I didn't catch all the lines where this was the case though.
- Put spaces between variable, operator and value where missing, especially in for()s. Again I only did this where I made changes, there are probably others left.

The result is that delete, backspace, Ctrl-X, Ctrl-C, Ctrl-V, and typing behaviour should now be consistent with text editor conventions, whether insert mode is enabled or not. I hope. :-)

Note, there's a nasty const_cast in there. Why isn't osgText::Font::getGlyph() declared const?

Also, as a note, the current implementation of cut, copy and paste (in addition to being Windows only, yuck) gets and puts the data into an std::string, thus if the osgText::String in the field contains unicode characters I think it won't work correctly. Perhaps someone could implement a proper clipboard class that would be cross-platform and support osgText::String (more precisely other languages like Chinese) correctly? Cut, copy and paste are not critical to what I'm doing so I won't invest the time to do that, but I just thought I'd mention it.
"
This commit is contained in:
Robert Osfield
2010-09-09 16:49:10 +00:00
parent 78cb15fdf8
commit 687fd9362f
5 changed files with 347 additions and 309 deletions

View File

@@ -12,42 +12,42 @@ public:
virtual bool isBinary() const { return true; }
virtual void writeBool( bool b )
{ char c = b?1:0; _out->write( &c, CHAR_SIZE ); }
{ char c = b?1:0; _out->write( &c, osgDB::CHAR_SIZE ); }
virtual void writeChar( char c )
{ _out->write( &c, CHAR_SIZE ); }
{ _out->write( &c, osgDB::CHAR_SIZE ); }
virtual void writeUChar( unsigned char c )
{ _out->write( (char*)&c, CHAR_SIZE ); }
{ _out->write( (char*)&c, osgDB::CHAR_SIZE ); }
virtual void writeShort( short s )
{ _out->write( (char*)&s, SHORT_SIZE ); }
{ _out->write( (char*)&s, osgDB::SHORT_SIZE ); }
virtual void writeUShort( unsigned short s )
{ _out->write( (char*)&s, SHORT_SIZE ); }
{ _out->write( (char*)&s, osgDB::SHORT_SIZE ); }
virtual void writeInt( int i )
{ _out->write( (char*)&i, INT_SIZE ); }
{ _out->write( (char*)&i, osgDB::INT_SIZE ); }
virtual void writeUInt( unsigned int i )
{ _out->write( (char*)&i, INT_SIZE ); }
{ _out->write( (char*)&i, osgDB::INT_SIZE ); }
virtual void writeLong( long l )
{ _out->write( (char*)&l, LONG_SIZE ); }
{ _out->write( (char*)&l, osgDB::LONG_SIZE ); }
virtual void writeULong( unsigned long l )
{ _out->write( (char*)&l, LONG_SIZE ); }
{ _out->write( (char*)&l, osgDB::LONG_SIZE ); }
virtual void writeFloat( float f )
{ _out->write( (char*)&f, FLOAT_SIZE ); }
{ _out->write( (char*)&f, osgDB::FLOAT_SIZE ); }
virtual void writeDouble( double d )
{ _out->write((char*)&d, DOUBLE_SIZE); }
{ _out->write((char*)&d, osgDB::DOUBLE_SIZE); }
virtual void writeString( const std::string& s )
{
int size = s.size();
_out->write( (char*)&size, INT_SIZE );
_out->write( (char*)&size, osgDB::INT_SIZE );
_out->write( s.c_str(), s.size() );
}
@@ -56,10 +56,10 @@ public:
virtual void writeBase( std::ios_base& (*fn)(std::ios_base&) ) {}
virtual void writeGLenum( const osgDB::ObjectGLenum& value )
{ GLenum e = value.get(); _out->write((char*)&e, GLENUM_SIZE); }
{ GLenum e = value.get(); _out->write((char*)&e, osgDB::GLENUM_SIZE); }
virtual void writeProperty( const osgDB::ObjectProperty& prop )
{ if (prop._mapProperty) _out->write((char*)&(prop._value), INT_SIZE); }
{ if (prop._mapProperty) _out->write((char*)&(prop._value), osgDB::INT_SIZE); }
virtual void writeMark( const osgDB::ObjectMark& mark ) {}
@@ -81,65 +81,65 @@ public:
virtual void readBool( bool& b )
{
char c = 0;
_in->read( &c, CHAR_SIZE );
_in->read( &c, osgDB::CHAR_SIZE );
b = (c!=0);
}
virtual void readChar( char& c )
{ _in->read( &c, CHAR_SIZE ); }
{ _in->read( &c, osgDB::CHAR_SIZE ); }
virtual void readSChar( signed char& c )
{ _in->read( (char*)&c, CHAR_SIZE ); }
{ _in->read( (char*)&c, osgDB::CHAR_SIZE ); }
virtual void readUChar( unsigned char& c )
{ _in->read( (char*)&c, CHAR_SIZE ); }
{ _in->read( (char*)&c, osgDB::CHAR_SIZE ); }
virtual void readShort( short& s )
{
_in->read( (char*)&s, SHORT_SIZE );
if ( _byteSwap ) osg::swapBytes( (char*)&s, SHORT_SIZE );
_in->read( (char*)&s, osgDB::SHORT_SIZE );
if ( _byteSwap ) osg::swapBytes( (char*)&s, osgDB::SHORT_SIZE );
}
virtual void readUShort( unsigned short& s )
{
_in->read( (char*)&s, SHORT_SIZE );
if ( _byteSwap ) osg::swapBytes( (char*)&s, SHORT_SIZE );
_in->read( (char*)&s, osgDB::SHORT_SIZE );
if ( _byteSwap ) osg::swapBytes( (char*)&s, osgDB::SHORT_SIZE );
}
virtual void readInt( int& i )
{
_in->read( (char*)&i, INT_SIZE );
if ( _byteSwap ) osg::swapBytes( (char*)&i, INT_SIZE );
_in->read( (char*)&i, osgDB::INT_SIZE );
if ( _byteSwap ) osg::swapBytes( (char*)&i, osgDB::INT_SIZE );
}
virtual void readUInt( unsigned int& i )
{
_in->read( (char*)&i, INT_SIZE );
if ( _byteSwap ) osg::swapBytes( (char*)&i, INT_SIZE );
_in->read( (char*)&i, osgDB::INT_SIZE );
if ( _byteSwap ) osg::swapBytes( (char*)&i, osgDB::INT_SIZE );
}
virtual void readLong( long& l )
{
_in->read( (char*)&l, LONG_SIZE );
if ( _byteSwap ) osg::swapBytes( (char*)&l, LONG_SIZE );
_in->read( (char*)&l, osgDB::LONG_SIZE );
if ( _byteSwap ) osg::swapBytes( (char*)&l, osgDB::LONG_SIZE );
}
virtual void readULong( unsigned long& l )
{
_in->read( (char*)&l, LONG_SIZE );
if ( _byteSwap ) osg::swapBytes( (char*)&l, LONG_SIZE );
_in->read( (char*)&l, osgDB::LONG_SIZE );
if ( _byteSwap ) osg::swapBytes( (char*)&l, osgDB::LONG_SIZE );
}
virtual void readFloat( float& f )
{
_in->read( (char*)&f, FLOAT_SIZE );
if ( _byteSwap ) osg::swapBytes( (char*)&f, FLOAT_SIZE );
_in->read( (char*)&f, osgDB::FLOAT_SIZE );
if ( _byteSwap ) osg::swapBytes( (char*)&f, osgDB::FLOAT_SIZE );
}
virtual void readDouble( double& d )
{
_in->read( (char*)&d, DOUBLE_SIZE );
if ( _byteSwap ) osg::swapBytes( (char*)&d, DOUBLE_SIZE );
_in->read( (char*)&d, osgDB::DOUBLE_SIZE );
if ( _byteSwap ) osg::swapBytes( (char*)&d, osgDB::DOUBLE_SIZE );
}
virtual void readString( std::string& s )
@@ -159,8 +159,8 @@ public:
virtual void readGLenum( osgDB::ObjectGLenum& value )
{
GLenum e = 0;
_in->read( (char*)&e, GLENUM_SIZE );
if ( _byteSwap ) osg::swapBytes( (char*)&e, GLENUM_SIZE );
_in->read( (char*)&e, osgDB::GLENUM_SIZE );
if ( _byteSwap ) osg::swapBytes( (char*)&e, osgDB::GLENUM_SIZE );
value.set( e );
}
@@ -169,8 +169,8 @@ public:
int value = 0;
if ( prop._mapProperty )
{
_in->read( (char*)&value, INT_SIZE );
if ( _byteSwap ) osg::swapBytes( (char*)&value, INT_SIZE );
_in->read( (char*)&value, osgDB::INT_SIZE );
if ( _byteSwap ) osg::swapBytes( (char*)&value, osgDB::INT_SIZE );
}
prop.set( value );
}