Changeset 233

Show
Ignore:
Timestamp:
01/04/07 03:46:43 (5 years ago)
Author:
mg
Message:

Allow up to two keys bound to the same action.

Location:
trunk
Files:
2 modified

Legend:

Unmodified
Added
Removed
  • trunk/NEWS.txt

    r232 r233  
    44  - Bugfix: after remapping the controls the old keys used to continue to 
    55    perform the same actions. 
     6  - Allow up to two keys bound to the same action. 
    67 
    78December 25, 2006: Released version 0.9.3: the Christmas release. 
  • trunk/src/pyspacewar/ui.py

    r232 r233  
    129129 
    130130    """ 
    131     for action, key in controls.items(): 
    132         text = text.replace(action, key_name(key)) 
     131    for action, keys in controls.items(): 
     132        text = text.replace(action, key_name(keys[0])) 
    133133    return text 
    134134 
     
    11651165        """Handle any keys that are pressed.""" 
    11661166        for key, (handler, args) in self._keymap_repeat.items(): 
    1167             key = self.ui.controls.get(key, key) 
    1168             if pressed[key]: 
    1169                 handler(*args) 
     1167            for key in self.ui.controls.get(key, [key]): 
     1168                if key is not None and pressed[key]: 
     1169                    handler(*args) 
    11701170 
    11711171    def handle_mouse_press(self, event): 
     
    14881488    """Mode: controls menu.""" 
    14891489 
     1490    def init(self): 
     1491        MenuMode.init(self) 
     1492        self.on_key(K_BACKSPACE, self.clear_item) 
     1493        self.on_key(K_DELETE, self.clear_item) 
     1494        self.on_key(K_KP_PERIOD, self.clear_item) 
     1495 
    14901496    def items(self, label, items): 
    14911497        return ([(label, )] + 
    1492                 [(title + '\t' + key_name(self.ui.controls[action]), 
     1498                [(title + '\t' + ', '.join(map(key_name, 
     1499                                               self.ui.controls[action])), 
    14931500                  self.set_control, title, action) 
    14941501                 for title, action in items]) 
     
    15231530        """Change a control""" 
    15241531        self.ui.ui_mode = WaitingForControlMode(self.ui, action, key) 
     1532 
     1533    def clear_item(self): 
     1534        """Clear the selected menu item.""" 
     1535        action = self.menu_items[self.menu.selected_item][1:] 
     1536        if action: 
     1537            handler = action[0] 
     1538            args = action[1:] 
     1539            if handler == self.set_control: 
     1540                title, action = args 
     1541                self.ui.set_control(action, None) 
     1542                self.reinit_menu() 
    15251543 
    15261544 
     
    17671785    def __init__(self): 
    17681786        self.rng = random.Random() 
    1769         self.controls = dict(DEFAULT_CONTROLS) 
    1770         self.rev_controls = dict([(value, key) 
    1771                                   for (key, value) in self.controls.items()]) 
    1772         assert len(self.controls) == len(self.rev_controls) 
     1787        self.controls = {} 
     1788        for action in DEFAULT_CONTROLS: 
     1789            self.controls[action] = [None] 
     1790        self.rev_controls = {} 
     1791        for action, key in DEFAULT_CONTROLS.items(): 
     1792            self.set_control(action, key) 
    17731793 
    17741794    def load_settings(self, filename=None): 
     
    17891809        for action in self.controls: 
    17901810            key = config.get('controls', action) 
    1791             try: 
    1792                 key = int(key) 
    1793             except ValueError: 
    1794                 key = None 
    1795             self.set_control(action, key) 
     1811            if key: 
     1812                # clear all current keys first 
     1813                self.set_control(action, None) 
     1814            for key in key.split(): 
     1815                try: 
     1816                    key = int(key) 
     1817                except ValueError: 
     1818                    key = None 
     1819                self.set_control(action, key) 
    17961820 
    17971821    def save_settings(self, filename=None): 
     
    18141838                   str(self.show_missile_trails)) 
    18151839        config.add_section('controls') 
    1816         for action, key in self.controls.items(): 
    1817             config.set('controls', action, key) 
     1840        for action, keys in self.controls.items(): 
     1841            config.set('controls', action, ' '.join(map(str, keys))) 
    18181842        return config 
    18191843 
     
    21502174        """Change a key mapping""" 
    21512175        if key in self.rev_controls: 
    2152             # key was previously bound to something else 
    21532176            old_action = self.rev_controls[key] 
    2154             self.controls[old_action] = None 
    2155         old_key = self.controls[action] 
    2156         if old_key: 
    2157             # some other key was previously bound to this action 
    2158             del self.rev_controls[old_key] 
    2159         self.controls[action] = key 
     2177            self.controls[old_action].remove(key) 
     2178            if not self.controls[old_action]: 
     2179                self.controls[old_action] = [None] 
     2180        keys = self.controls[action] 
     2181        if len(keys) > 1 or key is None or keys == [None]: 
     2182            for old_key in keys: 
     2183                if old_key is not None: 
     2184                    del self.rev_controls[old_key] 
     2185            self.controls[action] = [] 
     2186        self.controls[action].append(key) 
    21602187        if key is not None: 
    21612188            self.rev_controls[key] = action