1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221
|
#if defined(WIN32) && ! defined(__CYGWIN__)
#include <windows.h>
#define sleep(x) Sleep(x*1000);
#else
#include <unistd.h>
#endif
#include <GL/gl.h>
#include <Producer/RenderSurface>
#include <Producer/KeyboardMouse>
#include "Text.h"
using namespace Producer;
class Renderer
{
public:
Renderer( RenderSurface &rs ):
_initialized(false),
_keyChar(Producer::KeyChar_Unknown),
_rs(rs),
_mbutton(0),
_scroll_display_counter(0)
{
_mx = _my = _pmx = _pmy = 0;
}
void draw()
{
if( !_initialized ) init();
glViewport( 0, 0, _rs.getWindowWidth(), _rs.getWindowHeight() );
glClear( GL_COLOR_BUFFER_BIT );
glMatrixMode( GL_PROJECTION );
glLoadIdentity();
glOrtho( 0.0, double(_rs.getWindowWidth()), 0.0, double(_rs.getWindowHeight()), -1.0, 1.0);
glMatrixMode( GL_MODELVIEW );
glLoadIdentity();
char buff[128];
float x, y;
if( _keyChar != Producer::KeyChar_Unknown )
{
x = (_rs.getWindowWidth() - (22 * 10)) * 0.5;
y = (_rs.getWindowHeight() - 10) * 0.5;
glColor4f( 1, 1, 0, 1 );
if( Keyboard::isSpecialKey(_keyChar) )
{
y -= 30.0;
sprintf( buff, "Special Key Press: '0x%04x'", _keyChar );
}
else
sprintf( buff, " Key Press: '%c'", _keyChar );
glRasterPos2f(x,y);
Text::getSingleton()->drawString(Text::BitmapFont, buff );
}
x = 10.0;
y = float(_rs.getWindowHeight()) - 50.0;
glRasterPos2f(x,y);
glColor4f( 1, 1, 0, 1 );
sprintf( buff, "Passive Mouse Motion: %8.4f %8.4f", _pmx, _pmy );
Text::getSingleton()->drawString( Text::BitmapFont, buff );
y -= 30.0;
glRasterPos2f(x,y);
sprintf( buff, " Active Mouse Motion: %8.4f %8.4f button = 0x%x", _mx, _my, _mbutton );
Text::getSingleton()->drawString( Text::BitmapFont, buff );
y -= 30.0;
glRasterPos2f(x,y);
sprintf( buff, " Mouse Scroll: %s",
_scroll == KeyboardMouseCallback::ScrollUp ? "UP" :
_scroll == KeyboardMouseCallback::ScrollDown ? "DOWN" : " " );
Text::getSingleton()->drawString( Text::BitmapFont, buff );
if( _scroll_display_counter > 0 )
{
if( --_scroll_display_counter <= 0 )
_scroll = KeyboardMouseCallback::ScrollNone;
}
}
void setKeyChar(Producer::KeyCharacter keyChar ) { _keyChar = keyChar; }
void unsetKeyChar() { _keyChar = Producer::KeyChar_Unknown; }
void setPassiveMouse( float mx, float my )
{
_pmx = mx;
_pmy = my;
}
void setMouse(float mx, float my, unsigned int mbutton )
{
_mx = mx;
_my = my;
_mbutton = mbutton;
}
void setScroll( KeyboardMouseCallback::ScrollingMotion scroll )
{
_scroll = scroll;
_scroll_display_counter = 10;
}
private:
bool _initialized;
Producer::KeyCharacter _keyChar;
Producer::RenderSurface &_rs;
float _mx, _my;
float _pmx, _pmy;
unsigned int _mbutton;
KeyboardMouseCallback::ScrollingMotion _scroll;
unsigned int _scroll_display_counter;
void init()
{
glClearColor( 0.2f, 0.2f, 0.4f, 1.0f );
_initialized = true;
}
};
class myKeyboardMouseCallback : public KeyboardMouseCallback
{
public:
myKeyboardMouseCallback( Renderer &renderer ):
_renderer(renderer),
_done(false),
_button(0)
{}
virtual ~myKeyboardMouseCallback() {}
void keyPress( Producer::KeyCharacter key )
{
_renderer.setKeyChar( key );
}
void keyRelease( Producer::KeyCharacter /*key*/ )
{
_renderer.unsetKeyChar();
}
void specialKeyPress( Producer::KeyCharacter key )
{
_renderer.setKeyChar( key );
}
void specialKeyRelease( Producer::KeyCharacter key )
{
_renderer.unsetKeyChar();
if( key == Producer::KeyChar_Escape )
_done = true;
}
void mouseScroll ( Producer::KeyboardMouseCallback::ScrollingMotion scroll )
{
_renderer.setScroll( scroll );
}
void mouseMotion( float mx, float my )
{
_renderer.setMouse(mx,my,_button);
}
void buttonPress( float mx, float my, unsigned int button )
{
_button |= (1<<(button-1));
_renderer.setMouse(mx,my,_button);
}
void buttonRelease( float mx, float my, unsigned int button )
{
_button &= ~(1<<(button-1));
_renderer.setMouse(mx,my,_button);
}
void passiveMouseMotion( float mx, float my )
{
_renderer.setPassiveMouse(mx,my);
}
void shutdown() { _done = true; }
bool done() { return _done; }
private:
Renderer &_renderer;
bool _done;
unsigned int _button;
};
int main()
{
ref_ptr<RenderSurface> rs = new RenderSurface;
rs->setWindowRectangle(100,100,640,480);
rs->setWindowName( "Keyboard Mouse Test" );
Renderer renderer(*rs.get());
ref_ptr<KeyboardMouse> kbm = new KeyboardMouse(rs.get());
ref_ptr<myKeyboardMouseCallback> kbmcb = new myKeyboardMouseCallback(renderer);
kbm->setCallback( kbmcb.get() );
Keyboard::getSingleton()->mapKey( KeyCombination( KeyMod_Control|KeyMod_Alt, Key_A ), KeyChar_percent);
kbm->startThread();
rs->realize();
while( !kbmcb->done() )
{
renderer.draw();
rs->swapBuffers();
}
rs = 0;
kbmcb = 0;
kbm = 0;
return 0;
}
|