From 7a2a44ad8838ebb39a8a7684e3a1778e66ab1f52 Mon Sep 17 00:00:00 2001 From: huanghsiwu <375102906@qq.com> Date: Fri, 26 Sep 2014 16:16:34 +0800 Subject: [PATCH] Enable screen orientation change handling - by Teivaz --- cocos/platform/winrt/InputEvent.cpp | 10 ++++ cocos/platform/winrt/InputEvent.h | 9 ++++ .../platform/wp8-xaml/cpp/Direct3DInterop.cpp | 16 +++--- cocos/platform/wp8-xaml/cpp/Direct3DInterop.h | 1 + cocos/platform/wp8-xaml/xaml/MainPage.xaml.cs | 51 +++++++++++-------- .../proj.wp8-xaml/App/MainPage.xaml.cs | 51 +++++++++++-------- .../AppComponent/src/Direct3DInterop.cpp | 20 ++++---- .../AppComponent/src/Direct3DInterop.h | 1 + 8 files changed, 101 insertions(+), 58 deletions(-) diff --git a/cocos/platform/winrt/InputEvent.cpp b/cocos/platform/winrt/InputEvent.cpp index cfa1b4b6d6..2389dbd28d 100644 --- a/cocos/platform/winrt/InputEvent.cpp +++ b/cocos/platform/winrt/InputEvent.cpp @@ -123,6 +123,16 @@ void BackButtonEvent::execute() GLViewImpl::sharedOpenGLView()->OnBackKeyPress(); } +CustomInputEvent::CustomInputEvent(const std::function& fun) +: m_fun(fun) +{ +} +void CustomInputEvent::execute() +{ + m_fun(); +} + + NS_CC_END diff --git a/cocos/platform/winrt/InputEvent.h b/cocos/platform/winrt/InputEvent.h index 5c2dd39f2a..f8cbcdf664 100644 --- a/cocos/platform/winrt/InputEvent.h +++ b/cocos/platform/winrt/InputEvent.h @@ -99,6 +99,15 @@ public: virtual void execute(); }; +class CustomInputEvent : public InputEvent +{ +public: + CustomInputEvent(const std::function&); + virtual void execute(); +private: + std::function m_fun; +}; + NS_CC_END #endif // #ifndef __INPUT_EVENT__ diff --git a/cocos/platform/wp8-xaml/cpp/Direct3DInterop.cpp b/cocos/platform/wp8-xaml/cpp/Direct3DInterop.cpp index daf87f8f2d..e43b246088 100644 --- a/cocos/platform/wp8-xaml/cpp/Direct3DInterop.cpp +++ b/cocos/platform/wp8-xaml/cpp/Direct3DInterop.cpp @@ -109,6 +109,15 @@ void Direct3DInterop::OnPointerReleased(DrawingSurfaceManipulationHost^ sender, cocos2d::GLViewImpl::sharedOpenGLView()->QueuePointerEvent(cocos2d::PointerEventType::PointerReleased, args); } +void Direct3DInterop::OnOrientationChanged(Windows::Graphics::Display::DisplayOrientations orientation) +{ + std::shared_ptr e(new cocos2d::CustomInputEvent([this, orientation]() + { + m_renderer->OnOrientationChanged(orientation); + })); + cocos2d::GLViewImpl::sharedOpenGLView()->QueueEvent(e); +} + void Direct3DInterop::OnCocos2dKeyEvent(Cocos2dKeyEvent key) { std::shared_ptr e(new cocos2d::KeyboardEvent(key)); @@ -147,13 +156,6 @@ HRESULT Direct3DInterop::PrepareResources(_In_ const LARGE_INTEGER* presentTarge HRESULT Direct3DInterop::Draw(_In_ ID3D11Device1* device, _In_ ID3D11DeviceContext1* context, _In_ ID3D11RenderTargetView* renderTargetView) { m_renderer->UpdateDevice(device, context, renderTargetView); -#if 0 - if(mCurrentOrientation != WindowOrientation) - { - mCurrentOrientation = WindowOrientation; - m_renderer->OnOrientationChanged(mCurrentOrientation); - } -#endif // 0 cocos2d::GLViewImpl::sharedOpenGLView()->ProcessEvents(); m_renderer->Render(); diff --git a/cocos/platform/wp8-xaml/cpp/Direct3DInterop.h b/cocos/platform/wp8-xaml/cpp/Direct3DInterop.h index 0c8ef6fcab..6324dca1e9 100644 --- a/cocos/platform/wp8-xaml/cpp/Direct3DInterop.h +++ b/cocos/platform/wp8-xaml/cpp/Direct3DInterop.h @@ -60,6 +60,7 @@ public: void OnCocos2dKeyEvent(Cocos2dKeyEvent key); void OnCocos2dKeyEvent(Cocos2dKeyEvent key, Platform::String^ text); void OnCocos2dEditboxEvent(Platform::Object^ sender, Platform::String^ args, Windows::Foundation::EventHandler^ handler); + void OnOrientationChanged(Windows::Graphics::Display::DisplayOrientations orientation); void OnCocos2dOpenURL(Platform::String^ url); property Windows::Graphics::Display::DisplayOrientations WindowOrientation; diff --git a/cocos/platform/wp8-xaml/xaml/MainPage.xaml.cs b/cocos/platform/wp8-xaml/xaml/MainPage.xaml.cs index 56740b260c..2a85cc1144 100644 --- a/cocos/platform/wp8-xaml/xaml/MainPage.xaml.cs +++ b/cocos/platform/wp8-xaml/xaml/MainPage.xaml.cs @@ -51,33 +51,42 @@ namespace PhoneDirect3DXamlAppInterop #endif } + override protected void OnOrientationChanged(OrientationChangedEventArgs args) + { + base.OnOrientationChanged(args); + if (m_d3dInterop != null) + { + DisplayOrientations orientation = ConvertToNativeOrientation(args.Orientation); + m_d3dInterop.OnOrientationChanged(orientation); + } + } + + private static DisplayOrientations ConvertToNativeOrientation(PageOrientation xamlOrientation) + { + switch (xamlOrientation) + { + case PageOrientation.Portrait: + case PageOrientation.PortraitUp: + return DisplayOrientations.Portrait; + case PageOrientation.PortraitDown: + return DisplayOrientations.PortraitFlipped; + case PageOrientation.Landscape: + case PageOrientation.LandscapeLeft: + return DisplayOrientations.Landscape; + case PageOrientation.LandscapeRight: + return DisplayOrientations.LandscapeFlipped; + default: + return DisplayOrientations.Landscape; + } + } + private void DrawingSurfaceBackground_Loaded(object sender, RoutedEventArgs e) { if (m_d3dInterop == null) { PageOrientation pageOrientation = (PageOrientation)GetValue(OrientationProperty); - DisplayOrientations displayOrientation; + DisplayOrientations displayOrientation = ConvertToNativeOrientation(pageOrientation); - switch(pageOrientation) - { - case PageOrientation.Portrait: - case PageOrientation.PortraitUp: - displayOrientation = DisplayOrientations.Portrait; - break; - case PageOrientation.PortraitDown: - displayOrientation = DisplayOrientations.PortraitFlipped; - break; - case PageOrientation.Landscape: - case PageOrientation.LandscapeLeft: - displayOrientation = DisplayOrientations.Landscape; - break; - case PageOrientation.LandscapeRight: - displayOrientation = DisplayOrientations.LandscapeFlipped; - break; - default: - displayOrientation = DisplayOrientations.Landscape; - break; - } m_d3dInterop = new Direct3DInterop(displayOrientation); // Set WindowBounds to size of DrawingSurface diff --git a/templates/cpp-template-default/proj.wp8-xaml/App/MainPage.xaml.cs b/templates/cpp-template-default/proj.wp8-xaml/App/MainPage.xaml.cs index 56740b260c..2a85cc1144 100644 --- a/templates/cpp-template-default/proj.wp8-xaml/App/MainPage.xaml.cs +++ b/templates/cpp-template-default/proj.wp8-xaml/App/MainPage.xaml.cs @@ -51,33 +51,42 @@ namespace PhoneDirect3DXamlAppInterop #endif } + override protected void OnOrientationChanged(OrientationChangedEventArgs args) + { + base.OnOrientationChanged(args); + if (m_d3dInterop != null) + { + DisplayOrientations orientation = ConvertToNativeOrientation(args.Orientation); + m_d3dInterop.OnOrientationChanged(orientation); + } + } + + private static DisplayOrientations ConvertToNativeOrientation(PageOrientation xamlOrientation) + { + switch (xamlOrientation) + { + case PageOrientation.Portrait: + case PageOrientation.PortraitUp: + return DisplayOrientations.Portrait; + case PageOrientation.PortraitDown: + return DisplayOrientations.PortraitFlipped; + case PageOrientation.Landscape: + case PageOrientation.LandscapeLeft: + return DisplayOrientations.Landscape; + case PageOrientation.LandscapeRight: + return DisplayOrientations.LandscapeFlipped; + default: + return DisplayOrientations.Landscape; + } + } + private void DrawingSurfaceBackground_Loaded(object sender, RoutedEventArgs e) { if (m_d3dInterop == null) { PageOrientation pageOrientation = (PageOrientation)GetValue(OrientationProperty); - DisplayOrientations displayOrientation; + DisplayOrientations displayOrientation = ConvertToNativeOrientation(pageOrientation); - switch(pageOrientation) - { - case PageOrientation.Portrait: - case PageOrientation.PortraitUp: - displayOrientation = DisplayOrientations.Portrait; - break; - case PageOrientation.PortraitDown: - displayOrientation = DisplayOrientations.PortraitFlipped; - break; - case PageOrientation.Landscape: - case PageOrientation.LandscapeLeft: - displayOrientation = DisplayOrientations.Landscape; - break; - case PageOrientation.LandscapeRight: - displayOrientation = DisplayOrientations.LandscapeFlipped; - break; - default: - displayOrientation = DisplayOrientations.Landscape; - break; - } m_d3dInterop = new Direct3DInterop(displayOrientation); // Set WindowBounds to size of DrawingSurface diff --git a/templates/cpp-template-default/proj.wp8-xaml/AppComponent/src/Direct3DInterop.cpp b/templates/cpp-template-default/proj.wp8-xaml/AppComponent/src/Direct3DInterop.cpp index 421b71db67..e43b246088 100644 --- a/templates/cpp-template-default/proj.wp8-xaml/AppComponent/src/Direct3DInterop.cpp +++ b/templates/cpp-template-default/proj.wp8-xaml/AppComponent/src/Direct3DInterop.cpp @@ -109,6 +109,15 @@ void Direct3DInterop::OnPointerReleased(DrawingSurfaceManipulationHost^ sender, cocos2d::GLViewImpl::sharedOpenGLView()->QueuePointerEvent(cocos2d::PointerEventType::PointerReleased, args); } +void Direct3DInterop::OnOrientationChanged(Windows::Graphics::Display::DisplayOrientations orientation) +{ + std::shared_ptr e(new cocos2d::CustomInputEvent([this, orientation]() + { + m_renderer->OnOrientationChanged(orientation); + })); + cocos2d::GLViewImpl::sharedOpenGLView()->QueueEvent(e); +} + void Direct3DInterop::OnCocos2dKeyEvent(Cocos2dKeyEvent key) { std::shared_ptr e(new cocos2d::KeyboardEvent(key)); @@ -147,13 +156,6 @@ HRESULT Direct3DInterop::PrepareResources(_In_ const LARGE_INTEGER* presentTarge HRESULT Direct3DInterop::Draw(_In_ ID3D11Device1* device, _In_ ID3D11DeviceContext1* context, _In_ ID3D11RenderTargetView* renderTargetView) { m_renderer->UpdateDevice(device, context, renderTargetView); -#if 0 - if(mCurrentOrientation != WindowOrientation) - { - mCurrentOrientation = WindowOrientation; - m_renderer->OnOrientationChanged(mCurrentOrientation); - } -#endif // 0 cocos2d::GLViewImpl::sharedOpenGLView()->ProcessEvents(); m_renderer->Render(); @@ -188,10 +190,10 @@ void Direct3DInterop::SetCocos2dOpenURLDelegate(Cocos2dOpenURLDelegate ^ delegat bool Direct3DInterop::SendCocos2dEvent(Cocos2dEvent event) { - std::string str; + Platform::String^ str; if(m_delegate) { - m_delegate->Invoke(event, stringToPlatformString(str)); + m_delegate->Invoke(event, str); return true; } return false; diff --git a/templates/cpp-template-default/proj.wp8-xaml/AppComponent/src/Direct3DInterop.h b/templates/cpp-template-default/proj.wp8-xaml/AppComponent/src/Direct3DInterop.h index 0c8ef6fcab..6324dca1e9 100644 --- a/templates/cpp-template-default/proj.wp8-xaml/AppComponent/src/Direct3DInterop.h +++ b/templates/cpp-template-default/proj.wp8-xaml/AppComponent/src/Direct3DInterop.h @@ -60,6 +60,7 @@ public: void OnCocos2dKeyEvent(Cocos2dKeyEvent key); void OnCocos2dKeyEvent(Cocos2dKeyEvent key, Platform::String^ text); void OnCocos2dEditboxEvent(Platform::Object^ sender, Platform::String^ args, Windows::Foundation::EventHandler^ handler); + void OnOrientationChanged(Windows::Graphics::Display::DisplayOrientations orientation); void OnCocos2dOpenURL(Platform::String^ url); property Windows::Graphics::Display::DisplayOrientations WindowOrientation;