Skip to content

Commit 27269db

Browse files
committed
Added feature where zoom level determines scale. Also fixes a bug where grid moves out of place when other objects are in screen when scale changes.
1 parent 690e7a4 commit 27269db

File tree

1 file changed

+65
-11
lines changed

1 file changed

+65
-11
lines changed

graphics/graphics_grid.py

Lines changed: 65 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,13 @@ def __create_grid_objects(self):
7979
round(self.__scene.center.y, 2), \
8080
round(self.__scene.center.z, 2)
8181
self.__focal_point = [x_origin, y_origin, z_origin]
82+
# Convert focal point for 2D rendering. Puts focus point in centre of the view
83+
if not self.__is_3d:
84+
self.__focal_point = [val - int(self.__num_squares / 2) for val in self.__focal_point]
85+
x_origin = self.__focal_point[0]
86+
y_origin = self.__focal_point[1]
87+
z_origin = 0
88+
self.__focal_point[2] = z_origin
8289
else:
8390
x_origin, y_origin, z_origin = self.__focal_point[0], \
8491
self.__focal_point[1], \
@@ -117,6 +124,11 @@ def __create_grid_objects(self):
117124
y_coords = arange(min_y_coord, max_y_coord + self.__scale, self.__scale)
118125
z_coords = arange(min_z_coord, max_z_coord + self.__scale, self.__scale)
119126

127+
# Compound origins are in the middle of the bounding boxes. Thus new pos will be between max and min.
128+
x_middle = (max_x_coord + min_x_coord) / 2
129+
y_middle = (max_y_coord + min_y_coord) / 2
130+
z_middle = (max_z_coord + min_z_coord) / 2
131+
120132
# XZ plane
121133
for x_point in x_coords:
122134
# Draw a line across for each x coord, along the same y-axis, from min to max z coord
@@ -166,9 +178,23 @@ def __create_grid_objects(self):
166178
))
167179

168180
# Compound the lines together into respective objects
169-
xz_plane = compound(xz_lines)
170-
xy_plane = compound(xy_lines)
171-
yz_plane = compound(yz_lines)
181+
# XY Plane
182+
if camera_axes.z < 0:
183+
xy_plane = compound(xy_lines, origin=vector(x_middle, y_middle, min_z_coord))
184+
else:
185+
xy_plane = compound(xy_lines, origin=vector(x_middle, y_middle, max_z_coord))
186+
187+
# XZ Plane
188+
if camera_axes.y < 0:
189+
xz_plane = compound(xz_lines, origin=vector(x_middle, min_y_coord, z_middle))
190+
else:
191+
xz_plane = compound(xz_lines, origin=vector(x_middle, max_y_coord, z_middle))
192+
193+
# YZ Plane
194+
if camera_axes.x < 0:
195+
yz_plane = compound(yz_lines, origin=vector(min_x_coord, y_middle, z_middle))
196+
else:
197+
yz_plane = compound(yz_lines, origin=vector(max_x_coord, y_middle, z_middle))
172198

173199
# Combine all into one list
174200
grid = [None, None, None]
@@ -235,12 +261,19 @@ def __move_grid_objects(self):
235261
y_middle = (max_y_coord + min_y_coord) / 2
236262
z_middle = (max_z_coord + min_z_coord) / 2
237263

264+
print("x", min_x_coord, x_middle, max_x_coord)
265+
print("y", min_y_coord, y_middle, max_y_coord)
266+
print("z", min_z_coord, z_middle, max_z_coord)
267+
print("pos", self.grid_object[self.__planes_idx][self.__xy_plane_idx])
268+
238269
# XY Plane
239270
if camera_axes.z < 0:
240271
self.grid_object[self.__planes_idx][self.__xy_plane_idx].pos = vector(x_middle, y_middle, min_z_coord)
241272
else:
242273
self.grid_object[self.__planes_idx][self.__xy_plane_idx].pos = vector(x_middle, y_middle, max_z_coord)
243274

275+
print("pos", self.grid_object[self.__planes_idx][self.__xy_plane_idx])
276+
244277
# XZ Plane
245278
if camera_axes.y < 0:
246279
self.grid_object[self.__planes_idx][self.__xz_plane_idx].pos = vector(x_middle, min_y_coord, z_middle)
@@ -258,18 +291,23 @@ def update_grid(self):
258291
Update the grid axes and numbers if the camera position/rotation has changed.
259292
"""
260293
# Obtain the new camera settings
261-
new_camera_pos = self.__scene.camera.pos
262-
new_camera_axes = self.__scene.camera.axis
294+
new_camera_pos = vector(self.__scene.camera.pos)
295+
new_camera_axes = vector(self.__scene.camera.axis)
296+
297+
old_camera_pos = vector(self.camera_pos)
298+
old_camera_axes = vector(self.camera_axes)
299+
300+
# Update old positions
301+
self.camera_pos = new_camera_pos
302+
self.camera_axes = new_camera_axes
263303

264-
old_camera_pos = self.camera_pos
265-
old_camera_axes = self.camera_axes
304+
distance_from_center = mag(self.__scene.center - self.__scene.camera.pos)
305+
new_scale = round(distance_from_center / 30.0, 1)
306+
if not new_scale == self.__scale:
307+
self.set_scale(new_scale)
266308

267309
# If camera is different to previous: update
268310
if (not new_camera_axes.equals(old_camera_axes)) or (not new_camera_pos.equals(old_camera_pos)):
269-
# Update old positions
270-
self.camera_pos = new_camera_pos
271-
self.camera_axes = new_camera_axes
272-
273311
# Update grid
274312
self.__move_grid_objects()
275313
update_grid_numbers(self.__focal_point,
@@ -332,6 +370,22 @@ def set_relative(self, is_relative):
332370
self.__relative_cam = is_relative
333371
self.update_grid()
334372

373+
def set_scale(self, value):
374+
"""
375+
:param value: The value to set the scale to
376+
:type value: `float`
377+
"""
378+
value = max(min(value, 100), 0.1) # Between 0.1 and 100
379+
self.__scale = value
380+
# Turn off grid then delete
381+
for plane in self.grid_object[self.__planes_idx]:
382+
plane.visible = False
383+
for text in self.grid_object[self.__labels_idx]:
384+
text.visible = False
385+
386+
self.grid_object = [[], []]
387+
self.__init_grid()
388+
335389

336390
def create_line(pos1, pos2, scene, colour=None, thickness=0.01):
337391
"""

0 commit comments

Comments
 (0)