File size: 4,830 Bytes
a31f556
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
"""

modules/ar_interaction.py — FRIDAY AR Interaction Controls

Hand tracking + Mouse control for AR holograms.

"""

import os
import time
from typing import Optional, Tuple

from config import DATA_DIR

INTERACTION_FILE = os.path.join(DATA_DIR, "ar_interaction.json")


def _load() -> dict:
    if not os.path.exists(INTERACTION_FILE):
        return {"hand_enabled": False, "mouse_enabled": True, "last_hand_pos": None, "last_click": 0}
    try:
        import json
        with open(INTERACTION_FILE, "r") as f:
            return json.load(f)
    except:
        return {"hand_enabled": False, "mouse_enabled": True, "last_hand_pos": None, "last_click": 0}


def _save(data: dict) -> None:
    try:
        os.makedirs(DATA_DIR, exist_ok=True)
        import json
        with open(INTERACTION_FILE, "w") as f:
            json.dump(data, f)
    except:
        pass


# === Hand Tracking ===
def enable_hand_tracking(enabled: bool = True) -> str:
    """Enable/disable hand tracking."""
    data = _load()
    data["hand_enabled"] = enabled
    _save(data)
    return f"Hand tracking {'enabled' if enabled else 'disabled'}."


def is_hand_tracking() -> bool:
    return _load().get("hand_enabled", False)


def set_hand_position(x: float, y: float, z: float = 0.0) -> None:
    """Update hand position from tracking."""
    data = _load()
    data["last_hand_pos"] = {"x": x, "y": y, "z": z, "time": time.time()}
    _save(data)


def get_hand_position() -> Optional[dict]:
    """Get last hand position."""
    data = _load()
    pos = data.get("last_hand_pos")
    if pos and time.time() - pos.get("time", 0) < 0.5:  # Fresh within 500ms
        return pos
    return None


# === Mouse Control ===
def enable_mouse_control(enabled: bool = True) -> str:
    """Enable/disable mouse control."""
    data = _load()
    data["mouse_enabled"] = enabled
    _save(data)
    return f"Mouse control {'enabled' if enabled else 'disabled'}."


def is_mouse_enabled() -> bool:
    return _load().get("mouse_enabled", True)


def get_mouse_position() -> Tuple[int, int]:
    """Get current mouse position."""
    try:
        import ctypes
        pt = ctypes.wintypes.POINT()
        ctypes.windll.user32.GetCursorPos(ctypes.byref(pt))
        return (pt.x, pt.y)
    except:
        return (0, 0)


def simulate_click() -> None:
    """Simulate a mouse click."""
    try:
        import ctypes
        ctypes.windll.user32.mouse_event(0x0002, 0, 0, 0, 0)  # MOUSEEVENTF_LEFTDOWN
        ctypes.windll.user32.mouse_event(0x0004, 0, 0, 0, 0)  # MOUSEEVENTF_LEFTUP
    except:
        pass


def simulate_right_click() -> None:
    """Simulate a right mouse click."""
    try:
        import ctypes
        ctypes.windll.user32.mouse_event(0x0008, 0, 0, 0, 0)  # MOUSEEVENTF_RIGHTDOWN
        ctypes.windll.user32.mouse32mouse_event(0x0010, 0, 0, 0, 0)  # MOUSEEVENTF_RIGHTUP
    except:
        pass


def drag_to(x: int, y: int) -> None:
    """Drag mouse to position."""
    try:
        import ctypes
        ctypes.windll.user32.SetCursorPos(x, y)
    except:
        pass


# === AR Interaction State ===
def get_interaction_state() -> dict:
    """Get current interaction state for AR."""
    hand_pos = get_hand_position()
    mouse_pos = get_mouse_position()

    return {
        "hand_enabled": is_hand_tracking(),
        "mouse_enabled": is_mouse_enabled(),
        "hand_position": hand_pos,
        "mouse_position": mouse_pos,
    }


def process_gesture(gesture: str) -> str:
    """Process hand gesture commands."""
    gesture = gesture.lower()

    if "fist" in gesture or "grab" in gesture:
        simulate_click()
        return "Grab - Clicked!"
    elif "point" in gesture:
        return "Pointing..."
    elif "wave" in gesture:
        return "Waving..."
    elif "open" in gesture:
        return "Hand open..."
    else:
        return f"Gesture: {gesture}"


# === Voice Commands ===
def handle_command(command: str, speak) -> bool:
    c = command.lower()

    if "hand track" in c:
        if "enable" in c or "on" in c:
            result = enable_hand_tracking(True)
        elif "disable" in c or "off" in c:
            result = enable_hand_tracking(False)
        else:
            result = "Hand tracking enabled." if enable_hand_tracking(True) else ""
        speak(result)
        return True

    if "mouse control" in c:
        if "enable" in c or "on" in c:
            result = enable_mouse_control(True)
        elif "disable" in c or "off" in c:
            result = enable_mouse_control(False)
        else:
            result = "Mouse control enabled."
        speak(result)
        return True

    return False