Skip to content
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,12 @@ internal unsafe interface IInputRuntime
/// command sent to the device.</returns>
long DeviceCommand(int deviceId, InputDeviceCommand* commandPtr);

/// <summary>
/// Initialize the focus state based on current conditions of the application.
/// </summary>
/// <seealso cref="focusState"/>
void InitializeFocusState();

/// <summary>
/// Set delegate to be called on input updates.
/// </summary>
Expand Down Expand Up @@ -113,11 +119,15 @@ internal unsafe interface IInputRuntime
/// <summary>
/// Set delegate to call when the application changes focus.
/// </summary>
/// <seealso cref="Application.onFocusChanged"/>
/// <seealso cref="Application.focusChanged"/>
Action<bool> onPlayerFocusChanged { get; set; }
#endif

/// <summary>
/// Flags indicating various focus states for the application and editor.
/// </summary>
FocusFlags focusState { get; set; }

/// <summary>
/// Is true when the player or game view has focus.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ internal enum FocusFlags : ushort
/// <summary>
/// In editor this means the GameView has focus. In a built player this means the player has focus.
/// </summary>
ApplicationFocus = (1 << 0)
ApplicationFocus = (1 << 0),
Comment thread
VeraMommersteeg marked this conversation as resolved.
};

internal partial class InputManager
Expand All @@ -33,10 +33,7 @@ internal void OnFocusChanged(bool focus)
SyncAllDevicesWhenEditorIsActivated();

if (!m_Runtime.isInPlayMode)
{
focusState = focus ? FocusFlags.ApplicationFocus : FocusFlags.None;
return;
}

var gameViewFocus = m_Settings.editorInputBehaviorInPlayMode;
#endif
Expand All @@ -59,7 +56,6 @@ internal void OnFocusChanged(bool focus)
{
// If runInBackground is true, no device changes should happen, even when focus is gained. So early out.
// If runInBackground is false, we still want to sync devices when focus is gained. So we need to continue further.
focusState = focus ? FocusFlags.ApplicationFocus : FocusFlags.None;
return;
}

Expand Down Expand Up @@ -120,9 +116,6 @@ internal void OnFocusChanged(bool focus)
#if UNITY_EDITOR
m_CurrentUpdate = InputUpdateType.None;
#endif

// We set this *after* the block above as defaultUpdateType is influenced by the setting.
focusState = focus ? FocusFlags.ApplicationFocus : FocusFlags.None;
}

/// <summary>
Expand Down Expand Up @@ -150,8 +143,6 @@ private bool ShouldFlushEventBuffer()
/// <summary>
/// Determines if we should exit early from event processing without handling events.
/// </summary>
/// <param name="eventBuffer">The current event buffer</param>
/// <param name="canFlushBuffer">Whether the buffer can be flushed</param>
/// <param name="updateType">The current update type</param>
/// <returns>True if we should exit early, false otherwise.</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
Expand Down
36 changes: 3 additions & 33 deletions Packages/com.unity.inputsystem/InputSystem/Runtime/InputManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -264,27 +264,6 @@ public InputSettings.ScrollDeltaBehavior scrollDeltaBehavior
}
}

public FocusFlags focusState
{
get
{
#if UNITY_INPUTSYSTEM_SUPPORTS_FOCUS_EVENTS
return m_Runtime.focusState;
#else
return m_FocusState;
#endif
}
set
{
#if UNITY_INPUTSYSTEM_SUPPORTS_FOCUS_EVENTS
if (m_Runtime != null)
m_Runtime.focusState = value;
#else
m_FocusState = value;
#endif
}
}

public float pollingFrequency
{
get
Expand Down Expand Up @@ -557,7 +536,7 @@ internal static void StopEditorEventPassthrough()
#else
true;
#endif
private bool applicationHasFocus => (focusState & FocusFlags.ApplicationFocus) != FocusFlags.None;
private bool applicationHasFocus => m_Runtime != null ? m_Runtime.isPlayerFocused : Application.isFocused;

private bool gameHasFocus =>
#if UNITY_EDITOR
Expand Down Expand Up @@ -2027,11 +2006,6 @@ internal void InitializeData()
// we don't know which one the user is going to use. The user
// can manually turn off one of them to optimize operation.
m_UpdateMask = InputUpdateType.Dynamic | InputUpdateType.Fixed;
#if !UNITY_INPUTSYSTEM_SUPPORTS_FOCUS_EVENTS
m_FocusState = Application.isFocused
? m_FocusState | FocusFlags.ApplicationFocus
: m_FocusState & ~FocusFlags.ApplicationFocus;
#endif
#if UNITY_EDITOR
m_EditorIsActive = true;
m_UpdateMask |= InputUpdateType.Editor;
Expand Down Expand Up @@ -2277,9 +2251,7 @@ internal void InstallRuntime(IInputRuntime runtime)
#endif
m_Runtime.pollingFrequency = pollingFrequency;

focusState = m_Runtime.isPlayerFocused
? focusState | FocusFlags.ApplicationFocus
: focusState & ~FocusFlags.ApplicationFocus;
m_Runtime.InitializeFocusState();

// We only hook NativeInputSystem.onBeforeUpdate if necessary.
if (m_BeforeUpdateListeners.length > 0 || m_HaveDevicesWithStateCallbackReceivers)
Expand Down Expand Up @@ -2451,7 +2423,6 @@ internal struct AvailableDevice
private bool m_NativeBeforeUpdateHooked;
private bool m_HaveDevicesWithStateCallbackReceivers;
#if !UNITY_INPUTSYSTEM_SUPPORTS_FOCUS_EVENTS
private FocusFlags m_FocusState = FocusFlags.ApplicationFocus;
private bool m_DiscardOutOfFocusEvents;
private double m_FocusRegainedTime;
#endif
Expand Down Expand Up @@ -3938,8 +3909,7 @@ private void ProcessDeviceConfigurationEvent(InputDevice device)
private unsafe void ProcessFocusEvent(InputEvent* currentEventReadPtr)
{
var focusEventPtr = (InputFocusEvent*)currentEventReadPtr;
FocusFlags state = focusEventPtr->focusFlags;
focusState = state;
m_Runtime.focusState = focusEventPtr->focusFlags;

#if UNITY_EDITOR
SyncAllDevicesWhenEditorIsActivated();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -316,12 +316,24 @@ private bool OnWantsToShutdown()
return true;
}

public void InitializeFocusState()
{
m_FocusState = Application.isFocused
? m_FocusState | FocusFlags.ApplicationFocus
: m_FocusState & ~FocusFlags.ApplicationFocus;
}

#if !UNITY_INPUTSYSTEM_SUPPORTS_FOCUS_EVENTS
private Action<bool> m_FocusChangedMethod;

private void OnFocusChanged(bool focus)
{
m_FocusChangedMethod(focus);

// We set this *after* the callback above as InputManager.defaultUpdateType is influenced by the property.
m_FocusState = focus
Comment thread
chris-massie marked this conversation as resolved.
Outdated
? m_FocusState | FocusFlags.ApplicationFocus
: m_FocusState & ~FocusFlags.ApplicationFocus;
}

#endif
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,13 @@ public unsafe long DeviceCommand(int deviceId, InputDeviceCommand* commandPtr)
}
}

public void InitializeFocusState()
{
// Testing should start in focus and individual tests can change this test runtime to lose focus
// by calling InvokePlayerFocusChanged via InputTestFixture.ScheduleFocusChangedEvent.
m_FocusState = FocusFlags.ApplicationFocus;
}

public void InvokePlayerFocusChanged(bool newFocusState)
{
m_FocusState = newFocusState
Expand Down
Loading