| 120 | } |
| 121 | |
| 122 | void Screen::SetCursorLock(CursorLockMode mode) |
| 123 | { |
| 124 | #if USE_EDITOR |
| 125 | const auto win = Editor::Managed->GetGameWindow(true); |
| 126 | Rectangle bounds(Editor::Managed->GameViewportToScreen(Float2::Zero), Editor::Managed->GetGameWindowSize()); |
| 127 | if (win) |
| 128 | { |
| 129 | bounds = Rectangle(win->ScreenToClient(bounds.GetTopLeft()), bounds.Size); |
| 130 | |
| 131 | // Don't change lock mode in play mode when viewport doesn't have focus |
| 132 | if (mode != CursorLockMode::None && Editor::IsPlayMode && !Engine::HasGameViewportFocus()) |
| 133 | { |
| 134 | PendingCursorLock = mode; |
| 135 | return; |
| 136 | } |
| 137 | PendingCursorLock = CursorLockMode::None; |
| 138 | } |
| 139 | #else |
| 140 | const auto win = Engine::MainWindow; |
| 141 | Rectangle bounds = Rectangle::Empty; |
| 142 | if (win) |
| 143 | bounds.Size = win->GetClientSize(); |
| 144 | |
| 145 | // Don't change lock mode when window is not focused |
| 146 | if (mode != CursorLockMode::None && win && !win->IsFocused()) |
| 147 | { |
| 148 | PendingCursorLock = mode; |
| 149 | return; |
| 150 | } |
| 151 | PendingCursorLock = CursorLockMode::None; |
| 152 | #endif |
| 153 | if (win) |
| 154 | { |
| 155 | bool inRelativeMode = Input::Mouse && Input::Mouse->IsRelative(); |
| 156 | if (mode == CursorLockMode::Clipped) |
| 157 | win->StartClippingCursor(bounds); |
| 158 | #if PLATFORM_SDL |
| 159 | else if (mode == CursorLockMode::Locked) |
| 160 | { |
| 161 | // Use mouse clip region to restrict the cursor in one spot |
| 162 | win->StartClippingCursor(Rectangle(bounds.GetCenter(), Float2(1, 1))); |
| 163 | } |
| 164 | else if (CursorLock == CursorLockMode::Locked || CursorLock == CursorLockMode::Clipped) |
| 165 | win->EndClippingCursor(); |
| 166 | #else |
| 167 | else if (CursorLock == CursorLockMode::Clipped) |
| 168 | win->EndClippingCursor(); |
| 169 | #endif |
| 170 | |
| 171 | if (Input::Mouse) |
| 172 | { |
| 173 | // Enable relative mode when cursor is restricted |
| 174 | if (mode != CursorLockMode::None) |
| 175 | Input::Mouse->SetRelativeMode(true, win); |
| 176 | else if (mode == CursorLockMode::None && inRelativeMode) |
| 177 | Input::Mouse->SetRelativeMode(false, win); |
| 178 | } |
| 179 | } |
nothing calls this directly
no test coverage detected