text: line breaks and indentation

pull/35003/head
Cameron Clough 1 week ago
parent 432e9e563e
commit 03df050b48
  1. 30
      system/ui/text.py

@ -1,4 +1,5 @@
#!/usr/bin/env python3
import re
import pyray as rl
from openpilot.system.hardware import HARDWARE
from openpilot.system.ui.lib.button import gui_button, ButtonStyle
@ -17,18 +18,27 @@ DEMO_TEXT = """This is a sample text that will be wrapped and scrolled if necess
def wrap_text(text, font_size, max_width):
lines = []
current_line = ""
font = gui_app.font()
for word in text.split():
test_line = current_line + word + " "
if rl.measure_text_ex(font, test_line, font_size, 0).x <= max_width:
current_line = test_line
else:
for paragraph in text.split("\n"):
indent = re.match(r'^\s*', paragraph).group()
content = paragraph[len(indent):]
if content == "":
lines.append("")
continue
current_line = indent
for word in paragraph.split():
test_line = current_line + word + " "
if rl.measure_text_ex(font, test_line, font_size, 0).x <= max_width:
current_line = test_line
else:
lines.append(current_line)
current_line = indent + word + " "
if current_line:
lines.append(current_line)
current_line = word + " "
if current_line:
lines.append(current_line)
return lines
@ -46,7 +56,7 @@ class TextWindowRenderer:
position = rl.Vector2(self._textarea_rect.x + scroll.x, self._textarea_rect.y + scroll.y + i * LINE_HEIGHT)
if position.y + LINE_HEIGHT < self._textarea_rect.y or position.y > self._textarea_rect.y + self._textarea_rect.height:
continue
rl.draw_text_ex(gui_app.font(), line.strip(), position, FONT_SIZE, 0, rl.WHITE)
rl.draw_text_ex(gui_app.font(), line.rstrip(), position, FONT_SIZE, 0, rl.WHITE)
rl.end_scissor_mode()
button_bounds = rl.Rectangle(gui_app.width - MARGIN - BUTTON_SIZE.x, gui_app.height - MARGIN - BUTTON_SIZE.y, BUTTON_SIZE.x, BUTTON_SIZE.y)

Loading…
Cancel
Save