diff --git a/openpilot/selfdrive/assets/training/bg_finish.png b/openpilot/selfdrive/assets/training/bg_finish.png new file mode 100644 index 0000000000..571543296b Binary files /dev/null and b/openpilot/selfdrive/assets/training/bg_finish.png differ diff --git a/openpilot/selfdrive/assets/training/bg_welcome.png b/openpilot/selfdrive/assets/training/bg_welcome.png new file mode 100644 index 0000000000..5282417739 Binary files /dev/null and b/openpilot/selfdrive/assets/training/bg_welcome.png differ diff --git a/openpilot/selfdrive/assets/training/check.png b/openpilot/selfdrive/assets/training/check.png new file mode 100644 index 0000000000..bf828317cf Binary files /dev/null and b/openpilot/selfdrive/assets/training/check.png differ diff --git a/openpilot/selfdrive/assets/training/chevron.png b/openpilot/selfdrive/assets/training/chevron.png new file mode 100644 index 0000000000..8b8aaf948d Binary files /dev/null and b/openpilot/selfdrive/assets/training/chevron.png differ diff --git a/openpilot/selfdrive/assets/training/cross.png b/openpilot/selfdrive/assets/training/cross.png new file mode 100644 index 0000000000..17777e6119 Binary files /dev/null and b/openpilot/selfdrive/assets/training/cross.png differ diff --git a/openpilot/selfdrive/assets/training/icon_disengage.png b/openpilot/selfdrive/assets/training/icon_disengage.png new file mode 100644 index 0000000000..eaf564a02a Binary files /dev/null and b/openpilot/selfdrive/assets/training/icon_disengage.png differ diff --git a/openpilot/selfdrive/assets/training/icon_distracted.png b/openpilot/selfdrive/assets/training/icon_distracted.png new file mode 100644 index 0000000000..9274a4a8f5 Binary files /dev/null and b/openpilot/selfdrive/assets/training/icon_distracted.png differ diff --git a/openpilot/selfdrive/assets/training/icon_driver.png b/openpilot/selfdrive/assets/training/icon_driver.png new file mode 100644 index 0000000000..f6f2c3d911 Binary files /dev/null and b/openpilot/selfdrive/assets/training/icon_driver.png differ diff --git a/openpilot/selfdrive/assets/training/icon_engage.png b/openpilot/selfdrive/assets/training/icon_engage.png new file mode 100644 index 0000000000..9251362627 Binary files /dev/null and b/openpilot/selfdrive/assets/training/icon_engage.png differ diff --git a/openpilot/selfdrive/assets/training/icon_lane_change.png b/openpilot/selfdrive/assets/training/icon_lane_change.png new file mode 100644 index 0000000000..b64170f1a4 Binary files /dev/null and b/openpilot/selfdrive/assets/training/icon_lane_change.png differ diff --git a/openpilot/selfdrive/assets/training/icon_lead.png b/openpilot/selfdrive/assets/training/icon_lead.png new file mode 100644 index 0000000000..caa438b182 Binary files /dev/null and b/openpilot/selfdrive/assets/training/icon_lead.png differ diff --git a/openpilot/selfdrive/assets/training/icon_limited.png b/openpilot/selfdrive/assets/training/icon_limited.png new file mode 100644 index 0000000000..92d68bb70c Binary files /dev/null and b/openpilot/selfdrive/assets/training/icon_limited.png differ diff --git a/openpilot/selfdrive/assets/training/icon_max_speed.png b/openpilot/selfdrive/assets/training/icon_max_speed.png new file mode 100644 index 0000000000..dd47ed04d5 Binary files /dev/null and b/openpilot/selfdrive/assets/training/icon_max_speed.png differ diff --git a/openpilot/selfdrive/assets/training/icon_path.png b/openpilot/selfdrive/assets/training/icon_path.png new file mode 100644 index 0000000000..487a86fc7f Binary files /dev/null and b/openpilot/selfdrive/assets/training/icon_path.png differ diff --git a/openpilot/selfdrive/assets/training/photo_alert.png b/openpilot/selfdrive/assets/training/photo_alert.png new file mode 100644 index 0000000000..46361ba363 Binary files /dev/null and b/openpilot/selfdrive/assets/training/photo_alert.png differ diff --git a/openpilot/selfdrive/assets/training/photo_driver.png b/openpilot/selfdrive/assets/training/photo_driver.png new file mode 100644 index 0000000000..e64368927d Binary files /dev/null and b/openpilot/selfdrive/assets/training/photo_driver.png differ diff --git a/openpilot/selfdrive/assets/training/photo_driver_cam.png b/openpilot/selfdrive/assets/training/photo_driver_cam.png new file mode 100644 index 0000000000..8803f7a601 Binary files /dev/null and b/openpilot/selfdrive/assets/training/photo_driver_cam.png differ diff --git a/openpilot/selfdrive/assets/training/photo_lead.png b/openpilot/selfdrive/assets/training/photo_lead.png new file mode 100644 index 0000000000..22274587a9 Binary files /dev/null and b/openpilot/selfdrive/assets/training/photo_lead.png differ diff --git a/openpilot/selfdrive/assets/training/photo_max_speed.png b/openpilot/selfdrive/assets/training/photo_max_speed.png new file mode 100644 index 0000000000..e77a68480c Binary files /dev/null and b/openpilot/selfdrive/assets/training/photo_max_speed.png differ diff --git a/openpilot/selfdrive/assets/training/photo_path.png b/openpilot/selfdrive/assets/training/photo_path.png new file mode 100644 index 0000000000..f5ea92c1b8 Binary files /dev/null and b/openpilot/selfdrive/assets/training/photo_path.png differ diff --git a/openpilot/selfdrive/assets/training/photo_pedal.png b/openpilot/selfdrive/assets/training/photo_pedal.png new file mode 100644 index 0000000000..9ea5d75bc6 Binary files /dev/null and b/openpilot/selfdrive/assets/training/photo_pedal.png differ diff --git a/openpilot/selfdrive/assets/training/photo_set.png b/openpilot/selfdrive/assets/training/photo_set.png new file mode 100644 index 0000000000..007be1f103 Binary files /dev/null and b/openpilot/selfdrive/assets/training/photo_set.png differ diff --git a/openpilot/selfdrive/assets/training/photo_wheel.png b/openpilot/selfdrive/assets/training/photo_wheel.png new file mode 100644 index 0000000000..919da83a27 Binary files /dev/null and b/openpilot/selfdrive/assets/training/photo_wheel.png differ diff --git a/openpilot/selfdrive/assets/training/step0.png b/openpilot/selfdrive/assets/training/step0.png deleted file mode 100644 index 3c2c5c72a0..0000000000 Binary files a/openpilot/selfdrive/assets/training/step0.png and /dev/null differ diff --git a/openpilot/selfdrive/assets/training/step1.png b/openpilot/selfdrive/assets/training/step1.png deleted file mode 100644 index 0857893118..0000000000 Binary files a/openpilot/selfdrive/assets/training/step1.png and /dev/null differ diff --git a/openpilot/selfdrive/assets/training/step10.png b/openpilot/selfdrive/assets/training/step10.png deleted file mode 100644 index 2941316d17..0000000000 Binary files a/openpilot/selfdrive/assets/training/step10.png and /dev/null differ diff --git a/openpilot/selfdrive/assets/training/step11.png b/openpilot/selfdrive/assets/training/step11.png deleted file mode 100644 index 7a7c72e3df..0000000000 Binary files a/openpilot/selfdrive/assets/training/step11.png and /dev/null differ diff --git a/openpilot/selfdrive/assets/training/step12.png b/openpilot/selfdrive/assets/training/step12.png deleted file mode 100644 index 0d6f64eb84..0000000000 Binary files a/openpilot/selfdrive/assets/training/step12.png and /dev/null differ diff --git a/openpilot/selfdrive/assets/training/step13.png b/openpilot/selfdrive/assets/training/step13.png deleted file mode 100644 index 565e02fa3f..0000000000 Binary files a/openpilot/selfdrive/assets/training/step13.png and /dev/null differ diff --git a/openpilot/selfdrive/assets/training/step14.png b/openpilot/selfdrive/assets/training/step14.png deleted file mode 100644 index 225231cbaa..0000000000 Binary files a/openpilot/selfdrive/assets/training/step14.png and /dev/null differ diff --git a/openpilot/selfdrive/assets/training/step15.png b/openpilot/selfdrive/assets/training/step15.png deleted file mode 100644 index 929c759b26..0000000000 Binary files a/openpilot/selfdrive/assets/training/step15.png and /dev/null differ diff --git a/openpilot/selfdrive/assets/training/step16.png b/openpilot/selfdrive/assets/training/step16.png deleted file mode 100644 index 161af863aa..0000000000 Binary files a/openpilot/selfdrive/assets/training/step16.png and /dev/null differ diff --git a/openpilot/selfdrive/assets/training/step17.png b/openpilot/selfdrive/assets/training/step17.png deleted file mode 100644 index 1b0cdb6fbc..0000000000 Binary files a/openpilot/selfdrive/assets/training/step17.png and /dev/null differ diff --git a/openpilot/selfdrive/assets/training/step18.png b/openpilot/selfdrive/assets/training/step18.png deleted file mode 100644 index 0e3b64bab5..0000000000 Binary files a/openpilot/selfdrive/assets/training/step18.png and /dev/null differ diff --git a/openpilot/selfdrive/assets/training/step2.png b/openpilot/selfdrive/assets/training/step2.png deleted file mode 100644 index 55814b8ef9..0000000000 Binary files a/openpilot/selfdrive/assets/training/step2.png and /dev/null differ diff --git a/openpilot/selfdrive/assets/training/step3.png b/openpilot/selfdrive/assets/training/step3.png deleted file mode 100644 index 831095b0ae..0000000000 Binary files a/openpilot/selfdrive/assets/training/step3.png and /dev/null differ diff --git a/openpilot/selfdrive/assets/training/step4.png b/openpilot/selfdrive/assets/training/step4.png deleted file mode 100644 index 5433034939..0000000000 Binary files a/openpilot/selfdrive/assets/training/step4.png and /dev/null differ diff --git a/openpilot/selfdrive/assets/training/step5.png b/openpilot/selfdrive/assets/training/step5.png deleted file mode 100644 index 7191b63a0c..0000000000 Binary files a/openpilot/selfdrive/assets/training/step5.png and /dev/null differ diff --git a/openpilot/selfdrive/assets/training/step6.png b/openpilot/selfdrive/assets/training/step6.png deleted file mode 100644 index 8eafd4a198..0000000000 Binary files a/openpilot/selfdrive/assets/training/step6.png and /dev/null differ diff --git a/openpilot/selfdrive/assets/training/step7.png b/openpilot/selfdrive/assets/training/step7.png deleted file mode 100644 index 502f5f1b2e..0000000000 Binary files a/openpilot/selfdrive/assets/training/step7.png and /dev/null differ diff --git a/openpilot/selfdrive/assets/training/step8.png b/openpilot/selfdrive/assets/training/step8.png deleted file mode 100644 index 77ff9d7368..0000000000 Binary files a/openpilot/selfdrive/assets/training/step8.png and /dev/null differ diff --git a/openpilot/selfdrive/assets/training/step9.png b/openpilot/selfdrive/assets/training/step9.png deleted file mode 100644 index 84eae3a066..0000000000 Binary files a/openpilot/selfdrive/assets/training/step9.png and /dev/null differ diff --git a/openpilot/selfdrive/ui/layouts/onboarding.py b/openpilot/selfdrive/ui/layouts/onboarding.py index b48c186a10..5838e43ee9 100644 --- a/openpilot/selfdrive/ui/layouts/onboarding.py +++ b/openpilot/selfdrive/ui/layouts/onboarding.py @@ -1,135 +1,21 @@ -import os -import re -import threading from enum import IntEnum from openpilot.starpilot.ui.raylib import rl -from openpilot.common.basedir import BASEDIR from openpilot.system.ui.lib.application import FontWeight, gui_app, TextAlignment from openpilot.system.ui.lib.multilang import tr from openpilot.system.ui.widgets import Widget from openpilot.system.ui.widgets.button import Button, ButtonStyle from openpilot.system.ui.widgets.label import Label from openpilot.selfdrive.ui.ui_state import ui_state +from openpilot.selfdrive.ui.layouts.training_guide import TrainingGuide from openpilot.common.version import terms_version, training_version -DEBUG = False - -STEP_RECTS = [rl.Rectangle(104, 800, 633, 175), rl.Rectangle(1835, 0, 2159, 1080), rl.Rectangle(1835, 0, 2156, 1080), - rl.Rectangle(1526, 473, 427, 472), rl.Rectangle(1643, 441, 217, 223), rl.Rectangle(1835, 0, 2155, 1080), - rl.Rectangle(1786, 591, 267, 236), rl.Rectangle(1353, 0, 804, 1080), rl.Rectangle(1458, 485, 633, 211), - rl.Rectangle(95, 794, 1158, 187), rl.Rectangle(1560, 170, 392, 397), rl.Rectangle(1835, 0, 2159, 1080), - rl.Rectangle(1351, 0, 807, 1080), rl.Rectangle(1835, 0, 2158, 1080), rl.Rectangle(1531, 82, 441, 920), - rl.Rectangle(1336, 438, 490, 393), rl.Rectangle(1835, 0, 2159, 1080), rl.Rectangle(1835, 0, 2159, 1080), - rl.Rectangle(87, 795, 1187, 186)] - -DM_RECORD_STEP = 9 -DM_RECORD_YES_RECT = rl.Rectangle(695, 794, 558, 187) - -RESTART_TRAINING_RECT = rl.Rectangle(87, 795, 472, 186) - - class OnboardingState(IntEnum): TERMS = 0 ONBOARDING = 1 DECLINE = 2 -class TrainingGuide(Widget): - def __init__(self, completed_callback=None): - super().__init__() - self._completed_callback = completed_callback - - self._step = 0 - self._load_image_paths() - - # These textures belong to the guide, not the shared application cache. - self._textures = [gui_app._load_texture_from_image(gui_app._load_image_from_path(self._image_paths[0]))] - self._image_objs = [] - self._released = False - self._lock = threading.Lock() - - threading.Thread(target=self._preload_thread, daemon=True).start() - - def _load_image_paths(self): - paths = [fn for fn in os.listdir(os.path.join(BASEDIR, "openpilot/selfdrive/assets/training")) if re.match(r'^step\d*\.png$', fn)] - paths = sorted(paths, key=lambda x: int(re.search(r'\d+', x).group())) - self._image_paths = [os.path.join(BASEDIR, "openpilot/selfdrive/assets/training", fn) for fn in paths] - - def _preload_thread(self): - # PNG loading is slow in raylib, so we preload in a thread and upload to GPU in main thread - # We've already loaded the first image on init - for path in self._image_paths[1:]: - image = gui_app._load_image_from_path(path) - with self._lock: - if self._released: - rl.unload_image(image) - return - self._image_objs.append(image) - - def release(self): - with self._lock: - self._released = True - textures, self._textures = self._textures, [] - images, self._image_objs = self._image_objs, [] - for texture in textures: - rl.unload_texture(texture) - for image in images: - rl.unload_image(image) - - def hide_event(self): - self.release() - super().hide_event() - - def _handle_mouse_release(self, mouse_pos): - if rl.check_collision_point_rec(rl.Vector2(mouse_pos.x, mouse_pos.y), STEP_RECTS[self._step]): - # Record DM camera? - if self._step == DM_RECORD_STEP: - yes = rl.check_collision_point_rec(rl.Vector2(mouse_pos.x, mouse_pos.y), DM_RECORD_YES_RECT) - print(f"putting RecordFront to {yes}") - ui_state.params.put_bool("RecordFront", yes, block=True) - - # Restart training? - elif self._step == len(self._image_paths) - 1: - if rl.check_collision_point_rec(rl.Vector2(mouse_pos.x, mouse_pos.y), RESTART_TRAINING_RECT): - self._step = -1 - - self._step += 1 - - # Finished? - if self._step >= len(self._image_paths): - self._step = 0 - if self._completed_callback: - self._completed_callback() - - # NOTE: this pops OnboardingWindow during real onboarding - gui_app.pop_widget() - - def _update_state(self): - with self._lock: - if not self._released and self._image_objs: - self._textures.append(gui_app._load_texture_from_image(self._image_objs.pop(0))) - - def _render(self, _): - if not self._textures: - return -1 - # Safeguard against fast tapping - step = min(self._step, len(self._textures) - 1) - rl.draw_texture_ex(self._textures[step], rl.Vector2(0, 0), 0.0, 1.0, rl.WHITE) - - # progress bar - if 0 < step < len(STEP_RECTS) - 1: - h = 20 - w = int((step / (len(STEP_RECTS) - 1)) * self._rect.width) - rl.draw_rectangle(int(self._rect.x), int(self._rect.y + self._rect.height - h), - w, h, rl.Color(70, 91, 234, 255)) - - if DEBUG: - rl.draw_rectangle_lines_ex(STEP_RECTS[step], 3, rl.RED) - - return -1 - - class TermsPage(Widget): def __init__(self, on_accept=None, on_decline=None): super().__init__() diff --git a/openpilot/selfdrive/ui/layouts/settings/device.py b/openpilot/selfdrive/ui/layouts/settings/device.py index 4bd0ab4abf..f7340b0211 100644 --- a/openpilot/selfdrive/ui/layouts/settings/device.py +++ b/openpilot/selfdrive/ui/layouts/settings/device.py @@ -10,7 +10,7 @@ from openpilot.starpilot.ui.calibration_reset import calibration_reset from openpilot.common.swaglog import cloudlog from openpilot.selfdrive.ui.onroad.cabin_camera_dialog import CabinCameraDialog from openpilot.selfdrive.ui.ui_state import ui_state -from openpilot.selfdrive.ui.layouts.onboarding import TrainingGuide +from openpilot.selfdrive.ui.layouts.training_guide import TrainingGuide from openpilot.selfdrive.ui.widgets.pairing_dialog import PairingDialog from openpilot.system.ui.lib.application import FontWeight, gui_app from openpilot.system.ui.lib.multilang import multilang, tr, tr_noop diff --git a/openpilot/selfdrive/ui/layouts/training_guide.py b/openpilot/selfdrive/ui/layouts/training_guide.py new file mode 100644 index 0000000000..dd18c8ec3b --- /dev/null +++ b/openpilot/selfdrive/ui/layouts/training_guide.py @@ -0,0 +1,589 @@ +import os +import threading +from collections.abc import Callable +from typing import NamedTuple + +from openpilot.starpilot.ui.raylib import rl +from openpilot.common.basedir import BASEDIR +from openpilot.system.ui.lib.application import FontWeight, gui_app +from openpilot.system.ui.lib.multilang import tr, tr_noop +from openpilot.system.ui.lib.text_measure import measure_text_cached +from openpilot.system.ui.widgets import Widget +from openpilot.system.ui.widgets.button import Button +from openpilot.selfdrive.ui import UI_BORDER_SIZE +from openpilot.selfdrive.ui.onroad.alert_renderer import (ALERT_BORDER_RADIUS, ALERT_COLORS, ALERT_FONT_BIG, ALERT_FONT_SMALL, ALERT_HEIGHTS, + ALERT_LINE_SPACING, ALERT_MARGIN, ALERT_PADDING, AlertSize, AlertStatus) +from openpilot.selfdrive.ui.onroad.augmented_road_view import BORDER_COLORS +from openpilot.selfdrive.ui.onroad.hud_renderer import COLORS, FONT_SIZES, UI_CONFIG +from openpilot.selfdrive.ui.ui_state import ui_state, UIStatus + +ASSETS = "training" +MARGIN = 112 +PHOTO_X = 1350 + +TITLE_SIZE = 78 +TITLE_LINE = 98 +BODY_SIZE = 55 +BODY_LINE = 73 +BODY_GAP = 48 +INTRO_SIZE = 62 +INTRO_LINE = 90 +ITEM_SIZE = 59 + +# step background, next button +NAVY = (rl.Color(11, 16, 55, 255), rl.Color(70, 91, 234, 255)) +GREEN = (rl.Color(7, 104, 58, 255), rl.Color(35, 154, 97, 255)) +TEAL = (rl.Color(20, 71, 65, 255), rl.Color(37, 163, 149, 255)) +GRAY = (rl.Color(40, 40, 40, 255), rl.Color(101, 101, 101, 255)) +BLUE = (rl.Color(9, 76, 90, 255), rl.Color(4, 140, 170, 255)) +RED = (rl.Color(154, 35, 35, 255), rl.Color(220, 56, 56, 255)) + +# tap targets +NEXT_RECT = rl.Rectangle(1840, 0, 320, 1080) +BACK_RECT = rl.Rectangle(0, 980, 220, 80) +BEGIN_RECT = rl.Rectangle(MARGIN, 804, 618, 163) +DM_NO_RECT = rl.Rectangle(MARGIN, 804, 530, 163) +DM_YES_RECT = rl.Rectangle(706, 804, 530, 163) +RESTART_RECT = rl.Rectangle(MARGIN, 804, 422, 163) +FINISH_RECT = rl.Rectangle(630, 804, 626, 163) +PATH_RECT = rl.Rectangle(1526, 473, 427, 472) +LEAD_RECT = rl.Rectangle(1643, 441, 217, 223) +SET_RECT = rl.Rectangle(1786, 591, 267, 236) +DRIVER_RECT = rl.Rectangle(1353, 0, 804, 1080) +ALERT_RECT = rl.Rectangle(1458, 485, 633, 211) +MAX_SPEED_RECT = rl.Rectangle(1560, 170, 392, 397) +WHEEL_RECT = rl.Rectangle(1351, 0, 807, 1080) +PEDAL_RECT = rl.Rectangle(1336, 438, 490, 393) + +# what goes to the next step, for tests +STEP_RECTS = [BEGIN_RECT, NEXT_RECT, NEXT_RECT, PATH_RECT, LEAD_RECT, NEXT_RECT, SET_RECT, DRIVER_RECT, ALERT_RECT, DM_NO_RECT, + MAX_SPEED_RECT, NEXT_RECT, WHEEL_RECT, NEXT_RECT, NEXT_RECT, PEDAL_RECT, NEXT_RECT, NEXT_RECT, FINISH_RECT] + + +def _shade(color: rl.Color, factor: float) -> rl.Color: + """factor > 0 lightens towards white, < 0 darkens towards black""" + def ch(c): + return int(c + (255 - c) * factor) if factor > 0 else int(c * (1 + factor)) + return rl.Color(ch(color.r), ch(color.g), ch(color.b), color.a) + + +class Span(NamedTuple): + text: str + bold: bool = False + underline: bool = False + + +Paragraph = str | Span | tuple[str | Span, ...] + + +def _font(bold: bool) -> rl.Font: + return gui_app.font(FontWeight.BOLD if bold else FontWeight.NORMAL) + + +def _wrap(paragraph: Paragraph, font_size: int, max_width: float) -> list[list[tuple[str, Span]]]: + """Word wrap a paragraph of space separated styled spans into lines of (text, span) runs.""" + parts = paragraph if isinstance(paragraph, tuple) and not isinstance(paragraph, Span) else (paragraph,) + spans = [Span(p) if isinstance(p, str) else p for p in parts] + words = [(word, span) for span in spans for word in tr(span.text).split()] + + def width(text: str, span: Span) -> float: + return measure_text_cached(_font(span.bold), text, font_size).x + + lines: list[list[tuple[str, Span]]] = [] + line: list[tuple[str, Span]] = [] + line_width = 0. + for text, span in words: + w = width(text, span) + if line: + # spaces between words of one span take its style, e.g. continuous underline + space = (" ", span if line[-1][1] is span else Span(" ")) + if line_width + width(*space) + w <= max_width: + line += [space, (text, span)] + line_width += width(*space) + w + continue + lines.append(line) + line = [(text, span)] + line_width = w + if line: + lines.append(line) + return lines + + +def draw_paragraphs(paragraphs: tuple[Paragraph, ...], x: float, y: float, max_width: float, font_size: int, + line_height: float, paragraph_gap: float = 0) -> float: + """Draws left aligned paragraphs top down, returns the y below the last line.""" + for paragraph in paragraphs: + for line in _wrap(paragraph, font_size, max_width): + run_x = x + for text, span in line: + size = measure_text_cached(_font(span.bold), text, font_size) + rl.draw_text_ex(_font(span.bold), text, rl.Vector2(run_x, y), font_size, 0, rl.WHITE) + if span.underline: + rl.draw_rectangle_rec(rl.Rectangle(run_x, y + font_size * 1.05, size.x, 4), rl.WHITE) + run_x += size.x + y += line_height + y += paragraph_gap + return y - paragraph_gap + + +def draw_distracted_screen(origin: rl.Vector2, photo: rl.Texture): + """Redraw the device screen in the photo as the current onroad UI showing the distracted alert""" + screen = rl.Rectangle(origin.x + 1542, origin.y + 388, 465, 235) + w, h = gui_app.width, gui_app.height + rl.rl_push_matrix() + rl.rl_translatef(screen.x, screen.y, 0) + rl.rl_scalef(screen.width / w, screen.height / h, 1) + + # engaged border, road camera (the road in front of the car in the photo) + rl.draw_rectangle(0, 0, w, h, BORDER_COLORS[UIStatus.ENGAGED]) + content = rl.Rectangle(UI_BORDER_SIZE, UI_BORDER_SIZE, w - 2 * UI_BORDER_SIZE, h - 2 * UI_BORDER_SIZE) + rl.draw_texture_pro(photo, rl.Rectangle(80, 720, 650, 325), content, rl.Vector2(0, 0), 0, rl.WHITE) + + # max and current speed + bold, semi_bold, medium = gui_app.font(FontWeight.BOLD), gui_app.font(FontWeight.SEMI_BOLD), gui_app.font(FontWeight.MEDIUM) + box = rl.Rectangle(content.x + 60, content.y + 45, UI_CONFIG.set_speed_width_imperial, UI_CONFIG.set_speed_height) + rl.draw_rectangle_rounded(box, 0.35, 10, COLORS.BLACK_TRANSLUCENT) + rl.draw_rectangle_rounded_lines_ex(box, 0.35, 10, 6, COLORS.BORDER_TRANSLUCENT) + for text, font, size, y, color in ((tr("MAX"), semi_bold, FONT_SIZES.max_speed, box.y + 27, COLORS.ENGAGED), + ("70", bold, FONT_SIZES.set_speed, box.y + 77, COLORS.WHITE)): + tw = measure_text_cached(font, text, size).x + rl.draw_text_ex(font, text, rl.Vector2(box.x + (box.width - tw) / 2, y), size, 0, color) + for text, font, size, y, color in (("70", bold, FONT_SIZES.current_speed, 180, COLORS.WHITE), + (tr("mph"), medium, FONT_SIZES.speed_unit, 290, COLORS.WHITE_TRANSLUCENT)): + ts = measure_text_cached(font, text, size) + rl.draw_text_ex(font, text, rl.Vector2(content.x + (content.width - ts.x) / 2, y - ts.y / 2), size, 0, color) + + # alert, same layout as AlertRenderer for a mid size alert + alert_h = ALERT_HEIGHTS[AlertSize.mid] + alert = rl.Rectangle(content.x + ALERT_MARGIN, content.y + content.height - alert_h + ALERT_MARGIN, + content.width - 2 * ALERT_MARGIN, alert_h - 2 * ALERT_MARGIN) + rl.draw_rectangle_rounded(alert, ALERT_BORDER_RADIUS / (alert.height / 2), 10, ALERT_COLORS[AlertStatus.userPrompt]) + y = alert.y + ALERT_PADDING + for text, font, size in ((tr("Pay Attention"), bold, ALERT_FONT_BIG), (tr("Driver Distracted"), _font(False), ALERT_FONT_SMALL)): + tw = measure_text_cached(font, text, size).x + rl.draw_text_ex(font, text, rl.Vector2(alert.x + (alert.width - tw) / 2, y), size, 0, rl.WHITE) + y += ALERT_FONT_BIG + ALERT_LINE_SPACING + rl.rl_pop_matrix() + + +class TextureLoader: + """Decode photos in a worker and keep GPU textures owned by this guide.""" + + def __init__(self): + self._images: dict[str, rl.Image] = {} + self._textures: dict[str, rl.Texture] = {} + self._lock = threading.Lock() + self._generation = 0 + + @staticmethod + def _read(path: str) -> rl.Image: + return gui_app._load_image_from_path(os.path.join(BASEDIR, "openpilot/selfdrive/assets", path)) + + def queue(self, paths: list[str]): + threading.Thread(target=self._load, args=(paths, self._generation), daemon=True).start() + + def _load(self, paths: list[str], generation: int): + for path in paths: + with self._lock: + if generation != self._generation: + return + if path in self._textures or path in self._images: + continue + image = self._read(path) + with self._lock: + if generation == self._generation and path not in self._textures and path not in self._images: + self._images[path] = image + continue + rl.unload_image(image) + + def get(self, path: str) -> rl.Texture: + if path not in self._textures: + with self._lock: + image = self._images.pop(path, None) + texture = gui_app._load_texture_from_image(image if image is not None else self._read(path)) + with self._lock: + self._textures[path] = texture + duplicate = self._images.pop(path, None) + if duplicate is not None: + rl.unload_image(duplicate) + return self._textures[path] + + def release(self): + with self._lock: + self._generation += 1 + textures, self._textures = self._textures, {} + images, self._images = self._images, {} + for texture in textures.values(): + rl.unload_texture(texture) + for image in images.values(): + rl.unload_image(image) + + +class TapArea(Widget): + """Invisible tap target, e.g. part of a photo.""" + + def __init__(self, click_callback: Callable[[], None]): + super().__init__() + self._click_callback = click_callback + + def _render(self, _): + pass + + +class BackButton(Widget): + """Small "< Back" in the bottom left corner.""" + + def __init__(self, click_callback: Callable[[], None]): + super().__init__() + self._click_callback = click_callback + + def _render(self, rect: rl.Rectangle): + color = rl.Color(255, 255, 255, 140 if self.is_pressed else 200) + font = gui_app.font(FontWeight.MEDIUM) + text_size = measure_text_cached(font, tr("Back"), 30) + chevron = gui_app.texture(f"{ASSETS}/chevron.png", 22, 26, flip_x=True) + y = rect.y + rect.height / 2 + rl.draw_texture_v(chevron, rl.Vector2(rect.x + 40, y - chevron.height / 2), color) + rl.draw_text_ex(font, tr("Back"), rl.Vector2(rect.x + 72, y - text_size.y / 2), 30, 0, color) + + +class NextButton(Widget): + """Colored column on the right with a chevron.""" + + def __init__(self, color: rl.Color, click_callback: Callable[[], None]): + super().__init__() + self._color = color + self._pressed_color = _shade(color, -0.2) + self._click_callback = click_callback + + def _render(self, rect: rl.Rectangle): + rl.draw_rectangle_rec(rect, self._pressed_color if self.is_pressed else self._color) + chevron = gui_app.texture(f"{ASSETS}/chevron.png") + rl.draw_texture_v(chevron, rl.Vector2(rect.x + (rect.width - chevron.width) / 2, rect.y + (rect.height - chevron.height) / 2), rl.WHITE) + + +class GuideButton(Button): + def __init__(self, text: str, click_callback: Callable[[], None], color: rl.Color, text_color: rl.Color = rl.BLACK): + super().__init__(lambda: tr(text), click_callback, font_size=55, font_weight=FontWeight.BOLD, border_radius=24) + self._color = color + self._pressed_color = _shade(color, -0.25 if color.r > 128 else 0.15) + self._text_color = text_color + + def _update_state(self): + self._background_color = self._pressed_color if self.is_pressed else self._color + self._label.set_text_color(self._text_color) + + +class Step(Widget): + def __init__(self, bg: rl.Color): + super().__init__() + self._bg = bg + self._controls: list[tuple[Widget, rl.Rectangle]] = [] + + def textures(self) -> list[str]: + """Big textures to preload""" + return [] + + def add_control(self, widget: Widget, rect: rl.Rectangle): + self._controls.append((self._child(widget), rect)) + + def _render(self, rect: rl.Rectangle): + rl.draw_rectangle_rec(rect, self._bg) + self._draw(rect) + for widget, r in self._controls: + widget.render(rl.Rectangle(rect.x + r.x, rect.y + r.y, r.width, r.height)) + + def _draw(self, rect: rl.Rectangle): + pass + + +class SectionStep(Step): + """Text only step with a section number or an icon.""" + + def __init__(self, badge: int | str, title: str, body: tuple[Paragraph, ...], colors, on_next, paragraph_gap: int = 0): + super().__init__(colors[0]) + self._badge = badge + self._title = title + self._body = body + self._paragraph_gap = paragraph_gap + self.add_control(NextButton(colors[1], on_next), NEXT_RECT) + + def _draw(self, rect: rl.Rectangle): + if isinstance(self._badge, int): + center = rl.Vector2(rect.x + MARGIN + 64, rect.y + 176) + rl.draw_circle_v(center, 64, rl.Color(255, 255, 255, 64)) + number = str(self._badge) + size = measure_text_cached(_font(True), number, TITLE_SIZE) + rl.draw_text_ex(_font(True), number, rl.Vector2(center.x - size.x / 2, center.y - size.y / 2), TITLE_SIZE, 0, rl.WHITE) + title_x = 288 + else: + rl.draw_texture_v(gui_app.texture(f"{ASSETS}/icon_{self._badge}.png"), rl.Vector2(rect.x + MARGIN, rect.y + 112), rl.WHITE) + title_x = MARGIN + 196 + draw_paragraphs((Span(self._title, bold=True),), rect.x + title_x, rect.y + 127, NEXT_RECT.x - title_x - MARGIN, TITLE_SIZE, TITLE_LINE) + draw_paragraphs(self._body, rect.x + MARGIN, rect.y + 294, 1640, INTRO_SIZE, INTRO_LINE, self._paragraph_gap) + + +class ChecklistStep(Step): + def __init__(self, title: str, items: tuple[str, ...], mark: str, colors, on_next): + super().__init__(colors[0]) + self._title = title + self._items = items + self._mark = f"{ASSETS}/{mark}.png" + self.add_control(NextButton(colors[1], on_next), NEXT_RECT) + + def _draw(self, rect: rl.Rectangle): + width = NEXT_RECT.x - 288 - MARGIN + y = draw_paragraphs((Span(self._title, bold=True),), rect.x + MARGIN, rect.y + 112, width, TITLE_SIZE, TITLE_LINE) + 107 + mark = gui_app.texture(self._mark) + for item in self._items: + # items are centered on rows + lines = len(_wrap(item, ITEM_SIZE, width)) + item_y = y - (lines - 1) * BODY_LINE / 4 + rl.draw_texture_v(mark, rl.Vector2(rect.x + 100, item_y + (lines * BODY_LINE - mark.height) / 2 - 6), rl.WHITE) + draw_paragraphs((item,), rect.x + 288, item_y - 8, width, ITEM_SIZE, BODY_LINE) + y += 166 + (lines - 1) * BODY_LINE + + +class PhotoStep(Step): + """Text on the left and a photo on the right. Tap the photo (or part of it) to continue.""" + + def __init__(self, icon: str, title: str, body: tuple[Paragraph, ...], prompt: str | None, photo: str, colors, + loader: TextureLoader, tap_rect: rl.Rectangle | None = None, on_next: Callable[[], None] | None = None, + overlay: Callable[[rl.Vector2, rl.Texture], None] | None = None): + super().__init__(colors[0]) + self._overlay = overlay + self._icon = f"{ASSETS}/icon_{icon}.png" + self._title = title + self._body = body + self._prompt = prompt + self._photo = f"{ASSETS}/photo_{photo}.png" + self._loader = loader + if tap_rect is not None and on_next is not None: + self.add_control(TapArea(on_next), tap_rect) + + def textures(self) -> list[str]: + return [self._photo] + + def _draw(self, rect: rl.Rectangle): + photo = self._loader.get(self._photo) + rl.draw_texture_v(photo, rl.Vector2(rect.x + PHOTO_X, rect.y), rl.WHITE) + if self._overlay: + self._overlay(rl.Vector2(rect.x, rect.y), photo) + + # icon is centered on the title + title = Span(self._title, bold=True) + title_x = MARGIN + 196 + title_width = PHOTO_X - title_x - MARGIN + title_lines = len(_wrap(title, TITLE_SIZE, title_width)) + title_y = 136 - (title_lines - 1) * 23 + icon = gui_app.texture(self._icon) + rl.draw_texture_v(icon, rl.Vector2(rect.x + MARGIN, rect.y + 112 + (title_lines - 1) * 25), rl.WHITE) + draw_paragraphs((title,), rect.x + title_x, rect.y + title_y, title_width, TITLE_SIZE, TITLE_LINE) + body_y = rect.y + 307 + (title_lines - 1) * 50 + draw_paragraphs(self._body, rect.x + MARGIN, body_y, PHOTO_X - 2 * MARGIN, BODY_SIZE, BODY_LINE, BODY_GAP) + + if self._prompt: + draw_paragraphs((Span(self._prompt, bold=True),), rect.x + MARGIN, rect.y + 894, PHOTO_X - 2 * MARGIN, BODY_SIZE, BODY_LINE) + + +class HeroStep(Step): + """Full screen photo with text and buttons.""" + + def __init__(self, photo: str, title: str, body: Paragraph, loader: TextureLoader): + super().__init__(rl.BLACK) + self._photo = f"{ASSETS}/{photo}.png" + self._title = title + self._body = body + self._loader = loader + + def textures(self) -> list[str]: + return [self._photo] + + def _draw(self, rect: rl.Rectangle): + rl.draw_texture_v(self._loader.get(self._photo), rl.Vector2(rect.x, rect.y), rl.WHITE) + y = draw_paragraphs((Span(self._title, bold=True),), rect.x + MARGIN, rect.y + 112, 1180, TITLE_SIZE, TITLE_LINE) + draw_paragraphs((self._body,), rect.x + MARGIN, y + 52, 1440, INTRO_SIZE, INTRO_LINE) + + +class TrainingGuide(Widget): + def __init__(self, completed_callback=None): + super().__init__() + self._completed_callback = completed_callback + self._step = 0 + + nxt = self._next + self._loader = loader = TextureLoader() + + welcome = HeroStep("bg_welcome", tr_noop("Welcome to StarPilot"), + tr_noop("Let's review the functionality and limitations of StarPilot before getting started."), + loader) + welcome.add_control(GuideButton(tr_noop("Begin Training"), nxt, rl.WHITE), BEGIN_RECT) + + dm_record = PhotoStep("driver", tr_noop("Improve Driver Monitoring"), ( + ( + tr_noop("Help improve driver monitoring by including your driving data in the training data set."), + tr_noop("Your preference can be changed at any time in Settings. Would you like to share your data?"), + ), + ), None, "driver_cam", TEAL, loader) + dm_record.add_control(GuideButton(tr_noop("No"), lambda: self._record_front(False), _shade(TEAL[0], -0.55), rl.WHITE), DM_NO_RECT) + dm_record.add_control(GuideButton(tr_noop("Yes"), lambda: self._record_front(True), rl.WHITE), DM_YES_RECT) + + finish = HeroStep("bg_finish", tr_noop("Congratulations! You have completed StarPilot training."), + ( + tr_noop("This guide can be replayed at any time from the device settings."), + tr_noop("See the StarPilot wiki at github.com/StarPilotHQ/StarPilot for features and supported vehicles."), + ), + loader) + finish.add_control(GuideButton(tr_noop("Restart"), self._restart, rl.Color(64, 64, 64, 255), rl.WHITE), RESTART_RECT) + finish.add_control(GuideButton(tr_noop("Finish Training"), nxt, rl.WHITE), FINISH_RECT) + + self._steps: list[Step] = [ + welcome, + SectionStep(1, tr_noop("What is StarPilot?"), ( + tr_noop("StarPilot is an advanced driver assistance system."), + Span(tr_noop("A driver assistance system is not a self-driving car."), underline=True), + ( + tr_noop("This means StarPilot is designed to work with you, not without you."), + tr_noop("Your attention is required at all times to drive with StarPilot."), + ), + ), NAVY, nxt), + ChecklistStep(tr_noop("Before we continue, please confirm the following:"), ( + tr_noop("I will keep my eyes on the road."), + tr_noop("I will be ready to take over at any time."), + tr_noop("I will keep control of my vehicle."), + ), "check", GREEN, nxt), + PhotoStep("path", tr_noop("The Driving Path"), ( + tr_noop("StarPilot uses the road-facing camera to plan the best path to drive."), + tr_noop("Alongside this path, lane lines are displayed in white and road edges are displayed in red."), + ), tr_noop("Tap the driving path to continue"), "path", NAVY, loader, PATH_RECT, nxt), + PhotoStep("lead", tr_noop("The Lead Car Indicator"), (( + tr_noop("The lead car indicator is displayed as a triangle under the lead car."), + tr_noop("StarPilot can detect 2 cars simultaneously. The second triangle will appear when a cut-in is detected."), + tr_noop("If no triangle is present, your car may be using its stock ACC system, and some StarPilot features may not be available."), + "–", + Span(tr_noop("Tap the lead car indicator"), bold=True), + ),), None, "lead", NAVY, loader, LEAD_RECT, nxt), + SectionStep(2, tr_noop("How to engage and control StarPilot"), ( + ( + tr_noop("StarPilot is controlled using your car's cruise control inputs."), + tr_noop("These are usually located on the steering wheel or on a control lever near the steering column."), + ), + ), TEAL, nxt), + PhotoStep("engage", tr_noop("Engage StarPilot"), ( + ( + tr_noop("When you are ready to engage StarPilot at a comfortable speed, engage the cruise control system and press “SET” to begin."), + tr_noop("A green border shows combined control while cruising. An orange border can show why StarPilot is slowing."), + ), + ), tr_noop("Tap “SET” in the image to continue"), "set", TEAL, loader, SET_RECT, nxt), + PhotoStep("driver", tr_noop("Driver Monitoring"), ( + ( + tr_noop("StarPilot monitors your awareness through the device's driver-facing camera."), + tr_noop("If StarPilot senses that your eyes aren't on the road, the system will go through a series of alerts and actions."), + ), + ), tr_noop("Tap the driver to continue"), "driver", TEAL, loader, DRIVER_RECT, nxt), + PhotoStep("distracted", tr_noop("Distracted Driving"), ( + ( + tr_noop("You must pay attention at all times."), + tr_noop("If you are distracted, StarPilot will show alerts of increasing severity, and you will be locked out from engaging."), + tr_noop("If the problem persists, the system will begin decelerating the vehicle."), + ), + ), tr_noop("Tap the alert to continue"), "alert", TEAL, loader, ALERT_RECT, nxt, draw_distracted_screen), + dm_record, + PhotoStep("max_speed", tr_noop("Adjust the Max Speed"), ( + tr_noop("Press + or - on your cruise control inputs to adjust the max speed shown in the upper left corner."), + tr_noop("This is only a maximum, not a set speed. StarPilot may drive slower for curves, the road scene, red lights, and stop signs."), + ), tr_noop("Tap the max speed to continue"), "max_speed", TEAL, loader, MAX_SPEED_RECT, nxt), + SectionStep(3, tr_noop("How to change lanes with StarPilot"), ( + ( + tr_noop("If you are traveling above 20 mph (32 km/h), StarPilot can perform a lane change."), + tr_noop("Keep in mind that it is not capable of checking if a lane change is safe. This is your job."), + tr_noop("Once initiated, StarPilot will change lanes regardless if another vehicle is present."), + ), + ), GRAY, nxt), + PhotoStep("lane_change", tr_noop("Perform Lane Change"), ( + ( + tr_noop("With StarPilot engaged, turn on your signal and check your surroundings."), + tr_noop("When it's safe, gently nudge the steering wheel towards your desired lane."), + tr_noop("The sequence of turn signal and wheel nudge will prompt StarPilot to change lanes."), + ), + ), tr_noop("Tap the steering wheel to continue"), "wheel", GRAY, loader, WHEEL_RECT, nxt), + SectionStep(4, tr_noop("How to disengage StarPilot"), ( + tr_noop("When you encounter a potentially unsafe situation or need to exit a highway, press the brake pedal to disengage speed control and take over steering."), + ), BLUE, nxt), + SectionStep("limited", tr_noop("Limited Features"), ( + tr_noop("Keep in mind that StarPilot will not recognize certain scenarios including construction zones, close cut-ins, or pedestrians."), + tr_noop("You must stay alert and be ready to retake control of the vehicle at all times."), + ), BLUE, nxt, BODY_GAP), + PhotoStep("disengage", tr_noop("Disengage StarPilot"), ( + ( + tr_noop("While StarPilot is engaged, you may keep your hands on the wheel to override steering controls."), + tr_noop("Braking disengages speed control. If Always-On Lateral is armed, steering assist may continue. Use your steering assist control to turn it off."), + ), + ), tr_noop("Tap the brake pedal to continue"), "pedal", BLUE, loader, PEDAL_RECT, nxt), + ChecklistStep(tr_noop("Let's review. StarPilot can:"), ( + tr_noop("Determine a path to drive."), + tr_noop("Maintain a maximum speed."), + tr_noop("Maintain a safe distance from a lead car."), + tr_noop("Change lanes with driver assistance."), + ), "check", GREEN, nxt), + ChecklistStep(tr_noop("StarPilot cannot:"), ( + tr_noop("Stay engaged while the driver is distracted."), + tr_noop("See other cars during a lane change."), + tr_noop("Avoid construction barriers, cones, or pedestrians."), + tr_noop("React to unsafe situations like close vehicle cut-ins or road hazards."), + ), "cross", RED, nxt), + finish, + ] + assert len(self._steps) == len(STEP_RECTS) + + for step in self._steps: + self._child(step) + self._back_button = self._child(BackButton(self._back)) + self._back_button.set_visible(lambda: self._step > 0) + self._photo_paths = [path for step in self._steps for path in step.textures()] + self._prepare_textures() + + def _prepare_textures(self): + if not self._loader._textures: + self._loader.get(self._photo_paths[0]) + self._loader.queue(self._photo_paths[1:]) + + def show_event(self): + self._prepare_textures() + super().show_event() + + def hide_event(self): + self._loader.release() + super().hide_event() + + def _next(self): + self._step += 1 + if self._step >= len(self._steps): + self._step = 0 + if self._completed_callback: + self._completed_callback() + + # NOTE: this pops OnboardingWindow during real onboarding + gui_app.pop_widget() + + def _back(self): + self._step = max(self._step - 1, 0) + + def _restart(self): + self._step = 0 + + def _record_front(self, record: bool): + ui_state.params.put_bool("RecordFront", record, block=True) + self._next() + + def _render(self, rect: rl.Rectangle): + self._steps[self._step].render(rect) + + # progress bar + if 0 < self._step < len(self._steps) - 1: + h = 20 + w = int((self._step / (len(self._steps) - 1)) * rect.width) + rl.draw_rectangle(int(rect.x), int(rect.y + rect.height - h), w, h, rl.Color(70, 91, 234, 255)) + + self._back_button.render(rl.Rectangle(rect.x + BACK_RECT.x, rect.y + BACK_RECT.y, BACK_RECT.width, BACK_RECT.height)) + return -1 diff --git a/openpilot/selfdrive/ui/tests/diff/replay_script.py b/openpilot/selfdrive/ui/tests/diff/replay_script.py index f02009b1cd..eea4eef50d 100644 --- a/openpilot/selfdrive/ui/tests/diff/replay_script.py +++ b/openpilot/selfdrive/ui/tests/diff/replay_script.py @@ -401,14 +401,9 @@ def build_tizi_script(pm: PubMaster, main_layout, script: Script) -> None: def do_onboarding() -> None: """Click through the training guide and close.""" - from openpilot.selfdrive.ui.layouts.onboarding import STEP_RECTS - step = 0 - for step_rect in STEP_RECTS: - if step < len(STEP_RECTS) - 1: - script.click(int(step_rect.x), int(step_rect.y), wait_after=FAST_CLICK) - else: - script.click(950, 900) # On the last step, click Finish instead of restart - step += 1 + from openpilot.selfdrive.ui.layouts.training_guide import STEP_RECTS + for r in STEP_RECTS: + script.click(int(r.x + r.width / 2), int(r.y + r.height / 2), wait_after=FAST_CLICK) def type_keyboard() -> None: """Types 8 characters using the big keyboard to test different layouts and interactions."""