Sirikata
liboh/plugins/ogre/OgreSystemInputHandler.hpp
Go to the documentation of this file.
00001 // Copyright (c) 2011 Sirikata Authors. All rights reserved.
00002 // Use of this source code is governed by a BSD-style license that can
00003 // be found in the LICENSE file.
00004 
00005 #include <sirikata/proxyobject/Platform.hpp>
00006 #include "OgreSystem.hpp"
00007 #include <sirikata/ogre/WebView.hpp>
00008 #include <sirikata/ogre/WebViewManager.hpp>
00009 #include "OgreMeshRaytrace.hpp"
00010 #include <sirikata/core/transfer/DiskManager.hpp>
00011 #include <sirikata/ogre/input/InputListener.hpp>
00012 #include <sirikata/ogre/input/InputEventCompletion.hpp>
00013 
00014 namespace Sirikata {
00015 
00016 namespace Input {
00017 class InputDevice;
00018 }
00019 
00020 namespace Graphics {
00021 
00022 class OgreSystemInputHandler : public Input::InputListener {
00023 public:
00024     OgreSystemInputHandler(OgreSystem *parent);
00025     ~OgreSystemInputHandler();
00026 
00027     void alert(const String& title, const String& text);
00028     void tick(const Task::LocalTime& t);
00029 
00030     // Invoked when the main UI components are ready
00031     void uiReady();
00032 
00033     void addDelegate(Invokable* del);
00034     void removeDelegate(Invokable* del);
00035 
00036     // FIXME no reason for this to be in this class.
00037     SpaceObjectReference pick(Vector2f p, int direction, const SpaceObjectReference& ignore, Vector3f* hitPointOut=NULL);
00038 
00039     //FIXME should this be public?
00040     WebView* mUIWidgetView;
00041     void ensureUI();
00042     void windowResized(uint32 w, uint32 h);
00043 private:
00044     void delegateEvent(Sirikata::Input::InputEventPtr inputev);
00045 
00046     // Gets the current set of modifiers from the input system. Used for mouse
00047     // events exposed via Invokable interface since the internal mouse events
00048     // don't come with modifiers.
00049     Sirikata::Input::Modifier getCurrentModifiers() const;
00050 
00051     ProxyEntity* hoverEntity (Camera *cam, Time time, float xPixel, float yPixel, bool mousedown, int *hitCount,int which=0, Vector3f* hitPointOut=NULL, SpaceObjectReference ignore = SpaceObjectReference::null());
00052 
00053     bool recentMouseInRange(float x, float y, float *lastX, float *lastY);
00054 
00055     void createUIAction(const String& ui_page);
00056 
00057     // InputListener Interface
00058     virtual Input::EventResponse onInputDeviceEvent(Input::InputDeviceEventPtr ev);
00059     virtual Input::EventResponse onKeyPressedEvent(Input::ButtonPressedPtr ev);
00060     virtual Input::EventResponse onKeyRepeatedEvent(Input::ButtonRepeatedPtr ev);
00061     virtual Input::EventResponse onKeyReleasedEvent(Input::ButtonReleasedPtr ev);
00062     virtual Input::EventResponse onKeyDownEvent(Input::ButtonDownPtr ev);
00063     virtual Input::EventResponse onAxisEvent(Input::AxisEventPtr ev);
00064     virtual Input::EventResponse onTextInputEvent(Input::TextInputEventPtr ev);
00065     virtual Input::EventResponse onMouseHoverEvent(Input::MouseHoverEventPtr ev);
00066     virtual Input::EventResponse onMousePressedEvent(Input::MousePressedEventPtr ev);
00067     virtual Input::EventResponse onMouseReleasedEvent(Input::MouseReleasedEventPtr ev);
00068     virtual Input::EventResponse onMouseClickEvent(Input::MouseClickEventPtr ev);
00069     virtual Input::EventResponse onMouseDragEvent(Input::MouseDragEventPtr ev);
00070     virtual Input::EventResponse onWebViewEvent(Input::WebViewEventPtr ev);
00071 
00072     void fpsUpdateTick(const Task::LocalTime& t);
00073 
00074     void renderStatsUpdateTick(const Task::LocalTime& t);
00075 
00076     boost::any onUIAction(WebView* webview, const JSArguments& args);
00077 
00078 
00079     class WebViewInputListener : public Input::InputListener {
00080     public:
00081         WebViewInputListener(OgreSystemInputHandler* par)
00082          : mParent(par)
00083         {}
00084 
00085         virtual Input::EventResponse onInputDeviceEvent(Input::InputDeviceEventPtr ev) { return Input::EventResponse::nop(); }
00086         virtual Input::EventResponse onKeyPressedEvent(Input::ButtonPressedPtr ev) { return onKeyEvent(ev); }
00087         virtual Input::EventResponse onKeyRepeatedEvent(Input::ButtonRepeatedPtr ev) { return onKeyEvent(ev); }
00088         virtual Input::EventResponse onKeyReleasedEvent(Input::ButtonReleasedPtr ev) { return onKeyEvent(ev); }
00089         virtual Input::EventResponse onKeyDownEvent(Input::ButtonDownPtr ev) { return onKeyEvent(ev); }
00090         Input::EventResponse onKeyEvent(Input::ButtonEventPtr ev);
00091         virtual Input::EventResponse onAxisEvent(Input::AxisEventPtr ev);
00092         virtual Input::EventResponse onTextInputEvent(Input::TextInputEventPtr ev);
00093         virtual Input::EventResponse onMouseHoverEvent(Input::MouseHoverEventPtr ev);
00094         virtual Input::EventResponse onMousePressedEvent(Input::MousePressedEventPtr ev);
00095         virtual Input::EventResponse onMouseReleasedEvent(Input::MouseReleasedEventPtr ev);
00096         virtual Input::EventResponse onMouseClickEvent(Input::MouseClickEventPtr ev);
00097         virtual Input::EventResponse onMouseDragEvent(Input::MouseDragEventPtr ev);
00098         virtual Input::EventResponse onWebViewEvent(Input::WebViewEventPtr ev) { return Input::EventResponse::nop(); }
00099 
00100         OgreSystemInputHandler* mParent;
00101         std::set<int> mWebViewActiveButtons;
00102     };
00103     friend class WebViewInputListener;
00104     class DelegateInputListener : public Input::InputListener {
00105     public:
00106         DelegateInputListener(OgreSystemInputHandler* par)
00107          : mParent(par)
00108         {}
00109 
00110         virtual Input::EventResponse onInputDeviceEvent(Input::InputDeviceEventPtr ev) { return Input::EventResponse::cancel(); }
00111         virtual Input::EventResponse onKeyPressedEvent(Input::ButtonPressedPtr ev) { delegateEvent(ev); return Input::EventResponse::cancel(); }
00112         virtual Input::EventResponse onKeyRepeatedEvent(Input::ButtonRepeatedPtr ev) { delegateEvent(ev); return Input::EventResponse::cancel(); }
00113         virtual Input::EventResponse onKeyReleasedEvent(Input::ButtonReleasedPtr ev) { delegateEvent(ev); return Input::EventResponse::cancel(); }
00114         virtual Input::EventResponse onKeyDownEvent(Input::ButtonDownPtr ev) { delegateEvent(ev); return Input::EventResponse::cancel(); }
00115         virtual Input::EventResponse onAxisEvent(Input::AxisEventPtr ev) { delegateEvent(ev); return Input::EventResponse::cancel(); }
00116         virtual Input::EventResponse onTextInputEvent(Input::TextInputEventPtr ev) { delegateEvent(ev); return Input::EventResponse::cancel(); }
00117         virtual Input::EventResponse onMouseHoverEvent(Input::MouseHoverEventPtr ev) { delegateEvent(ev); return Input::EventResponse::cancel(); }
00118         virtual Input::EventResponse onMousePressedEvent(Input::MousePressedEventPtr ev) { delegateEvent(ev); return Input::EventResponse::cancel(); }
00119         virtual Input::EventResponse onMouseReleasedEvent(Input::MouseReleasedEventPtr ev) { delegateEvent(ev); return Input::EventResponse::cancel(); }
00120         virtual Input::EventResponse onMouseClickEvent(Input::MouseClickEventPtr ev) { delegateEvent(ev); return Input::EventResponse::cancel(); }
00121         virtual Input::EventResponse onMouseDragEvent(Input::MouseDragEventPtr ev) { delegateEvent(ev); return Input::EventResponse::cancel(); }
00122         virtual Input::EventResponse onWebViewEvent(Input::WebViewEventPtr ev) { delegateEvent(ev); return Input::EventResponse::cancel(); }
00123 
00124         void delegateEvent(Input::InputEventPtr inputev);
00125 
00126         OgreSystemInputHandler* mParent;
00127         //key and value are same.
00128         std::map<Invokable*,Invokable*> mDelegates;
00129     };
00130     friend class DelegateInputListener;
00131 
00132     OgreSystem *mParent;
00133 
00134     WebViewInputListener mWebViewInputListener;
00135     DelegateInputListener mDelegateInputListener;
00136     Input::InputEventCompletion mEventCompleter;
00137 
00138     int mWhichRayObject;
00139 
00140     int mLastHitCount;
00141     float mLastHitX;
00142     float mLastHitY;
00143 
00144     Task::LocalTime mLastCameraTime;
00145     Task::LocalTime mLastFpsTime;
00146     Task::LocalTime mLastRenderStatsTime;
00147 
00148     bool mUIReady;
00149 };
00150 
00151 
00152 }
00153 }