瀏覽代碼

Fixed find algorithm to detect which element is clicked

Yentl Van Tendeloo 8 年之前
父節點
當前提交
1685cd533b
共有 1 個文件被更改,包括 12 次插入4 次删除
  1. 12 4
      interface/FSA/main.py

+ 12 - 4
interface/FSA/main.py

@@ -16,6 +16,7 @@ import time
 JUMP = 50
 MAX_WIDTH = 20 * JUMP
 MAX_HEIGHT = 20 * JUMP
+CLICK_TOLERANCE = 10
 address = "http://127.0.0.1:8001"
 username = "test"
 
@@ -219,7 +220,8 @@ class InterfaceCore():
             y = event.y
             self.mv.instantiate_block(str(name), "State")
             r = canvas.create_oval(lower(x), lower(y), upper(x), upper(y), fill="white")
-            b = (lower(x), lower(y), upper(x), upper(y), str(name))
+            b = (lower(x), lower(y), upper(x), upper(y), str(name), "NODE")
+            self.drawn.add(b)
             self.refs[str(name)] = [r]
             name += 1
 
@@ -231,13 +233,19 @@ class InterfaceCore():
         matches = []
         for e in self.drawn:
             x1, y1, x2, y2, x0, y0 = e[0], e[1], e[2], e[3], x, y
-            distance = abs((y2 - y1) * x0 - (x2 - x1) * y0 + x2 * y1 - y2 * x1) / sqrt((y2 - y1) ** 2 + (x2 - x1) ** 2)
+            if e[5] == "LINK":
+                distance = abs((y2 - y1) * x0 - (x2 - x1) * y0 + x2 * y1 - y2 * x1) / sqrt((y2 - y1) ** 2 + (x2 - x1) ** 2) - CLICK_TOLERANCE
+            elif e[5] == "NODE":
+                if x0 <= x2 and x0 > x1 and y0 <= y2 and y0 > y1:
+                        distance = -float("inf")
+                else:
+                    distance = float("inf")
             matches.append((distance, e[4]))
 
         if matches:
             minimal_distance, name = sorted(matches)[0]
             print("Distance to " + name + " is " + str(minimal_distance))
-            if minimal_distance < 10:
+            if minimal_distance <= 0:
                 return name
 
         return None
@@ -252,7 +260,7 @@ class InterfaceCore():
             global name
             self.mv.instantiate_link(str(name), "Transition", source, target)
             self.refs[str(name)] = [canvas.create_line(start[0], start[1], end[0], end[1], fill="black", arrow=LAST)]
-            self.drawn.add([start[0], start[1], end[0], end[1], str(name)])
+            self.drawn.add((start[0], start[1], end[0], end[1], str(name), "LINK"))
             name += 1
 
 core = InterfaceCore()