Add method addingLines to UiConsole and fix writing on console

This commit is contained in:
Michał Czekański 2020-04-04 20:49:02 +02:00
parent 41b7cd2a3f
commit bf166232f3

View File

@ -16,20 +16,51 @@ class UiConsole(UiElement):
self.image = pygame.Surface((rect.width, rect.height)) self.image = pygame.Surface((rect.width, rect.height))
self.image.fill(bgColor) self.image.fill(bgColor)
self.consoleLines = ["Hello from console!"] self.consoleWidth = self.image.get_width()
self.linesCount = 1 self.linesImagesCount = 0
self.consoleLines = []
self.linesCount = 0
self.linesImages = [] self.linesImages = []
self.linesImages.append(font.render(self.consoleLines[0], False, textColor)) self.lineHeight = font.render("sampleText", False, textColor) .get_height()
self.lineHeight = self.linesImages[0].get_height()
self.maxLines = int(self.image.get_height() / self.lineHeight) 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) self.image.fill(self.bgColor)
writtenLines = 0 writtenLines = 0
for i in range(startingLineInd, min(self.maxLines + startingLineInd, self.linesCount)): for i in range(startingLineInd, min(self.maxLines + startingLineInd, self.linesImagesCount)):
self.image.blit(self.linesImages[i], (writtenLines*self.lineHeight, 0)) self.image.blit(self.linesImages[i], (0, writtenLines * self.lineHeight))
writtenLines += 1 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