From 3adbe82e079c0c07425fddf4c0fffc50feb2645f Mon Sep 17 00:00:00 2001 From: Antonio Vazquez Date: Tue, 27 Dec 2022 10:01:37 +0100 Subject: [PATCH] GPencil: Extract function to calculate cursor size This function will be used in other areas of Blender. --- .../blender/editors/gpencil/gpencil_utils.c | 78 ++++++++++++------- source/blender/editors/include/ED_gpencil.h | 5 ++ 2 files changed, 56 insertions(+), 27 deletions(-) diff --git a/source/blender/editors/gpencil/gpencil_utils.c b/source/blender/editors/gpencil/gpencil_utils.c index 9f6b17b66e2..f8e2a8fd080 100644 --- a/source/blender/editors/gpencil/gpencil_utils.c +++ b/source/blender/editors/gpencil/gpencil_utils.c @@ -1730,6 +1730,56 @@ static bool gpencil_brush_cursor_poll(bContext *C) return false; } +float ED_gpencil_cursor_radius(bContext *C, int x, int y) +{ + Scene *scene = CTX_data_scene(C); + Object *ob = CTX_data_active_object(C); + ARegion *region = CTX_wm_region(C); + Brush *brush = brush = scene->toolsettings->gp_paint->paint.brush; + bGPdata *gpd = ED_gpencil_data_get_active(C); + + /* Show brush size. */ + tGPspoint point2D; + float p1[3]; + float p2[3]; + float distance; + float radius = 2.0f; + + if (ELEM(NULL, gpd, brush)) { + return radius; + } + + /* Strokes in screen space or world space? */ + if ((gpd->flag & GP_DATA_STROKE_KEEPTHICKNESS) != 0) { + /* In screen space the cursor radius matches the brush size. */ + radius = (float)brush->size * 0.5f; + } + else { + /* To calculate the brush size in world space, we have to establish the zoom level. + * For this we take two 2D screen coordinates with a fixed offset, + * convert them to 3D coordinates and measure the offset distance in 3D. + * A small distance means a high zoom level. */ + point2D.m_xy[0] = (float)x; + point2D.m_xy[1] = (float)y; + gpencil_stroke_convertcoords_tpoint(scene, region, ob, &point2D, NULL, p1); + point2D.m_xy[0] = (float)(x + 64); + gpencil_stroke_convertcoords_tpoint(scene, region, ob, &point2D, NULL, p2); + /* Clip extreme zoom level (and avoid division by zero). */ + distance = MAX2(len_v3v3(p1, p2), 0.001f); + + /* Handle layer thickness change. */ + float brush_size = (float)brush->size; + bGPDlayer *gpl = BKE_gpencil_layer_active_get(gpd); + if (gpl != NULL) { + brush_size = MAX2(1.0f, brush_size + gpl->line_change); + } + + /* Convert the 3D offset distance to a brush radius. */ + radius = (1 / distance) * 2.0f * gpd->pixfactor * (brush_size / 64); + } + return radius; +} + /** * Helper callback for drawing the cursor itself. */ @@ -1813,39 +1863,13 @@ static void gpencil_brush_cursor_draw(bContext *C, int x, int y, void *customdat copy_v3_v3(color, is_vertex_stroke ? brush->rgb : gp_style->stroke_rgba); } else { - /* Show brush size. */ - tGPspoint point2D; - float p1[3]; - float p2[3]; - float distance; - /* Strokes in screen space or world space? */ if ((gpd->flag & GP_DATA_STROKE_KEEPTHICKNESS) != 0) { /* In screen space the cursor radius matches the brush size. */ radius = (float)brush->size * 0.5f; } else { - /* To calculate the brush size in world space, we have to establish the zoom level. - * For this we take two 2D screen coordinates with a fixed offset, - * convert them to 3D coordinates and measure the offset distance in 3D. - * A small distance means a high zoom level. */ - point2D.m_xy[0] = (float)x; - point2D.m_xy[1] = (float)y; - gpencil_stroke_convertcoords_tpoint(scene, region, ob, &point2D, NULL, p1); - point2D.m_xy[0] = (float)(x + 64); - gpencil_stroke_convertcoords_tpoint(scene, region, ob, &point2D, NULL, p2); - /* Clip extreme zoom level (and avoid division by zero). */ - distance = MAX2(len_v3v3(p1, p2), 0.001f); - - /* Handle layer thickness change. */ - float brush_size = (float)brush->size; - bGPDlayer *gpl = BKE_gpencil_layer_active_get(gpd); - if (gpl != NULL) { - brush_size = MAX2(1.0f, brush_size + gpl->line_change); - } - - /* Convert the 3D offset distance to a brush radius. */ - radius = (1 / distance) * 2.0f * gpd->pixfactor * (brush_size / 64); + radius = ED_gpencil_cursor_radius(C, x, y); } copy_v3_v3(color, is_vertex_stroke ? brush->rgb : gp_style->stroke_rgba); diff --git a/source/blender/editors/include/ED_gpencil.h b/source/blender/editors/include/ED_gpencil.h index 45e61592424..129229a8861 100644 --- a/source/blender/editors/include/ED_gpencil.h +++ b/source/blender/editors/include/ED_gpencil.h @@ -641,6 +641,11 @@ struct bGPDstroke *ED_gpencil_stroke_join_and_trim(struct bGPdata *gpd, */ void ED_gpencil_stroke_close_by_distance(struct bGPDstroke *gps, float threshold); +/** + * Calculate the brush cursor size in world space. + */ +float ED_gpencil_cursor_radius(struct bContext *C, int x, int y); + #ifdef __cplusplus } #endif