diff --git a/components/input_drivers/include/pointer_input.hpp b/components/input_drivers/include/pointer_input.hpp index e273cddee..ac5f4f51e 100644 --- a/components/input_drivers/include/pointer_input.hpp +++ b/components/input_drivers/include/pointer_input.hpp @@ -153,8 +153,39 @@ class PointerInput : public BaseComponent { return; } read_(x, y, left_pressed, right_pressed); - data->point.x = std::clamp(x, 0, screen_size_x_ - cursor_radius_ * 2); - data->point.y = std::clamp(y, 0, screen_size_y_ - cursor_radius_ * 2); + auto cursor_diameter = cursor_radius_ * 2; + auto disp = lv_display_get_default(); + // NOTE: we cache these static values since the screen size is not going to + // change during runtime, and we want the actual screen size (not the + // rotated size) The input driver in lvgl will perform the input + // rotation for us, so we should not rotate the coordinates here. + static int screen_size_x = lv_disp_get_hor_res(disp); + static int screen_size_y = lv_disp_get_ver_res(disp); + auto rotation = lv_disp_get_rotation(disp); + // adjust which side of the clamp we're adjusting by the cursor size based + // on rotation + switch (rotation) { + default: + case LV_DISPLAY_ROTATION_0: + x = std::clamp(x, 0, screen_size_x - cursor_diameter); + y = std::clamp(y, 0, screen_size_y - cursor_diameter); + break; + case LV_DISPLAY_ROTATION_90: + x = std::clamp(x, 0, screen_size_x - cursor_diameter); + y = std::clamp(y, cursor_diameter, screen_size_y); + break; + case LV_DISPLAY_ROTATION_180: + x = std::clamp(x, cursor_diameter, screen_size_x); + y = std::clamp(y, cursor_diameter, screen_size_y); + break; + case LV_DISPLAY_ROTATION_270: + x = std::clamp(x, cursor_diameter, screen_size_x); + y = std::clamp(y, 0, screen_size_y - cursor_diameter); + break; + } + + data->point.x = x; + data->point.y = y; data->state = left_pressed ? LV_INDEV_STATE_PR : LV_INDEV_STATE_REL; } @@ -178,16 +209,10 @@ class PointerInput : public BaseComponent { lv_obj_set_style_bg_color(cursor_obj_, lv_palette_lighten(LV_PALETTE_BLUE, 2), 0); lv_obj_clear_flag(cursor_obj_, LV_OBJ_FLAG_CLICKABLE); lv_indev_set_cursor(indev_pointer_, cursor_obj_); - - auto disp = lv_display_get_default(); - screen_size_x_ = (uint16_t)lv_display_get_horizontal_resolution(disp); - screen_size_y_ = (uint16_t)lv_display_get_vertical_resolution(disp); } read_fn read_; int cursor_radius_{8}; - uint16_t screen_size_x_; - uint16_t screen_size_y_; lv_indev_t *indev_pointer_{nullptr}; lv_obj_t *cursor_obj_{nullptr}; };