Changeset 135:c9b6685ebebf


Ignore:
Timestamp:
Sep 2, 2013, 4:19:57 PM (7 years ago)
Author:
Neil Muller <drnlmuller@…>
Branch:
default
rebase_source:
10cdb285f9c291a4cd0e4e643cc1d04f949b125e
Message:

Add hack'ish 'close polygon' option

File:
1 edited

Legend:

Unmodified
Added
Removed
  • tools/area_editor.py

    r134 r135  
    9090            self.polygons[index].pop()
    9191
     92    def close_poly(self, index):
     93        """Attempts to close the current polygon.
     94
     95           We allow a small additional step to close the polygon, but
     96           it's limited as it's a magic point addition"""
     97        if len(self.polygons[index]) < 2:
     98            # Too small
     99            return False
     100        first = self.polygons[index][0]
     101        last = self.polygons[index][-1]
     102        print first, last
     103        print self.fix_angle(index, self.point_to_pygame(first))
     104        if self.fix_angle(index, self.point_to_pygame(first)) == first:
     105            self.add_point(index, self.point_to_pygame(first))
     106            return True
     107        candidates = [(first[0] + 10 * i, first[1]) for
     108                      i in (-3, -2, -1, 1, 2, 3)]
     109        candidates.extend([(first[0], first[1] + 10 * i) for
     110                          i in (-3, -2, -1, 1, 2, 3)])
     111        candidates.extend([(first[0] + 10 * i, first[1] + 10 * i) for
     112                          i in (-3, -2, -1, 1, 2, 3)])
     113        candidates.extend([(first[0] + 10 * i, first[1] - 10 * i) for
     114                          i in (-3, -2, -1, 1, 2, 3)])
     115        min_dist = 99999
     116        poss = None
     117        for cand in candidates:
     118            if self.fix_angle(index, self.point_to_pygame(cand)) == cand:
     119                dist = (first[0] - cand[0]) ** 2 + (first[1] - cand[1]) ** 2
     120                if dist < min_dist:
     121                    poss = cand
     122        if poss is not None:
     123            self.add_point(index, self.point_to_pygame(poss))
     124            self.add_point(index, self.point_to_pygame(first))
     125            return True
     126        return False
     127
    92128    def draw(self, surface, topleft, mouse_pos, mouse_poly, filled):
    93129        self._draw_background(True)
     
    170206        elif ev.key == pgl.K_f:
    171207            self.set_filled()
     208        elif ev.key == pgl.K_c:
     209            self.close_poly()
    172210
    173211    def set_filled(self):
     
    189227            self.level.add_point(self.cur_poly,
    190228                                 self._level_coordinates(ev.pos))
     229
     230    def close_poly(self):
     231        if self.cur_poly is None:
     232            return
     233        if self.level.close_poly(self.cur_poly):
     234            alert("Successfully closed the polygon")
     235            self.change_poly(None)
     236        else:
     237            alert("Failed to close the polygon")
    191238
    192239
     
    247294        save_but.rect.move_ip(MENU_LEFT, y)
    248295        self.add(save_but)
     296        y += MENU_BUTTON_HEIGHT + MENU_PAD
     297
     298        close_poly_but = Button('Close Polygon',
     299                                action=self.level_widget.close_poly)
     300        close_poly_but.rect = button_rect.copy()
     301        close_poly_but.rect.move_ip(MENU_LEFT, y)
     302        self.add(close_poly_but)
    249303        y += MENU_BUTTON_HEIGHT + MENU_PAD
    250304
Note: See TracChangeset for help on using the changeset viewer.