From 12b01dcb25ee7897c15639ee07f5ebfec8d3551b Mon Sep 17 00:00:00 2001 From: Dale Stammen Date: Sat, 19 Apr 2014 09:32:59 -0700 Subject: [PATCH] input events refactor --- .../platform/wp8-xaml/cpp/Cocos2dRenderer.cpp | 25 ---------- .../platform/wp8-xaml/cpp/Cocos2dRenderer.h | 4 -- .../platform/wp8-xaml/cpp/Direct3DInterop.cpp | 46 +++++-------------- .../platform/wp8-xaml/cpp/Direct3DInterop.h | 8 +--- .../2d/platform/wp8-xaml/cpp/EditBoxEvent.cpp | 2 +- cocos/2d/platform/wp8-xaml/cpp/EditBoxEvent.h | 6 +-- cocos/2d/platform/wp8/CCGLView.cpp | 39 ++++++++++++++++ cocos/2d/platform/wp8/CCGLView.h | 22 ++++++++- 8 files changed, 77 insertions(+), 75 deletions(-) diff --git a/cocos/2d/platform/wp8-xaml/cpp/Cocos2dRenderer.cpp b/cocos/2d/platform/wp8-xaml/cpp/Cocos2dRenderer.cpp index 7f1e429135..3a048a40fc 100644 --- a/cocos/2d/platform/wp8-xaml/cpp/Cocos2dRenderer.cpp +++ b/cocos/2d/platform/wp8-xaml/cpp/Cocos2dRenderer.cpp @@ -99,15 +99,6 @@ IAsyncAction^ Cocos2dRenderer::OnSuspending() } -// user pressed the Back Key on the phone -void Cocos2dRenderer::OnBackKeyPress() -{ - // handle the backkey in your app here. - // call Cocos2dEvent::TerminateApp if it is time to exit your app. - // ie. the user is on your first page and wishes to exit your app. - m_delegate->Invoke(Cocos2dEvent::TerminateApp); -} - void Cocos2dRenderer::OnUpdateDevice() { GLView* glview = GLView::sharedOpenGLView(); @@ -132,21 +123,6 @@ bool Cocos2dRenderer::OnRender() return false; } -void Cocos2dRenderer::OnPointerPressed(PointerEventArgs^ args) -{ - GLView::sharedOpenGLView()->OnPointerPressed(args); -} - -void Cocos2dRenderer::OnPointerMoved(PointerEventArgs^ args) -{ - GLView::sharedOpenGLView()->OnPointerMoved(args); -} - -void Cocos2dRenderer::OnPointerReleased(PointerEventArgs^ args) -{ - GLView::sharedOpenGLView()->OnPointerReleased(args); -} - void Cocos2dRenderer::OnKeyPressed(Platform::String^ text) { char szUtf8[8] = {0}; @@ -154,7 +130,6 @@ void Cocos2dRenderer::OnKeyPressed(Platform::String^ text) IMEDispatcher::sharedDispatcher()->dispatchInsertText(szUtf8, nLen); } - void Cocos2dRenderer::OnCocos2dKeyEvent(Cocos2dKeyEvent event) { switch(event) diff --git a/cocos/2d/platform/wp8-xaml/cpp/Cocos2dRenderer.h b/cocos/2d/platform/wp8-xaml/cpp/Cocos2dRenderer.h index c2117b9829..796cc6e526 100644 --- a/cocos/2d/platform/wp8-xaml/cpp/Cocos2dRenderer.h +++ b/cocos/2d/platform/wp8-xaml/cpp/Cocos2dRenderer.h @@ -46,11 +46,7 @@ public: void SetXamlMessageBoxDelegate(PhoneDirect3DXamlAppComponent::Cocos2dMessageBoxDelegate^ delegate); void SetXamlEditBoxDelegate(PhoneDirect3DXamlAppComponent::Cocos2dEditBoxDelegate^ delegate); - void OnPointerPressed(Windows::UI::Core::PointerEventArgs^ args); - void OnPointerMoved(Windows::UI::Core::PointerEventArgs^ args); - void OnPointerReleased(Windows::UI::Core::PointerEventArgs^ args); Windows::Foundation::IAsyncAction^ OnSuspending(); - void OnBackKeyPress(); void Connect(); void Disconnect(); diff --git a/cocos/2d/platform/wp8-xaml/cpp/Direct3DInterop.cpp b/cocos/2d/platform/wp8-xaml/cpp/Direct3DInterop.cpp index c855a40cbb..cddd4763f9 100644 --- a/cocos/2d/platform/wp8-xaml/cpp/Direct3DInterop.cpp +++ b/cocos/2d/platform/wp8-xaml/cpp/Direct3DInterop.cpp @@ -25,6 +25,7 @@ THE SOFTWARE. #include "Direct3DInterop.h" #include "Direct3DContentProvider.h" #include "EditBoxEvent.h" +#include "cocos2d.h" using namespace Windows::Foundation; using namespace Windows::UI::Core; @@ -91,69 +92,46 @@ IAsyncAction^ Direct3DInterop::OnSuspending() void Direct3DInterop::OnBackKeyPress() { - std::lock_guard guard(mMutex); - std::shared_ptr e(new BackButtonEvent()); - mInputEvents.push(e); + cocos2d::GLView::sharedOpenGLView()->QueueBackKeyPress(); } // Pointer Event Handlers. We need to queue up pointer events to pass them to the drawing thread void Direct3DInterop::OnPointerPressed(DrawingSurfaceManipulationHost^ sender, PointerEventArgs^ args) { - AddPointerEvent(PointerEventType::PointerPressed, args); + cocos2d::GLView::sharedOpenGLView()->QueuePointerEvent(cocos2d::PointerEventType::PointerPressed, args); } - void Direct3DInterop::OnPointerMoved(DrawingSurfaceManipulationHost^ sender, PointerEventArgs^ args) { - AddPointerEvent(PointerEventType::PointerMoved, args); + cocos2d::GLView::sharedOpenGLView()->QueuePointerEvent(cocos2d::PointerEventType::PointerMoved, args); } void Direct3DInterop::OnPointerReleased(DrawingSurfaceManipulationHost^ sender, PointerEventArgs^ args) { - AddPointerEvent(PointerEventType::PointerReleased, args); + cocos2d::GLView::sharedOpenGLView()->QueuePointerEvent(cocos2d::PointerEventType::PointerReleased, args); } void Direct3DInterop::OnCocos2dKeyEvent(Cocos2dKeyEvent key) { - std::lock_guard guard(mMutex); - std::shared_ptr e(new KeyboardEvent(key)); - mInputEvents.push(e); + std::shared_ptr e(new cocos2d::KeyboardEvent(key)); + cocos2d::GLView::sharedOpenGLView()->QueueEvent(e); } void Direct3DInterop::OnCocos2dKeyEvent(Cocos2dKeyEvent key, Platform::String^ text) { - std::lock_guard guard(mMutex); - std::shared_ptr e(new KeyboardEvent(key,text)); - mInputEvents.push(e); + std::shared_ptr e(new cocos2d::KeyboardEvent(key,text)); + cocos2d::GLView::sharedOpenGLView()->QueueEvent(e); } -void Direct3DInterop::AddPointerEvent(PointerEventType type, PointerEventArgs^ args) -{ - std::lock_guard guard(mMutex); - std::shared_ptr e(new PointerEvent(type, args)); - mInputEvents.push(e); -} - void Direct3DInterop::OnCocos2dEditboxEvent(Object^ sender, Platform::String^ args, Windows::Foundation::EventHandler^ handler) { - std::lock_guard guard(mMutex); - std::shared_ptr e(new EditBoxEvent(sender, args, handler)); - mInputEvents.push(e); + std::shared_ptr e(new EditBoxEvent(sender, args, handler)); + cocos2d::GLView::sharedOpenGLView()->QueueEvent(e); } -void Direct3DInterop::ProcessEvents() -{ - std::lock_guard guard(mMutex); - while(!mInputEvents.empty()) - { - InputEvent* e = mInputEvents.front().get(); - e->execute(m_renderer); - mInputEvents.pop(); - } -} HRESULT Direct3DInterop::PrepareResources(_In_ const LARGE_INTEGER* presentTargetTime, _Inout_ DrawingSurfaceSizeF* desiredRenderTargetSize) @@ -176,7 +154,7 @@ HRESULT Direct3DInterop::Draw(_In_ ID3D11Device1* device, _In_ ID3D11DeviceConte } #endif // 0 - ProcessEvents(); + cocos2d::GLView::sharedOpenGLView()->ProcessEvents(); m_renderer->Render(); RequestAdditionalFrame(); return S_OK; diff --git a/cocos/2d/platform/wp8-xaml/cpp/Direct3DInterop.h b/cocos/2d/platform/wp8-xaml/cpp/Direct3DInterop.h index 142408c6e1..ca9b90f75c 100644 --- a/cocos/2d/platform/wp8-xaml/cpp/Direct3DInterop.h +++ b/cocos/2d/platform/wp8-xaml/cpp/Direct3DInterop.h @@ -39,7 +39,6 @@ namespace PhoneDirect3DXamlAppComponent public delegate void RequestAdditionalFrameHandler(); - [Windows::Foundation::Metadata::WebHostHidden] public ref class Direct3DInterop sealed : public Windows::Phone::Input::Interop::IDrawingSurfaceManipulationHandler { @@ -60,7 +59,7 @@ public: void OnBackKeyPress(); void OnCocos2dKeyEvent(Cocos2dKeyEvent key); void OnCocos2dKeyEvent(Cocos2dKeyEvent key, Platform::String^ text); - void OnCocos2dEditboxEvent(Object^ sender, Platform::String^ args, Windows::Foundation::EventHandler^ handler); + void OnCocos2dEditboxEvent(Platform::Object^ sender, Platform::String^ args, Windows::Foundation::EventHandler^ handler); property Windows::Graphics::Display::DisplayOrientations WindowOrientation; property Windows::Foundation::Size WindowBounds; @@ -84,13 +83,10 @@ internal: bool SendCocos2dEvent(Cocos2dEvent event); private: - void ProcessEvents(); - void AddPointerEvent(PointerEventType type, Windows::UI::Core::PointerEventArgs^ args); Cocos2dRenderer^ m_renderer; Windows::Graphics::Display::DisplayOrientations mCurrentOrientation; - std::queue> mInputEvents; - std::mutex mMutex; + std::mutex mRenderingMutex; Cocos2dEventDelegate^ m_delegate; diff --git a/cocos/2d/platform/wp8-xaml/cpp/EditBoxEvent.cpp b/cocos/2d/platform/wp8-xaml/cpp/EditBoxEvent.cpp index 6b7b1f6f70..992dfe2d60 100644 --- a/cocos/2d/platform/wp8-xaml/cpp/EditBoxEvent.cpp +++ b/cocos/2d/platform/wp8-xaml/cpp/EditBoxEvent.cpp @@ -36,7 +36,7 @@ namespace PhoneDirect3DXamlAppComponent } - void EditBoxEvent::execute( Cocos2dRenderer ^ renderer ) + void EditBoxEvent::execute() { if(m_handler.Get()) { diff --git a/cocos/2d/platform/wp8-xaml/cpp/EditBoxEvent.h b/cocos/2d/platform/wp8-xaml/cpp/EditBoxEvent.h index e00a51c0db..7206c6254b 100644 --- a/cocos/2d/platform/wp8-xaml/cpp/EditBoxEvent.h +++ b/cocos/2d/platform/wp8-xaml/cpp/EditBoxEvent.h @@ -25,18 +25,18 @@ THE SOFTWARE. #ifndef __EditBoxEVENT_H__ #define __EditBoxEVENT_H__ -#include "platform/wp8-xaml/cpp/InputEvent.h" +#include "InputEvent.h" #include namespace PhoneDirect3DXamlAppComponent { - class EditBoxEvent : public InputEvent + class EditBoxEvent : public cocos2d::InputEvent { public: EditBoxEvent(Platform::Object^ sender, Platform::String^ arg, Windows::Foundation::EventHandler^ handle); - virtual void execute(Cocos2dRenderer ^ renderer); + virtual void execute(); private: Platform::Agile m_sender; diff --git a/cocos/2d/platform/wp8/CCGLView.cpp b/cocos/2d/platform/wp8/CCGLView.cpp index 2872b69c96..82955d7b26 100644 --- a/cocos/2d/platform/wp8/CCGLView.cpp +++ b/cocos/2d/platform/wp8/CCGLView.cpp @@ -173,6 +173,14 @@ void GLView::OnResuming(Platform::Object^ sender, Platform::Object^ args) { } +// user pressed the Back Key on the phone +void GLView::OnBackKeyPress() +{ + if(m_delegate) + { + m_delegate->Invoke(Cocos2dEvent::TerminateApp); + } +} void GLView::OnPointerPressed(CoreWindow^ sender, PointerEventArgs^ args) { @@ -504,5 +512,36 @@ void GLView::setScissorInPoints(float x , float y , float w , float h) } } +void GLView::QueueBackKeyPress() +{ + std::lock_guard guard(mMutex); + std::shared_ptr e(new BackButtonEvent()); + mInputEvents.push(e); +} + +void GLView::QueuePointerEvent(PointerEventType type, PointerEventArgs^ args) +{ + std::lock_guard guard(mMutex); + std::shared_ptr e(new PointerEvent(type, args)); + mInputEvents.push(e); +} + +void GLView::QueueEvent(std::shared_ptr& event) +{ + std::lock_guard guard(mMutex); + mInputEvents.push(event); +} + +void GLView::ProcessEvents() +{ + std::lock_guard guard(mMutex); + + while (!mInputEvents.empty()) + { + InputEvent* e = mInputEvents.front().get(); + e->execute(); + mInputEvents.pop(); + } +} NS_CC_END diff --git a/cocos/2d/platform/wp8/CCGLView.h b/cocos/2d/platform/wp8/CCGLView.h index 701a7c2396..99dbca8274 100644 --- a/cocos/2d/platform/wp8/CCGLView.h +++ b/cocos/2d/platform/wp8/CCGLView.h @@ -30,15 +30,19 @@ THE SOFTWARE. #include "platform/CCCommon.h" #include "CCGeometry.h" #include "platform/CCGLViewProtocol.h" +#include "InputEvent.h" + + #include #include #include +#include +#include #include #include #include "kazmath/mat4.h" -#include "../wp8-xaml/cpp/InputEvent.h" #include @@ -83,7 +87,12 @@ public: void OnWindowClosed(Windows::UI::Core::CoreWindow^ sender, Windows::UI::Core::CoreWindowEventArgs^ args); void OnResuming(Platform::Object^ sender, Platform::Object^ args); void OnSuspending(Platform::Object^ sender, Windows::ApplicationModel::SuspendingEventArgs^ args); - + void OnBackKeyPress(); + + void QueueBackKeyPress(); + void QueuePointerEvent(PointerEventType type, Windows::UI::Core::PointerEventArgs^ args); + void GLView::QueueEvent(std::shared_ptr& event); + void SetXamlEventDelegate(PhoneDirect3DXamlAppComponent::Cocos2dEventDelegate^ delegate) { m_delegate = delegate; }; void SetXamlMessageBoxDelegate(PhoneDirect3DXamlAppComponent::Cocos2dMessageBoxDelegate^ delegate) { m_messageBoxDelegate = delegate; }; void SetXamlEditBoxDelegate(PhoneDirect3DXamlAppComponent::Cocos2dEditBoxDelegate^ delegate) { m_editBoxDelegate = delegate; }; @@ -108,6 +117,11 @@ public: */ static GLView* sharedOpenGLView(); + void ProcessEvents(); + void AddPointerEvent(PointerEventType type, Windows::UI::Core::PointerEventArgs^ args); + + + protected: GLView(); virtual ~GLView(); @@ -165,6 +179,10 @@ private: PhoneDirect3DXamlAppComponent::Cocos2dEventDelegate^ m_delegate; PhoneDirect3DXamlAppComponent::Cocos2dMessageBoxDelegate^ m_messageBoxDelegate; PhoneDirect3DXamlAppComponent::Cocos2dEditBoxDelegate^ m_editBoxDelegate; + + std::queue> mInputEvents; + std::mutex mMutex; + }; NS_CC_END