#include <QWidget>
#include <QTimer>
#include <QKeyEvent>
#include <irrlicht.h>
class IrrlichtWidget
: public QWidget{ Q_OBJECT
public:
IrrlichtWidget(irr::u32 width = 600, irr::u32 height = 400){
setFixedSize(width, height);
irr::SIrrlichtCreationParameters params;
params.DriverType = irr::video::EDT_OPENGL;
params.WindowId = (void*)winId();
params.WindowSize.Width = width;
params.WindowSize.Height = height;
m_device = irr::createDeviceEx(params);
setAttribute(Qt::WA_OpaquePaintEvent);
m_timer->setInterval(0);
QObject::connect(m_timer,
&QTimer
::timeout,
[this](){ emit inIrrlichtMainLoop();
m_device->run();
});
m_timer->start();
}
irr::IrrlichtDevice *getIrrlichtDevice() const{
return m_device;
}
signals:
void inIrrlichtMainLoop();
protected:
irr::SEvent irrEvent;
irrEvent.EventType = irr::EET_KEY_INPUT_EVENT;
IrrlichtKey irrKey;
irrKey.code = (irr::EKEY_CODE)(0);
irrKey.ch = (wchar_t)(0);
if((event->key() >= Qt::Key_A && event->key() <= Qt::Key_Z) || (event->key() >= Qt::Key_0 && event->key() <= Qt::Key_9)){
irrKey.code = (irr::EKEY_CODE)event->key();
irrKey.ch = (wchar_t)event->key();
}
else{
switch(event->key()){
case Qt::Key_Up:
irrKey.code = irr::KEY_UP;
break;
case Qt::Key_Down:
irrKey.code = irr::KEY_DOWN;
break;
case Qt::Key_Left:
irrKey.code = irr::KEY_LEFT;
break;
case Qt::Key_Right:
irrKey.code = irr::KEY_RIGHT;
break;
}
}
if(irrKey.code != 0){
irrEvent.KeyInput.Key = irrKey.code;
irrEvent.KeyInput.Control = ((event->modifiers() & Qt::ControlModifier) != 0);
irrEvent.KeyInput.Shift = ((event->modifiers() & Qt::ShiftModifier) != 0);
irrEvent.KeyInput.Char = irrKey.ch;
irrEvent.KeyInput.PressedDown = true;
m_device->postEventFromUser(irrEvent);
}
event->ignore();
}
irr::SEvent irrEvent;
irrEvent.EventType = irr::EET_KEY_INPUT_EVENT;
IrrlichtKey irrKey;
irrKey.code = (irr::EKEY_CODE)(0);
irrKey.ch = (wchar_t)(0);
if((event->key() >= Qt::Key_A && event->key() <= Qt::Key_Z) || (event->key() >= Qt::Key_0 && event->key() <= Qt::Key_9)){
irrKey.code = (irr::EKEY_CODE)event->key();
irrKey.ch = (wchar_t)event->key();
}
else{
switch(event->key()){
case Qt::Key_Up:
irrKey.code = irr::KEY_UP;
break;
case Qt::Key_Down:
irrKey.code = irr::KEY_DOWN;
break;
case Qt::Key_Left:
irrKey.code = irr::KEY_LEFT;
break;
case Qt::Key_Right:
irrKey.code = irr::KEY_RIGHT;
break;
}
}
if(irrKey.code != 0){
irrEvent.KeyInput.Key = irrKey.code;
irrEvent.KeyInput.Control = ((event->modifiers() & Qt::ControlModifier) != 0);
irrEvent.KeyInput.Shift = ((event->modifiers() & Qt::ShiftModifier) != 0);
irrEvent.KeyInput.Char = irrKey.ch;
irrEvent.KeyInput.PressedDown = false;
m_device->postEventFromUser(irrEvent);
}
event->ignore();
}
irr::SEvent irrEvent;
irrEvent.EventType = irr::EET_MOUSE_INPUT_EVENT;
switch(event->button()){
case Qt::LeftButton:
irrEvent.MouseInput.Event = irr::EMIE_LMOUSE_PRESSED_DOWN;
break;
case Qt::MidButton:
irrEvent.MouseInput.Event = irr::EMIE_MMOUSE_PRESSED_DOWN;
break;
case Qt::RightButton:
irrEvent.MouseInput.Event = irr::EMIE_RMOUSE_PRESSED_DOWN;
break;
default:
return;
}
irrEvent.MouseInput.X = event->x();
irrEvent.MouseInput.Y = event->y();
irrEvent.MouseInput.Wheel = 0.0f;
m_device->postEventFromUser(irrEvent);
event->ignore();
}
irr::SEvent irrEvent;
irrEvent.EventType = irr::EET_MOUSE_INPUT_EVENT;
switch(event->button()){
case Qt::LeftButton:
irrEvent.MouseInput.Event = irr::EMIE_LMOUSE_LEFT_UP;
break;
case Qt::MidButton:
irrEvent.MouseInput.Event = irr::EMIE_MMOUSE_LEFT_UP;
break;
case Qt::RightButton:
irrEvent.MouseInput.Event = irr::EMIE_RMOUSE_LEFT_UP;
break;
default:
return;
}
irrEvent.MouseInput.X = event->x();
irrEvent.MouseInput.Y = event->y();
irrEvent.MouseInput.Wheel = 0.0f;
m_device->postEventFromUser(irrEvent);
event->ignore();
}
if(event->orientation() == Qt::Vertical){
irr::SEvent irrEvent;
irrEvent.EventType = irr::EET_MOUSE_INPUT_EVENT;
irrEvent.MouseInput.Event = irr::EMIE_MOUSE_WHEEL;
irrEvent.MouseInput.X = 0;
irrEvent.MouseInput.Y = 0;
irrEvent.MouseInput.Wheel = event->delta() / 120.0f;
m_device->postEventFromUser(irrEvent);
}
event->ignore();
}
private:
struct IrrlichtKey{
irr::EKEY_CODE code;
wchar_t ch;
};
irr::IrrlichtDevice *m_device;
};
Bookmarks