-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcube_widget.py
More file actions
107 lines (87 loc) · 3.81 KB
/
Copy pathcube_widget.py
File metadata and controls
107 lines (87 loc) · 3.81 KB
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
import sys
from PyQt5.QtWidgets import QOpenGLWidget
from PyQt5.QtGui import QKeyEvent, QMouseEvent
from PyQt5.QtCore import Qt
from OpenGL.GL import *
from OpenGL.GLU import gluPerspective
from OpenGL.GLUT import glutInit
from config import DEFAULT_CONFIG
from PyQt5.QtCore import Qt
from OpenGL.GLUT import glutInit
from OpenGL.GL import (
GL_COLOR_BUFFER_BIT, GL_DEPTH_BUFFER_BIT, GL_DEPTH_TEST, GL_QUADS,
glBegin, glClear, glClearColor, glColor3f, glEnable, glEnd, glLoadIdentity,
glMatrixMode, glRotatef, glTranslatef, glVertex3fv, glViewport, GL_MODELVIEW, GL_PROJECTION
)
from OpenGL.GLU import gluPerspective
from PyQt5.QtWidgets import (
QOpenGLWidget,
)
from keyboard_mouse_input import KeyboardMouseInput
from spacemouse_input import SpaceMouseInput
class CubeWidget(QOpenGLWidget):
def __init__(self, parent=None):
super().__init__(parent)
glutInit()
self.rotation = {"rx": 0, "ry": 0, "rz": 0}
self.translation = {"tx": 0, "ty": 0, "tz": -5}
self.sensitivity = DEFAULT_CONFIG["sensitivity"].copy()
self.axis_direction = DEFAULT_CONFIG["axis_direction"].copy()
self.threshold = DEFAULT_CONFIG["threshold"]
self.axis_enabled = DEFAULT_CONFIG["axis_enabled"].copy()
self.current_action = "None"
self.last_mouse_pos = None # Track mouse movement
# Initialize SpaceMouse safely
self.space_mouse = SpaceMouseInput()
# Initialize Keyboard & Mouse Input
self.keyboard_mouse = KeyboardMouseInput(self)
self.setFocusPolicy(Qt.StrongFocus) # Ensure keyboard focus
# Reset cube on startup
self.reset_cube()
def initializeGL(self):
glEnable(GL_DEPTH_TEST)
glClearColor(0.2, 0.2, 0.2, 1.0)
def resizeGL(self, w, h):
glViewport(0, 0, w, h)
glMatrixMode(GL_PROJECTION)
glLoadIdentity()
gluPerspective(45, w / h, 0.1, 50.0)
glMatrixMode(GL_MODELVIEW)
def paintGL(self):
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
glLoadIdentity()
glTranslatef(self.translation["tx"], self.translation["tz"], self.translation["ty"])
glRotatef(self.rotation["ry"], 1, 0, 0)
glRotatef(self.rotation["rz"], 0, 1, 0)
glRotatef(self.rotation["rx"], 0, 0, 1)
self.draw_cube()
def draw_cube(self):
"""Draws a 3D cube."""
vertices = [(1, 1, -1), (-1, 1, -1), (-1, -1, -1), (1, -1, -1),
(1, 1, 1), (-1, 1, 1), (-1, -1, 1), (1, -1, 1)]
faces = [(0, 1, 2, 3), (4, 5, 6, 7), (0, 3, 7, 4), (1, 2, 6, 5), (0, 1, 5, 4), (3, 2, 6, 7)]
colors = [(1, 0, 0), (0, 1, 0), (0, 0, 1), (1, 1, 0), (1, 0, 1), (0, 1, 1)]
glBegin(GL_QUADS)
for i, face in enumerate(faces):
glColor3f(*colors[i])
for vertex in face:
glVertex3fv(vertices[vertex])
glEnd()
def reset_cube(self):
"""Resets cube position and rotation."""
self.rotation = {"rx": 0, "ry": 0, "rz": 0}
self.translation = {"tx": 0, "ty": -5, "tz": 0}
self.update()
# ----------- DELEGATE KEYBOARD & MOUSE EVENTS ------------
def keyPressEvent(self, event: QKeyEvent):
"""Delegate keyboard events to `KeyboardMouseInput`."""
self.keyboard_mouse.handle_key_press(event)
def mousePressEvent(self, event: QMouseEvent):
"""Delegate mouse press events to `KeyboardMouseInput`."""
self.keyboard_mouse.handle_mouse_press(event)
def mouseMoveEvent(self, event: QMouseEvent):
"""Delegate mouse movement events to `KeyboardMouseInput`."""
self.keyboard_mouse.handle_mouse_move(event)
def wheelEvent(self, event):
"""Delegate mouse wheel events to `KeyboardMouseInput`."""
self.keyboard_mouse.handle_wheel_event(event)