From 30c1164287d9ebb7ac3fb9e967d4e1d01bbb23f5 Mon Sep 17 00:00:00 2001 From: Dean Lee Date: Tue, 5 Jan 2021 20:01:32 +0800 Subject: [PATCH] refactor ui_draw_image (#19656) --- selfdrive/ui/paint.cc | 16 +++++++++------- selfdrive/ui/paint.hpp | 2 +- selfdrive/ui/sidebar.cc | 25 ++++++++----------------- 3 files changed, 18 insertions(+), 25 deletions(-) diff --git a/selfdrive/ui/paint.cc b/selfdrive/ui/paint.cc index a6c22ef213..a862a89850 100644 --- a/selfdrive/ui/paint.cc +++ b/selfdrive/ui/paint.cc @@ -96,16 +96,17 @@ static void draw_chevron(UIState *s, float x_in, float y_in, float sz, nvgFill(s->vg); } -static void ui_draw_circle_image(NVGcontext *vg, float x, float y, int size, int image, NVGcolor color, float img_alpha, int img_y = 0) { +static void ui_draw_circle_image(NVGcontext *vg, int x, int y, int size, int image, NVGcolor color, float img_alpha, int img_y = 0) { const int img_size = size * 1.5; nvgBeginPath(vg); nvgCircle(vg, x, y + (bdr_s * 1.5), size); nvgFillColor(vg, color); nvgFill(vg); - ui_draw_image(vg, x - (img_size / 2), img_y ? img_y : y - (size / 4), img_size, img_size, image, img_alpha); + const Rect rect = {x - (img_size / 2), img_y ? img_y : y - (size / 4), img_size, img_size}; + ui_draw_image(vg, rect, image, img_alpha); } -static void ui_draw_circle_image(NVGcontext *vg, float x, float y, int size, int image, bool active) { +static void ui_draw_circle_image(NVGcontext *vg, int x, int y, int size, int image, bool active) { float bg_alpha = active ? 0.3f : 0.1f; float img_alpha = active ? 1.0f : 0.15f; ui_draw_circle_image(vg, x, y, size, image, nvgRGBA(0, 0, 0, (255 * bg_alpha)), img_alpha); @@ -322,7 +323,8 @@ static void ui_draw_vision_event(UIState *s) { if (s->scene.controls_state.getDecelForModel() && s->scene.controls_state.getEnabled()) { // draw winding road sign const int img_turn_size = 160*1.5; - 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); + const Rect rect = {viz_event_x - (img_turn_size / 4), viz_event_y + bdr_s - 25, img_turn_size, img_turn_size}; + ui_draw_image(s->vg, rect, s->img_turn, 1.0f); } else if (s->scene.controls_state.getEngageable()) { // draw steering wheel const int bg_wheel_size = 96; @@ -526,10 +528,10 @@ void ui_draw(UIState *s) { glDisable(GL_BLEND); } -void ui_draw_image(NVGcontext *vg, float x, float y, float w, float h, int image, float alpha){ +void ui_draw_image(NVGcontext *vg, const Rect &r, int image, float alpha){ nvgBeginPath(vg); - NVGpaint imgPaint = nvgImagePattern(vg, x, y, w, h, 0, image, alpha); - nvgRect(vg, x, y, w, h); + NVGpaint imgPaint = nvgImagePattern(vg, r.x, r.y, r.w, r.h, 0, image, alpha); + nvgRect(vg, r.x, r.y, r.w, r.h); nvgFillPaint(vg, imgPaint); nvgFill(vg); } diff --git a/selfdrive/ui/paint.hpp b/selfdrive/ui/paint.hpp index 203beef172..d84bfb0e33 100644 --- a/selfdrive/ui/paint.hpp +++ b/selfdrive/ui/paint.hpp @@ -2,7 +2,7 @@ #include "ui.hpp" void ui_draw(UIState *s); -void ui_draw_image(NVGcontext *vg, float x, float y, float w, float h, int image, float alpha); +void ui_draw_image(NVGcontext *vg, const Rect &r, 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); diff --git a/selfdrive/ui/sidebar.cc b/selfdrive/ui/sidebar.cc index 7617525194..b5f840a34f 100644 --- a/selfdrive/ui/sidebar.cc +++ b/selfdrive/ui/sidebar.cc @@ -17,12 +17,12 @@ static void ui_draw_sidebar_background(UIState *s) { static void ui_draw_sidebar_settings_button(UIState *s) { const float alpha = s->active_app == cereal::UiLayoutState::App::SETTINGS ? 1.0f : 0.65f; - ui_draw_image(s->vg, settings_btn.x, settings_btn.y, settings_btn.w, settings_btn.h, s->img_button_settings, alpha); + ui_draw_image(s->vg, settings_btn, s->img_button_settings, alpha); } static void ui_draw_sidebar_home_button(UIState *s) { const float alpha = s->active_app == cereal::UiLayoutState::App::HOME ? 1.0f : 0.65f;; - ui_draw_image(s->vg, home_btn.x, home_btn.y, home_btn.w, home_btn.h, s->img_button_home, alpha); + ui_draw_image(s->vg, home_btn, s->img_button_home, alpha); } static void ui_draw_sidebar_network_strength(UIState *s) { @@ -32,26 +32,17 @@ static void ui_draw_sidebar_network_strength(UIState *s) { {cereal::ThermalData::NetworkStrength::MODERATE, 3}, {cereal::ThermalData::NetworkStrength::GOOD, 4}, {cereal::ThermalData::NetworkStrength::GREAT, 5}}; - const int network_img_h = 27; - const int network_img_w = 176; - const int network_img_x = 58; - const int network_img_y = 196; + const Rect rect = {58, 196, 176, 27}; const int img_idx = s->scene.thermal.getNetworkType() == cereal::ThermalData::NetworkType::NONE ? 0 : network_strength_map[s->scene.thermal.getNetworkStrength()]; - ui_draw_image(s->vg, network_img_x, network_img_y, network_img_w, network_img_h, s->img_network[img_idx], 1.0f); + ui_draw_image(s->vg, rect, s->img_network[img_idx], 1.0f); } static void ui_draw_sidebar_battery_icon(UIState *s) { - const int battery_img_h = 36; - const int battery_img_w = 76; - const int battery_img_x = 160; - const int battery_img_y = 255; - int battery_img = s->scene.thermal.getBatteryStatus() == "Charging" ? s->img_battery_charging : s->img_battery; - - ui_draw_rect(s->vg, battery_img_x + 6, battery_img_y + 5, - ((battery_img_w - 19) * (s->scene.thermal.getBatteryPercent() * 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); + const Rect rect = {160, 255, 76, 36}; + ui_draw_rect(s->vg, rect.x + 6, rect.y + 5, + ((rect.w - 19) * (s->scene.thermal.getBatteryPercent() * 0.01)), rect.h - 11, COLOR_WHITE); + ui_draw_image(s->vg, rect, battery_img, 1.0f); } static void ui_draw_sidebar_network_type(UIState *s) {