From b20015f845dac03e223ce85489aff8a8c5e06e0f Mon Sep 17 00:00:00 2001 From: Dale Stammen Date: Thu, 23 Oct 2014 07:07:58 -0700 Subject: [PATCH] updated for Universal App support --- cocos/platform/winrt/CCGLViewImpl-winrt.cpp | 189 +++++++------------- cocos/platform/winrt/CCGLViewImpl-winrt.h | 48 ++--- 2 files changed, 79 insertions(+), 158 deletions(-) diff --git a/cocos/platform/winrt/CCGLViewImpl-winrt.cpp b/cocos/platform/winrt/CCGLViewImpl-winrt.cpp index d5ecc8e138..4526c07091 100644 --- a/cocos/platform/winrt/CCGLViewImpl-winrt.cpp +++ b/cocos/platform/winrt/CCGLViewImpl-winrt.cpp @@ -24,7 +24,6 @@ THE SOFTWARE. ****************************************************************************/ #include "CCGLViewImpl-winrt.h" -#include "deprecated/CCSet.h" #include "base/ccMacros.h" #include "base/CCDirector.h" #include "base/CCTouch.h" @@ -40,7 +39,9 @@ using namespace Windows::Graphics::Display; using namespace Windows::UI::Input; using namespace Windows::UI::Core; using namespace Windows::UI::Xaml; +using namespace Windows::UI::Xaml::Controls; using namespace Windows::UI::Xaml::Media; +using namespace Windows::UI::Xaml::Input; using namespace Windows::System; using namespace Windows::UI::ViewManagement; using namespace Windows::ApplicationModel; @@ -48,7 +49,6 @@ using namespace Windows::ApplicationModel::Core; using namespace Windows::ApplicationModel::Activation; using namespace Platform; using namespace Microsoft::WRL; -using namespace PhoneDirect3DXamlAppComponent; NS_CC_BEGIN @@ -79,8 +79,6 @@ GLViewImpl::GLViewImpl() , m_windowVisible(true) , m_width(0) , m_height(0) - , m_delegate(nullptr) - , m_messageBoxDelegate(nullptr) , m_orientation(DisplayOrientations::Landscape) { s_pEglView = this; @@ -116,6 +114,16 @@ bool GLViewImpl::Create(float width, float height, DisplayOrientations orientati return true; } +void GLViewImpl::setDispatcher(Windows::UI::Core::CoreDispatcher^ dispatcher) +{ + m_dispatcher = dispatcher; +} + +void GLViewImpl::setPanel(Windows::UI::Xaml::Controls::Panel^ panel) +{ + m_panel = panel; +} + void GLViewImpl::setIMEKeyboardState(bool bOpen) @@ -124,44 +132,55 @@ void GLViewImpl::setIMEKeyboardState(bool bOpen) setIMEKeyboardState(bOpen, str); } +bool GLViewImpl::ShowMessageBox(Platform::String^ title, Platform::String^ message) +{ + if (m_dispatcher.Get()) + { + m_dispatcher.Get()->RunAsync(Windows::UI::Core::CoreDispatcherPriority::Normal, ref new Windows::UI::Core::DispatchedHandler([title, message]() + { + // Show the message dialog + auto msg = ref new Windows::UI::Popups::MessageDialog(message, title); + // Set the command to be invoked when a user presses 'ESC' + msg->CancelCommandIndex = 1; + msg->ShowAsync(); + })); + + return true; + } + return false; +} + void GLViewImpl::setIMEKeyboardState(bool bOpen, std::string str) { - if(m_delegate) + if(bOpen) { - if(bOpen) + if (m_keyboard == nullptr) { - m_delegate->Invoke(Cocos2dEvent::ShowKeyboard, stringToPlatformString(str)); + m_keyboard = ref new KeyBoardWinRT(m_dispatcher.Get(), m_panel.Get()); } - else + m_keyboard->ShowKeyboard(PlatformStringFromString(str)); + } + else + { + if (m_keyboard != nullptr) { - m_delegate->Invoke(Cocos2dEvent::HideKeyboard, stringToPlatformString(str)); + m_keyboard->HideKeyboard(PlatformStringFromString(str)); } + m_keyboard = nullptr; } } -Platform::String^ GLViewImpl::stringToPlatformString(std::string strSrc) -{ - // to wide char - int strLen = MultiByteToWideChar(CP_UTF8, 0, strSrc.c_str(), -1, NULL, 0); - wchar_t* wstr = new wchar_t[strLen + 1]; - memset(wstr, 0, strLen + 1); - MultiByteToWideChar(CP_UTF8, 0, strSrc.c_str(), -1, wstr, strLen); - Platform::String^ strDst = ref new Platform::String(wstr); - delete[] wstr; - return strDst; -} + void GLViewImpl::swapBuffers() { - //eglSwapBuffers(m_eglDisplay, m_eglSurface); + } bool GLViewImpl::isOpenGLReady() { return true; - // TODO: need to revisit this - //return (m_eglDisplay && m_orientation != DisplayOrientations::None); } void GLViewImpl::end() @@ -184,7 +203,7 @@ void GLViewImpl::OnBackKeyPress() std::string str; if(m_delegate) { - m_delegate->Invoke(Cocos2dEvent::TerminateApp, stringToPlatformString(str)); + //m_delegate->Invoke(Cocos2dEvent::TerminateApp, stringToPlatformString(str)); } } @@ -308,30 +327,6 @@ void GLViewImpl::OnRendering() } } - - -bool GLViewImpl::ShowMessageBox(Platform::String^ title, Platform::String^ message) -{ - if(m_messageBoxDelegate) - { - m_messageBoxDelegate->Invoke(title, message); - return true; - } - return false; -} - -bool GLViewImpl::OpenXamlEditBox(Platform::String^ strPlaceHolder, Platform::String^ strText, int maxLength, int inputMode, int inputFlag, Windows::Foundation::EventHandler^ receiveHandler) -{ - if(m_editBoxDelegate) - { - m_editBoxDelegate->Invoke(strPlaceHolder, strText, maxLength, inputMode, inputFlag, receiveHandler); - return true; - } - return false; -} - - - // called by orientation change from WP8 XAML void GLViewImpl::UpdateOrientation(DisplayOrientations orientation) { @@ -345,9 +340,12 @@ void GLViewImpl::UpdateOrientation(DisplayOrientations orientation) // called by size change from WP8 XAML void GLViewImpl::UpdateForWindowSizeChange(float width, float height) { - m_width = width; - m_height = height; - UpdateWindowSize(); + if (width != m_width || height != m_height) + { + m_width = width; + m_height = height; + UpdateWindowSize(); + } } #if 0 @@ -371,18 +369,9 @@ void GLViewImpl::UpdateWindowSize() { float width, height; - if(m_orientation == DisplayOrientations::Landscape || m_orientation == DisplayOrientations::LandscapeFlipped) - { - width = m_height; - height = m_width; - } - else - { - width = m_width; - height = m_height; - } + width = m_width; + height = m_height; - UpdateOrientationMatrix(); //CCSize designSize = getDesignResolutionSize(); if(!m_initialized) @@ -403,45 +392,15 @@ void GLViewImpl::UpdateWindowSize() } } -const Mat4& GLViewImpl::getOrientationMatrix() const -{ - return m_orientationMatrix; -}; - - -void GLViewImpl::UpdateOrientationMatrix() -{ - kmMat4Identity(&m_orientationMatrix); - kmMat4Identity(&m_reverseOrientationMatrix); - switch(m_orientation) - { - case Windows::Graphics::Display::DisplayOrientations::PortraitFlipped: - kmMat4RotationZ(&m_orientationMatrix, static_cast(M_PI)); - kmMat4RotationZ(&m_reverseOrientationMatrix, static_cast(-M_PI)); - break; - - case Windows::Graphics::Display::DisplayOrientations::Landscape: - kmMat4RotationZ(&m_orientationMatrix, static_cast(-M_PI_2)); - kmMat4RotationZ(&m_reverseOrientationMatrix, static_cast(M_PI_2)); - break; - - case Windows::Graphics::Display::DisplayOrientations::LandscapeFlipped: - kmMat4RotationZ(&m_orientationMatrix, static_cast(M_PI_2)); - kmMat4RotationZ(&m_reverseOrientationMatrix, static_cast(-M_PI_2)); - break; - - default: - break; - } -} - cocos2d::Vec2 GLViewImpl::TransformToOrientation(Windows::Foundation::Point p) { cocos2d::Vec2 returnValue; float x = p.X; float y = p.Y; + returnValue = Vec2(x, y); +#if 0 switch (m_orientation) { case DisplayOrientations::Portrait: @@ -458,6 +417,7 @@ cocos2d::Vec2 GLViewImpl::TransformToOrientation(Windows::Foundation::Point p) returnValue = Vec2(m_height - y, x); break; } +#endif float zoomFactor = GLViewImpl::sharedOpenGLView()->getFrameZoomFactor(); if(zoomFactor > 0.0f) { @@ -473,48 +433,23 @@ cocos2d::Vec2 GLViewImpl::TransformToOrientation(Windows::Foundation::Point p) Vec2 GLViewImpl::GetPoint(PointerEventArgs^ args) { return TransformToOrientation(args->CurrentPoint->Position); - } void GLViewImpl::setViewPortInPoints(float x , float y , float w , float h) { - switch(m_orientation) - { - case DisplayOrientations::Landscape: - case DisplayOrientations::LandscapeFlipped: - glViewport((GLint)(y * _scaleY + _viewPortRect.origin.y), - (GLint)(x * _scaleX + _viewPortRect.origin.x), - (GLsizei)(h * _scaleY), - (GLsizei)(w * _scaleX)); - break; - - default: - glViewport((GLint)(x * _scaleX + _viewPortRect.origin.x), - (GLint)(y * _scaleY + _viewPortRect.origin.y), - (GLsizei)(w * _scaleX), - (GLsizei)(h * _scaleY)); - } + glViewport((GLint) (x * _scaleX + _viewPortRect.origin.x), + (GLint) (y * _scaleY + _viewPortRect.origin.y), + (GLsizei) (w * _scaleX), + (GLsizei) (h * _scaleY)); } void GLViewImpl::setScissorInPoints(float x , float y , float w , float h) { - switch(m_orientation) - { - case DisplayOrientations::Landscape: - case DisplayOrientations::LandscapeFlipped: - glScissor((GLint)(y * _scaleX + _viewPortRect.origin.y), - (GLint)((_viewPortRect.size.width - ((x + w) * _scaleX)) + _viewPortRect.origin.x), - (GLsizei)(h * _scaleY), - (GLsizei)(w * _scaleX)); - break; - - default: - glScissor((GLint)(x * _scaleX + _viewPortRect.origin.x), - (GLint)(y * _scaleY + _viewPortRect.origin.y), - (GLsizei)(w * _scaleX), - (GLsizei)(h * _scaleY)); - } + glScissor((GLint) (x * _scaleX + _viewPortRect.origin.x), + (GLint) (y * _scaleY + _viewPortRect.origin.y), + (GLsizei) (w * _scaleX), + (GLsizei) (h * _scaleY)); } void GLViewImpl::QueueBackKeyPress() diff --git a/cocos/platform/winrt/CCGLViewImpl-winrt.h b/cocos/platform/winrt/CCGLViewImpl-winrt.h index e30672fb2a..33eac1cfbe 100644 --- a/cocos/platform/winrt/CCGLViewImpl-winrt.h +++ b/cocos/platform/winrt/CCGLViewImpl-winrt.h @@ -33,21 +33,16 @@ THE SOFTWARE. #include - +#include #include -#include #include #include - -#include -#include - +#include NS_CC_BEGIN class GLViewImpl; - class CC_DLL GLViewImpl : public GLView { public: @@ -59,20 +54,20 @@ public: virtual void swapBuffers(); virtual void setViewPortInPoints(float x , float y , float w , float h); virtual void setScissorInPoints(float x , float y , float w , float h); - const Mat4& getOrientationMatrix() const; - const Mat4& getReverseOrientationMatrix () const {return m_reverseOrientationMatrix;}; Windows::Graphics::Display::DisplayOrientations getDeviceOrientation() {return m_orientation;}; Size getRenerTargetSize() const { return Size(m_width, m_height); } virtual void setIMEKeyboardState(bool bOpen); virtual void setIMEKeyboardState(bool bOpen, std::string str); - Platform::String^ stringToPlatformString(std::string strSrc); - void ShowKeyboard(Windows::Foundation::Rect r); - void HideKeyboard(Windows::Foundation::Rect r); - // WP8 XAML app - virtual bool Create(float width, float height, Windows::Graphics::Display::DisplayOrientations orientation); + virtual bool Create(float width, float height ,Windows::Graphics::Display::DisplayOrientations orientation); + + void setDispatcher(Windows::UI::Core::CoreDispatcher^ dispatcher); + Windows::UI::Core::CoreDispatcher^ getDispatcher() {return m_dispatcher.Get();} + + void setPanel(Windows::UI::Xaml::Controls::Panel^ panel); + Windows::UI::Xaml::Controls::Panel^ getPanel() {return m_panel.Get();} void OnPointerPressed(Windows::UI::Core::PointerEventArgs^ args); void OnPointerMoved(Windows::UI::Core::PointerEventArgs^ args); @@ -89,14 +84,9 @@ public: void QueueBackKeyPress(); void QueuePointerEvent(PointerEventType type, Windows::UI::Core::PointerEventArgs^ args); - void GLViewImpl::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; }; + void QueueEvent(std::shared_ptr& event); bool ShowMessageBox(Platform::String^ title, Platform::String^ message); - bool OpenXamlEditBox(Platform::String^ strPlaceHolder, Platform::String^ strText, int maxLength, int inputMode, int inputFlag, Windows::Foundation::EventHandler^ receiveHandler); int Run(); void Render(); @@ -116,9 +106,6 @@ public: static GLViewImpl* sharedOpenGLView(); void ProcessEvents(); - void AddPointerEvent(PointerEventType type, Windows::UI::Core::PointerEventArgs^ args); - - protected: GLViewImpl(); @@ -144,8 +131,7 @@ private: void OnRendering(); void UpdateWindowSize(); - void UpdateOrientationMatrix(); - + cocos2d::Vec2 TransformToOrientation(Windows::Foundation::Point point); cocos2d::Vec2 GetPoint(Windows::UI::Core::PointerEventArgs^ args); @@ -162,20 +148,20 @@ private: bool m_lastPointValid; bool m_windowClosed; bool m_windowVisible; - Mat4 m_orientationMatrix; - Mat4 m_reverseOrientationMatrix; - bool m_running; bool m_initialized; - PhoneDirect3DXamlAppComponent::Cocos2dEventDelegate^ m_delegate; - PhoneDirect3DXamlAppComponent::Cocos2dMessageBoxDelegate^ m_messageBoxDelegate; - PhoneDirect3DXamlAppComponent::Cocos2dEditBoxDelegate^ m_editBoxDelegate; + Cocos2dEventDelegate^ m_delegate; + Cocos2dMessageBoxDelegate^ m_messageBoxDelegate; + Cocos2dEditBoxDelegate^ m_editBoxDelegate; std::queue> mInputEvents; std::mutex mMutex; + Platform::Agile m_dispatcher; + Platform::Agile m_panel; + KeyBoardWinRT^ m_keyboard; }; NS_CC_END