-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create turn notifier for when the game is running for Windows (#6682)
* Create turn notifier for when the game is running for Windows If playing on Desktop, you often put the game into background, but still want to know if it's your turn. A standard Windows function for that is `FlashWindow` from winuser.h, which is implemented here * Fix: Use the window from the listener instead of the static one from libGDL * Only notify if it's the turn of the player that is playing * Always notify spectators of the next players' turn * Refactor: Move notifier into GeneralPlatformSpecificHelpers * Only load Windows DLL when we're actually on Windows
- Loading branch information
Showing
6 changed files
with
88 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
64 changes: 64 additions & 0 deletions
64
desktop/src/com/unciv/app/desktop/MultiplayerTurnNotifierDesktop.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
package com.unciv.app.desktop | ||
|
||
import com.badlogic.gdx.backends.lwjgl3.Lwjgl3Window | ||
import com.badlogic.gdx.backends.lwjgl3.Lwjgl3WindowAdapter | ||
import com.sun.jna.Native | ||
import com.sun.jna.Pointer | ||
import com.sun.jna.platform.win32.User32 | ||
import com.sun.jna.platform.win32.WinNT | ||
import com.sun.jna.platform.win32.WinUser | ||
import org.lwjgl.glfw.GLFWNativeWin32 | ||
|
||
class MultiplayerTurnNotifierDesktop: Lwjgl3WindowAdapter() { | ||
companion object { | ||
val user32: User32? = try { | ||
if (System.getProperty("os.name")?.contains("Windows") == true) { | ||
Native.load(User32::class.java) | ||
} else { | ||
null | ||
} | ||
} catch (e: UnsatisfiedLinkError) { | ||
println("Error while initializing turn notifier: " + e.message) | ||
null | ||
} | ||
} | ||
private var window: Lwjgl3Window? = null | ||
private var hasFocus: Boolean = true | ||
|
||
override fun created(window: Lwjgl3Window?) { | ||
this.window = window | ||
} | ||
|
||
override fun focusLost() { | ||
hasFocus = false | ||
} | ||
|
||
override fun focusGained() { | ||
hasFocus = true | ||
} | ||
|
||
|
||
fun turnStarted() { | ||
flashWindow() | ||
} | ||
|
||
/** | ||
* See https://docs.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-flashwindowex | ||
* | ||
* We should've used FlashWindow instead of FlashWindowEx, but for some reason the former has no binding in Java's User32 | ||
*/ | ||
private fun flashWindow() { | ||
try { | ||
if (user32 == null || window == null || hasFocus) return | ||
val flashwinfo = WinUser.FLASHWINFO() | ||
val hwnd = GLFWNativeWin32.glfwGetWin32Window(window!!.windowHandle) | ||
flashwinfo.hWnd = WinNT.HANDLE(Pointer.createConstant(hwnd)) | ||
flashwinfo.dwFlags = 3 // FLASHW_ALL | ||
flashwinfo.uCount = 3 | ||
user32.FlashWindowEx(flashwinfo) | ||
} catch (e: Throwable) { | ||
/** try to ignore even if we get an [Error], just log it */ | ||
println("Error while notifying the user of their turn: " + e.message) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters