uwp: port cx to winrt #238
This commit is contained in:
+222
-211
@@ -1,219 +1,230 @@
|
||||
#include "stdafx.h"
|
||||
#include "App_Windows.h"
|
||||
#include "Editor.h"
|
||||
|
||||
#include <ppltasks.h>
|
||||
// Use the C++ standard templated min/max
|
||||
#define NOMINMAX
|
||||
|
||||
using namespace Template_UWP;
|
||||
#include <algorithm>
|
||||
#include <cmath>
|
||||
#include <cstdint>
|
||||
#include <exception>
|
||||
#include <future>
|
||||
#include <iterator>
|
||||
#include <memory>
|
||||
#include <stdexcept>
|
||||
|
||||
using namespace concurrency;
|
||||
using namespace Windows::ApplicationModel;
|
||||
using namespace Windows::ApplicationModel::Core;
|
||||
using namespace Windows::ApplicationModel::Activation;
|
||||
using namespace Windows::UI::Core;
|
||||
using namespace Windows::UI::Input;
|
||||
using namespace Windows::System;
|
||||
using namespace Windows::Foundation;
|
||||
using namespace Windows::Graphics::Display;
|
||||
#include "winrt/Windows.ApplicationModel.h"
|
||||
#include "winrt/Windows.ApplicationModel.Core.h"
|
||||
#include "winrt/Windows.ApplicationModel.Activation.h"
|
||||
#include "winrt/Windows.Foundation.h"
|
||||
#include "winrt/Windows.Graphics.Display.h"
|
||||
#include "winrt/Windows.System.h"
|
||||
#include "winrt/Windows.UI.Core.h"
|
||||
#include "winrt/Windows.UI.Input.h"
|
||||
#include "winrt/Windows.UI.ViewManagement.h"
|
||||
|
||||
// The main function is only used to initialize our IFrameworkView class.
|
||||
[Platform::MTAThread]
|
||||
int main(Platform::Array<Platform::String^>^)
|
||||
using namespace winrt::Windows::ApplicationModel;
|
||||
using namespace winrt::Windows::ApplicationModel::Core;
|
||||
using namespace winrt::Windows::ApplicationModel::Activation;
|
||||
using namespace winrt::Windows::UI::Core;
|
||||
using namespace winrt::Windows::UI::Input;
|
||||
using namespace winrt::Windows::UI::ViewManagement;
|
||||
using namespace winrt::Windows::System;
|
||||
using namespace winrt::Windows::Foundation;
|
||||
using namespace winrt::Windows::Graphics::Display;
|
||||
|
||||
class ViewProvider : public winrt::implements<ViewProvider, IFrameworkView>
|
||||
{
|
||||
auto direct3DApplicationSource = ref new Direct3DApplicationSource();
|
||||
CoreApplication::Run(direct3DApplicationSource);
|
||||
public:
|
||||
|
||||
// IFrameworkView methods
|
||||
void Initialize(CoreApplicationView const& applicationView)
|
||||
{
|
||||
applicationView.Activated({ this, &ViewProvider::OnActivated });
|
||||
|
||||
CoreApplication::Suspending({ this, &ViewProvider::OnSuspending });
|
||||
|
||||
CoreApplication::Resuming({ this, &ViewProvider::OnResuming });
|
||||
|
||||
main.infoDisplay.active = true;
|
||||
main.infoDisplay.watermark = true;
|
||||
main.infoDisplay.resolution = true;
|
||||
main.infoDisplay.fpsinfo = true;
|
||||
|
||||
wiRenderer::SetShaderPath("shaders/");
|
||||
}
|
||||
|
||||
void Uninitialize() noexcept
|
||||
{
|
||||
}
|
||||
|
||||
void SetWindow(CoreWindow const& window)
|
||||
{
|
||||
window.SizeChanged({ this, &ViewProvider::OnWindowSizeChanged });
|
||||
|
||||
window.VisibilityChanged({ this, &ViewProvider::OnVisibilityChanged });
|
||||
|
||||
window.Closed([this](auto&&, auto&&) { m_exit = true; });
|
||||
|
||||
auto dispatcher = CoreWindow::GetForCurrentThread().Dispatcher();
|
||||
|
||||
dispatcher.AcceleratorKeyActivated({ this, &ViewProvider::OnAcceleratorKeyActivated });
|
||||
|
||||
auto navigation = SystemNavigationManager::GetForCurrentView();
|
||||
|
||||
// UWP on Xbox One triggers a back request whenever the B button is pressed
|
||||
// which can result in the app being suspended if unhandled
|
||||
navigation.BackRequested([](const winrt::Windows::Foundation::IInspectable&, const BackRequestedEventArgs& args)
|
||||
{
|
||||
args.Handled(true);
|
||||
});
|
||||
|
||||
auto currentDisplayInformation = DisplayInformation::GetForCurrentView();
|
||||
|
||||
currentDisplayInformation.DpiChanged({ this, &ViewProvider::OnDpiChanged });
|
||||
|
||||
DisplayInformation::DisplayContentsInvalidated({ this, &ViewProvider::OnDisplayContentsInvalidated });
|
||||
|
||||
m_DPI = currentDisplayInformation.LogicalDpi();
|
||||
|
||||
m_logicalWidth = window.Bounds().Width;
|
||||
m_logicalHeight = window.Bounds().Height;
|
||||
|
||||
main.SetWindow(&window);
|
||||
}
|
||||
|
||||
void Load(winrt::hstring const&) noexcept
|
||||
{
|
||||
}
|
||||
|
||||
void Run()
|
||||
{
|
||||
while (!m_exit)
|
||||
{
|
||||
if (m_visible)
|
||||
{
|
||||
main.Run();
|
||||
|
||||
CoreWindow::GetForCurrentThread().Dispatcher().ProcessEvents(CoreProcessEventsOption::ProcessAllIfPresent);
|
||||
}
|
||||
else
|
||||
{
|
||||
CoreWindow::GetForCurrentThread().Dispatcher().ProcessEvents(CoreProcessEventsOption::ProcessOneAndAllPending);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
protected:
|
||||
// Event handlers
|
||||
void OnActivated(CoreApplicationView const& /*applicationView*/, IActivatedEventArgs const& args)
|
||||
{
|
||||
if (args.Kind() == ActivationKind::Launch)
|
||||
{
|
||||
auto launchArgs = (const LaunchActivatedEventArgs*)(&args);
|
||||
wiStartupArguments::Parse(launchArgs->Arguments().data());
|
||||
}
|
||||
CoreWindow::GetForCurrentThread().Activate();
|
||||
}
|
||||
|
||||
void OnSuspending(IInspectable const& /*sender*/, SuspendingEventArgs const& args)
|
||||
{
|
||||
auto deferral = args.SuspendingOperation().GetDeferral();
|
||||
|
||||
auto f = std::async(std::launch::async, [this, deferral]()
|
||||
{
|
||||
deferral.Complete();
|
||||
});
|
||||
}
|
||||
|
||||
void OnResuming(IInspectable const& /*sender*/, IInspectable const& /*args*/)
|
||||
{
|
||||
}
|
||||
|
||||
void OnWindowSizeChanged(CoreWindow const& sender, WindowSizeChangedEventArgs const& /*args*/)
|
||||
{
|
||||
m_logicalWidth = sender.Bounds().Width;
|
||||
m_logicalHeight = sender.Bounds().Height;
|
||||
|
||||
float dpiscale = wiPlatform::GetDPIScaling();
|
||||
uint64_t data = 0;
|
||||
data |= int(m_logicalWidth * dpiscale);
|
||||
data |= int(m_logicalHeight * dpiscale) << 16;
|
||||
wiEvent::FireEvent(SYSTEM_EVENT_CHANGE_RESOLUTION, data);
|
||||
}
|
||||
|
||||
void OnVisibilityChanged(CoreWindow const& /*sender*/, VisibilityChangedEventArgs const& args)
|
||||
{
|
||||
m_visible = args.Visible();
|
||||
}
|
||||
|
||||
void OnAcceleratorKeyActivated(CoreDispatcher const&, AcceleratorKeyEventArgs const& args)
|
||||
{
|
||||
if (args.EventType() == CoreAcceleratorKeyEventType::SystemKeyDown
|
||||
&& args.VirtualKey() == VirtualKey::Enter
|
||||
&& args.KeyStatus().IsMenuKeyDown
|
||||
&& !args.KeyStatus().WasKeyDown)
|
||||
{
|
||||
// Implements the classic ALT+ENTER fullscreen toggle
|
||||
auto view = ApplicationView::GetForCurrentView();
|
||||
|
||||
if (view.IsFullScreenMode())
|
||||
view.ExitFullScreenMode();
|
||||
else
|
||||
view.TryEnterFullScreenMode();
|
||||
|
||||
args.Handled(true);
|
||||
}
|
||||
}
|
||||
|
||||
void OnDpiChanged(DisplayInformation const& sender, IInspectable const& /*args*/)
|
||||
{
|
||||
m_DPI = sender.LogicalDpi();
|
||||
|
||||
wiEvent::FireEvent(SYSTEM_EVENT_CHANGE_DPI, (int)m_DPI);
|
||||
}
|
||||
|
||||
void OnDisplayContentsInvalidated(DisplayInformation const& /*sender*/, IInspectable const& /*args*/)
|
||||
{
|
||||
}
|
||||
|
||||
private:
|
||||
bool m_exit = false;
|
||||
bool m_visible = true;
|
||||
float m_DPI = 96;
|
||||
float m_logicalWidth = 800;
|
||||
float m_logicalHeight = 600;
|
||||
Editor main;
|
||||
|
||||
inline int ConvertDipsToPixels(float dips) const noexcept
|
||||
{
|
||||
return int(dips * m_DPI / 96.f + 0.5f);
|
||||
}
|
||||
|
||||
inline float ConvertPixelsToDips(int pixels) const noexcept
|
||||
{
|
||||
return (float(pixels) * 96.f / m_DPI);
|
||||
}
|
||||
};
|
||||
|
||||
class ViewProviderFactory : public winrt::implements<ViewProviderFactory, IFrameworkViewSource>
|
||||
{
|
||||
public:
|
||||
IFrameworkView CreateView()
|
||||
{
|
||||
return winrt::make<ViewProvider>();
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
// Entry point
|
||||
int WINAPI wWinMain(
|
||||
_In_ HINSTANCE /*hInstance*/,
|
||||
_In_ HINSTANCE /*hPrevInstance*/,
|
||||
_In_ LPWSTR /*lpCmdLine*/,
|
||||
_In_ int /*nCmdShow*/
|
||||
)
|
||||
{
|
||||
auto viewProviderFactory = winrt::make<ViewProviderFactory>();
|
||||
CoreApplication::Run(viewProviderFactory);
|
||||
return 0;
|
||||
}
|
||||
|
||||
IFrameworkView^ Direct3DApplicationSource::CreateView()
|
||||
{
|
||||
return ref new App();
|
||||
}
|
||||
|
||||
App::App() :
|
||||
m_windowClosed(false),
|
||||
m_windowVisible(true)
|
||||
{
|
||||
}
|
||||
|
||||
// The first method called when the IFrameworkView is being created.
|
||||
void App::Initialize(CoreApplicationView^ applicationView)
|
||||
{
|
||||
// Register event handlers for app lifecycle. This example includes Activated, so that we
|
||||
// can make the CoreWindow active and start rendering on the window.
|
||||
applicationView->Activated +=
|
||||
ref new TypedEventHandler<CoreApplicationView^, IActivatedEventArgs^>(this, &App::OnActivated);
|
||||
|
||||
CoreApplication::Suspending +=
|
||||
ref new EventHandler<SuspendingEventArgs^>(this, &App::OnSuspending);
|
||||
|
||||
CoreApplication::Resuming +=
|
||||
ref new EventHandler<Platform::Object^>(this, &App::OnResuming);
|
||||
|
||||
wiRenderer::SetShaderPath("shaders/");
|
||||
}
|
||||
|
||||
// Called when the CoreWindow object is created (or re-created).
|
||||
void App::SetWindow(CoreWindow^ window)
|
||||
{
|
||||
window->SizeChanged +=
|
||||
ref new TypedEventHandler<CoreWindow^, WindowSizeChangedEventArgs^>(this, &App::OnWindowSizeChanged);
|
||||
|
||||
window->VisibilityChanged +=
|
||||
ref new TypedEventHandler<CoreWindow^, VisibilityChangedEventArgs^>(this, &App::OnVisibilityChanged);
|
||||
|
||||
window->Closed +=
|
||||
ref new TypedEventHandler<CoreWindow^, CoreWindowEventArgs^>(this, &App::OnWindowClosed);
|
||||
|
||||
DisplayInformation^ currentDisplayInformation = DisplayInformation::GetForCurrentView();
|
||||
|
||||
currentDisplayInformation->DpiChanged +=
|
||||
ref new TypedEventHandler<DisplayInformation^, Object^>(this, &App::OnDpiChanged);
|
||||
|
||||
currentDisplayInformation->OrientationChanged +=
|
||||
ref new TypedEventHandler<DisplayInformation^, Object^>(this, &App::OnOrientationChanged);
|
||||
|
||||
DisplayInformation::DisplayContentsInvalidated +=
|
||||
ref new TypedEventHandler<DisplayInformation^, Object^>(this, &App::OnDisplayContentsInvalidated);
|
||||
|
||||
window->KeyDown += ref new TypedEventHandler<CoreWindow^, KeyEventArgs^>(this, &App::OnKeyDown);
|
||||
window->CharacterReceived += ref new TypedEventHandler<CoreWindow^, CharacterReceivedEventArgs^>(this, &App::OnCharacterReceived);
|
||||
|
||||
editor.SetWindow(wiPlatform::window_type(window));
|
||||
}
|
||||
|
||||
// Initializes scene resources, or loads a previously saved app state.
|
||||
void App::Load(Platform::String^ entryPoint)
|
||||
{
|
||||
}
|
||||
|
||||
// This method is called after the window becomes active.
|
||||
void App::Run()
|
||||
{
|
||||
while (!m_windowClosed)
|
||||
{
|
||||
if (m_windowVisible)
|
||||
{
|
||||
CoreWindow::GetForCurrentThread()->Dispatcher->ProcessEvents(CoreProcessEventsOption::ProcessAllIfPresent);
|
||||
|
||||
editor.Run();
|
||||
}
|
||||
else
|
||||
{
|
||||
CoreWindow::GetForCurrentThread()->Dispatcher->ProcessEvents(CoreProcessEventsOption::ProcessOneAndAllPending);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Required for IFrameworkView.
|
||||
// Terminate events do not cause Uninitialize to be called. It will be called if your IFrameworkView
|
||||
// class is torn down while the app is in the foreground.
|
||||
void App::Uninitialize()
|
||||
{
|
||||
}
|
||||
|
||||
// Application lifecycle event handlers.
|
||||
|
||||
void App::OnActivated(CoreApplicationView^ applicationView, IActivatedEventArgs^ args)
|
||||
{
|
||||
if (args->Kind == ActivationKind::Launch)
|
||||
{
|
||||
LaunchActivatedEventArgs^ launchargs = (LaunchActivatedEventArgs^)args;
|
||||
wiStartupArguments::Parse(launchargs->Arguments->Data());
|
||||
}
|
||||
|
||||
// Run() won't start until the CoreWindow is activated.
|
||||
CoreWindow::GetForCurrentThread()->Activate();
|
||||
}
|
||||
|
||||
void App::OnSuspending(Platform::Object^ sender, SuspendingEventArgs^ args)
|
||||
{
|
||||
// Save app state asynchronously after requesting a deferral. Holding a deferral
|
||||
// indicates that the application is busy performing suspending operations. Be
|
||||
// aware that a deferral may not be held indefinitely. After about five seconds,
|
||||
// the app will be forced to exit.
|
||||
SuspendingDeferral^ deferral = args->SuspendingOperation->GetDeferral();
|
||||
|
||||
create_task([this, deferral]()
|
||||
{
|
||||
// Insert your code here.
|
||||
|
||||
deferral->Complete();
|
||||
});
|
||||
}
|
||||
|
||||
void App::OnResuming(Platform::Object^ sender, Platform::Object^ args)
|
||||
{
|
||||
// Restore any data or state that was unloaded on suspend. By default, data
|
||||
// and state are persisted when resuming from suspend. Note that this event
|
||||
// does not occur if the app was previously terminated.
|
||||
|
||||
// Insert your code here.
|
||||
}
|
||||
|
||||
// Window event handlers.
|
||||
|
||||
void App::OnWindowSizeChanged(CoreWindow^ sender, WindowSizeChangedEventArgs^ args)
|
||||
{
|
||||
float dpiscale = wiPlatform::GetDPIScaling();
|
||||
uint64 data = 0;
|
||||
data |= int(sender->Bounds.Width * dpiscale);
|
||||
data |= int(sender->Bounds.Height * dpiscale) << 16;
|
||||
wiEvent::FireEvent(SYSTEM_EVENT_CHANGE_RESOLUTION, data);
|
||||
}
|
||||
|
||||
void App::OnVisibilityChanged(CoreWindow^ sender, VisibilityChangedEventArgs^ args)
|
||||
{
|
||||
m_windowVisible = args->Visible;
|
||||
}
|
||||
|
||||
void App::OnWindowClosed(CoreWindow^ sender, CoreWindowEventArgs^ args)
|
||||
{
|
||||
m_windowClosed = true;
|
||||
}
|
||||
|
||||
// DisplayInformation event handlers.
|
||||
|
||||
void App::OnDpiChanged(DisplayInformation^ sender, Object^ args)
|
||||
{
|
||||
wiEvent::FireEvent(SYSTEM_EVENT_CHANGE_DPI, (int)sender->LogicalDpi);
|
||||
}
|
||||
|
||||
void App::OnOrientationChanged(DisplayInformation^ sender, Object^ args)
|
||||
{
|
||||
}
|
||||
|
||||
void App::OnDisplayContentsInvalidated(DisplayInformation^ sender, Object^ args)
|
||||
{
|
||||
}
|
||||
|
||||
// Input event handlers
|
||||
void App::OnKeyDown(CoreWindow^ sender, KeyEventArgs^ key)
|
||||
{
|
||||
}
|
||||
void App::OnCharacterReceived(CoreWindow^ sender, CharacterReceivedEventArgs^ key)
|
||||
{
|
||||
|
||||
switch (key->KeyCode)
|
||||
{
|
||||
case (unsigned int)VirtualKey::Back:
|
||||
if (wiBackLog::isActive())
|
||||
wiBackLog::deletefromInput();
|
||||
wiTextInputField::DeleteFromInput();
|
||||
break;
|
||||
|
||||
case (unsigned int)VirtualKey::Enter:
|
||||
break;
|
||||
|
||||
default:
|
||||
{
|
||||
const char c = (const char)key->KeyCode;
|
||||
if (wiBackLog::isActive())
|
||||
{
|
||||
wiBackLog::input(c);
|
||||
}
|
||||
wiTextInputField::AddInput(c);
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user