diff --git a/gui/events.cpp b/gui/events.cpp new file mode 100644 index 000000000..39b5e9f1c --- /dev/null +++ b/gui/events.cpp @@ -0,0 +1,23 @@ +#include +#include + +using namespace MyGUI; +using namespace OIS; + +void EventInjector::event(Type type, int index, const void *p) +{ + if(enabled) return; + + KeyEvent *key = (KeyEvent*)p; + MouseEvent *mouse = (MouseEvent*)p; + MouseButtonID id = (MouseButtonID)index; + + switch(type) + { + case EV_KeyDown: gui->injectKeyPress(key); break; + case EV_KeyUp: gui->injectKeyRelease(key); break; + case EV_MouseDown: gui->injectMousePress(mouse, id); break; + case EV_MouseUp: gui->injectMouseRelease(mouse, id); break; + case EV_MouseMove: gui->injectMouseMove(mouse); break; + } +} diff --git a/gui/events.hpp b/gui/events.hpp new file mode 100644 index 000000000..b994a422d --- /dev/null +++ b/gui/events.hpp @@ -0,0 +1,26 @@ +#ifndef OENGINE_MYGUI_EVENTS_H +#define OENGINE_MYGUI_EVENTS_H + +#include + +namespace MyGUI +{ + class Gui; +} + +namespace GUI +{ + /** Event handler that injects OIS events into MyGUI + */ + class EventInjector : Mangle::Input::Event + { + MyGUI::Gui *gui; + + public: + bool enabled; + + EventInjector(MyGUI::Gui *g) : gui(g), enabled(true) {} + void event(Type type, int index, const void *p); + }; +} +#endif diff --git a/ogre/exitlistener.hpp b/ogre/exitlistener.hpp new file mode 100644 index 000000000..e1af9604f --- /dev/null +++ b/ogre/exitlistener.hpp @@ -0,0 +1,33 @@ +#ifndef OENGINE_OGRE_EXITLISTEN_H +#define OENGINE_OGRE_EXITLISTEN_H + +/* + This FrameListener simply exits the rendering loop when the window + is closed. You can also tell it to exit manually by setting the exit + member to true; + */ + +#include +#include + +namespace Render +{ + struct ExitListener : Ogre::FrameListener + { + Ogre::RenderWindow *window; + bool exit; + + ExitListener(Ogre::RenderWindow *wnd) + : window(wnd), exit(false) {} + + bool frameStarted(const FrameEvent &evt) + { + if(window->isClosed()) + exit = true; + + return !exit; + } + }; +} + +#endif diff --git a/ogre/mouselook.cpp b/ogre/mouselook.cpp new file mode 100644 index 000000000..d4d15cee9 --- /dev/null +++ b/ogre/mouselook.cpp @@ -0,0 +1,36 @@ +#include "mouselook.hpp" + +#include +#include + +using namespace OIS; +using namespace Ogre; + +void MouseLookEvent::event(Type type, int index, const void *p) +{ + if(type != EV_MouseMove || camera == NULL) return; + + MouseEvent *arg = (MouseEvent*)(p); + + float x = arg->state.X.rel * sensX; + float y = arg->state.Y.rel * sensY; + + camera->yaw(Degree(-x)); + + if(flipProt) + { + // The camera before pitching + Quaternion nopitch = camera->getOrientation(); + + camera->pitch(Degree(-y)); + + // Apply some failsafe measures against the camera flipping + // upside down. Is the camera close to pointing straight up or + // down? + if(camera->getUp()[1] <= 0.1) + // If so, undo the last pitch + camera->setOrientation(nopitch); + } + else + camera->pitch(Degree(-y)); +} diff --git a/ogre/mouselook.hpp b/ogre/mouselook.hpp new file mode 100644 index 000000000..0b11c0b79 --- /dev/null +++ b/ogre/mouselook.hpp @@ -0,0 +1,54 @@ +#ifndef OENGINE_OGRE_MOUSELOOK_H +#define OENGINE_OGRE_MOUSELOOK_H + +/* + A mouse-look class for Ogre. Accepts input events from Mangle::Input + and translates them. + + You can adjust the mouse sensibility and switch to a different + camera. The mouselook class also has an optional wrap protection + that keeps the camera from flipping upside down. + + You can disable the mouse looker at any time by calling + setCamera(NULL), and reenable it by setting the camera back. + + NOTE: The current implementation will ONLY work for native OIS + events. + */ + +#include + +namespace Ogre +{ + class Camera; +} + +namespace Render +{ + class MouseLookEvent : public Mangle::Input::Event + { + Ogre::Camera* camera; + float sensX, sensY; // Mouse sensibility + bool flipProt; // Flip protection + + public: + MouseLookEvent(Ogre::Camera *cam=NULL, + float sX=0.2, float sY=0.2, + bool prot=true) + : camera(cam) + , sensX(sX) + , sensY(sy) + , flipProt(prot) + {} + + void setCamera(Ogre::Camera *cam) + { camera = cam; } + void setSens(float sX, float sY) + { sensX = sX; sensY = sY; } + void setProt(bool p) { flipProt = p; } + + void event(Type type, int index, const void *p); + }; +} + +#endif diff --git a/ogre/renderer.cpp b/ogre/renderer.cpp index 982f0f050..86382127c 100644 --- a/ogre/renderer.cpp +++ b/ogre/renderer.cpp @@ -17,6 +17,11 @@ void OgreRenderer::cleanup() mRoot = NULL; } +void OgreRenderer::start() +{ + mRoot->startRendering(); +} + void OgreRenderer::screenshot(const std::string &file) { mWindow->writeContentsToFile(file); diff --git a/ogre/renderer.hpp b/ogre/renderer.hpp index 12dada2d5..ac44f5ee1 100644 --- a/ogre/renderer.hpp +++ b/ogre/renderer.hpp @@ -5,7 +5,6 @@ Ogre renderer class */ -#include #include namespace Ogre @@ -52,7 +51,7 @@ namespace Render void cleanup(); /// Start the main rendering loop - void start() { mRoot->startRendering(); } + void start(); /// Write a screenshot to file void screenshot(const std::string &file);