2d drawing test
This commit is contained in:
parent
8db6743dcd
commit
fdc3948f8c
@ -14,7 +14,7 @@ class OpenGLWidget(QOpenGLWidget):
|
||||
self.xRot = 0
|
||||
self.yRot = 0
|
||||
self.zoom = -10.0
|
||||
|
||||
self.sketch = []
|
||||
def map_value_to_range(self, value, value_min: int = 0, value_max: int= 1920, range_min=-1, range_max=1):
|
||||
"""
|
||||
Maps a value from one range to another.
|
||||
@ -70,8 +70,9 @@ class OpenGLWidget(QOpenGLWidget):
|
||||
|
||||
# Draw the rectangle if start and end positions are defined
|
||||
self.draw_area()
|
||||
if self.startPos and self.endPos:
|
||||
self.draw_line(self.startPos, self.endPos)
|
||||
|
||||
if self.sketch:
|
||||
self.draw_lines_between_points(self.sketch)
|
||||
|
||||
|
||||
def draw_area(self):
|
||||
@ -81,19 +82,20 @@ class OpenGLWidget(QOpenGLWidget):
|
||||
# Draw vertical lines
|
||||
glBegin(GL_LINES)
|
||||
for x in range(0, self.width(), 20):
|
||||
x_ndc = self.map_value_to_range(x, 0, value_max=self.width(), range_min=-1, range_max=1)
|
||||
x_ndc = self.map_value_to_range(x, 0, value_max=self.width(), range_min=-2, range_max=2)
|
||||
glVertex2f(x_ndc, -1) # Start from y = -1
|
||||
glVertex2f(x_ndc, 1) # End at y = 1
|
||||
glEnd()
|
||||
|
||||
# Draw horizontal lines
|
||||
glBegin(GL_LINES)
|
||||
|
||||
for y in range(0, self.height(), 20):
|
||||
y_ndc = self.map_value_to_range(y, 0, value_max=self.height(),range_min=-1, range_max=1)
|
||||
glVertex2f(-1, y_ndc) # Start from x = -1
|
||||
glVertex2f(1, y_ndc) # End at x = 1
|
||||
glVertex2f(-2, y_ndc) # Start from x = -1
|
||||
glVertex2f(2, y_ndc) # End at x = 1
|
||||
glEnd()
|
||||
|
||||
|
||||
|
||||
def draw_line(self, startPos, endPos):
|
||||
# Get normalized mouse coordinates
|
||||
print("start", startPos)
|
||||
@ -114,68 +116,77 @@ class OpenGLWidget(QOpenGLWidget):
|
||||
glVertex2f(x2, y2)
|
||||
glEnd()
|
||||
|
||||
def mousePressEvent(self, event):
|
||||
if event.buttons() & Qt.MouseButton.LeftButton:
|
||||
# Get mouse position in widget coordinates
|
||||
mouse_pos = event.pos()
|
||||
|
||||
# Map mouse position to normalized device coordinates
|
||||
normalized_x = self.map_value_to_range(mouse_pos.x(), 0, self.width())
|
||||
normalized_y = self.map_value_to_range(mouse_pos.y(), 0, self.height())
|
||||
|
||||
def draw_lines_between_points(self, points):
|
||||
glBegin(GL_LINES)
|
||||
glColor3f(1.0, 1.0, 1.0) # Set line color to white
|
||||
num_points = len(points)
|
||||
for i in range(num_points - 1):
|
||||
x1, y1 = points[i]
|
||||
x2, y2 = points[i + 1]
|
||||
glVertex2f(x1, y1)
|
||||
glVertex2f(x2, y2)
|
||||
# Connect the last point to the first point
|
||||
x1, y1 = points[num_points - 1]
|
||||
x2, y2 = points[0]
|
||||
glVertex2f(x1, y1)
|
||||
glVertex2f(x2, y2)
|
||||
glEnd()
|
||||
|
||||
glPointSize(8)
|
||||
glBegin(GL_POINTS)
|
||||
glColor3f(1.0, 0.2, 1.0) # Set dot color to white
|
||||
for point in points:
|
||||
glVertex2f(point[0], point[1])
|
||||
glEnd()
|
||||
|
||||
def mousePressEvent(self, event):
|
||||
|
||||
if event.buttons() & Qt.MouseButton.RightButton:
|
||||
# Get mouse position in widget coordinates
|
||||
mouse_pos = event.pos()
|
||||
|
||||
# Map mouse position to normalized device coordinates
|
||||
normalized_x = self.map_value_to_range(mouse_pos.x(), 0, value_max=self.width(),range_min=-1, range_max=1)
|
||||
normalized_x = self.map_value_to_range(mouse_pos.x(), 0, value_max=self.width(),range_min=-2, range_max=2)
|
||||
normalized_y = self.map_value_to_range(mouse_pos.y(), 0, value_max=self.height(),range_min=-1, range_max=1)
|
||||
|
||||
self.startPos = [normalized_x, -normalized_y]
|
||||
|
||||
# Now you have the mouse position in normalized coordinates
|
||||
print("Right mouse button pressed - Mouse position (normalized):", normalized_x, normalized_y)
|
||||
def mouseReleaseEvent(self, event):
|
||||
if event.button() == Qt.MouseButton.RightButton:
|
||||
mouse_pos = event.pos()
|
||||
|
||||
"""def mouseReleaseEvent(self, event):
|
||||
print("release")
|
||||
# Map mouse position to normalized device coordinates
|
||||
normalized_x = self.map_value_to_range(mouse_pos.x(), 0, value_max=self.width(), range_min=-2, range_max=2)
|
||||
normalized_y = self.map_value_to_range(mouse_pos.y(), 0, value_max=self.height(), range_min=-1, range_max=1)
|
||||
|
||||
# Get mouse position in widget coordinates
|
||||
mouse_pos = event.pos()
|
||||
self.endPos = [normalized_x, -normalized_y]
|
||||
self.add_to_sketch(self.startPos)
|
||||
self.add_to_sketch(self.endPos)
|
||||
|
||||
# Map mouse position to normalized device coordinates
|
||||
normalized_x = self.map_value_to_range(mouse_pos.x(), 0, self.width())
|
||||
normalized_y = self.map_value_to_range(mouse_pos.y(), 0, self.height())
|
||||
self.update()
|
||||
print("releaseonly")
|
||||
# Now you have the mouse position in normalized coordinates
|
||||
print("Right mouse button pressed - Mouse position (normalized):", normalized_x, normalized_y)
|
||||
|
||||
self.endPos = [normalized_x, normalized_y]
|
||||
print(self.endPos)
|
||||
|
||||
# Now you have the mouse position in normalized coordinates
|
||||
print("Right mouse button rel - Mouse position (normalized):", normalized_x, normalized_y)
|
||||
#self.update()"""
|
||||
|
||||
def mouseMoveEvent(self, event):
|
||||
dx = event.x() - self.lastPos.x()
|
||||
dy = event.y() - self.lastPos.y()
|
||||
|
||||
if event.buttons() & Qt.MouseButton.LeftButton:
|
||||
if event.buttons() & Qt.MouseButton.LeftButton :
|
||||
self.xRot += 0.5 * dy
|
||||
self.yRot += 0.5 * dx
|
||||
self.update()
|
||||
self.lastPos = event.pos()
|
||||
self.lastPos = event.pos()
|
||||
self.update()
|
||||
|
||||
if event.buttons() & Qt.MouseButton.RightButton:
|
||||
mouse_pos = event.pos()
|
||||
def add_to_sketch(self, pos):
|
||||
self.sketch.append(pos)
|
||||
print(self.sketch)
|
||||
|
||||
# Map mouse position to normalized device coordinates
|
||||
normalized_x = self.map_value_to_range(mouse_pos.x(), 0, self.width())
|
||||
normalized_y = self.map_value_to_range(mouse_pos.y(), 0, self.height())
|
||||
|
||||
self.endPos = [normalized_x, -normalized_y]
|
||||
print(self.endPos)
|
||||
|
||||
# Now you have the mouse position in normalized coordinates
|
||||
print("Right mouse button rel - Mouse position (normalized):", normalized_x, normalized_y)
|
||||
self.update()
|
||||
def wheelEvent(self, event):
|
||||
delta = event.angleDelta().y()
|
||||
self.zoom += delta / 120
|
||||
|
Loading…
x
Reference in New Issue
Block a user