Created
August 18, 2018 06:25
-
-
Save iletai/a0dd74455cf85499f6f79838863c48c0 to your computer and use it in GitHub Desktop.
WCP
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "pch.h" | |
#include "WCPManaged.h" | |
using namespace Windows::ApplicationModel; | |
using namespace Windows::ApplicationModel::Core; | |
using namespace Windows::ApplicationModel::Activation; | |
using namespace Windows::UI::Core; | |
using namespace Windows::System; | |
using namespace Windows::Foundation; | |
using namespace Windows::Graphics::Display; | |
using namespace Windows::Storage; | |
using namespace concurrency; | |
using namespace Windows::UI::Popups; | |
using namespace Windows::UI::Input; | |
using namespace Windows::Media::Devices; | |
using namespace Windows::ApplicationModel::Background; | |
using namespace Windows::Security::ExchangeActiveSyncProvisioning; | |
using namespace Windows::System::Profile; | |
#ifndef WIN8_SUPPORT_XAML | |
using namespace Windows::UI::Xaml; | |
using namespace Windows::UI::Xaml::Controls;///switch to SwapChainPanel | |
#endif | |
#pragma region IMPORT | |
__declspec(dllimport) void Init(); | |
__declspec(dllimport) IDXGISwapChain* GetSwapChain(); | |
__declspec(dllimport) void Render(); | |
__declspec(dllimport) void OnResolutionChanged(); | |
__declspec(dllimport) void OnKeyPressed(int); | |
__declspec(dllimport) void OnKeyReleased(int); | |
__declspec(dllimport) void TrackingUpdate(float); | |
__declspec(dllimport) void OnPointerPressed(); | |
__declspec(dllimport) void OnPointerReleased(); | |
__declspec(dllimport) void OnResolutionChanged(); | |
__declspec(dllimport) void SetRotationSpeed(int speed); | |
__declspec(dllimport) void Trim(); | |
#pragma endregion | |
static bool gs_IsMoviePlaying = false; | |
void RenderLoopFunction() | |
{ | |
static bool first = true; | |
if (first) | |
{ | |
Init(); | |
WCPToolkit::GetWCPRenderManager()->SetDXGISwapChain(GetSwapChain()); | |
first = false; | |
} | |
else | |
{ | |
#if defined(OS_WD) | |
if (!first) | |
#endif | |
{ | |
Render(); | |
} | |
} | |
} | |
void OnMovieEvent(void* args, WCPToolkit::IWCPMovieManager::WCP_MovieEventType movieEventType) | |
{ | |
if (movieEventType == WCPToolkit::IWCPMovieManager::WCP_MovieEventType::WCPMET_Ended) | |
{ | |
//end video return gamerunning | |
WCPToolkit::GetWCPStateManager()->GoToStateAsync(WCPToolkit::IWCPStateManager::WCP_StateType::WCPST_GameRunning); | |
} | |
} | |
void OnEvent(WCPToolkit::IWCPEventManager::WCP_EventType eventType, ::WCPToolkit::IWCPStateManager::WCP_StateType stateType) | |
{ | |
///movie | |
if (stateType == WCPToolkit::IWCPStateManager::WCP_StateType::WCPST_Movie && eventType == WCPToolkit::IWCPEventManager::WCP_EventType::WCPET_EnterState) | |
{ | |
WCPToolkit::IWCPMovieManager::WCPMovieInitData movieInitData; | |
movieInitData.m_aMoviePath = "ms-appx:///Assets/GameloftLogo1.mp4"; | |
//added a handler for the movie, so that i know when it ends | |
WCPToolkit::IWCPMovieManager::WCPMovieHandler movieHandler; | |
movieHandler.m_fOnMovieEvent = OnMovieEvent; | |
movieInitData.m_MovieHandler = movieHandler; | |
//Play movie | |
WCPToolkit::GetWCPMovieManager()->PlayMovie(movieInitData); | |
} | |
///resolution | |
if (stateType == WCPToolkit::IWCPEventManager::WCP_EventType::WCPET_ResolutionChangedSync) | |
{ | |
OnResolutionChanged(); | |
} | |
///SNAP HERE | |
if (stateType == WCPToolkit::IWCPStateManager::WCP_StateType::WCPST_Snap && eventType == WCPToolkit::IWCPEventManager::WCP_EventType::WCPET_EnterState) | |
{ | |
::WCPToolkit::IWCPSnapManager::WCPSnapDescriptor snapDesciptor; | |
snapDesciptor.m_aGetSnapText = "Oh snap"; | |
snapDesciptor.m_aPictureName = "ms-appx:///Assets/WCPSplash.png"; | |
snapDesciptor.m_PictureLayout = ::WCPToolkit::WCPTypes::WCPLayout::WCPL_UniformFill; | |
WCPToolkit::GetWCPSnapManager()->SetSnapDescriptor(snapDesciptor); | |
} | |
if (stateType == ::WCPToolkit::IWCPEventManager::WCP_EventType::WCPET_CompositionScaleChangedAsync) | |
{ | |
float cs = ::WCPToolkit::GetWCPRenderManager()->GetCompositionScale(); | |
WCPToolkit::GetWCPRenderManager()->SetRenderScaleFactor(cs); | |
} | |
if (stateType == ::WCPToolkit::IWCPEventManager::WCP_EventType::WCPET_OnSuspending) | |
{ | |
Trim(); | |
} | |
} | |
#pragma region KEY_POINTER_EVENT | |
void OnPointerEvent(void*, unsigned long i_Id, ::WCPToolkit::IWCPInputManager::WCP_PointerType i_Pt, ::WCPToolkit::IWCPInputManager::WCP_PointerEventType i_Pet, float i_X, float i_Y) | |
{ | |
switch (i_Pt) | |
{ | |
case ::WCPToolkit::IWCPInputManager::WCP_PointerType::WCPPT_Mouse: | |
{ | |
switch (i_Pet) | |
{ | |
case ::WCPToolkit::IWCPInputManager::WCP_PointerEventType::WCPPET_MouseButtonLeftPressed: | |
{ | |
OnPointerPressed(); | |
break; | |
} | |
case ::WCPToolkit::IWCPInputManager::WCP_PointerEventType::WCPPET_MouseButtonRightPressed: | |
{ | |
OnPointerPressed(); | |
break; | |
} | |
case ::WCPToolkit::IWCPInputManager::WCP_PointerEventType::WCPPET_MouseMoved: | |
{ | |
TrackingUpdate(i_X); | |
break; | |
} | |
case ::WCPToolkit::IWCPInputManager::WCP_PointerEventType::WCPPET_MouseDeltaMoved: | |
{ | |
break; | |
} | |
case ::WCPToolkit::IWCPInputManager::WCP_PointerEventType::WCPPET_MouseButtonLeftReleased: | |
{ | |
OnPointerReleased(); | |
break; | |
} | |
case ::WCPToolkit::IWCPInputManager::WCP_PointerEventType::WCPPET_MouseButtonRightReleased: | |
{ | |
OnPointerReleased(); | |
break; | |
} | |
} | |
break; | |
} | |
case ::WCPToolkit::IWCPInputManager::WCP_PointerType::WCPPT_Touch: | |
{ | |
switch (i_Pet) | |
{ | |
case ::WCPToolkit::IWCPInputManager::WCP_PointerEventType::WCPPET_TouchPressed: | |
{ | |
OnPointerPressed(); | |
break; | |
} | |
case ::WCPToolkit::IWCPInputManager::WCP_PointerEventType::WCPPET_TouchReleased: | |
{ | |
OnPointerReleased(); | |
break; | |
} | |
case ::WCPToolkit::IWCPInputManager::WCP_PointerEventType::WCPPET_TouchDragged: | |
{ | |
TrackingUpdate(i_X); | |
break; | |
} | |
} | |
break; | |
} | |
} | |
} | |
void OnKeyEvent(void* args, ::WCPToolkit::IWCPInputManager::WCP_KeyCode keyCode, ::WCPToolkit::IWCPInputManager::WCP_KeyEventType keyEventType) | |
{ | |
switch (keyEventType) | |
{ | |
case WCPToolkit::IWCPInputManager::WCPKET_Pressed: | |
{ | |
OnKeyPressed(keyCode); | |
} | |
break; | |
case WCPToolkit::IWCPInputManager::WCPKET_Released: | |
{ | |
if (keyCode == WCPToolkit::IWCPInputManager::WCP_KeyCode::WCPKC_Escape) | |
{ | |
WCPToolkit::ExitApp(); | |
return; | |
} | |
OnKeyReleased(keyCode); | |
} | |
break; | |
} | |
} | |
void OnVKShowClick(Platform::Object^ sender, Windows::UI::Xaml::RoutedEventArgs^ e) | |
{ | |
Windows::UI::Xaml::Controls::Button^ button = (Windows::UI::Xaml::Controls::Button^)sender; | |
WCPToolkit::IWCPVirtualKeyboard::WCPKeyboardInit keyboardInit; | |
keyboardInit.m_IsTextBoxVisible = false; | |
keyboardInit.m_aInitialText = button->Content; | |
keyboardInit.m_Width = button->ActualWidth; | |
keyboardInit.m_Height = button->ActualHeight; | |
keyboardInit.m_Left = button->Margin.Left; | |
keyboardInit.m_Top = button->Margin.Top; | |
keyboardInit.m_FontProperties.m_FontSize = 0.01f; | |
WCPToolkit::GetWCPVirtualKeyboard()->RegisterVKOnTextChangedCallback([=](WCPToolkit::WCPTypes::WCPString& i_aNewText) | |
{ | |
WCPToolkit::GetWCPUtils()->UTF8toUTF16(i_aNewText, [button](const wchar_t* wstr) { | |
button->Content = ref new Platform::String(wstr); }); | |
}); | |
} | |
#pragma endregion | |
#pragma region SHOW_NOTIFITION | |
void OnShowLocalNotificationClick(Platform::Object^ sender, Windows::UI::Xaml::RoutedEventArgs^ e) | |
{ | |
WCPToolkit::GetWCPLocalNotifications()->ShowLocalNotification("Title TL", "Body", nullptr, 10, nullptr, nullptr); | |
} | |
void OnShowMessageClick(Platform::Object^ sender, Windows::UI::Xaml::RoutedEventArgs^ e) | |
{ | |
WCPToolkit::GetWCPLocalNotifications()->ShowMessageBox(nullptr, "Body of message", "Title of message"); | |
} | |
void OnShowToastClick(Platform::Object^ sender, Windows::UI::Xaml::RoutedEventArgs^ e) | |
{ | |
WCPToolkit::WCPTypes::WCPResolution resolution = WCPToolkit::GetWCPScreenUtils()->GetWindowResolution(); | |
float position = resolution.m_Height / 2; | |
WCPToolkit::GetWCPLocalNotifications()->ShowToast("Body of notification", position); | |
} | |
#pragma endregion | |
void OnMovieEventLoad(void* i_pArg, ::WCPToolkit::IWCPMovieManager::WCP_MovieEventType i_Met) | |
{ | |
switch (i_Met) | |
{ | |
case ::WCPToolkit::IWCPMovieManager::WCP_MovieEventType::WCPMET_Canceled: | |
case ::WCPToolkit::IWCPMovieManager::WCP_MovieEventType::WCPMET_Ended: | |
{ | |
gs_IsMoviePlaying = false; | |
break; | |
} | |
default: | |
break; | |
} | |
} | |
void* WCPAlloc(std::size_t size) | |
{ | |
return malloc(size); | |
} | |
void WCPFree(void* ptr) | |
{ | |
free(ptr); | |
} | |
void OnValueChanged(Platform::Object^ sender, Windows::UI::Xaml::Controls::Primitives::RangeBaseValueChangedEventArgs^ e) | |
{ | |
SetRotationSpeed(e->NewValue); | |
} | |
void OnMoviePlayClick(Platform::Object^ sender, Windows::UI::Xaml::RoutedEventArgs^ e) | |
{ | |
WCPToolkit::GetWCPStateManager()->GoToStateAsync(WCPToolkit::IWCPStateManager::WCP_StateType::WCPST_Movie); | |
} | |
static bool ShoudSnapFunction(const WCPToolkit::IWCPSnapManager::WCPSnapData& snapData) | |
{ | |
if (snapData.m_State == ::WCPToolkit::IWCPStateManager::WCP_StateType::WCPST_GameRunning) | |
{ | |
const float k_MinAspectRatio = 1.2f; | |
const float k_MaxAspectratio = 2.0f; | |
float _AspectRation = snapData.m_Resolution.m_Width / snapData.m_Resolution.m_Height; | |
if (k_MinAspectRatio < _AspectRation || k_MaxAspectratio < _AspectRation || snapData.m_Resolution.m_Width < 500 || snapData.m_Resolution.m_Height < 500) | |
{ | |
return true; | |
} | |
} | |
return false; | |
} | |
static void OnBack(void* arg) | |
{ | |
#if !defined(OS_WD) | |
WCPToolkit::GetWCPLocalNotifications()->ShowMessageBox([](int i_Button) | |
{ | |
if (i_Button == 0) | |
{ | |
WCPToolkit::ExitApp(); | |
} | |
}, nullptr, "Close?", "Yes", "No"); | |
#endif | |
} | |
static void RegisterCallbacks() | |
{ | |
::WCPToolkit::IWCPInputManager::WCPInputEventHandlers inputEventHandlers; | |
inputEventHandlers.m_fOnPointerEvent = OnPointerEvent; | |
inputEventHandlers.m_fOnKeyEvent = OnKeyEvent; | |
inputEventHandlers.m_fOnBackButtonPressedEvent = OnBack; | |
::WCPToolkit::GetWCPInputManager()->RegisterInputEventHandlers(inputEventHandlers); | |
::WCPToolkit::GetWCPEventManager()->RegisterEventHandler(OnEvent); | |
WCPToolkit::GetWCPSnapManager()->RegisterShouldSnapFunction(ShoudSnapFunction); | |
::WCPToolkit::GetWCPRenderManager()->RegisterLoopFunction(RenderLoopFunction); | |
#if !defined(OS_WD) | |
#endif /// !defined(OS_W)D | |
} | |
void SetupIntroMovie(const char* i_aMoviePath, const char* i_aSkipButtonPath) | |
{ | |
::WCPToolkit::IWCPMovieManager::WCPMovieInitData _Mid; | |
_Mid.m_aMoviePath = const_cast<char*>(i_aMoviePath); | |
_Mid.m_aSkipButtonPath = const_cast<char*>(i_aSkipButtonPath); | |
_Mid.m_MovieHandler.m_fOnMovieEvent = OnMovieEvent; | |
// ::WCPToolkit::GetWCPMovieManager()->PlayMovie(_Mid); | |
gs_IsMoviePlaying = true; | |
} | |
void AddRenderingOverlay() | |
{ | |
::WCPToolkit::GetWCPUtils()->RunOnUIThread([=]() | |
{ | |
Windows::UI::Xaml::Controls::Grid^ mainGrid = ref new Windows::UI::Xaml::Controls::Grid(); | |
Windows::UI::Xaml::Controls::Slider^ speedSlider = ref new Windows::UI::Xaml::Controls::Slider(); | |
Windows::UI::Xaml::Controls::Button^ button = ref new Windows::UI::Xaml::Controls::Button(); | |
Windows::UI::Xaml::Controls::Button^ showMessageButton = ref new Windows::UI::Xaml::Controls::Button(); | |
Windows::UI::Xaml::Controls::Button^ showLocalNotificationButton = ref new Windows::UI::Xaml::Controls::Button(); | |
Windows::UI::Xaml::Controls::Button^ showToastButton = ref new Windows::UI::Xaml::Controls::Button(); | |
Windows::UI::Xaml::Controls::Button^ showVBButton = ref new Windows::UI::Xaml::Controls::Button(); | |
showVBButton->Content = "Show VK"; | |
showVBButton->Click += ref new Windows::UI::Xaml::RoutedEventHandler(&OnVKShowClick); | |
showVBButton->VerticalAlignment = Windows::UI::Xaml::VerticalAlignment::Top; | |
showVBButton->Margin = Windows::UI::Xaml::Thickness(0, 300, 0, 0); | |
button->Content = "Start movie"; | |
button->Click += ref new Windows::UI::Xaml::RoutedEventHandler(&OnMoviePlayClick); | |
speedSlider->Minimum = 1; | |
speedSlider->Maximum = 5; | |
speedSlider->Height = 30; | |
speedSlider->ValueChanged += ref new Windows::UI::Xaml::Controls::Primitives::RangeBaseValueChangedEventHandler(&OnValueChanged); | |
speedSlider->VerticalAlignment = Windows::UI::Xaml::VerticalAlignment::Top; | |
showLocalNotificationButton->Content = "Show local notfication in 10s"; | |
showLocalNotificationButton->Click += ref new Windows::UI::Xaml::RoutedEventHandler(&OnShowLocalNotificationClick); | |
showLocalNotificationButton->VerticalAlignment = Windows::UI::Xaml::VerticalAlignment::Top; | |
showLocalNotificationButton->Margin = Windows::UI::Xaml::Thickness(0, 150, 0, 0); | |
//added a button to show a message | |
showMessageButton->Content = "Show message"; | |
showMessageButton->Click += ref new Windows::UI::Xaml::RoutedEventHandler(&OnShowMessageClick); | |
showMessageButton->VerticalAlignment = Windows::UI::Xaml::VerticalAlignment::Top; | |
showMessageButton->Margin = Windows::UI::Xaml::Thickness(0, 200, 0, 0); | |
//added a button to show a toast | |
showToastButton->Content = "Start toast"; | |
showToastButton->Click += ref new Windows::UI::Xaml::RoutedEventHandler(&OnShowToastClick); | |
showToastButton->VerticalAlignment = Windows::UI::Xaml::VerticalAlignment::Top; | |
showToastButton->Margin = Windows::UI::Xaml::Thickness(0, 250, 0, 0); | |
mainGrid->Children->Append(button); | |
mainGrid->Children->Append(showVBButton); | |
mainGrid->Children->Append(speedSlider); | |
mainGrid->Children->Append(showMessageButton); | |
mainGrid->Children->Append(showLocalNotificationButton); | |
mainGrid->Children->Append(showToastButton); | |
WCPToolkit::GetWCPRenderManagerManaged()->AddOverlayGrid(mainGrid); | |
//omited code | |
}); | |
} | |
void InitWCP() | |
{ | |
WCPToolkit::WCPTypes::WCPInitStructure initStruct; | |
//AddRenderingOverlay(); | |
initStruct.m_InitType = WCPToolkit::WCPTypes::WCP_InitType::WCPIT_Default; | |
initStruct.m_EnableExtraStates = 0; | |
initStruct.m_fAlloc = WCPAlloc; // | |
initStruct.m_fFree = WCPFree; // | |
initStruct.m_UseDummyFunctions = true; | |
initStruct.m_WCPVirtualKeyboardVersion = 1; | |
/// this selects what version of the vk to use 0 is the normal one, and 1 is the experimental one | |
::WCPToolkit::Init(initStruct); | |
} | |
#if defined(OS_WD) | |
int __cdecl main(int i_Argc, char* i_aArgv[]) | |
#else | |
int __cdecl main(Platform::Array<Platform::String^>^ args) | |
#endif | |
{ | |
//LoadLiveTiles(); | |
#if defined (WCPDEBUG) | |
WCPToolkit::IWCPDebug::WCPMetrics metrics; | |
metrics.m_FPSSampleSize = 20; | |
metrics.m_MetricsPosition = WCPToolkit::IWCPDebug::WCPMP_UpperRight; | |
WCPToolkit::GetWCPDebug()->InitMetricsLayer(metrics); | |
WCPToolkit::GetWCPDebug()->ToggleMetricsLayer(); | |
WCPToolkit::GetWCPDebug()->ShowDebugLayer(true); | |
WCPToolkit::GetWCPDebug()->RegisterErrorHandler(OnErrorGot); | |
#endif | |
InitWCP(); | |
AddRenderingOverlay(); | |
RegisterCallbacks(); | |
#if defined(OS_WD) | |
WCPToolkit::GetWCPLogoManager()->ShowLogo("..\\..\\..\\..\\Assets\\WCPSnap.png", 2.0f, WCPToolkit::IWCPStateManager::WCPST_GameRunning); | |
#else | |
#endif ///defined(OS_WD) | |
::WCPToolkit::GetWCPLogoManager()->ShowLogo("ms-appx:///Assets/WCPSnap.png", 0.0f, ::WCPToolkit::IWCPStateManager::WCP_StateType::WCPST_GameRunning); | |
SetupIntroMovie("ms-appx://Assets/GameloftLogo1.mp4", "ms-appx:///Assets/1_largetile.png"); | |
WCPToolkit::WCPTypes::WCPStartStructure startStructure; | |
WCPToolkit::StartApp(startStructure); | |
} | |
void LoadLiveTiles() | |
{ | |
::std::wstring _WLocation = ::WCPToolkit::GetWCPStorage()->GetInstallFolderPath().w_str(); | |
_WLocation += L"\\Assets\\1_largetile.png"; | |
::WCPToolkit::GetWCPLiveTiles()->LoadTilesFromPath(_WLocation.c_str(), "", ::WCPToolkit::IWCPLiveTiles::WCP_LiveTileBranding::WCPLTB_Title); | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment