From 846a58507fb2e19d399555ad321c0208f1dec0d9 Mon Sep 17 00:00:00 2001 From: Dean Lee Date: Sun, 3 May 2020 02:27:03 +0800 Subject: [PATCH] simplify drawing text and rect (#1446) * add ui_draw_text remove draw textbox make draw_text static add blank line * add ui_draw_text remove draw textbox make draw_text static add blank line add ui_draw_rect draw rounded rect fx typo * remove used variables * reduce lines fix typo * resolve confilit --- selfdrive/ui/paint.cc | 261 +++++++++++++--------------------------- selfdrive/ui/sidebar.cc | 20 +-- selfdrive/ui/ui.hpp | 2 + 3 files changed, 89 insertions(+), 194 deletions(-) diff --git a/selfdrive/ui/paint.cc b/selfdrive/ui/paint.cc index 747236800f..09ff1426ad 100644 --- a/selfdrive/ui/paint.cc +++ b/selfdrive/ui/paint.cc @@ -57,6 +57,13 @@ static float lerp(float v0, float v1, float t) { return (1 - t) * v0 + t * v1; } +static void ui_draw_text(NVGcontext *vg, float x, float y, const char* string, float size, NVGcolor color, int font){ + nvgFontFaceId(vg, font); + nvgFontSize(vg, size); + nvgFillColor(vg, color); + nvgText(vg, x, y, string, NULL); +} + static void draw_chevron(UIState *s, float x_in, float y_in, float sz, NVGcolor fillColor, NVGcolor glowColor) { const vec4 p_car_space = (vec4){{x_in, y_in, 0., 1.}}; @@ -406,11 +413,6 @@ static void ui_draw_vision_maxspeed(UIState *s) { /*if (!s->longitudinal_control){ return; }*/ - - const UIScene *scene = &s->scene; - int ui_viz_rx = scene->ui_viz_rx; - int ui_viz_rw = scene->ui_viz_rw; - char maxspeed_str[32]; float maxspeed = s->scene.v_cruise; int maxspeed_calc = maxspeed * 0.6225 + 0.5; @@ -430,7 +432,7 @@ static void ui_draw_vision_maxspeed(UIState *s) { int viz_maxspeed_w = 184; int viz_maxspeed_h = 202; - int viz_maxspeed_x = (ui_viz_rx + (bdr_s*2)); + int viz_maxspeed_x = (s->scene.ui_viz_rx + (bdr_s*2)); int viz_maxspeed_y = (box_y + (bdr_s*1.5)); int viz_maxspeed_xo = 180; @@ -442,63 +444,32 @@ static void ui_draw_vision_maxspeed(UIState *s) { #endif // Draw Background - nvgBeginPath(s->vg); - nvgRoundedRect(s->vg, viz_maxspeed_x, viz_maxspeed_y, viz_maxspeed_w, viz_maxspeed_h, 30); - if (is_set_over_limit) { - nvgFillColor(s->vg, nvgRGBA(218, 111, 37, 180)); - } else { - nvgFillColor(s->vg, COLOR_BLACK_ALPHA(100)); - } - nvgFill(s->vg); + ui_draw_rect(s->vg, viz_maxspeed_x, viz_maxspeed_y, viz_maxspeed_w, viz_maxspeed_h, + is_set_over_limit ? nvgRGBA(218, 111, 37, 180) : COLOR_BLACK_ALPHA(100), 30); // Draw Border - nvgBeginPath(s->vg); - nvgRoundedRect(s->vg, viz_maxspeed_x, viz_maxspeed_y, viz_maxspeed_w, viz_maxspeed_h, 20); + NVGcolor color = COLOR_WHITE_ALPHA(100); if (is_set_over_limit) { - nvgStrokeColor(s->vg, COLOR_OCHRE); - } else if (is_speedlim_valid && !s->is_ego_over_limit) { - nvgStrokeColor(s->vg, COLOR_WHITE); - } else if (is_speedlim_valid && s->is_ego_over_limit) { - nvgStrokeColor(s->vg, COLOR_WHITE_ALPHA(20)); - } else { - nvgStrokeColor(s->vg, COLOR_WHITE_ALPHA(100)); + color = COLOR_OCHRE; + } else if (is_speedlim_valid) { + color = s->is_ego_over_limit ? COLOR_WHITE_ALPHA(20) : COLOR_WHITE; } - nvgStrokeWidth(s->vg, 10); - nvgStroke(s->vg); - // Draw "MAX" Text + ui_draw_rect(s->vg, viz_maxspeed_x, viz_maxspeed_y, viz_maxspeed_w, viz_maxspeed_h, color, 20, 10); + nvgTextAlign(s->vg, NVG_ALIGN_CENTER | NVG_ALIGN_BASELINE); - nvgFontFaceId(s->vg, s->font_sans_regular); - nvgFontSize(s->vg, 26*2.5); - if (is_cruise_set) { - nvgFillColor(s->vg, COLOR_WHITE_ALPHA(200)); - } else { - nvgFillColor(s->vg, COLOR_WHITE_ALPHA(100)); - } const int text_x = viz_maxspeed_x + (viz_maxspeed_xo / 2) + (viz_maxspeed_w / 2); - nvgText(s->vg, text_x, 148, "MAX", NULL); + ui_draw_text(s->vg, text_x, 148, "MAX", 26 * 2.5, COLOR_WHITE_ALPHA(is_cruise_set ? 200 : 100), s->font_sans_regular); - // Draw Speed Text - nvgFontFaceId(s->vg, s->font_sans_bold); - nvgFontSize(s->vg, 48*2.5); if (is_cruise_set) { snprintf(maxspeed_str, sizeof(maxspeed_str), "%d", maxspeed_calc); - nvgFillColor(s->vg, COLOR_WHITE); - nvgText(s->vg, text_x, 242, maxspeed_str, NULL); + ui_draw_text(s->vg, text_x, 242, maxspeed_str, 48 * 2.5, COLOR_WHITE, s->font_sans_bold); } else { - nvgFontFaceId(s->vg, s->font_sans_semibold); - nvgFontSize(s->vg, 42*2.5); - nvgFillColor(s->vg, COLOR_WHITE_ALPHA(100)); - nvgText(s->vg, text_x, 242, "N/A", NULL); + ui_draw_text(s->vg, text_x, 242, "N/A", 42 * 2.5, COLOR_WHITE_ALPHA(100), s->font_sans_semibold); } - } static void ui_draw_vision_speedlimit(UIState *s) { - const UIScene *scene = &s->scene; - int ui_viz_rx = scene->ui_viz_rx; - int ui_viz_rw = scene->ui_viz_rw; - char speedlim_str[32]; float speedlimit = s->scene.speedlimit; int speedlim_calc = speedlimit * 2.2369363 + 0.5; @@ -515,7 +486,7 @@ static void ui_draw_vision_speedlimit(UIState *s) { int viz_speedlim_w = 180; int viz_speedlim_h = 202; - int viz_speedlim_x = (ui_viz_rx + (bdr_s*2)); + int viz_speedlim_x = (s->scene.ui_viz_rx + (bdr_s*2)); int viz_speedlim_y = (box_y + (bdr_s*1.5)); if (!is_speedlim_valid) { viz_speedlim_w -= 5; @@ -523,98 +494,55 @@ static void ui_draw_vision_speedlimit(UIState *s) { viz_speedlim_x += 9; viz_speedlim_y += 5; } - int viz_speedlim_bdr = is_speedlim_valid ? 30 : 15; - // Draw Background - nvgBeginPath(s->vg); - nvgRoundedRect(s->vg, viz_speedlim_x, viz_speedlim_y, viz_speedlim_w, viz_speedlim_h, viz_speedlim_bdr); + NVGcolor color = COLOR_WHITE_ALPHA(100); if (is_speedlim_valid && s->is_ego_over_limit) { - nvgFillColor(s->vg, nvgRGBA(218, 111, 37, 180)); + color = nvgRGBA(218, 111, 37, 180); } else if (is_speedlim_valid) { - nvgFillColor(s->vg, COLOR_WHITE); - } else { - nvgFillColor(s->vg, COLOR_WHITE_ALPHA(100)); + color = COLOR_WHITE; } - nvgFill(s->vg); + ui_draw_rect(s->vg, viz_speedlim_x, viz_speedlim_y, viz_speedlim_w, viz_speedlim_h, color, is_speedlim_valid ? 30 : 15); // Draw Border if (is_speedlim_valid) { - nvgStrokeWidth(s->vg, 10); - nvgStroke(s->vg); - nvgBeginPath(s->vg); - nvgRoundedRect(s->vg, viz_speedlim_x, viz_speedlim_y, viz_speedlim_w, viz_speedlim_h, 20); - if (s->is_ego_over_limit) { - nvgStrokeColor(s->vg, COLOR_OCHRE); - } else if (is_speedlim_valid) { - nvgStrokeColor(s->vg, COLOR_WHITE); - } + ui_draw_rect(s->vg, viz_speedlim_x, viz_speedlim_y, viz_speedlim_w, viz_speedlim_h, + s->is_ego_over_limit ? COLOR_OCHRE : COLOR_WHITE, 20, 10); } - + const float text_x = viz_speedlim_x + viz_speedlim_w / 2; + const float text_y = viz_speedlim_y + (is_speedlim_valid ? 50 : 45); // Draw "Speed Limit" Text - nvgTextAlign(s->vg, NVG_ALIGN_CENTER | NVG_ALIGN_BASELINE); - nvgFontFaceId(s->vg, s->font_sans_semibold); - nvgFontSize(s->vg, 50); - nvgFillColor(s->vg, COLOR_BLACK); - if (is_speedlim_valid && s->is_ego_over_limit) { - nvgFillColor(s->vg, COLOR_WHITE); - } - const int text_x = viz_speedlim_x + viz_speedlim_w / 2; - nvgText(s->vg, text_x + (is_speedlim_valid ? 6 : 0), viz_speedlim_y + (is_speedlim_valid ? 50 : 45), "SMART", NULL); - nvgText(s->vg, text_x + (is_speedlim_valid ? 6 : 0), viz_speedlim_y + (is_speedlim_valid ? 90 : 85), "SPEED", NULL); + color = is_speedlim_valid && s->is_ego_over_limit ? COLOR_WHITE : COLOR_BLACK; + ui_draw_text(s->vg, text_x + (is_speedlim_valid ? 6 : 0), text_y, "SMART", 50, color, s->font_sans_semibold); + ui_draw_text(s->vg, text_x + (is_speedlim_valid ? 6 : 0), text_y + 40, "SPEED", 50, color, s->font_sans_semibold); // Draw Speed Text - nvgFontFaceId(s->vg, s->font_sans_bold); - nvgFontSize(s->vg, 48*2.5); - if (s->is_ego_over_limit) { - nvgFillColor(s->vg, COLOR_WHITE); - } else { - nvgFillColor(s->vg, COLOR_BLACK); - } + color = s->is_ego_over_limit ? COLOR_WHITE : COLOR_BLACK; if (is_speedlim_valid) { snprintf(speedlim_str, sizeof(speedlim_str), "%d", speedlim_calc); - nvgText(s->vg, text_x, viz_speedlim_y + (is_speedlim_valid ? 170 : 165), speedlim_str, NULL); + ui_draw_text(s->vg, text_x, viz_speedlim_y + (is_speedlim_valid ? 170 : 165), speedlim_str, 48*2.5, color, s->font_sans_bold); } else { - nvgFontFaceId(s->vg, s->font_sans_semibold); - nvgFontSize(s->vg, 42*2.5); - nvgText(s->vg, text_x, viz_speedlim_y + (is_speedlim_valid ? 170 : 165), "N/A", NULL); + ui_draw_text(s->vg, text_x, viz_speedlim_y + (is_speedlim_valid ? 170 : 165), "N/A", 42*2.5, color, s->font_sans_semibold); } } static void ui_draw_vision_speed(UIState *s) { const UIScene *scene = &s->scene; - int ui_viz_rx = scene->ui_viz_rx; - int ui_viz_rw = scene->ui_viz_rw; - float speed = s->scene.v_ego; - + float speed = s->scene.v_ego * 2.2369363 + 0.5; + if (s->is_metric){ + speed = s->scene.v_ego * 3.6 + 0.5; + } const int viz_speed_w = 280; - const int viz_speed_x = ui_viz_rx+((ui_viz_rw/2)-(viz_speed_w/2)); + const int viz_speed_x = scene->ui_viz_rx+((scene->ui_viz_rw/2)-(viz_speed_w/2)); char speed_str[32]; nvgBeginPath(s->vg); nvgRect(s->vg, viz_speed_x, box_y, viz_speed_w, header_h); nvgTextAlign(s->vg, NVG_ALIGN_CENTER | NVG_ALIGN_BASELINE); - if (s->is_metric) { - snprintf(speed_str, sizeof(speed_str), "%d", (int)(speed * 3.6 + 0.5)); - } else { - snprintf(speed_str, sizeof(speed_str), "%d", (int)(speed * 2.2369363 + 0.5)); - } - const int text_x = viz_speed_x + viz_speed_w / 2; - nvgFontFaceId(s->vg, s->font_sans_bold); - nvgFontSize(s->vg, 96*2.5); - nvgFillColor(s->vg, COLOR_WHITE); - nvgText(s->vg, text_x, 240, speed_str, NULL); - - nvgFontFaceId(s->vg, s->font_sans_regular); - nvgFontSize(s->vg, 36*2.5); - nvgFillColor(s->vg, COLOR_WHITE_ALPHA(200)); - - if (s->is_metric) { - nvgText(s->vg, text_x, 320, "kph", NULL); - } else { - nvgText(s->vg, text_x, 320, "mph", NULL); - } + snprintf(speed_str, sizeof(speed_str), "%d", (int)speed); + ui_draw_text(s->vg, viz_speed_x + viz_speed_w / 2, 240, speed_str, 96*2.5, COLOR_WHITE, s->font_sans_bold); + ui_draw_text(s->vg, viz_speed_x + viz_speed_w / 2, 320, s->is_metric?"kph":"mph", 36*2.5, COLOR_WHITE_ALPHA(200), s->font_sans_regular); } static void ui_draw_vision_event(UIState *s) { @@ -666,38 +594,23 @@ static void ui_draw_driver_view(UIState *s) { // blackout if (!scene->is_rhd) { - nvgBeginPath(s->vg); NVGpaint gradient = nvgLinearGradient(s->vg, valid_frame_x + valid_frame_w, box_y, valid_frame_x + box_h / 2, box_y, nvgRGBAf(0,0,0,1), nvgRGBAf(0,0,0,0)); - nvgFillPaint(s->vg, gradient); - nvgRect(s->vg, valid_frame_x + box_h / 2, box_y, valid_frame_w - box_h / 2, box_h); - nvgFill(s->vg); + ui_draw_rect(s->vg, valid_frame_x + box_h / 2, box_y, valid_frame_w - box_h / 2, box_h, gradient); } else { - nvgBeginPath(s->vg); NVGpaint gradient = nvgLinearGradient(s->vg, valid_frame_x, box_y, valid_frame_w - box_h / 2, box_y, nvgRGBAf(0,0,0,1), nvgRGBAf(0,0,0,0)); - nvgFillPaint(s->vg, gradient); - nvgRect(s->vg, valid_frame_x, box_y, valid_frame_w - box_h / 2, box_h); - nvgFill(s->vg); + ui_draw_rect(s->vg, valid_frame_x, box_y, valid_frame_w - box_h / 2, box_h, gradient); } - nvgBeginPath(s->vg); - nvgRect(s->vg, scene->is_rhd ? valid_frame_x:valid_frame_x + box_h / 2, box_y, valid_frame_w - box_h / 2, box_h); - nvgFillColor(s->vg, COLOR_BLACK_ALPHA(144)); - nvgFill(s->vg); + ui_draw_rect(s->vg, scene->is_rhd ? valid_frame_x : valid_frame_x + box_h / 2, box_y, valid_frame_w - box_h / 2, box_h, COLOR_BLACK_ALPHA(144)); // borders - nvgBeginPath(s->vg); - nvgRect(s->vg, frame_x, box_y, valid_frame_x - frame_x, box_h); - nvgFillColor(s->vg, nvgRGBA(23,51,73,255)); - nvgFill(s->vg); - nvgBeginPath(s->vg); - nvgRect(s->vg, valid_frame_x + valid_frame_w, box_y, frame_w - valid_frame_w - (valid_frame_x - frame_x), box_h); - nvgFillColor(s->vg, nvgRGBA(23,51,73,255)); - nvgFill(s->vg); + ui_draw_rect(s->vg, frame_x, box_y, valid_frame_x - frame_x, box_h, nvgRGBA(23, 51, 73, 255)); + ui_draw_rect(s->vg, valid_frame_x + valid_frame_w, box_y, frame_w - valid_frame_w - (valid_frame_x - frame_x), box_h, nvgRGBA(23, 51, 73, 255)); // draw face box if (scene->face_prob > 0.4) { @@ -709,17 +622,11 @@ static void ui_draw_driver_view(UIState *s) { fbox_x = valid_frame_x + valid_frame_w - box_h / 2 + (scene->face_x + 0.5) * (box_h / 2) - 0.5 * 0.6 * box_h / 2; } if (abs(scene->face_x) <= 0.35 && abs(scene->face_y) <= 0.4) { - nvgBeginPath(s->vg); - nvgRoundedRect(s->vg, fbox_x, fbox_y, 0.6 * box_h / 2, 0.6 * box_h / 2, 35); - nvgStrokeColor(s->vg, nvgRGBAf(1.0, 1.0, 1.0, 0.8 - ((abs(scene->face_x) > abs(scene->face_y) ? abs(scene->face_x):abs(scene->face_y))) * 0.6 / 0.375)); - nvgStrokeWidth(s->vg, 10); - nvgStroke(s->vg); + ui_draw_rect(s->vg, fbox_x, fbox_y, 0.6 * box_h / 2, 0.6 * box_h / 2, + nvgRGBAf(1.0, 1.0, 1.0, 0.8 - ((abs(scene->face_x) > abs(scene->face_y) ? abs(scene->face_x) : abs(scene->face_y))) * 0.6 / 0.375), + 35, 10); } else { - nvgBeginPath(s->vg); - nvgRoundedRect(s->vg, fbox_x, fbox_y, 0.6 * box_h / 2, 0.6 * box_h / 2, 35); - nvgStrokeColor(s->vg, nvgRGBAf(1.0, 1.0, 1.0, 0.2)); - nvgStrokeWidth(s->vg, 10); - nvgStroke(s->vg); + ui_draw_rect(s->vg, fbox_x, fbox_y, 0.6 * box_h / 2, 0.6 * box_h / 2, nvgRGBAf(1.0, 1.0, 1.0, 0.2), 35, 10); } } else { ; @@ -737,14 +644,11 @@ static void ui_draw_vision_header(UIState *s) { int ui_viz_rx = scene->ui_viz_rx; int ui_viz_rw = scene->ui_viz_rw; - nvgBeginPath(s->vg); NVGpaint gradient = nvgLinearGradient(s->vg, ui_viz_rx, (box_y+(header_h-(header_h/2.5))), ui_viz_rx, box_y+header_h, nvgRGBAf(0,0,0,0.45), nvgRGBAf(0,0,0,0)); - nvgFillPaint(s->vg, gradient); - nvgRect(s->vg, ui_viz_rx, box_y, ui_viz_rw, header_h); - nvgFill(s->vg); + ui_draw_rect(s->vg, ui_viz_rx, box_y, ui_viz_rw, header_h, gradient); ui_draw_vision_maxspeed(s); @@ -756,12 +660,8 @@ static void ui_draw_vision_header(UIState *s) { } static void ui_draw_vision_footer(UIState *s) { - const UIScene *scene = &s->scene; - int ui_viz_rx = scene->ui_viz_rx; - int ui_viz_rw = scene->ui_viz_rw; - nvgBeginPath(s->vg); - nvgRect(s->vg, ui_viz_rx, footer_y, ui_viz_rw, footer_h); + nvgRect(s->vg, s->scene.ui_viz_rx, footer_y, s->scene.ui_viz_rw, footer_h); ui_draw_vision_face(s); @@ -773,45 +673,31 @@ static void ui_draw_vision_footer(UIState *s) { void ui_draw_vision_alert(UIState *s, int va_size, int va_color, const char* va_text1, const char* va_text2) { const UIScene *scene = &s->scene; - int ui_viz_rx = scene->ui_viz_rx; - int ui_viz_rw = scene->ui_viz_rw; const bool hasSidebar = !scene->uilayout_sidebarcollapsed; const bool mapEnabled = scene->uilayout_mapenabled; bool longAlert1 = strlen(va_text1) > 15; const uint8_t *color = alert_colors[va_color]; const int alr_s = alert_sizes[va_size]; - const int alr_x = ui_viz_rx-(mapEnabled?(hasSidebar?nav_w:(nav_ww)):0)-bdr_s; - const int alr_w = ui_viz_rw+(mapEnabled?(hasSidebar?nav_w:(nav_ww)):0)+(bdr_s*2); + const int alr_x = scene->ui_viz_rx-(mapEnabled?(hasSidebar?nav_w:(nav_ww)):0)-bdr_s; + const int alr_w = scene->ui_viz_rw+(mapEnabled?(hasSidebar?nav_w:(nav_ww)):0)+(bdr_s*2); const int alr_h = alr_s+(va_size==ALERTSIZE_NONE?0:bdr_s); const int alr_y = vwp_h-alr_h; - nvgBeginPath(s->vg); - nvgRect(s->vg, alr_x, alr_y, alr_w, alr_h); - nvgFillColor(s->vg, nvgRGBA(color[0],color[1],color[2],(color[3]*s->alert_blinking_alpha))); - nvgFill(s->vg); + ui_draw_rect(s->vg, alr_x, alr_y, alr_w, alr_h, nvgRGBA(color[0],color[1],color[2],(color[3]*s->alert_blinking_alpha))); - nvgBeginPath(s->vg); NVGpaint gradient = nvgLinearGradient(s->vg, alr_x, alr_y, alr_x, alr_y+alr_h, nvgRGBAf(0.0,0.0,0.0,0.05), nvgRGBAf(0.0,0.0,0.0,0.35)); - nvgFillPaint(s->vg, gradient); - nvgRect(s->vg, alr_x, alr_y, alr_w, alr_h); - nvgFill(s->vg); + ui_draw_rect(s->vg, alr_x, alr_y, alr_w, alr_h, gradient); nvgFillColor(s->vg, COLOR_WHITE); nvgTextAlign(s->vg, NVG_ALIGN_CENTER | NVG_ALIGN_BASELINE); if (va_size == ALERTSIZE_SMALL) { - nvgFontFaceId(s->vg, s->font_sans_semibold); - nvgFontSize(s->vg, 40*2.5); - nvgText(s->vg, alr_x+alr_w/2, alr_y+alr_h/2+15, va_text1, NULL); + ui_draw_text(s->vg, alr_x+alr_w/2, alr_y+alr_h/2+15, va_text1, 40*2.5, COLOR_WHITE, s->font_sans_semibold); } else if (va_size== ALERTSIZE_MID) { - nvgFontFaceId(s->vg, s->font_sans_bold); - nvgFontSize(s->vg, 48*2.5); - nvgText(s->vg, alr_x+alr_w/2, alr_y+alr_h/2-45, va_text1, NULL); - nvgFontFaceId(s->vg, s->font_sans_regular); - nvgFontSize(s->vg, 36*2.5); - nvgText(s->vg, alr_x+alr_w/2, alr_y+alr_h/2+75, va_text2, NULL); + ui_draw_text(s->vg, alr_x+alr_w/2, alr_y+alr_h/2-45, va_text1, 48*2.5, COLOR_WHITE, s->font_sans_bold); + ui_draw_text(s->vg, alr_x+alr_w/2, alr_y+alr_h/2+75, va_text2, 36*2.5, COLOR_WHITE, s->font_sans_regular); } else if (va_size== ALERTSIZE_FULL) { nvgFontSize(s->vg, (longAlert1?72:96)*2.5); nvgFontFaceId(s->vg, s->font_sans_bold); @@ -826,14 +712,11 @@ void ui_draw_vision_alert(UIState *s, int va_size, int va_color, static void ui_draw_vision(UIState *s) { const UIScene *scene = &s->scene; - int ui_viz_rx = scene->ui_viz_rx; - int ui_viz_rw = scene->ui_viz_rw; - int ui_viz_ro = scene->ui_viz_ro; // Draw video frames glEnable(GL_SCISSOR_TEST); - glViewport(ui_viz_rx+ui_viz_ro, s->fb_h-(box_y+box_h), viz_w, box_h); - glScissor(ui_viz_rx, s->fb_h-(box_y+box_h), ui_viz_rw, box_h); + glViewport(scene->ui_viz_rx+scene->ui_viz_ro, s->fb_h-(box_y+box_h), viz_w, box_h); + glScissor(scene->ui_viz_rx, s->fb_h-(box_y+box_h), scene->ui_viz_rw, box_h); draw_frame(s); glDisable(GL_SCISSOR_TEST); @@ -891,6 +774,26 @@ void ui_draw_image(NVGcontext *vg, float x, float y, float w, float h, int image nvgFill(vg); } +void ui_draw_rect(NVGcontext *vg, float x, float y, float w, float h, NVGcolor color, float r, int width) { + nvgBeginPath(vg); + r > 0? nvgRoundedRect(vg, x, y, w, h, r) : nvgRect(vg, x, y, w, h); + if (width) { + nvgStrokeColor(vg, color); + nvgStrokeWidth(vg, width); + nvgStroke(vg); + } else { + nvgFillColor(vg, color); + nvgFill(vg); + } +} + +void ui_draw_rect(NVGcontext *vg, float x, float y, float w, float h, NVGpaint paint, float r){ + nvgBeginPath(vg); + r > 0? nvgRoundedRect(vg, x, y, w, h, r) : nvgRect(vg, x, y, w, h); + nvgFillPaint(vg, paint); + nvgFill(vg); +} + #ifdef NANOVG_GL3_IMPLEMENTATION static const char frame_vertex_shader[] = "#version 150 core\n" diff --git a/selfdrive/ui/sidebar.cc b/selfdrive/ui/sidebar.cc index 9ca9119048..dcc243e9c5 100644 --- a/selfdrive/ui/sidebar.cc +++ b/selfdrive/ui/sidebar.cc @@ -5,11 +5,7 @@ static void ui_draw_sidebar_background(UIState *s) { int sbr_x = !s->scene.uilayout_sidebarcollapsed ? 0 : -(sbr_w) + bdr_s * 2; - - nvgBeginPath(s->vg); - nvgRect(s->vg, sbr_x, 0, sbr_w, vwp_h); - nvgFillColor(s->vg, COLOR_BLACK_ALPHA(85)); - nvgFill(s->vg); + ui_draw_rect(s->vg, sbr_x, 0, sbr_w, vwp_h, COLOR_BLACK_ALPHA(85)); } static void ui_draw_sidebar_settings_button(UIState *s) { @@ -46,11 +42,8 @@ static void ui_draw_sidebar_battery_icon(UIState *s) { int battery_img = strcmp(s->scene.batteryStatus, "Charging") == 0 ? s->img_battery_charging : s->img_battery; - nvgBeginPath(s->vg); - nvgRect(s->vg, battery_img_x + 6, battery_img_y + 5, - ((battery_img_w - 19) * (s->scene.batteryPercent * 0.01)), battery_img_h - 11); - nvgFillColor(s->vg, COLOR_WHITE); - nvgFill(s->vg); + ui_draw_rect(s->vg, battery_img_x + 6, battery_img_y + 5, + ((battery_img_w - 19) * (s->scene.batteryPercent * 0.01)), battery_img_h - 11, COLOR_WHITE); ui_draw_image(s->vg, battery_img_x, battery_img_y, battery_img_w, battery_img_h, battery_img, 1.0f); } @@ -90,11 +83,8 @@ static void ui_draw_sidebar_metric(UIState *s, const char* label_str, const char status_color = COLOR_RED; } - nvgBeginPath(s->vg); - nvgRoundedRect(s->vg, metric_x, metric_y, metric_w, metric_h, 20); - nvgStrokeColor(s->vg, severity > 0 ? COLOR_WHITE : COLOR_WHITE_ALPHA(85)); - nvgStrokeWidth(s->vg, 2); - nvgStroke(s->vg); + ui_draw_rect(s->vg, metric_x, metric_y, metric_w, metric_h, + severity > 0 ? COLOR_WHITE : COLOR_WHITE_ALPHA(85), 20, 2); nvgBeginPath(s->vg); nvgRoundedRectVarying(s->vg, metric_x + 6, metric_y + 6, 18, metric_h - 12, 25, 0, 0, 25); diff --git a/selfdrive/ui/ui.hpp b/selfdrive/ui/ui.hpp index 941b9a8d35..a27f58c606 100644 --- a/selfdrive/ui/ui.hpp +++ b/selfdrive/ui/ui.hpp @@ -313,6 +313,8 @@ void ui_draw_vision_alert(UIState *s, int va_size, int va_color, void ui_draw(UIState *s); void ui_draw_sidebar(UIState *s); void ui_draw_image(NVGcontext *vg, float x, float y, float w, float h, int image, float alpha); +void ui_draw_rect(NVGcontext *vg, float x, float y, float w, float h, NVGcolor color, float r = 0, int width = 0); +void ui_draw_rect(NVGcontext *vg, float x, float y, float w, float h, NVGpaint paint, float r = 0); void ui_nvg_init(UIState *s); #endif