Changeset 424:a64d894aa1bd


Ignore:
Timestamp:
09/07/13 11:30:06 (8 years ago)
Author:
Simon Cross <hodgestar@…>
Branch:
default
Children:
425:1a85044f81a8, 427:3ee839f227ad
Phase:
public
Rebase:
34356163653232333261643461353031316565343939306434383437336635663532636438643030
Message:

Actually snap to grid.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • tools/area_editor.py

    r423 r424  
    7474        self._move_poly = None
    7575
    76     def round_point(self, pos):
    77         return (10 * (pos[0] // 10), 10 * (pos[1] // 10))
    78 
    7976    def load(self, space):
    8077        super(EditorLevel, self).load(space)
     
    9188        self.polygons.setdefault(poly_index, [])
    9289        if not self.polygons[poly_index]:
    93             point = self.point_to_pymunk(self.round_point(pos))
     90            point = self.point_to_pymunk(pos)
    9491            self.polygons[poly_index].append(point)
    9592        else:
    96             add_pos = self.fix_poly_angle(poly_index, pos)
     93            add_pos = self.point_to_pymunk(pos)
    9794            self.polygons[poly_index].append(add_pos)
    98 
    99     def _fix_angle(self, point1, pos):
    100         # We want the line (point1 to pos) to be an angle of
    101         # 0, 45, 90, 135, 180, 225, 270, 305
    102         # However, we only need to consider half the circle
    103         # This is a hack to approximate the right thing
    104         pos0 = (pos[0], point1[1])
    105         pos90 = (point1[0], pos[1])
    106         dist = max(abs(point1[0] - pos[0]), abs(point1[1] - pos[1]))
    107         pos45 = (point1[0] + dist, point1[1] + dist)
    108         pos135 = (point1[0] + dist, point1[1] - dist)
    109         pos225 = (point1[0] - dist, point1[1] - dist)
    110         pos305 = (point1[0] - dist, point1[1] + dist)
    111         min_dist = 9999999
    112         new_pos = point1
    113         for cand in [pos0, pos90, pos45, pos135, pos225, pos305]:
    114             dist = distance(pos, cand)
    115             if dist < min_dist:
    116                 new_pos = cand
    117                 min_dist = dist
    118         return self.point_to_pymunk(new_pos)
    119 
    120     def fix_line_angle(self, start_pos, pos):
    121         start_pos = self.round_point(start_pos)
    122         pos = self.round_point(pos)
    123         return self._fix_angle(start_pos, pos)
    124 
    125     def fix_poly_angle(self, index, pos):
    126         # Last point
    127         point1 = self.point_to_pygame(self.polygons[index][-1])
    128         pos = self.round_point(pos)
    129         return self._fix_angle(point1, pos)
    13095
    13196    def delete_point(self, index):
     
    142107            return False
    143108        first = self.polygons[index][0]
    144         if self.fix_poly_angle(index, self.point_to_pygame(first)) == first:
    145             self.add_point(index, self.point_to_pygame(first))
    146             return True
    147         candidates = [(first[0] + 10 * i, first[1]) for
    148                       i in (-3, -2, -1, 1, 2, 3)]
    149         candidates.extend([(first[0], first[1] + 10 * i) for
    150                           i in (-3, -2, -1, 1, 2, 3)])
    151         candidates.extend([(first[0] + 10 * i, first[1] + 10 * i) for
    152                           i in (-3, -2, -1, 1, 2, 3)])
    153         candidates.extend([(first[0] + 10 * i, first[1] - 10 * i) for
    154                           i in (-3, -2, -1, 1, 2, 3)])
    155         min_dist = 99999
    156         poss = None
    157         for cand in candidates:
    158             if self.fix_poly_angle(index, self.point_to_pygame(cand)) == cand:
    159                 dist = distance(first, cand)
    160                 if dist < min_dist:
    161                     poss = cand
    162         if poss is not None:
    163             self.add_point(index, self.point_to_pygame(poss))
    164             self.add_point(index, self.point_to_pygame(first))
    165             return True
    166         return False
     109        self.add_point(index, self.point_to_pygame(first))
     110        return True
    167111
    168112    def add_line(self, start_pos, end_pos):
    169         endpoint = self.fix_line_angle(start_pos, end_pos)
    170         startpoint = self.point_to_pymunk(self.round_point(start_pos))
     113        startpoint = self.point_to_pymunk(start_pos)
     114        endpoint = self.point_to_pymunk(end_pos)
    171115        self.lines.append([startpoint, endpoint])
    172116
     
    189133                pygame.draw.lines(self._surface, color, False, pointlist, 2)
    190134            if index == mouse_poly and mouse_pos:
    191                 endpoint = self.fix_poly_angle(index, mouse_pos)
     135                endpoint = self.point_to_pymunk(mouse_pos)
    192136                pygame.draw.line(self._surface, color,
    193137                                 self.point_to_pygame(polygon[-1]),
     
    203147            pygame.draw.lines(self._surface, LINE_COLOR, False, pointlist, 2)
    204148        if draw_cand_line and start_pos and mouse_pos:
    205             endpoint = self.fix_line_angle(start_pos, mouse_pos)
    206             pointlist = [self.round_point(start_pos),
     149            endpoint = self.snap_to_grid(mouse_pos)
     150            pointlist = [start_pos,
    207151                         self.point_to_pygame(endpoint)]
    208152            pygame.draw.lines(self._surface, LINE_COLOR, False, pointlist, 1)
     
    484428        self.grid_size = max(1, self.grid_size + amount)
    485429
     430    def snap_to_grid(self, pos):
     431        x = pos[0] - (pos[0] % self.grid_size)
     432        y = pos[1] - (pos[1] % self.grid_size)
     433        return (x, y)
     434
    486435    def set_objects(self, value):
    487436        if self._draw_objects != value:
     
    509458            # We have an active polygon
    510459            mouse_pos = self._level_coordinates(self.mouse_pos)
     460            mouse_pos = self.snap_to_grid(mouse_pos)
    511461        elif self._draw_lines:
    512462            # Interior wall mode
    513463            mouse_pos = self._level_coordinates(self.mouse_pos)
     464            mouse_pos = self.snap_to_grid(mouse_pos)
    514465        elif self._move_point_mode:
    515466            mouse_pos = self._level_coordinates(self.mouse_pos)
     467            mouse_pos = self.snap_to_grid(mouse_pos)
    516468        else:
    517469            mouse_pos = None
     
    606558    def mouse_down(self, ev):
    607559        corrected_pos = self._level_coordinates(ev.pos)
     560        snapped_pos = self.snap_to_grid(corrected_pos)
    608561        if self.sel_mode and ev.button == 1:
    609562            obj = self.level.find_obj_at_pos(corrected_pos)
     
    613566            if self._move_point:
    614567                # Place the current point
    615                 self.level.replace_vertex(self._move_point, corrected_pos)
     568                self.level.replace_vertex(self._move_point, snapped_pos)
    616569                self._move_point = None
    617570                self.invalidate()
    618571            else:
    619572                # find the current point
    620                 self._move_point = self.level.find_vertex(corrected_pos)
     573                self._move_point = self.level.find_vertex(snapped_pos)
    621574        elif ev.button == 1:
    622575            if self._draw_lines:
     
    624577                    self._start_pos = corrected_pos
    625578                else:
    626                     self.level.add_line(self._start_pos, corrected_pos)
     579                    self.level.add_line(self._start_pos, snapped_pos)
    627580                    self._start_pos = None
    628581            else:
     
    639592        elif self.cur_poly and ev.button == 1:
    640593            # Add a point
    641             self.level.add_point(self.cur_poly, corrected_pos)
     594            self.level.add_point(self.cur_poly, snapped_pos)
    642595        elif ev.button == 3:
    643596            self._mouse_drag = True
Note: See TracChangeset for help on using the changeset viewer.