Files
StarPilot/openpilot/starpilot/ui/clip.py
T
firestar5683 a82935f326 User interfaces and Android Auto
Integrate onroad views, configurable widgets, local settings, steering-profile contracts, Bluetooth and Android Auto with their device and runtime services.

Co-authored-by: Adeeb Shihadeh <adeebshihadeh@gmail.com>
Co-authored-by: Daniel Koepping <elkoled@gmail.com>
Co-authored-by: Danny <dannyirwin31@yahoo.com>
Co-authored-by: DimitriChrysafis <Dimitri.chrysafis@gmail.com>
Co-authored-by: Dom <168790843+firestar5683@users.noreply.github.com>
Co-authored-by: Harald Schäfer <harald.the.engineer@gmail.com>
Co-authored-by: Prabhaav Pillai <prabhaav@msu.edu>
Co-authored-by: Shane Smiskol <shane@smiskol.com>
Co-authored-by: dirwin31 <83434411+dirwin31@users.noreply.github.com>
Co-authored-by: firestarsdog <229254897+firestarsdog@users.noreply.github.com>
Co-authored-by: stef <19478336+stefpi@users.noreply.github.com>
Co-authored-by: whoisdomi <dcantu83@gmail.com>

Co-authored-by: Intelli <contact@intelli.software>
Co-authored-by: pharmacomaniac <blittle65@gmail.com>

Co-authored-by: prabhaavp <prabhaav@msu.edu>
2026-10-14 20:02:39 -05:00

73 lines
2.5 KiB
Python

"""Screen-space clipping for native shell views drawn at a widget offset."""
from __future__ import annotations
from contextlib import contextmanager
from contextvars import ContextVar
from collections.abc import Iterator
import pyray as rl
_placement: ContextVar[tuple[float, float, rl.Rectangle, rl.Rectangle | None] | None] = ContextVar("shell_placement", default=None)
def _intersection(a: rl.Rectangle, b: rl.Rectangle) -> rl.Rectangle:
x = max(a.x, b.x)
y = max(a.y, b.y)
return rl.Rectangle(x, y, max(0, min(a.x + a.width, b.x + b.width) - x),
max(0, min(a.y + a.height, b.y + b.height) - y))
@contextmanager
def placed_at(rect: rl.Rectangle, parent_clip: rl.Rectangle | None = None) -> Iterator[None]:
"""Translate view geometry and its explicit scissors together.
The compact scroller already has a screen-space scissor. Child scissors replace
it in raylib, so each child clip is intersected with that parent and restored.
"""
viewport = _intersection(rect, parent_clip) if parent_clip is not None else rect
token = _placement.set((rect.x, rect.y, viewport, parent_clip))
rl.rl_push_matrix()
rl.rl_translatef(rect.x, rect.y, 0)
try:
yield
finally:
rl.rl_pop_matrix()
if parent_clip is not None:
rl.begin_scissor_mode(int(parent_clip.x), int(parent_clip.y), int(parent_clip.width), int(parent_clip.height))
else:
rl.end_scissor_mode()
_placement.reset(token)
@contextmanager
def clipped(rect: rl.Rectangle, parent: rl.Rectangle) -> Iterator[None]:
bounds = _intersection(rect, parent)
rl.rl_draw_render_batch_active()
begin_scissor_mode(bounds.x, bounds.y, bounds.width, bounds.height)
try:
yield
finally:
rl.rl_draw_render_batch_active()
begin_scissor_mode(parent.x, parent.y, parent.width, parent.height)
def begin_scissor_mode(x: float, y: float, width: float, height: float) -> None:
placement = _placement.get()
if placement is None:
rl.begin_scissor_mode(int(x), int(y), int(width), int(height))
return
ox, oy, viewport, _ = placement
clipped = _intersection(rl.Rectangle(x + ox, y + oy, width, height), viewport)
rl.begin_scissor_mode(int(clipped.x), int(clipped.y), int(clipped.width), int(clipped.height))
def end_scissor_mode() -> None:
placement = _placement.get()
if placement is None:
rl.end_scissor_mode()
return
_, _, viewport, _ = placement
rl.begin_scissor_mode(int(viewport.x), int(viewport.y), int(viewport.width), int(viewport.height))