From c029d9e8b62e1b818d31416de3abc148f65be0c0 Mon Sep 17 00:00:00 2001 From: Taylor Bockman Date: Mon, 19 Aug 2019 17:50:23 -0700 Subject: [PATCH] Move attributes to the point itself --- clusterview/points.py | 70 +++++++++++++++++++++++++++++++++---------------- main_window.py | 1 - tests/test_point.py | 7 ++++- tests/test_point_set.py | 24 +---------------- 4 files changed, 54 insertions(+), 48 deletions(-) diff --git a/clusterview/points.py b/clusterview/points.py index 662a79b..3d19d50 100644 --- a/clusterview/points.py +++ b/clusterview/points.py @@ -29,17 +29,14 @@ class Point: self.__viewport_width = viewport_width self.__viewport_height = viewport_height - half_point = floor(point_size / 2.0) + self.__calculate_hitbox() self.__check_window_bounds(x, y) self.__selected = False - self.__top_left_corner = (self.__x - half_point, - self.__y + half_point) + self.__attributes = [] - self.__bottom_right_corner = (self.__x + half_point, - self.__y - half_point) @property def x(self): return self.__x @@ -56,6 +53,26 @@ class Point: def selected(self): return self.__selected + @property + def attributes(self): + return self.__attributes + + def add_attribute(self, attr): + self.__attributes.append(attr) + + def __calculate_hitbox(self): + """ + Calculates the hit box for the point given the current + position (center) and the point size. + """ + half_point = floor(self.point_size / 2.0) + + self.__top_left_corner = (self.__x - half_point, + self.__y + half_point) + + self.__bottom_right_corner = (self.__x + half_point, + self.__y - half_point) + def __check_window_bounds(self, x, y): """ Simple window bound check that raises an exception when @@ -90,7 +107,11 @@ class Point: self.__x += dx self.__y += dy - + + # It's important to note as we move the point we need to + # make sure we are constantly updating it's hitbox. + self.__calculate_hitbox() + def __eq__(self, other): """ Override for class equality. @@ -115,7 +136,7 @@ class Point: s = " attribute_list - # dictionary. - l.attributes(p) - def test_remove_point_bounding_box(): """ This test checks the bounding box hit heuristic. @@ -73,18 +63,6 @@ def test_remove_point_bounding_box(): assert len(points) == 0 - with pytest.raises(KeyError): - # We expect a call to attributes on a removed - # point to raise a KeyError because it no - # longer exists in the point -> attribute_list - # dictionary. - l.attributes(p) - -def test_attributes_must_be_array_of_attributes(): - with pytest.raises(ValueError): - l = PointSet(8, 100, 100) - l.add_point(4, 4, attrs=[1,2,3,4,5]) - def test_clear_all_selected_points(): l = PointSet(8, 100, 100) l.add_point(4, 4)