2024-04-23 01:53:30 +00:00
|
|
|
import pygame
|
|
|
|
from .doodles import Doodle
|
2024-04-24 03:07:25 +00:00
|
|
|
from .world import world
|
2024-04-23 01:53:30 +00:00
|
|
|
|
|
|
|
|
|
|
|
class Text(Doodle):
|
|
|
|
def __init__(self, parent=None):
|
|
|
|
"""
|
|
|
|
Text will be centered at `pos`
|
|
|
|
"""
|
|
|
|
super().__init__(parent)
|
|
|
|
self._text = ""
|
2024-04-24 03:07:25 +00:00
|
|
|
self._rendered = None # the surface we pre-render the text to
|
2024-04-23 01:53:30 +00:00
|
|
|
self._font = None
|
|
|
|
|
|
|
|
def __repr__(self):
|
|
|
|
return f"Text(pos={self.pos_vec}, text={self._text}, parent={self._parent})"
|
|
|
|
|
2024-04-24 03:07:25 +00:00
|
|
|
def draw(self):
|
|
|
|
world.draw_engine.text_draw(self)
|
2024-04-23 01:53:30 +00:00
|
|
|
|
|
|
|
def text(self, text: str) -> "Doodle":
|
|
|
|
"""
|
2024-04-24 03:07:25 +00:00
|
|
|
A setter for the text.
|
|
|
|
|
|
|
|
This is the only place text can change,
|
|
|
|
so we can pre-render the surface here.
|
2024-04-23 01:53:30 +00:00
|
|
|
"""
|
|
|
|
self._text = text
|
2024-04-24 03:07:25 +00:00
|
|
|
self._render()
|
|
|
|
return self
|
|
|
|
|
|
|
|
def _render(self):
|
|
|
|
"""
|
|
|
|
This function needs to set the _rendered property.
|
|
|
|
|
|
|
|
_rendered may be relied upon by draw_engine.draw_text.
|
|
|
|
"""
|
2024-04-23 01:53:30 +00:00
|
|
|
# text needs to be rendered once on change to be performant
|
|
|
|
# doing this in draw would be much slower since it is called
|
|
|
|
# much more often than the text changes
|
|
|
|
if not self._font:
|
2024-04-24 03:07:25 +00:00
|
|
|
self._font = world.draw_engine.get_font() # default font
|
|
|
|
self._rendered = world.draw_engine.text_render(
|
|
|
|
self._text, self._font, self._color
|
|
|
|
)
|
2024-04-23 01:53:30 +00:00
|
|
|
|
|
|
|
def font(self, font: str) -> "Doodle":
|
2024-04-24 03:07:25 +00:00
|
|
|
self._font = world.draw_engine.get_font(font)
|
2024-04-23 01:53:30 +00:00
|
|
|
return self
|