input events refactor

This commit is contained in:
Dale Stammen 2014-04-19 09:32:59 -07:00
parent 1a56f435fe
commit 12b01dcb25
8 changed files with 77 additions and 75 deletions

View File

@ -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() void Cocos2dRenderer::OnUpdateDevice()
{ {
GLView* glview = GLView::sharedOpenGLView(); GLView* glview = GLView::sharedOpenGLView();
@ -132,21 +123,6 @@ bool Cocos2dRenderer::OnRender()
return false; 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) void Cocos2dRenderer::OnKeyPressed(Platform::String^ text)
{ {
char szUtf8[8] = {0}; char szUtf8[8] = {0};
@ -154,7 +130,6 @@ void Cocos2dRenderer::OnKeyPressed(Platform::String^ text)
IMEDispatcher::sharedDispatcher()->dispatchInsertText(szUtf8, nLen); IMEDispatcher::sharedDispatcher()->dispatchInsertText(szUtf8, nLen);
} }
void Cocos2dRenderer::OnCocos2dKeyEvent(Cocos2dKeyEvent event) void Cocos2dRenderer::OnCocos2dKeyEvent(Cocos2dKeyEvent event)
{ {
switch(event) switch(event)

View File

@ -46,11 +46,7 @@ public:
void SetXamlMessageBoxDelegate(PhoneDirect3DXamlAppComponent::Cocos2dMessageBoxDelegate^ delegate); void SetXamlMessageBoxDelegate(PhoneDirect3DXamlAppComponent::Cocos2dMessageBoxDelegate^ delegate);
void SetXamlEditBoxDelegate(PhoneDirect3DXamlAppComponent::Cocos2dEditBoxDelegate^ 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(); Windows::Foundation::IAsyncAction^ OnSuspending();
void OnBackKeyPress();
void Connect(); void Connect();
void Disconnect(); void Disconnect();

View File

@ -25,6 +25,7 @@ THE SOFTWARE.
#include "Direct3DInterop.h" #include "Direct3DInterop.h"
#include "Direct3DContentProvider.h" #include "Direct3DContentProvider.h"
#include "EditBoxEvent.h" #include "EditBoxEvent.h"
#include "cocos2d.h"
using namespace Windows::Foundation; using namespace Windows::Foundation;
using namespace Windows::UI::Core; using namespace Windows::UI::Core;
@ -91,69 +92,46 @@ IAsyncAction^ Direct3DInterop::OnSuspending()
void Direct3DInterop::OnBackKeyPress() void Direct3DInterop::OnBackKeyPress()
{ {
std::lock_guard<std::mutex> guard(mMutex); cocos2d::GLView::sharedOpenGLView()->QueueBackKeyPress();
std::shared_ptr<BackButtonEvent> e(new BackButtonEvent());
mInputEvents.push(e);
} }
// Pointer Event Handlers. We need to queue up pointer events to pass them to the drawing thread // Pointer Event Handlers. We need to queue up pointer events to pass them to the drawing thread
void Direct3DInterop::OnPointerPressed(DrawingSurfaceManipulationHost^ sender, PointerEventArgs^ args) 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) 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) void Direct3DInterop::OnPointerReleased(DrawingSurfaceManipulationHost^ sender, PointerEventArgs^ args)
{ {
AddPointerEvent(PointerEventType::PointerReleased, args); cocos2d::GLView::sharedOpenGLView()->QueuePointerEvent(cocos2d::PointerEventType::PointerReleased, args);
} }
void Direct3DInterop::OnCocos2dKeyEvent(Cocos2dKeyEvent key) void Direct3DInterop::OnCocos2dKeyEvent(Cocos2dKeyEvent key)
{ {
std::lock_guard<std::mutex> guard(mMutex); std::shared_ptr<cocos2d::InputEvent> e(new cocos2d::KeyboardEvent(key));
std::shared_ptr<KeyboardEvent> e(new KeyboardEvent(key)); cocos2d::GLView::sharedOpenGLView()->QueueEvent(e);
mInputEvents.push(e);
} }
void Direct3DInterop::OnCocos2dKeyEvent(Cocos2dKeyEvent key, Platform::String^ text) void Direct3DInterop::OnCocos2dKeyEvent(Cocos2dKeyEvent key, Platform::String^ text)
{ {
std::lock_guard<std::mutex> guard(mMutex); std::shared_ptr<cocos2d::InputEvent> e(new cocos2d::KeyboardEvent(key,text));
std::shared_ptr<KeyboardEvent> e(new KeyboardEvent(key,text)); cocos2d::GLView::sharedOpenGLView()->QueueEvent(e);
mInputEvents.push(e);
} }
void Direct3DInterop::AddPointerEvent(PointerEventType type, PointerEventArgs^ args)
{
std::lock_guard<std::mutex> guard(mMutex);
std::shared_ptr<PointerEvent> e(new PointerEvent(type, args));
mInputEvents.push(e);
}
void Direct3DInterop::OnCocos2dEditboxEvent(Object^ sender, Platform::String^ args, Windows::Foundation::EventHandler<Platform::String^>^ handler) void Direct3DInterop::OnCocos2dEditboxEvent(Object^ sender, Platform::String^ args, Windows::Foundation::EventHandler<Platform::String^>^ handler)
{ {
std::lock_guard<std::mutex> guard(mMutex); std::shared_ptr<cocos2d::InputEvent> e(new EditBoxEvent(sender, args, handler));
std::shared_ptr<EditBoxEvent> e(new EditBoxEvent(sender, args, handler)); cocos2d::GLView::sharedOpenGLView()->QueueEvent(e);
mInputEvents.push(e);
} }
void Direct3DInterop::ProcessEvents()
{
std::lock_guard<std::mutex> 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) 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 #endif // 0
ProcessEvents(); cocos2d::GLView::sharedOpenGLView()->ProcessEvents();
m_renderer->Render(); m_renderer->Render();
RequestAdditionalFrame(); RequestAdditionalFrame();
return S_OK; return S_OK;

View File

@ -39,7 +39,6 @@ namespace PhoneDirect3DXamlAppComponent
public delegate void RequestAdditionalFrameHandler(); public delegate void RequestAdditionalFrameHandler();
[Windows::Foundation::Metadata::WebHostHidden] [Windows::Foundation::Metadata::WebHostHidden]
public ref class Direct3DInterop sealed : public Windows::Phone::Input::Interop::IDrawingSurfaceManipulationHandler public ref class Direct3DInterop sealed : public Windows::Phone::Input::Interop::IDrawingSurfaceManipulationHandler
{ {
@ -60,7 +59,7 @@ public:
void OnBackKeyPress(); void OnBackKeyPress();
void OnCocos2dKeyEvent(Cocos2dKeyEvent key); void OnCocos2dKeyEvent(Cocos2dKeyEvent key);
void OnCocos2dKeyEvent(Cocos2dKeyEvent key, Platform::String^ text); void OnCocos2dKeyEvent(Cocos2dKeyEvent key, Platform::String^ text);
void OnCocos2dEditboxEvent(Object^ sender, Platform::String^ args, Windows::Foundation::EventHandler<Platform::String^>^ handler); void OnCocos2dEditboxEvent(Platform::Object^ sender, Platform::String^ args, Windows::Foundation::EventHandler<Platform::String^>^ handler);
property Windows::Graphics::Display::DisplayOrientations WindowOrientation; property Windows::Graphics::Display::DisplayOrientations WindowOrientation;
property Windows::Foundation::Size WindowBounds; property Windows::Foundation::Size WindowBounds;
@ -84,13 +83,10 @@ internal:
bool SendCocos2dEvent(Cocos2dEvent event); bool SendCocos2dEvent(Cocos2dEvent event);
private: private:
void ProcessEvents();
void AddPointerEvent(PointerEventType type, Windows::UI::Core::PointerEventArgs^ args);
Cocos2dRenderer^ m_renderer; Cocos2dRenderer^ m_renderer;
Windows::Graphics::Display::DisplayOrientations mCurrentOrientation; Windows::Graphics::Display::DisplayOrientations mCurrentOrientation;
std::queue<std::shared_ptr<InputEvent>> mInputEvents;
std::mutex mMutex;
std::mutex mRenderingMutex; std::mutex mRenderingMutex;
Cocos2dEventDelegate^ m_delegate; Cocos2dEventDelegate^ m_delegate;

View File

@ -36,7 +36,7 @@ namespace PhoneDirect3DXamlAppComponent
} }
void EditBoxEvent::execute( Cocos2dRenderer ^ renderer ) void EditBoxEvent::execute()
{ {
if(m_handler.Get()) if(m_handler.Get())
{ {

View File

@ -25,18 +25,18 @@ THE SOFTWARE.
#ifndef __EditBoxEVENT_H__ #ifndef __EditBoxEVENT_H__
#define __EditBoxEVENT_H__ #define __EditBoxEVENT_H__
#include "platform/wp8-xaml/cpp/InputEvent.h" #include "InputEvent.h"
#include <agile.h> #include <agile.h>
namespace PhoneDirect3DXamlAppComponent namespace PhoneDirect3DXamlAppComponent
{ {
class EditBoxEvent : public InputEvent class EditBoxEvent : public cocos2d::InputEvent
{ {
public: public:
EditBoxEvent(Platform::Object^ sender, Platform::String^ arg, Windows::Foundation::EventHandler<Platform::String^>^ handle); EditBoxEvent(Platform::Object^ sender, Platform::String^ arg, Windows::Foundation::EventHandler<Platform::String^>^ handle);
virtual void execute(Cocos2dRenderer ^ renderer); virtual void execute();
private: private:
Platform::Agile<Platform::Object^> m_sender; Platform::Agile<Platform::Object^> m_sender;

View File

@ -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) 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<std::mutex> guard(mMutex);
std::shared_ptr<BackButtonEvent> e(new BackButtonEvent());
mInputEvents.push(e);
}
void GLView::QueuePointerEvent(PointerEventType type, PointerEventArgs^ args)
{
std::lock_guard<std::mutex> guard(mMutex);
std::shared_ptr<PointerEvent> e(new PointerEvent(type, args));
mInputEvents.push(e);
}
void GLView::QueueEvent(std::shared_ptr<InputEvent>& event)
{
std::lock_guard<std::mutex> guard(mMutex);
mInputEvents.push(event);
}
void GLView::ProcessEvents()
{
std::lock_guard<std::mutex> guard(mMutex);
while (!mInputEvents.empty())
{
InputEvent* e = mInputEvents.front().get();
e->execute();
mInputEvents.pop();
}
}
NS_CC_END NS_CC_END

View File

@ -30,15 +30,19 @@ THE SOFTWARE.
#include "platform/CCCommon.h" #include "platform/CCCommon.h"
#include "CCGeometry.h" #include "CCGeometry.h"
#include "platform/CCGLViewProtocol.h" #include "platform/CCGLViewProtocol.h"
#include "InputEvent.h"
#include <agile.h> #include <agile.h>
#include <wrl/client.h> #include <wrl/client.h>
#include <d3d11_1.h> #include <d3d11_1.h>
#include <mutex>
#include <queue>
#include <agile.h> #include <agile.h>
#include <DirectXMath.h> #include <DirectXMath.h>
#include "kazmath/mat4.h" #include "kazmath/mat4.h"
#include "../wp8-xaml/cpp/InputEvent.h"
#include <EGL/egl.h> #include <EGL/egl.h>
@ -83,7 +87,12 @@ public:
void OnWindowClosed(Windows::UI::Core::CoreWindow^ sender, Windows::UI::Core::CoreWindowEventArgs^ args); void OnWindowClosed(Windows::UI::Core::CoreWindow^ sender, Windows::UI::Core::CoreWindowEventArgs^ args);
void OnResuming(Platform::Object^ sender, Platform::Object^ args); void OnResuming(Platform::Object^ sender, Platform::Object^ args);
void OnSuspending(Platform::Object^ sender, Windows::ApplicationModel::SuspendingEventArgs^ 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<InputEvent>& event);
void SetXamlEventDelegate(PhoneDirect3DXamlAppComponent::Cocos2dEventDelegate^ delegate) { m_delegate = delegate; }; void SetXamlEventDelegate(PhoneDirect3DXamlAppComponent::Cocos2dEventDelegate^ delegate) { m_delegate = delegate; };
void SetXamlMessageBoxDelegate(PhoneDirect3DXamlAppComponent::Cocos2dMessageBoxDelegate^ delegate) { m_messageBoxDelegate = delegate; }; void SetXamlMessageBoxDelegate(PhoneDirect3DXamlAppComponent::Cocos2dMessageBoxDelegate^ delegate) { m_messageBoxDelegate = delegate; };
void SetXamlEditBoxDelegate(PhoneDirect3DXamlAppComponent::Cocos2dEditBoxDelegate^ delegate) { m_editBoxDelegate = delegate; }; void SetXamlEditBoxDelegate(PhoneDirect3DXamlAppComponent::Cocos2dEditBoxDelegate^ delegate) { m_editBoxDelegate = delegate; };
@ -108,6 +117,11 @@ public:
*/ */
static GLView* sharedOpenGLView(); static GLView* sharedOpenGLView();
void ProcessEvents();
void AddPointerEvent(PointerEventType type, Windows::UI::Core::PointerEventArgs^ args);
protected: protected:
GLView(); GLView();
virtual ~GLView(); virtual ~GLView();
@ -165,6 +179,10 @@ private:
PhoneDirect3DXamlAppComponent::Cocos2dEventDelegate^ m_delegate; PhoneDirect3DXamlAppComponent::Cocos2dEventDelegate^ m_delegate;
PhoneDirect3DXamlAppComponent::Cocos2dMessageBoxDelegate^ m_messageBoxDelegate; PhoneDirect3DXamlAppComponent::Cocos2dMessageBoxDelegate^ m_messageBoxDelegate;
PhoneDirect3DXamlAppComponent::Cocos2dEditBoxDelegate^ m_editBoxDelegate; PhoneDirect3DXamlAppComponent::Cocos2dEditBoxDelegate^ m_editBoxDelegate;
std::queue<std::shared_ptr<InputEvent>> mInputEvents;
std::mutex mMutex;
}; };
NS_CC_END NS_CC_END