Fix bug with Screen.CursorVisible set in script OnStart

#799
This commit is contained in:
Wojtek Figat
2022-10-29 19:33:34 +02:00
parent 7ddc1a1553
commit e1836a222c
2 changed files with 38 additions and 18 deletions

View File

@@ -21,7 +21,8 @@ namespace FlaxEditor.Windows
private readonly GameRoot _guiRoot; private readonly GameRoot _guiRoot;
private bool _showGUI = true; private bool _showGUI = true;
private bool _showDebugDraw = false; private bool _showDebugDraw = false;
private bool _isMaximized = false; private bool _isMaximized = false, _isUnlockingMouse = false;
private bool _cursorVisible = true;
private float _gameStartTime; private float _gameStartTime;
private GUI.Docking.DockState _maximizeRestoreDockState; private GUI.Docking.DockState _maximizeRestoreDockState;
private GUI.Docking.DockPanel _maximizeRestoreDockTo; private GUI.Docking.DockPanel _maximizeRestoreDockTo;
@@ -460,14 +461,20 @@ namespace FlaxEditor.Windows
{ {
if (Editor.StateMachine.IsPlayMode) if (Editor.StateMachine.IsPlayMode)
{ {
Screen.CursorVisible = true; // Cache cursor
Focus(null); _cursorVisible = Screen.CursorVisible;
Editor.Windows.MainWindow.Focus(); _cursorLockMode = Screen.CursorLock;
if (Editor.Windows.PropertiesWin.IsDocked)
Editor.Windows.PropertiesWin.Focus();
Screen.CursorVisible = true; Screen.CursorVisible = true;
if (Screen.CursorLock == CursorLockMode.Clipped) if (Screen.CursorLock == CursorLockMode.Clipped)
Screen.CursorLock = CursorLockMode.None; Screen.CursorLock = CursorLockMode.None;
// Defocus
_isUnlockingMouse = true;
Focus(null);
_isUnlockingMouse = false;
Editor.Windows.MainWindow.Focus();
if (Editor.Windows.PropertiesWin.IsDocked)
Editor.Windows.PropertiesWin.Focus();
} }
} }
@@ -552,9 +559,11 @@ namespace FlaxEditor.Windows
Root.MousePosition = center; Root.MousePosition = center;
} }
// Restore lock mode // Restore cursor
if (_cursorLockMode != CursorLockMode.None) if (_cursorLockMode != CursorLockMode.None)
Screen.CursorLock = _cursorLockMode; Screen.CursorLock = _cursorLockMode;
if (!_cursorVisible)
Screen.CursorVisible = false;
} }
} }
@@ -563,11 +572,16 @@ namespace FlaxEditor.Windows
{ {
base.OnEndContainsFocus(); base.OnEndContainsFocus();
// Restore cursor visibility (could be hidden by the game) if (!_isUnlockingMouse)
Screen.CursorVisible = true; {
// Cache cursor
_cursorVisible = Screen.CursorVisible;
_cursorLockMode = Screen.CursorLock;
// Cache lock mode // Restore cursor visibility (could be hidden by the game)
_cursorLockMode = Screen.CursorLock; if (!_cursorVisible)
Screen.CursorVisible = true;
}
} }
/// <inheritdoc /> /// <inheritdoc />

View File

@@ -15,7 +15,8 @@
Nullable<bool> Fullscreen; Nullable<bool> Fullscreen;
Nullable<Float2> Size; Nullable<Float2> Size;
static CursorLockMode CursorLock = CursorLockMode::None; bool CursorVisible = true;
CursorLockMode CursorLock = CursorLockMode::None;
class ScreenService : public EngineService class ScreenService : public EngineService
{ {
@@ -25,6 +26,7 @@ public:
{ {
} }
void Update() override;
void Draw() override; void Draw() override;
}; };
@@ -88,12 +90,7 @@ Float2 Screen::GameViewportToScreen(const Float2& viewportPos)
bool Screen::GetCursorVisible() bool Screen::GetCursorVisible()
{ {
#if USE_EDITOR return CursorVisible;
const auto win = Editor::Managed->GetGameWindow(true);
#else
const auto win = Engine::MainWindow;
#endif
return win ? win->GetCursor() != CursorType::Hidden : true;
} }
void Screen::SetCursorVisible(const bool value) void Screen::SetCursorVisible(const bool value)
@@ -107,6 +104,7 @@ void Screen::SetCursorVisible(const bool value)
{ {
win->SetCursor(value ? CursorType::Default : CursorType::Hidden); win->SetCursor(value ? CursorType::Default : CursorType::Hidden);
} }
CursorVisible = value;
} }
CursorLockMode Screen::GetCursorLock() CursorLockMode Screen::GetCursorLock()
@@ -137,6 +135,14 @@ void Screen::SetCursorLock(CursorLockMode mode)
CursorLock = mode; CursorLock = mode;
} }
void ScreenService::Update()
{
#if USE_EDITOR
// Sync current cursor state in Editor (eg. when viewport focus can change)
Screen::SetCursorVisible(CursorVisible);
#endif
}
void ScreenService::Draw() void ScreenService::Draw()
{ {
#if USE_EDITOR #if USE_EDITOR