changeset 135:c9b6685ebebf

Add hack'ish 'close polygon' option
author Neil Muller <drnlmuller@gmail.com>
date Mon, 02 Sep 2013 18:19:57 +0200
parents 2e1059b1a247
children 0280ee006d95
files tools/area_editor.py
diffstat 1 files changed, 54 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/tools/area_editor.py	Mon Sep 02 17:57:06 2013 +0200
+++ b/tools/area_editor.py	Mon Sep 02 18:19:57 2013 +0200
@@ -89,6 +89,42 @@
         if index in self.polygons and len(self.polygons[index]) > 0:
             self.polygons[index].pop()
 
+    def close_poly(self, index):
+        """Attempts to close the current polygon.
+
+           We allow a small additional step to close the polygon, but
+           it's limited as it's a magic point addition"""
+        if len(self.polygons[index]) < 2:
+            # Too small
+            return False
+        first = self.polygons[index][0]
+        last = self.polygons[index][-1]
+        print first, last
+        print self.fix_angle(index, self.point_to_pygame(first))
+        if self.fix_angle(index, self.point_to_pygame(first)) == first:
+            self.add_point(index, self.point_to_pygame(first))
+            return True
+        candidates = [(first[0] + 10 * i, first[1]) for
+                      i in (-3, -2, -1, 1, 2, 3)]
+        candidates.extend([(first[0], first[1] + 10 * i) for
+                          i in (-3, -2, -1, 1, 2, 3)])
+        candidates.extend([(first[0] + 10 * i, first[1] + 10 * i) for
+                          i in (-3, -2, -1, 1, 2, 3)])
+        candidates.extend([(first[0] + 10 * i, first[1] - 10 * i) for
+                          i in (-3, -2, -1, 1, 2, 3)])
+        min_dist = 99999
+        poss = None
+        for cand in candidates:
+            if self.fix_angle(index, self.point_to_pygame(cand)) == cand:
+                dist = (first[0] - cand[0]) ** 2 + (first[1] - cand[1]) ** 2
+                if dist < min_dist:
+                    poss = cand
+        if poss is not None:
+            self.add_point(index, self.point_to_pygame(poss))
+            self.add_point(index, self.point_to_pygame(first))
+            return True
+        return False
+
     def draw(self, surface, topleft, mouse_pos, mouse_poly, filled):
         self._draw_background(True)
         # Draw polygons as needed for the editor
@@ -169,6 +205,8 @@
             self.level.delete_point(self.cur_poly)
         elif ev.key == pgl.K_f:
             self.set_filled()
+        elif ev.key == pgl.K_c:
+            self.close_poly()
 
     def set_filled(self):
         if level.all_closed():
@@ -189,6 +227,15 @@
             self.level.add_point(self.cur_poly,
                                  self._level_coordinates(ev.pos))
 
+    def close_poly(self):
+        if self.cur_poly is None:
+            return
+        if self.level.close_poly(self.cur_poly):
+            alert("Successfully closed the polygon")
+            self.change_poly(None)
+        else:
+            alert("Failed to close the polygon")
+
 
 class PolyButton(Button):
     """Button for coosing the correct polygon"""
@@ -248,6 +295,13 @@
         self.add(save_but)
         y += MENU_BUTTON_HEIGHT + MENU_PAD
 
+        close_poly_but = Button('Close Polygon',
+                                action=self.level_widget.close_poly)
+        close_poly_but.rect = button_rect.copy()
+        close_poly_but.rect.move_ip(MENU_LEFT, y)
+        self.add(close_poly_but)
+        y += MENU_BUTTON_HEIGHT + MENU_PAD
+
         quit_but = Button('Quit', action=self.quit)
         quit_but.rect = button_rect.copy()
         quit_but.rect.move_ip(MENU_LEFT, y)