updated for Universal App support

This commit is contained in:
Dale Stammen 2014-10-23 07:07:58 -07:00
parent 06913670dc
commit b20015f845
2 changed files with 79 additions and 158 deletions

View File

@ -24,7 +24,6 @@ THE SOFTWARE.
****************************************************************************/ ****************************************************************************/
#include "CCGLViewImpl-winrt.h" #include "CCGLViewImpl-winrt.h"
#include "deprecated/CCSet.h"
#include "base/ccMacros.h" #include "base/ccMacros.h"
#include "base/CCDirector.h" #include "base/CCDirector.h"
#include "base/CCTouch.h" #include "base/CCTouch.h"
@ -40,7 +39,9 @@ using namespace Windows::Graphics::Display;
using namespace Windows::UI::Input; using namespace Windows::UI::Input;
using namespace Windows::UI::Core; using namespace Windows::UI::Core;
using namespace Windows::UI::Xaml; using namespace Windows::UI::Xaml;
using namespace Windows::UI::Xaml::Controls;
using namespace Windows::UI::Xaml::Media; using namespace Windows::UI::Xaml::Media;
using namespace Windows::UI::Xaml::Input;
using namespace Windows::System; using namespace Windows::System;
using namespace Windows::UI::ViewManagement; using namespace Windows::UI::ViewManagement;
using namespace Windows::ApplicationModel; using namespace Windows::ApplicationModel;
@ -48,7 +49,6 @@ using namespace Windows::ApplicationModel::Core;
using namespace Windows::ApplicationModel::Activation; using namespace Windows::ApplicationModel::Activation;
using namespace Platform; using namespace Platform;
using namespace Microsoft::WRL; using namespace Microsoft::WRL;
using namespace PhoneDirect3DXamlAppComponent;
NS_CC_BEGIN NS_CC_BEGIN
@ -79,8 +79,6 @@ GLViewImpl::GLViewImpl()
, m_windowVisible(true) , m_windowVisible(true)
, m_width(0) , m_width(0)
, m_height(0) , m_height(0)
, m_delegate(nullptr)
, m_messageBoxDelegate(nullptr)
, m_orientation(DisplayOrientations::Landscape) , m_orientation(DisplayOrientations::Landscape)
{ {
s_pEglView = this; s_pEglView = this;
@ -116,6 +114,16 @@ bool GLViewImpl::Create(float width, float height, DisplayOrientations orientati
return true; 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) void GLViewImpl::setIMEKeyboardState(bool bOpen)
@ -124,44 +132,55 @@ void GLViewImpl::setIMEKeyboardState(bool bOpen)
setIMEKeyboardState(bOpen, str); 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) 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() void GLViewImpl::swapBuffers()
{ {
//eglSwapBuffers(m_eglDisplay, m_eglSurface);
} }
bool GLViewImpl::isOpenGLReady() bool GLViewImpl::isOpenGLReady()
{ {
return true; return true;
// TODO: need to revisit this
//return (m_eglDisplay && m_orientation != DisplayOrientations::None);
} }
void GLViewImpl::end() void GLViewImpl::end()
@ -184,7 +203,7 @@ void GLViewImpl::OnBackKeyPress()
std::string str; std::string str;
if(m_delegate) 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<Platform::String^>^ receiveHandler)
{
if(m_editBoxDelegate)
{
m_editBoxDelegate->Invoke(strPlaceHolder, strText, maxLength, inputMode, inputFlag, receiveHandler);
return true;
}
return false;
}
// called by orientation change from WP8 XAML // called by orientation change from WP8 XAML
void GLViewImpl::UpdateOrientation(DisplayOrientations orientation) void GLViewImpl::UpdateOrientation(DisplayOrientations orientation)
{ {
@ -345,9 +340,12 @@ void GLViewImpl::UpdateOrientation(DisplayOrientations orientation)
// called by size change from WP8 XAML // called by size change from WP8 XAML
void GLViewImpl::UpdateForWindowSizeChange(float width, float height) void GLViewImpl::UpdateForWindowSizeChange(float width, float height)
{ {
m_width = width; if (width != m_width || height != m_height)
m_height = height; {
UpdateWindowSize(); m_width = width;
m_height = height;
UpdateWindowSize();
}
} }
#if 0 #if 0
@ -371,18 +369,9 @@ void GLViewImpl::UpdateWindowSize()
{ {
float width, height; float width, height;
if(m_orientation == DisplayOrientations::Landscape || m_orientation == DisplayOrientations::LandscapeFlipped) width = m_width;
{ height = m_height;
width = m_height;
height = m_width;
}
else
{
width = m_width;
height = m_height;
}
UpdateOrientationMatrix();
//CCSize designSize = getDesignResolutionSize(); //CCSize designSize = getDesignResolutionSize();
if(!m_initialized) 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<float>(M_PI));
kmMat4RotationZ(&m_reverseOrientationMatrix, static_cast<float>(-M_PI));
break;
case Windows::Graphics::Display::DisplayOrientations::Landscape:
kmMat4RotationZ(&m_orientationMatrix, static_cast<float>(-M_PI_2));
kmMat4RotationZ(&m_reverseOrientationMatrix, static_cast<float>(M_PI_2));
break;
case Windows::Graphics::Display::DisplayOrientations::LandscapeFlipped:
kmMat4RotationZ(&m_orientationMatrix, static_cast<float>(M_PI_2));
kmMat4RotationZ(&m_reverseOrientationMatrix, static_cast<float>(-M_PI_2));
break;
default:
break;
}
}
cocos2d::Vec2 GLViewImpl::TransformToOrientation(Windows::Foundation::Point p) cocos2d::Vec2 GLViewImpl::TransformToOrientation(Windows::Foundation::Point p)
{ {
cocos2d::Vec2 returnValue; cocos2d::Vec2 returnValue;
float x = p.X; float x = p.X;
float y = p.Y; float y = p.Y;
returnValue = Vec2(x, y);
#if 0
switch (m_orientation) switch (m_orientation)
{ {
case DisplayOrientations::Portrait: case DisplayOrientations::Portrait:
@ -458,6 +417,7 @@ cocos2d::Vec2 GLViewImpl::TransformToOrientation(Windows::Foundation::Point p)
returnValue = Vec2(m_height - y, x); returnValue = Vec2(m_height - y, x);
break; break;
} }
#endif
float zoomFactor = GLViewImpl::sharedOpenGLView()->getFrameZoomFactor(); float zoomFactor = GLViewImpl::sharedOpenGLView()->getFrameZoomFactor();
if(zoomFactor > 0.0f) { if(zoomFactor > 0.0f) {
@ -473,48 +433,23 @@ cocos2d::Vec2 GLViewImpl::TransformToOrientation(Windows::Foundation::Point p)
Vec2 GLViewImpl::GetPoint(PointerEventArgs^ args) { Vec2 GLViewImpl::GetPoint(PointerEventArgs^ args) {
return TransformToOrientation(args->CurrentPoint->Position); return TransformToOrientation(args->CurrentPoint->Position);
} }
void GLViewImpl::setViewPortInPoints(float x , float y , float w , float h) void GLViewImpl::setViewPortInPoints(float x , float y , float w , float h)
{ {
switch(m_orientation) glViewport((GLint) (x * _scaleX + _viewPortRect.origin.x),
{ (GLint) (y * _scaleY + _viewPortRect.origin.y),
case DisplayOrientations::Landscape: (GLsizei) (w * _scaleX),
case DisplayOrientations::LandscapeFlipped: (GLsizei) (h * _scaleY));
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));
}
} }
void GLViewImpl::setScissorInPoints(float x , float y , float w , float h) void GLViewImpl::setScissorInPoints(float x , float y , float w , float h)
{ {
switch(m_orientation) glScissor((GLint) (x * _scaleX + _viewPortRect.origin.x),
{ (GLint) (y * _scaleY + _viewPortRect.origin.y),
case DisplayOrientations::Landscape: (GLsizei) (w * _scaleX),
case DisplayOrientations::LandscapeFlipped: (GLsizei) (h * _scaleY));
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));
}
} }
void GLViewImpl::QueueBackKeyPress() void GLViewImpl::QueueBackKeyPress()

View File

@ -33,21 +33,16 @@ THE SOFTWARE.
#include <agile.h> #include <agile.h>
#include <string>
#include <wrl/client.h> #include <wrl/client.h>
#include <d3d11_1.h>
#include <mutex> #include <mutex>
#include <queue> #include <queue>
#include <Keyboard-winrt.h>
#include <agile.h>
#include <DirectXMath.h>
NS_CC_BEGIN NS_CC_BEGIN
class GLViewImpl; class GLViewImpl;
class CC_DLL GLViewImpl : public GLView class CC_DLL GLViewImpl : public GLView
{ {
public: public:
@ -59,20 +54,20 @@ public:
virtual void swapBuffers(); virtual void swapBuffers();
virtual void setViewPortInPoints(float x , float y , float w , float h); virtual void setViewPortInPoints(float x , float y , float w , float h);
virtual void setScissorInPoints(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;}; Windows::Graphics::Display::DisplayOrientations getDeviceOrientation() {return m_orientation;};
Size getRenerTargetSize() const { return Size(m_width, m_height); } Size getRenerTargetSize() const { return Size(m_width, m_height); }
virtual void setIMEKeyboardState(bool bOpen); virtual void setIMEKeyboardState(bool bOpen);
virtual void setIMEKeyboardState(bool bOpen, std::string str); 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 OnPointerPressed(Windows::UI::Core::PointerEventArgs^ args);
void OnPointerMoved(Windows::UI::Core::PointerEventArgs^ args); void OnPointerMoved(Windows::UI::Core::PointerEventArgs^ args);
@ -89,14 +84,9 @@ public:
void QueueBackKeyPress(); void QueueBackKeyPress();
void QueuePointerEvent(PointerEventType type, Windows::UI::Core::PointerEventArgs^ args); void QueuePointerEvent(PointerEventType type, Windows::UI::Core::PointerEventArgs^ args);
void GLViewImpl::QueueEvent(std::shared_ptr<InputEvent>& event); void 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; };
bool ShowMessageBox(Platform::String^ title, Platform::String^ message); 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<Platform::String^>^ receiveHandler);
int Run(); int Run();
void Render(); void Render();
@ -116,9 +106,6 @@ public:
static GLViewImpl* sharedOpenGLView(); static GLViewImpl* sharedOpenGLView();
void ProcessEvents(); void ProcessEvents();
void AddPointerEvent(PointerEventType type, Windows::UI::Core::PointerEventArgs^ args);
protected: protected:
GLViewImpl(); GLViewImpl();
@ -144,8 +131,7 @@ private:
void OnRendering(); void OnRendering();
void UpdateWindowSize(); void UpdateWindowSize();
void UpdateOrientationMatrix();
cocos2d::Vec2 TransformToOrientation(Windows::Foundation::Point point); cocos2d::Vec2 TransformToOrientation(Windows::Foundation::Point point);
cocos2d::Vec2 GetPoint(Windows::UI::Core::PointerEventArgs^ args); cocos2d::Vec2 GetPoint(Windows::UI::Core::PointerEventArgs^ args);
@ -162,20 +148,20 @@ private:
bool m_lastPointValid; bool m_lastPointValid;
bool m_windowClosed; bool m_windowClosed;
bool m_windowVisible; bool m_windowVisible;
Mat4 m_orientationMatrix;
Mat4 m_reverseOrientationMatrix;
bool m_running; bool m_running;
bool m_initialized; bool m_initialized;
PhoneDirect3DXamlAppComponent::Cocos2dEventDelegate^ m_delegate; Cocos2dEventDelegate^ m_delegate;
PhoneDirect3DXamlAppComponent::Cocos2dMessageBoxDelegate^ m_messageBoxDelegate; Cocos2dMessageBoxDelegate^ m_messageBoxDelegate;
PhoneDirect3DXamlAppComponent::Cocos2dEditBoxDelegate^ m_editBoxDelegate; Cocos2dEditBoxDelegate^ m_editBoxDelegate;
std::queue<std::shared_ptr<InputEvent>> mInputEvents; std::queue<std::shared_ptr<InputEvent>> mInputEvents;
std::mutex mMutex; std::mutex mMutex;
Platform::Agile<Windows::UI::Core::CoreDispatcher> m_dispatcher;
Platform::Agile<Windows::UI::Xaml::Controls::Panel> m_panel;
KeyBoardWinRT^ m_keyboard;
}; };
NS_CC_END NS_CC_END