diff --git a/ui/hud_overlay.py b/ui/hud_overlay.py index 0e83010..bd373ec 100644 --- a/ui/hud_overlay.py +++ b/ui/hud_overlay.py @@ -14,6 +14,28 @@ from typing import Optional, Dict, Any if sys.platform == 'win32': import ctypes from ctypes import wintypes + + # Windows API constants for window detection + GW_OWNER = 4 + + # GetForegroundWindow function + user32 = ctypes.windll.user32 + GetForegroundWindow = user32.GetForegroundWindow + GetForegroundWindow.restype = wintypes.HWND + + # GetWindowText functions + GetWindowTextLengthW = user32.GetWindowTextLengthW + GetWindowTextLengthW.argtypes = [wintypes.HWND] + GetWindowTextLengthW.restype = ctypes.c_int + + GetWindowTextW = user32.GetWindowTextW + GetWindowTextW.argtypes = [wintypes.HWND, wintypes.LPWSTR, ctypes.c_int] + GetWindowTextW.restype = ctypes.c_int + + # FindWindow function + FindWindowW = user32.FindWindowW + FindWindowW.argtypes = [wintypes.LPCWSTR, wintypes.LPCWSTR] + FindWindowW.restype = wintypes.HWND from PyQt6.QtWidgets import ( QApplication, QWidget, QVBoxLayout, QHBoxLayout, @@ -124,10 +146,21 @@ class HUDOverlay(QWidget): self._drag_offset = QPoint() self._modifier_pressed = False + # Game window detection + self._auto_hide_with_game = True # Auto-hide when game not focused + self._game_window_title = "Entropia Universe" # Window title to track + self._was_visible_before_unfocus = False + # Timer for session time updates self._timer = QTimer(self) self._timer.timeout.connect(self._update_session_time) + # Timer for game window detection (Windows only) + self._window_check_timer = QTimer(self) + self._window_check_timer.timeout.connect(self._check_game_window) + if sys.platform == 'win32': + self._window_check_timer.start(500) # Check every 500ms + self._setup_window() self._setup_ui() self._load_position() @@ -609,6 +642,51 @@ class HUDOverlay(QWidget): self._stats.session_time = datetime.now() - self._session_start self._update_time_display() + def _check_game_window(self) -> None: + """ + Check if Entropia Universe window is in foreground. + Auto-hide HUD when game is not focused (optional feature). + """ + if not self._auto_hide_with_game or sys.platform != 'win32': + return + + try: + # Get foreground window title + hwnd = GetForegroundWindow() + if hwnd: + length = GetWindowTextLengthW(hwnd) + if length > 0: + buffer = ctypes.create_unicode_buffer(length + 1) + GetWindowTextW(hwnd, buffer, length + 1) + title = buffer.value + + # Check if game is in foreground + is_game_focused = self._game_window_title in title + + if is_game_focused: + # Game is focused - show HUD if it was hidden + if self._was_visible_before_unfocus and not self.isVisible(): + self.show() + else: + # Game not focused - hide HUD if visible + if self.isVisible(): + self._was_visible_before_unfocus = True + self.hide() + else: + self._was_visible_before_unfocus = False + except Exception: + # Silently ignore errors (e.g., Windows API issues) + pass + + def set_auto_hide_with_game(self, enabled: bool) -> None: + """ + Enable/disable auto-hide when game not focused. + + Args: + enabled: True to auto-hide HUD when game loses focus + """ + self._auto_hide_with_game = enabled + def _update_time_display(self) -> None: """Update the time label.""" total_seconds = int(self._stats.session_time.total_seconds())