From 3a028da28ffdbbc5f68e166db9d2be3cb4709564 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Czeka=C5=84ski?= Date: Sun, 10 May 2020 17:47:54 +0200 Subject: [PATCH 1/4] Add method writing to console --- src/ui/UiConsole.py | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/src/ui/UiConsole.py b/src/ui/UiConsole.py index 48ca682..8322088 100644 --- a/src/ui/UiConsole.py +++ b/src/ui/UiConsole.py @@ -32,6 +32,14 @@ class UiConsole(UiElement): self.addLinesToConsole(["Hello from console!"]) self.writeConsoleLines() + # TODO: Should this method be static? We need to instantiate some console before we can write anything to it. + def writeToConsole(self, inp: str): + """ + Writes given string to console without scrolling to bottom. + :param inp: String to be written to console. + """ + self.addLinesToConsole([inp]) + def writeConsoleLines(self, startingLineInd=0): self.image.fill(self.bgColor) if startingLineInd < 0: From bdcc564ebb1d79646a4dd0b089c0f5323e33f90e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Czeka=C5=84ski?= Date: Sun, 10 May 2020 17:57:27 +0200 Subject: [PATCH 2/4] Fix writeToConsole method This method was adding given string to the console's list of lines, but wasn't displaying this given string. User had to perform scroll to display this new string. --- src/ui/UiConsole.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/ui/UiConsole.py b/src/ui/UiConsole.py index 8322088..e7202e6 100644 --- a/src/ui/UiConsole.py +++ b/src/ui/UiConsole.py @@ -35,10 +35,10 @@ class UiConsole(UiElement): # TODO: Should this method be static? We need to instantiate some console before we can write anything to it. def writeToConsole(self, inp: str): """ - Writes given string to console without scrolling to bottom. + Writes given string to console and scrolls (console) down to display it. :param inp: String to be written to console. """ - self.addLinesToConsole([inp]) + self.addLinesToConsoleAndScrollToDisplayThem([inp]) def writeConsoleLines(self, startingLineInd=0): self.image.fill(self.bgColor) From 82eb2b9857ec63b738ddfd4662cfd4a48cbe60f9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Czeka=C5=84ski?= Date: Sun, 10 May 2020 18:03:29 +0200 Subject: [PATCH 3/4] Add comments to all methods in UiConsole class --- src/ui/UiConsole.py | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/src/ui/UiConsole.py b/src/ui/UiConsole.py index e7202e6..31c343c 100644 --- a/src/ui/UiConsole.py +++ b/src/ui/UiConsole.py @@ -40,7 +40,11 @@ class UiConsole(UiElement): """ self.addLinesToConsoleAndScrollToDisplayThem([inp]) - def writeConsoleLines(self, startingLineInd=0): + def writeConsoleLines(self, startingLineInd: int = 0): + """ + Displays lines stored in console's list of lines, starting from line with given index. + :param startingLineInd: Line index, which will be written on top of the console. + """ self.image.fill(self.bgColor) if startingLineInd < 0: startingLineInd = 0 @@ -53,6 +57,13 @@ class UiConsole(UiElement): writtenLines += 1 def addLinesToConsole(self, linesToAdd): + """ + Adds lines to console's list of lines. If one line is too long to display, then it is being cut to pieces, + so that it is appropriate size. + + Warning: this method doesn't display given lines, just adds to list of lines. + :param linesToAdd: + """ for line in linesToAdd: self.consoleLines.append(line) self.linesCount += 1 @@ -81,6 +92,10 @@ class UiConsole(UiElement): self.linesImagesCount += 1 def addLinesToConsoleAndScrollToDisplayThem(self, linesToAdd): + """ + Adds given lines to console's list of lines, writes them and scrolls console down to display them. + :param linesToAdd: Lines to add to console's list of lines and to display. + """ self.addLinesToConsole(linesToAdd) ind = 0 if self.linesImagesCount > self.maxLines: From d2f7cbc6ede33e673e35e92e3c61b5cbe96359e2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Czeka=C5=84ski?= Date: Sun, 10 May 2020 18:27:45 +0200 Subject: [PATCH 4/4] Add type hints for fields and method parameters in UiConsole --- src/ui/UiConsole.py | 22 +++++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) diff --git a/src/ui/UiConsole.py b/src/ui/UiConsole.py index 31c343c..7ed5f62 100644 --- a/src/ui/UiConsole.py +++ b/src/ui/UiConsole.py @@ -1,10 +1,26 @@ import pygame +from typing import Tuple, List +from pygame.font import FontType from src.ui.UiElement import UiElement class UiConsole(UiElement): - def __init__(self, rect: pygame.Rect, bgColor=(125, 125, 125), textColor=(255, 255, 255), font=None, antialias=True): + linesCount: int + antialias: bool + bgColor: Tuple[int, int, int] + font: pygame.font.Font + maxLines: int + lineHeight: int + linesImages: List[pygame.Surface] + topWrittenLineInd: int + consoleLines: List[str] + linesImagesCount: int + consoleWidth: float + image: pygame.Surface + + def __init__(self, rect: pygame.Rect, bgColor: Tuple[int, int, int] = (125, 125, 125), + textColor: Tuple[int, int, int] = (255, 255, 255), font: pygame.font.Font = None, antialias: bool = True): super().__init__(rect) self.textColor = textColor @@ -56,7 +72,7 @@ class UiConsole(UiElement): self.image.blit(self.linesImages[i], (0, writtenLines * self.lineHeight)) writtenLines += 1 - def addLinesToConsole(self, linesToAdd): + def addLinesToConsole(self, linesToAdd: List[str]): """ Adds lines to console's list of lines. If one line is too long to display, then it is being cut to pieces, so that it is appropriate size. @@ -91,7 +107,7 @@ class UiConsole(UiElement): self.linesImages.append(row) self.linesImagesCount += 1 - def addLinesToConsoleAndScrollToDisplayThem(self, linesToAdd): + def addLinesToConsoleAndScrollToDisplayThem(self, linesToAdd: List[str]): """ Adds given lines to console's list of lines, writes them and scrolls console down to display them. :param linesToAdd: Lines to add to console's list of lines and to display.