Changeset 407:314ddad2d6d2


Ignore:
Timestamp:
Sep 7, 2013, 10:10:42 AM (7 years ago)
Author:
Neil Muller <drnlmuller@…>
Branch:
default
rebase_source:
6c7d875116fd63e806afed5fc4d1295e2d36bbba
Message:

Move points button

File:
1 edited

Legend:

Unmodified
Added
Removed
  • tools/area_editor.py

    r355 r407  
    5454    def __init__(self):
    5555        self.level_state = {}
     56        self.inventory = {}
     57
     58
     59def distance(tup1, tup2):
     60    return (tup1[0] - tup2[0]) ** 2 + (tup1[1] - tup2[1]) ** 2
    5661
    5762
     
    6570        # Lookup initiliasition info from the objects
    6671        self.lookup = {}
     72        self._move_poly = None
    6773
    6874    def round_point(self, pos):
     
    104110        new_pos = point1
    105111        for cand in [pos0, pos90, pos45, pos135, pos225, pos305]:
    106             dist = (pos[0] - cand[0]) ** 2 + (pos[1] - cand[1]) ** 2
     112            dist = distance(pos, cand)
    107113            if dist < min_dist:
    108114                new_pos = cand
     
    149155        for cand in candidates:
    150156            if self.fix_poly_angle(index, self.point_to_pygame(cand)) == cand:
    151                 dist = (first[0] - cand[0]) ** 2 + (first[1] - cand[1]) ** 2
     157                dist = distance(first, cand)
    152158                if dist < min_dist:
    153159                    poss = cand
     
    163169        self.lines.append([startpoint, endpoint])
    164170
    165     def draw(self, mouse_pos, mouse_poly, filled, draw_cand_line, start_pos):
     171    def draw(self, mouse_pos, mouse_poly, filled, draw_cand_line, start_pos,
     172             move_point_mode, move_point):
    166173        self._draw_background(True)
    167174        # Draw polygons as needed for the editor
     
    170177        for index, polygon in self.polygons.items():
    171178            color = POLY_COLORS[index]
    172             if len(polygon) > 1:
     179            if move_point_mode and index == self._move_poly:
     180                pointlist = [p for p in polygon]
     181                pointlist = [self.point_to_pygame(p) if p != move_point else
     182                             mouse_pos for p in pointlist]
     183                pygame.draw.lines(self._surface, color, False, pointlist, 2)
     184            elif len(polygon) > 1:
    173185                pointlist = [self.point_to_pygame(p) for p in polygon]
    174186                pygame.draw.lines(self._surface, color, False, pointlist, 2)
    175             if index == mouse_poly and mouse_pos:
     187            elif index == mouse_poly and mouse_pos:
    176188                endpoint = self.fix_poly_angle(index, mouse_pos)
    177189                pygame.draw.line(self._surface, color,
    178190                                 self.point_to_pygame(polygon[-1]),
    179191                                 self.point_to_pygame(endpoint))
     192        line_found = False  # Hack for sane behaviour if lines overlap
    180193        for line in self.lines:
    181194            pointlist = [self.point_to_pygame(p) for p in line]
     195            if move_point_mode and not self._move_poly and not line_found:
     196                if move_point in line:
     197                    line_found = True
     198                    pointlist.remove(self.point_to_pygame(move_point))
     199                    pointlist.append(mouse_pos)
    182200            pygame.draw.lines(self._surface, LINE_COLOR, False, pointlist, 2)
    183201        if draw_cand_line and start_pos and mouse_pos:
     
    236254                return obj
    237255        return None
     256
     257    def find_vertex(self, mouse_pos):
     258        # search for vertexes closest to where we've killed
     259        mindist = 400
     260        move_point = None
     261        search_point = self.point_to_pymunk(mouse_pos)
     262        for index, polygon in self.polygons.items():
     263            for point in polygon:
     264                dist = distance(point, search_point)
     265                if dist < mindist:
     266                    mindist = dist
     267                    move_point = point
     268                    self._move_poly = index
     269        # Also check lines
     270        for line in self.lines:
     271            for point in line:
     272                dist = distance(point, search_point)
     273                if dist < mindist:
     274                    mindist = dist
     275                    move_point = point
     276                    self._move_poly = None
     277        return move_point
     278
     279    def replace_vertex(self, old_point, new_point):
     280        new_point = self.point_to_pymunk(new_point)
     281        if self._move_poly:
     282            new_polygon = [p if p != old_point else new_point for p in
     283                           self.polygons[self._move_poly]]
     284            self.polygons[self._move_poly] = new_polygon
     285        else:
     286            for line in self.lines:
     287                if old_point in line:
     288                    line.remove(old_point)
     289                    line.append(new_point)
     290                    break
    238291
    239292
     
    400453        self._start_pos = None
    401454        self._parent = parent
     455        self._move_point_mode = False
     456        self._move_point = False
    402457
    403458    def _level_coordinates(self, pos):
     
    437492            # Interior wall mode
    438493            mouse_pos = self._level_coordinates(self.mouse_pos)
     494        elif self._move_point_mode:
     495            mouse_pos = self._level_coordinates(self.mouse_pos)
    439496        else:
    440497            mouse_pos = None
    441498        level_surface = level.draw(mouse_pos, self.cur_poly, self.filled_mode,
    442                                    self._draw_lines, self._start_pos)
     499                                   self._draw_lines, self._start_pos,
     500                                   self._move_point_mode, self._move_point)
    443501        if self._draw_objects:
    444502            for thing in self.level.drawables:
     
    465523        self.filled_mode = False
    466524        self._start_pos = None
     525        self._move_point_mode = False
    467526
    468527    def key_down(self, ev):
     
    486545            self.close_poly()
    487546
     547    def set_move_mode(self):
     548        self._draw_lines = False
     549        self._move_point_mode = True
     550        self.filled_mode = False
     551        self._parent.reset_lit_buttons()
     552        self._move_point = None
     553
    488554    def set_filled(self):
    489555        closed, _ = self.level.all_closed()
     
    493559            self.filled_mode = True
    494560            self._draw_lines = False
     561            self._move_point_mode = False
    495562        else:
    496563            alert('Not all polygons closed, so not filling')
     
    499566        old_pos = self.mouse_pos
    500567        self.mouse_pos = ev.pos
    501         if old_pos != self.mouse_pos and (self.cur_poly or self._draw_lines):
     568        if old_pos != self.mouse_pos and (self.cur_poly or self._draw_lines
     569           or self._move_point_mode):
    502570            self.invalidate()
    503571
     
    512580
    513581    def mouse_down(self, ev):
     582        corrected_pos = ev.pos[0] + self.pos[0], ev.pos[1] + self.pos[1]
    514583        if self.sel_mode and ev.button == 1:
    515             corrected_pos = ev.pos[0] + self.pos[0], ev.pos[1] + self.pos[1]
    516584            obj = self.level.find_obj_at_pos(corrected_pos)
    517585            if obj is not None:
    518586                self._edit_selected(obj)
     587        elif self._move_point_mode and ev.button == 1:
     588            if self._move_point:
     589                # Place the current point
     590                self.level.replace_vertex(self._move_point, corrected_pos)
     591                self._move_point = None
     592                self.invalidate()
     593            else:
     594                # find the current point
     595                self._move_point = self.level.find_vertex(corrected_pos)
    519596        elif ev.button == 1:
    520597            if self._draw_lines:
     
    780857        y += MENU_BUTTON_HEIGHT + MENU_PAD
    781858
     859        self.move_point_but = HighLightButton("Move Point", self,
     860                                              action=self.move_point)
     861        self.move_point_but.rect = BUTTON_RECT.copy()
     862        self.move_point_but.rect.move_ip(MENU_LEFT, y)
     863        widgets.append(self.move_point_but)
     864        self._light_buttons.append(self.move_point_but)
     865        y += MENU_BUTTON_HEIGHT + MENU_PAD
     866
    782867        self.draw_line_but = HighLightButton("Draw interior wall", self,
    783868                                             action=self.set_line_mode)
     
    9701055    def mouse_move(self, ev):
    9711056        self.level_widget.mouse_move(ev)
     1057
     1058    def move_point(self):
     1059        self.level_widget.set_move_mode()
     1060        self.move_point_but.highlight()
    9721061
    9731062    def draw(self, surface):
Note: See TracChangeset for help on using the changeset viewer.