Ejemplo n.º 1
0
 def __init__(self, app, row, column):
     super(TextBoxCell, self).__init__(app, row, column)
     textAlign = self.styleGet('textAlign')
     self.inputBox = InputBox(
         self.app,
         Area(
             CellAttachedPoint((0, 0), self, textAlign),
             self._getUsableRect().size, textAlign),
         font=self.styleGet('font'),
         colour=self.styleGet('foreColour'))
     self.inputBox.onEdit.addListener(self._valueChanged)
     self.elements = [self.inputBox]
     self._oldText = ''
     self._readOnly = True
     self.setReadOnly(False)
     self.onValueChanged = Event()
Ejemplo n.º 2
0
    def __init__(self,
                 app,
                 font,
                 area,
                 colour=(192, 192, 192),
                 backColour=(0, 64, 0),
                 historyLimit=2000,
                 locals={}):
        TwistedInteractiveConsole.__init__(self, locals=locals)
        framework.Element.__init__(self, app)

        self.font = font
        self.area = area
        self.colour = colour
        self.backColour = backColour
        self.rect = None
        self.image = None
        self.dirty = True
        self.history = ['']
        self.historyLimit = historyLimit
        self.inBox = ib = InputBox(self.app,
                                   ScaledArea(0, 0, 1000, 100),
                                   font=self.font)
        ib.onEnter.addListener(self._enter)
        ib.hasFocus = True
        ib.gotFocus()

        self.readDeferred = None
        self.deferredInputs = []  # List of (prompt, deferred)
        self.font = font
        self.typeAhead = []
        self.inText = ''
        self.cursorPos = 0
        self._dummyStdIn = DummyStdIn(self.stdInRead)
        self._dummyStdIn.deferred_raw_input = self.raw_input
Ejemplo n.º 3
0
    def __init__(self, app, world, interface):
        super(ChatBox, self).__init__(app)

        self.world = world
        self.app = app
        self.interface = interface

        self.font = self.app.screenManager.fonts.newChatFont

        self.frameColour = self.app.theme.colours.chatFrameColour
        self.insideColour = self.app.theme.colours.chatInsideColour
        self.textColour = self.app.theme.colours.chatNormalColour

        self.sayToTeam = TextElement(
            self.app,
            text="Say to team:",
            font=self.font,
            pos=Location(FullScreenAttachedPoint((20, 501), 'topleft'),
                         'topleft'),
            colour=self.textColour,
            shadow=True,
        )

        self.inputPosition = Area(
            FullScreenAttachedPoint((145, 500), 'topleft'), (370, 20),
            'topleft')
        self.input = InputBox(self.app, self.inputPosition, font=self.font)
        self.input.onEnter.addListener(
            lambda sender: self.hitEnter(sender.value))
        self.input.onEsc.addListener(lambda sender: self.close())
        self.input.onClick.addListener(self.setFocus)

        self.messages = MessageBank(
            self.app, 10, 100,
            Location(FullScreenAttachedPoint((20, 470), 'topleft'), 'topleft'),
            'left', 'bottom', self.font)

        self._chatOpen = False
        self.teamChat = True
        self.player = None

        self.messageBuffer = []

        self.MESSAGE_GAP = self.font.getHeight(self.app)

        self.elements = [self.messages]
Ejemplo n.º 4
0
    def __init__(self, app, nick, head):
        title = 'Player settings'
        DialogBox.__init__(self, app, Canvas(400, 290), title)

        labelFont = app.screenManager.fonts.bigMenuFont
        labelColour = app.theme.colours.dialogBoxTextColour
        btnFont = app.screenManager.fonts.bigMenuFont
        btnColour = app.theme.colours.dialogButtonColour
        highlightColour = app.theme.colours.black
        inputFont = app.screenManager.fonts.defaultTextBoxFont
        inputColour = app.theme.colours.grey

        self.inputBox = InputBox(
            app,
            Region(topleft=self.Relative(0.1, 0.25),
                   bottomright=self.Relative(0.9, 0.45)),
            nick,
            font=inputFont,
            colour=inputColour,
            onClick=self.setFocus,
            onEnter=self.ok_clicked,
            onEsc=self.cancel_clicked,
        )

        self.heads = HeadSelector(
            app,
            Region(topleft=self.Relative(0.1, 0.5),
                   bottomright=self.Relative(0.9, 0.8)))
        self.heads.selected(head)

        self.elements = [
            TextElement(app, 'Nickname:', labelFont,
                        Location(self.Relative(0.1, 0.15), 'midleft'),
                        labelColour),
            self.inputBox,
            self.heads,
            TextButton(app,
                       Location(self.Relative(0.3, 0.9), 'center'),
                       'Ok',
                       btnFont,
                       btnColour,
                       highlightColour,
                       onClick=self.ok_clicked),
            TextButton(app,
                       Location(self.Relative(0.7, 0.9), 'center'),
                       'Cancel',
                       btnFont,
                       btnColour,
                       highlightColour,
                       onClick=self.cancel_clicked),
        ]

        self.setFocus(self.inputBox)
Ejemplo n.º 5
0
    def __init__(self, app, title, label, validator=None):
        DialogBox.__init__(self, app, Canvas(400, 230), title)

        labelFont = app.screenManager.fonts.bigMenuFont
        labelColour = app.theme.colours.dialogBoxTextColour
        btnFont = app.screenManager.fonts.bigMenuFont
        btnColour = app.theme.colours.dialogButtonColour
        highlightColour = app.theme.colours.black
        inputFont = app.screenManager.fonts.defaultTextBoxFont
        inputColour = app.theme.colours.grey

        self.inputBox = InputBox(app,
                                 Region(topleft=self.Relative(0.1, 0.4),
                                        bottomright=self.Relative(0.9, 0.6)),
                                 font=inputFont,
                                 colour=inputColour,
                                 onClick=self.setFocus,
                                 onEnter=self.okClicked,
                                 onEsc=self.cancelClicked,
                                 validator=validator)

        self.elements = [
            TextElement(app, label, labelFont,
                        Location(self.Relative(0.1, 0.2), 'midleft'),
                        labelColour),
            self.inputBox,
            TextButton(app,
                       Location(self.Relative(0.3, 0.9), 'center'),
                       'Ok',
                       btnFont,
                       btnColour,
                       highlightColour,
                       onClick=self.okClicked),
            TextButton(app,
                       Location(self.Relative(0.7, 0.9), 'center'),
                       'Cancel',
                       btnFont,
                       btnColour,
                       highlightColour,
                       onClick=self.cancelClicked),
        ]

        self.setFocus(self.inputBox)
Ejemplo n.º 6
0
class TextBoxCell(Cell):
    def __init__(self, app, row, column):
        super(TextBoxCell, self).__init__(app, row, column)
        textAlign = self.styleGet('textAlign')
        self.inputBox = InputBox(
            self.app,
            Area(
                CellAttachedPoint((0, 0), self, textAlign),
                self._getUsableRect().size, textAlign),
            font=self.styleGet('font'),
            colour=self.styleGet('foreColour'))
        self.inputBox.onEdit.addListener(self._valueChanged)
        self.elements = [self.inputBox]
        self._oldText = ''
        self._readOnly = True
        self.setReadOnly(False)
        self.onValueChanged = Event()

    def _valueChanged(self, sender):
        self._oldText = self.inputBox.getValue()
        self.onValueChanged.execute(self)

    def setReadOnly(self, readOnly):
        if readOnly == self._readOnly:
            return
        self._readOnly = readOnly

        if readOnly:
            self.inputBox.onClick.removeListener(self._boxClicked)
        else:
            self.inputBox.onClick.addListener(self._boxClicked)
            self.setFocus(None)

    def lostFocus(self):
        self.setFocus(None)

    def setText(self, text):
        if text != self._oldText:
            self.inputBox.setValue(text)
            self._styleChanged = True
            self._oldText = text

    def getText(self):
        return self._oldText

    def setMaxLength(self, length):
        self.inputBox.setMaxLength(length)

    def setValidator(self, validator):
        self.inputBox.setValidator(validator)

    def _update(self):
        self.inputBox.setFont(self.styleGet('font'))
        self.inputBox.setFontColour(self.styleGet('foreColour'))
        bgColour = self.styleGet('backColour')
        if bgColour is not None:
            self.inputBox.setBackColour(bgColour)
        textAlign = self.styleGet('textAlign')
        self.inputBox.area.anchor = textAlign
        self.inputBox.area.point.attachedAt = textAlign
Ejemplo n.º 7
0
    def __init__(self, app, width=2, height=1):
        super(MapSizeBox, self).__init__(app, ScaledSize(400, 230),
                                         "Custom Size")
        labelFont = app.screenManager.fonts.bigMenuFont
        labelColour = app.theme.colours.dialogBoxTextColour
        btnFont = app.screenManager.fonts.bigMenuFont
        btnColour = app.theme.colours.dialogButtonColour
        highlightColour = app.theme.colours.black
        inputFont = app.screenManager.fonts.defaultTextBoxFont
        inputColour = app.theme.colours.grey

        self.widthBox = InputBox(app,
                                 Region(midleft=self.Relative(0.65, 0.25),
                                        size=self.Relative(0.15, 0.2)),
                                 str(width),
                                 font=inputFont,
                                 colour=inputColour,
                                 onClick=self.setFocus,
                                 onEnter=self.okClicked,
                                 validator=intValidator,
                                 maxLength=2)

        self.heightBox = InputBox(app,
                                  Region(midleft=self.Relative(0.65, 0.55),
                                         size=self.Relative(0.15, 0.2)),
                                  str(height),
                                  font=inputFont,
                                  colour=inputColour,
                                  onClick=self.setFocus,
                                  onEnter=self.okClicked,
                                  validator=intValidator,
                                  maxLength=2)

        # Add elements to screen
        self.elements = [
            TextElement(app, 'Half Width:', labelFont,
                        Location(self.Relative(0.6, 0.25), 'midright'),
                        labelColour),
            self.widthBox,
            TextElement(app, 'Height:', labelFont,
                        Location(self.Relative(0.6, 0.55), 'midright'),
                        labelColour),
            self.heightBox,
            TextButton(app,
                       Location(self.Relative(0.3, 0.9), 'center'),
                       'Ok',
                       btnFont,
                       btnColour,
                       highlightColour,
                       onClick=self.okClicked),
            TextButton(app,
                       Location(self.Relative(0.7, 0.9), 'center'),
                       'Cancel',
                       btnFont,
                       btnColour,
                       highlightColour,
                       onClick=self.cancelClicked),
        ]

        self.tabOrder = [self.widthBox, self.heightBox]
        self.setFocus(self.widthBox)
Ejemplo n.º 8
0
class ChatBox(framework.CompoundElement):
    def __init__(self, app, world, interface):
        super(ChatBox, self).__init__(app)

        self.world = world
        self.app = app
        self.interface = interface

        self.font = self.app.screenManager.fonts.newChatFont

        self.frameColour = self.app.theme.colours.chatFrameColour
        self.insideColour = self.app.theme.colours.chatInsideColour
        self.textColour = self.app.theme.colours.chatNormalColour

        self.sayToTeam = TextElement(
            self.app,
            text="Say to team:",
            font=self.font,
            pos=Location(FullScreenAttachedPoint((20, 501), 'topleft'),
                         'topleft'),
            colour=self.textColour,
            shadow=True,
        )

        self.inputPosition = Area(
            FullScreenAttachedPoint((145, 500), 'topleft'), (370, 20),
            'topleft')
        self.input = InputBox(self.app, self.inputPosition, font=self.font)
        self.input.onEnter.addListener(
            lambda sender: self.hitEnter(sender.value))
        self.input.onEsc.addListener(lambda sender: self.close())
        self.input.onClick.addListener(self.setFocus)

        self.messages = MessageBank(
            self.app, 10, 100,
            Location(FullScreenAttachedPoint((20, 470), 'topleft'), 'topleft'),
            'left', 'bottom', self.font)

        self._chatOpen = False
        self.teamChat = True
        self.player = None

        self.messageBuffer = []

        self.MESSAGE_GAP = self.font.getHeight(self.app)

        self.elements = [self.messages]

    def setPlayer(self, player):
        self.player = player
        if self.player.team is None:
            self.refreshMode()

    def canTeamChat(self):
        return self.player and self.player.team is not None

    def switchModes(self):
        self.teamChat = not self.teamChat
        self.refreshMode()

    def refreshMode(self):
        self.teamChat = self.teamChat and self.canTeamChat()
        if self.teamChat:
            self.sayToTeam.setText('Say to team:')
        else:
            self.sayToTeam.setText('Say to all:')

    def hitEnter(self, senderValue):
        if senderValue.strip() != '':
            self.sendChat(senderValue)
        self.input.clear()

    def sendChat(self, senderValue):
        # Interpret lines with initial hash.
        if senderValue.startswith('#'):
            i = 1
            while senderValue[i:i + 1].isdigit():
                i += 1
            try:
                playerId = bytes([int(senderValue[1:i])])
            except ValueError:
                pass
            else:
                self.interface.sendPrivateChat(self.player, playerId,
                                               senderValue[i:].lstrip())
                return

        if self.teamChat:
            self.interface.sendTeamChat(self.player, senderValue)
        else:
            self.interface.sendPublicChat(self.player, senderValue)

    def refresh(self):
        if not self.isOpen():
            return
        self.elements = [self.sayToTeam, self.input]

        initialY = 470
        count = 0
        for text, nick, colour, firstLine in reversed(self.messageBuffer):
            currentY = initialY - count * self.MESSAGE_GAP
            if currentY < 200 or count >= 10:
                break

            if firstLine and nick is not None:
                person = TextElement(
                    self.app,
                    text=nick,
                    font=self.font,
                    pos=Location(
                        FullScreenAttachedPoint((20, currentY), 'topleft'),
                        'topleft'),
                    colour=colour,
                    shadow=True,
                )

                xOffset = person._getRect().width
                self.elements.append(person)

                text = text[len(nick):]

            else:
                xOffset = 0

            if nick is None:
                colour = self.app.theme.colours.serverChat
            else:
                colour = self.textColour

            message = TextElement(
                self.app,
                text=text,
                font=self.font,
                pos=Location(
                    FullScreenAttachedPoint((20 + xOffset, currentY),
                                            'topleft'), 'topleft'),
                colour=colour,
                shadow=True,
            )
            self.elements.append(message)
            count += 1

    def open(self):
        self._chatOpen = True
        self.refresh()
        self.setFocus(self.input)
        self.input.clear()

    def close(self):
        self._chatOpen = False
        self.elements = [self.messages]
        pygame.key.set_repeat()

    def isOpen(self):
        return self._chatOpen

    def newMessage(self, message, nick, colour):

        message = nick + message

        wrappedMessage = wrapline(message, self.font._getFont(self.app), 480)

        # Update the "box open" message elements
        firstLine = True
        for line in wrappedMessage:
            self.messageBuffer.append((line, nick, colour, firstLine))
            firstLine = False

        while len(self.messageBuffer) > 100:
            self.messageBuffer.pop(0)

        # Update the "box closed" message elements
        firstLine = wrappedMessage.pop(0)[len(nick):]
        parts = [(nick, colour), (firstLine, self.textColour)]

        self.messages.newColourMessage(parts)

        for line in wrappedMessage:
            self.messages.newMessage(line, self.textColour)

        self.refresh()

    def newServerMessage(self, message):
        '''Server messages don't follow the format of normal messages and
        require special attention to display.'''

        colour = self.app.theme.colours.serverChat
        wrappedMessage = wrapline("SERVER" + message,
                                  self.font._getFont(self.app), 480)

        # Update the "box open" message elements
        firstLine = True
        for line in wrappedMessage:
            self.messageBuffer.append((line, None, colour, firstLine))
            firstLine = False

        while len(self.messageBuffer) > 100:
            self.messageBuffer.remove(0)

        # Update the "box closed" message elements
        for line in wrappedMessage:
            self.messages.newMessage(line, colour)

        self.refresh()

    def _getRect(self):
        return self.area.getRect(self.app)

    def draw(self, surface):

        if self._chatOpen:

            pointX = 5
            pointY = 300

            # Draw the frame first
            frame = pygame.Surface((520, 230))
            frame.fill(self.frameColour)

            mainBox = pygame.Surface((500, 180))
            mainBox.fill(self.insideColour)

            sayBox = pygame.Surface((120, 20))
            sayBox.fill(self.insideColour)

            if self.app.displaySettings.alphaOverlays:
                mainBox.set_alpha(128)
                sayBox.set_alpha(128)
                frame.set_alpha(128)

            surface.blit(frame, (pointX, pointY))
            surface.blit(mainBox, (pointX + 10, pointY + 10))
            surface.blit(sayBox, (pointX + 10, pointY + 200))

        super(ChatBox, self).draw(surface)

    def processEvent(self, event):
        if not self.isOpen():
            return event

        if event.type == pygame.KEYDOWN and event.key in (pygame.K_LCTRL,
                                                          pygame.K_RCTRL):
            self.switchModes()
        else:
            return self.input.processEvent(event)