Пример #1
0
class TextInput:
    """Text input field"""
    def __init__(self, batch, groups, on_click, on_enter, on_motion,
                 on_search):
        # create document
        self.document = FormattedDocument(' ')
        self.document.set_style(
            0, 1,
            dict(font_name=FONT_NAME, font_size=FONT_SIZE, color=FONT_COLOR))
        # calculate font height and margin
        font = self.document.get_font(0)
        self.font_height = font.ascent - font.descent
        self.margin = self.font_height * TEXT_INPUT_MARGIN
        # create text input
        self.input = IncrementalTextLayout(self.document,
                                           100,
                                           self.font_height,
                                           batch=batch,
                                           group=groups[1])
        self.input.x = 100
        self.input.y = 100
        # creating a caret and push it to window handlers
        self.caret = Caret(self.input, FONT_COLOR[:3], on_click, on_enter,
                           on_motion, on_search)
        self.clear_text()
        # create background rectangle
        self.rect = Rectangle(0,
                              0,
                              100,
                              self.font_height + 2 * self.margin,
                              color=TEXT_INPUT_COLOR,
                              batch=batch,
                              group=groups[0])

    def resize(self, x, y, w):
        """Resize the text input with given coordinates and width"""
        self.rect.x = x
        self.rect.y = y
        self.rect.width = w
        self.input.x = x + self.margin
        self.input.y = y + self.margin
        self.input.width = w - 2 * self.margin

    def get_text(self):
        """Return currently displayed text"""
        return self.document.text

    def set_text(self, text):
        """Set the text to display"""
        self.document.text = text

    def clear_text(self):
        """Clear displayed text"""
        self.document.text = ''