Changeset 275:2abb61878bb1


Ignore:
Timestamp:
Sep 5, 2013, 10:04:14 AM (7 years ago)
Author:
Neil Muller <drnlmuller@…>
Branch:
default
Message:

Add a 'select object' with pop-up for easier editing

File:
1 edited

Legend:

Unmodified
Added
Removed
  • tools/area_editor.py

    r262 r275  
    5656        self.x = x
    5757        self.y = y
     58        # Lookup initiliasition info from the objects
     59        self.lookup = {}
    5860
    5961    def round_point(self, pos):
    6062        return (10 * (pos[0] // 10), 10 * (pos[1] // 10))
     63
     64    def load(self, space):
     65        super(EditorLevel, self).load(space)
     66        # Needed to fill in the lookup dict
     67        self.reset_objs()
    6168
    6269    def point_to_pymunk(self, pos):
     
    180187        self._glue = np.PuzzleGlue()
    181188        for game_object_dict in self._game_objects:
    182             self._create_game_object(pymunk.Space(), **game_object_dict)
     189            obj = self._create_game_object(pymunk.Space(), **game_object_dict)
     190            self.lookup[obj] = game_object_dict
    183191        for enemy_dict in self._enemies:
    184             self._create_enemy(pymunk.Space(), **enemy_dict)
     192            obj = self._create_enemy(pymunk.Space(), **enemy_dict)
     193            self.lookup[obj] = enemy_dict
    185194
    186195    def get_class(self, classname, mod=None):
     
    213222        return False
    214223
     224    def find_obj_at_pos(self, mouse_pos):
     225        pymunk_pos = self.point_to_pymunk(mouse_pos)
     226        # Search visible objects
     227        for obj in self.drawables:
     228            if obj.get_shape().point_query(pymunk_pos):
     229                return obj
     230        return None
     231
    215232
    216233class ObjectTable(TableView):
     
    246263class EditClassDialog(Dialog):
    247264
    248     def __init__(self, classname, cls, data):
     265    def __init__(self, classname, cls, data, delete=False):
    249266        super(EditClassDialog, self).__init__()
    250267        self.classname = classname
     
    282299            y += 30
    283300        buttons = []
    284         for text in ['OK', 'Cancel']:
     301        if delete:
     302            labels = ['OK', 'Delete', 'Cancel']
     303        else:
     304            labels = ['OK', 'Cancel']
     305        for text in labels:
    285306            but = Button(text, action=lambda x=text: self.dismiss(x))
    286307            buttons.append(but)
     
    323344        self._draw_enemies = False
    324345        self._draw_lines = False
     346        self.sel_mode = False
    325347        self._start_pos = None
    326348
     
    433455
    434456    def mouse_down(self, ev):
    435         if ev.button == 1:
     457        if self.sel_mode and ev.button == 1:
     458            corrected_pos = ev.pos[0] + self.pos[0], ev.pos[1] + self.pos[1]
     459            obj = self.level.find_obj_at_pos(corrected_pos)
     460            if obj is not None:
     461                self._edit_selected(obj)
     462        elif ev.button == 1:
    436463            if self._draw_lines:
    437464                if self._start_pos is None:
     
    479506        return None
    480507
     508    def _edit_selected(self, obj):
     509        data = self.level.lookup[obj]
     510        cls = obj.__class__
     511        classname = obj.__class__.__name__
     512        dialog = EditClassDialog(classname, cls, data, True)
     513        res = dialog.present()
     514        if res == 'OK':
     515            edited = dialog.get_data()
     516            if edited is not None:
     517                for target in [self.level._game_objects, self.level._enemies]:
     518                    if data in target:
     519                        self.level.try_new_object(classname, target,
     520                                                  edited, data)
     521                        break
     522        elif res == 'Delete':
     523            for target in [self.level._game_objects, self.level._enemies]:
     524                if data in target:
     525                    target.remove(data)
     526                    self.level.reset_objs()
     527                    break
     528
    481529    def _make_edit_dialog(self, entries):
    482530        # Dialog to hold the editor
     
    743791
    744792        y += MENU_PAD
     793        sel_mode_but = Button('Select Object',
     794                              action=self.sel_mode)
     795        sel_mode_but.rect = BUTTON_RECT.copy()
     796        sel_mode_but.rect.move_ip(MENU_LEFT, y)
     797        widgets.append(sel_mode_but)
     798        y += MENU_BUTTON_HEIGHT + MENU_PAD
     799
     800        y += MENU_PAD
    745801        switch_but = Button('Switch to Drawing', action=self.switch_to_draw)
    746802        switch_but.rect = BUTTON_RECT.copy()
     
    799855    def _populate_menu(self):
    800856        self.level_widget.change_poly(None)
     857        self.level_widget.sel_mode = False
    801858        for widget in self._dMenus[self._menu_mode]:
    802859            self.add(widget)
    803860        self.invalidate()
     861
     862    def sel_mode(self):
     863        self.level_widget.sel_mode = not self.level_widget.sel_mode
    804864
    805865    def mouse_move(self, ev):
Note: See TracChangeset for help on using the changeset viewer.