File: input.py

package info (click to toggle)
python-box2d 2.3.2~dfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 3,596 kB
  • ctags: 5,116
  • sloc: python: 14,384; cpp: 13,393; makefile: 8; sh: 6
file content (156 lines) | stat: -rw-r--r-- 4,684 bytes parent folder | download | duplicates (2)
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
"""
"""
import pygame
from pygame.locals import *

from .const import *
from . import widget

class Input(widget.Widget):
    """A single line text input.
    
    Example:
        w = Input(value="Cuzco the Goat",size=20)
        w = Input("Marbles")
    
    """

    _value = None

    def __init__(self,value="",size=20,**params):
        """Create a new Input widget.

        Keyword arguments:
            value -- initial text
            size -- size for the text box, in characters

        """
        params.setdefault('cls','input')
        widget.Widget.__init__(self,**params)
        self.value = value
        self.pos = len(str(value))
        self.vpos = 0
        self.font = self.style.font
        w,h = self.font.size("e"*size)
        if not self.style.height: self.style.height = h
        if not self.style.width: self.style.width = w
        #self.style.height = max(self.style.height,h)
        #self.style.width = max(self.style.width,w)
        #self.rect.w=w+self.style.padding_left+self.style.padding_right;
        #self.rect.h=h+self.style.padding_top+self.style.padding_bottom;
    
    def paint(self,s):
        r = pygame.Rect(0,0,self.rect.w,self.rect.h)
        
        cs = 2 #NOTE: should be in a style
        
        w,h = self.font.size(self.value[0:self.pos])
        x = w-self.vpos
        if x < 0: self.vpos -= -x
        if x+cs > s.get_width(): self.vpos += x+cs-s.get_width()
        
        s.blit(self.font.render(self.value, 1, self.style.color),(-self.vpos,0))
        
        if self.container.myfocus is self:
            w,h = self.font.size(self.value[0:self.pos])
            r.x = w-self.vpos
            r.w = cs
            r.h = h
            s.fill(self.style.color,r)
    
    def _setvalue(self,v):
        #self.__dict__['value'] = v
        self._value = v
        self.send(CHANGE)
    
    def event(self,e):
        used = None
        if e.type == KEYDOWN:
            if e.key == K_BACKSPACE:
                if self.pos:
                    self._setvalue(self.value[:self.pos-1] + self.value[self.pos:])
                    self.pos -= 1
            elif e.key == K_DELETE:
                if len(self.value) > self.pos:
                    self._setvalue(self.value[:self.pos] + self.value[self.pos+1:])
            elif e.key == K_HOME: 
                self.pos = 0
            elif e.key == K_END:
                self.pos = len(self.value)
            elif e.key == K_LEFT:
                if self.pos > 0: self.pos -= 1
                used = True
            elif e.key == K_RIGHT:
                if self.pos < len(self.value): self.pos += 1
                used = True
            elif e.key == K_RETURN:
                self.next()
            elif e.key == K_TAB:
                pass
            else:
                #c = str(e.unicode)
                try:
                    c = (e.unicode).encode('latin-1')
                    if c:
                        self._setvalue(self.value[:self.pos] + c + self.value[self.pos:])
                        self.pos += 1
                except: #ignore weird characters
                    pass
            self.repaint()
        elif e.type == FOCUS:
            self.repaint()
        elif e.type == BLUR:
            self.repaint()
        
        self.pcls = ""
        if self.container.myfocus is self: self.pcls = "focus"
        
        return used
    
    @property
    def value(self):
        return self._value

    @value.setter
    def value(self, val):
        if (val == None): 
            val = ""
        val = str(val)
        self.pos = len(val)
        oldval = self._value
        self._value = val
        if (oldval != val):
            self.send(CHANGE)
            self.repaint()


class Password(Input):
    """A password input, in which text is rendered with '*' characters."""

    def paint(self,s):
        hidden="*"
        show=len(self.value)*hidden
        
        #print "self.value:",self.value

        if self.pos == None: self.pos = len(self.value)
        
        r = pygame.Rect(0,0,self.rect.w,self.rect.h)
        
        cs = 2 #NOTE: should be in a style
        
        w,h = self.font.size(show)
        x = w-self.vpos
        if x < 0: self.vpos -= -x
        if x+cs > s.get_width(): self.vpos += x+cs-s.get_width()
        
        s.blit(self.font.render(show, 1, self.style.color),(-self.vpos,0))
        
        if self.container.myfocus is self:
            #w,h = self.font.size(self.value[0:self.pos])            
            w,h = self.font.size(show[0:self.pos])
            r.x = w-self.vpos
            r.w = cs
            r.h = h
            s.fill(self.style.color,r)