From bf166232f3f6d1d1cab76856018860f5e37ea9b0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Czeka=C5=84ski?= Date: Sat, 4 Apr 2020 20:49:02 +0200 Subject: [PATCH] Add method addingLines to UiConsole and fix writing on console --- src/ui/UiConsole.py | 49 ++++++++++++++++++++++++++++++++++++--------- 1 file changed, 40 insertions(+), 9 deletions(-) diff --git a/src/ui/UiConsole.py b/src/ui/UiConsole.py index 557060c..53591e5 100644 --- a/src/ui/UiConsole.py +++ b/src/ui/UiConsole.py @@ -16,20 +16,51 @@ class UiConsole(UiElement): self.image = pygame.Surface((rect.width, rect.height)) self.image.fill(bgColor) - self.consoleLines = ["Hello from console!"] - self.linesCount = 1 + self.consoleWidth = self.image.get_width() + self.linesImagesCount = 0 + + self.consoleLines = [] + self.linesCount = 0 + self.linesImages = [] - self.linesImages.append(font.render(self.consoleLines[0], False, textColor)) - self.lineHeight = self.linesImages[0].get_height() + self.lineHeight = font.render("sampleText", False, textColor) .get_height() self.maxLines = int(self.image.get_height() / self.lineHeight) + self.addLinesToConsole(["Hello from console!"]) + self.writeConsoleLines() - self.__writeConsoleLines__() - - def __writeConsoleLines__(self, startingLineInd=0): + def writeConsoleLines(self, startingLineInd=0): self.image.fill(self.bgColor) writtenLines = 0 - for i in range(startingLineInd, min(self.maxLines + startingLineInd, self.linesCount)): - self.image.blit(self.linesImages[i], (writtenLines*self.lineHeight, 0)) + for i in range(startingLineInd, min(self.maxLines + startingLineInd, self.linesImagesCount)): + self.image.blit(self.linesImages[i], (0, writtenLines * self.lineHeight)) writtenLines += 1 + + def addLinesToConsole(self, linesToAdd): + for line in linesToAdd: + self.consoleLines.append(line) + self.linesCount += 1 + + row = pygame.Surface((self.consoleWidth, self.lineHeight)) + row.fill(self.bgColor) + + howMuchRowIsFilled = 0 + words = line.split(' ') + for word in words: + wordImage = self.font.render(' ' + word, False, self.textColor) + if howMuchRowIsFilled + wordImage.get_width() <= self.consoleWidth: + row.blit(wordImage, (howMuchRowIsFilled, 0)) + howMuchRowIsFilled += wordImage.get_width() + else: + self.linesImages.append(row) + self.linesImagesCount += 1 + row = pygame.Surface((self.consoleWidth, self.lineHeight)) + row.fill(self.bgColor) + howMuchRowIsFilled = 0 + + row.blit(wordImage, (howMuchRowIsFilled, 0)) + howMuchRowIsFilled += wordImage.get_width() + + self.linesImages.append(row) + self.linesImagesCount += 1