Files
firestar5683 7d7d39faca UI Replay
2026-04-12 23:03:57 -05:00

125 lines
5.2 KiB
C++

#include "selfdrive/ui/qt/onroad/buttons.h"
#include <QPainter>
#include "selfdrive/ui/qt/util.h"
void drawIcon(QPainter &p, const QPoint &center, const QPixmap &img, const QBrush &bg, float opacity, const int &angle) {
p.setRenderHint(QPainter::Antialiasing);
p.setOpacity(1.0); // bg dictates opacity of ellipse
p.setPen(Qt::NoPen);
p.setBrush(bg);
p.drawEllipse(center, btn_size / 2, btn_size / 2);
p.save();
p.translate(center);
p.rotate(angle);
p.setOpacity(opacity);
p.drawPixmap(-QPoint(img.width() / 2, img.height() / 2), img);
p.restore();
p.setOpacity(1.0);
}
// ExperimentalButton
ExperimentalButton::ExperimentalButton(QWidget *parent) : QPushButton(parent), experimental_mode(false), engageable(false), steering_angle_deg(0) {
setFixedSize(btn_size, btn_size);
engage_img = loadPixmap("../assets/icons/chffr_wheel.png", {img_size, img_size});
experimental_img = loadPixmap("../assets/icons/experimental.svg", {img_size, img_size});
QObject::connect(this, &QPushButton::clicked, this, &ExperimentalButton::changeMode);
QObject::connect(starpilotUIState(), &StarPilotUIState::themeUpdated, this, &ExperimentalButton::updateTheme);
}
void ExperimentalButton::changeMode() {
const auto cp = (*uiState()->sm)["carParams"].getCarParams();
if (params.getBool("SafeMode")) {
return;
}
bool can_change = hasLongitudinalControl(cp) && params.getBool("ExperimentalModeConfirmed");
if (can_change) {
if (starpilot_toggles.value("conditional_experimental_mode").toBool()) {
int override_value = (starpilot_scene.conditional_status == 1 || starpilot_scene.conditional_status == 2) ? 0 : experimental_mode ? 1 : 2;
params_memory.putInt("CEStatus", override_value);
params.putInt("PersistedCEStatus", params.getBool("PersistExperimentalState") ? override_value : 0);
} else {
params.putBool("ExperimentalMode", !experimental_mode);
}
}
}
void ExperimentalButton::updateState(const UIState &s, const StarPilotUIState &fs) {
const auto cs = (*s.sm)["selfdriveState"].getSelfdriveState();
bool eng = cs.getEngageable() || cs.getEnabled() || fs.starpilot_scene.always_on_lateral_active;
if ((cs.getExperimentalMode() != experimental_mode) || (eng != engageable)) {
engageable = eng;
experimental_mode = cs.getExperimentalMode();
update();
}
const cereal::CarState::Reader &carState = (*s.sm)["carState"].getCarState();
updateBackgroundColor();
int current_steering_angle_deg = -carState.getSteeringAngleDeg();
if (current_steering_angle_deg != steering_angle_deg && starpilot_toggles.value("rotating_wheel").toBool()) {
steering_angle_deg = current_steering_angle_deg;
update();
} else if (!starpilot_toggles.value("rotating_wheel").toBool()) {
steering_angle_deg = 0;
}
if (params_memory.getBool("UpdateWheelImage")) {
updateTheme();
params_memory.remove("UpdateWheelImage");
}
}
void ExperimentalButton::paintEvent(QPaintEvent *event) {
QPainter p(this);
p.setClipRegion(QRegion(QRect(0, 0, btn_size, btn_size), QRegion::Ellipse));
p.setRenderHint(QPainter::Antialiasing);
if (starpilot_toggles.value("wheel_image").toString() == "stock") {
QPixmap img = experimental_mode ? experimental_img : engage_img;
drawIcon(p, QPoint(btn_size / 2, btn_size / 2), img, background_color, (isDown() || !engageable) ? 0.6 : 1.0, steering_angle_deg);
} else if (wheel_gif) {
drawIcon(p, QPoint(btn_size / 2, btn_size / 2), wheel_gif->currentPixmap(), background_color, (isDown() || !engageable) ? 0.6 : 1.0, steering_angle_deg);
} else if (!wheel_img.isNull()) {
drawIcon(p, QPoint(btn_size / 2, btn_size / 2), wheel_img, background_color, (isDown() || !engageable) ? 0.6 : 1.0, steering_angle_deg);
} else {
QPixmap img = experimental_mode ? experimental_img : engage_img;
drawIcon(p, QPoint(btn_size / 2, btn_size / 2), img, background_color, (isDown() || !engageable) ? 0.6 : 1.0, steering_angle_deg);
}
}
void ExperimentalButton::showEvent(QShowEvent *event) {
updateTheme();
}
void ExperimentalButton::updateBackgroundColor() {
if (starpilot_toggles.value("simple_mode").toBool()) {
background_color = QColor(0, 0, 0, 166);
} else if (isDown() || !engageable) {
background_color = QColor(0, 0, 0, 166);
} else if (starpilot_scene.switchback_mode_enabled) {
background_color = bg_colors[STATUS_SWITCHBACK_MODE_ENABLED];
} else if (starpilot_scene.always_on_lateral_active) {
background_color = bg_colors[STATUS_ALWAYS_ON_LATERAL_ACTIVE];
} else if (starpilot_scene.conditional_status == 1) {
background_color = bg_colors[STATUS_CEM_DISABLED];
} else if (experimental_mode) {
background_color = bg_colors[STATUS_EXPERIMENTAL_MODE_ENABLED];
} else if (starpilot_scene.traffic_mode_enabled) {
background_color = bg_colors[STATUS_TRAFFIC_MODE_ENABLED];
} else {
background_color = QColor(0, 0, 0, 166);
}
}
void ExperimentalButton::updateTheme() {
loadImage("../../starpilot/assets/active_theme/steering_wheel/wheel", wheel_img, wheel_gif, QSize(img_size, img_size), this);
if (!wheel_gif && wheel_img.isNull()) {
loadImage("../../starpilot/assets/stock_theme/steering_wheel/wheel", wheel_img, wheel_gif, QSize(img_size, img_size), this);
}
}