diff --git a/selfdrive/ui/paint.cc b/selfdrive/ui/paint.cc index 8b55abac98..958999c0d6 100644 --- a/selfdrive/ui/paint.cc +++ b/selfdrive/ui/paint.cc @@ -631,15 +631,7 @@ static void ui_draw_vision_event(UIState *s) { if (s->scene.decel_for_model && s->scene.engaged) { // draw winding road sign const int img_turn_size = 160*1.5; - const int img_turn_x = viz_event_x-(img_turn_size/4); - const int img_turn_y = viz_event_y+bdr_s-25; - float img_turn_alpha = 1.0f; - nvgBeginPath(s->vg); - NVGpaint imgPaint = nvgImagePattern(s->vg, img_turn_x, img_turn_y, - img_turn_size, img_turn_size, 0, s->img_turn, img_turn_alpha); - nvgRect(s->vg, img_turn_x, img_turn_y, img_turn_size, img_turn_size); - nvgFillPaint(s->vg, imgPaint); - nvgFill(s->vg); + ui_draw_image(s->vg, viz_event_x - (img_turn_size / 4), viz_event_y + bdr_s - 25, img_turn_size, img_turn_size, s->img_turn, 1.0f); } else { // draw steering wheel const int bg_wheel_size = 96; @@ -665,12 +657,7 @@ static void ui_draw_vision_event(UIState *s) { nvgFill(s->vg); img_wheel_alpha = 1.0f; } - nvgBeginPath(s->vg); - NVGpaint imgPaint = nvgImagePattern(s->vg, img_wheel_x, img_wheel_y, - img_wheel_size, img_wheel_size, 0, s->img_wheel, img_wheel_alpha); - nvgRect(s->vg, img_wheel_x, img_wheel_y, img_wheel_size, img_wheel_size); - nvgFillPaint(s->vg, imgPaint); - nvgFill(s->vg); + ui_draw_image(s->vg, img_wheel_x, img_wheel_y, img_wheel_size, img_wheel_size, s->img_wheel, img_wheel_alpha); } } @@ -687,18 +674,13 @@ static void ui_draw_vision_map(UIState *s) { float map_img_alpha = map_valid ? 1.0f : 0.15f; float map_bg_alpha = map_valid ? 0.3f : 0.1f; NVGcolor map_bg = nvgRGBA(0, 0, 0, (255 * map_bg_alpha)); - NVGpaint map_img = nvgImagePattern(s->vg, map_img_x, map_img_y, - map_img_size, map_img_size, 0, s->img_map, map_img_alpha); nvgBeginPath(s->vg); nvgCircle(s->vg, map_x, (map_y + (bdr_s * 1.5)), map_size); nvgFillColor(s->vg, map_bg); nvgFill(s->vg); - nvgBeginPath(s->vg); - nvgRect(s->vg, map_img_x, map_img_y, map_img_size, map_img_size); - nvgFillPaint(s->vg, map_img); - nvgFill(s->vg); + ui_draw_image(s->vg, map_img_x, map_img_y, map_img_size, map_img_size, s->img_map, map_img_alpha); } static void ui_draw_vision_face(UIState *s) { @@ -712,18 +694,13 @@ static void ui_draw_vision_face(UIState *s) { float face_img_alpha = scene->monitoring_active ? 1.0f : 0.15f; float face_bg_alpha = scene->monitoring_active ? 0.3f : 0.1f; NVGcolor face_bg = nvgRGBA(0, 0, 0, (255 * face_bg_alpha)); - NVGpaint face_img = nvgImagePattern(s->vg, face_img_x, face_img_y, - face_img_size, face_img_size, 0, s->img_face, face_img_alpha); nvgBeginPath(s->vg); nvgCircle(s->vg, face_x, (face_y + (bdr_s * 1.5)), face_size); nvgFillColor(s->vg, face_bg); nvgFill(s->vg); - nvgBeginPath(s->vg); - nvgRect(s->vg, face_img_x, face_img_y, face_img_size, face_img_size); - nvgFillPaint(s->vg, face_img); - nvgFill(s->vg); + ui_draw_image(s->vg, face_img_x, face_img_y, face_img_size, face_img_size, s->img_face, face_img_alpha); } static void ui_draw_vision_header(UIState *s) { @@ -901,6 +878,14 @@ void ui_draw(UIState *s) { } } +void ui_draw_image(NVGcontext *vg, float x, float y, float w, float h, int image, float alpha){ + nvgBeginPath(vg); + NVGpaint imgPaint = nvgImagePattern(vg, x, y, w, h, 0, image, alpha); + nvgRect(vg, x, y, w, h); + nvgFillPaint(vg, imgPaint); + 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 74d5cbe534..3632ef640e 100644 --- a/selfdrive/ui/sidebar.cc +++ b/selfdrive/ui/sidebar.cc @@ -16,24 +16,14 @@ static void ui_draw_sidebar_settings_button(UIState *s) { bool settingsActive = s->active_app == cereal_UiLayoutState_App_settings; const int settings_btn_xr = !s->scene.uilayout_sidebarcollapsed ? settings_btn_x : -(sbr_w); - nvgBeginPath(s->vg); - NVGpaint imgPaint = nvgImagePattern(s->vg, settings_btn_xr, settings_btn_y, - settings_btn_w, settings_btn_h, 0, s->img_button_settings, settingsActive ? 1.0f : 0.65f); - nvgRect(s->vg, settings_btn_xr, settings_btn_y, settings_btn_w, settings_btn_h); - nvgFillPaint(s->vg, imgPaint); - nvgFill(s->vg); + ui_draw_image(s->vg, settings_btn_xr, settings_btn_y, settings_btn_w, settings_btn_h, s->img_button_settings, settingsActive ? 1.0f : 0.65f); } static void ui_draw_sidebar_home_button(UIState *s) { bool homeActive = s->active_app == cereal_UiLayoutState_App_home; const int home_btn_xr = !s->scene.uilayout_sidebarcollapsed ? home_btn_x : -(sbr_w); - nvgBeginPath(s->vg); - NVGpaint imgPaint = nvgImagePattern(s->vg, home_btn_xr, home_btn_y, - home_btn_w, home_btn_h, 0, s->img_button_home, homeActive ? 1.0f : 0.65f); - nvgRect(s->vg, home_btn_xr, home_btn_y, home_btn_w, home_btn_h); - nvgFillPaint(s->vg, imgPaint); - nvgFill(s->vg); + ui_draw_image(s->vg, home_btn_xr, home_btn_y, home_btn_w, home_btn_h, s->img_button_home, homeActive ? 1.0f : 0.65f); } static void ui_draw_sidebar_network_strength(UIState *s) { @@ -44,12 +34,7 @@ static void ui_draw_sidebar_network_strength(UIState *s) { const int network_img = s->scene.networkType == cereal_ThermalData_NetworkType_none ? s->img_network[0] : s->img_network[s->scene.networkStrength + 1]; - nvgBeginPath(s->vg); - NVGpaint imgPaint = nvgImagePattern(s->vg, network_img_x, network_img_y, - network_img_w, network_img_h, 0, network_img, 1.0f); - nvgRect(s->vg, network_img_x, network_img_y, network_img_w, network_img_h); - nvgFillPaint(s->vg, imgPaint); - nvgFill(s->vg); + ui_draw_image(s->vg, network_img_x, network_img_y, network_img_w, network_img_h, network_img, 1.0f); } static void ui_draw_sidebar_battery_icon(UIState *s) { @@ -67,12 +52,7 @@ static void ui_draw_sidebar_battery_icon(UIState *s) { nvgFillColor(s->vg, COLOR_WHITE); nvgFill(s->vg); - nvgBeginPath(s->vg); - NVGpaint imgPaint = nvgImagePattern(s->vg, battery_img_x, battery_img_y, - battery_img_w, battery_img_h, 0, battery_img, 1.0f); - nvgRect(s->vg, battery_img_x, battery_img_y, battery_img_w, battery_img_h); - nvgFillPaint(s->vg, imgPaint); - nvgFill(s->vg); + ui_draw_image(s->vg, battery_img_x, battery_img_y, battery_img_w, battery_img_h, battery_img, 1.0f); } static void ui_draw_sidebar_network_type(UIState *s) { diff --git a/selfdrive/ui/ui.hpp b/selfdrive/ui/ui.hpp index b06b51c62b..7da0442352 100644 --- a/selfdrive/ui/ui.hpp +++ b/selfdrive/ui/ui.hpp @@ -304,6 +304,7 @@ void ui_draw_vision_alert(UIState *s, int va_size, int va_color, const char* va_text1, const char* va_text2); 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_nvg_init(UIState *s); #endif