private void CheckAllowedOrientation(ScreenOrientation orientation) { if (AndroidNativeCalls.isOrientationAllowed((int)ConvertToAndroidOrientation(orientation))) { m_AllowedOrientations |= orientation; } }
protected override void OnStartRunning() { base.OnStartRunning(); // setup window Console.WriteLine("Android Window init."); try { m_Initialized = AndroidNativeCalls.init(); } catch { Console.WriteLine(" Exception during initialization."); m_Initialized = false; } if (!m_Initialized) { Console.WriteLine(" Failed."); World.QuitUpdate = true; return; } SetCallbacks(); UpdateDisplayInfo(true); CheckAllowedOrientation(ScreenOrientation.Portrait); CheckAllowedOrientation(ScreenOrientation.ReversePortrait); CheckAllowedOrientation(ScreenOrientation.Landscape); CheckAllowedOrientation(ScreenOrientation.ReverseLandscape); SetOrientationMask(m_ScreenOrientationMask); }
protected override void OnUpdate() { base.OnUpdate(); // advances input state one frame unsafe { // key, scancode, action, mods int keyStreamLen = 0; int *keyStream = AndroidNativeCalls.getKeyStream(ref keyStreamLen); for (int i = 0; i < keyStreamLen; i += 4) { int key = keyStream[i]; int scancode = keyStream[i + 1]; int action = keyStream[i + 2]; int mods = keyStream[i + 3]; KeyCode translatedKey = TranslateKey(key, scancode, mods); if (translatedKey == KeyCode.None) { continue; } if (action == ACTION_UP) { m_inputState.KeyUp(translatedKey); } else if (action == ACTION_DOWN) { m_inputState.KeyDown(translatedKey); } } // touch int touchInfoStreamLen = 0; int *touchInfoStream = AndroidNativeCalls.getTouchInfoStream(ref touchInfoStreamLen); for (int i = 0; i < touchInfoStreamLen; i += 4) { if (touchInfoStream[i + 1] == ACTION_DOWN) { m_inputState.TouchEvent(touchInfoStream[i], TouchState.Began, touchInfoStream[i + 2], touchInfoStream[i + 3]); } else if (touchInfoStream[i + 1] == ACTION_UP) { m_inputState.TouchEvent(touchInfoStream[i], TouchState.Ended, touchInfoStream[i + 2], touchInfoStream[i + 3]); } else if (touchInfoStream[i + 1] == ACTION_MOVE) { m_inputState.TouchEvent(touchInfoStream[i], TouchState.Moved, touchInfoStream[i + 2], touchInfoStream[i + 3]); } else if (touchInfoStream[i + 1] == ACTION_CANCEL) { m_inputState.TouchEvent(touchInfoStream[i], TouchState.Canceled, touchInfoStream[i + 2], touchInfoStream[i + 3]); } } if (touchInfoStreamLen != 0) { m_inputState.hasTouch = true; } } AndroidNativeCalls.resetStreams(); }
protected override void OnDestroy() { // close window if (initialized) { Console.WriteLine("Android Window shutdown."); AndroidNativeCalls.shutdown(0); initialized = false; } }
protected override void OnStartRunning() { base.OnStartRunning(); // setup window Console.WriteLine("Android Window init."); var env = World.GetExistingSystem <TinyEnvironment>(); var config = env.GetConfigData <DisplayInfo>(); try { initialized = AndroidNativeCalls.init(); } catch { Console.WriteLine(" Exception during initialization."); initialized = false; } if (!initialized) { Console.WriteLine(" Failed."); World.QuitUpdate = true; return; } SetOnPauseCallback(); int winw = 0, winh = 0; AndroidNativeCalls.getWindowSize(ref winw, ref winh); config.focused = true; config.visible = true; config.orientation = winw >= winh ? ScreenOrientation.Landscape : ScreenOrientation.Portrait; config.frameWidth = winw; config.frameHeight = winh; int sw = 0, sh = 0; AndroidNativeCalls.getScreenSize(ref sw, ref sh); config.screenWidth = sw; config.screenHeight = sh; config.width = winw; config.height = winh; int fbw = 0, fbh = 0; AndroidNativeCalls.getFramebufferSize(ref fbw, ref fbh); config.framebufferWidth = fbw; config.framebufferHeight = fbh; env.SetConfigData(config); frameTime = AndroidNativeCalls.time(); }
protected override void OnUpdate() { if (!initialized) { return; } #if UNITY_DOTSPLAYER Unity.Profiling.Profiler.FrameEnd(); Unity.Profiling.Profiler.FrameBegin(); #endif var env = World.GetExistingSystem <TinyEnvironment>(); var config = env.GetConfigData <DisplayInfo>(); int winw = 0, winh = 0; AndroidNativeCalls.getWindowSize(ref winw, ref winh); if (winw != config.width || winh != config.height) { if (config.autoSizeToFrame) { Console.WriteLine("Android Window update size."); config.orientation = winw >= winh ? ScreenOrientation.Landscape : ScreenOrientation.Portrait; config.width = winw; config.height = winh; config.frameWidth = winw; config.frameHeight = winh; int fbw = 0, fbh = 0; AndroidNativeCalls.getFramebufferSize(ref fbw, ref fbh); config.framebufferWidth = fbw; config.framebufferHeight = fbh; env.SetConfigData(config); } else { AndroidNativeCalls.resize(config.width, config.height); } } if (!AndroidNativeCalls.messagePump()) { Console.WriteLine("Android message pump exit."); AndroidNativeCalls.shutdown(1); World.QuitUpdate = true; initialized = false; return; } double newFrameTime = AndroidNativeCalls.time(); var timeData = env.StepWallRealtimeFrame(newFrameTime - frameTime); World.SetTime(timeData); frameTime = newFrameTime; }
public override void DebugReadbackImage(out int w, out int h, out NativeArray <byte> pixels) { var env = World.GetExistingSystem <TinyEnvironment>(); var config = env.GetConfigData <DisplayInfo>(); pixels = new NativeArray <byte>(config.framebufferWidth * config.framebufferHeight * 4, Allocator.Persistent); unsafe { AndroidNativeCalls.debugReadback(config.framebufferWidth, config.framebufferHeight, pixels.GetUnsafePtr()); } w = config.framebufferWidth; h = config.framebufferHeight; }
private void OnDeviceOrientationChanged(int orientation) { var deviceOrientation = ConvertFromAndroidOrientation(orientation); if (deviceOrientation != m_DeviceOrientation) { PlatformEvents.SendDeviceOrientationEvent(this, new DeviceOrientationEvent((int)deviceOrientation)); if ((deviceOrientation & m_ScreenOrientationMask) != 0) { AndroidNativeCalls.setOrientation(orientation); } m_DeviceOrientation = deviceOrientation; } }
protected override void OnUpdate() { if (!initialized) { return; } var env = World.GetExistingSystem <TinyEnvironment>(); var config = env.GetConfigData <DisplayInfo>(); int winw = 0, winh = 0; AndroidNativeCalls.getWindowSize(ref winw, ref winh); if (winw != config.width || winh != config.height) { if (config.autoSizeToFrame) { Console.WriteLine("Android Window update size."); config.orientation = winw >= winh ? DisplayOrientation.Horizontal : DisplayOrientation.Vertical; config.width = winw; config.height = winh; config.frameWidth = winw; config.frameHeight = winh; int fbw = 0, fbh = 0; AndroidNativeCalls.getFramebufferSize(ref fbw, ref fbh); config.framebufferWidth = fbw; config.framebufferHeight = fbh; config.renderMode = RenderMode.BGFX; env.SetConfigData(config); } else { AndroidNativeCalls.resize(config.width, config.height); } } if (!AndroidNativeCalls.messagePump()) { Console.WriteLine("Android message pump exit."); AndroidNativeCalls.shutdown(1); World.QuitUpdate = true; initialized = false; return; } #if DEBUG AndroidNativeCalls.debugClear(); #endif double newFrameTime = AndroidNativeCalls.time(); var timeData = env.StepWallRealtimeFrame(newFrameTime - frameTime); World.SetTime(timeData); frameTime = newFrameTime; }
private void UpdateDisplayInfo(bool firstTime) { var config = GetSingleton <DisplayInfo>(); if (firstTime) { config.focused = true; config.visible = true; config.screenDpiScale = 1.0f; config.orientation = m_ScreenOrientation; } int sw = 0, sh = 0; AndroidNativeCalls.getScreenSize(ref sw, ref sh); int winw = 0, winh = 0; AndroidNativeCalls.getWindowSize(ref winw, ref winh); if (firstTime || m_ScreenOrientation != config.orientation || sw != config.screenWidth || sh != config.screenHeight || winw != config.width || winh != config.height || config.framebufferWidth != config.width || config.framebufferHeight != config.height) { Console.WriteLine($"Android Window update, screen size {sw} x {sh}, window size {winw} x {winh}, orientation {(int)m_ScreenOrientation}"); if (config.orientation != m_ScreenOrientation) { PlatformEvents.SendScreenOrientationEvent(this, new ScreenOrientationEvent((int)m_ScreenOrientation)); config.orientation = m_ScreenOrientation; } config.screenWidth = sw; config.screenHeight = sh; if (config.autoSizeToFrame) { config.width = sw; config.height = sh; } else { AndroidNativeCalls.setResolution(config.width, config.height); } config.frameWidth = config.width; config.frameHeight = config.height; config.framebufferWidth = config.width; config.framebufferHeight = config.height; SetSingleton(config); } }
protected override void OnUpdate() { if (!m_Initialized) { return; } UpdateDisplayInfo(false); if (!AndroidNativeCalls.messagePump()) { Console.WriteLine("Android message pump exit."); AndroidNativeCalls.shutdown(1); World.QuitUpdate = true; m_Initialized = false; return; } }
protected override void OnUpdate() { base.OnUpdate(); // advances input state one frame unsafe { // touch int touchInfoStreamLen = 0; int *touchInfoStream = AndroidNativeCalls.getTouchInfoStream(ref touchInfoStreamLen); for (int i = 0; i < touchInfoStreamLen; i += 4) { if (touchInfoStream[i + 1] == 0) //ACTION_DOWN { m_inputState.TouchEvent(touchInfoStream[i], TouchState.Began, touchInfoStream[i + 2], touchInfoStream[i + 3]); } else if (touchInfoStream[i + 1] == 1) //ACTION_UP { m_inputState.TouchEvent(touchInfoStream[i], TouchState.Ended, touchInfoStream[i + 2], touchInfoStream[i + 3]); } else if (touchInfoStream[i + 1] == 2) //ACTION_MOVE { m_inputState.TouchEvent(touchInfoStream[i], TouchState.Moved, touchInfoStream[i + 2], touchInfoStream[i + 3]); } else if (touchInfoStream[i + 1] == 3) //ACTION_CANCEL { m_inputState.TouchEvent(touchInfoStream[i], TouchState.Canceled, touchInfoStream[i + 2], touchInfoStream[i + 3]); } } if (touchInfoStreamLen != 0) { m_inputState.hasTouch = true; } } AndroidNativeCalls.resetStreams(); }
public void SetOnPauseCallback() { AndroidNativeCalls.set_pause_callback(Marshal.GetFunctionPointerForDelegate((OnPauseDelegate)ManagedOnPauseCallback)); }
public override IntPtr GetPlatformWindowHandle() { return((IntPtr)AndroidNativeCalls.getNativeWindow()); }
public void SetOnDestroyCallback(Action m) { onDestroyM = m; AndroidNativeCalls.set_destroy_callback(Marshal.GetFunctionPointerForDelegate((Action)ManagedOnDestroyCallback)); }
public override void InfiniteMainLoop(MainLoopDelegate m) { staticM = m; AndroidNativeCalls.set_animation_frame_callback(Marshal.GetFunctionPointerForDelegate((MainLoopDelegate)ManagedRAFCallback)); }
public override void SetOrientationMask(ScreenOrientation orientation) { Assert.IsTrue(orientation != ScreenOrientation.Unknown, "Orientation mask cannot be 0"); var allowedOrientationMask = orientation & m_AllowedOrientations; if (allowedOrientationMask == 0) { Console.WriteLine($"Orientation mask {(int)orientation} is disabled in project settings"); return; } m_ScreenOrientationMask = orientation; var screenOrientation = GetOrientation(); if (m_DeviceOrientation != screenOrientation && (m_DeviceOrientation & allowedOrientationMask) != 0) { // it is possible to set screen orientation based on current device orientation if (AndroidNativeCalls.setOrientation((int)ConvertToAndroidOrientation(m_DeviceOrientation))) { m_ScreenOrientation = m_DeviceOrientation; } } else if ((screenOrientation & allowedOrientationMask) == 0) { // current orientation is not allowed anymore, trying to find the "best" possibile enabled variant var newOrientation = ScreenOrientation.Portrait; if (screenOrientation == ScreenOrientation.Portrait && (ScreenOrientation.ReversePortrait & allowedOrientationMask) != 0) { newOrientation = ScreenOrientation.ReversePortrait; } else if (screenOrientation == ScreenOrientation.ReversePortrait && (ScreenOrientation.Portrait & allowedOrientationMask) != 0) { newOrientation = ScreenOrientation.Portrait; } else if (screenOrientation == ScreenOrientation.Landscape && (ScreenOrientation.ReverseLandscape & allowedOrientationMask) != 0) { newOrientation = ScreenOrientation.ReverseLandscape; } else if (screenOrientation == ScreenOrientation.ReverseLandscape && (ScreenOrientation.Landscape & allowedOrientationMask) != 0) { newOrientation = ScreenOrientation.Landscape; } else if ((ScreenOrientation.Portrait & allowedOrientationMask) != 0) { newOrientation = ScreenOrientation.Portrait; } else if ((ScreenOrientation.Landscape & allowedOrientationMask) != 0) { newOrientation = ScreenOrientation.Landscape; } else if ((ScreenOrientation.ReversePortrait & allowedOrientationMask) != 0) { newOrientation = ScreenOrientation.ReversePortrait; } else if ((ScreenOrientation.ReverseLandscape & allowedOrientationMask) != 0) { newOrientation = ScreenOrientation.ReverseLandscape; } else { Assert.IsTrue(false, "Unexpected orientation mask {(int)m_ScreenOrientationMask}"); } if (AndroidNativeCalls.setOrientation((int)ConvertToAndroidOrientation(newOrientation))) { m_ScreenOrientation = newOrientation; } } }
private void SetOnDeviceOrientationChangedCallback() { AndroidNativeCalls.set_device_orientation_callback(Marshal.GetFunctionPointerForDelegate((Action <int>)ManagedOnDeviceOrientationChangedCallback)); }
private void SetOnDestroyCallback() { AndroidNativeCalls.set_destroy_callback(Marshal.GetFunctionPointerForDelegate((Action)ManagedOnDestroyCallback)); }
private void SetOnPauseCallback() { AndroidNativeCalls.set_pause_callback(Marshal.GetFunctionPointerForDelegate((Action <int>)ManagedOnPauseCallback)); }