Browse Source

Add instantiate which immediately fixes the location of the new elements

Yentl Van Tendeloo 4 years ago
parent
commit
1ca6d44dd9
1 changed files with 17 additions and 22 deletions
  1. 17 22
      interface/graphical/main.py

+ 17 - 22
interface/graphical/main.py

@@ -71,6 +71,7 @@ class Window(object):
         self.canvas.bind("<Button-1>", self.left_clicked)
         self.canvas.bind("<B1-Motion>", self.left_drag)
         self.canvas.bind("<ButtonRelease-1>", self.left_released)
+        self.canvas.bind("<Button-2>", self.middle_clicked)
         self.canvas.bind("<Button-3>", self.right_clicked)
 
     def find_AS(self, x, y):
@@ -128,7 +129,7 @@ class Window(object):
             attr_assign(rendered_formula(self.selected_model.get(), self.selected_mapper.get()), self.AS_to_CS[self.current_element], "x", attrs["x"] + d_x)
             attr_assign(rendered_formula(self.selected_model.get(), self.selected_mapper.get()), self.AS_to_CS[self.current_element], "y", attrs["y"] + d_y)
 
-    def right_clicked(self, evt):
+    def middle_clicked(self, evt):
         # Middle clicked means we are modifying attributes of something
         x, y = evt.x, evt.y
 
@@ -259,26 +260,20 @@ class Window(object):
         mappers = transformation_between(type_model, MM_RENDERED)
         return mappers
 
-    def create_element(self, t):
-        def create_elem():
-            element_id = instantiate(self.selected_model.get(), t)
-            print("Created new element " + str(element_id))
-            elements = self.render_model()
-            for f in elements:
-                print("ELEM " + str(f))
-                if "__asid" in f:
-                    print("Check element " + str(f["__asid"]))
-                    if f["__asid"] == element_id:
-                        print("Found element!")
-                        attr_assign(rendered_formula(self.selected_model.get(), self.selected_mapper.get()), f["id"], "x", 100)
-                        attr_assign(rendered_formula(self.selected_model.get(), self.selected_mapper.get()), f["id"], "y", 100)
-
-
-            print("Rerender")
-            self.render_model()
-            print("DONE")
-
-        return create_elem
+    def right_clicked(self, evt):
+        element_id = instantiate(self.selected_model.get(), self.current_element)
+        elements = self.render_model()
+        for f in elements:
+            if "__asid" in f:
+                if f["__asid"] == element_id:
+                    attr_assign(rendered_formula(self.selected_model.get(), self.selected_mapper.get()), f["id"], "x", evt.x)
+                    attr_assign(rendered_formula(self.selected_model.get(), self.selected_mapper.get()), f["id"], "y", evt.y)
+        self.render_model()
+
+    def select_element(self, t):
+        def set_elem():
+            self.current_element = t
+        return set_elem
 
     def render_model(self):
         import json
@@ -298,7 +293,7 @@ class Window(object):
 
         self.mm_buttons = []
         for i, t in enumerate(available_types):
-            self.mm_buttons.append(Button(root, text=t, command=self.create_element(t)))
+            self.mm_buttons.append(Button(root, text=t, command=self.select_element(t)))
             self.mm_buttons[-1].grid(row=1, column=i)
 
     def instantiate_model(self):