forked from cheeze36/harnessIT
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHarnessDrawFrame.py
More file actions
179 lines (142 loc) · 7.23 KB
/
Copy pathHarnessDrawFrame.py
File metadata and controls
179 lines (142 loc) · 7.23 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
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
174
175
176
177
178
179
"""
This module defines the drawing frame for the HarnessIT application.
"""
import tkinter
import pygame
import tkinter as tk
import os
import HarnessComponents
import HarnessITUtils
class DrawFrame():
"""
The drawing frame for the HarnessIT application.
"""
def __init__(self,parent,app ):#= tk.Tk()):
"""
Initializes the DrawFrame.
Args:
parent: The parent widget.
app: The main application instance.
"""
self.app = app
self.parent = parent
self.frame = tk.Frame(self.parent, width =self.parent.winfo_width() - 100, height = self.parent.winfo_height() - 120)
os.environ['SDL_WINDOWID'] = str(self.frame.winfo_id())
os.environ['SDL_VIDEODRIVER'] = 'windib'
self.screen = pygame.display.set_mode()
pygame.display.init()
pygame.font.init()
self.font = pygame.font.SysFont(None, 24)
self.selected = []
self.connectors = []
self.wires =[]
self.zoom_level = 1.0
self.view_offset = [0, 0]
def world_to_screen(self, x, y):
"""Converts world coordinates to screen coordinates."""
screen_x = (x - self.view_offset[0]) * self.zoom_level
screen_y = (y - self.view_offset[1]) * self.zoom_level
return int(screen_x), int(screen_y)
def screen_to_world(self, x, y):
"""Converts screen coordinates to world coordinates."""
world_x = x / self.zoom_level + self.view_offset[0]
world_y = y / self.zoom_level + self.view_offset[1]
return int(world_x), int(world_y)
def snap_to_grid(self, x, y):
"""Snaps the given coordinates to the nearest grid intersection."""
grid_size = self.app.grid_size
snapped_x = round(x / grid_size) * grid_size
snapped_y = round(y / grid_size) * grid_size
return snapped_x, snapped_y
def zoom(self, event):
"""Zooms the view in or out."""
mouse_pos_before_zoom = self.screen_to_world(event.x, event.y)
if event.delta > 0:
self.zoom_level *= 1.1
else:
self.zoom_level /= 1.1
self.zoom_level = max(0.1, min(self.zoom_level, 5.0))
mouse_pos_after_zoom = self.screen_to_world(event.x, event.y)
self.view_offset[0] += mouse_pos_before_zoom[0] - mouse_pos_after_zoom[0]
self.view_offset[1] += mouse_pos_before_zoom[1] - mouse_pos_after_zoom[1]
def draw(self):
"""
Draws the harness components on the screen.
"""
self.screen.fill(pygame.Color(100, 100, 100))
# Draw grid
if self.app.grid_visible.get():
grid_size = self.app.grid_size
start_x, start_y = self.screen_to_world(0, 0)
end_x, end_y = self.screen_to_world(self.screen.get_width(), self.screen.get_height())
for x in range(start_x - (start_x % grid_size), end_x, grid_size):
start_pos = self.world_to_screen(x, start_y)
end_pos = self.world_to_screen(x, end_y)
pygame.draw.line(self.screen, (155, 155, 155), start_pos, end_pos, 1)
for y in range(start_y - (start_y % grid_size), end_y, grid_size):
start_pos = self.world_to_screen(start_x, y)
end_pos = self.world_to_screen(end_x, y)
pygame.draw.line(self.screen, (155, 155, 155), start_pos, end_pos, 1)
for s in self.selected:
if isinstance(s,HarnessComponents.Connector):
screen_pos = self.world_to_screen(s.rect.x, s.rect.y)
scaled_image = pygame.transform.scale(s.image, (int(s.rect.width * self.zoom_level), int(s.rect.height * self.zoom_level)))
HarnessITUtils.outline_image(scaled_image,self.screen,screen_pos)
elif isinstance(s,HarnessComponents.Node):
screen_pos = self.world_to_screen(s.rect.centerx, s.rect.centery)
pygame.draw.circle(self.screen, (0, 0, 0), screen_pos, 7)
pygame.draw.circle(self.screen, (200, 200, 200), screen_pos, 5)
for c in self.connectors:
screen_pos = self.world_to_screen(c.rect.x, c.rect.y)
scaled_image = pygame.transform.scale(c.image, (int(c.rect.width * self.zoom_level), int(c.rect.height * self.zoom_level)))
self.screen.blit(scaled_image, screen_pos)
if self.app.view_connector_names.get():
text = self.font.render(c.name, True, (0, 0, 0))
text_rect = text.get_rect(center=self.world_to_screen(c.rect.centerx, c.rect.centery - 20))
self.screen.blit(text, text_rect)
if self.app.state == "wire":
for n in c.nodes:
screen_pos = self.world_to_screen(n.rect.centerx, n.rect.centery)
if n in self.app.wirenodes:
pygame.draw.circle(self.screen, (255, 255, 255), screen_pos, 7)
pygame.draw.circle(self.screen, (100, 155, 55), screen_pos, 5)
else:
pygame.draw.circle(self.screen, (255, 255, 255), screen_pos, 7)
pygame.draw.circle(self.screen,(0,155,255), screen_pos,5)
if self.app.view_pin_numbers.get():
for n in c.nodes:
screen_pos = self.world_to_screen(n.rect.centerx, n.rect.centery)
pin_text = self.font.render(str(n.get_display_pin()), True, (0, 0, 0))
text_rect = pin_text.get_rect(center=screen_pos)
self.screen.blit(pin_text, text_rect)
for w in self.wires:
color = HarnessITUtils.COLORS[w.get_color()]
gauge = HarnessITUtils.GAUGE[w.get_gauge()]
for i in range(len(w.nodes) - 1):
start_pos = self.world_to_screen(w.nodes[i].rect.centerx, w.nodes[i].rect.centery)
end_pos = self.world_to_screen(w.nodes[i+1].rect.centerx, w.nodes[i+1].rect.centery)
pygame.draw.line(self.screen, color, start_pos, end_pos, gauge)
if self.app.view_wire_names.get():
midpoint_x = (w.nodes[0].rect.centerx + w.nodes[-1].rect.centerx) / 2
midpoint_y = (w.nodes[0].rect.centery + w.nodes[-1].rect.centery) / 2
screen_pos = self.world_to_screen(midpoint_x, midpoint_y)
text = self.font.render(w.name, True, (0, 0, 0))
text_rect = text.get_rect(center=screen_pos)
self.screen.blit(text, text_rect)
if self.app.state == "wire" or self.app.state == "selecting":
for node in w.nodes:
if not isinstance(node.parent, HarnessComponents.Connector):
screen_pos = self.world_to_screen(node.rect.centerx, node.rect.centery)
pygame.draw.circle(self.screen, (200, 200, 200), screen_pos, 5)
def update(self):
"""
Updates the display and the harness components.
"""
pygame.display.update()
for c in self.connectors:
c.update()
def resize(self):
"""
Resizes the drawing frame.
"""
self.frame.config(width =self.parent.winfo_width() - 25, height = self.parent.winfo_height() - 25)