mirror of
https://github.com/TES3MP/openmw-tes3mp.git
synced 2025-02-04 06:45:32 +00:00
Added event listeners (mouselook and gui injector) and exit listener for Ogre
This commit is contained in:
parent
e55ef227fe
commit
b9b306cd4c
7 changed files with 178 additions and 2 deletions
23
gui/events.cpp
Normal file
23
gui/events.cpp
Normal file
|
@ -0,0 +1,23 @@
|
||||||
|
#include <MyGUI.h>
|
||||||
|
#include <OIS/OIS.h>
|
||||||
|
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
}
|
26
gui/events.hpp
Normal file
26
gui/events.hpp
Normal file
|
@ -0,0 +1,26 @@
|
||||||
|
#ifndef OENGINE_MYGUI_EVENTS_H
|
||||||
|
#define OENGINE_MYGUI_EVENTS_H
|
||||||
|
|
||||||
|
#include <mangle/input/event.hpp>
|
||||||
|
|
||||||
|
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
|
33
ogre/exitlistener.hpp
Normal file
33
ogre/exitlistener.hpp
Normal file
|
@ -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 <OgreFrameListener.h>
|
||||||
|
#include <OgreRenderWindow.h>
|
||||||
|
|
||||||
|
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
|
36
ogre/mouselook.cpp
Normal file
36
ogre/mouselook.cpp
Normal file
|
@ -0,0 +1,36 @@
|
||||||
|
#include "mouselook.hpp"
|
||||||
|
|
||||||
|
#include <OIS/OIS.h>
|
||||||
|
#include <OgreCamera.h>
|
||||||
|
|
||||||
|
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));
|
||||||
|
}
|
54
ogre/mouselook.hpp
Normal file
54
ogre/mouselook.hpp
Normal file
|
@ -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 <mangle/input/event.hpp>
|
||||||
|
|
||||||
|
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
|
|
@ -17,6 +17,11 @@ void OgreRenderer::cleanup()
|
||||||
mRoot = NULL;
|
mRoot = NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void OgreRenderer::start()
|
||||||
|
{
|
||||||
|
mRoot->startRendering();
|
||||||
|
}
|
||||||
|
|
||||||
void OgreRenderer::screenshot(const std::string &file)
|
void OgreRenderer::screenshot(const std::string &file)
|
||||||
{
|
{
|
||||||
mWindow->writeContentsToFile(file);
|
mWindow->writeContentsToFile(file);
|
||||||
|
|
|
@ -5,7 +5,6 @@
|
||||||
Ogre renderer class
|
Ogre renderer class
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#include <OgreRoot.h>
|
|
||||||
#include <string>
|
#include <string>
|
||||||
|
|
||||||
namespace Ogre
|
namespace Ogre
|
||||||
|
@ -52,7 +51,7 @@ namespace Render
|
||||||
void cleanup();
|
void cleanup();
|
||||||
|
|
||||||
/// Start the main rendering loop
|
/// Start the main rendering loop
|
||||||
void start() { mRoot->startRendering(); }
|
void start();
|
||||||
|
|
||||||
/// Write a screenshot to file
|
/// Write a screenshot to file
|
||||||
void screenshot(const std::string &file);
|
void screenshot(const std::string &file);
|
||||||
|
|
Loading…
Reference in a new issue