2d drawing test
This commit is contained in:
parent
8db6743dcd
commit
fdc3948f8c
@ -14,7 +14,7 @@ class OpenGLWidget(QOpenGLWidget):
|
|||||||
self.xRot = 0
|
self.xRot = 0
|
||||||
self.yRot = 0
|
self.yRot = 0
|
||||||
self.zoom = -10.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):
|
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.
|
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
|
# Draw the rectangle if start and end positions are defined
|
||||||
self.draw_area()
|
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):
|
def draw_area(self):
|
||||||
@ -81,19 +82,20 @@ class OpenGLWidget(QOpenGLWidget):
|
|||||||
# Draw vertical lines
|
# Draw vertical lines
|
||||||
glBegin(GL_LINES)
|
glBegin(GL_LINES)
|
||||||
for x in range(0, self.width(), 20):
|
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) # Start from y = -1
|
||||||
glVertex2f(x_ndc, 1) # End at y = 1
|
glVertex2f(x_ndc, 1) # End at y = 1
|
||||||
glEnd()
|
|
||||||
|
|
||||||
# Draw horizontal lines
|
# Draw horizontal lines
|
||||||
glBegin(GL_LINES)
|
|
||||||
for y in range(0, self.height(), 20):
|
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)
|
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(-2, y_ndc) # Start from x = -1
|
||||||
glVertex2f(1, y_ndc) # End at x = 1
|
glVertex2f(2, y_ndc) # End at x = 1
|
||||||
glEnd()
|
glEnd()
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
def draw_line(self, startPos, endPos):
|
def draw_line(self, startPos, endPos):
|
||||||
# Get normalized mouse coordinates
|
# Get normalized mouse coordinates
|
||||||
print("start", startPos)
|
print("start", startPos)
|
||||||
@ -114,68 +116,77 @@ class OpenGLWidget(QOpenGLWidget):
|
|||||||
glVertex2f(x2, y2)
|
glVertex2f(x2, y2)
|
||||||
glEnd()
|
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())
|
def draw_lines_between_points(self, points):
|
||||||
normalized_y = self.map_value_to_range(mouse_pos.y(), 0, self.height())
|
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:
|
if event.buttons() & Qt.MouseButton.RightButton:
|
||||||
# Get mouse position in widget coordinates
|
# Get mouse position in widget coordinates
|
||||||
mouse_pos = event.pos()
|
mouse_pos = event.pos()
|
||||||
|
|
||||||
# Map mouse position to normalized device coordinates
|
# 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)
|
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]
|
self.startPos = [normalized_x, -normalized_y]
|
||||||
|
|
||||||
# Now you have the mouse position in normalized coordinates
|
# Now you have the mouse position in normalized coordinates
|
||||||
print("Right mouse button pressed - Mouse position (normalized):", normalized_x, normalized_y)
|
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):
|
# Map mouse position to normalized device coordinates
|
||||||
print("release")
|
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
|
self.endPos = [normalized_x, -normalized_y]
|
||||||
mouse_pos = event.pos()
|
self.add_to_sketch(self.startPos)
|
||||||
|
self.add_to_sketch(self.endPos)
|
||||||
|
|
||||||
# Map mouse position to normalized device coordinates
|
self.update()
|
||||||
normalized_x = self.map_value_to_range(mouse_pos.x(), 0, self.width())
|
print("releaseonly")
|
||||||
normalized_y = self.map_value_to_range(mouse_pos.y(), 0, self.height())
|
# 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):
|
def mouseMoveEvent(self, event):
|
||||||
dx = event.x() - self.lastPos.x()
|
dx = event.x() - self.lastPos.x()
|
||||||
dy = event.y() - self.lastPos.y()
|
dy = event.y() - self.lastPos.y()
|
||||||
|
|
||||||
if event.buttons() & Qt.MouseButton.LeftButton:
|
if event.buttons() & Qt.MouseButton.LeftButton :
|
||||||
self.xRot += 0.5 * dy
|
self.xRot += 0.5 * dy
|
||||||
self.yRot += 0.5 * dx
|
self.yRot += 0.5 * dx
|
||||||
self.update()
|
self.lastPos = event.pos()
|
||||||
self.lastPos = event.pos()
|
self.update()
|
||||||
|
|
||||||
if event.buttons() & Qt.MouseButton.RightButton:
|
def add_to_sketch(self, pos):
|
||||||
mouse_pos = event.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):
|
def wheelEvent(self, event):
|
||||||
delta = event.angleDelta().y()
|
delta = event.angleDelta().y()
|
||||||
self.zoom += delta / 120
|
self.zoom += delta / 120
|
||||||
|
Loading…
x
Reference in New Issue
Block a user