GPencil: Extract function to calculate cursor size

This function will be used in other areas of Blender.
This commit is contained in:
Antonio Vazquez 2022-12-27 10:01:37 +01:00
parent dce7917717
commit 3adbe82e07
2 changed files with 56 additions and 27 deletions

View File

@ -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);

View File

@ -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