summaryrefslogtreecommitdiff
path: root/asciifarm/client/inputhandler.py
blob: b2e4ac93ec4b6760fa2a802e77247af4ed072bbc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98

import string

from .commandhandler import CommandHandler, InvalidCommandException

import ratuil.inputs as inp


class InputHandler:
    
    def __init__(self, client, keybindings):
        self.client = client
        self.keybindings = keybindings
        self.commandHandler = CommandHandler(self.client)
        
        self.typing = False
        self.string = ""
        self.cursor = 0
    
    
    def onInput(self, key):
        if not self.typing:
            keyName = key
            if keyName in self.keybindings:
                self.commandHandler.execute(self.keybindings[keyName])
        else:
            self.addKey(key)
    
    
    def processString(self, message):
        if message:
            if message[0] == '/':
                if len(message) == 1:
                    return
                if message[1] == '/':
                    self.commandHandler.chat(message[1:])
                else:
                    try:
                        command, _sep, arg = message[1:].partition(' ')
                        try:
                            self.commandHandler.execute([command, arg])
                        except Exception as e:
                            self.log(e)
                    except InvalidCommandException as e:
                        self.client.log(", ".join(e.args))
            else:
                self.commandHandler.chat(message)
    
    def startTyping(self, startText=""):
        self.typing = True
        if startText and not self.string:
            self.string = startText
            self.cursor = len(self.string)
            
        self.showString()
    
    def showString(self):
        self.client.display.setInputString(self.string, self.cursor if self.typing else -1)
    
    def addKey(self, key):
        if key in string.printable:
            self.string = self.string[:self.cursor] + key + self.string[self.cursor:]
            self.cursor += 1
        elif key == inp.BACKSPACE:
            self.string = self.string[:self.cursor-1] + self.string[self.cursor:]
            self.cursor = max(self.cursor - 1, 0)
        elif key == inp.RIGHT:
            self.cursor = min(self.cursor + 1, len(self.string))
        elif key == inp.LEFT:
            self.cursor = max(self.cursor - 1, 0)
        elif key == inp.DELETE:
            self.string = self.string[:self.cursor] + self.string[self.cursor+1:]
        elif key == inp.HOME:
            self.cursor = 0
        elif key == inp.END:
            self.cursor = len(self.string)
        
        elif key == inp.ESCAPE:
            # throw away entered string and go back to game
            self.typing = False
            self.string = ""
            self.cursor = 0
        elif key == inp.ENTER:
            # process entered string and reset it
            message = self.string
            self.string = ""
            self.cursor = 0
            self.typing = False
            self.processString(message)
        elif key == "^I": # tab
            # return to game but keep entered string
            self.typing = False
        
        self.showString()