I'm writing a project using pygame-gui. I need a class Chart which is a box that would be filled with some text, drawing, etc. So I create a different surface for it. In its subclass ClassChart(Chart) I place an UITextEntryLine.
The problem is that using a new manager (relating to its surface) this entry line doesn't work. If I use the manager from the main class (where I create the window) everything works. So how to make my entry line work with the new manager and is it worth it? Is it ok to use the same manager for the whole project?
My main:
FPS = 60
windowWidth = 900
windowHeight = 900
clock = pygame.time.Clock()
pygame.init()
windowSurface = pygame.display.set_mode((windowWidth, windowHeight))
manager = gui.UIManager((windowWidth, windowHeight))
background = pygame.Surface((windowWidth, windowHeight))
background.fill(Colors.WHITE)
chart = ClassChart(windowSurface, manager, x=100, y=100, name="TEst")
running = True
while running:
events = pygame.event.get()
for event in events:
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
manager.process_events(event)
windowSurface.blit(background, (0, 0))
chart.draw()
manager.update(FPS)
manager.draw_ui(windowSurface)
pygame.display.update()
clock.tick(FPS)
My ClassChart (commented line shows how to work with the manager from main)
class ClassChart(Chart):
def __init__(self,
parentSurface,
manager,
width=160,
height=200,
boundColor=Colors.BLACK,
x=0, y=0, thickness=2,
name="Class",
backgroundColor=Colors.LIGHT_LIGHT_GREY, ):
super().__init__(parentSurface, width, height, boundColor, x, y, thickness)
self.manager = gui.UIManager((self.width, self.height))
self.name = gui.elements.UITextEntryLine(pygame.Rect((0, 0), (self.width, self.height)), self.manager)
# self.name = gui.elements.UITextEntryLine(pygame.Rect((self.x, self.y), (self.width, self.height)), manager)
self.nameSize = self.height // 10
self.fieldSize = (self.height - self.nameSize) // 2
self.name.set_text(name)
def draw(self):
super(ClassChart, self).draw()
pygame.draw.line(self.surface, self.boundColor, (0, self.nameSize),
(self.width, self.nameSize), self.thickness)
pygame.draw.line(self.surface, self.boundColor, (0, self.nameSize + self.fieldSize),
(self.width, self.nameSize + self.fieldSize), self.thickness)
self.manager.update(60)
self.manager.draw_ui(self.surface)
self.blit()