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()
{
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)

View File

@ -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();

View File

@ -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<std::mutex> guard(mMutex);
std::shared_ptr<BackButtonEvent> 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<std::mutex> guard(mMutex);
std::shared_ptr<KeyboardEvent> e(new KeyboardEvent(key));
mInputEvents.push(e);
std::shared_ptr<cocos2d::InputEvent> e(new cocos2d::KeyboardEvent(key));
cocos2d::GLView::sharedOpenGLView()->QueueEvent(e);
}
void Direct3DInterop::OnCocos2dKeyEvent(Cocos2dKeyEvent key, Platform::String^ text)
{
std::lock_guard<std::mutex> guard(mMutex);
std::shared_ptr<KeyboardEvent> e(new KeyboardEvent(key,text));
mInputEvents.push(e);
std::shared_ptr<cocos2d::InputEvent> e(new cocos2d::KeyboardEvent(key,text));
cocos2d::GLView::sharedOpenGLView()->QueueEvent(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)
{
std::lock_guard<std::mutex> guard(mMutex);
std::shared_ptr<EditBoxEvent> e(new EditBoxEvent(sender, args, handler));
mInputEvents.push(e);
std::shared_ptr<cocos2d::InputEvent> e(new EditBoxEvent(sender, args, handler));
cocos2d::GLView::sharedOpenGLView()->QueueEvent(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)
@ -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;

View File

@ -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<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::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<std::shared_ptr<InputEvent>> mInputEvents;
std::mutex mMutex;
std::mutex mRenderingMutex;
Cocos2dEventDelegate^ m_delegate;

View File

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

View File

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

View File

@ -30,15 +30,19 @@ THE SOFTWARE.
#include "platform/CCCommon.h"
#include "CCGeometry.h"
#include "platform/CCGLViewProtocol.h"
#include "InputEvent.h"
#include <agile.h>
#include <wrl/client.h>
#include <d3d11_1.h>
#include <mutex>
#include <queue>
#include <agile.h>
#include <DirectXMath.h>
#include "kazmath/mat4.h"
#include "../wp8-xaml/cpp/InputEvent.h"
#include <EGL/egl.h>
@ -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<InputEvent>& 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<std::shared_ptr<InputEvent>> mInputEvents;
std::mutex mMutex;
};
NS_CC_END