diff options
Diffstat (limited to 'source/blender/editors/gpencil')
-rw-r--r-- | source/blender/editors/gpencil/CMakeLists.txt | 10 | ||||
-rw-r--r-- | source/blender/editors/gpencil/SConscript | 5 | ||||
-rw-r--r-- | source/blender/editors/gpencil/drawgpencil.c | 952 | ||||
-rw-r--r-- | source/blender/editors/gpencil/editaction_gpencil.c | 61 | ||||
-rw-r--r-- | source/blender/editors/gpencil/gpencil_buttons.c | 378 | ||||
-rw-r--r-- | source/blender/editors/gpencil/gpencil_convert.c | 1484 | ||||
-rw-r--r-- | source/blender/editors/gpencil/gpencil_data.c | 447 | ||||
-rw-r--r-- | source/blender/editors/gpencil/gpencil_edit.c | 2144 | ||||
-rw-r--r-- | source/blender/editors/gpencil/gpencil_intern.h | 103 | ||||
-rw-r--r-- | source/blender/editors/gpencil/gpencil_ops.c | 222 | ||||
-rw-r--r-- | source/blender/editors/gpencil/gpencil_paint.c | 451 | ||||
-rw-r--r-- | source/blender/editors/gpencil/gpencil_select.c | 930 | ||||
-rw-r--r-- | source/blender/editors/gpencil/gpencil_undo.c | 61 | ||||
-rw-r--r-- | source/blender/editors/gpencil/gpencil_utils.c | 375 |
14 files changed, 5238 insertions, 2385 deletions
diff --git a/source/blender/editors/gpencil/CMakeLists.txt b/source/blender/editors/gpencil/CMakeLists.txt index 904ad4892ed..83a13abb33f 100644 --- a/source/blender/editors/gpencil/CMakeLists.txt +++ b/source/blender/editors/gpencil/CMakeLists.txt @@ -23,11 +23,14 @@ set(INC ../../blenfont ../../blenkernel ../../blenlib + ../../blentranslation ../../imbuf + ../../gpu ../../makesdna ../../makesrna ../../windowmanager ../../../../intern/guardedalloc + ../../../../intern/glew-mx ) set(INC_SYS @@ -37,11 +40,14 @@ set(INC_SYS set(SRC drawgpencil.c editaction_gpencil.c - gpencil_buttons.c + gpencil_convert.c + gpencil_data.c gpencil_edit.c gpencil_ops.c gpencil_paint.c + gpencil_select.c gpencil_undo.c + gpencil_utils.c gpencil_intern.h ) @@ -50,4 +56,6 @@ if(WITH_INTERNATIONAL) add_definitions(-DWITH_INTERNATIONAL) endif() +add_definitions(${GL_DEFINITIONS}) + blender_add_lib(bf_editor_gpencil "${SRC}" "${INC}" "${INC_SYS}") diff --git a/source/blender/editors/gpencil/SConscript b/source/blender/editors/gpencil/SConscript index 8b891fcb8cf..3830a164b7b 100644 --- a/source/blender/editors/gpencil/SConscript +++ b/source/blender/editors/gpencil/SConscript @@ -31,12 +31,14 @@ sources = env.Glob('*.c') incs = [ '#/intern/guardedalloc', - '#/extern/glew/include', + env['BF_GLEW_INC'], + '#/intern/glew-mx', '#/intern/elbeem/extern', '../include', '../../blenfont', '../../blenkernel', '../../blenlib', + '../../blentranslation', '../../bmesh', '../../gpu', '../../imbuf', @@ -47,6 +49,7 @@ incs = [ ] defs = [] +defs += env['BF_GL_DEFINITIONS'] if env['WITH_BF_INTERNATIONAL']: defs.append('WITH_INTERNATIONAL') diff --git a/source/blender/editors/gpencil/drawgpencil.c b/source/blender/editors/gpencil/drawgpencil.c index 5a838d7bc39..6b71c0ac053 100644 --- a/source/blender/editors/gpencil/drawgpencil.c +++ b/source/blender/editors/gpencil/drawgpencil.c @@ -27,6 +27,7 @@ * \ingroup edgpencil */ + #include <stdio.h> #include <string.h> #include <stdlib.h> @@ -39,6 +40,9 @@ #include "BLI_math.h" #include "BLI_utildefines.h" +#include "BLF_api.h" +#include "BLT_translation.h" + #include "DNA_gpencil_types.h" #include "DNA_scene_types.h" #include "DNA_screen_types.h" @@ -56,9 +60,11 @@ #include "BIF_glutil.h" #include "ED_gpencil.h" +#include "ED_screen.h" #include "ED_view3d.h" -#include "gpencil_intern.h" +#include "UI_interface_icons.h" +#include "UI_resources.h" /* ************************************************** */ /* GREASE PENCIL DRAWING */ @@ -73,6 +79,9 @@ typedef enum eDrawStrokeFlags { GP_DRAWDATA_ONLYI2D = (1 << 3), /* only draw 'image' strokes */ GP_DRAWDATA_IEDITHACK = (1 << 4), /* special hack for drawing strokes in Image Editor (weird coordinates) */ GP_DRAWDATA_NO_XRAY = (1 << 5), /* don't draw xray in 3D view (which is default) */ + GP_DRAWDATA_NO_ONIONS = (1 << 6), /* no onionskins should be drawn (for animation playback) */ + GP_DRAWDATA_VOLUMETRIC = (1 << 7), /* draw strokes as "volumetric" circular billboards */ + GP_DRAWDATA_FILL = (1 << 8), /* fill insides/bounded-regions of strokes */ } eDrawStrokeFlags; @@ -111,22 +120,22 @@ static void gp_draw_stroke_buffer(tGPspoint *points, int totpoints, short thickn /* draw stroke curve */ if (G.debug & G_DEBUG) setlinestyle(2); - - glLineWidth(oldpressure * thickness); + + glLineWidth(max_ff(oldpressure * thickness, 1.0)); glBegin(GL_LINE_STRIP); - + for (i = 0, pt = points; i < totpoints && pt; i++, pt++) { /* if there was a significant pressure change, stop the curve, change the thickness of the stroke, * and continue drawing again (since line-width cannot change in middle of GL_LINE_STRIP) */ if (fabsf(pt->pressure - oldpressure) > 0.2f) { glEnd(); - glLineWidth(pt->pressure * thickness); + glLineWidth(max_ff(pt->pressure * thickness, 1.0f)); glBegin(GL_LINE_STRIP); /* need to roll-back one point to ensure that there are no gaps in the stroke */ if (i != 0) glVertex2iv(&(pt - 1)->x); - + /* now the point we want... */ glVertex2iv(&pt->x); @@ -136,20 +145,230 @@ static void gp_draw_stroke_buffer(tGPspoint *points, int totpoints, short thickn glVertex2iv(&pt->x); } glEnd(); - + /* reset for predictable OpenGL context */ glLineWidth(1.0f); - + if (G.debug & G_DEBUG) setlinestyle(0); } } +/* --------- 2D Stroke Drawing Helpers --------- */ + +/* helper function to calculate x-y drawing coordinates for 2D points */ +static void gp_calc_2d_stroke_xy(bGPDspoint *pt, short sflag, int offsx, int offsy, int winx, int winy, float r_co[2]) +{ + if (sflag & GP_STROKE_2DSPACE) { + r_co[0] = pt->x; + r_co[1] = pt->y; + } + else if (sflag & GP_STROKE_2DIMAGE) { + const float x = (float)((pt->x * winx) + offsx); + const float y = (float)((pt->y * winy) + offsy); + + r_co[0] = x; + r_co[1] = y; + } + else { + const float x = (float)(pt->x / 100 * winx) + offsx; + const float y = (float)(pt->y / 100 * winy) + offsy; + + r_co[0] = x; + r_co[1] = y; + } +} + +/* ----------- Volumetric Strokes --------------- */ + +/* draw a 2D buffer stroke in "volumetric" style + * NOTE: the stroke buffer doesn't have any coordinate offsets/transforms + */ +static void gp_draw_stroke_volumetric_buffer(tGPspoint *points, int totpoints, short thickness, + short dflag, short UNUSED(sflag)) +{ + GLUquadricObj *qobj = gluNewQuadric(); + float modelview[4][4]; + + tGPspoint *pt; + int i; + + /* error checking */ + if ((points == NULL) || (totpoints <= 0)) + return; + + /* check if buffer can be drawn */ + if (dflag & (GP_DRAWDATA_ONLY3D | GP_DRAWDATA_ONLYV2D)) + return; + + /* get basic matrix - should be camera space (i.e "identity") */ + glGetFloatv(GL_MODELVIEW_MATRIX, (float *)modelview); + + /* draw points */ + glPushMatrix(); + + for (i = 0, pt = points; i < totpoints; i++, pt++) { + /* set the transformed position */ + // TODO: scale should change based on zoom level, which requires proper translation mult too! + modelview[3][0] = pt->x; + modelview[3][1] = pt->y; + + glLoadMatrixf((float *)modelview); + + /* draw the disk using the current state... */ + gluDisk(qobj, 0.0, pt->pressure * thickness, 32, 1); + + + modelview[3][0] = modelview[3][1] = 0.0f; + } + + glPopMatrix(); + gluDeleteQuadric(qobj); +} + +/* draw a 2D strokes in "volumetric" style */ +static void gp_draw_stroke_volumetric_2d(bGPDspoint *points, int totpoints, short thickness, + short dflag, short sflag, + int offsx, int offsy, int winx, int winy) +{ + GLUquadricObj *qobj = gluNewQuadric(); + float modelview[4][4]; + float baseloc[3]; + float scalefac = 1.0f; + + bGPDspoint *pt; + int i; + + + /* HACK: We need a scale factor for the drawing in the image editor, + * which seems to use 1 unit as it's maximum size, whereas everything + * else assumes 1 unit = 1 pixel. Otherwise, we only get a massive blob. + */ + if ((dflag & GP_DRAWDATA_IEDITHACK) && (dflag & GP_DRAWDATA_ONLYV2D)) { + scalefac = 0.001f; + } + + /* get basic matrix */ + glGetFloatv(GL_MODELVIEW_MATRIX, (float *)modelview); + copy_v3_v3(baseloc, modelview[3]); + + /* draw points */ + glPushMatrix(); + + for (i = 0, pt = points; i < totpoints; i++, pt++) { + /* set the transformed position */ + float co[2]; + + gp_calc_2d_stroke_xy(pt, sflag, offsx, offsy, winx, winy, co); + translate_m4(modelview, co[0], co[1], 0.0f); + + glLoadMatrixf((float *)modelview); + + /* draw the disk using the current state... */ + gluDisk(qobj, 0.0, pt->pressure * thickness * scalefac, 32, 1); + + /* restore matrix */ + copy_v3_v3(modelview[3], baseloc); + } + + glPopMatrix(); + gluDeleteQuadric(qobj); +} + +/* draw a 3D stroke in "volumetric" style */ +static void gp_draw_stroke_volumetric_3d(bGPDspoint *points, int totpoints, short thickness, + short UNUSED(dflag), short UNUSED(sflag)) +{ + GLUquadricObj *qobj = gluNewQuadric(); + + float base_modelview[4][4], modelview[4][4]; + float base_loc[3]; + + bGPDspoint *pt; + int i; + + + /* Get the basic modelview matrix we use for performing calculations */ + glGetFloatv(GL_MODELVIEW_MATRIX, (float *)base_modelview); + copy_v3_v3(base_loc, base_modelview[3]); + + /* Create the basic view-aligned billboard matrix we're going to actually draw qobj with: + * - We need to knock out the rotation so that we are + * simply left with a camera-facing billboard + * - The scale factors here are chosen so that the thickness + * is relatively reasonable. Otherwise, it gets far too + * large! + */ + scale_m4_fl(modelview, 0.1f); + + /* draw each point as a disk... */ + glPushMatrix(); + + for (i = 0, pt = points; i < totpoints && pt; i++, pt++) { + /* apply translation to base_modelview, so that the translated point is put in the right place */ + translate_m4(base_modelview, pt->x, pt->y, pt->z); + + /* copy the translation component to the billboard matrix we're going to use, + * then reset the base matrix to the original values so that we can do the same + * for the next point without accumulation/pollution effects + */ + copy_v3_v3(modelview[3], base_modelview[3]); /* copy offset value */ + copy_v3_v3(base_modelview[3], base_loc); /* restore */ + + /* apply our billboard matrix for drawing... */ + glLoadMatrixf((float *)modelview); + + /* draw the disk using the current state... */ + gluDisk(qobj, 0.0, pt->pressure * thickness, 32, 1); + } + + glPopMatrix(); + gluDeleteQuadric(qobj); +} + + +/* --------------- Stroke Fills ----------------- */ + +/* draw fills for shapes */ +static void gp_draw_stroke_fill(bGPDspoint *points, int totpoints, short UNUSED(thickness), + short UNUSED(dflag), short sflag, + int offsx, int offsy, int winx, int winy) +{ + bGPDspoint *pt; + int i; + + BLI_assert(totpoints >= 3); + + /* As an initial implementation, we use the OpenGL filled polygon drawing + * here since it's the easiest option to implement for this case. It does + * come with limitations (notably for concave shapes), though it shouldn't + * be much of an issue in most cases. + */ + glBegin(GL_POLYGON); + + for (i = 0, pt = points; i < totpoints; i++, pt++) { + if (sflag & GP_STROKE_3DSPACE) { + glVertex3fv(&pt->x); + } + else { + float co[2]; + + gp_calc_2d_stroke_xy(pt, sflag, offsx, offsy, winx, winy, co); + glVertex2fv(co); + } + } + + glEnd(); +} + /* ----- Existing Strokes Drawing (3D and Point) ------ */ /* draw a given stroke - just a single dot (only one point) */ static void gp_draw_stroke_point(bGPDspoint *points, short thickness, short dflag, short sflag, int offsx, int offsy, int winx, int winy) { + /* set point thickness (since there's only one of these) */ + glPointSize((float)(thickness + 2) * points->pressure); + /* draw point */ if (sflag & GP_STROKE_3DSPACE) { glBegin(GL_POINTS); @@ -160,18 +379,7 @@ static void gp_draw_stroke_point(bGPDspoint *points, short thickness, short dfla float co[2]; /* get coordinates of point */ - if (sflag & GP_STROKE_2DSPACE) { - co[0] = points->x; - co[1] = points->y; - } - else if (sflag & GP_STROKE_2DIMAGE) { - co[0] = (points->x * winx) + offsx; - co[1] = (points->y * winy) + offsy; - } - else { - co[0] = (points->x / 100 * winx) + offsx; - co[1] = (points->y / 100 * winy) + offsy; - } + gp_calc_2d_stroke_xy(points, sflag, offsx, offsy, winx, winy, co); /* if thickness is less than GP_DRAWTHICKNESS_SPECIAL, simple dot looks ok * - also mandatory in if Image Editor 'image-based' dot @@ -185,13 +393,13 @@ static void gp_draw_stroke_point(bGPDspoint *points, short thickness, short dfla } else { /* draw filled circle as is done in circf (but without the matrix push/pops which screwed things up) */ - GLUquadricObj *qobj = gluNewQuadric(); + GLUquadricObj *qobj = gluNewQuadric(); - gluQuadricDrawStyle(qobj, GLU_FILL); + gluQuadricDrawStyle(qobj, GLU_FILL); /* need to translate drawing position, but must reset after too! */ - glTranslatef(co[0], co[1], 0.0); - gluDisk(qobj, 0.0, thickness, 32, 1); + glTranslate2fv(co); + gluDisk(qobj, 0.0, thickness, 32, 1); glTranslatef(-co[0], -co[1], 0.0); gluDeleteQuadric(qobj); @@ -200,14 +408,14 @@ static void gp_draw_stroke_point(bGPDspoint *points, short thickness, short dfla } /* draw a given stroke in 3d (i.e. in 3d-space), using simple ogl lines */ -static void gp_draw_stroke_3d(bGPDspoint *points, int totpoints, short thickness, short debug) +static void gp_draw_stroke_3d(bGPDspoint *points, int totpoints, short thickness, bool debug, short UNUSED(sflag)) { bGPDspoint *pt; float curpressure = points[0].pressure; int i; /* draw stroke curve */ - glLineWidth(curpressure * thickness); + glLineWidth(max_ff(curpressure * thickness, 1.0f)); glBegin(GL_LINE_STRIP); for (i = 0, pt = points; i < totpoints && pt; i++, pt++) { /* if there was a significant pressure change, stop the curve, change the thickness of the stroke, @@ -217,7 +425,7 @@ static void gp_draw_stroke_3d(bGPDspoint *points, int totpoints, short thickness if (fabsf(pt->pressure - curpressure) > 0.2f / (float)thickness) { glEnd(); curpressure = pt->pressure; - glLineWidth(curpressure * thickness); + glLineWidth(max_ff(curpressure * thickness, 1.0f)); glBegin(GL_LINE_STRIP); /* need to roll-back one point to ensure that there are no gaps in the stroke */ @@ -231,9 +439,12 @@ static void gp_draw_stroke_3d(bGPDspoint *points, int totpoints, short thickness } } glEnd(); - + /* draw debug points of curve on top? */ + /* XXX: for now, we represent "selected" strokes in the same way as debug, which isn't used anymore */ if (debug) { + glPointSize((float)(thickness + 2)); + glBegin(GL_POINTS); for (i = 0, pt = points; i < totpoints && pt; i++, pt++) glVertex3fv(&pt->x); @@ -244,46 +455,22 @@ static void gp_draw_stroke_3d(bGPDspoint *points, int totpoints, short thickness /* ----- Fancy 2D-Stroke Drawing ------ */ /* draw a given stroke in 2d */ -static void gp_draw_stroke(bGPDspoint *points, int totpoints, short thickness_s, short dflag, short sflag, - short debug, int offsx, int offsy, int winx, int winy) +static void gp_draw_stroke_2d(bGPDspoint *points, int totpoints, short thickness_s, short dflag, short sflag, + bool debug, int offsx, int offsy, int winx, int winy) { /* otherwise thickness is twice that of the 3D view */ float thickness = (float)thickness_s * 0.5f; - - /* if thickness is less than GP_DRAWTHICKNESS_SPECIAL, 'smooth' opengl lines look better - * - 'smooth' opengl lines are also required if Image Editor 'image-based' stroke - */ - if ((thickness < GP_DRAWTHICKNESS_SPECIAL) || - ((dflag & GP_DRAWDATA_IEDITHACK) && (dflag & GP_DRAWDATA_ONLYV2D))) - { - bGPDspoint *pt; - int i; - - glBegin(GL_LINE_STRIP); - for (i = 0, pt = points; i < totpoints && pt; i++, pt++) { - if (sflag & GP_STROKE_2DSPACE) { - glVertex2f(pt->x, pt->y); - } - else if (sflag & GP_STROKE_2DIMAGE) { - const float x = (pt->x * winx) + offsx; - const float y = (pt->y * winy) + offsy; - - glVertex2f(x, y); - } - else { - const float x = (pt->x / 100 * winx) + offsx; - const float y = (pt->y / 100 * winy) + offsy; - - glVertex2f(x, y); - } - } - glEnd(); + + /* strokes in Image Editor need a scale factor, since units there are not pixels! */ + float scalefac = 1.0f; + if ((dflag & GP_DRAWDATA_IEDITHACK) && (dflag & GP_DRAWDATA_ONLYV2D)) { + scalefac = 0.001f; } /* tessellation code - draw stroke as series of connected quads with connection * edges rotated to minimize shrinking artifacts, and rounded endcaps */ - else { + { bGPDspoint *pt1, *pt2; float pm[2]; int i; @@ -299,22 +486,8 @@ static void gp_draw_stroke(bGPDspoint *points, int totpoints, short thickness_s, float pthick; /* thickness at segment point */ /* get x and y coordinates from points */ - if (sflag & GP_STROKE_2DSPACE) { - s0[0] = pt1->x; s0[1] = pt1->y; - s1[0] = pt2->x; s1[1] = pt2->y; - } - else if (sflag & GP_STROKE_2DIMAGE) { - s0[0] = (pt1->x * winx) + offsx; - s0[1] = (pt1->y * winy) + offsy; - s1[0] = (pt2->x * winx) + offsx; - s1[1] = (pt2->y * winy) + offsy; - } - else { - s0[0] = (pt1->x / 100 * winx) + offsx; - s0[1] = (pt1->y / 100 * winy) + offsy; - s1[0] = (pt2->x / 100 * winx) + offsx; - s1[1] = (pt2->y / 100 * winy) + offsy; - } + gp_calc_2d_stroke_xy(pt1, sflag, offsx, offsy, winx, winy, s0); + gp_calc_2d_stroke_xy(pt2, sflag, offsx, offsy, winx, winy, s1); /* calculate gradient and normal - 'angle'=(ny/nx) */ m1[1] = s1[1] - s0[1]; @@ -324,12 +497,12 @@ static void gp_draw_stroke(bGPDspoint *points, int totpoints, short thickness_s, m2[0] = m1[1]; /* always use pressure from first point here */ - pthick = (pt1->pressure * thickness); + pthick = (pt1->pressure * thickness * scalefac); /* if the first segment, start of segment is segment's normal */ if (i == 0) { - /* draw start cap first - * - make points slightly closer to center (about halfway across) + /* draw start cap first + * - make points slightly closer to center (about halfway across) */ mt[0] = m2[0] * pthick * 0.5f; mt[1] = m2[1] * pthick * 0.5f; @@ -369,7 +542,7 @@ static void gp_draw_stroke(bGPDspoint *points, int totpoints, short thickness_s, mb[1] = (pm[1] + m2[1]) / 2; normalize_v2(mb); - /* calculate gradient to apply + /* calculate gradient to apply * - as basis, use just pthick * bisector gradient * - if cross-section not as thick as it should be, add extra padding to fix it */ @@ -399,7 +572,7 @@ static void gp_draw_stroke(bGPDspoint *points, int totpoints, short thickness_s, /* if last segment, also draw end of segment (defined as segment's normal) */ if (i == totpoints - 2) { /* for once, we use second point's pressure (otherwise it won't be drawn) */ - pthick = (pt2->pressure * thickness); + pthick = (pt2->pressure * thickness * scalefac); /* calculate points for end of segment */ mt[0] = m2[0] * pthick; @@ -417,8 +590,8 @@ static void gp_draw_stroke(bGPDspoint *points, int totpoints, short thickness_s, glVertex2fv(t1); - /* draw end cap as last step - * - make points slightly closer to center (about halfway across) + /* draw end cap as last step + * - make points slightly closer to center (about halfway across) */ mt[0] = m2[0] * pthick * 0.5f; mt[1] = m2[1] * pthick * 0.5f; @@ -446,54 +619,61 @@ static void gp_draw_stroke(bGPDspoint *points, int totpoints, short thickness_s, bGPDspoint *pt; int i; + glPointSize((float)(thickness_s + 2)); + glBegin(GL_POINTS); for (i = 0, pt = points; i < totpoints && pt; i++, pt++) { - if (sflag & GP_STROKE_2DSPACE) { - glVertex2fv(&pt->x); - } - else if (sflag & GP_STROKE_2DIMAGE) { - const float x = (float)((pt->x * winx) + offsx); - const float y = (float)((pt->y * winy) + offsy); - - glVertex2f(x, y); - } - else { - const float x = (float)(pt->x / 100 * winx) + offsx; - const float y = (float)(pt->y / 100 * winy) + offsy; - - glVertex2f(x, y); - } + float co[2]; + + gp_calc_2d_stroke_xy(pt, sflag, offsx, offsy, winx, winy, co); + glVertex2fv(co); } glEnd(); } } -/* ----- General Drawing ------ */ +/* ----- Strokes Drawing ------ */ + +/* Helper for doing all the checks on whether a stroke can be drawn */ +static bool gp_can_draw_stroke(const bGPDstroke *gps, const int dflag) +{ + /* skip stroke if it isn't in the right display space for this drawing context */ + /* 1) 3D Strokes */ + if ((dflag & GP_DRAWDATA_ONLY3D) && !(gps->flag & GP_STROKE_3DSPACE)) + return false; + if (!(dflag & GP_DRAWDATA_ONLY3D) && (gps->flag & GP_STROKE_3DSPACE)) + return false; + + /* 2) Screen Space 2D Strokes */ + if ((dflag & GP_DRAWDATA_ONLYV2D) && !(gps->flag & GP_STROKE_2DSPACE)) + return false; + if (!(dflag & GP_DRAWDATA_ONLYV2D) && (gps->flag & GP_STROKE_2DSPACE)) + return false; + + /* 3) Image Space (2D) */ + if ((dflag & GP_DRAWDATA_ONLYI2D) && !(gps->flag & GP_STROKE_2DIMAGE)) + return false; + if (!(dflag & GP_DRAWDATA_ONLYI2D) && (gps->flag & GP_STROKE_2DIMAGE)) + return false; + + + /* skip stroke if it doesn't have any valid data */ + if ((gps->points == NULL) || (gps->totpoints < 1)) + return false; + + /* stroke can be drawn */ + return true; +} /* draw a set of strokes */ static void gp_draw_strokes(bGPDframe *gpf, int offsx, int offsy, int winx, int winy, int dflag, - short debug, short lthick, const float color[4]) + bool debug, short lthick, const float color[4], const float fill_color[4]) { bGPDstroke *gps; - /* set color first (may need to reset it again later too) */ - glColor4fv(color); - for (gps = gpf->strokes.first; gps; gps = gps->next) { - /* check if stroke can be drawn - checks here generally fall into pairs */ - if ((dflag & GP_DRAWDATA_ONLY3D) && !(gps->flag & GP_STROKE_3DSPACE)) - continue; - if (!(dflag & GP_DRAWDATA_ONLY3D) && (gps->flag & GP_STROKE_3DSPACE)) - continue; - if ((dflag & GP_DRAWDATA_ONLYV2D) && !(gps->flag & GP_STROKE_2DSPACE)) - continue; - if (!(dflag & GP_DRAWDATA_ONLYV2D) && (gps->flag & GP_STROKE_2DSPACE)) - continue; - if ((dflag & GP_DRAWDATA_ONLYI2D) && !(gps->flag & GP_STROKE_2DIMAGE)) - continue; - if (!(dflag & GP_DRAWDATA_ONLYI2D) && (gps->flag & GP_STROKE_2DIMAGE)) - continue; - if ((gps->points == NULL) || (gps->totpoints < 1)) + /* check if stroke can be drawn */ + if (gp_can_draw_stroke(gps, dflag) == false) continue; /* check which stroke-drawer to use */ @@ -515,11 +695,27 @@ static void gp_draw_strokes(bGPDframe *gpf, int offsx, int offsy, int winx, int #endif } - if (gps->totpoints == 1) { - gp_draw_stroke_point(gps->points, lthick, dflag, gps->flag, offsx, offsy, winx, winy); + /* 3D Fill */ + if ((dflag & GP_DRAWDATA_FILL) && (gps->totpoints >= 3)) { + glColor4fv(fill_color); + gp_draw_stroke_fill(gps->points, gps->totpoints, lthick, dflag, gps->flag, offsx, offsy, winx, winy); + } + + /* 3D Stroke */ + glColor4fv(color); + + if (dflag & GP_DRAWDATA_VOLUMETRIC) { + /* volumetric stroke drawing */ + gp_draw_stroke_volumetric_3d(gps->points, gps->totpoints, lthick, dflag, gps->flag); } else { - gp_draw_stroke_3d(gps->points, gps->totpoints, lthick, debug); + /* 3D Lines - OpenGL primitives-based */ + if (gps->totpoints == 1) { + gp_draw_stroke_point(gps->points, lthick, dflag, gps->flag, offsx, offsy, winx, winy); + } + else { + gp_draw_stroke_3d(gps->points, gps->totpoints, lthick, debug, gps->flag); + } } if (no_xray) { @@ -534,116 +730,301 @@ static void gp_draw_strokes(bGPDframe *gpf, int offsx, int offsy, int winx, int } } else { - if (gps->totpoints == 1) { - gp_draw_stroke_point(gps->points, lthick, dflag, gps->flag, offsx, offsy, winx, winy); + /* 2D - Fill */ + if ((dflag & GP_DRAWDATA_FILL) && (gps->totpoints >= 3)) { + glColor4fv(fill_color); + gp_draw_stroke_fill(gps->points, gps->totpoints, lthick, dflag, gps->flag, offsx, offsy, winx, winy); + } + + /* 2D Strokes... */ + glColor4fv(color); + + if (dflag & GP_DRAWDATA_VOLUMETRIC) { + /* blob/disk-based "volumetric" drawing */ + gp_draw_stroke_volumetric_2d(gps->points, gps->totpoints, lthick, dflag, gps->flag, offsx, offsy, winx, winy); } else { - gp_draw_stroke(gps->points, gps->totpoints, lthick, dflag, gps->flag, debug, offsx, offsy, winx, winy); + /* normal 2D strokes */ + if (gps->totpoints == 1) { + gp_draw_stroke_point(gps->points, lthick, dflag, gps->flag, offsx, offsy, winx, winy); + } + else { + gp_draw_stroke_2d(gps->points, gps->totpoints, lthick, dflag, gps->flag, debug, offsx, offsy, winx, winy); + } } } } } -/* draw grease-pencil datablock */ -static void gp_draw_data(bGPdata *gpd, int offsx, int offsy, int winx, int winy, int cfra, int dflag) +/* Draw selected verts for strokes being edited */ +static void gp_draw_strokes_edit(bGPDframe *gpf, int offsx, int offsy, int winx, int winy, short dflag, const float tcolor[3]) { - bGPDlayer *gpl; + bGPDstroke *gps; - /* reset line drawing style (in case previous user didn't reset) */ - setlinestyle(0); + const bool no_xray = (dflag & GP_DRAWDATA_NO_XRAY) != 0; + int mask_orig = 0; - /* turn on smooth lines (i.e. anti-aliasing) */ - glEnable(GL_LINE_SMOOTH); + /* set up depth masks... */ + if (dflag & GP_DRAWDATA_ONLY3D) { + if (no_xray) { + glGetIntegerv(GL_DEPTH_WRITEMASK, &mask_orig); + glDepthMask(0); + glEnable(GL_DEPTH_TEST); + + /* first arg is normally rv3d->dist, but this isn't + * available here and seems to work quite well without */ + bglPolygonOffset(1.0f, 1.0f); +#if 0 + glEnable(GL_POLYGON_OFFSET_LINE); + glPolygonOffset(-1.0f, -1.0f); +#endif + } + } - /* turn on alpha-blending */ - glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); - glEnable(GL_BLEND); + + /* draw stroke verts */ + for (gps = gpf->strokes.first; gps; gps = gps->next) { + bGPDspoint *pt; + float vsize, bsize; + int i; + + /* check if stroke can be drawn */ + if (gp_can_draw_stroke(gps, dflag) == false) + continue; + + /* Optimisation: only draw points for selected strokes + * We assume that selected points can only occur in + * strokes that are selected too. + */ + if ((gps->flag & GP_STROKE_SELECT) == 0) + continue; + + /* Get size of verts: + * - The selected state needs to be larger than the unselected state so that + * they stand out more. + * - We use the theme setting for size of the unselected verts + */ + bsize = UI_GetThemeValuef(TH_GP_VERTEX_SIZE); + if ((int)bsize > 8) { + vsize = 10.0f; + bsize = 8.0f; + } + else { + vsize = bsize + 2; + } + + /* First Pass: Draw all the verts (i.e. these become the unselected state) */ + if (tcolor != NULL) { + /* for now, we assume that the base color of the points is not too close to the real color */ + glColor3fv(tcolor); + } + else { + /* this doesn't work well with the default theme and black strokes... */ + UI_ThemeColor(TH_GP_VERTEX); + } + glPointSize(bsize); + + glBegin(GL_POINTS); + for (i = 0, pt = gps->points; i < gps->totpoints && pt; i++, pt++) { + if (gps->flag & GP_STROKE_3DSPACE) { + glVertex3fv(&pt->x); + } + else { + float co[2]; + + gp_calc_2d_stroke_xy(pt, gps->flag, offsx, offsy, winx, winy, co); + glVertex2fv(co); + } + } + glEnd(); - /* loop over layers, drawing them */ + + /* Second Pass: Draw only verts which are selected */ + UI_ThemeColor(TH_GP_VERTEX_SELECT); + glPointSize(vsize); + + glBegin(GL_POINTS); + for (i = 0, pt = gps->points; i < gps->totpoints && pt; i++, pt++) { + if (pt->flag & GP_SPOINT_SELECT) { + if (gps->flag & GP_STROKE_3DSPACE) { + glVertex3fv(&pt->x); + } + else { + float co[2]; + + gp_calc_2d_stroke_xy(pt, gps->flag, offsx, offsy, winx, winy, co); + glVertex2fv(co); + } + } + } + glEnd(); + } + + + /* clear depth mask */ + if (dflag & GP_DRAWDATA_ONLY3D) { + if (no_xray) { + glDepthMask(mask_orig); + glDisable(GL_DEPTH_TEST); + + bglPolygonOffset(0.0, 0.0); +#if 0 + glDisable(GL_POLYGON_OFFSET_LINE); + glPolygonOffset(0, 0); +#endif + } + } +} + +/* ----- General Drawing ------ */ + +/* draw onion-skinning for a layer */ +static void gp_draw_onionskins(bGPDlayer *gpl, bGPDframe *gpf, int offsx, int offsy, int winx, int winy, + int UNUSED(cfra), int dflag, bool debug, short lthick) +{ + const float alpha = gpl->color[3]; + float color[4]; + + /* 1) Draw Previous Frames First */ + if (gpl->flag & GP_LAYER_GHOST_PREVCOL) { + copy_v3_v3(color, gpl->gcolor_prev); + } + else { + copy_v3_v3(color, gpl->color); + } + + if (gpl->gstep) { + bGPDframe *gf; + float fac; + + /* draw previous frames first */ + for (gf = gpf->prev; gf; gf = gf->prev) { + /* check if frame is drawable */ + if ((gpf->framenum - gf->framenum) <= gpl->gstep) { + /* alpha decreases with distance from curframe index */ + fac = 1.0f - ((float)(gpf->framenum - gf->framenum) / (float)(gpl->gstep + 1)); + color[3] = alpha * fac * 0.66f; + gp_draw_strokes(gf, offsx, offsy, winx, winy, dflag, debug, lthick, color, color); + } + else + break; + } + } + else { + /* draw the strokes for the ghost frames (at half of the alpha set by user) */ + if (gpf->prev) { + color[3] = (alpha / 7); + gp_draw_strokes(gpf->prev, offsx, offsy, winx, winy, dflag, debug, lthick, color, color); + } + } + + + /* 2) Now draw next frames */ + if (gpl->flag & GP_LAYER_GHOST_NEXTCOL) { + copy_v3_v3(color, gpl->gcolor_next); + } + else { + copy_v3_v3(color, gpl->color); + } + + if (gpl->gstep_next) { + bGPDframe *gf; + float fac; + + /* now draw next frames */ + for (gf = gpf->next; gf; gf = gf->next) { + /* check if frame is drawable */ + if ((gf->framenum - gpf->framenum) <= gpl->gstep_next) { + /* alpha decreases with distance from curframe index */ + fac = 1.0f - ((float)(gf->framenum - gpf->framenum) / (float)(gpl->gstep_next + 1)); + color[3] = alpha * fac * 0.66f; + gp_draw_strokes(gf, offsx, offsy, winx, winy, dflag, debug, lthick, color, color); + } + else + break; + } + } + else { + /* draw the strokes for the ghost frames (at half of the alpha set by user) */ + if (gpf->next) { + color[3] = (alpha / 4); + gp_draw_strokes(gpf->next, offsx, offsy, winx, winy, dflag, debug, lthick, color, color); + } + } + + /* 3) restore alpha */ + glColor4fv(gpl->color); +} + +/* loop over gpencil data layers, drawing them */ +static void gp_draw_data_layers(bGPdata *gpd, int offsx, int offsy, int winx, int winy, int cfra, int dflag) +{ + bGPDlayer *gpl; + for (gpl = gpd->layers.first; gpl; gpl = gpl->next) { bGPDframe *gpf; - short debug = (gpl->flag & GP_LAYER_DRAWDEBUG) ? 1 : 0; + bool debug = (gpl->flag & GP_LAYER_DRAWDEBUG) ? true : false; short lthick = gpl->thickness; - float color[4], tcolor[4]; /* don't draw layer if hidden */ - if (gpl->flag & GP_LAYER_HIDE) + if (gpl->flag & GP_LAYER_HIDE) continue; /* get frame to draw */ gpf = gpencil_layer_getframe(gpl, cfra, 0); - if (gpf == NULL) + if (gpf == NULL) continue; - /* set color, stroke thickness, and point size */ + /* set basic stroke thickness */ glLineWidth(lthick); - copy_v4_v4(color, gpl->color); // just for copying 4 array elements - copy_v4_v4(tcolor, gpl->color); // additional copy of color (for ghosting) - glColor4fv(color); - glPointSize((float)(gpl->thickness + 2)); - /* apply xray layer setting */ - if (gpl->flag & GP_LAYER_NO_XRAY) dflag |= GP_DRAWDATA_NO_XRAY; - else dflag &= ~GP_DRAWDATA_NO_XRAY; + /* Add layer drawing settings to the set of "draw flags" + * NOTE: If the setting doesn't apply, it *must* be cleared, + * as dflag's carry over from the previous layer + */ +#define GP_DRAWFLAG_APPLY(condition, draw_flag_value) { \ + if (condition) dflag |= (draw_flag_value); \ + else dflag &= ~(draw_flag_value); \ + } (void)0 + + /* xray... */ + GP_DRAWFLAG_APPLY((gpl->flag & GP_LAYER_NO_XRAY), GP_DRAWDATA_NO_XRAY); + + /* volumetric strokes... */ + GP_DRAWFLAG_APPLY((gpl->flag & GP_LAYER_VOLUMETRIC), GP_DRAWDATA_VOLUMETRIC); + + /* fill strokes... */ + // XXX: this is not a very good limit + GP_DRAWFLAG_APPLY((gpl->fill[3] > 0.001f), GP_DRAWDATA_FILL); +#undef GP_DRAWFLAG_APPLY /* draw 'onionskins' (frame left + right) */ - if (gpl->flag & GP_LAYER_ONIONSKIN) { - /* drawing method - only immediately surrounding (gstep = 0), - * or within a frame range on either side (gstep > 0)*/ - if (gpl->gstep) { - bGPDframe *gf; - float fac; - - /* draw previous frames first */ - for (gf = gpf->prev; gf; gf = gf->prev) { - /* check if frame is drawable */ - if ((gpf->framenum - gf->framenum) <= gpl->gstep) { - /* alpha decreases with distance from curframe index */ - fac = 1.0f - ((float)(gpf->framenum - gf->framenum) / (float)(gpl->gstep + 1)); - tcolor[3] = color[3] * fac * 0.66f; - gp_draw_strokes(gf, offsx, offsy, winx, winy, dflag, debug, lthick, tcolor); - } - else - break; - } - - /* now draw next frames */ - for (gf = gpf->next; gf; gf = gf->next) { - /* check if frame is drawable */ - if ((gf->framenum - gpf->framenum) <= gpl->gstep) { - /* alpha decreases with distance from curframe index */ - fac = 1.0f - ((float)(gf->framenum - gpf->framenum) / (float)(gpl->gstep + 1)); - tcolor[3] = color[3] * fac * 0.66f; - gp_draw_strokes(gf, offsx, offsy, winx, winy, dflag, debug, lthick, tcolor); - } - else - break; - } - - /* restore alpha */ - glColor4fv(color); - } - else { - /* draw the strokes for the ghost frames (at half of the alpha set by user) */ - if (gpf->prev) { - tcolor[3] = (color[3] / 7); - gp_draw_strokes(gpf->prev, offsx, offsy, winx, winy, dflag, debug, lthick, tcolor); - } - - if (gpf->next) { - tcolor[3] = (color[3] / 4); - gp_draw_strokes(gpf->next, offsx, offsy, winx, winy, dflag, debug, lthick, tcolor); - } - - /* restore alpha */ - glColor4fv(color); - } + if ((gpl->flag & GP_LAYER_ONIONSKIN) && !(dflag & GP_DRAWDATA_NO_ONIONS)) { + /* Drawing method - only immediately surrounding (gstep = 0), + * or within a frame range on either side (gstep > 0) + */ + gp_draw_onionskins(gpl, gpf, offsx, offsy, winx, winy, cfra, dflag, debug, lthick); } /* draw the strokes already in active frame */ - tcolor[3] = color[3]; - gp_draw_strokes(gpf, offsx, offsy, winx, winy, dflag, debug, lthick, tcolor); + gp_draw_strokes(gpf, offsx, offsy, winx, winy, dflag, debug, lthick, gpl->color, gpl->fill); + + /* Draw verts of selected strokes + * - when doing OpenGL renders, we don't want to be showing these, as that ends up flickering + * - locked layers can't be edited, so there's no point showing these verts + * as they will have no bearings on what gets edited + * - only show when in editmode, since operators shouldn't work otherwise + * (NOTE: doing it this way means that the toggling editmode shows visible change immediately) + */ + /* XXX: perhaps we don't want to show these when users are drawing... */ + if ((G.f & G_RENDER_OGL) == 0 && + (gpl->flag & GP_LAYER_LOCKED) == 0 && + (gpd->flag & GP_DATA_STROKE_EDITMODE)) + { + gp_draw_strokes_edit(gpf, offsx, offsy, winx, winy, dflag, + (gpl->color[3] < 0.95f) ? gpl->color : NULL); + } /* Check if may need to draw the active stroke cache, only if this layer is the active layer * that is being edited. (Stroke buffer is currently stored in gp-data) @@ -651,22 +1032,135 @@ static void gp_draw_data(bGPdata *gpd, int offsx, int offsy, int winx, int winy, if (ED_gpencil_session_active() && (gpl->flag & GP_LAYER_ACTIVE) && (gpf->flag & GP_FRAME_PAINT)) { + /* Set color for drawing buffer stroke - since this may not be set yet */ + glColor4fv(gpl->color); + /* Buffer stroke needs to be drawn with a different linestyle - * to help differentiate them from normal strokes. */ - gp_draw_stroke_buffer(gpd->sbuffer, gpd->sbuffer_size, lthick, dflag, gpd->sbuffer_sflag); + * to help differentiate them from normal strokes. + * + * It should also be noted that sbuffer contains temporary point types + * i.e. tGPspoints NOT bGPDspoints + */ + if (gpl->flag & GP_LAYER_VOLUMETRIC) { + gp_draw_stroke_volumetric_buffer(gpd->sbuffer, gpd->sbuffer_size, lthick, dflag, gpd->sbuffer_sflag); + } + else { + gp_draw_stroke_buffer(gpd->sbuffer, gpd->sbuffer_size, lthick, dflag, gpd->sbuffer_sflag); + } } } +} + +/* draw a short status message in the top-right corner */ +static void gp_draw_status_text(bGPdata *gpd, ARegion *ar) +{ + rcti rect; + + /* Cannot draw any status text when drawing OpenGL Renders */ + if (G.f & G_RENDER_OGL) + return; + + /* Get bounds of region - Necessary to avoid problems with region overlap */ + ED_region_visible_rect(ar, &rect); + + /* for now, this should only be used to indicate when we are in stroke editmode */ + if (gpd->flag & GP_DATA_STROKE_EDITMODE) { + const char *printable = IFACE_("GPencil Stroke Editing"); + float printable_size[2]; + int xco, yco; + + BLF_width_and_height_default(printable, BLF_DRAW_STR_DUMMY_MAX, &printable_size[0], &printable_size[1]); + + xco = (rect.xmax - U.widget_unit) - (int)printable_size[0]; + yco = (rect.ymax - U.widget_unit); + + /* text label */ + UI_ThemeColor(TH_TEXT_HI); +#ifdef WITH_INTERNATIONAL + BLF_draw_default(xco, yco, 0.0f, printable, BLF_DRAW_STR_DUMMY_MAX); +#else + BLF_draw_default_ascii(xco, yco, 0.0f, printable, BLF_DRAW_STR_DUMMY_MAX); +#endif + + /* grease pencil icon... */ + // XXX: is this too intrusive? + glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); + glEnable(GL_BLEND); + + xco -= U.widget_unit; + yco -= (int)printable_size[1] / 2; + + UI_icon_draw(xco, yco, ICON_GREASEPENCIL); + + glDisable(GL_BLEND); + } +} + +/* draw grease-pencil datablock */ +static void gp_draw_data(bGPdata *gpd, int offsx, int offsy, int winx, int winy, int cfra, int dflag) +{ + /* reset line drawing style (in case previous user didn't reset) */ + setlinestyle(0); + + /* turn on smooth lines (i.e. anti-aliasing) */ + glEnable(GL_LINE_SMOOTH); + + /* XXX: turn on some way of ensuring that the polygon edges get smoothed + * GL_POLYGON_SMOOTH is nasty and shouldn't be used, as it ends up + * creating internal white rays due to the ways it accumulates stuff + */ + + /* turn on alpha-blending */ + if (GLEW_VERSION_1_4) { + glBlendFuncSeparate(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA, GL_ONE, GL_ONE_MINUS_SRC_ALPHA); + } + else { + glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); + } + + glEnable(GL_BLEND); + + /* draw! */ + gp_draw_data_layers(gpd, offsx, offsy, winx, winy, cfra, dflag); /* turn off alpha blending, then smooth lines */ glDisable(GL_BLEND); // alpha blending glDisable(GL_LINE_SMOOTH); // smooth lines - + /* restore initial gl conditions */ glLineWidth(1.0); glPointSize(1.0); glColor4f(0, 0, 0, 1); } +/* if we have strokes for scenes (3d view)/clips (movie clip editor) + * and objects/tracks, multiple data blocks have to be drawn */ +static void gp_draw_data_all(Scene *scene, bGPdata *gpd, int offsx, int offsy, int winx, int winy, + int cfra, int dflag, const char spacetype) +{ + bGPdata *gpd_source = NULL; + + if (scene) { + if (spacetype == SPACE_VIEW3D) { + gpd_source = (scene->gpd ? scene->gpd : NULL); + } + else if (spacetype == SPACE_CLIP && scene->clip) { + /* currently drawing only gpencil data from either clip or track, but not both - XXX fix logic behind */ + gpd_source = (scene->clip->gpd ? scene->clip->gpd : NULL); + } + + if (gpd_source) { + gp_draw_data(gpd_source, offsx, offsy, winx, winy, cfra, dflag); + } + } + + /* scene/clip data has already been drawn, only object/track data is drawn here + * if gpd_source == gpd, we don't have any object/track data and we can skip */ + if (gpd_source == NULL || (gpd_source && gpd_source != gpd)) { + gp_draw_data(gpd, offsx, offsy, winx, winy, cfra, dflag); + } +} + /* ----- Grease Pencil Sketches Drawing API ------ */ /* ............................ @@ -693,7 +1187,7 @@ void ED_gpencil_draw_2dimage(const bContext *C) case SPACE_IMAGE: /* image */ case SPACE_CLIP: /* clip */ { - + /* just draw using standard scaling (settings here are currently ignored anyways) */ /* FIXME: the opengl poly-strokes don't draw at right thickness when done this way, so disabled */ offsx = 0; @@ -714,8 +1208,8 @@ void ED_gpencil_draw_2dimage(const bContext *C) sizex = ar->winx; sizey = ar->winy; - /* NOTE: I2D was used in 2.4x, but the old settings for that have been deprecated - * and everything moved to standard View2d + /* NOTE: I2D was used in 2.4x, but the old settings for that have been deprecated + * and everything moved to standard View2d */ dflag |= GP_DRAWDATA_ONLYV2D; break; @@ -732,10 +1226,10 @@ void ED_gpencil_draw_2dimage(const bContext *C) /* draw it! */ - gp_draw_data(gpd, offsx, offsy, sizex, sizey, CFRA, dflag); + gp_draw_data_all(scene, gpd, offsx, offsy, sizex, sizey, CFRA, dflag, sa->spacetype); } -/* draw grease-pencil sketches to specified 2d-view assuming that matrices are already set correctly +/* draw grease-pencil sketches to specified 2d-view assuming that matrices are already set correctly * Note: this gets called twice - first time with onlyv2d=1 to draw 'canvas' strokes, * second time with onlyv2d=0 for screen-aligned strokes */ void ED_gpencil_draw_view2d(const bContext *C, bool onlyv2d) @@ -758,10 +1252,15 @@ void ED_gpencil_draw_view2d(const bContext *C, bool onlyv2d) /* draw it! */ if (onlyv2d) dflag |= (GP_DRAWDATA_ONLYV2D | GP_DRAWDATA_NOSTATUS); - gp_draw_data(gpd, 0, 0, ar->winx, ar->winy, CFRA, dflag); + gp_draw_data_all(scene, gpd, 0, 0, ar->winx, ar->winy, CFRA, dflag, sa->spacetype); + + /* draw status text (if in screen/pixel-space) */ + if (onlyv2d == false) { + gp_draw_status_text(gpd, ar); + } } -/* draw grease-pencil sketches to specified 3d-view assuming that matrices are already set correctly +/* draw grease-pencil sketches to specified 3d-view assuming that matrices are already set correctly * Note: this gets called twice - first time with only3d=1 to draw 3d-strokes, * second time with only3d=0 for screen-aligned strokes */ void ED_gpencil_draw_view3d(Scene *scene, View3D *v3d, ARegion *ar, bool only3d) @@ -770,17 +1269,17 @@ void ED_gpencil_draw_view3d(Scene *scene, View3D *v3d, ARegion *ar, bool only3d) int dflag = 0; RegionView3D *rv3d = ar->regiondata; int offsx, offsy, winx, winy; - + /* check that we have grease-pencil stuff to draw */ gpd = ED_gpencil_data_get_active_v3d(scene, v3d); if (gpd == NULL) return; - + /* when rendering to the offscreen buffer we don't want to * deal with the camera border, otherwise map the coords to the camera border. */ if ((rv3d->persp == RV3D_CAMOB) && !(G.f & G_RENDER_OGL)) { rctf rectf; ED_view3d_calc_camera_border(scene, ar, v3d, rv3d, &rectf, true); /* no shift */ - + offsx = iroundf(rectf.xmin); offsy = iroundf(rectf.ymin); winx = iroundf(rectf.xmax - rectf.xmin); @@ -793,17 +1292,34 @@ void ED_gpencil_draw_view3d(Scene *scene, View3D *v3d, ARegion *ar, bool only3d) winy = ar->winy; } + /* set flags */ + if (only3d) { + /* 3D strokes/3D space: + * - only 3D space points + * - don't status text either (as it's the wrong space) + */ + dflag |= (GP_DRAWDATA_ONLY3D | GP_DRAWDATA_NOSTATUS); + } + + if (v3d->flag2 & V3D_RENDER_OVERRIDE) { + /* don't draw status text when "only render" flag is set */ + dflag |= GP_DRAWDATA_NOSTATUS; + } + /* draw it! */ - if (only3d) dflag |= (GP_DRAWDATA_ONLY3D | GP_DRAWDATA_NOSTATUS); - - gp_draw_data(gpd, offsx, offsy, winx, winy, CFRA, dflag); + gp_draw_data_all(scene, gpd, offsx, offsy, winx, winy, CFRA, dflag, v3d->spacetype); + + /* draw status text (if in screen/pixel-space) */ + if (only3d == false) { + gp_draw_status_text(gpd, ar); + } } -void ED_gpencil_draw_ex(bGPdata *gpd, int winx, int winy, const int cfra) +void ED_gpencil_draw_ex(Scene *scene, bGPdata *gpd, int winx, int winy, const int cfra, const char spacetype) { int dflag = GP_DRAWDATA_NOSTATUS | GP_DRAWDATA_ONLYV2D; - - gp_draw_data(gpd, 0, 0, winx, winy, cfra, dflag); + + gp_draw_data_all(scene, gpd, 0, 0, winx, winy, cfra, dflag, spacetype); } /* ************************************************** */ diff --git a/source/blender/editors/gpencil/editaction_gpencil.c b/source/blender/editors/gpencil/editaction_gpencil.c index dba80164e93..a2ba6216f9c 100644 --- a/source/blender/editors/gpencil/editaction_gpencil.c +++ b/source/blender/editors/gpencil/editaction_gpencil.c @@ -27,7 +27,7 @@ * \ingroup edgpencil */ - + #include <stdio.h> #include <string.h> #include <stdlib.h> @@ -49,8 +49,6 @@ #include "ED_keyframes_edit.h" #include "ED_markers.h" -#include "gpencil_intern.h" - /* ***************************************** */ /* NOTE ABOUT THIS FILE: * This file contains code for editing Grease Pencil data in the Action Editor @@ -75,7 +73,7 @@ bool ED_gplayer_frames_looper(bGPDlayer *gpl, Scene *scene, short (*gpf_cb)(bGPD if (gpf_cb(gpf, scene)) return true; } - + /* nothing to return */ return false; } @@ -115,7 +113,7 @@ bool ED_gplayer_frame_select_check(bGPDlayer *gpl) bGPDframe *gpf; /* error checking */ - if (gpl == NULL) + if (gpl == NULL) return false; /* stop at the first one found */ @@ -153,9 +151,9 @@ void ED_gpencil_select_frames(bGPDlayer *gpl, short select_mode) bGPDframe *gpf; /* error checking */ - if (gpl == NULL) + if (gpl == NULL) return; - + /* handle according to mode */ for (gpf = gpl->frames.first; gpf; gpf = gpf->next) { gpframe_select(gpf, select_mode); @@ -166,7 +164,7 @@ void ED_gpencil_select_frames(bGPDlayer *gpl, short select_mode) void ED_gplayer_frame_select_set(bGPDlayer *gpl, short mode) { /* error checking */ - if (gpl == NULL) + if (gpl == NULL) return; /* now call the standard function */ @@ -178,11 +176,11 @@ void ED_gpencil_select_frame(bGPDlayer *gpl, int selx, short select_mode) { bGPDframe *gpf; - if (gpl == NULL) + if (gpl == NULL) return; - + gpf = BKE_gpencil_layer_find_frame(gpl, selx); - + if (gpf) { gpframe_select(gpf, select_mode); } @@ -215,7 +213,7 @@ bool ED_gplayer_frames_delete(bGPDlayer *gpl) /* error checking */ if (gpl == NULL) return false; - + /* check for frames to delete */ for (gpf = gpl->frames.first; gpf; gpf = gpfn) { gpfn = gpf->next; @@ -223,7 +221,7 @@ bool ED_gplayer_frames_delete(bGPDlayer *gpl) if (gpf->flag & GP_FRAME_SELECT) changed |= gpencil_layer_delframe(gpl, gpf); } - + return changed; } @@ -242,7 +240,7 @@ void ED_gplayer_frames_duplicate(bGPDlayer *gpl) /* duplicate this frame */ if (gpf->flag & GP_FRAME_SELECT) { - bGPDframe *gpfd; + bGPDframe *gpfd; /* duplicate frame, and deselect self */ gpfd = gpencil_frame_duplicate(gpf); @@ -253,6 +251,23 @@ void ED_gplayer_frames_duplicate(bGPDlayer *gpl) } } +/* Set keyframe type for selected frames from given gp-layer + * \param type The type of keyframe (eBezTriple_KeyframeType) to set selected frames to + */ +void ED_gplayer_frames_keytype_set(bGPDlayer *gpl, short type) +{ + bGPDframe *gpf; + + if (gpl == NULL) + return; + + for (gpf = gpl->frames.first; gpf; gpf = gpf->next) { + if (gpf->flag & GP_FRAME_SELECT) { + gpf->key_type = type; + } + } +} + #if 0 // XXX disabled until grease pencil code stabilises again /* -------------------------------------- */ /* Copy and Paste Tools */ @@ -263,7 +278,7 @@ void ED_gplayer_frames_duplicate(bGPDlayer *gpl) * the current frame and the 'first keyframe' (i.e. the earliest one in all channels). * - The earliest frame is calculated per copy operation. */ - + /* globals for copy/paste data (like for other copy/paste buffers) */ ListBase gpcopybuf = {NULL, NULL}; static int gpcopy_firstframe = 999999999; @@ -271,7 +286,7 @@ static int gpcopy_firstframe = 999999999; /* This function frees any MEM_calloc'ed copy/paste buffer data */ void free_gpcopybuf() { - free_gpencil_layers(&gpcopybuf); + free_gpencil_layers(&gpcopybuf); BLI_listbase_clear(&gpcopybuf); gpcopy_firstframe = 999999999; @@ -375,7 +390,7 @@ void paste_gpdata(Scene *scene) /* find suitable layer from buffer to use to paste from */ for (gpls = gpcopybuf.first; gpls; gpls = gpls->next) { /* check if layer name matches */ - if ((no_name) || (strcmp(gpls->info, gpld->info) == 0)) + if ((no_name) || STREQ(gpls->info, gpld->info)) break; } @@ -398,8 +413,8 @@ void paste_gpdata(Scene *scene) //sa = gpencil_data_findowner((bGPdata *)ale->owner); sa = NULL; - /* this should be the right frame... as it may be a pre-existing frame, - * must make sure that only compatible stroke types get copied over + /* this should be the right frame... as it may be a pre-existing frame, + * must make sure that only compatible stroke types get copied over * - we cannot just add a duplicate frame, as that would cause errors * - need to check for compatible types to minimize memory usage (copying 'junk' over) */ @@ -418,14 +433,14 @@ void paste_gpdata(Scene *scene) if ((gps->flag == 0) || (gps->flag & GP_STROKE_3DSPACE)) stroke_ok = 1; break; - + case SPACE_NODE: /* Nodes Editor: either screen-aligned or view-aligned */ case SPACE_IMAGE: /* Image Editor: either screen-aligned or view\image-aligned */ case SPACE_CLIP: /* Image Editor: either screen-aligned or view\image-aligned */ if ((gps->flag == 0) || (gps->flag & GP_STROKE_2DSPACE)) stroke_ok = 1; break; - + case SPACE_SEQ: /* Sequence Editor: either screen-aligned or view-aligned */ if ((gps->flag == 0) || (gps->flag & GP_STROKE_2DIMAGE)) stroke_ok = 1; @@ -567,9 +582,9 @@ static short mirror_gpf_marker(bGPDframe *gpf, Scene *scene) /* In order for this mirror function to work without * any extra arguments being added, we use the case - * of bezt==NULL to denote that we should find the + * of bezt==NULL to denote that we should find the * marker to mirror over. The static pointer is safe - * to use this way, as it will be set to null after + * to use this way, as it will be set to null after * each cycle in which this is called. */ diff --git a/source/blender/editors/gpencil/gpencil_buttons.c b/source/blender/editors/gpencil/gpencil_buttons.c deleted file mode 100644 index 0acff8fc0a5..00000000000 --- a/source/blender/editors/gpencil/gpencil_buttons.c +++ /dev/null @@ -1,378 +0,0 @@ -/* - * ***** BEGIN GPL LICENSE BLOCK ***** - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU General Public License - * as published by the Free Software Foundation; either version 2 - * of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - * - * The Original Code is Copyright (C) 2008, Blender Foundation, Joshua Leung - * This is a new part of Blender - * - * Contributor(s): Joshua Leung - * - * ***** END GPL LICENSE BLOCK ***** - */ - -/** \file blender/editors/gpencil/gpencil_buttons.c - * \ingroup edgpencil - */ - - -#include <stdio.h> -#include <string.h> -#include <stdlib.h> -#include <stddef.h> - -#include "BLI_blenlib.h" - -#include "BLF_translation.h" - -#include "DNA_gpencil_types.h" -#include "DNA_screen_types.h" -#include "DNA_space_types.h" - -#include "BKE_context.h" -#include "BKE_global.h" -#include "BKE_gpencil.h" - -#include "WM_api.h" -#include "WM_types.h" - -#include "RNA_access.h" - -#include "ED_gpencil.h" - -#include "UI_interface.h" -#include "UI_resources.h" - -#include "gpencil_intern.h" - -/* ************************************************** */ -/* GREASE PENCIL PANEL-UI DRAWING */ - -/* Every space which implements Grease-Pencil functionality should have a panel - * for the settings. All of the space-dependent parts should be coded in the panel - * code for that space, but the rest is all handled by generic panel here. - */ - -/* ------- Callbacks ----------- */ -/* These are just 'dummy wrappers' around gpencil api calls */ - -/* make layer active one after being clicked on */ -static void gp_ui_activelayer_cb(bContext *C, void *gpd, void *gpl) -{ - /* make sure the layer we want to remove is the active one */ - gpencil_layer_setactive(gpd, gpl); - - WM_event_add_notifier(C, NC_GPENCIL | ND_DATA | NA_EDITED, NULL); -} - -/* delete 'active' layer */ -static void gp_ui_dellayer_cb(bContext *C, void *gpd, void *gpl) -{ - gpencil_layer_delete((bGPdata *)gpd, (bGPDlayer *)gpl); - - WM_event_add_notifier(C, NC_GPENCIL | ND_DATA | NA_EDITED, NULL); -} - -/* move layer up */ -static void gp_ui_layer_up_cb(bContext *C, void *gpd_v, void *gpl_v) -{ - bGPdata *gpd = gpd_v; - bGPDlayer *gpl = gpl_v; - - BLI_remlink(&gpd->layers, gpl); - BLI_insertlinkbefore(&gpd->layers, gpl->prev, gpl); - - WM_event_add_notifier(C, NC_GPENCIL | ND_DATA | NA_EDITED, NULL); -} - -/* move layer down */ -static void gp_ui_layer_down_cb(bContext *C, void *gpd_v, void *gpl_v) -{ - bGPdata *gpd = gpd_v; - bGPDlayer *gpl = gpl_v; - - BLI_remlink(&gpd->layers, gpl); - BLI_insertlinkafter(&gpd->layers, gpl->next, gpl); - - WM_event_add_notifier(C, NC_GPENCIL | ND_DATA | NA_EDITED, NULL); -} - -/* ------- Drawing Code ------- */ - -/* draw the controls for a given layer */ -static void gp_drawui_layer(uiLayout *layout, bGPdata *gpd, bGPDlayer *gpl, const bool is_v3d) -{ - uiLayout *box = NULL, *split = NULL; - uiLayout *col = NULL; - uiLayout *row = NULL, *sub = NULL; - uiBlock *block; - uiBut *but; - PointerRNA ptr; - int icon; - - /* make pointer to layer data */ - RNA_pointer_create((ID *)gpd, &RNA_GPencilLayer, gpl, &ptr); - - /* unless button has own callback, it adds this callback to button */ - block = uiLayoutGetBlock(layout); - uiBlockSetFunc(block, gp_ui_activelayer_cb, gpd, gpl); - - /* draw header ---------------------------------- */ - /* get layout-row + UI-block for header */ - box = uiLayoutBox(layout); - - row = uiLayoutRow(box, false); - uiLayoutSetAlignment(row, UI_LAYOUT_ALIGN_EXPAND); - block = uiLayoutGetBlock(row); /* err... */ - - uiBlockSetEmboss(block, UI_EMBOSSN); - - /* left-align ............................... */ - sub = uiLayoutRow(row, false); - - /* active */ - block = uiLayoutGetBlock(sub); - icon = (gpl->flag & GP_LAYER_ACTIVE) ? ICON_RADIOBUT_ON : ICON_RADIOBUT_OFF; - but = uiDefIconButBitI(block, TOG, GP_LAYER_ACTIVE, 0, icon, 0, 0, UI_UNIT_X, UI_UNIT_Y, - &gpl->flag, 0.0, 0.0, 0.0, 0.0, TIP_("Set active layer")); - uiButSetFunc(but, gp_ui_activelayer_cb, gpd, gpl); - - /* locked */ - icon = (gpl->flag & GP_LAYER_LOCKED) ? ICON_LOCKED : ICON_UNLOCKED; - uiItemR(sub, &ptr, "lock", 0, "", icon); - - /* when layer is locked or hidden, only draw header */ - if (gpl->flag & (GP_LAYER_LOCKED | GP_LAYER_HIDE)) { - char name[256]; /* gpl->info is 128, but we need space for 'locked/hidden' as well */ - - /* visibility button (only if hidden but not locked!) */ - if ((gpl->flag & GP_LAYER_HIDE) && !(gpl->flag & GP_LAYER_LOCKED)) - uiItemR(sub, &ptr, "hide", 0, "", ICON_RESTRICT_VIEW_ON); - - /* name */ - if (gpl->flag & GP_LAYER_HIDE) - BLI_snprintf(name, sizeof(name), IFACE_("%s (Hidden)"), gpl->info); - else - BLI_snprintf(name, sizeof(name), IFACE_("%s (Locked)"), gpl->info); - uiItemL(sub, name, ICON_NONE); - - /* delete button (only if hidden but not locked!) */ - if ((gpl->flag & GP_LAYER_HIDE) && !(gpl->flag & GP_LAYER_LOCKED)) { - /* right-align ............................... */ - sub = uiLayoutRow(row, true); - uiLayoutSetAlignment(sub, UI_LAYOUT_ALIGN_RIGHT); - block = uiLayoutGetBlock(sub); /* XXX... err... */ - - but = uiDefIconBut(block, BUT, 0, ICON_X, 0, 0, UI_UNIT_X, UI_UNIT_Y, - NULL, 0.0, 0.0, 0.0, 0.0, TIP_("Delete layer")); - uiButSetFunc(but, gp_ui_dellayer_cb, gpd, gpl); - } - uiBlockSetEmboss(block, UI_EMBOSS); - } - else { - /* draw rest of header -------------------------------- */ - /* visibility button */ - uiItemR(sub, &ptr, "hide", 0, "", ICON_RESTRICT_VIEW_OFF); - - /* frame locking */ - /* TODO: this needs its own icons... */ - icon = (gpl->flag & GP_LAYER_FRAMELOCK) ? ICON_RENDER_STILL : ICON_RENDER_ANIMATION; - uiItemR(sub, &ptr, "lock_frame", 0, "", icon); - - uiBlockSetEmboss(block, UI_EMBOSS); - - /* name */ - uiItemR(sub, &ptr, "info", 0, "", ICON_NONE); - - /* move up/down */ - uiBlockBeginAlign(block); - - if (gpl->prev) { - but = uiDefIconBut(block, BUT, 0, ICON_TRIA_UP, 0, 0, UI_UNIT_X, UI_UNIT_Y, - NULL, 0.0, 0.0, 0.0, 0.0, TIP_("Move layer up")); - uiButSetFunc(but, gp_ui_layer_up_cb, gpd, gpl); - } - if (gpl->next) { - but = uiDefIconBut(block, BUT, 0, ICON_TRIA_DOWN, 0, 0, UI_UNIT_X, UI_UNIT_Y, - NULL, 0.0, 0.0, 0.0, 0.0, TIP_("Move layer down")); - uiButSetFunc(but, gp_ui_layer_down_cb, gpd, gpl); - } - - uiBlockEndAlign(block); - - /* delete 'button' */ - uiBlockSetEmboss(block, UI_EMBOSSN); - /* right-align ............................... */ - sub = uiLayoutRow(row, true); - uiLayoutSetAlignment(sub, UI_LAYOUT_ALIGN_RIGHT); - block = uiLayoutGetBlock(sub); /* XXX... err... */ - - but = uiDefIconBut(block, BUT, 0, ICON_X, 0, 0, UI_UNIT_X, UI_UNIT_Y, - NULL, 0.0, 0.0, 0.0, 0.0, TIP_("Delete layer")); - uiButSetFunc(but, gp_ui_dellayer_cb, gpd, gpl); - uiBlockSetEmboss(block, UI_EMBOSS); - - /* new backdrop ----------------------------------- */ - box = uiLayoutBox(layout); - split = uiLayoutSplit(box, 0.5f, false); - - /* draw settings ---------------------------------- */ - /* left column ..................... */ - col = uiLayoutColumn(split, false); - - /* color */ - sub = uiLayoutColumn(col, true); - uiItemR(sub, &ptr, "color", 0, "", ICON_NONE); - uiItemR(sub, &ptr, "alpha", UI_ITEM_R_SLIDER, NULL, ICON_NONE); - - /* stroke thickness */ - uiItemR(col, &ptr, "line_width", UI_ITEM_R_SLIDER, NULL, ICON_NONE); - - /* debugging options */ - if (G.debug & G_DEBUG) { - uiItemR(col, &ptr, "show_points", 0, NULL, ICON_NONE); - } - - /* right column ................... */ - col = uiLayoutColumn(split, false); - - /* onion-skinning */ - sub = uiLayoutColumn(col, true); - uiItemR(sub, &ptr, "use_onion_skinning", 0, NULL, ICON_NONE); - uiItemR(sub, &ptr, "ghost_range_max", 0, IFACE_("Frames"), ICON_NONE); - - /* 3d-view specific drawing options */ - if (is_v3d) { - uiItemR(col, &ptr, "show_x_ray", 0, NULL, ICON_NONE); - } - } -} - -/* stroke drawing options available */ -typedef enum eGP_Stroke_Ops { - STROKE_OPTS_NORMAL = 0, - STROKE_OPTS_V3D_OFF, - STROKE_OPTS_V3D_ON, -} eGP_Stroke_Ops; - -static void draw_gpencil_space_specials(const bContext *C, uiLayout *layout) -{ - uiLayout *col, *row; - SpaceClip *sc = CTX_wm_space_clip(C); - - col = uiLayoutColumn(layout, false); - - if (sc) { - bScreen *screen = CTX_wm_screen(C); - PointerRNA sc_ptr; - - RNA_pointer_create(&screen->id, &RNA_SpaceClipEditor, sc, &sc_ptr); - row = uiLayoutRow(col, true); - uiItemR(row, &sc_ptr, "grease_pencil_source", UI_ITEM_R_EXPAND, NULL, ICON_NONE); - } -} - -/* Draw the contents for a grease-pencil panel*/ -static void draw_gpencil_panel(bContext *C, uiLayout *layout, bGPdata *gpd, PointerRNA *ctx_ptr) -{ - PointerRNA gpd_ptr; - bGPDlayer *gpl; - uiLayout *col, *row; - SpaceClip *sc = CTX_wm_space_clip(C); - short v3d_stroke_opts = STROKE_OPTS_NORMAL; - const bool is_v3d = CTX_wm_view3d(C) != NULL; - - /* make new PointerRNA for Grease Pencil block */ - RNA_id_pointer_create((ID *)gpd, &gpd_ptr); - - /* draw gpd settings first ------------------------------------- */ - col = uiLayoutColumn(layout, false); - - /* current Grease Pencil block */ - /* TODO: show some info about who owns this? */ - uiTemplateID(col, C, ctx_ptr, "grease_pencil", "GPENCIL_OT_data_add", NULL, "GPENCIL_OT_data_unlink"); - - /* add new layer button - can be used even when no data, since it can add a new block too */ - uiItemO(col, IFACE_("New Layer"), ICON_NONE, "GPENCIL_OT_layer_add"); - row = uiLayoutRow(col, true); - uiItemO(row, IFACE_("Delete Frame"), ICON_NONE, "GPENCIL_OT_active_frame_delete"); - uiItemO(row, IFACE_("Convert"), ICON_NONE, "GPENCIL_OT_convert"); - - /* sanity checks... */ - if (gpd == NULL) - return; - - /* draw each layer --------------------------------------------- */ - for (gpl = gpd->layers.first; gpl; gpl = gpl->next) { - col = uiLayoutColumn(layout, true); - gp_drawui_layer(col, gpd, gpl, is_v3d); - } - - /* draw gpd drawing settings first ------------------------------------- */ - col = uiLayoutColumn(layout, true); - /* label */ - uiItemL(col, IFACE_("Drawing Settings:"), ICON_NONE); - - /* check whether advanced 3D-View drawing space options can be used */ - if (is_v3d) { - if (gpd->flag & (GP_DATA_DEPTH_STROKE | GP_DATA_DEPTH_VIEW)) - v3d_stroke_opts = STROKE_OPTS_V3D_ON; - else - v3d_stroke_opts = STROKE_OPTS_V3D_OFF; - } - - /* drawing space options */ - row = uiLayoutRow(col, true); - uiItemEnumR_string(row, &gpd_ptr, "draw_mode", "VIEW", NULL, ICON_NONE); - uiItemEnumR_string(row, &gpd_ptr, "draw_mode", "CURSOR", NULL, ICON_NONE); - - if (sc == NULL) { - row = uiLayoutRow(col, true); - uiLayoutSetActive(row, v3d_stroke_opts); - uiItemEnumR_string(row, &gpd_ptr, "draw_mode", "SURFACE", NULL, ICON_NONE); - uiItemEnumR_string(row, &gpd_ptr, "draw_mode", "STROKE", NULL, ICON_NONE); - - row = uiLayoutRow(col, false); - uiLayoutSetActive(row, v3d_stroke_opts == STROKE_OPTS_V3D_ON); - uiItemR(row, &gpd_ptr, "use_stroke_endpoints", 0, NULL, ICON_NONE); - } -} - -void ED_gpencil_panel_standard_header(const bContext *C, Panel *pa) -{ - PointerRNA ptr; - RNA_pointer_create((ID *)CTX_wm_screen(C), &RNA_Space, CTX_wm_space_data(C), &ptr); - - uiItemR(pa->layout, &ptr, "show_grease_pencil", 0, "", ICON_NONE); -} - -/* Standard panel to be included wherever Grease Pencil is used... */ -void ED_gpencil_panel_standard(const bContext *C, Panel *pa) -{ - bGPdata **gpd_ptr = NULL; - PointerRNA ptr; - - /* if (v3d->flag2 & V3D_DISPGP)... etc. */ - - draw_gpencil_space_specials(C, pa->layout); - - /* get pointer to Grease Pencil Data */ - gpd_ptr = ED_gpencil_data_get_pointers((bContext *)C, &ptr); - - if (gpd_ptr) - draw_gpencil_panel((bContext *)C, pa->layout, *gpd_ptr, &ptr); -} - -/* ************************************************** */ diff --git a/source/blender/editors/gpencil/gpencil_convert.c b/source/blender/editors/gpencil/gpencil_convert.c new file mode 100644 index 00000000000..e8d73eaffdf --- /dev/null +++ b/source/blender/editors/gpencil/gpencil_convert.c @@ -0,0 +1,1484 @@ +/* + * ***** BEGIN GPL LICENSE BLOCK ***** + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + * + * The Original Code is Copyright (C) 2008, Blender Foundation + * This is a new part of Blender + * + * Contributor(s): Joshua Leung + * Bastien Montagne + * + * ***** END GPL LICENSE BLOCK ***** + * + * Operator for converting Grease Pencil data to geometry + */ + +/** \file blender/editors/gpencil/gpencil_convert.c + * \ingroup edgpencil + */ + + +#include <stdio.h> +#include <string.h> +#include <stdlib.h> +#include <stddef.h> +#include <math.h> + +#include "MEM_guardedalloc.h" + +#include "BLI_math.h" +#include "BLI_blenlib.h" +#include "BLI_rand.h" +#include "BLI_utildefines.h" + +#include "BLT_translation.h" + +#include "DNA_anim_types.h" +#include "DNA_curve_types.h" +#include "DNA_object_types.h" +#include "DNA_node_types.h" +#include "DNA_scene_types.h" +#include "DNA_screen_types.h" +#include "DNA_space_types.h" +#include "DNA_view3d_types.h" +#include "DNA_gpencil_types.h" + +#include "BKE_context.h" +#include "BKE_curve.h" +#include "BKE_depsgraph.h" +#include "BKE_fcurve.h" +#include "BKE_global.h" +#include "BKE_gpencil.h" +#include "BKE_library.h" +#include "BKE_object.h" +#include "BKE_report.h" +#include "BKE_scene.h" +#include "BKE_screen.h" +#include "BKE_tracking.h" + +#include "UI_interface.h" + +#include "WM_api.h" +#include "WM_types.h" + +#include "RNA_access.h" +#include "RNA_define.h" + +#include "UI_view2d.h" + +#include "ED_gpencil.h" +#include "ED_view3d.h" +#include "ED_clip.h" +#include "ED_keyframing.h" + +#include "gpencil_intern.h" + +/* ************************************************ */ +/* Grease Pencil to Data Operator */ + +/* defines for possible modes */ +enum { + GP_STROKECONVERT_PATH = 1, + GP_STROKECONVERT_CURVE, + GP_STROKECONVERT_POLY, +}; + +/* Defines for possible timing modes */ +enum { + GP_STROKECONVERT_TIMING_NONE = 1, + GP_STROKECONVERT_TIMING_LINEAR = 2, + GP_STROKECONVERT_TIMING_FULL = 3, + GP_STROKECONVERT_TIMING_CUSTOMGAP = 4, +}; + +/* RNA enum define */ +static EnumPropertyItem prop_gpencil_convertmodes[] = { + {GP_STROKECONVERT_PATH, "PATH", 0, "Path", ""}, + {GP_STROKECONVERT_CURVE, "CURVE", 0, "Bezier Curve", ""}, + {GP_STROKECONVERT_POLY, "POLY", 0, "Polygon Curve", ""}, + {0, NULL, 0, NULL, NULL} +}; + +static EnumPropertyItem prop_gpencil_convert_timingmodes_restricted[] = { + {GP_STROKECONVERT_TIMING_NONE, "NONE", 0, "No Timing", "Ignore timing"}, + {GP_STROKECONVERT_TIMING_LINEAR, "LINEAR", 0, "Linear", "Simple linear timing"}, + {0, NULL, 0, NULL, NULL}, +}; + +static EnumPropertyItem prop_gpencil_convert_timingmodes[] = { + {GP_STROKECONVERT_TIMING_NONE, "NONE", 0, "No Timing", "Ignore timing"}, + {GP_STROKECONVERT_TIMING_LINEAR, "LINEAR", 0, "Linear", "Simple linear timing"}, + {GP_STROKECONVERT_TIMING_FULL, "FULL", 0, "Original", "Use the original timing, gaps included"}, + {GP_STROKECONVERT_TIMING_CUSTOMGAP, "CUSTOMGAP", 0, "Custom Gaps", + "Use the original timing, but with custom gap lengths (in frames)"}, + {0, NULL, 0, NULL, NULL}, +}; + +static EnumPropertyItem *rna_GPConvert_mode_items(bContext *UNUSED(C), PointerRNA *ptr, PropertyRNA *UNUSED(prop), + bool *UNUSED(r_free)) +{ + if (RNA_boolean_get(ptr, "use_timing_data")) { + return prop_gpencil_convert_timingmodes; + } + return prop_gpencil_convert_timingmodes_restricted; +} + +/* --- */ + +/* convert the coordinates from the given stroke point into 3d-coordinates + * - assumes that the active space is the 3D-View + */ +static void gp_strokepoint_convertcoords(bContext *C, bGPDstroke *gps, bGPDspoint *pt, float p3d[3], rctf *subrect) +{ + Scene *scene = CTX_data_scene(C); + View3D *v3d = CTX_wm_view3d(C); + ARegion *ar = CTX_wm_region(C); + + if (gps->flag & GP_STROKE_3DSPACE) { + /* directly use 3d-coordinates */ + copy_v3_v3(p3d, &pt->x); + } + else { + const float *fp = ED_view3d_cursor3d_get(scene, v3d); + float mvalf[2]; + + /* get screen coordinate */ + if (gps->flag & GP_STROKE_2DSPACE) { + View2D *v2d = &ar->v2d; + UI_view2d_view_to_region_fl(v2d, pt->x, pt->y, &mvalf[0], &mvalf[1]); + } + else { + if (subrect) { + mvalf[0] = (((float)pt->x / 100.0f) * BLI_rctf_size_x(subrect)) + subrect->xmin; + mvalf[1] = (((float)pt->y / 100.0f) * BLI_rctf_size_y(subrect)) + subrect->ymin; + } + else { + mvalf[0] = (float)pt->x / 100.0f * ar->winx; + mvalf[1] = (float)pt->y / 100.0f * ar->winy; + } + } + + ED_view3d_win_to_3d(ar, fp, mvalf, p3d); + } +} + +/* --- */ + +/* temp struct for gp_stroke_path_animation() */ +typedef struct tGpTimingData { + /* Data set from operator settings */ + int mode; + int frame_range; /* Number of frames evaluated for path animation */ + int start_frame, end_frame; + bool realtime; /* Will overwrite end_frame in case of Original or CustomGap timing... */ + float gap_duration, gap_randomness; /* To be used with CustomGap mode*/ + int seed; + + /* Data set from points, used to compute final timing FCurve */ + int num_points, cur_point; + + /* Distances */ + float *dists; + float tot_dist; + + /* Times */ + float *times; /* Note: Gap times will be negative! */ + float tot_time, gap_tot_time; + double inittime; + + /* Only used during creation of dists & times lists. */ + float offset_time; +} tGpTimingData; + +/* Init point buffers for timing data. + * Note this assumes we only grow those arrays! + */ +static void gp_timing_data_set_nbr(tGpTimingData *gtd, const int nbr) +{ + float *tmp; + + BLI_assert(nbr > gtd->num_points); + + /* distances */ + tmp = gtd->dists; + gtd->dists = MEM_callocN(sizeof(float) * nbr, __func__); + if (tmp) { + memcpy(gtd->dists, tmp, sizeof(float) * gtd->num_points); + MEM_freeN(tmp); + } + + /* times */ + tmp = gtd->times; + gtd->times = MEM_callocN(sizeof(float) * nbr, __func__); + if (tmp) { + memcpy(gtd->times, tmp, sizeof(float) * gtd->num_points); + MEM_freeN(tmp); + } + + gtd->num_points = nbr; +} + +/* add stroke point to timing buffers */ +static void gp_timing_data_add_point(tGpTimingData *gtd, const double stroke_inittime, const float time, + const float delta_dist) +{ + float delta_time = 0.0f; + const int cur_point = gtd->cur_point; + + if (!cur_point) { + /* Special case, first point, if time is not 0.0f we have to compensate! */ + gtd->offset_time = -time; + gtd->times[cur_point] = 0.0f; + } + else if (time < 0.0f) { + /* This is a gap, negative value! */ + gtd->times[cur_point] = -(((float)(stroke_inittime - gtd->inittime)) + time + gtd->offset_time); + delta_time = -gtd->times[cur_point] - gtd->times[cur_point - 1]; + + gtd->gap_tot_time += delta_time; + } + else { + gtd->times[cur_point] = (((float)(stroke_inittime - gtd->inittime)) + time + gtd->offset_time); + delta_time = gtd->times[cur_point] - fabsf(gtd->times[cur_point - 1]); + } + + gtd->tot_time += delta_time; + gtd->tot_dist += delta_dist; + gtd->dists[cur_point] = gtd->tot_dist; + + gtd->cur_point++; +} + +/* In frames! Binary search for FCurve keys have a threshold of 0.01, so we can't set + * arbitrarily close points - this is esp. important with NoGaps mode! + */ +#define MIN_TIME_DELTA 0.02f + +/* Loop over next points to find the end of the stroke, and compute */ +static int gp_find_end_of_stroke_idx(tGpTimingData *gtd, RNG *rng, const int idx, const int nbr_gaps, + int *nbr_done_gaps, const float tot_gaps_time, const float delta_time, + float *next_delta_time) +{ + int j; + + for (j = idx + 1; j < gtd->num_points; j++) { + if (gtd->times[j] < 0) { + gtd->times[j] = -gtd->times[j]; + if (gtd->mode == GP_STROKECONVERT_TIMING_CUSTOMGAP) { + /* In this mode, gap time between this stroke and the next should be 0 currently... + * So we have to compute its final duration! + */ + if (gtd->gap_randomness > 0.0f) { + /* We want gaps that are in gtd->gap_duration +/- gtd->gap_randomness range, + * and which sum to exactly tot_gaps_time... + */ + int rem_gaps = nbr_gaps - (*nbr_done_gaps); + if (rem_gaps < 2) { + /* Last gap, just give remaining time! */ + *next_delta_time = tot_gaps_time; + } + else { + float delta, min, max; + + /* This code ensures that if the first gaps have been shorter than average gap_duration, + * next gaps will tend to be longer (i.e. try to recover the lateness), and vice-versa! + */ + delta = delta_time - (gtd->gap_duration * (*nbr_done_gaps)); + + /* Clamp min between [-gap_randomness, 0.0], with lower delta giving higher min */ + min = -gtd->gap_randomness - delta; + CLAMP(min, -gtd->gap_randomness, 0.0f); + + /* Clamp max between [0.0, gap_randomness], with lower delta giving higher max */ + max = gtd->gap_randomness - delta; + CLAMP(max, 0.0f, gtd->gap_randomness); + *next_delta_time += gtd->gap_duration + (BLI_rng_get_float(rng) * (max - min)) + min; + } + } + else { + *next_delta_time += gtd->gap_duration; + } + } + (*nbr_done_gaps)++; + break; + } + } + + return j - 1; +} + +static void gp_stroke_path_animation_preprocess_gaps(tGpTimingData *gtd, RNG *rng, int *nbr_gaps, float *tot_gaps_time) +{ + int i; + float delta_time = 0.0f; + + for (i = 0; i < gtd->num_points; i++) { + if (gtd->times[i] < 0 && i) { + (*nbr_gaps)++; + gtd->times[i] = -gtd->times[i] - delta_time; + delta_time += gtd->times[i] - gtd->times[i - 1]; + gtd->times[i] = -gtd->times[i - 1]; /* Temp marker, values *have* to be different! */ + } + else { + gtd->times[i] -= delta_time; + } + } + gtd->tot_time -= delta_time; + + *tot_gaps_time = (float)(*nbr_gaps) * gtd->gap_duration; + gtd->tot_time += *tot_gaps_time; + if (G.debug & G_DEBUG) { + printf("%f, %f, %f, %d\n", gtd->tot_time, delta_time, *tot_gaps_time, *nbr_gaps); + } + if (gtd->gap_randomness > 0.0f) { + BLI_rng_srandom(rng, gtd->seed); + } +} + +static void gp_stroke_path_animation_add_keyframes(ReportList *reports, PointerRNA ptr, PropertyRNA *prop, FCurve *fcu, + Curve *cu, tGpTimingData *gtd, RNG *rng, const float time_range, + const int nbr_gaps, const float tot_gaps_time) +{ + /* Use actual recorded timing! */ + const float time_start = (float)gtd->start_frame; + + float last_valid_time = 0.0f; + int end_stroke_idx = -1, start_stroke_idx = 0; + float end_stroke_time = 0.0f; + + /* CustomGaps specific */ + float delta_time = 0.0f, next_delta_time = 0.0f; + int nbr_done_gaps = 0; + + int i; + float cfra; + + /* This is a bit tricky, as: + * - We can't add arbitrarily close points on FCurve (in time). + * - We *must* have all "caps" points of all strokes in FCurve, as much as possible! + */ + for (i = 0; i < gtd->num_points; i++) { + /* If new stroke... */ + if (i > end_stroke_idx) { + start_stroke_idx = i; + delta_time = next_delta_time; + /* find end of that new stroke */ + end_stroke_idx = gp_find_end_of_stroke_idx(gtd, rng, i, nbr_gaps, &nbr_done_gaps, + tot_gaps_time, delta_time, &next_delta_time); + /* This one should *never* be negative! */ + end_stroke_time = time_start + ((gtd->times[end_stroke_idx] + delta_time) / gtd->tot_time * time_range); + } + + /* Simple proportional stuff... */ + cu->ctime = gtd->dists[i] / gtd->tot_dist * cu->pathlen; + cfra = time_start + ((gtd->times[i] + delta_time) / gtd->tot_time * time_range); + + /* And now, the checks about timing... */ + if (i == start_stroke_idx) { + /* If first point of a stroke, be sure it's enough ahead of last valid keyframe, and + * that the end point of the stroke is far enough! + * In case it is not, we keep the end point... + * Note that with CustomGaps mode, this is here we set the actual gap timing! + */ + if ((end_stroke_time - last_valid_time) > MIN_TIME_DELTA * 2) { + if ((cfra - last_valid_time) < MIN_TIME_DELTA) { + cfra = last_valid_time + MIN_TIME_DELTA; + } + insert_keyframe_direct(reports, ptr, prop, fcu, cfra, INSERTKEY_FAST); + last_valid_time = cfra; + } + else if (G.debug & G_DEBUG) { + printf("\t Skipping start point %d, too close from end point %d\n", i, end_stroke_idx); + } + } + else if (i == end_stroke_idx) { + /* Always try to insert end point of a curve (should be safe enough, anyway...) */ + if ((cfra - last_valid_time) < MIN_TIME_DELTA) { + cfra = last_valid_time + MIN_TIME_DELTA; + } + insert_keyframe_direct(reports, ptr, prop, fcu, cfra, INSERTKEY_FAST); + last_valid_time = cfra; + } + else { + /* Else ("middle" point), we only insert it if it's far enough from last keyframe, + * and also far enough from (not yet added!) end_stroke keyframe! + */ + if ((cfra - last_valid_time) > MIN_TIME_DELTA && (end_stroke_time - cfra) > MIN_TIME_DELTA) { + insert_keyframe_direct(reports, ptr, prop, fcu, cfra, INSERTKEY_FAST); + last_valid_time = cfra; + } + else if (G.debug & G_DEBUG) { + printf("\t Skipping \"middle\" point %d, too close from last added point or end point %d\n", + i, end_stroke_idx); + } + } + } +} + +static void gp_stroke_path_animation(bContext *C, ReportList *reports, Curve *cu, tGpTimingData *gtd) +{ + Scene *scene = CTX_data_scene(C); + bAction *act; + FCurve *fcu; + PointerRNA ptr; + PropertyRNA *prop = NULL; + int nbr_gaps = 0, i; + + if (gtd->mode == GP_STROKECONVERT_TIMING_NONE) + return; + + /* gap_duration and gap_randomness are in frames, but we need seconds!!! */ + gtd->gap_duration = FRA2TIME(gtd->gap_duration); + gtd->gap_randomness = FRA2TIME(gtd->gap_randomness); + + /* Enable path! */ + cu->flag |= CU_PATH; + cu->pathlen = gtd->frame_range; + + /* Get RNA pointer to read/write path time values */ + RNA_id_pointer_create((ID *)cu, &ptr); + prop = RNA_struct_find_property(&ptr, "eval_time"); + + /* Ensure we have an F-Curve to add keyframes to */ + act = verify_adt_action((ID *)cu, true); + fcu = verify_fcurve(act, NULL, &ptr, "eval_time", 0, true); + + if (G.debug & G_DEBUG) { + printf("%s: tot len: %f\t\ttot time: %f\n", __func__, gtd->tot_dist, gtd->tot_time); + for (i = 0; i < gtd->num_points; i++) { + printf("\tpoint %d:\t\tlen: %f\t\ttime: %f\n", i, gtd->dists[i], gtd->times[i]); + } + } + + if (gtd->mode == GP_STROKECONVERT_TIMING_LINEAR) { + float cfra; + + /* Linear extrapolation! */ + fcu->extend = FCURVE_EXTRAPOLATE_LINEAR; + + cu->ctime = 0.0f; + cfra = (float)gtd->start_frame; + insert_keyframe_direct(reports, ptr, prop, fcu, cfra, INSERTKEY_FAST); + + cu->ctime = cu->pathlen; + if (gtd->realtime) { + cfra += (float)TIME2FRA(gtd->tot_time); /* Seconds to frames */ + } + else { + cfra = (float)gtd->end_frame; + } + insert_keyframe_direct(reports, ptr, prop, fcu, cfra, INSERTKEY_FAST); + } + else { + /* Use actual recorded timing! */ + RNG *rng = BLI_rng_new(0); + float time_range; + + /* CustomGaps specific */ + float tot_gaps_time = 0.0f; + + /* Pre-process gaps, in case we don't want to keep their original timing */ + if (gtd->mode == GP_STROKECONVERT_TIMING_CUSTOMGAP) { + gp_stroke_path_animation_preprocess_gaps(gtd, rng, &nbr_gaps, &tot_gaps_time); + } + + if (gtd->realtime) { + time_range = (float)TIME2FRA(gtd->tot_time); /* Seconds to frames */ + } + else { + time_range = (float)(gtd->end_frame - gtd->start_frame); + } + + if (G.debug & G_DEBUG) { + printf("GP Stroke Path Conversion: Starting keying!\n"); + } + + gp_stroke_path_animation_add_keyframes(reports, ptr, prop, fcu, cu, gtd, rng, time_range, + nbr_gaps, tot_gaps_time); + + BLI_rng_free(rng); + } + + /* As we used INSERTKEY_FAST mode, we need to recompute all curve's handles now */ + calchandles_fcurve(fcu); + + if (G.debug & G_DEBUG) { + printf("%s: \ntot len: %f\t\ttot time: %f\n", __func__, gtd->tot_dist, gtd->tot_time); + for (i = 0; i < gtd->num_points; i++) { + printf("\tpoint %d:\t\tlen: %f\t\ttime: %f\n", i, gtd->dists[i], gtd->times[i]); + } + printf("\n\n"); + } + + WM_event_add_notifier(C, NC_ANIMATION | ND_KEYFRAME | NA_EDITED, NULL); + + /* send updates */ + DAG_id_tag_update(&cu->id, 0); +} + +#undef MIN_TIME_DELTA + +#define GAP_DFAC 0.01f +#define WIDTH_CORR_FAC 0.1f +#define BEZT_HANDLE_FAC 0.3f + +/* convert stroke to 3d path */ + +/* helper */ +static void gp_stroke_to_path_add_point(tGpTimingData *gtd, BPoint *bp, const float p[3], const float prev_p[3], + const bool do_gtd, const double inittime, const float time, + const float width, const float rad_fac, float minmax_weights[2]) +{ + copy_v3_v3(bp->vec, p); + bp->vec[3] = 1.0f; + + /* set settings */ + bp->f1 = SELECT; + bp->radius = width * rad_fac; + bp->weight = width; + CLAMP(bp->weight, 0.0f, 1.0f); + if (bp->weight < minmax_weights[0]) { + minmax_weights[0] = bp->weight; + } + else if (bp->weight > minmax_weights[1]) { + minmax_weights[1] = bp->weight; + } + + /* Update timing data */ + if (do_gtd) { + gp_timing_data_add_point(gtd, inittime, time, len_v3v3(prev_p, p)); + } +} + +static void gp_stroke_to_path(bContext *C, bGPDlayer *gpl, bGPDstroke *gps, Curve *cu, rctf *subrect, Nurb **curnu, + float minmax_weights[2], const float rad_fac, bool stitch, const bool add_start_point, + const bool add_end_point, tGpTimingData *gtd) +{ + bGPDspoint *pt; + Nurb *nu = (curnu) ? *curnu : NULL; + BPoint *bp, *prev_bp = NULL; + const bool do_gtd = (gtd->mode != GP_STROKECONVERT_TIMING_NONE); + const int add_start_end_points = (add_start_point ? 1 : 0) + (add_end_point ? 1 : 0); + int i, old_nbp = 0; + + /* create new 'nurb' or extend current one within the curve */ + if (nu) { + old_nbp = nu->pntsu; + + /* If stitch, the first point of this stroke is already present in current nu. + * Else, we have to add two additional points to make the zero-radius link between strokes. + */ + BKE_nurb_points_add(nu, gps->totpoints + (stitch ? -1 : 2) + add_start_end_points); + } + else { + nu = (Nurb *)MEM_callocN(sizeof(Nurb), "gpstroke_to_path(nurb)"); + + nu->pntsu = gps->totpoints + add_start_end_points; + nu->pntsv = 1; + nu->orderu = 2; /* point-to-point! */ + nu->type = CU_NURBS; + nu->flagu = CU_NURB_ENDPOINT; + nu->resolu = cu->resolu; + nu->resolv = cu->resolv; + nu->knotsu = NULL; + + nu->bp = (BPoint *)MEM_callocN(sizeof(BPoint) * nu->pntsu, "bpoints"); + + stitch = false; /* Security! */ + } + + if (do_gtd) { + gp_timing_data_set_nbr(gtd, nu->pntsu); + } + + /* If needed, make the link between both strokes with two zero-radius additional points */ + /* About "zero-radius" point interpolations: + * - If we have at least two points in current curve (most common case), we linearly extrapolate + * the last segment to get the first point (p1) position and timing. + * - If we do not have those (quite odd, but may happen), we linearly interpolate the last point + * with the first point of the current stroke. + * The same goes for the second point, first segment of the current stroke is "negatively" extrapolated + * if it exists, else (if the stroke is a single point), linear interpolation with last curve point... + */ + if (curnu && !stitch && old_nbp) { + float p1[3], p2[3], p[3], next_p[3]; + float dt1 = 0.0f, dt2 = 0.0f; + + BLI_assert(gps->prev != NULL); + + prev_bp = NULL; + if ((old_nbp > 1) && (gps->prev->totpoints > 1)) { + /* Only use last curve segment if previous stroke was not a single-point one! */ + prev_bp = &nu->bp[old_nbp - 2]; + } + bp = &nu->bp[old_nbp - 1]; + + /* First point */ + gp_strokepoint_convertcoords(C, gps, gps->points, p, subrect); + if (prev_bp) { + interp_v3_v3v3(p1, bp->vec, prev_bp->vec, -GAP_DFAC); + if (do_gtd) { + const int idx = gps->prev->totpoints - 1; + dt1 = interpf(gps->prev->points[idx - 1].time, gps->prev->points[idx].time, -GAP_DFAC); + } + } + else { + interp_v3_v3v3(p1, bp->vec, p, GAP_DFAC); + if (do_gtd) { + dt1 = interpf(gps->inittime - gps->prev->inittime, 0.0f, GAP_DFAC); + } + } + bp++; + gp_stroke_to_path_add_point(gtd, bp, p1, (bp - 1)->vec, do_gtd, gps->prev->inittime, dt1, + 0.0f, rad_fac, minmax_weights); + + /* Second point */ + /* Note dt2 is always negative, which marks the gap. */ + if (gps->totpoints > 1) { + gp_strokepoint_convertcoords(C, gps, gps->points + 1, next_p, subrect); + interp_v3_v3v3(p2, p, next_p, -GAP_DFAC); + if (do_gtd) { + dt2 = interpf(gps->points[1].time, gps->points[0].time, -GAP_DFAC); + } + } + else { + interp_v3_v3v3(p2, p, bp->vec, GAP_DFAC); + if (do_gtd) { + dt2 = interpf(gps->prev->inittime - gps->inittime, 0.0f, GAP_DFAC); + } + } + bp++; + gp_stroke_to_path_add_point(gtd, bp, p2, p1, do_gtd, gps->inittime, dt2, 0.0f, rad_fac, minmax_weights); + + old_nbp += 2; + } + else if (add_start_point) { + float p[3], next_p[3]; + float dt = 0.0f; + + gp_strokepoint_convertcoords(C, gps, gps->points, p, subrect); + if (gps->totpoints > 1) { + gp_strokepoint_convertcoords(C, gps, gps->points + 1, next_p, subrect); + interp_v3_v3v3(p, p, next_p, -GAP_DFAC); + if (do_gtd) { + dt = interpf(gps->points[1].time, gps->points[0].time, -GAP_DFAC); + } + } + else { + p[0] -= GAP_DFAC; /* Rather arbitrary... */ + dt = -GAP_DFAC; /* Rather arbitrary too! */ + } + bp = &nu->bp[old_nbp]; + /* Note we can't give anything else than 0.0 as time here, since a negative one (which would be expected value) + * would not work (it would be *before* gtd->inittime, which is not supported currently). + */ + gp_stroke_to_path_add_point(gtd, bp, p, p, do_gtd, gps->inittime, dt, 0.0f, rad_fac, minmax_weights); + + old_nbp++; + } + + if (old_nbp) { + prev_bp = &nu->bp[old_nbp - 1]; + } + + /* add points */ + for (i = (stitch) ? 1 : 0, pt = &gps->points[(stitch) ? 1 : 0], bp = &nu->bp[old_nbp]; + i < gps->totpoints; + i++, pt++, bp++) + { + float p[3]; + float width = pt->pressure * gpl->thickness * WIDTH_CORR_FAC; + + /* get coordinates to add at */ + gp_strokepoint_convertcoords(C, gps, pt, p, subrect); + + gp_stroke_to_path_add_point(gtd, bp, p, (prev_bp) ? prev_bp->vec : p, do_gtd, gps->inittime, pt->time, + width, rad_fac, minmax_weights); + + prev_bp = bp; + } + + if (add_end_point) { + float p[3]; + float dt = 0.0f; + + if (gps->totpoints > 1) { + interp_v3_v3v3(p, prev_bp->vec, (prev_bp - 1)->vec, -GAP_DFAC); + if (do_gtd) { + const int idx = gps->totpoints - 1; + dt = interpf(gps->points[idx - 1].time, gps->points[idx].time, -GAP_DFAC); + } + } + else { + copy_v3_v3(p, prev_bp->vec); + p[0] += GAP_DFAC; /* Rather arbitrary... */ + dt = GAP_DFAC; /* Rather arbitrary too! */ + } + /* Note bp has already been incremented in main loop above, so it points to the right place. */ + gp_stroke_to_path_add_point(gtd, bp, p, prev_bp->vec, do_gtd, gps->inittime, dt, 0.0f, rad_fac, minmax_weights); + } + + /* add nurb to curve */ + if (!curnu || !*curnu) { + BLI_addtail(&cu->nurb, nu); + } + if (curnu) { + *curnu = nu; + } + + BKE_nurb_knot_calc_u(nu); +} + +/* convert stroke to 3d bezier */ + +/* helper */ +static void gp_stroke_to_bezier_add_point(tGpTimingData *gtd, BezTriple *bezt, + const float p[3], const float h1[3], const float h2[3], const float prev_p[3], + const bool do_gtd, const double inittime, const float time, + const float width, const float rad_fac, float minmax_weights[2]) +{ + copy_v3_v3(bezt->vec[0], h1); + copy_v3_v3(bezt->vec[1], p); + copy_v3_v3(bezt->vec[2], h2); + + /* set settings */ + bezt->h1 = bezt->h2 = HD_FREE; + bezt->f1 = bezt->f2 = bezt->f3 = SELECT; + bezt->radius = width * rad_fac; + bezt->weight = width; + CLAMP(bezt->weight, 0.0f, 1.0f); + if (bezt->weight < minmax_weights[0]) { + minmax_weights[0] = bezt->weight; + } + else if (bezt->weight > minmax_weights[1]) { + minmax_weights[1] = bezt->weight; + } + + /* Update timing data */ + if (do_gtd) { + gp_timing_data_add_point(gtd, inittime, time, len_v3v3(prev_p, p)); + } +} + +static void gp_stroke_to_bezier(bContext *C, bGPDlayer *gpl, bGPDstroke *gps, Curve *cu, rctf *subrect, Nurb **curnu, + float minmax_weights[2], const float rad_fac, bool stitch, const bool add_start_point, + const bool add_end_point, tGpTimingData *gtd) +{ + bGPDspoint *pt; + Nurb *nu = (curnu) ? *curnu : NULL; + BezTriple *bezt, *prev_bezt = NULL; + int i, tot, old_nbezt = 0; + const int add_start_end_points = (add_start_point ? 1 : 0) + (add_end_point ? 1 : 0); + float p3d_cur[3], p3d_prev[3], p3d_next[3], h1[3], h2[3]; + const bool do_gtd = (gtd->mode != GP_STROKECONVERT_TIMING_NONE); + + /* create new 'nurb' or extend current one within the curve */ + if (nu) { + old_nbezt = nu->pntsu; + /* If we do stitch, first point of current stroke is assumed the same as last point of previous stroke, + * so no need to add it. + * If no stitch, we want to add two additional points to make a "zero-radius" link between both strokes. + */ + BKE_nurb_bezierPoints_add(nu, gps->totpoints + ((stitch) ? -1 : 2) + add_start_end_points); + } + else { + nu = (Nurb *)MEM_callocN(sizeof(Nurb), "gpstroke_to_bezier(nurb)"); + + nu->pntsu = gps->totpoints + add_start_end_points; + nu->resolu = 12; + nu->resolv = 12; + nu->type = CU_BEZIER; + nu->bezt = (BezTriple *)MEM_callocN(sizeof(BezTriple) * nu->pntsu, "bezts"); + + stitch = false; /* Security! */ + } + + if (do_gtd) { + gp_timing_data_set_nbr(gtd, nu->pntsu); + } + + tot = gps->totpoints; + + /* get initial coordinates */ + pt = gps->points; + if (tot) { + gp_strokepoint_convertcoords(C, gps, pt, (stitch) ? p3d_prev : p3d_cur, subrect); + if (tot > 1) { + gp_strokepoint_convertcoords(C, gps, pt + 1, (stitch) ? p3d_cur : p3d_next, subrect); + } + if (stitch && tot > 2) { + gp_strokepoint_convertcoords(C, gps, pt + 2, p3d_next, subrect); + } + } + + /* If needed, make the link between both strokes with two zero-radius additional points */ + if (curnu && old_nbezt) { + BLI_assert(gps->prev != NULL); + + /* Update last point's second handle */ + if (stitch) { + bezt = &nu->bezt[old_nbezt - 1]; + interp_v3_v3v3(h2, bezt->vec[1], p3d_cur, BEZT_HANDLE_FAC); + copy_v3_v3(bezt->vec[2], h2); + pt++; + } + + /* Create "link points" */ + /* About "zero-radius" point interpolations: + * - If we have at least two points in current curve (most common case), we linearly extrapolate + * the last segment to get the first point (p1) position and timing. + * - If we do not have those (quite odd, but may happen), we linearly interpolate the last point + * with the first point of the current stroke. + * The same goes for the second point, first segment of the current stroke is "negatively" extrapolated + * if it exists, else (if the stroke is a single point), linear interpolation with last curve point... + */ + else { + float p1[3], p2[3]; + float dt1 = 0.0f, dt2 = 0.0f; + + prev_bezt = NULL; + if ((old_nbezt > 1) && (gps->prev->totpoints > 1)) { + /* Only use last curve segment if previous stroke was not a single-point one! */ + prev_bezt = &nu->bezt[old_nbezt - 2]; + } + bezt = &nu->bezt[old_nbezt - 1]; + + /* First point */ + if (prev_bezt) { + interp_v3_v3v3(p1, prev_bezt->vec[1], bezt->vec[1], 1.0f + GAP_DFAC); + if (do_gtd) { + const int idx = gps->prev->totpoints - 1; + dt1 = interpf(gps->prev->points[idx - 1].time, gps->prev->points[idx].time, -GAP_DFAC); + } + } + else { + interp_v3_v3v3(p1, bezt->vec[1], p3d_cur, GAP_DFAC); + if (do_gtd) { + dt1 = interpf(gps->inittime - gps->prev->inittime, 0.0f, GAP_DFAC); + } + } + + /* Second point */ + /* Note dt2 is always negative, which marks the gap. */ + if (tot > 1) { + interp_v3_v3v3(p2, p3d_cur, p3d_next, -GAP_DFAC); + if (do_gtd) { + dt2 = interpf(gps->points[1].time, gps->points[0].time, -GAP_DFAC); + } + } + else { + interp_v3_v3v3(p2, p3d_cur, bezt->vec[1], GAP_DFAC); + if (do_gtd) { + dt2 = interpf(gps->prev->inittime - gps->inittime, 0.0f, GAP_DFAC); + } + } + + /* Second handle of last point of previous stroke. */ + interp_v3_v3v3(h2, bezt->vec[1], p1, BEZT_HANDLE_FAC); + copy_v3_v3(bezt->vec[2], h2); + + /* First point */ + interp_v3_v3v3(h1, p1, bezt->vec[1], BEZT_HANDLE_FAC); + interp_v3_v3v3(h2, p1, p2, BEZT_HANDLE_FAC); + bezt++; + gp_stroke_to_bezier_add_point(gtd, bezt, p1, h1, h2, (bezt - 1)->vec[1], do_gtd, gps->prev->inittime, dt1, + 0.0f, rad_fac, minmax_weights); + + /* Second point */ + interp_v3_v3v3(h1, p2, p1, BEZT_HANDLE_FAC); + interp_v3_v3v3(h2, p2, p3d_cur, BEZT_HANDLE_FAC); + bezt++; + gp_stroke_to_bezier_add_point(gtd, bezt, p2, h1, h2, p1, do_gtd, gps->inittime, dt2, + 0.0f, rad_fac, minmax_weights); + + old_nbezt += 2; + copy_v3_v3(p3d_prev, p2); + } + } + else if (add_start_point) { + float p[3]; + float dt = 0.0f; + + if (gps->totpoints > 1) { + interp_v3_v3v3(p, p3d_cur, p3d_next, -GAP_DFAC); + if (do_gtd) { + dt = interpf(gps->points[1].time, gps->points[0].time, -GAP_DFAC); + } + } + else { + copy_v3_v3(p, p3d_cur); + p[0] -= GAP_DFAC; /* Rather arbitrary... */ + dt = -GAP_DFAC; /* Rather arbitrary too! */ + } + interp_v3_v3v3(h1, p, p3d_cur, -BEZT_HANDLE_FAC); + interp_v3_v3v3(h2, p, p3d_cur, BEZT_HANDLE_FAC); + bezt = &nu->bezt[old_nbezt]; + gp_stroke_to_bezier_add_point(gtd, bezt, p, h1, h2, p, do_gtd, gps->inittime, dt, + 0.0f, rad_fac, minmax_weights); + + old_nbezt++; + copy_v3_v3(p3d_prev, p); + } + + if (old_nbezt) { + prev_bezt = &nu->bezt[old_nbezt - 1]; + } + + /* add points */ + for (i = stitch ? 1 : 0, bezt = &nu->bezt[old_nbezt]; i < tot; i++, pt++, bezt++) { + float width = pt->pressure * gpl->thickness * WIDTH_CORR_FAC; + + if (i || old_nbezt) { + interp_v3_v3v3(h1, p3d_cur, p3d_prev, BEZT_HANDLE_FAC); + } + else { + interp_v3_v3v3(h1, p3d_cur, p3d_next, -BEZT_HANDLE_FAC); + } + + if (i < tot - 1) { + interp_v3_v3v3(h2, p3d_cur, p3d_next, BEZT_HANDLE_FAC); + } + else { + interp_v3_v3v3(h2, p3d_cur, p3d_prev, -BEZT_HANDLE_FAC); + } + + gp_stroke_to_bezier_add_point(gtd, bezt, p3d_cur, h1, h2, prev_bezt ? prev_bezt->vec[1] : p3d_cur, + do_gtd, gps->inittime, pt->time, width, rad_fac, minmax_weights); + + /* shift coord vects */ + copy_v3_v3(p3d_prev, p3d_cur); + copy_v3_v3(p3d_cur, p3d_next); + + if (i + 2 < tot) { + gp_strokepoint_convertcoords(C, gps, pt + 2, p3d_next, subrect); + } + + prev_bezt = bezt; + } + + if (add_end_point) { + float p[3]; + float dt = 0.0f; + + if (gps->totpoints > 1) { + interp_v3_v3v3(p, prev_bezt->vec[1], (prev_bezt - 1)->vec[1], -GAP_DFAC); + if (do_gtd) { + const int idx = gps->totpoints - 1; + dt = interpf(gps->points[idx - 1].time, gps->points[idx].time, -GAP_DFAC); + } + } + else { + copy_v3_v3(p, prev_bezt->vec[1]); + p[0] += GAP_DFAC; /* Rather arbitrary... */ + dt = GAP_DFAC; /* Rather arbitrary too! */ + } + + /* Second handle of last point of this stroke. */ + interp_v3_v3v3(h2, prev_bezt->vec[1], p, BEZT_HANDLE_FAC); + copy_v3_v3(prev_bezt->vec[2], h2); + + /* The end point */ + interp_v3_v3v3(h1, p, prev_bezt->vec[1], BEZT_HANDLE_FAC); + interp_v3_v3v3(h2, p, prev_bezt->vec[1], -BEZT_HANDLE_FAC); + /* Note bezt has already been incremented in main loop above, so it points to the right place. */ + gp_stroke_to_bezier_add_point(gtd, bezt, p, h1, h2, prev_bezt->vec[1], do_gtd, gps->inittime, dt, + 0.0f, rad_fac, minmax_weights); + } + + /* must calculate handles or else we crash */ + BKE_nurb_handles_calc(nu); + + if (!curnu || !*curnu) { + BLI_addtail(&cu->nurb, nu); + } + if (curnu) { + *curnu = nu; + } +} + +#undef GAP_DFAC +#undef WIDTH_CORR_FAC +#undef BEZT_HANDLE_FAC + +static void gp_stroke_finalize_curve_endpoints(Curve *cu) +{ + /* start */ + Nurb *nu = cu->nurb.first; + int i = 0; + if (nu->bezt) { + BezTriple *bezt = nu->bezt; + if (bezt) { + bezt[i].weight = bezt[i].radius = 0.0f; + } + } + else if (nu->bp) { + BPoint *bp = nu->bp; + if (bp) { + bp[i].weight = bp[i].radius = 0.0f; + } + } + + /* end */ + nu = cu->nurb.last; + i = nu->pntsu - 1; + if (nu->bezt) { + BezTriple *bezt = nu->bezt; + if (bezt) { + bezt[i].weight = bezt[i].radius = 0.0f; + } + } + else if (nu->bp) { + BPoint *bp = nu->bp; + if (bp) { + bp[i].weight = bp[i].radius = 0.0f; + } + } +} + +static void gp_stroke_norm_curve_weights(Curve *cu, const float minmax_weights[2]) +{ + Nurb *nu; + const float delta = minmax_weights[0]; + float fac; + int i; + + /* when delta == minmax_weights[0] == minmax_weights[1], we get div by zero [#35686] */ + if (IS_EQF(delta, minmax_weights[1])) + fac = 1.0f; + else + fac = 1.0f / (minmax_weights[1] - delta); + + for (nu = cu->nurb.first; nu; nu = nu->next) { + if (nu->bezt) { + BezTriple *bezt = nu->bezt; + for (i = 0; i < nu->pntsu; i++, bezt++) { + bezt->weight = (bezt->weight - delta) * fac; + } + } + else if (nu->bp) { + BPoint *bp = nu->bp; + for (i = 0; i < nu->pntsu; i++, bp++) { + bp->weight = (bp->weight - delta) * fac; + } + } + } +} + +static int gp_camera_view_subrect(bContext *C, rctf *subrect) +{ + View3D *v3d = CTX_wm_view3d(C); + ARegion *ar = CTX_wm_region(C); + + if (v3d) { + RegionView3D *rv3d = ar->regiondata; + + /* for camera view set the subrect */ + if (rv3d->persp == RV3D_CAMOB) { + Scene *scene = CTX_data_scene(C); + ED_view3d_calc_camera_border(scene, ar, v3d, rv3d, subrect, true); /* no shift */ + return 1; + } + } + + return 0; +} + +/* convert a given grease-pencil layer to a 3d-curve representation (using current view if appropriate) */ +static void gp_layer_to_curve(bContext *C, ReportList *reports, bGPdata *gpd, bGPDlayer *gpl, const int mode, + const bool norm_weights, const float rad_fac, const bool link_strokes, tGpTimingData *gtd) +{ + struct Main *bmain = CTX_data_main(C); + View3D *v3d = CTX_wm_view3d(C); /* may be NULL */ + Scene *scene = CTX_data_scene(C); + bGPDframe *gpf = gpencil_layer_getframe(gpl, CFRA, 0); + bGPDstroke *gps, *prev_gps = NULL; + Object *ob; + Curve *cu; + Nurb *nu = NULL; + Base *base_orig = BASACT, *base_new = NULL; + float minmax_weights[2] = {1.0f, 0.0f}; + + /* camera framing */ + rctf subrect, *subrect_ptr = NULL; + + /* error checking */ + if (ELEM(NULL, gpd, gpl, gpf)) + return; + + /* only convert if there are any strokes on this layer's frame to convert */ + if (BLI_listbase_is_empty(&gpf->strokes)) + return; + + /* initialize camera framing */ + if (gp_camera_view_subrect(C, &subrect)) { + subrect_ptr = &subrect; + } + + /* init the curve object (remove rotation and get curve data from it) + * - must clear transforms set on object, as those skew our results + */ + ob = BKE_object_add_only_object(bmain, OB_CURVE, gpl->info); + cu = ob->data = BKE_curve_add(bmain, gpl->info, OB_CURVE); + base_new = BKE_scene_base_add(scene, ob); + + cu->flag |= CU_3D; + + gtd->inittime = ((bGPDstroke *)gpf->strokes.first)->inittime; + + /* add points to curve */ + for (gps = gpf->strokes.first; gps; gps = gps->next) { + const bool add_start_point = (link_strokes && !(prev_gps)); + const bool add_end_point = (link_strokes && !(gps->next)); + + /* Detect new strokes created because of GP_STROKE_BUFFER_MAX reached, and stitch them to previous one. */ + bool stitch = false; + if (prev_gps) { + bGPDspoint *pt1 = &prev_gps->points[prev_gps->totpoints - 1]; + bGPDspoint *pt2 = &gps->points[0]; + + if ((pt1->x == pt2->x) && (pt1->y == pt2->y)) { + stitch = true; + } + } + + /* Decide whether we connect this stroke to previous one */ + if (!(stitch || link_strokes)) { + nu = NULL; + } + + switch (mode) { + case GP_STROKECONVERT_PATH: + gp_stroke_to_path(C, gpl, gps, cu, subrect_ptr, &nu, minmax_weights, rad_fac, stitch, + add_start_point, add_end_point, gtd); + break; + case GP_STROKECONVERT_CURVE: + case GP_STROKECONVERT_POLY: /* convert after */ + gp_stroke_to_bezier(C, gpl, gps, cu, subrect_ptr, &nu, minmax_weights, rad_fac, stitch, + add_start_point, add_end_point, gtd); + break; + default: + BLI_assert(!"invalid mode"); + break; + } + prev_gps = gps; + } + + /* If link_strokes, be sure first and last points have a zero weight/size! */ + if (link_strokes) { + gp_stroke_finalize_curve_endpoints(cu); + } + + /* Update curve's weights, if needed */ + if (norm_weights && ((minmax_weights[0] > 0.0f) || (minmax_weights[1] < 1.0f))) { + gp_stroke_norm_curve_weights(cu, minmax_weights); + } + + /* Create the path animation, if needed */ + gp_stroke_path_animation(C, reports, cu, gtd); + + if (mode == GP_STROKECONVERT_POLY) { + for (nu = cu->nurb.first; nu; nu = nu->next) { + BKE_nurb_type_convert(nu, CU_POLY, false); + } + } + + /* set the layer and select */ + base_new->lay = ob->lay = base_orig ? base_orig->lay : BKE_screen_view3d_layer_active(v3d, scene); + base_new->flag = ob->flag = base_new->flag | SELECT; +} + +/* --- */ + +/* Check a GP layer has valid timing data! Else, most timing options are hidden in the operator. + * op may be NULL. + */ +static bool gp_convert_check_has_valid_timing(bContext *C, bGPDlayer *gpl, wmOperator *op) +{ + Scene *scene = CTX_data_scene(C); + bGPDframe *gpf = NULL; + bGPDstroke *gps = NULL; + bGPDspoint *pt; + double base_time, cur_time, prev_time = -1.0; + int i; + bool valid = true; + + if (!gpl || !(gpf = gpencil_layer_getframe(gpl, CFRA, 0)) || !(gps = gpf->strokes.first)) + return false; + + do { + base_time = cur_time = gps->inittime; + if (cur_time <= prev_time) { + valid = false; + break; + } + + prev_time = cur_time; + for (i = 0, pt = gps->points; i < gps->totpoints; i++, pt++) { + cur_time = base_time + (double)pt->time; + /* First point of a stroke should have the same time as stroke's inittime, + * so it's the only case where equality is allowed! + */ + if ((i && cur_time <= prev_time) || (cur_time < prev_time)) { + valid = false; + break; + } + prev_time = cur_time; + } + + if (!valid) { + break; + } + } while ((gps = gps->next)); + + if (op) { + RNA_boolean_set(op->ptr, "use_timing_data", valid); + } + return valid; +} + +/* Check end_frame is always > start frame! */ +static void gp_convert_set_end_frame(struct Main *UNUSED(main), struct Scene *UNUSED(scene), struct PointerRNA *ptr) +{ + int start_frame = RNA_int_get(ptr, "start_frame"); + int end_frame = RNA_int_get(ptr, "end_frame"); + + if (end_frame <= start_frame) { + RNA_int_set(ptr, "end_frame", start_frame + 1); + } +} + +static int gp_convert_poll(bContext *C) +{ + bGPdata *gpd = ED_gpencil_data_get_active(C); + bGPDlayer *gpl = NULL; + bGPDframe *gpf = NULL; + ScrArea *sa = CTX_wm_area(C); + Scene *scene = CTX_data_scene(C); + + /* only if the current view is 3D View, if there's valid data (i.e. at least one stroke!), + * and if we are not in edit mode! + */ + return ((sa && sa->spacetype == SPACE_VIEW3D) && + (gpl = gpencil_layer_getactive(gpd)) && + (gpf = gpencil_layer_getframe(gpl, CFRA, 0)) && + (gpf->strokes.first) && + (scene->obedit == NULL)); +} + +static int gp_convert_layer_exec(bContext *C, wmOperator *op) +{ + PropertyRNA *prop = RNA_struct_find_property(op->ptr, "use_timing_data"); + bGPdata *gpd = ED_gpencil_data_get_active(C); + bGPDlayer *gpl = gpencil_layer_getactive(gpd); + Scene *scene = CTX_data_scene(C); + const int mode = RNA_enum_get(op->ptr, "type"); + const bool norm_weights = RNA_boolean_get(op->ptr, "use_normalize_weights"); + const float rad_fac = RNA_float_get(op->ptr, "radius_multiplier"); + const bool link_strokes = RNA_boolean_get(op->ptr, "use_link_strokes"); + bool valid_timing; + tGpTimingData gtd; + + /* check if there's data to work with */ + if (gpd == NULL) { + BKE_report(op->reports, RPT_ERROR, "No Grease Pencil data to work on"); + return OPERATOR_CANCELLED; + } + + if (!RNA_property_is_set(op->ptr, prop) && !gp_convert_check_has_valid_timing(C, gpl, op)) { + BKE_report(op->reports, RPT_WARNING, + "Current Grease Pencil strokes have no valid timing data, most timing options will be hidden!"); + } + valid_timing = RNA_property_boolean_get(op->ptr, prop); + + gtd.mode = RNA_enum_get(op->ptr, "timing_mode"); + /* Check for illegal timing mode! */ + if (!valid_timing && !ELEM(gtd.mode, GP_STROKECONVERT_TIMING_NONE, GP_STROKECONVERT_TIMING_LINEAR)) { + gtd.mode = GP_STROKECONVERT_TIMING_LINEAR; + RNA_enum_set(op->ptr, "timing_mode", gtd.mode); + } + if (!link_strokes) { + gtd.mode = GP_STROKECONVERT_TIMING_NONE; + } + + /* grab all relevant settings */ + gtd.frame_range = RNA_int_get(op->ptr, "frame_range"); + gtd.start_frame = RNA_int_get(op->ptr, "start_frame"); + gtd.realtime = valid_timing ? RNA_boolean_get(op->ptr, "use_realtime") : false; + gtd.end_frame = RNA_int_get(op->ptr, "end_frame"); + gtd.gap_duration = RNA_float_get(op->ptr, "gap_duration"); + gtd.gap_randomness = RNA_float_get(op->ptr, "gap_randomness"); + gtd.gap_randomness = min_ff(gtd.gap_randomness, gtd.gap_duration); + gtd.seed = RNA_int_get(op->ptr, "seed"); + gtd.num_points = gtd.cur_point = 0; + gtd.dists = gtd.times = NULL; + gtd.tot_dist = gtd.tot_time = gtd.gap_tot_time = 0.0f; + gtd.inittime = 0.0; + gtd.offset_time = 0.0f; + + /* perform conversion */ + gp_layer_to_curve(C, op->reports, gpd, gpl, mode, norm_weights, rad_fac, link_strokes, >d); + + /* free temp memory */ + if (gtd.dists) { + MEM_freeN(gtd.dists); + gtd.dists = NULL; + } + if (gtd.times) { + MEM_freeN(gtd.times); + gtd.times = NULL; + } + + /* notifiers */ + WM_event_add_notifier(C, NC_OBJECT | NA_ADDED, NULL); + WM_event_add_notifier(C, NC_SCENE | ND_OB_ACTIVE, scene); + + /* done */ + return OPERATOR_FINISHED; +} + +static bool gp_convert_draw_check_prop(PointerRNA *ptr, PropertyRNA *prop) +{ + const char *prop_id = RNA_property_identifier(prop); + const bool link_strokes = RNA_boolean_get(ptr, "use_link_strokes"); + int timing_mode = RNA_enum_get(ptr, "timing_mode"); + bool realtime = RNA_boolean_get(ptr, "use_realtime"); + float gap_duration = RNA_float_get(ptr, "gap_duration"); + float gap_randomness = RNA_float_get(ptr, "gap_randomness"); + const bool valid_timing = RNA_boolean_get(ptr, "use_timing_data"); + + /* Always show those props */ + if (STREQ(prop_id, "type") || + STREQ(prop_id, "use_normalize_weights") || + STREQ(prop_id, "radius_multiplier") || + STREQ(prop_id, "use_link_strokes")) + { + return true; + } + + /* Never show this prop */ + if (STREQ(prop_id, "use_timing_data")) + return false; + + if (link_strokes) { + /* Only show when link_stroke is true */ + if (STREQ(prop_id, "timing_mode")) + return true; + + if (timing_mode != GP_STROKECONVERT_TIMING_NONE) { + /* Only show when link_stroke is true and stroke timing is enabled */ + if (STREQ(prop_id, "frame_range") || + STREQ(prop_id, "start_frame")) + { + return true; + } + + /* Only show if we have valid timing data! */ + if (valid_timing && STREQ(prop_id, "use_realtime")) + return true; + + /* Only show if realtime or valid_timing is false! */ + if ((!realtime || !valid_timing) && STREQ(prop_id, "end_frame")) + return true; + + if (valid_timing && timing_mode == GP_STROKECONVERT_TIMING_CUSTOMGAP) { + /* Only show for custom gaps! */ + if (STREQ(prop_id, "gap_duration")) + return true; + + /* Only show randomness for non-null custom gaps! */ + if (STREQ(prop_id, "gap_randomness") && (gap_duration > 0.0f)) + return true; + + /* Only show seed for randomize action! */ + if (STREQ(prop_id, "seed") && (gap_duration > 0.0f) && (gap_randomness > 0.0f)) + return true; + } + } + } + + /* Else, hidden! */ + return false; +} + +static void gp_convert_ui(bContext *C, wmOperator *op) +{ + uiLayout *layout = op->layout; + wmWindowManager *wm = CTX_wm_manager(C); + PointerRNA ptr; + + RNA_pointer_create(&wm->id, op->type->srna, op->properties, &ptr); + + /* Main auto-draw call */ + uiDefAutoButsRNA(layout, &ptr, gp_convert_draw_check_prop, '\0'); +} + +void GPENCIL_OT_convert(wmOperatorType *ot) +{ + PropertyRNA *prop; + + /* identifiers */ + ot->name = "Convert Grease Pencil"; + ot->idname = "GPENCIL_OT_convert"; + ot->description = "Convert the active Grease Pencil layer to a new Curve Object"; + + /* callbacks */ + ot->invoke = WM_menu_invoke; + ot->exec = gp_convert_layer_exec; + ot->poll = gp_convert_poll; + ot->ui = gp_convert_ui; + + /* flags */ + ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO; + + /* properties */ + ot->prop = RNA_def_enum(ot->srna, "type", prop_gpencil_convertmodes, 0, "Type", "Which type of curve to convert to"); + + RNA_def_boolean(ot->srna, "use_normalize_weights", true, "Normalize Weight", + "Normalize weight (set from stroke width)"); + RNA_def_float(ot->srna, "radius_multiplier", 1.0f, 0.0f, 1000.0f, "Radius Fac", + "Multiplier for the points' radii (set from stroke width)", 0.0f, 10.0f); + RNA_def_boolean(ot->srna, "use_link_strokes", true, "Link Strokes", + "Whether to link strokes with zero-radius sections of curves"); + + prop = RNA_def_enum(ot->srna, "timing_mode", prop_gpencil_convert_timingmodes, GP_STROKECONVERT_TIMING_FULL, + "Timing Mode", "How to use timing data stored in strokes"); + RNA_def_enum_funcs(prop, rna_GPConvert_mode_items); + + RNA_def_int(ot->srna, "frame_range", 100, 1, 10000, "Frame Range", + "The duration of evaluation of the path control curve", 1, 1000); + RNA_def_int(ot->srna, "start_frame", 1, 1, 100000, "Start Frame", + "The start frame of the path control curve", 1, 100000); + RNA_def_boolean(ot->srna, "use_realtime", false, "Realtime", + "Whether the path control curve reproduces the drawing in realtime, starting from Start Frame"); + prop = RNA_def_int(ot->srna, "end_frame", 250, 1, 100000, "End Frame", + "The end frame of the path control curve (if Realtime is not set)", 1, 100000); + RNA_def_property_update_runtime(prop, gp_convert_set_end_frame); + + RNA_def_float(ot->srna, "gap_duration", 0.0f, 0.0f, 10000.0f, "Gap Duration", + "Custom Gap mode: (Average) length of gaps, in frames " + "(Note: Realtime value, will be scaled if Realtime is not set)", 0.0f, 1000.0f); + RNA_def_float(ot->srna, "gap_randomness", 0.0f, 0.0f, 10000.0f, "Gap Randomness", + "Custom Gap mode: Number of frames that gap lengths can vary", 0.0f, 1000.0f); + RNA_def_int(ot->srna, "seed", 0, 0, 1000, "Random Seed", + "Custom Gap mode: Random generator seed", 0, 100); + + /* Note: Internal use, this one will always be hidden by UI code... */ + prop = RNA_def_boolean(ot->srna, "use_timing_data", false, "Has Valid Timing", + "Whether the converted Grease Pencil layer has valid timing data (internal use)"); + RNA_def_property_flag(prop, PROP_SKIP_SAVE); +} + +/* ************************************************ */ diff --git a/source/blender/editors/gpencil/gpencil_data.c b/source/blender/editors/gpencil/gpencil_data.c new file mode 100644 index 00000000000..de966776645 --- /dev/null +++ b/source/blender/editors/gpencil/gpencil_data.c @@ -0,0 +1,447 @@ +/* + * ***** BEGIN GPL LICENSE BLOCK ***** + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + * + * The Original Code is Copyright (C) 2008, Blender Foundation, Joshua Leung + * This is a new part of Blender + * + * Contributor(s): Joshua Leung + * + * ***** END GPL LICENSE BLOCK ***** + * + * Operators for dealing with GP datablocks and layers + */ + +/** \file blender/editors/gpencil/gpencil_data.c + * \ingroup edgpencil + */ + + +#include <stdio.h> +#include <string.h> +#include <stdlib.h> +#include <stddef.h> +#include <math.h> + +#include "MEM_guardedalloc.h" + +#include "BLI_blenlib.h" +#include "BLI_utildefines.h" + +#include "BLT_translation.h" + +#include "DNA_scene_types.h" +#include "DNA_screen_types.h" +#include "DNA_space_types.h" +#include "DNA_view3d_types.h" +#include "DNA_gpencil_types.h" + +#include "BKE_context.h" +#include "BKE_global.h" +#include "BKE_gpencil.h" +#include "BKE_library.h" +#include "BKE_object.h" +#include "BKE_report.h" +#include "BKE_scene.h" +#include "BKE_screen.h" + +#include "UI_interface.h" + +#include "WM_api.h" +#include "WM_types.h" + +#include "RNA_access.h" +#include "RNA_define.h" + +#include "ED_gpencil.h" + +#include "gpencil_intern.h" + + +/* ************************************************ */ +/* Datablock Operators */ + +/* ******************* Add New Data ************************ */ + +/* add new datablock - wrapper around API */ +static int gp_data_add_exec(bContext *C, wmOperator *op) +{ + bGPdata **gpd_ptr = ED_gpencil_data_get_pointers(C, NULL); + + if (gpd_ptr == NULL) { + BKE_report(op->reports, RPT_ERROR, "Nowhere for grease pencil data to go"); + return OPERATOR_CANCELLED; + } + else { + /* decrement user count and add new datablock */ + bGPdata *gpd = (*gpd_ptr); + + id_us_min(&gpd->id); + *gpd_ptr = gpencil_data_addnew(DATA_("GPencil")); + } + + /* notifiers */ + WM_event_add_notifier(C, NC_GPENCIL | ND_DATA | NA_EDITED, NULL); + + return OPERATOR_FINISHED; +} + +void GPENCIL_OT_data_add(wmOperatorType *ot) +{ + /* identifiers */ + ot->name = "Grease Pencil Add New"; + ot->idname = "GPENCIL_OT_data_add"; + ot->description = "Add new Grease Pencil datablock"; + ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO; + + /* callbacks */ + ot->exec = gp_data_add_exec; + ot->poll = gp_add_poll; +} + +/* ******************* Unlink Data ************************ */ + +/* poll callback for adding data/layers - special */ +static int gp_data_unlink_poll(bContext *C) +{ + bGPdata **gpd_ptr = ED_gpencil_data_get_pointers(C, NULL); + + /* if we have access to some active data, make sure there's a datablock before enabling this */ + return (gpd_ptr && *gpd_ptr); +} + + +/* unlink datablock - wrapper around API */ +static int gp_data_unlink_exec(bContext *C, wmOperator *op) +{ + bGPdata **gpd_ptr = ED_gpencil_data_get_pointers(C, NULL); + + if (gpd_ptr == NULL) { + BKE_report(op->reports, RPT_ERROR, "Nowhere for grease pencil data to go"); + return OPERATOR_CANCELLED; + } + else { + /* just unlink datablock now, decreasing its user count */ + bGPdata *gpd = (*gpd_ptr); + + id_us_min(&gpd->id); + *gpd_ptr = NULL; + } + + /* notifiers */ + WM_event_add_notifier(C, NC_GPENCIL | ND_DATA | NA_EDITED, NULL); + + return OPERATOR_FINISHED; +} + +void GPENCIL_OT_data_unlink(wmOperatorType *ot) +{ + /* identifiers */ + ot->name = "Grease Pencil Unlink"; + ot->idname = "GPENCIL_OT_data_unlink"; + ot->description = "Unlink active Grease Pencil datablock"; + ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO; + + /* callbacks */ + ot->exec = gp_data_unlink_exec; + ot->poll = gp_data_unlink_poll; +} + + +/* ************************************************ */ +/* Layer Operators */ + +/* ******************* Add New Layer ************************ */ + +/* add new layer - wrapper around API */ +static int gp_layer_add_exec(bContext *C, wmOperator *op) +{ + bGPdata **gpd_ptr = ED_gpencil_data_get_pointers(C, NULL); + + /* if there's no existing Grease-Pencil data there, add some */ + if (gpd_ptr == NULL) { + BKE_report(op->reports, RPT_ERROR, "Nowhere for grease pencil data to go"); + return OPERATOR_CANCELLED; + } + if (*gpd_ptr == NULL) + *gpd_ptr = gpencil_data_addnew(DATA_("GPencil")); + + /* add new layer now */ + gpencil_layer_addnew(*gpd_ptr, DATA_("GP_Layer"), 1); + + /* notifiers */ + WM_event_add_notifier(C, NC_GPENCIL | ND_DATA | NA_EDITED, NULL); + + return OPERATOR_FINISHED; +} + +void GPENCIL_OT_layer_add(wmOperatorType *ot) +{ + /* identifiers */ + ot->name = "Add New Layer"; + ot->idname = "GPENCIL_OT_layer_add"; + ot->description = "Add new Grease Pencil layer for the active Grease Pencil datablock"; + + ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO; + + /* callbacks */ + ot->exec = gp_layer_add_exec; + ot->poll = gp_add_poll; +} + +/* ******************* Remove Active Layer ************************* */ + +static int gp_layer_remove_exec(bContext *C, wmOperator *op) +{ + bGPdata *gpd = ED_gpencil_data_get_active(C); + bGPDlayer *gpl = gpencil_layer_getactive(gpd); + + /* sanity checks */ + if (ELEM(NULL, gpd, gpl)) + return OPERATOR_CANCELLED; + + if (gpl->flag & GP_LAYER_LOCKED) { + BKE_report(op->reports, RPT_ERROR, "Cannot delete locked layers"); + return OPERATOR_CANCELLED; + } + + /* make the layer before this the new active layer + * - use the one after if this is the first + * - if this is the only layer, this naturally becomes NULL + */ + if (gpl->prev) + gpencil_layer_setactive(gpd, gpl->prev); + else + gpencil_layer_setactive(gpd, gpl->next); + + /* delete the layer now... */ + gpencil_layer_delete(gpd, gpl); + + /* notifiers */ + WM_event_add_notifier(C, NC_GPENCIL | ND_DATA | NA_EDITED, NULL); + + return OPERATOR_FINISHED; +} + +void GPENCIL_OT_layer_remove(wmOperatorType *ot) +{ + /* identifiers */ + ot->name = "Remove Layer"; + ot->idname = "GPENCIL_OT_layer_remove"; + ot->description = "Remove active Grease Pencil layer"; + + ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO; + + /* callbacks */ + ot->exec = gp_layer_remove_exec; + ot->poll = gp_active_layer_poll; +} + +/* ******************* Move Layer Up/Down ************************** */ + +enum { + GP_LAYER_MOVE_UP = -1, + GP_LAYER_MOVE_DOWN = 1 +}; + +static int gp_layer_move_exec(bContext *C, wmOperator *op) +{ + bGPdata *gpd = ED_gpencil_data_get_active(C); + bGPDlayer *gpl = gpencil_layer_getactive(gpd); + + int direction = RNA_enum_get(op->ptr, "type"); + + /* sanity checks */ + if (ELEM(NULL, gpd, gpl)) + return OPERATOR_CANCELLED; + + /* up or down? */ + if (direction == GP_LAYER_MOVE_UP) { + /* up */ + BLI_remlink(&gpd->layers, gpl); + BLI_insertlinkbefore(&gpd->layers, gpl->prev, gpl); + } + else { + /* down */ + BLI_remlink(&gpd->layers, gpl); + BLI_insertlinkafter(&gpd->layers, gpl->next, gpl); + } + + /* notifiers */ + WM_event_add_notifier(C, NC_GPENCIL | ND_DATA | NA_EDITED, NULL); + + return OPERATOR_FINISHED; +} + +void GPENCIL_OT_layer_move(wmOperatorType *ot) +{ + static EnumPropertyItem slot_move[] = { + {GP_LAYER_MOVE_UP, "UP", 0, "Up", ""}, + {GP_LAYER_MOVE_DOWN, "DOWN", 0, "Down", ""}, + {0, NULL, 0, NULL, NULL} + }; + + /* identifiers */ + ot->name = "Move Grease Pencil Layer"; + ot->idname = "GPENCIL_OT_layer_move"; + ot->description = "Move the active Grease Pencil layer up/down in the list"; + + /* api callbacks */ + ot->exec = gp_layer_move_exec; + ot->poll = gp_active_layer_poll; + + /* flags */ + ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO; + + ot->prop = RNA_def_enum(ot->srna, "type", slot_move, 0, "Type", ""); +} + +/* ********************* Duplicate Layer ************************** */ + +static int gp_layer_copy_exec(bContext *C, wmOperator *UNUSED(op)) +{ + bGPdata *gpd = ED_gpencil_data_get_active(C); + bGPDlayer *gpl = gpencil_layer_getactive(gpd); + bGPDlayer *new_layer; + + /* sanity checks */ + if (ELEM(NULL, gpd, gpl)) + return OPERATOR_CANCELLED; + + /* make copy of layer, and add it immediately after the existing layer */ + new_layer = gpencil_layer_duplicate(gpl); + BLI_insertlinkafter(&gpd->layers, gpl, new_layer); + + /* ensure new layer has a unique name, and is now the active layer */ + BLI_uniquename(&gpd->layers, new_layer, DATA_("GP_Layer"), '.', offsetof(bGPDlayer, info), sizeof(new_layer->info)); + gpencil_layer_setactive(gpd, new_layer); + + /* notifiers */ + WM_event_add_notifier(C, NC_GPENCIL | ND_DATA | NA_EDITED, NULL); + + return OPERATOR_FINISHED; +} + +void GPENCIL_OT_layer_duplicate(wmOperatorType *ot) +{ + /* identifiers */ + ot->name = "Duplicate Layer"; + ot->idname = "GPENCIL_OT_layer_duplicate"; + ot->description = "Make a copy of the active Grease Pencil layer"; + + /* callbacks */ + ot->exec = gp_layer_copy_exec; + ot->poll = gp_active_layer_poll; + + /* flags */ + ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO; +} + +/* *********************** Hide Layers ******************************** */ + +static int gp_hide_exec(bContext *C, wmOperator *op) +{ + bGPdata *gpd = ED_gpencil_data_get_active(C); + bGPDlayer *layer = gpencil_layer_getactive(gpd); + bool unselected = RNA_boolean_get(op->ptr, "unselected"); + + /* sanity checks */ + if (ELEM(NULL, gpd, layer)) + return OPERATOR_CANCELLED; + + if (unselected) { + bGPDlayer *gpl; + + /* hide unselected */ + for (gpl = gpd->layers.first; gpl; gpl = gpl->next) { + if (gpl != layer) { + gpl->flag |= GP_LAYER_HIDE; + } + } + } + else { + /* hide selected/active */ + layer->flag |= GP_LAYER_HIDE; + } + + /* notifiers */ + WM_event_add_notifier(C, NC_GPENCIL | ND_DATA | NA_EDITED, NULL); + + return OPERATOR_FINISHED; +} + +void GPENCIL_OT_hide(wmOperatorType *ot) +{ + /* identifiers */ + ot->name = "Hide Layer(s)"; + ot->idname = "GPENCIL_OT_hide"; + ot->description = "Hide selected/unselected Grease Pencil layers"; + + /* callbacks */ + ot->exec = gp_hide_exec; + ot->poll = gp_active_layer_poll; /* NOTE: we need an active layer to play with */ + + /* flags */ + ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO; + + /* props */ + RNA_def_boolean(ot->srna, "unselected", 0, "Unselected", "Hide unselected rather than selected layers"); +} + +/* ********************** Show All Layers ***************************** */ + +/* poll callback for showing layers */ +static int gp_reveal_poll(bContext *C) +{ + return ED_gpencil_data_get_active(C) != NULL; +} + +static int gp_reveal_exec(bContext *C, wmOperator *UNUSED(op)) +{ + bGPdata *gpd = ED_gpencil_data_get_active(C); + bGPDlayer *gpl; + + /* sanity checks */ + if (gpd == NULL) + return OPERATOR_CANCELLED; + + /* make all layers visible */ + for (gpl = gpd->layers.first; gpl; gpl = gpl->next) { + gpl->flag &= ~GP_LAYER_HIDE; + } + + /* notifiers */ + WM_event_add_notifier(C, NC_GPENCIL | ND_DATA | NA_EDITED, NULL); + + return OPERATOR_FINISHED; +} + +void GPENCIL_OT_reveal(wmOperatorType *ot) +{ + /* identifiers */ + ot->name = "Show All Layers"; + ot->idname = "GPENCIL_OT_reveal"; + ot->description = "Show all Grease Pencil layers"; + + /* callbacks */ + ot->exec = gp_reveal_exec; + ot->poll = gp_reveal_poll; + + /* flags */ + ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO; +} + +/* ************************************************ */ diff --git a/source/blender/editors/gpencil/gpencil_edit.c b/source/blender/editors/gpencil/gpencil_edit.c index 13334448941..5c37a0a5b60 100644 --- a/source/blender/editors/gpencil/gpencil_edit.c +++ b/source/blender/editors/gpencil/gpencil_edit.c @@ -21,12 +21,15 @@ * Contributor(s): Joshua Leung * * ***** END GPL LICENSE BLOCK ***** + * + * Operators for editing Grease Pencil strokes */ /** \file blender/editors/gpencil/gpencil_edit.c * \ingroup edgpencil */ + #include <stdio.h> #include <string.h> #include <stdlib.h> @@ -37,15 +40,10 @@ #include "BLI_math.h" #include "BLI_blenlib.h" -#include "BLI_rand.h" #include "BLI_utildefines.h" -#include "BLF_translation.h" +#include "BLT_translation.h" -#include "DNA_anim_types.h" -#include "DNA_curve_types.h" -#include "DNA_object_types.h" -#include "DNA_node_types.h" #include "DNA_scene_types.h" #include "DNA_screen_types.h" #include "DNA_space_types.h" @@ -53,17 +51,11 @@ #include "DNA_gpencil_types.h" #include "BKE_context.h" -#include "BKE_curve.h" -#include "BKE_depsgraph.h" -#include "BKE_fcurve.h" #include "BKE_global.h" #include "BKE_gpencil.h" #include "BKE_library.h" -#include "BKE_object.h" #include "BKE_report.h" -#include "BKE_scene.h" #include "BKE_screen.h" -#include "BKE_tracking.h" #include "UI_interface.h" @@ -77,261 +69,369 @@ #include "ED_gpencil.h" #include "ED_view3d.h" -#include "ED_clip.h" -#include "ED_keyframing.h" #include "gpencil_intern.h" - /* ************************************************ */ -/* Context Wrangling... */ +/* Stroke Editing Operators */ -/* Get pointer to active Grease Pencil datablock, and an RNA-pointer to trace back to whatever owns it */ -bGPdata **ED_gpencil_data_get_pointers(const bContext *C, PointerRNA *ptr) +/* poll callback for all stroke editing operators */ +static int gp_stroke_edit_poll(bContext *C) { - ID *screen_id = (ID *)CTX_wm_screen(C); - Scene *scene = CTX_data_scene(C); - ScrArea *sa = CTX_wm_area(C); - - /* if there's an active area, check if the particular editor may - * have defined any special Grease Pencil context for editing... - */ - if (sa) { - switch (sa->spacetype) { - case SPACE_VIEW3D: /* 3D-View */ - { - Object *ob = CTX_data_active_object(C); - - /* TODO: we can include other data-types such as bones later if need be... */ - - /* just in case no active/selected object */ - if (ob && (ob->flag & SELECT)) { - /* for now, as long as there's an object, default to using that in 3D-View */ - if (ptr) RNA_id_pointer_create(&ob->id, ptr); - return &ob->gpd; - } - break; - } - case SPACE_NODE: /* Nodes Editor */ - { - SpaceNode *snode = (SpaceNode *)CTX_wm_space_data(C); + /* NOTE: this is a bit slower, but is the most accurate... */ + return CTX_DATA_COUNT(C, editable_gpencil_strokes) != 0; +} - /* return the GP data for the active node block/node */ - if (snode && snode->nodetree) { - /* for now, as long as there's an active node tree, default to using that in the Nodes Editor */ - if (ptr) RNA_id_pointer_create(&snode->nodetree->id, ptr); - return &snode->nodetree->gpd; - } +/* ************** Duplicate Selected Strokes **************** */ - /* even when there is no node-tree, don't allow this to flow to scene */ - return NULL; +/* Make copies of selected point segments in a selected stroke */ +static void gp_duplicate_points(const bGPDstroke *gps, ListBase *new_strokes) +{ + bGPDspoint *pt; + int i; + + int start_idx = -1; + + + /* Step through the original stroke's points: + * - We accumulate selected points (from start_idx to current index) + * and then convert that to a new stroke + */ + for (i = 0, pt = gps->points; i < gps->totpoints; i++, pt++) { + /* searching for start, are waiting for end? */ + if (start_idx == -1) { + /* is this the first selected point for a new island? */ + if (pt->flag & GP_SPOINT_SELECT) { + start_idx = i; } - case SPACE_SEQ: /* Sequencer */ - { - SpaceSeq *sseq = (SpaceSeq *)CTX_wm_space_data(C); - - /* for now, Grease Pencil data is associated with the space (actually preview region only) */ - /* XXX our convention for everything else is to link to data though... */ - if (ptr) RNA_pointer_create(screen_id, &RNA_SpaceSequenceEditor, sseq, ptr); - return &sseq->gpd; + } + else { + size_t len = 0; + + /* is this the end of current island yet? + * 1) Point i-1 was the last one that was selected + * 2) Point i is the last in the array + */ + if ((pt->flag & GP_SPOINT_SELECT) == 0) { + len = i - start_idx; } - case SPACE_IMAGE: /* Image/UV Editor */ - { - SpaceImage *sima = (SpaceImage *)CTX_wm_space_data(C); - - /* for now, Grease Pencil data is associated with the space... */ - /* XXX our convention for everything else is to link to data though... */ - if (ptr) RNA_pointer_create(screen_id, &RNA_SpaceImageEditor, sima, ptr); - return &sima->gpd; + else if (i == gps->totpoints - 1) { + len = i - start_idx + 1; } - case SPACE_CLIP: /* Nodes Editor */ - { - SpaceClip *sc = (SpaceClip *)CTX_wm_space_data(C); - MovieClip *clip = ED_space_clip_get_clip(sc); - - if (clip) { - if (sc->gpencil_src == SC_GPENCIL_SRC_TRACK) { - MovieTrackingTrack *track = BKE_tracking_track_get_active(&clip->tracking); - - if (!track) - return NULL; - - if (ptr) - RNA_pointer_create(&clip->id, &RNA_MovieTrackingTrack, track, ptr); - - return &track->gpd; - } - else { - if (ptr) - RNA_id_pointer_create(&clip->id, ptr); - - return &clip->gpd; - } - } - break; + //printf("copying from %d to %d = %d\n", start_idx, i, len); + + /* make copies of the relevant data */ + if (len) { + bGPDstroke *gpsd; + + /* make a stupid copy first of the entire stroke (to get the flags too) */ + gpsd = MEM_dupallocN(gps); + + /* now, make a new points array, and copy of the relevant parts */ + gpsd->points = MEM_callocN(sizeof(bGPDspoint) * len, "gps stroke points copy"); + memcpy(gpsd->points, gps->points + start_idx, sizeof(bGPDspoint) * len); + gpsd->totpoints = len; + + /* add to temp buffer */ + gpsd->next = gpsd->prev = NULL; + BLI_addtail(new_strokes, gpsd); + + /* cleanup + reset for next */ + start_idx = -1; } - default: /* unsupported space */ - return NULL; } } - - /* just fall back on the scene's GP data */ - if (ptr) RNA_id_pointer_create((ID *)scene, ptr); - return (scene) ? &scene->gpd : NULL; -} - -/* Get the active Grease Pencil datablock */ -bGPdata *ED_gpencil_data_get_active(const bContext *C) -{ - bGPdata **gpd_ptr = ED_gpencil_data_get_pointers(C, NULL); - return (gpd_ptr) ? *(gpd_ptr) : NULL; -} - -bGPdata *ED_gpencil_data_get_active_v3d(Scene *scene, View3D *v3d) -{ - Base *base = scene->basact; - bGPdata *gpd = NULL; - /* We have to make sure active object is actually visible and selected, else we must use default scene gpd, - * to be consistent with ED_gpencil_data_get_active's behavior. - */ - - if (base && TESTBASE(v3d, base)) { - gpd = base->object->gpd; - } - return gpd ? gpd : scene->gpd; -} - -/* ************************************************ */ -/* Panel Operators */ - -/* poll callback for adding data/layers - special */ -static int gp_add_poll(bContext *C) -{ - /* the base line we have is that we have somewhere to add Grease Pencil data */ - return ED_gpencil_data_get_pointers(C, NULL) != NULL; } -/* ******************* Add New Data ************************ */ - -/* add new datablock - wrapper around API */ -static int gp_data_add_exec(bContext *C, wmOperator *op) +static int gp_duplicate_exec(bContext *C, wmOperator *op) { - bGPdata **gpd_ptr = ED_gpencil_data_get_pointers(C, NULL); - - if (gpd_ptr == NULL) { - BKE_report(op->reports, RPT_ERROR, "Nowhere for grease pencil data to go"); + bGPdata *gpd = ED_gpencil_data_get_active(C); + + if (gpd == NULL) { + BKE_report(op->reports, RPT_ERROR, "No Grease Pencil data"); return OPERATOR_CANCELLED; } - else { - /* decrement user count and add new datablock */ - bGPdata *gpd = (*gpd_ptr); - - id_us_min(&gpd->id); - *gpd_ptr = gpencil_data_addnew(DATA_("GPencil")); + + /* for each visible (and editable) layer's selected strokes, + * copy the strokes into a temporary buffer, then append + * once all done + */ + CTX_DATA_BEGIN(C, bGPDlayer *, gpl, editable_gpencil_layers) + { + ListBase new_strokes = {NULL, NULL}; + bGPDframe *gpf = gpl->actframe; + bGPDstroke *gps; + + if (gpf == NULL) + continue; + + /* make copies of selected strokes, and deselect these once we're done */ + for (gps = gpf->strokes.first; gps; gps = gps->next) { + /* skip strokes that are invalid for current view */ + if (ED_gpencil_stroke_can_use(C, gps) == false) + continue; + + if (gps->flag & GP_STROKE_SELECT) { + if (gps->totpoints == 1) { + /* Special Case: If there's just a single point in this stroke... */ + bGPDstroke *gpsd; + + /* make direct copies of the stroke and its points */ + gpsd = MEM_dupallocN(gps); + gpsd->points = MEM_dupallocN(gps->points); + + /* add to temp buffer */ + gpsd->next = gpsd->prev = NULL; + BLI_addtail(&new_strokes, gpsd); + } + else { + /* delegate to a helper, as there's too much to fit in here (for copying subsets)... */ + gp_duplicate_points(gps, &new_strokes); + } + + /* deselect original stroke, or else the originals get moved too + * (when using the copy + move macro) + */ + gps->flag &= ~GP_STROKE_SELECT; + } + } + + /* add all new strokes in temp buffer to the frame (preventing double-copies) */ + BLI_movelisttolist(&gpf->strokes, &new_strokes); + BLI_assert(new_strokes.first == NULL); } - - /* notifiers */ + CTX_DATA_END; + + /* updates */ WM_event_add_notifier(C, NC_GPENCIL | ND_DATA | NA_EDITED, NULL); - + return OPERATOR_FINISHED; } -void GPENCIL_OT_data_add(wmOperatorType *ot) +void GPENCIL_OT_duplicate(wmOperatorType *ot) { /* identifiers */ - ot->name = "Grease Pencil Add New"; - ot->idname = "GPENCIL_OT_data_add"; - ot->description = "Add new Grease Pencil datablock"; - ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO; - + ot->name = "Duplicate Strokes"; + ot->idname = "GPENCIL_OT_duplicate"; + ot->description = "Duplicate the selected Grease Pencil strokes"; + /* callbacks */ - ot->exec = gp_data_add_exec; - ot->poll = gp_add_poll; + ot->exec = gp_duplicate_exec; + ot->poll = gp_stroke_edit_poll; + + /* flags */ + ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO; } -/* ******************* Unlink Data ************************ */ +/* ******************* Copy/Paste Strokes ************************* */ +/* Grease Pencil stroke data copy/paste buffer: + * - The copy operation collects all segments of selected strokes, + * dumping "ready to be copied" copies of the strokes into the buffer. + * - The paste operation makes a copy of those elements, and adds them + * to the active layer. This effectively flattens down the strokes + * from several different layers into a single layer. + */ -/* poll callback for adding data/layers - special */ -static int gp_data_unlink_poll(bContext *C) -{ - bGPdata **gpd_ptr = ED_gpencil_data_get_pointers(C, NULL); +/* list of bGPDstroke instances */ +static ListBase gp_strokes_copypastebuf = {NULL, NULL}; - /* if we have access to some active data, make sure there's a datablock before enabling this */ - return (gpd_ptr && *gpd_ptr); +/* Free copy/paste buffer data */ +void ED_gpencil_strokes_copybuf_free(void) +{ + bGPDstroke *gps, *gpsn; + + for (gps = gp_strokes_copypastebuf.first; gps; gps = gpsn) { + gpsn = gps->next; + + MEM_freeN(gps->points); + BLI_freelinkN(&gp_strokes_copypastebuf, gps); + } + + BLI_listbase_clear(&gp_strokes_copypastebuf); } +/* --------------------- */ +/* Copy selected strokes */ -/* unlink datablock - wrapper around API */ -static int gp_data_unlink_exec(bContext *C, wmOperator *op) +static int gp_strokes_copy_exec(bContext *C, wmOperator *op) { - bGPdata **gpd_ptr = ED_gpencil_data_get_pointers(C, NULL); - - if (gpd_ptr == NULL) { - BKE_report(op->reports, RPT_ERROR, "Nowhere for grease pencil data to go"); + bGPdata *gpd = ED_gpencil_data_get_active(C); + + if (gpd == NULL) { + BKE_report(op->reports, RPT_ERROR, "No Grease Pencil data"); return OPERATOR_CANCELLED; } - else { - /* just unlink datablock now, decreasing its user count */ - bGPdata *gpd = (*gpd_ptr); - - id_us_min(&gpd->id); - *gpd_ptr = NULL; + + /* clear the buffer first */ + ED_gpencil_strokes_copybuf_free(); + + /* for each visible (and editable) layer's selected strokes, + * copy the strokes into a temporary buffer, then append + * once all done + */ + CTX_DATA_BEGIN(C, bGPDlayer *, gpl, editable_gpencil_layers) + { + bGPDframe *gpf = gpl->actframe; + bGPDstroke *gps; + + if (gpf == NULL) + continue; + + /* make copies of selected strokes, and deselect these once we're done */ + for (gps = gpf->strokes.first; gps; gps = gps->next) { + /* skip strokes that are invalid for current view */ + if (ED_gpencil_stroke_can_use(C, gps) == false) + continue; + + if (gps->flag & GP_STROKE_SELECT) { + if (gps->totpoints == 1) { + /* Special Case: If there's just a single point in this stroke... */ + bGPDstroke *gpsd; + + /* make direct copies of the stroke and its points */ + gpsd = MEM_dupallocN(gps); + gpsd->points = MEM_dupallocN(gps->points); + + /* add to temp buffer */ + gpsd->next = gpsd->prev = NULL; + BLI_addtail(&gp_strokes_copypastebuf, gpsd); + } + else { + /* delegate to a helper, as there's too much to fit in here (for copying subsets)... */ + gp_duplicate_points(gps, &gp_strokes_copypastebuf); + } + } + } } - - /* notifiers */ - WM_event_add_notifier(C, NC_GPENCIL | ND_DATA | NA_EDITED, NULL); - + CTX_DATA_END; + + /* done - no updates needed */ return OPERATOR_FINISHED; } -void GPENCIL_OT_data_unlink(wmOperatorType *ot) +void GPENCIL_OT_copy(wmOperatorType *ot) { /* identifiers */ - ot->name = "Grease Pencil Unlink"; - ot->idname = "GPENCIL_OT_data_unlink"; - ot->description = "Unlink active Grease Pencil datablock"; - ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO; - + ot->name = "Copy Strokes"; + ot->idname = "GPENCIL_OT_copy"; + ot->description = "Copy selected Grease Pencil points and strokes"; + /* callbacks */ - ot->exec = gp_data_unlink_exec; - ot->poll = gp_data_unlink_poll; + ot->exec = gp_strokes_copy_exec; + ot->poll = gp_stroke_edit_poll; + + /* flags */ + //ot->flag = OPTYPE_REGISTER; } -/* ******************* Add New Layer ************************ */ +/* --------------------- */ +/* Paste selected strokes */ -/* add new layer - wrapper around API */ -static int gp_layer_add_exec(bContext *C, wmOperator *op) +static int gp_strokes_paste_exec(bContext *C, wmOperator *op) { - bGPdata **gpd_ptr = ED_gpencil_data_get_pointers(C, NULL); - - /* if there's no existing Grease-Pencil data there, add some */ - if (gpd_ptr == NULL) { - BKE_report(op->reports, RPT_ERROR, "Nowhere for grease pencil data to go"); + Scene *scene = CTX_data_scene(C); + bGPdata *gpd = ED_gpencil_data_get_active(C); + bGPDlayer *gpl = CTX_data_active_gpencil_layer(C); + bGPDframe *gpf; + + /* check for various error conditions */ + if (gpd == NULL) { + BKE_report(op->reports, RPT_ERROR, "No Grease Pencil data"); return OPERATOR_CANCELLED; } - if (*gpd_ptr == NULL) - *gpd_ptr = gpencil_data_addnew(DATA_("GPencil")); - - /* add new layer now */ - gpencil_layer_addnew(*gpd_ptr, DATA_("GP_Layer"), 1); - - /* notifiers */ + else if (gp_strokes_copypastebuf.first == NULL) { + BKE_report(op->reports, RPT_ERROR, "No strokes to paste, select and copy some points before trying again"); + return OPERATOR_CANCELLED; + } + else if (gpl == NULL) { + /* no active layer - let's just create one */ + gpl = gpencil_layer_addnew(gpd, DATA_("GP_Layer"), 1); + } + else if (gpl->flag & (GP_LAYER_HIDE | GP_LAYER_LOCKED)) { + BKE_report(op->reports, RPT_ERROR, "Can not paste strokes when active layer is hidden or locked"); + return OPERATOR_CANCELLED; + } + else { + /* Check that some of the strokes in the buffer can be used */ + bGPDstroke *gps; + bool ok = false; + + for (gps = gp_strokes_copypastebuf.first; gps; gps = gps->next) { + if (ED_gpencil_stroke_can_use(C, gps)) { + ok = true; + break; + } + } + + if (ok == false) { + /* XXX: this check is not 100% accurate (i.e. image editor is incompatible with normal 2D strokes), + * but should be enough to give users a good idea of what's going on + */ + if (CTX_wm_area(C)->spacetype == SPACE_VIEW3D) + BKE_report(op->reports, RPT_ERROR, "Cannot paste 2D strokes in 3D View"); + else + BKE_report(op->reports, RPT_ERROR, "Cannot paste 3D strokes in 2D editors"); + + return OPERATOR_CANCELLED; + } + } + + /* Deselect all strokes first */ + CTX_DATA_BEGIN(C, bGPDstroke *, gps, editable_gpencil_strokes) + { + bGPDspoint *pt; + int i; + + for (i = 0, pt = gps->points; i < gps->totpoints; i++, pt++) { + pt->flag &= ~GP_SPOINT_SELECT; + } + + gps->flag &= ~GP_STROKE_SELECT; + } + CTX_DATA_END; + + /* Ensure we have a frame to draw into + * NOTE: Since this is an op which creates strokes, + * we are obliged to add a new frame if one + * doesn't exist already + */ + gpf = gpencil_layer_getframe(gpl, CFRA, true); + + if (gpf) { + bGPDstroke *gps; + + /* Copy each stroke into the layer */ + for (gps = gp_strokes_copypastebuf.first; gps; gps = gps->next) { + if (ED_gpencil_stroke_can_use(C, gps)) { + bGPDstroke *new_stroke = MEM_dupallocN(gps); + + new_stroke->points = MEM_dupallocN(gps->points); + new_stroke->next = new_stroke->prev = NULL; + + BLI_addtail(&gpf->strokes, new_stroke); + } + } + } + + /* updates */ WM_event_add_notifier(C, NC_GPENCIL | ND_DATA | NA_EDITED, NULL); - + return OPERATOR_FINISHED; } -void GPENCIL_OT_layer_add(wmOperatorType *ot) +void GPENCIL_OT_paste(wmOperatorType *ot) { /* identifiers */ - ot->name = "Add New Layer"; - ot->idname = "GPENCIL_OT_layer_add"; - ot->description = "Add new Grease Pencil layer for the active Grease Pencil datablock"; - ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO; - + ot->name = "Paste Strokes"; + ot->idname = "GPENCIL_OT_paste"; + ot->description = "Paste previously copied strokes into active layer"; + /* callbacks */ - ot->exec = gp_layer_add_exec; - ot->poll = gp_add_poll; + ot->exec = gp_strokes_paste_exec; + ot->poll = gp_stroke_edit_poll; + + /* flags */ + ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO; } /* ******************* Delete Active Frame ************************ */ @@ -340,7 +440,7 @@ static int gp_actframe_delete_poll(bContext *C) { bGPdata *gpd = ED_gpencil_data_get_active(C); bGPDlayer *gpl = gpencil_layer_getactive(gpd); - + /* only if there's an active layer with an active frame */ return (gpl && gpl->actframe); } @@ -352,7 +452,7 @@ static int gp_actframe_delete_exec(bContext *C, wmOperator *op) bGPdata *gpd = ED_gpencil_data_get_active(C); bGPDlayer *gpl = gpencil_layer_getactive(gpd); bGPDframe *gpf = gpencil_layer_getframe(gpl, CFRA, 0); - + /* if there's no existing Grease-Pencil data there, add some */ if (gpd == NULL) { BKE_report(op->reports, RPT_ERROR, "No grease pencil data"); @@ -362,13 +462,13 @@ static int gp_actframe_delete_exec(bContext *C, wmOperator *op) BKE_report(op->reports, RPT_ERROR, "No active frame to delete"); return OPERATOR_CANCELLED; } - + /* delete it... */ gpencil_layer_delframe(gpl, gpf); - + /* notifiers */ WM_event_add_notifier(C, NC_GPENCIL | ND_DATA | NA_EDITED, NULL); - + return OPERATOR_FINISHED; } @@ -378,1406 +478,338 @@ void GPENCIL_OT_active_frame_delete(wmOperatorType *ot) ot->name = "Delete Active Frame"; ot->idname = "GPENCIL_OT_active_frame_delete"; ot->description = "Delete the active frame for the active Grease Pencil datablock"; + ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO; - + /* callbacks */ ot->exec = gp_actframe_delete_exec; ot->poll = gp_actframe_delete_poll; } -/* ************************************************ */ -/* Grease Pencil to Data Operator */ - -/* defines for possible modes */ -enum { - GP_STROKECONVERT_PATH = 1, - GP_STROKECONVERT_CURVE, - GP_STROKECONVERT_POLY, -}; - -/* Defines for possible timing modes */ -enum { - GP_STROKECONVERT_TIMING_NONE = 1, - GP_STROKECONVERT_TIMING_LINEAR = 2, - GP_STROKECONVERT_TIMING_FULL = 3, - GP_STROKECONVERT_TIMING_CUSTOMGAP = 4, -}; +/* ******************* Delete Operator ************************ */ -/* RNA enum define */ -static EnumPropertyItem prop_gpencil_convertmodes[] = { - {GP_STROKECONVERT_PATH, "PATH", 0, "Path", ""}, - {GP_STROKECONVERT_CURVE, "CURVE", 0, "Bezier Curve", ""}, - {GP_STROKECONVERT_POLY, "POLY", 0, "Polygon Curve", ""}, - {0, NULL, 0, NULL, NULL} -}; +typedef enum eGP_DeleteMode { + /* delete selected stroke points */ + GP_DELETEOP_POINTS = 0, + /* delete selected strokes */ + GP_DELETEOP_STROKES = 1, + /* delete active frame */ + GP_DELETEOP_FRAME = 2, +} eGP_DeleteMode; -static EnumPropertyItem prop_gpencil_convert_timingmodes_restricted[] = { - {GP_STROKECONVERT_TIMING_NONE, "NONE", 0, "No Timing", "Ignore timing"}, - {GP_STROKECONVERT_TIMING_LINEAR, "LINEAR", 0, "Linear", "Simple linear timing"}, - {0, NULL, 0, NULL, NULL}, -}; -static EnumPropertyItem prop_gpencil_convert_timingmodes[] = { - {GP_STROKECONVERT_TIMING_NONE, "NONE", 0, "No Timing", "Ignore timing"}, - {GP_STROKECONVERT_TIMING_LINEAR, "LINEAR", 0, "Linear", "Simple linear timing"}, - {GP_STROKECONVERT_TIMING_FULL, "FULL", 0, "Original", "Use the original timing, gaps included"}, - {GP_STROKECONVERT_TIMING_CUSTOMGAP, "CUSTOMGAP", 0, "Custom Gaps", - "Use the original timing, but with custom gap lengths (in frames)"}, - {0, NULL, 0, NULL, NULL}, -}; - -static EnumPropertyItem *rna_GPConvert_mode_items(bContext *UNUSED(C), PointerRNA *ptr, PropertyRNA *UNUSED(prop), - bool *UNUSED(r_free)) +/* Delete selected strokes */ +static int gp_delete_selected_strokes(bContext *C) { - if (RNA_boolean_get(ptr, "use_timing_data")) { - return prop_gpencil_convert_timingmodes; - } - return prop_gpencil_convert_timingmodes_restricted; -} - -/* --- */ - -/* convert the coordinates from the given stroke point into 3d-coordinates - * - assumes that the active space is the 3D-View - */ -static void gp_strokepoint_convertcoords(bContext *C, bGPDstroke *gps, bGPDspoint *pt, float p3d[3], rctf *subrect) -{ - Scene *scene = CTX_data_scene(C); - View3D *v3d = CTX_wm_view3d(C); - ARegion *ar = CTX_wm_region(C); - - if (gps->flag & GP_STROKE_3DSPACE) { - /* directly use 3d-coordinates */ - copy_v3_v3(p3d, &pt->x); - } - else { - const float *fp = ED_view3d_cursor3d_get(scene, v3d); - float mvalf[2]; - - /* get screen coordinate */ - if (gps->flag & GP_STROKE_2DSPACE) { - View2D *v2d = &ar->v2d; - UI_view2d_view_to_region_fl(v2d, pt->x, pt->y, &mvalf[0], &mvalf[1]); - } - else { - if (subrect) { - mvalf[0] = (((float)pt->x / 100.0f) * BLI_rctf_size_x(subrect)) + subrect->xmin; - mvalf[1] = (((float)pt->y / 100.0f) * BLI_rctf_size_y(subrect)) + subrect->ymin; - } - else { - mvalf[0] = (float)pt->x / 100.0f * ar->winx; - mvalf[1] = (float)pt->y / 100.0f * ar->winy; - } - } - - ED_view3d_win_to_3d(ar, fp, mvalf, p3d); - } -} - -/* --- */ - -/* temp struct for gp_stroke_path_animation() */ -typedef struct tGpTimingData { - /* Data set from operator settings */ - int mode; - int frame_range; /* Number of frames evaluated for path animation */ - int start_frame, end_frame; - bool realtime; /* Will overwrite end_frame in case of Original or CustomGap timing... */ - float gap_duration, gap_randomness; /* To be used with CustomGap mode*/ - int seed; - - /* Data set from points, used to compute final timing FCurve */ - int num_points, cur_point; - - /* Distances */ - float *dists; - float tot_dist; - - /* Times */ - float *times; /* Note: Gap times will be negative! */ - float tot_time, gap_tot_time; - double inittime; - - /* Only used during creation of dists & times lists. */ - float offset_time; -} tGpTimingData; - -/* Init point buffers for timing data. - * Note this assumes we only grow those arrays! - */ -static void gp_timing_data_set_nbr(tGpTimingData *gtd, const int nbr) -{ - float *tmp; - - BLI_assert(nbr > gtd->num_points); - - /* distances */ - tmp = gtd->dists; - gtd->dists = MEM_callocN(sizeof(float) * nbr, __func__); - if (tmp) { - memcpy(gtd->dists, tmp, sizeof(float) * gtd->num_points); - MEM_freeN(tmp); - } - - /* times */ - tmp = gtd->times; - gtd->times = MEM_callocN(sizeof(float) * nbr, __func__); - if (tmp) { - memcpy(gtd->times, tmp, sizeof(float) * gtd->num_points); - MEM_freeN(tmp); - } - - gtd->num_points = nbr; -} - -/* add stroke point to timing buffers */ -static void gp_timing_data_add_point(tGpTimingData *gtd, const double stroke_inittime, const float time, - const float delta_dist) -{ - float delta_time = 0.0f; - const int cur_point = gtd->cur_point; - - if (!cur_point) { - /* Special case, first point, if time is not 0.0f we have to compensate! */ - gtd->offset_time = -time; - gtd->times[cur_point] = 0.0f; - } - else if (time < 0.0f) { - /* This is a gap, negative value! */ - gtd->times[cur_point] = -(((float)(stroke_inittime - gtd->inittime)) + time + gtd->offset_time); - delta_time = -gtd->times[cur_point] - gtd->times[cur_point - 1]; - - gtd->gap_tot_time += delta_time; + bool changed = false; + + CTX_DATA_BEGIN(C, bGPDlayer *, gpl, editable_gpencil_layers) + { + bGPDframe *gpf = gpl->actframe; + bGPDstroke *gps, *gpsn; + + if (gpf == NULL) + continue; + + /* simply delete strokes which are selected */ + for (gps = gpf->strokes.first; gps; gps = gpsn) { + gpsn = gps->next; + + /* skip strokes that are invalid for current view */ + if (ED_gpencil_stroke_can_use(C, gps) == false) + continue; + + /* free stroke if selected */ + if (gps->flag & GP_STROKE_SELECT) { + /* free stroke memory arrays, then stroke itself */ + if (gps->points) MEM_freeN(gps->points); + BLI_freelinkN(&gpf->strokes, gps); + + changed = true; + } + } + } + CTX_DATA_END; + + if (changed) { + WM_event_add_notifier(C, NC_GPENCIL | ND_DATA | NA_EDITED, NULL); + return OPERATOR_FINISHED; } else { - gtd->times[cur_point] = (((float)(stroke_inittime - gtd->inittime)) + time + gtd->offset_time); - delta_time = gtd->times[cur_point] - fabsf(gtd->times[cur_point - 1]); + return OPERATOR_CANCELLED; } - - gtd->tot_time += delta_time; - gtd->tot_dist += delta_dist; - gtd->dists[cur_point] = gtd->tot_dist; - - gtd->cur_point++; } -/* In frames! Binary search for FCurve keys have a threshold of 0.01, so we can't set - * arbitrarily close points - this is esp. important with NoGaps mode! - */ -#define MIN_TIME_DELTA 0.02f - -/* Loop over next points to find the end of the stroke, and compute */ -static int gp_find_end_of_stroke_idx(tGpTimingData *gtd, RNG *rng, const int idx, const int nbr_gaps, - int *nbr_done_gaps, const float tot_gaps_time, const float delta_time, - float *next_delta_time) +/* Delete selected points but keep the stroke */ +static int gp_dissolve_selected_points(bContext *C) { - int j; - - for (j = idx + 1; j < gtd->num_points; j++) { - if (gtd->times[j] < 0) { - gtd->times[j] = -gtd->times[j]; - if (gtd->mode == GP_STROKECONVERT_TIMING_CUSTOMGAP) { - /* In this mode, gap time between this stroke and the next should be 0 currently... - * So we have to compute its final duration! - */ - if (gtd->gap_randomness > 0.0f) { - /* We want gaps that are in gtd->gap_duration +/- gtd->gap_randomness range, - * and which sum to exactly tot_gaps_time... - */ - int rem_gaps = nbr_gaps - (*nbr_done_gaps); - if (rem_gaps < 2) { - /* Last gap, just give remaining time! */ - *next_delta_time = tot_gaps_time; - } - else { - float delta, min, max; - - /* This code ensures that if the first gaps have been shorter than average gap_duration, - * next gaps will tend to be longer (i.e. try to recover the lateness), and vice-versa! - */ - delta = delta_time - (gtd->gap_duration * (*nbr_done_gaps)); - - /* Clamp min between [-gap_randomness, 0.0], with lower delta giving higher min */ - min = -gtd->gap_randomness - delta; - CLAMP(min, -gtd->gap_randomness, 0.0f); - - /* Clamp max between [0.0, gap_randomness], with lower delta giving higher max */ - max = gtd->gap_randomness - delta; - CLAMP(max, 0.0f, gtd->gap_randomness); - *next_delta_time += gtd->gap_duration + (BLI_rng_get_float(rng) * (max - min)) + min; + bool changed = false; + + CTX_DATA_BEGIN(C, bGPDlayer *, gpl, editable_gpencil_layers) + { + bGPDframe *gpf = gpl->actframe; + bGPDstroke *gps, *gpsn; + + if (gpf == NULL) + continue; + + /* simply delete points from selected strokes + * NOTE: we may still have to remove the stroke if it ends up having no points! + */ + for (gps = gpf->strokes.first; gps; gps = gpsn) { + gpsn = gps->next; + + /* skip strokes that are invalid for current view */ + if (ED_gpencil_stroke_can_use(C, gps) == false) + continue; + + if (gps->flag & GP_STROKE_SELECT) { + bGPDspoint *pt; + int i; + + int tot = gps->totpoints; /* number of points in new buffer */ + + /* First Pass: Count how many points are selected (i.e. how many to remove) */ + for (i = 0, pt = gps->points; i < gps->totpoints; i++, pt++) { + if (pt->flag & GP_SPOINT_SELECT) { + /* selected point - one of the points to remove */ + tot--; } } - else { - *next_delta_time += gtd->gap_duration; + + /* if no points are left, we simply delete the entire stroke */ + if (tot <= 0) { + /* remove the entire stroke */ + MEM_freeN(gps->points); + BLI_freelinkN(&gpf->strokes, gps); } - } - (*nbr_done_gaps)++; - break; - } - } - - return j - 1; -} - -static void gp_stroke_path_animation_preprocess_gaps(tGpTimingData *gtd, RNG *rng, int *nbr_gaps, float *tot_gaps_time) -{ - int i; - float delta_time = 0.0f; - - for (i = 0; i < gtd->num_points; i++) { - if (gtd->times[i] < 0 && i) { - (*nbr_gaps)++; - gtd->times[i] = -gtd->times[i] - delta_time; - delta_time += gtd->times[i] - gtd->times[i - 1]; - gtd->times[i] = -gtd->times[i - 1]; /* Temp marker, values *have* to be different! */ - } - else { - gtd->times[i] -= delta_time; - } - } - gtd->tot_time -= delta_time; - - *tot_gaps_time = (float)(*nbr_gaps) * gtd->gap_duration; - gtd->tot_time += *tot_gaps_time; - if (G.debug & G_DEBUG) { - printf("%f, %f, %f, %d\n", gtd->tot_time, delta_time, *tot_gaps_time, *nbr_gaps); - } - if (gtd->gap_randomness > 0.0f) { - BLI_rng_srandom(rng, gtd->seed); - } -} - -static void gp_stroke_path_animation_add_keyframes(ReportList *reports, PointerRNA ptr, PropertyRNA *prop, FCurve *fcu, - Curve *cu, tGpTimingData *gtd, RNG *rng, const float time_range, - const int nbr_gaps, const float tot_gaps_time) -{ - /* Use actual recorded timing! */ - const float time_start = (float)gtd->start_frame; - - float last_valid_time = 0.0f; - int end_stroke_idx = -1, start_stroke_idx = 0; - float end_stroke_time = 0.0f; - - /* CustomGaps specific */ - float delta_time = 0.0f, next_delta_time = 0.0f; - int nbr_done_gaps = 0; - - int i; - float cfra; - - /* This is a bit tricky, as: - * - We can't add arbitrarily close points on FCurve (in time). - * - We *must* have all "caps" points of all strokes in FCurve, as much as possible! - */ - for (i = 0; i < gtd->num_points; i++) { - /* If new stroke... */ - if (i > end_stroke_idx) { - start_stroke_idx = i; - delta_time = next_delta_time; - /* find end of that new stroke */ - end_stroke_idx = gp_find_end_of_stroke_idx(gtd, rng, i, nbr_gaps, &nbr_done_gaps, - tot_gaps_time, delta_time, &next_delta_time); - /* This one should *never* be negative! */ - end_stroke_time = time_start + ((gtd->times[end_stroke_idx] + delta_time) / gtd->tot_time * time_range); - } - - /* Simple proportional stuff... */ - cu->ctime = gtd->dists[i] / gtd->tot_dist * cu->pathlen; - cfra = time_start + ((gtd->times[i] + delta_time) / gtd->tot_time * time_range); - - /* And now, the checks about timing... */ - if (i == start_stroke_idx) { - /* If first point of a stroke, be sure it's enough ahead of last valid keyframe, and - * that the end point of the stroke is far enough! - * In case it is not, we keep the end point... - * Note that with CustomGaps mode, this is here we set the actual gap timing! - */ - if ((end_stroke_time - last_valid_time) > MIN_TIME_DELTA * 2) { - if ((cfra - last_valid_time) < MIN_TIME_DELTA) { - cfra = last_valid_time + MIN_TIME_DELTA; + else { + /* just copy all unselected into a smaller buffer */ + bGPDspoint *new_points = MEM_callocN(sizeof(bGPDspoint) * tot, "new gp stroke points copy"); + bGPDspoint *npt = new_points; + + for (i = 0, pt = gps->points; i < gps->totpoints; i++, pt++) { + if ((pt->flag & GP_SPOINT_SELECT) == 0) { + *npt = *pt; + npt++; + } + } + + /* free the old buffer */ + MEM_freeN(gps->points); + + /* save the new buffer */ + gps->points = new_points; + gps->totpoints = tot; + + /* deselect the stroke, since none of its selected points will still be selected */ + gps->flag &= ~GP_STROKE_SELECT; } - insert_keyframe_direct(reports, ptr, prop, fcu, cfra, INSERTKEY_FAST); - last_valid_time = cfra; - } - else if (G.debug & G_DEBUG) { - printf("\t Skipping start point %d, too close from end point %d\n", i, end_stroke_idx); - } - } - else if (i == end_stroke_idx) { - /* Always try to insert end point of a curve (should be safe enough, anyway...) */ - if ((cfra - last_valid_time) < MIN_TIME_DELTA) { - cfra = last_valid_time + MIN_TIME_DELTA; - } - insert_keyframe_direct(reports, ptr, prop, fcu, cfra, INSERTKEY_FAST); - last_valid_time = cfra; - } - else { - /* Else ("middle" point), we only insert it if it's far enough from last keyframe, - * and also far enough from (not yet added!) end_stroke keyframe! - */ - if ((cfra - last_valid_time) > MIN_TIME_DELTA && (end_stroke_time - cfra) > MIN_TIME_DELTA) { - insert_keyframe_direct(reports, ptr, prop, fcu, cfra, INSERTKEY_FAST); - last_valid_time = cfra; - } - else if (G.debug & G_DEBUG) { - printf("\t Skipping \"middle\" point %d, too close from last added point or end point %d\n", - i, end_stroke_idx); + + changed = true; } } } -} - -static void gp_stroke_path_animation(bContext *C, ReportList *reports, Curve *cu, tGpTimingData *gtd) -{ - Scene *scene = CTX_data_scene(C); - bAction *act; - FCurve *fcu; - PointerRNA ptr; - PropertyRNA *prop = NULL; - int nbr_gaps = 0, i; - - if (gtd->mode == GP_STROKECONVERT_TIMING_NONE) - return; - - /* gap_duration and gap_randomness are in frames, but we need seconds!!! */ - gtd->gap_duration = FRA2TIME(gtd->gap_duration); - gtd->gap_randomness = FRA2TIME(gtd->gap_randomness); - - /* Enable path! */ - cu->flag |= CU_PATH; - cu->pathlen = gtd->frame_range; - - /* Get RNA pointer to read/write path time values */ - RNA_id_pointer_create((ID *)cu, &ptr); - prop = RNA_struct_find_property(&ptr, "eval_time"); - - /* Ensure we have an F-Curve to add keyframes to */ - act = verify_adt_action((ID *)cu, true); - fcu = verify_fcurve(act, NULL, &ptr, "eval_time", 0, true); - - if (G.debug & G_DEBUG) { - printf("%s: tot len: %f\t\ttot time: %f\n", __func__, gtd->tot_dist, gtd->tot_time); - for (i = 0; i < gtd->num_points; i++) { - printf("\tpoint %d:\t\tlen: %f\t\ttime: %f\n", i, gtd->dists[i], gtd->times[i]); - } - } - - if (gtd->mode == GP_STROKECONVERT_TIMING_LINEAR) { - float cfra; - - /* Linear extrapolation! */ - fcu->extend = FCURVE_EXTRAPOLATE_LINEAR; - - cu->ctime = 0.0f; - cfra = (float)gtd->start_frame; - insert_keyframe_direct(reports, ptr, prop, fcu, cfra, INSERTKEY_FAST); - - cu->ctime = cu->pathlen; - if (gtd->realtime) { - cfra += (float)TIME2FRA(gtd->tot_time); /* Seconds to frames */ - } - else { - cfra = (float)gtd->end_frame; - } - insert_keyframe_direct(reports, ptr, prop, fcu, cfra, INSERTKEY_FAST); + CTX_DATA_END; + + if (changed) { + WM_event_add_notifier(C, NC_GPENCIL | ND_DATA | NA_EDITED, NULL); + return OPERATOR_FINISHED; } else { - /* Use actual recorded timing! */ - RNG *rng = BLI_rng_new(0); - float time_range; - - /* CustomGaps specific */ - float tot_gaps_time = 0.0f; - - /* Pre-process gaps, in case we don't want to keep their original timing */ - if (gtd->mode == GP_STROKECONVERT_TIMING_CUSTOMGAP) { - gp_stroke_path_animation_preprocess_gaps(gtd, rng, &nbr_gaps, &tot_gaps_time); - } - - if (gtd->realtime) { - time_range = (float)TIME2FRA(gtd->tot_time); /* Seconds to frames */ - } - else { - time_range = (float)(gtd->end_frame - gtd->start_frame); - } - - if (G.debug & G_DEBUG) { - printf("GP Stroke Path Conversion: Starting keying!\n"); - } - - gp_stroke_path_animation_add_keyframes(reports, ptr, prop, fcu, cu, gtd, rng, time_range, - nbr_gaps, tot_gaps_time); - - BLI_rng_free(rng); - } - - /* As we used INSERTKEY_FAST mode, we need to recompute all curve's handles now */ - calchandles_fcurve(fcu); - - if (G.debug & G_DEBUG) { - printf("%s: \ntot len: %f\t\ttot time: %f\n", __func__, gtd->tot_dist, gtd->tot_time); - for (i = 0; i < gtd->num_points; i++) { - printf("\tpoint %d:\t\tlen: %f\t\ttime: %f\n", i, gtd->dists[i], gtd->times[i]); - } - printf("\n\n"); - } - - WM_event_add_notifier(C, NC_ANIMATION | ND_KEYFRAME | NA_EDITED, NULL); - - /* send updates */ - DAG_id_tag_update(&cu->id, 0); -} - -#undef MIN_TIME_DELTA - -#define GAP_DFAC 0.01f -#define WIDTH_CORR_FAC 0.1f -#define BEZT_HANDLE_FAC 0.3f - -/* convert stroke to 3d path */ - -/* helper */ -static void gp_stroke_to_path_add_point(tGpTimingData *gtd, BPoint *bp, const float p[3], const float prev_p[3], - const bool do_gtd, const double inittime, const float time, - const float width, const float rad_fac, float minmax_weights[2]) -{ - copy_v3_v3(bp->vec, p); - bp->vec[3] = 1.0f; - - /* set settings */ - bp->f1 = SELECT; - bp->radius = width * rad_fac; - bp->weight = width; - CLAMP(bp->weight, 0.0f, 1.0f); - if (bp->weight < minmax_weights[0]) { - minmax_weights[0] = bp->weight; - } - else if (bp->weight > minmax_weights[1]) { - minmax_weights[1] = bp->weight; - } - - /* Update timing data */ - if (do_gtd) { - gp_timing_data_add_point(gtd, inittime, time, len_v3v3(prev_p, p)); + return OPERATOR_CANCELLED; } } -static void gp_stroke_to_path(bContext *C, bGPDlayer *gpl, bGPDstroke *gps, Curve *cu, rctf *subrect, Nurb **curnu, - float minmax_weights[2], const float rad_fac, bool stitch, const bool add_start_point, - const bool add_end_point, tGpTimingData *gtd) +/* Split selected strokes into segments, splitting on selected points */ +static int gp_delete_selected_points(bContext *C) { - bGPDspoint *pt; - Nurb *nu = (curnu) ? *curnu : NULL; - BPoint *bp, *prev_bp = NULL; - const bool do_gtd = (gtd->mode != GP_STROKECONVERT_TIMING_NONE); - const int add_start_end_points = (add_start_point ? 1 : 0) + (add_end_point ? 1 : 0); - int i, old_nbp = 0; - - /* create new 'nurb' or extend current one within the curve */ - if (nu) { - old_nbp = nu->pntsu; - - /* If stitch, the first point of this stroke is already present in current nu. - * Else, we have to add two additional points to make the zero-radius link between strokes. - */ - BKE_nurb_points_add(nu, gps->totpoints + (stitch ? -1 : 2) + add_start_end_points); - } - else { - nu = (Nurb *)MEM_callocN(sizeof(Nurb), "gpstroke_to_path(nurb)"); - - nu->pntsu = gps->totpoints + add_start_end_points; - nu->pntsv = 1; - nu->orderu = 2; /* point-to-point! */ - nu->type = CU_NURBS; - nu->flagu = CU_NURB_ENDPOINT; - nu->resolu = cu->resolu; - nu->resolv = cu->resolv; - nu->knotsu = NULL; - - nu->bp = (BPoint *)MEM_callocN(sizeof(BPoint) * nu->pntsu, "bpoints"); - - stitch = false; /* Security! */ - } - - if (do_gtd) { - gp_timing_data_set_nbr(gtd, nu->pntsu); - } - - /* If needed, make the link between both strokes with two zero-radius additional points */ - /* About "zero-radius" point interpolations: - * - If we have at least two points in current curve (most common case), we linearly extrapolate - * the last segment to get the first point (p1) position and timing. - * - If we do not have those (quite odd, but may happen), we linearly interpolate the last point - * with the first point of the current stroke. - * The same goes for the second point, first segment of the current stroke is "negatively" extrapolated - * if it exists, else (if the stroke is a single point), linear interpolation with last curve point... - */ - if (curnu && !stitch && old_nbp) { - float p1[3], p2[3], p[3], next_p[3]; - float dt1 = 0.0f, dt2 = 0.0f; - - BLI_assert(gps->prev != NULL); - - prev_bp = NULL; - if ((old_nbp > 1) && (gps->prev->totpoints > 1)) { - /* Only use last curve segment if previous stroke was not a single-point one! */ - prev_bp = &nu->bp[old_nbp - 2]; - } - bp = &nu->bp[old_nbp - 1]; - - /* First point */ - gp_strokepoint_convertcoords(C, gps, gps->points, p, subrect); - if (prev_bp) { - interp_v3_v3v3(p1, bp->vec, prev_bp->vec, -GAP_DFAC); - if (do_gtd) { - const int idx = gps->prev->totpoints - 1; - dt1 = interpf(gps->prev->points[idx - 1].time, gps->prev->points[idx].time, -GAP_DFAC); - } - } - else { - interp_v3_v3v3(p1, bp->vec, p, GAP_DFAC); - if (do_gtd) { - dt1 = interpf(gps->inittime - gps->prev->inittime, 0.0f, GAP_DFAC); - } - } - bp++; - gp_stroke_to_path_add_point(gtd, bp, p1, (bp - 1)->vec, do_gtd, gps->prev->inittime, dt1, - 0.0f, rad_fac, minmax_weights); - - /* Second point */ - /* Note dt2 is always negative, which marks the gap. */ - if (gps->totpoints > 1) { - gp_strokepoint_convertcoords(C, gps, gps->points + 1, next_p, subrect); - interp_v3_v3v3(p2, p, next_p, -GAP_DFAC); - if (do_gtd) { - dt2 = interpf(gps->points[1].time, gps->points[0].time, -GAP_DFAC); - } - } - else { - interp_v3_v3v3(p2, p, bp->vec, GAP_DFAC); - if (do_gtd) { - dt2 = interpf(gps->prev->inittime - gps->inittime, 0.0f, GAP_DFAC); - } - } - bp++; - gp_stroke_to_path_add_point(gtd, bp, p2, p1, do_gtd, gps->inittime, dt2, 0.0f, rad_fac, minmax_weights); - - old_nbp += 2; - } - else if (add_start_point) { - float p[3], next_p[3]; - float dt = 0.0f; - - gp_strokepoint_convertcoords(C, gps, gps->points, p, subrect); - if (gps->totpoints > 1) { - gp_strokepoint_convertcoords(C, gps, gps->points + 1, next_p, subrect); - interp_v3_v3v3(p, p, next_p, -GAP_DFAC); - if (do_gtd) { - dt = interpf(gps->points[1].time, gps->points[0].time, -GAP_DFAC); - } - } - else { - p[0] -= GAP_DFAC; /* Rather arbitrary... */ - dt = -GAP_DFAC; /* Rather arbitrary too! */ - } - bp = &nu->bp[old_nbp]; - /* Note we can't give anything else than 0.0 as time here, since a negative one (which would be expected value) - * would not work (it would be *before* gtd->inittime, which is not supported currently). - */ - gp_stroke_to_path_add_point(gtd, bp, p, p, do_gtd, gps->inittime, dt, 0.0f, rad_fac, minmax_weights); - - old_nbp++; - } - - if (old_nbp) { - prev_bp = &nu->bp[old_nbp - 1]; - } - - /* add points */ - for (i = (stitch) ? 1 : 0, pt = &gps->points[(stitch) ? 1 : 0], bp = &nu->bp[old_nbp]; - i < gps->totpoints; - i++, pt++, bp++) + bool changed = false; + + CTX_DATA_BEGIN(C, bGPDlayer *, gpl, editable_gpencil_layers) { - float p[3]; - float width = pt->pressure * gpl->thickness * WIDTH_CORR_FAC; - - /* get coordinates to add at */ - gp_strokepoint_convertcoords(C, gps, pt, p, subrect); - - gp_stroke_to_path_add_point(gtd, bp, p, (prev_bp) ? prev_bp->vec : p, do_gtd, gps->inittime, pt->time, - width, rad_fac, minmax_weights); - - prev_bp = bp; - } - - if (add_end_point) { - float p[3]; - float dt = 0.0f; - - if (gps->totpoints > 1) { - interp_v3_v3v3(p, prev_bp->vec, (prev_bp - 1)->vec, -GAP_DFAC); - if (do_gtd) { - const int idx = gps->totpoints - 1; - dt = interpf(gps->points[idx - 1].time, gps->points[idx].time, -GAP_DFAC); - } - } - else { - copy_v3_v3(p, prev_bp->vec); - p[0] += GAP_DFAC; /* Rather arbitrary... */ - dt = GAP_DFAC; /* Rather arbitrary too! */ - } - /* Note bp has already been incremented in main loop above, so it points to the right place. */ - gp_stroke_to_path_add_point(gtd, bp, p, prev_bp->vec, do_gtd, gps->inittime, dt, 0.0f, rad_fac, minmax_weights); - } - - /* add nurb to curve */ - if (!curnu || !*curnu) { - BLI_addtail(&cu->nurb, nu); - } - if (curnu) { - *curnu = nu; - } - - BKE_nurb_knot_calc_u(nu); -} - -/* convert stroke to 3d bezier */ - -/* helper */ -static void gp_stroke_to_bezier_add_point(tGpTimingData *gtd, BezTriple *bezt, - const float p[3], const float h1[3], const float h2[3], const float prev_p[3], - const bool do_gtd, const double inittime, const float time, - const float width, const float rad_fac, float minmax_weights[2]) -{ - copy_v3_v3(bezt->vec[0], h1); - copy_v3_v3(bezt->vec[1], p); - copy_v3_v3(bezt->vec[2], h2); - - /* set settings */ - bezt->h1 = bezt->h2 = HD_FREE; - bezt->f1 = bezt->f2 = bezt->f3 = SELECT; - bezt->radius = width * rad_fac; - bezt->weight = width; - CLAMP(bezt->weight, 0.0f, 1.0f); - if (bezt->weight < minmax_weights[0]) { - minmax_weights[0] = bezt->weight; - } - else if (bezt->weight > minmax_weights[1]) { - minmax_weights[1] = bezt->weight; - } - - /* Update timing data */ - if (do_gtd) { - gp_timing_data_add_point(gtd, inittime, time, len_v3v3(prev_p, p)); - } -} - -static void gp_stroke_to_bezier(bContext *C, bGPDlayer *gpl, bGPDstroke *gps, Curve *cu, rctf *subrect, Nurb **curnu, - float minmax_weights[2], const float rad_fac, bool stitch, const bool add_start_point, - const bool add_end_point, tGpTimingData *gtd) -{ - bGPDspoint *pt; - Nurb *nu = (curnu) ? *curnu : NULL; - BezTriple *bezt, *prev_bezt = NULL; - int i, tot, old_nbezt = 0; - const int add_start_end_points = (add_start_point ? 1 : 0) + (add_end_point ? 1 : 0); - float p3d_cur[3], p3d_prev[3], p3d_next[3], h1[3], h2[3]; - const bool do_gtd = (gtd->mode != GP_STROKECONVERT_TIMING_NONE); - - /* create new 'nurb' or extend current one within the curve */ - if (nu) { - old_nbezt = nu->pntsu; - /* If we do stitch, first point of current stroke is assumed the same as last point of previous stroke, - * so no need to add it. - * If no stitch, we want to add two additional points to make a "zero-radius" link between both strokes. - */ - BKE_nurb_bezierPoints_add(nu, gps->totpoints + ((stitch) ? -1 : 2) + add_start_end_points); - } - else { - nu = (Nurb *)MEM_callocN(sizeof(Nurb), "gpstroke_to_bezier(nurb)"); - - nu->pntsu = gps->totpoints + add_start_end_points; - nu->resolu = 12; - nu->resolv = 12; - nu->type = CU_BEZIER; - nu->bezt = (BezTriple *)MEM_callocN(sizeof(BezTriple) * nu->pntsu, "bezts"); - - stitch = false; /* Security! */ - } - - if (do_gtd) { - gp_timing_data_set_nbr(gtd, nu->pntsu); - } - - tot = gps->totpoints; - - /* get initial coordinates */ - pt = gps->points; - if (tot) { - gp_strokepoint_convertcoords(C, gps, pt, (stitch) ? p3d_prev : p3d_cur, subrect); - if (tot > 1) { - gp_strokepoint_convertcoords(C, gps, pt + 1, (stitch) ? p3d_cur : p3d_next, subrect); - } - if (stitch && tot > 2) { - gp_strokepoint_convertcoords(C, gps, pt + 2, p3d_next, subrect); - } - } - - /* If needed, make the link between both strokes with two zero-radius additional points */ - if (curnu && old_nbezt) { - BLI_assert(gps->prev != NULL); - - /* Update last point's second handle */ - if (stitch) { - bezt = &nu->bezt[old_nbezt - 1]; - interp_v3_v3v3(h2, bezt->vec[1], p3d_cur, BEZT_HANDLE_FAC); - copy_v3_v3(bezt->vec[2], h2); - pt++; - } - - /* Create "link points" */ - /* About "zero-radius" point interpolations: - * - If we have at least two points in current curve (most common case), we linearly extrapolate - * the last segment to get the first point (p1) position and timing. - * - If we do not have those (quite odd, but may happen), we linearly interpolate the last point - * with the first point of the current stroke. - * The same goes for the second point, first segment of the current stroke is "negatively" extrapolated - * if it exists, else (if the stroke is a single point), linear interpolation with last curve point... - */ - else { - float p1[3], p2[3]; - float dt1 = 0.0f, dt2 = 0.0f; - - prev_bezt = NULL; - if ((old_nbezt > 1) && (gps->prev->totpoints > 1)) { - /* Only use last curve segment if previous stroke was not a single-point one! */ - prev_bezt = &nu->bezt[old_nbezt - 2]; - } - bezt = &nu->bezt[old_nbezt - 1]; - - /* First point */ - if (prev_bezt) { - interp_v3_v3v3(p1, prev_bezt->vec[1], bezt->vec[1], 1.0f + GAP_DFAC); - if (do_gtd) { - const int idx = gps->prev->totpoints - 1; - dt1 = interpf(gps->prev->points[idx - 1].time, gps->prev->points[idx].time, -GAP_DFAC); - } - } - else { - interp_v3_v3v3(p1, bezt->vec[1], p3d_cur, GAP_DFAC); - if (do_gtd) { - dt1 = interpf(gps->inittime - gps->prev->inittime, 0.0f, GAP_DFAC); - } - } - - /* Second point */ - /* Note dt2 is always negative, which marks the gap. */ - if (tot > 1) { - interp_v3_v3v3(p2, p3d_cur, p3d_next, -GAP_DFAC); - if (do_gtd) { - dt2 = interpf(gps->points[1].time, gps->points[0].time, -GAP_DFAC); + bGPDframe *gpf = gpl->actframe; + bGPDstroke *gps, *gpsn; + + if (gpf == NULL) + continue; + + /* simply delete strokes which are selected */ + for (gps = gpf->strokes.first; gps; gps = gpsn) { + gpsn = gps->next; + + /* skip strokes that are invalid for current view */ + if (ED_gpencil_stroke_can_use(C, gps) == false) + continue; + + + if (gps->flag & GP_STROKE_SELECT) { + bGPDspoint *pt; + int i; + + /* The algorithm used here is as follows: + * 1) We firstly identify the number of "islands" of non-selected points + * which will all end up being in new strokes. + * - In the most extreme case (i.e. every other vert is a 1-vert island), + * we have at most n / 2 islands + * - Once we start having larger islands than that, the number required + * becomes much less + * 2) Each island gets converted to a new stroke + */ + typedef struct tGPDeleteIsland { + int start_idx; + int end_idx; + } tGPDeleteIsland; + + tGPDeleteIsland *islands = MEM_callocN(sizeof(tGPDeleteIsland) * (gps->totpoints + 1) / 2, "gp_point_islands"); + bool in_island = false; + int num_islands = 0; + + /* First Pass: Identify start/end of islands */ + for (i = 0, pt = gps->points; i < gps->totpoints; i++, pt++) { + if (pt->flag & GP_SPOINT_SELECT) { + /* selected - stop accumulating to island */ + in_island = false; + } + else { + /* unselected - start of a new island? */ + int idx; + + if (in_island) { + /* extend existing island */ + idx = num_islands - 1; + islands[idx].end_idx = i; + } + else { + /* start of new island */ + in_island = true; + num_islands++; + + idx = num_islands - 1; + islands[idx].start_idx = islands[idx].end_idx = i; + } + } } - } - else { - interp_v3_v3v3(p2, p3d_cur, bezt->vec[1], GAP_DFAC); - if (do_gtd) { - dt2 = interpf(gps->prev->inittime - gps->inittime, 0.0f, GAP_DFAC); + + /* Watch out for special case where No islands = All points selected = Delete Stroke only */ + if (num_islands) { + /* there are islands, so create a series of new strokes, adding them before the "next" stroke */ + int idx; + + /* deselect old stroke, since it will be used as template for the new strokes */ + gps->flag &= ~GP_STROKE_SELECT; + + /* create each new stroke... */ + for (idx = 0; idx < num_islands; idx++) { + tGPDeleteIsland *island = &islands[idx]; + bGPDstroke *new_stroke = MEM_dupallocN(gps); + + /* compute new buffer size (+ 1 needed as the endpoint index is "inclusive") */ + new_stroke->totpoints = island->end_idx - island->start_idx + 1; + new_stroke->points = MEM_callocN(sizeof(bGPDspoint) * new_stroke->totpoints, "gp delete stroke fragment"); + + /* copy over the relevant points */ + memcpy(new_stroke->points, gps->points + island->start_idx, sizeof(bGPDspoint) * new_stroke->totpoints); + + /* add new stroke to the frame */ + if (gpsn) { + BLI_insertlinkbefore(&gpf->strokes, gpsn, new_stroke); + } + else { + BLI_addtail(&gpf->strokes, new_stroke); + } + } } - } - - /* Second handle of last point of previous stroke. */ - interp_v3_v3v3(h2, bezt->vec[1], p1, BEZT_HANDLE_FAC); - copy_v3_v3(bezt->vec[2], h2); - - /* First point */ - interp_v3_v3v3(h1, p1, bezt->vec[1], BEZT_HANDLE_FAC); - interp_v3_v3v3(h2, p1, p2, BEZT_HANDLE_FAC); - bezt++; - gp_stroke_to_bezier_add_point(gtd, bezt, p1, h1, h2, (bezt - 1)->vec[1], do_gtd, gps->prev->inittime, dt1, - 0.0f, rad_fac, minmax_weights); - - /* Second point */ - interp_v3_v3v3(h1, p2, p1, BEZT_HANDLE_FAC); - interp_v3_v3v3(h2, p2, p3d_cur, BEZT_HANDLE_FAC); - bezt++; - gp_stroke_to_bezier_add_point(gtd, bezt, p2, h1, h2, p1, do_gtd, gps->inittime, dt2, - 0.0f, rad_fac, minmax_weights); - - old_nbezt += 2; - copy_v3_v3(p3d_prev, p2); - } - } - else if (add_start_point) { - float p[3]; - float dt = 0.0f; - - if (gps->totpoints > 1) { - interp_v3_v3v3(p, p3d_cur, p3d_next, -GAP_DFAC); - if (do_gtd) { - dt = interpf(gps->points[1].time, gps->points[0].time, -GAP_DFAC); + + /* free islands */ + MEM_freeN(islands); + + /* Delete the old stroke */ + MEM_freeN(gps->points); + BLI_freelinkN(&gpf->strokes, gps); + + changed = true; } } - else { - copy_v3_v3(p, p3d_cur); - p[0] -= GAP_DFAC; /* Rather arbitrary... */ - dt = -GAP_DFAC; /* Rather arbitrary too! */ - } - interp_v3_v3v3(h1, p, p3d_cur, -BEZT_HANDLE_FAC); - interp_v3_v3v3(h2, p, p3d_cur, BEZT_HANDLE_FAC); - bezt = &nu->bezt[old_nbezt]; - gp_stroke_to_bezier_add_point(gtd, bezt, p, h1, h2, p, do_gtd, gps->inittime, dt, - 0.0f, rad_fac, minmax_weights); - - old_nbezt++; - copy_v3_v3(p3d_prev, p); } - - if (old_nbezt) { - prev_bezt = &nu->bezt[old_nbezt - 1]; + CTX_DATA_END; + + if (changed) { + WM_event_add_notifier(C, NC_GPENCIL | ND_DATA | NA_EDITED, NULL); + return OPERATOR_FINISHED; } - - /* add points */ - for (i = stitch ? 1 : 0, bezt = &nu->bezt[old_nbezt]; i < tot; i++, pt++, bezt++) { - float width = pt->pressure * gpl->thickness * WIDTH_CORR_FAC; - - if (i || old_nbezt) { - interp_v3_v3v3(h1, p3d_cur, p3d_prev, BEZT_HANDLE_FAC); - } - else { - interp_v3_v3v3(h1, p3d_cur, p3d_next, -BEZT_HANDLE_FAC); - } - - if (i < tot - 1) { - interp_v3_v3v3(h2, p3d_cur, p3d_next, BEZT_HANDLE_FAC); - } - else { - interp_v3_v3v3(h2, p3d_cur, p3d_prev, -BEZT_HANDLE_FAC); - } - - gp_stroke_to_bezier_add_point(gtd, bezt, p3d_cur, h1, h2, prev_bezt ? prev_bezt->vec[1] : p3d_cur, - do_gtd, gps->inittime, pt->time, width, rad_fac, minmax_weights); - - /* shift coord vects */ - copy_v3_v3(p3d_prev, p3d_cur); - copy_v3_v3(p3d_cur, p3d_next); - - if (i + 2 < tot) { - gp_strokepoint_convertcoords(C, gps, pt + 2, p3d_next, subrect); - } - - prev_bezt = bezt; - } - - if (add_end_point) { - float p[3]; - float dt = 0.0f; - - if (gps->totpoints > 1) { - interp_v3_v3v3(p, prev_bezt->vec[1], (prev_bezt - 1)->vec[1], -GAP_DFAC); - if (do_gtd) { - const int idx = gps->totpoints - 1; - dt = interpf(gps->points[idx - 1].time, gps->points[idx].time, -GAP_DFAC); - } - } - else { - copy_v3_v3(p, prev_bezt->vec[1]); - p[0] += GAP_DFAC; /* Rather arbitrary... */ - dt = GAP_DFAC; /* Rather arbitrary too! */ - } - - /* Second handle of last point of this stroke. */ - interp_v3_v3v3(h2, prev_bezt->vec[1], p, BEZT_HANDLE_FAC); - copy_v3_v3(prev_bezt->vec[2], h2); - - /* The end point */ - interp_v3_v3v3(h1, p, prev_bezt->vec[1], BEZT_HANDLE_FAC); - interp_v3_v3v3(h2, p, prev_bezt->vec[1], -BEZT_HANDLE_FAC); - /* Note bezt has already been incremented in main loop above, so it points to the right place. */ - gp_stroke_to_bezier_add_point(gtd, bezt, p, h1, h2, prev_bezt->vec[1], do_gtd, gps->inittime, dt, - 0.0f, rad_fac, minmax_weights); - } - - /* must calculate handles or else we crash */ - BKE_nurb_handles_calc(nu); - - if (!curnu || !*curnu) { - BLI_addtail(&cu->nurb, nu); - } - if (curnu) { - *curnu = nu; - } -} - -#undef GAP_DFAC -#undef WIDTH_CORR_FAC -#undef BEZT_HANDLE_FAC - -static void gp_stroke_finalize_curve_endpoints(Curve *cu) -{ - /* start */ - Nurb *nu = cu->nurb.first; - int i = 0; - if (nu->bezt) { - BezTriple *bezt = nu->bezt; - if (bezt) { - bezt[i].weight = bezt[i].radius = 0.0f; - } - } - else if (nu->bp) { - BPoint *bp = nu->bp; - if (bp) { - bp[i].weight = bp[i].radius = 0.0f; - } - } - - /* end */ - nu = cu->nurb.last; - i = nu->pntsu - 1; - if (nu->bezt) { - BezTriple *bezt = nu->bezt; - if (bezt) { - bezt[i].weight = bezt[i].radius = 0.0f; - } - } - else if (nu->bp) { - BPoint *bp = nu->bp; - if (bp) { - bp[i].weight = bp[i].radius = 0.0f; - } - } -} - -static void gp_stroke_norm_curve_weights(Curve *cu, const float minmax_weights[2]) -{ - Nurb *nu; - const float delta = minmax_weights[0]; - float fac; - int i; - - /* when delta == minmax_weights[0] == minmax_weights[1], we get div by zero [#35686] */ - if (IS_EQF(delta, minmax_weights[1])) - fac = 1.0f; - else - fac = 1.0f / (minmax_weights[1] - delta); - - for (nu = cu->nurb.first; nu; nu = nu->next) { - if (nu->bezt) { - BezTriple *bezt = nu->bezt; - for (i = 0; i < nu->pntsu; i++, bezt++) { - bezt->weight = (bezt->weight - delta) * fac; - } - } - else if (nu->bp) { - BPoint *bp = nu->bp; - for (i = 0; i < nu->pntsu; i++, bp++) { - bp->weight = (bp->weight - delta) * fac; - } - } - } -} - -static int gp_camera_view_subrect(bContext *C, rctf *subrect) -{ - View3D *v3d = CTX_wm_view3d(C); - ARegion *ar = CTX_wm_region(C); - - if (v3d) { - RegionView3D *rv3d = ar->regiondata; - - /* for camera view set the subrect */ - if (rv3d->persp == RV3D_CAMOB) { - Scene *scene = CTX_data_scene(C); - ED_view3d_calc_camera_border(scene, ar, v3d, rv3d, subrect, true); /* no shift */ - return 1; - } - } - - return 0; -} - -/* convert a given grease-pencil layer to a 3d-curve representation (using current view if appropriate) */ -static void gp_layer_to_curve(bContext *C, ReportList *reports, bGPdata *gpd, bGPDlayer *gpl, const int mode, - const bool norm_weights, const float rad_fac, const bool link_strokes, tGpTimingData *gtd) -{ - struct Main *bmain = CTX_data_main(C); - View3D *v3d = CTX_wm_view3d(C); /* may be NULL */ - Scene *scene = CTX_data_scene(C); - bGPDframe *gpf = gpencil_layer_getframe(gpl, CFRA, 0); - bGPDstroke *gps, *prev_gps = NULL; - Object *ob; - Curve *cu; - Nurb *nu = NULL; - Base *base_orig = BASACT, *base_new = NULL; - float minmax_weights[2] = {1.0f, 0.0f}; - - /* camera framing */ - rctf subrect, *subrect_ptr = NULL; - - /* error checking */ - if (ELEM(NULL, gpd, gpl, gpf)) - return; - - /* only convert if there are any strokes on this layer's frame to convert */ - if (BLI_listbase_is_empty(&gpf->strokes)) - return; - - /* initialize camera framing */ - if (gp_camera_view_subrect(C, &subrect)) { - subrect_ptr = &subrect; - } - - /* init the curve object (remove rotation and get curve data from it) - * - must clear transforms set on object, as those skew our results - */ - ob = BKE_object_add_only_object(bmain, OB_CURVE, gpl->info); - cu = ob->data = BKE_curve_add(bmain, gpl->info, OB_CURVE); - base_new = BKE_scene_base_add(scene, ob); - - cu->flag |= CU_3D; - - gtd->inittime = ((bGPDstroke *)gpf->strokes.first)->inittime; - - /* add points to curve */ - for (gps = gpf->strokes.first; gps; gps = gps->next) { - const bool add_start_point = (link_strokes && !(prev_gps)); - const bool add_end_point = (link_strokes && !(gps->next)); - - /* Detect new strokes created because of GP_STROKE_BUFFER_MAX reached, and stitch them to previous one. */ - bool stitch = false; - if (prev_gps) { - bGPDspoint *pt1 = &prev_gps->points[prev_gps->totpoints - 1]; - bGPDspoint *pt2 = &gps->points[0]; - - if ((pt1->x == pt2->x) && (pt1->y == pt2->y)) { - stitch = true; - } - } - - /* Decide whether we connect this stroke to previous one */ - if (!(stitch || link_strokes)) { - nu = NULL; - } - - switch (mode) { - case GP_STROKECONVERT_PATH: - gp_stroke_to_path(C, gpl, gps, cu, subrect_ptr, &nu, minmax_weights, rad_fac, stitch, - add_start_point, add_end_point, gtd); - break; - case GP_STROKECONVERT_CURVE: - case GP_STROKECONVERT_POLY: /* convert after */ - gp_stroke_to_bezier(C, gpl, gps, cu, subrect_ptr, &nu, minmax_weights, rad_fac, stitch, - add_start_point, add_end_point, gtd); - break; - default: - BLI_assert(!"invalid mode"); - break; - } - prev_gps = gps; - } - - /* If link_strokes, be sure first and last points have a zero weight/size! */ - if (link_strokes) { - gp_stroke_finalize_curve_endpoints(cu); - } - - /* Update curve's weights, if needed */ - if (norm_weights && ((minmax_weights[0] > 0.0f) || (minmax_weights[1] < 1.0f))) { - gp_stroke_norm_curve_weights(cu, minmax_weights); - } - - /* Create the path animation, if needed */ - gp_stroke_path_animation(C, reports, cu, gtd); - - if (mode == GP_STROKECONVERT_POLY) { - for (nu = cu->nurb.first; nu; nu = nu->next) { - BKE_nurb_type_convert(nu, CU_POLY, false); - } + else { + return OPERATOR_CANCELLED; } - - /* set the layer and select */ - base_new->lay = ob->lay = base_orig ? base_orig->lay : BKE_screen_view3d_layer_active(v3d, scene); - base_new->flag = ob->flag = base_new->flag | SELECT; } -/* --- */ -/* Check a GP layer has valid timing data! Else, most timing options are hidden in the operator. - * op may be NULL. - */ -static bool gp_convert_check_has_valid_timing(bContext *C, bGPDlayer *gpl, wmOperator *op) +static int gp_delete_exec(bContext *C, wmOperator *op) { - Scene *scene = CTX_data_scene(C); - bGPDframe *gpf = NULL; - bGPDstroke *gps = NULL; - bGPDspoint *pt; - double base_time, cur_time, prev_time = -1.0; - int i; - bool valid = true; - - if (!gpl || !(gpf = gpencil_layer_getframe(gpl, CFRA, 0)) || !(gps = gpf->strokes.first)) - return false; - - do { - base_time = cur_time = gps->inittime; - if (cur_time <= prev_time) { - valid = false; + eGP_DeleteMode mode = RNA_enum_get(op->ptr, "type"); + int result = OPERATOR_CANCELLED; + + switch (mode) { + case GP_DELETEOP_STROKES: /* selected strokes */ + result = gp_delete_selected_strokes(C); break; - } - - prev_time = cur_time; - for (i = 0, pt = gps->points; i < gps->totpoints; i++, pt++) { - cur_time = base_time + (double)pt->time; - /* First point of a stroke should have the same time as stroke's inittime, - * so it's the only case where equality is allowed! - */ - if ((i && cur_time <= prev_time) || (cur_time < prev_time)) { - valid = false; - break; - } - prev_time = cur_time; - } - - if (!valid) { + + case GP_DELETEOP_POINTS: /* selected points (breaks the stroke into segments) */ + result = gp_delete_selected_points(C); break; - } - } while ((gps = gps->next)); - if (op) { - RNA_boolean_set(op->ptr, "use_timing_data", valid); - } - return valid; -} - -/* Check end_frame is always > start frame! */ -static void gp_convert_set_end_frame(struct Main *UNUSED(main), struct Scene *UNUSED(scene), struct PointerRNA *ptr) -{ - int start_frame = RNA_int_get(ptr, "start_frame"); - int end_frame = RNA_int_get(ptr, "end_frame"); - - if (end_frame <= start_frame) { - RNA_int_set(ptr, "end_frame", start_frame + 1); + case GP_DELETEOP_FRAME: /* active frame */ + result = gp_actframe_delete_exec(C, op); + break; } + + return result; } -static int gp_convert_poll(bContext *C) +void GPENCIL_OT_delete(wmOperatorType *ot) { - bGPdata *gpd = ED_gpencil_data_get_active(C); - bGPDlayer *gpl = NULL; - bGPDframe *gpf = NULL; - ScrArea *sa = CTX_wm_area(C); - Scene *scene = CTX_data_scene(C); - - /* only if the current view is 3D View, if there's valid data (i.e. at least one stroke!), - * and if we are not in edit mode! - */ - return ((sa && sa->spacetype == SPACE_VIEW3D) && - (gpl = gpencil_layer_getactive(gpd)) && - (gpf = gpencil_layer_getframe(gpl, CFRA, 0)) && - (gpf->strokes.first) && - (scene->obedit == NULL)); -} - -static int gp_convert_layer_exec(bContext *C, wmOperator *op) -{ - PropertyRNA *prop = RNA_struct_find_property(op->ptr, "use_timing_data"); - bGPdata *gpd = ED_gpencil_data_get_active(C); - bGPDlayer *gpl = gpencil_layer_getactive(gpd); - Scene *scene = CTX_data_scene(C); - const int mode = RNA_enum_get(op->ptr, "type"); - const bool norm_weights = RNA_boolean_get(op->ptr, "use_normalize_weights"); - const float rad_fac = RNA_float_get(op->ptr, "radius_multiplier"); - const bool link_strokes = RNA_boolean_get(op->ptr, "use_link_strokes"); - bool valid_timing; - tGpTimingData gtd; - - /* check if there's data to work with */ - if (gpd == NULL) { - BKE_report(op->reports, RPT_ERROR, "No Grease Pencil data to work on"); - return OPERATOR_CANCELLED; - } - - if (!RNA_property_is_set(op->ptr, prop) && !gp_convert_check_has_valid_timing(C, gpl, op)) { - BKE_report(op->reports, RPT_WARNING, - "Current Grease Pencil strokes have no valid timing data, most timing options will be hidden!"); - } - valid_timing = RNA_property_boolean_get(op->ptr, prop); - - gtd.mode = RNA_enum_get(op->ptr, "timing_mode"); - /* Check for illegal timing mode! */ - if (!valid_timing && !ELEM(gtd.mode, GP_STROKECONVERT_TIMING_NONE, GP_STROKECONVERT_TIMING_LINEAR)) { - gtd.mode = GP_STROKECONVERT_TIMING_LINEAR; - RNA_enum_set(op->ptr, "timing_mode", gtd.mode); - } - if (!link_strokes) { - gtd.mode = GP_STROKECONVERT_TIMING_NONE; - } - - /* grab all relevant settings */ - gtd.frame_range = RNA_int_get(op->ptr, "frame_range"); - gtd.start_frame = RNA_int_get(op->ptr, "start_frame"); - gtd.realtime = valid_timing ? RNA_boolean_get(op->ptr, "use_realtime") : false; - gtd.end_frame = RNA_int_get(op->ptr, "end_frame"); - gtd.gap_duration = RNA_float_get(op->ptr, "gap_duration"); - gtd.gap_randomness = RNA_float_get(op->ptr, "gap_randomness"); - gtd.gap_randomness = min_ff(gtd.gap_randomness, gtd.gap_duration); - gtd.seed = RNA_int_get(op->ptr, "seed"); - gtd.num_points = gtd.cur_point = 0; - gtd.dists = gtd.times = NULL; - gtd.tot_dist = gtd.tot_time = gtd.gap_tot_time = 0.0f; - gtd.inittime = 0.0; - gtd.offset_time = 0.0f; - - /* perform conversion */ - gp_layer_to_curve(C, op->reports, gpd, gpl, mode, norm_weights, rad_fac, link_strokes, >d); - - /* free temp memory */ - if (gtd.dists) { - MEM_freeN(gtd.dists); - gtd.dists = NULL; - } - if (gtd.times) { - MEM_freeN(gtd.times); - gtd.times = NULL; - } - - /* notifiers */ - WM_event_add_notifier(C, NC_OBJECT | NA_ADDED, NULL); - WM_event_add_notifier(C, NC_SCENE | ND_OB_ACTIVE, scene); - - /* done */ - return OPERATOR_FINISHED; -} - -static bool gp_convert_draw_check_prop(PointerRNA *ptr, PropertyRNA *prop) -{ - const char *prop_id = RNA_property_identifier(prop); - const bool link_strokes = RNA_boolean_get(ptr, "use_link_strokes"); - int timing_mode = RNA_enum_get(ptr, "timing_mode"); - bool realtime = RNA_boolean_get(ptr, "use_realtime"); - float gap_duration = RNA_float_get(ptr, "gap_duration"); - float gap_randomness = RNA_float_get(ptr, "gap_randomness"); - const bool valid_timing = RNA_boolean_get(ptr, "use_timing_data"); - - /* Always show those props */ - if (strcmp(prop_id, "type") == 0 || - strcmp(prop_id, "use_normalize_weights") == 0 || - strcmp(prop_id, "radius_multiplier") == 0 || - strcmp(prop_id, "use_link_strokes") == 0) - { - return true; - } - - /* Never show this prop */ - if (strcmp(prop_id, "use_timing_data") == 0) - return false; - - if (link_strokes) { - /* Only show when link_stroke is true */ - if (strcmp(prop_id, "timing_mode") == 0) - return true; - - if (timing_mode != GP_STROKECONVERT_TIMING_NONE) { - /* Only show when link_stroke is true and stroke timing is enabled */ - if (strcmp(prop_id, "frame_range") == 0 || - strcmp(prop_id, "start_frame") == 0) - { - return true; - } - - /* Only show if we have valid timing data! */ - if (valid_timing && strcmp(prop_id, "use_realtime") == 0) - return true; - - /* Only show if realtime or valid_timing is false! */ - if ((!realtime || !valid_timing) && strcmp(prop_id, "end_frame") == 0) - return true; - - if (valid_timing && timing_mode == GP_STROKECONVERT_TIMING_CUSTOMGAP) { - /* Only show for custom gaps! */ - if (strcmp(prop_id, "gap_duration") == 0) - return true; - - /* Only show randomness for non-null custom gaps! */ - if (strcmp(prop_id, "gap_randomness") == 0 && (gap_duration > 0.0f)) - return true; - - /* Only show seed for randomize action! */ - if (strcmp(prop_id, "seed") == 0 && (gap_duration > 0.0f) && (gap_randomness > 0.0f)) - return true; - } - } - } - - /* Else, hidden! */ - return false; + static EnumPropertyItem prop_gpencil_delete_types[] = { + {GP_DELETEOP_POINTS, "POINTS", 0, "Points", "Delete selected points and split strokes into segments"}, + {GP_DELETEOP_STROKES, "STROKES", 0, "Strokes", "Delete selected strokes"}, + {GP_DELETEOP_FRAME, "FRAME", 0, "Frame", "Delete active frame"}, + {0, NULL, 0, NULL, NULL} + }; + + /* identifiers */ + ot->name = "Delete..."; + ot->idname = "GPENCIL_OT_delete"; + ot->description = "Delete selected Grease Pencil strokes, vertices, or frames"; + + /* callbacks */ + ot->invoke = WM_menu_invoke; + ot->exec = gp_delete_exec; + ot->poll = gp_stroke_edit_poll; + + /* flags */ + ot->flag = OPTYPE_UNDO | OPTYPE_REGISTER; + + /* props */ + ot->prop = RNA_def_enum(ot->srna, "type", prop_gpencil_delete_types, 0, "Type", "Method used for deleting Grease Pencil data"); } -static void gp_convert_ui(bContext *C, wmOperator *op) +static int gp_dissolve_exec(bContext *C, wmOperator *UNUSED(op)) { - uiLayout *layout = op->layout; - wmWindowManager *wm = CTX_wm_manager(C); - PointerRNA ptr; - - RNA_pointer_create(&wm->id, op->type->srna, op->properties, &ptr); - - /* Main auto-draw call */ - uiDefAutoButsRNA(layout, &ptr, gp_convert_draw_check_prop, '\0'); + return gp_dissolve_selected_points(C); } -void GPENCIL_OT_convert(wmOperatorType *ot) +void GPENCIL_OT_dissolve(wmOperatorType *ot) { - PropertyRNA *prop; - /* identifiers */ - ot->name = "Convert Grease Pencil"; - ot->idname = "GPENCIL_OT_convert"; - ot->description = "Convert the active Grease Pencil layer to a new Curve Object"; + ot->name = "Dissolve"; + ot->idname = "GPENCIL_OT_dissolve"; + ot->description = "Delete selected points without splitting strokes"; /* callbacks */ - ot->invoke = WM_menu_invoke; - ot->exec = gp_convert_layer_exec; - ot->poll = gp_convert_poll; - ot->ui = gp_convert_ui; + ot->exec = gp_dissolve_exec; + ot->poll = gp_stroke_edit_poll; /* flags */ - ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO; - - /* properties */ - ot->prop = RNA_def_enum(ot->srna, "type", prop_gpencil_convertmodes, 0, "Type", "Which type of curve to convert to"); - - RNA_def_boolean(ot->srna, "use_normalize_weights", true, "Normalize Weight", - "Normalize weight (set from stroke width)"); - RNA_def_float(ot->srna, "radius_multiplier", 1.0f, 0.0f, 1000.0f, "Radius Fac", - "Multiplier for the points' radii (set from stroke width)", 0.0f, 10.0f); - RNA_def_boolean(ot->srna, "use_link_strokes", true, "Link Strokes", - "Whether to link strokes with zero-radius sections of curves"); - - prop = RNA_def_enum(ot->srna, "timing_mode", prop_gpencil_convert_timingmodes, GP_STROKECONVERT_TIMING_FULL, - "Timing Mode", "How to use timing data stored in strokes"); - RNA_def_enum_funcs(prop, rna_GPConvert_mode_items); - - RNA_def_int(ot->srna, "frame_range", 100, 1, 10000, "Frame Range", - "The duration of evaluation of the path control curve", 1, 1000); - RNA_def_int(ot->srna, "start_frame", 1, 1, 100000, "Start Frame", - "The start frame of the path control curve", 1, 100000); - RNA_def_boolean(ot->srna, "use_realtime", false, "Realtime", - "Whether the path control curve reproduces the drawing in realtime, starting from Start Frame"); - prop = RNA_def_int(ot->srna, "end_frame", 250, 1, 100000, "End Frame", - "The end frame of the path control curve (if Realtime is not set)", 1, 100000); - RNA_def_property_update_runtime(prop, gp_convert_set_end_frame); - - RNA_def_float(ot->srna, "gap_duration", 0.0f, 0.0f, 10000.0f, "Gap Duration", - "Custom Gap mode: (Average) length of gaps, in frames " - "(Note: Realtime value, will be scaled if Realtime is not set)", 0.0f, 1000.0f); - RNA_def_float(ot->srna, "gap_randomness", 0.0f, 0.0f, 10000.0f, "Gap Randomness", - "Custom Gap mode: Number of frames that gap lengths can vary", 0.0f, 1000.0f); - RNA_def_int(ot->srna, "seed", 0, 0, 1000, "Random Seed", - "Custom Gap mode: Random generator seed", 0, 100); - - /* Note: Internal use, this one will always be hidden by UI code... */ - prop = RNA_def_boolean(ot->srna, "use_timing_data", false, "Has Valid Timing", - "Whether the converted Grease Pencil layer has valid timing data (internal use)"); - RNA_def_property_flag(prop, PROP_SKIP_SAVE); + ot->flag = OPTYPE_UNDO | OPTYPE_REGISTER; } /* ************************************************ */ diff --git a/source/blender/editors/gpencil/gpencil_intern.h b/source/blender/editors/gpencil/gpencil_intern.h index 28eb1355caf..290935a06cf 100644 --- a/source/blender/editors/gpencil/gpencil_intern.h +++ b/source/blender/editors/gpencil/gpencil_intern.h @@ -31,15 +31,80 @@ #ifndef __GPENCIL_INTERN_H__ #define __GPENCIL_INTERN_H__ -/* internal exports only */ +#include "DNA_vec_types.h" -/* ***************************************************** */ -/* Operator Defines */ +/* internal exports only */ struct bGPdata; +struct bGPDstroke; +struct bGPDspoint; + +struct ARegion; +struct View2D; struct wmOperatorType; + +/* ***************************************************** */ +/* Internal API */ + +/* Stroke Coordinates API ------------------------------ */ +/* gpencil_utils.c */ + +typedef struct GP_SpaceConversion { + struct bGPdata *gpd; + struct bGPDlayer *gpl; + + struct ScrArea *sa; + struct ARegion *ar; + struct View2D *v2d; + + rctf *subrect; /* for using the camera rect within the 3d view */ + rctf subrect_data; + + float mat[4][4]; /* transform matrix on the strokes (introduced in [b770964]) */ +} GP_SpaceConversion; + + +/** + * Check whether a given stroke segment is inside a circular brush + * + * \param mval The current screen-space coordinates (midpoint) of the brush + * \param mvalo The previous screen-space coordinates (midpoint) of the brush (NOT CURRENTLY USED) + * \param rad The radius of the brush + * + * \param x0, y0 The screen-space x and y coordinates of the start of the stroke segment + * \param x1, y1 The screen-space x and y coordinates of the end of the stroke segment + */ +bool gp_stroke_inside_circle(const int mval[2], const int UNUSED(mvalo[2]), + int rad, int x0, int y0, int x1, int y1); + + +/** + * Init settings for stroke point space conversions + * + * \param[out] r_gsc The space conversion settings struct, populated with necessary params + */ +void gp_point_conversion_init(struct bContext *C, GP_SpaceConversion *r_gsc); + +/** + * Convert a Grease Pencil coordinate (i.e. can be 2D or 3D) to screenspace (2D) + * + * \param[out] r_x The screen-space x-coordinate of the point + * \param[out] r_y The screen-space y-coordinate of the point + */ +void gp_point_to_xy(GP_SpaceConversion *settings, struct bGPDstroke *gps, struct bGPDspoint *pt, + int *r_x, int *r_y); + +/* Poll Callbacks ------------------------------------ */ +/* gpencil_utils.c */ + +int gp_add_poll(struct bContext *C); +int gp_active_layer_poll(struct bContext *C); + +/* ***************************************************** */ +/* Operator Defines */ + /* drawing ---------- */ void GPENCIL_OT_draw(struct wmOperatorType *ot); @@ -52,12 +117,36 @@ typedef enum eGPencil_PaintModes { GP_PAINTMODE_DRAW_POLY } eGPencil_PaintModes; +/* stroke editing ----- */ + +void GPENCIL_OT_select(struct wmOperatorType *ot); +void GPENCIL_OT_select_all(struct wmOperatorType *ot); +void GPENCIL_OT_select_circle(struct wmOperatorType *ot); +void GPENCIL_OT_select_border(struct wmOperatorType *ot); +void GPENCIL_OT_select_lasso(struct wmOperatorType *ot); + +void GPENCIL_OT_select_linked(struct wmOperatorType *ot); +void GPENCIL_OT_select_more(struct wmOperatorType *ot); +void GPENCIL_OT_select_less(struct wmOperatorType *ot); + +void GPENCIL_OT_duplicate(struct wmOperatorType *ot); +void GPENCIL_OT_delete(struct wmOperatorType *ot); +void GPENCIL_OT_dissolve(struct wmOperatorType *ot); +void GPENCIL_OT_copy(struct wmOperatorType *ot); +void GPENCIL_OT_paste(struct wmOperatorType *ot); + /* buttons editing --- */ void GPENCIL_OT_data_add(struct wmOperatorType *ot); void GPENCIL_OT_data_unlink(struct wmOperatorType *ot); void GPENCIL_OT_layer_add(struct wmOperatorType *ot); +void GPENCIL_OT_layer_remove(struct wmOperatorType *ot); +void GPENCIL_OT_layer_move(struct wmOperatorType *ot); +void GPENCIL_OT_layer_duplicate(struct wmOperatorType *ot); + +void GPENCIL_OT_hide(struct wmOperatorType *ot); +void GPENCIL_OT_reveal(struct wmOperatorType *ot); void GPENCIL_OT_active_frame_delete(struct wmOperatorType *ot); @@ -76,17 +165,17 @@ void gpencil_undo_finish(void); /* This struct defines a structure used for quick access */ typedef struct bActListElem { struct bActListElem *next, *prev; - + void *data; /* source data this elem represents */ int type; /* one of the ACTTYPE_* values */ int flag; /* copy of elem's flags for quick access */ int index; /* copy of adrcode where applicable */ - + void *key_data; /* motion data - ipo or ipo-curve */ short datatype; /* type of motion data to expect */ - + struct bActionGroup *grp; /* action group that owns the channel */ - + void *owner; /* will either be an action channel or fake ipo-channel (for keys) */ short ownertype; /* type of owner */ } bActListElem; diff --git a/source/blender/editors/gpencil/gpencil_ops.c b/source/blender/editors/gpencil/gpencil_ops.c index 75ff0895931..ab56565f4ca 100644 --- a/source/blender/editors/gpencil/gpencil_ops.c +++ b/source/blender/editors/gpencil/gpencil_ops.c @@ -34,7 +34,9 @@ #include "BLI_sys_types.h" -#include "BLI_blenlib.h" +#include "BKE_context.h" + +#include "DNA_gpencil_types.h" #include "WM_api.h" #include "WM_types.h" @@ -42,34 +44,199 @@ #include "RNA_access.h" #include "ED_gpencil.h" +#include "ED_object.h" +#include "ED_transform.h" #include "gpencil_intern.h" /* ****************************************** */ -/* Generic Editing Keymap */ +/* Grease Pencil Keymaps */ -void ED_keymap_gpencil(wmKeyConfig *keyconf) +/* Generic Drawing Keymap */ +static void ed_keymap_gpencil_general(wmKeyConfig *keyconf) { wmKeyMap *keymap = WM_keymap_find(keyconf, "Grease Pencil", 0, 0); wmKeyMapItem *kmi; - /* Draw */ - + /* Draw --------------------------------------- */ /* draw */ kmi = WM_keymap_add_item(keymap, "GPENCIL_OT_draw", LEFTMOUSE, KM_PRESS, 0, DKEY); RNA_enum_set(kmi->ptr, "mode", GP_PAINTMODE_DRAW); - + RNA_boolean_set(kmi->ptr, "wait_for_input", false); + /* draw - straight lines */ kmi = WM_keymap_add_item(keymap, "GPENCIL_OT_draw", LEFTMOUSE, KM_PRESS, KM_CTRL, DKEY); RNA_enum_set(kmi->ptr, "mode", GP_PAINTMODE_DRAW_STRAIGHT); - + RNA_boolean_set(kmi->ptr, "wait_for_input", false); + /* draw - poly lines */ kmi = WM_keymap_add_item(keymap, "GPENCIL_OT_draw", RIGHTMOUSE, KM_PRESS, KM_CTRL, DKEY); RNA_enum_set(kmi->ptr, "mode", GP_PAINTMODE_DRAW_POLY); - + RNA_boolean_set(kmi->ptr, "wait_for_input", false); + /* erase */ kmi = WM_keymap_add_item(keymap, "GPENCIL_OT_draw", RIGHTMOUSE, KM_PRESS, 0, DKEY); RNA_enum_set(kmi->ptr, "mode", GP_PAINTMODE_ERASER); + RNA_boolean_set(kmi->ptr, "wait_for_input", false); + + /* Viewport Tools ------------------------------- */ + + /* Enter EditMode */ + kmi = WM_keymap_add_item(keymap, "WM_OT_context_toggle", TABKEY, KM_PRESS, 0, DKEY); + RNA_string_set(kmi->ptr, "data_path", "gpencil_data.use_stroke_edit_mode"); + + /* Pie Menu - For standard tools */ + WM_keymap_add_menu_pie(keymap, "GPENCIL_PIE_tool_palette", QKEY, KM_PRESS, 0, DKEY); + WM_keymap_add_menu_pie(keymap, "GPENCIL_PIE_settings_palette", WKEY, KM_PRESS, 0, DKEY); +} + +/* ==================== */ + +/* Poll callback for stroke editing mode */ +static int gp_stroke_editmode_poll(bContext *C) +{ + bGPdata *gpd = CTX_data_gpencil_data(C); + return (gpd && (gpd->flag & GP_DATA_STROKE_EDITMODE)); +} + +/* Stroke Editing Keymap - Only when editmode is enabled */ +static void ed_keymap_gpencil_editing(wmKeyConfig *keyconf) +{ + wmKeyMap *keymap = WM_keymap_find(keyconf, "Grease Pencil Stroke Edit Mode", 0, 0); + wmKeyMapItem *kmi; + + /* set poll callback - so that this keymap only gets enabled when stroke editmode is enabled */ + keymap->poll = gp_stroke_editmode_poll; + + /* ----------------------------------------------- */ + + /* Exit EditMode */ + kmi = WM_keymap_add_item(keymap, "WM_OT_context_toggle", TABKEY, KM_PRESS, 0, 0); + RNA_string_set(kmi->ptr, "data_path", "gpencil_data.use_stroke_edit_mode"); + + /* Brush Settings */ + /* NOTE: We cannot expose these in the standard keymap, as they will interfere with regular hotkeys + * in other modes. However, when we are dealing with Stroke Edit Mode, we know for certain + * that the only data being edited is that of the Grease Pencil strokes + */ + + /* FKEY = Eraser Radius */ + kmi = WM_keymap_add_item(keymap, "WM_OT_radial_control", FKEY, KM_PRESS, 0, 0); + RNA_string_set(kmi->ptr, "data_path_primary", "user_preferences.edit.grease_pencil_eraser_radius"); + + /* Selection ------------------------------------- */ + /* select all */ + kmi = WM_keymap_add_item(keymap, "GPENCIL_OT_select_all", AKEY, KM_PRESS, 0, 0); + RNA_enum_set(kmi->ptr, "action", SEL_TOGGLE); + + kmi = WM_keymap_add_item(keymap, "GPENCIL_OT_select_all", IKEY, KM_PRESS, KM_CTRL, 0); + RNA_enum_set(kmi->ptr, "action", SEL_INVERT); + + /* circle select */ + WM_keymap_add_item(keymap, "GPENCIL_OT_select_circle", CKEY, KM_PRESS, 0, 0); + + /* border select */ + WM_keymap_add_item(keymap, "GPENCIL_OT_select_border", BKEY, KM_PRESS, 0, 0); + + /* lasso select */ + kmi = WM_keymap_add_item(keymap, "GPENCIL_OT_select_lasso", EVT_TWEAK_A, KM_ANY, KM_CTRL, 0); + RNA_boolean_set(kmi->ptr, "deselect", false); + kmi = WM_keymap_add_item(keymap, "GPENCIL_OT_select_lasso", EVT_TWEAK_A, KM_ANY, KM_SHIFT | KM_CTRL, 0); + RNA_boolean_set(kmi->ptr, "deselect", true); + + /* normal select */ + WM_keymap_add_item(keymap, "GPENCIL_OT_select", SELECTMOUSE, KM_PRESS, 0, 0); + + kmi = WM_keymap_add_item(keymap, "GPENCIL_OT_select", SELECTMOUSE, KM_PRESS, KM_SHIFT, 0); + RNA_boolean_set(kmi->ptr, "extend", true); + RNA_boolean_set(kmi->ptr, "toggle", true); + + /* whole stroke select */ + kmi = WM_keymap_add_item(keymap, "GPENCIL_OT_select", SELECTMOUSE, KM_PRESS, KM_ALT, 0); + RNA_boolean_set(kmi->ptr, "entire_strokes", true); + + /* select linked */ + /* NOTE: While LKEY is redundant, not having it breaks the mode illusion too much */ + WM_keymap_add_item(keymap, "GPENCIL_OT_select_linked", LKEY, KM_PRESS, 0, 0); + WM_keymap_add_item(keymap, "GPENCIL_OT_select_linked", LKEY, KM_PRESS, KM_CTRL, 0); + + /* select more/less */ + WM_keymap_add_item(keymap, "GPENCIL_OT_select_more", PADPLUSKEY, KM_PRESS, KM_CTRL, 0); + WM_keymap_add_item(keymap, "GPENCIL_OT_select_less", PADMINUS, KM_PRESS, KM_CTRL, 0); + + + /* Editing ----------------------------------------- */ + + /* duplicate and move selected points */ + WM_keymap_add_item(keymap, "GPENCIL_OT_duplicate_move", DKEY, KM_PRESS, KM_SHIFT, 0); + + /* delete */ + WM_keymap_add_menu(keymap, "VIEW3D_MT_edit_gpencil_delete", XKEY, KM_PRESS, 0, 0); + WM_keymap_add_menu(keymap, "VIEW3D_MT_edit_gpencil_delete", DELKEY, KM_PRESS, 0, 0); + + WM_keymap_add_item(keymap, "GPENCIL_OT_dissolve", XKEY, KM_PRESS, KM_CTRL, 0); + WM_keymap_add_item(keymap, "GPENCIL_OT_dissolve", DELKEY, KM_PRESS, KM_CTRL, 0); + + /* copy + paste */ + WM_keymap_add_item(keymap, "GPENCIL_OT_copy", CKEY, KM_PRESS, KM_CTRL, 0); + WM_keymap_add_item(keymap, "GPENCIL_OT_paste", VKEY, KM_PRESS, KM_CTRL, 0); + +#ifdef __APPLE__ + WM_keymap_add_item(keymap, "GPENCIL_OT_copy", CKEY, KM_PRESS, KM_OSKEY, 0); + WM_keymap_add_item(keymap, "GPENCIL_OT_paste", VKEY, KM_PRESS, KM_OSKEY, 0); +#endif + + /* Show/Hide */ + /* NOTE: These are available only in EditMode now, since they clash with general-purpose hotkeys */ + WM_keymap_add_item(keymap, "GPENCIL_OT_reveal", HKEY, KM_PRESS, KM_ALT, 0); + + kmi = WM_keymap_add_item(keymap, "GPENCIL_OT_hide", HKEY, KM_PRESS, 0, 0); + RNA_boolean_set(kmi->ptr, "unselected", false); + + kmi = WM_keymap_add_item(keymap, "GPENCIL_OT_hide", HKEY, KM_PRESS, KM_SHIFT, 0); + RNA_boolean_set(kmi->ptr, "unselected", true); + + + /* Transform Tools */ + kmi = WM_keymap_add_item(keymap, "TRANSFORM_OT_translate", GKEY, KM_PRESS, 0, 0); + RNA_boolean_set(kmi->ptr, "gpencil_strokes", true); + + kmi = WM_keymap_add_item(keymap, "TRANSFORM_OT_translate", EVT_TWEAK_S, KM_ANY, 0, 0); + RNA_boolean_set(kmi->ptr, "gpencil_strokes", true); + + kmi = WM_keymap_add_item(keymap, "TRANSFORM_OT_rotate", RKEY, KM_PRESS, 0, 0); + RNA_boolean_set(kmi->ptr, "gpencil_strokes", true); + + kmi = WM_keymap_add_item(keymap, "TRANSFORM_OT_resize", SKEY, KM_PRESS, 0, 0); + RNA_boolean_set(kmi->ptr, "gpencil_strokes", true); + + kmi = WM_keymap_add_item(keymap, "TRANSFORM_OT_mirror", MKEY, KM_PRESS, KM_CTRL, 0); + RNA_boolean_set(kmi->ptr, "gpencil_strokes", true); + + kmi = WM_keymap_add_item(keymap, "TRANSFORM_OT_bend", WKEY, KM_PRESS, KM_SHIFT, 0); + RNA_boolean_set(kmi->ptr, "gpencil_strokes", true); + + WM_keymap_add_item(keymap, "TRANSFORM_OT_tosphere", SKEY, KM_PRESS, KM_ALT | KM_SHIFT, 0); + RNA_boolean_set(kmi->ptr, "gpencil_strokes", true); + + WM_keymap_add_item(keymap, "TRANSFORM_OT_shear", SKEY, KM_PRESS, KM_ALT | KM_CTRL | KM_SHIFT, 0); + RNA_boolean_set(kmi->ptr, "gpencil_strokes", true); + + kmi = WM_keymap_add_item(keymap, "TRANSFORM_OT_transform", SKEY, KM_PRESS, KM_ALT, 0); + RNA_enum_set(kmi->ptr, "mode", TFM_GPENCIL_SHRINKFATTEN); + RNA_boolean_set(kmi->ptr, "gpencil_strokes", true); + + /* Proportional Editing */ + ED_keymap_proportional_cycle(keyconf, keymap); + ED_keymap_proportional_editmode(keyconf, keymap, true); +} + +/* ==================== */ + +void ED_keymap_gpencil(wmKeyConfig *keyconf) +{ + ed_keymap_gpencil_general(keyconf); + ed_keymap_gpencil_editing(keyconf); } /* ****************************************** */ @@ -80,18 +247,55 @@ void ED_operatortypes_gpencil(void) WM_operatortype_append(GPENCIL_OT_draw); + /* Editing (Strokes) ------------ */ + + WM_operatortype_append(GPENCIL_OT_select); + WM_operatortype_append(GPENCIL_OT_select_all); + WM_operatortype_append(GPENCIL_OT_select_circle); + WM_operatortype_append(GPENCIL_OT_select_border); + WM_operatortype_append(GPENCIL_OT_select_lasso); + + WM_operatortype_append(GPENCIL_OT_select_linked); + WM_operatortype_append(GPENCIL_OT_select_more); + WM_operatortype_append(GPENCIL_OT_select_less); + + WM_operatortype_append(GPENCIL_OT_duplicate); + WM_operatortype_append(GPENCIL_OT_delete); + WM_operatortype_append(GPENCIL_OT_dissolve); + WM_operatortype_append(GPENCIL_OT_copy); + WM_operatortype_append(GPENCIL_OT_paste); + /* Editing (Buttons) ------------ */ WM_operatortype_append(GPENCIL_OT_data_add); WM_operatortype_append(GPENCIL_OT_data_unlink); WM_operatortype_append(GPENCIL_OT_layer_add); + WM_operatortype_append(GPENCIL_OT_layer_remove); + WM_operatortype_append(GPENCIL_OT_layer_move); + WM_operatortype_append(GPENCIL_OT_layer_duplicate); + + WM_operatortype_append(GPENCIL_OT_hide); + WM_operatortype_append(GPENCIL_OT_reveal); WM_operatortype_append(GPENCIL_OT_active_frame_delete); WM_operatortype_append(GPENCIL_OT_convert); - + /* Editing (Time) --------------- */ } +void ED_operatormacros_gpencil(void) +{ + wmOperatorType *ot; + wmOperatorTypeMacro *otmacro; + + ot = WM_operatortype_append_macro("GPENCIL_OT_duplicate_move", "Duplicate Strokes", + "Make copies of the selected Grease Pencil strokes and move them", + OPTYPE_UNDO | OPTYPE_REGISTER); + WM_operatortype_macro_define(ot, "GPENCIL_OT_duplicate"); + otmacro = WM_operatortype_macro_define(ot, "TRANSFORM_OT_translate"); + RNA_boolean_set(otmacro->ptr, "gpencil_strokes", true); +} + /* ****************************************** */ diff --git a/source/blender/editors/gpencil/gpencil_paint.c b/source/blender/editors/gpencil/gpencil_paint.c index afecdd91599..df88da073ca 100644 --- a/source/blender/editors/gpencil/gpencil_paint.c +++ b/source/blender/editors/gpencil/gpencil_paint.c @@ -27,6 +27,7 @@ * \ingroup edgpencil */ + #include <stdio.h> #include <stddef.h> #include <stdlib.h> @@ -39,7 +40,7 @@ #include "BLI_math.h" #include "BLI_utildefines.h" -#include "BLF_translation.h" +#include "BLT_translation.h" #include "PIL_time.h" @@ -47,6 +48,7 @@ #include "BKE_context.h" #include "BKE_global.h" #include "BKE_report.h" +#include "BKE_screen.h" #include "BKE_tracking.h" #include "DNA_object_types.h" @@ -86,11 +88,13 @@ typedef struct tGPsdata { rctf *subrect; /* for using the camera rect within the 3d view */ rctf subrect_data; + GP_SpaceConversion gsc; /* settings to pass to gp_points_to_xy() */ + PointerRNA ownerPtr; /* pointer to owner of gp-datablock */ bGPdata *gpd; /* gp-datablock layer comes from */ bGPDlayer *gpl; /* layer we're working on */ bGPDframe *gpf; /* frame we're working on */ - + short status; /* current status of painting */ short paintmode; /* mode for painting */ @@ -110,9 +114,10 @@ typedef struct tGPsdata { double inittime; /* Used when converting to path */ double curtime; /* Used when converting to path */ double ocurtime; /* Used when converting to path */ - + float imat[4][4]; /* inverted transformation matrix applying when converting coords from screen-space * to region space */ + float mat[4][4]; float custom_color[4]; /* custom color - hack for enforcing a particular color for track/mask editing */ @@ -218,7 +223,7 @@ static void gp_get_3d_reference(tGPsdata *p, float vec[3]) if (p->ownerPtr.type == &RNA_Object) { Object *ob = (Object *)p->ownerPtr.data; - /* active Object + /* active Object * - use relative distance of 3D-cursor from object center */ sub_v3_v3v3(vec, fp, ob->loc); @@ -243,13 +248,13 @@ static bool gp_stroke_filtermval(tGPsdata *p, const int mval[2], int pmval[2]) if (p->gpd->sbuffer_size == 0) return true; - /* check if mouse moved at least certain distance on both axes (best case) + /* check if mouse moved at least certain distance on both axes (best case) * - aims to eliminate some jitter-noise from input when trying to draw straight lines freehand */ else if ((dx > MIN_MANHATTEN_PX) && (dy > MIN_MANHATTEN_PX)) return true; - /* check if the distance since the last point is significant enough + /* check if the distance since the last point is significant enough * - prevents points being added too densely * - distance here doesn't use sqrt to prevent slowness... we should still be safe from overflows though */ @@ -279,7 +284,7 @@ static void gp_stroke_convertcoords(tGPsdata *p, const int mval[2], float out[3] float rvec[3], dvec[3]; float mval_f[2] = {UNPACK2(mval)}; float zfac; - + /* Current method just converts each point in screen-coordinates to * 3D-coordinates using the 3D-cursor as reference. In general, this * works OK, but it could of course be improved. @@ -291,7 +296,7 @@ static void gp_stroke_convertcoords(tGPsdata *p, const int mval[2], float out[3] gp_get_3d_reference(p, rvec); zfac = ED_view3d_calc_zfac(p->ar->regiondata, rvec, NULL); - + if (ED_view3d_project_float_global(p->ar, rvec, mval_prj, V3D_PROJ_TEST_NOP) == V3D_PROJ_RET_OK) { sub_v2_v2v2(mval_f, mval_prj, mval_f); ED_view3d_win_to_delta(p->ar, mval_f, dvec, zfac); @@ -327,7 +332,7 @@ static short gp_stroke_addpoint(tGPsdata *p, const int mval[2], float pressure, { bGPdata *gpd = p->gpd; tGPspoint *pt; - + /* check painting mode */ if (p->paintmode == GP_PAINTMODE_DRAW_STRAIGHT) { /* straight lines only - i.e. only store start and end point in buffer */ @@ -337,29 +342,25 @@ static short gp_stroke_addpoint(tGPsdata *p, const int mval[2], float pressure, /* store settings */ copy_v2_v2_int(&pt->x, mval); - pt->pressure = pressure; + pt->pressure = 1.0f; /* T44932 - Pressure vals are unreliable, so ignore for now */ pt->time = (float)(curtime - p->inittime); /* increment buffer size */ gpd->sbuffer_size++; } else { - /* normally, we just reset the endpoint to the latest value + /* just reset the endpoint to the latest value * - assume that pointers for this are always valid... */ pt = ((tGPspoint *)(gpd->sbuffer) + 1); /* store settings */ copy_v2_v2_int(&pt->x, mval); - pt->pressure = pressure; + pt->pressure = 1.0f; /* T44932 - Pressure vals are unreliable, so ignore for now */ pt->time = (float)(curtime - p->inittime); - /* if this is just the second point we've added, increment the buffer size - * so that it will be drawn properly... - * otherwise, just leave it alone, otherwise we get problems - */ - if (gpd->sbuffer_size != 2) - gpd->sbuffer_size = 2; + /* now the buffer has 2 points (and shouldn't be allowed to get any larger) */ + gpd->sbuffer_size = 2; } /* can keep carrying on this way :) */ @@ -393,7 +394,7 @@ static short gp_stroke_addpoint(tGPsdata *p, const int mval[2], float pressure, /* store settings */ copy_v2_v2_int(&pt->x, mval); - pt->pressure = pressure; + pt->pressure = 1.0f; /* T44932 - Pressure vals are unreliable, so ignore for now */ pt->time = (float)(curtime - p->inittime); /* if there's stroke for this poly line session add (or replace last) point @@ -438,7 +439,7 @@ static short gp_stroke_addpoint(tGPsdata *p, const int mval[2], float pressure, return GP_STROKEADD_NORMAL; } - + /* return invalid state for now... */ return GP_STROKEADD_INVALID; } @@ -458,12 +459,12 @@ static void gp_stroke_smooth(tGPsdata *p) if ((cmx <= 2) || (gpd->sbuffer == NULL)) return; - /* Calculate smoothing coordinates using weighted-averages + /* Calculate smoothing coordinates using weighted-averages * WARNING: we do NOT smooth first and last points (to avoid shrinkage) */ spt = (tGPspoint *)gpd->sbuffer; - /* This (tmp_spt) small array stores the last two points' original coordinates, + /* This (tmp_spt) small array stores the last two points' original coordinates, * as we don't want to use already averaged ones! It is used as a cyclic buffer... */ tmp_spt[0] = *spt; @@ -482,7 +483,7 @@ static void gp_stroke_smooth(tGPsdata *p) } } -/* simplify a stroke (in buffer) before storing it +/* simplify a stroke (in buffer) before storing it * - applies a reverse Chaikin filter * - code adapted from etch-a-ton branch (editarmature_sketch.c) */ @@ -502,7 +503,7 @@ static void gp_stroke_simplify(tGPsdata *p) if ((num_points <= 4) || (old_points == NULL)) return; - /* clear buffer (but don't free mem yet) so that we can write to it + /* clear buffer (but don't free mem yet) so that we can write to it * - firstly set sbuffer to NULL, so a new one is allocated * - secondly, reset flag after, as it gets cleared auto */ @@ -569,7 +570,7 @@ static void gp_stroke_newfrombuffer(tGPsdata *p) /* since strokes are so fine, when using their depth we need a margin otherwise they might get missed */ int depth_margin = (p->gpd->flag & GP_DATA_DEPTH_STROKE) ? 4 : 0; - /* get total number of points to allocate space for + /* get total number of points to allocate space for * - drawing straight-lines only requires the endpoints */ if (p->paintmode == GP_PAINTMODE_DRAW_STRAIGHT) @@ -625,7 +626,7 @@ static void gp_stroke_newfrombuffer(tGPsdata *p) pt++; } - + if (totelem == 2) { /* last point if applicable */ ptc = ((tGPspoint *)gpd->sbuffer) + (gpd->sbuffer_size - 1); @@ -746,7 +747,7 @@ static short gp_stroke_eraser_splitdel(bGPDframe *gpf, bGPDstroke *gps, int i) /* free stroke points, then stroke */ MEM_freeN(pt_tmp); BLI_freelinkN(&gpf->strokes, gps); - + /* nothing left in stroke, so stop */ return 1; } @@ -755,7 +756,7 @@ static short gp_stroke_eraser_splitdel(bGPDframe *gpf, bGPDstroke *gps, int i) else if (i == gps->totpoints - 2) { /* allocate new points array, and assign most of the old stroke there */ gps->totpoints--; - gps->points = MEM_callocN(sizeof(bGPDspoint) * gps->totpoints, "gp_stroke_points"); + gps->points = MEM_mallocN(sizeof(bGPDspoint) * gps->totpoints, "gp_stroke_points"); memcpy(gps->points, pt_tmp, sizeof(bGPDspoint) * gps->totpoints); /* free temp buffer */ @@ -769,7 +770,7 @@ static short gp_stroke_eraser_splitdel(bGPDframe *gpf, bGPDstroke *gps, int i) else if (i == 0) { /* allocate new points array, and assign most of the old stroke there */ gps->totpoints--; - gps->points = MEM_callocN(sizeof(bGPDspoint) * gps->totpoints, "gp_stroke_points"); + gps->points = MEM_mallocN(sizeof(bGPDspoint) * gps->totpoints, "gp_stroke_points"); memcpy(gps->points, pt_tmp + 1, sizeof(bGPDspoint) * gps->totpoints); /* We must adjust timings! @@ -806,7 +807,7 @@ static short gp_stroke_eraser_splitdel(bGPDframe *gpf, bGPDstroke *gps, int i) BLI_insertlinkafter(&gpf->strokes, gps, gsn); gsn->totpoints = gps->totpoints - i; - gsn->points = MEM_callocN(sizeof(bGPDspoint) * gsn->totpoints, "gp_stroke_points"); + gsn->points = MEM_mallocN(sizeof(bGPDspoint) * gsn->totpoints, "gp_stroke_points"); memcpy(gsn->points, pt_tmp + i, sizeof(bGPDspoint) * gsn->totpoints); /* We must adjust timings of this new stroke! @@ -830,8 +831,8 @@ static short gp_stroke_eraser_splitdel(bGPDframe *gpf, bGPDstroke *gps, int i) /* adjust existing stroke */ gps->totpoints = i; - gps->points = MEM_callocN(sizeof(bGPDspoint) * gps->totpoints, "gp_stroke_points"); - memcpy(gps->points, pt_tmp, sizeof(bGPDspoint) * i); + gps->points = MEM_mallocN(sizeof(bGPDspoint) * gps->totpoints, "gp_stroke_points"); + memcpy(gps->points, pt_tmp, sizeof(bGPDspoint) * gps->totpoints); /* free temp buffer */ MEM_freeN(pt_tmp); @@ -852,8 +853,7 @@ static float view3d_point_depth(const RegionView3D *rv3d, const float co[3]) } } -static bool gp_stroke_eraser_is_occluded(tGPsdata *p, - const bGPDspoint *pt, const int x, const int y) +static bool gp_stroke_eraser_is_occluded(tGPsdata *p, const bGPDspoint *pt, const int x, const int y) { if ((p->sa->spacetype == SPACE_VIEW3D) && (p->flags & GP_PAINTFLAG_V3D_ERASER_DEPTH)) @@ -861,11 +861,11 @@ static bool gp_stroke_eraser_is_occluded(tGPsdata *p, RegionView3D *rv3d = p->ar->regiondata; const int mval[2] = {x, y}; float mval_3d[3]; - + if (ED_view3d_autodist_simple(p->ar, mval, mval_3d, 0, NULL)) { const float depth_mval = view3d_point_depth(rv3d, mval_3d); const float depth_pt = view3d_point_depth(rv3d, &pt->x); - + if (depth_pt > depth_mval) { return true; } @@ -874,53 +874,6 @@ static bool gp_stroke_eraser_is_occluded(tGPsdata *p, return false; } -/* eraser tool - check if part of stroke occurs within last segment drawn by eraser */ -static short gp_stroke_eraser_strokeinside(const int mval[2], const int UNUSED(mvalo[2]), - int rad, int x0, int y0, int x1, int y1) -{ - /* simple within-radius check for now */ - const float mval_fl[2] = {mval[0], mval[1]}; - const float screen_co_a[2] = {x0, y0}; - const float screen_co_b[2] = {x1, y1}; - - if (edge_inside_circle(mval_fl, rad, screen_co_a, screen_co_b)) { - return true; - } - - /* not inside */ - return false; -} - -static void gp_point_to_xy(ARegion *ar, View2D *v2d, rctf *subrect, bGPDstroke *gps, bGPDspoint *pt, - int *r_x, int *r_y) -{ - int xyval[2]; - - if (gps->flag & GP_STROKE_3DSPACE) { - if (ED_view3d_project_int_global(ar, &pt->x, xyval, V3D_PROJ_TEST_NOP) == V3D_PROJ_RET_OK) { - *r_x = xyval[0]; - *r_y = xyval[1]; - } - else { - *r_x = V2D_IS_CLIPPED; - *r_y = V2D_IS_CLIPPED; - } - } - else if (gps->flag & GP_STROKE_2DSPACE) { - UI_view2d_view_to_region_clip(v2d, pt->x, pt->y, r_x, r_y); - } - else { - if (subrect == NULL) { /* normal 3D view */ - *r_x = (int)(pt->x / 100 * ar->winx); - *r_y = (int)(pt->y / 100 * ar->winy); - } - else { /* camera view, use subrect */ - *r_x = (int)((pt->x / 100) * BLI_rctf_size_x(subrect)) + subrect->xmin; - *r_y = (int)((pt->y / 100) * BLI_rctf_size_y(subrect)) + subrect->ymin; - } - } -} - /* eraser tool - evaluation per stroke */ /* TODO: this could really do with some optimization (KD-Tree/BVH?) */ @@ -934,12 +887,12 @@ static void gp_stroke_eraser_dostroke(tGPsdata *p, if (gps->totpoints == 0) { /* just free stroke */ - if (gps->points) + if (gps->points) MEM_freeN(gps->points); BLI_freelinkN(&gpf->strokes, gps); } else if (gps->totpoints == 1) { - gp_point_to_xy(p->ar, p->v2d, p->subrect, gps, gps->points, &x0, &y0); + gp_point_to_xy(&p->gsc, gps, gps->points, &x0, &y0); /* do boundbox check first */ if ((!ELEM(V2D_IS_CLIPPED, x0, y0)) && BLI_rcti_isect_pt(rect, x0, y0)) { @@ -952,7 +905,7 @@ static void gp_stroke_eraser_dostroke(tGPsdata *p, } } else { - /* loop over the points in the stroke, checking for intersections + /* loop over the points in the stroke, checking for intersections * - an intersection will require the stroke to be split */ for (i = 0; (i + 1) < gps->totpoints; i++) { @@ -960,8 +913,8 @@ static void gp_stroke_eraser_dostroke(tGPsdata *p, pt1 = gps->points + i; pt2 = gps->points + i + 1; - gp_point_to_xy(p->ar, p->v2d, p->subrect, gps, pt1, &x0, &y0); - gp_point_to_xy(p->ar, p->v2d, p->subrect, gps, pt2, &x1, &y1); + gp_point_to_xy(&p->gsc, gps, pt1, &x0, &y0); + gp_point_to_xy(&p->gsc, gps, pt2, &x1, &y1); /* check that point segment of the boundbox of the eraser stroke */ if (((!ELEM(V2D_IS_CLIPPED, x0, y0)) && BLI_rcti_isect_pt(rect, x0, y0)) || @@ -971,7 +924,7 @@ static void gp_stroke_eraser_dostroke(tGPsdata *p, * eraser region (either within stroke painted, or on its lines) * - this assumes that linewidth is irrelevant */ - if (gp_stroke_eraser_strokeinside(mval, mvalo, rad, x0, y0, x1, y1)) { + if (gp_stroke_inside_circle(mval, mvalo, rad, x0, y0, x1, y1)) { if ((gp_stroke_eraser_is_occluded(p, pt1, x0, y0) == false) || (gp_stroke_eraser_is_occluded(p, pt2, x1, y1) == false)) { @@ -997,16 +950,16 @@ static void gp_stroke_doeraser(tGPsdata *p) rect.ymin = p->mval[1] - p->radius; rect.xmax = p->mval[0] + p->radius; rect.ymax = p->mval[1] + p->radius; - + if (p->sa->spacetype == SPACE_VIEW3D) { if (p->flags & GP_PAINTFLAG_V3D_ERASER_DEPTH) { View3D *v3d = p->sa->spacedata.first; - + view3d_region_operator_needs_opengl(p->win, p->ar); ED_view3d_autodist_init(p->scene, p->ar, v3d, 0); } } - + /* loop over strokes, checking segments for intersections */ for (gps = gpf->strokes.first; gps; gps = gpn) { gpn = gps->next; @@ -1037,13 +990,13 @@ static void gp_session_validatebuffer(tGPsdata *p) /* reset flags */ gpd->sbuffer_sflag = 0; - + /* reset inittime */ p->inittime = 0.0; } /* (re)init new painting data */ -static int gp_session_initdata(bContext *C, tGPsdata *p) +static bool gp_session_initdata(bContext *C, tGPsdata *p) { bGPdata **gpd_ptr = NULL; ScrArea *curarea = CTX_wm_area(C); @@ -1060,8 +1013,9 @@ static int gp_session_initdata(bContext *C, tGPsdata *p) /* pass on current scene and window */ p->scene = CTX_data_scene(C); p->win = CTX_wm_window(C); - + unit_m4(p->imat); + unit_m4(p->mat); switch (curarea->spacetype) { /* supported views first */ @@ -1070,9 +1024,10 @@ static int gp_session_initdata(bContext *C, tGPsdata *p) /* View3D *v3d = curarea->spacedata.first; */ /* RegionView3D *rv3d = ar->regiondata; */ - /* set current area + /* set current area * - must verify that region data is 3D-view (and not something else) */ + /* CAUTION: If this is the "toolbar", then this will change on the first stroke */ p->sa = curarea; p->ar = ar; @@ -1125,7 +1080,13 @@ static int gp_session_initdata(bContext *C, tGPsdata *p) case SPACE_CLIP: { SpaceClip *sc = curarea->spacedata.first; + MovieClip *clip = ED_space_clip_get_clip(sc); + if (clip == NULL) { + p->status = GP_STATUS_ERROR; + return false; + } + /* set the current area */ p->sa = curarea; p->ar = ar; @@ -1140,14 +1101,22 @@ static int gp_session_initdata(bContext *C, tGPsdata *p) p->custom_color[3] = 0.9f; if (sc->gpencil_src == SC_GPENCIL_SRC_TRACK) { - MovieClip *clip = ED_space_clip_get_clip(sc); int framenr = ED_space_clip_get_clip_frame_number(sc); MovieTrackingTrack *track = BKE_tracking_track_get_active(&clip->tracking); - MovieTrackingMarker *marker = BKE_tracking_marker_get(track, framenr); - - p->imat[3][0] -= marker->pos[0]; - p->imat[3][1] -= marker->pos[1]; + MovieTrackingMarker *marker = track ? BKE_tracking_marker_get(track, framenr) : NULL; + + if (marker) { + p->imat[3][0] -= marker->pos[0]; + p->imat[3][1] -= marker->pos[1]; + } + else { + p->status = GP_STATUS_ERROR; + return false; + } } + + invert_m4_m4(p->mat, p->imat); + copy_m4_m4(p->gsc.mat, p->mat); break; } /* unsupported views */ @@ -1192,10 +1161,10 @@ static int gp_session_initdata(bContext *C, tGPsdata *p) static tGPsdata *gp_session_initpaint(bContext *C) { tGPsdata *p = NULL; - + /* create new context data */ p = MEM_callocN(sizeof(tGPsdata), "GPencil Drawing Data"); - + gp_session_initdata(C, p); /* return context data for running paint operator */ @@ -1226,7 +1195,7 @@ static void gp_session_cleanup(tGPsdata *p) /* init new stroke */ static void gp_paint_initstroke(tGPsdata *p, short paintmode) -{ +{ /* get active layer (or add a new one if non-existent) */ p->gpl = gpencil_layer_getactive(p->gpd); if (p->gpl == NULL) { @@ -1241,7 +1210,7 @@ static void gp_paint_initstroke(tGPsdata *p, short paintmode) printf("Error: Cannot paint on locked layer\n"); return; } - + /* get active frame (add a new one if not matching frame) */ p->gpf = gpencil_layer_getframe(p->gpl, p->scene->r.cfra, 1); if (p->gpf == NULL) { @@ -1257,7 +1226,7 @@ static void gp_paint_initstroke(tGPsdata *p, short paintmode) p->paintmode = paintmode; if (p->paintmode == GP_PAINTMODE_ERASER) { p->gpd->sbuffer_sflag |= GP_STROKE_ERASER; - + /* check if we should respect depth while erasing */ if (p->sa->spacetype == SPACE_VIEW3D) { if (p->gpl->flag & GP_LAYER_NO_XRAY) { @@ -1265,12 +1234,23 @@ static void gp_paint_initstroke(tGPsdata *p, short paintmode) } } } - + else { + /* disable eraser flags - so that we can switch modes during a session */ + p->gpd->sbuffer_sflag &= ~GP_STROKE_ERASER; + + if (p->sa->spacetype == SPACE_VIEW3D) { + if (p->gpl->flag & GP_LAYER_NO_XRAY) { + p->flags &= ~GP_PAINTFLAG_V3D_ERASER_DEPTH; + } + } + } + /* set 'initial run' flag, which is only used to denote when a new stroke is starting */ p->flags |= GP_PAINTFLAG_FIRSTRUN; - + /* when drawing in the camera view, in 2D space, set the subrect */ + p->subrect = NULL; if (!(p->gpd->flag & GP_DATA_VIEWALIGN)) { if (p->sa->spacetype == SPACE_VIEW3D) { View3D *v3d = p->sa->spacedata.first; @@ -1283,7 +1263,21 @@ static void gp_paint_initstroke(tGPsdata *p, short paintmode) } } } - + + /* init stroke point space-conversion settings... */ + p->gsc.gpd = p->gpd; + p->gsc.gpl = p->gpl; + + p->gsc.sa = p->sa; + p->gsc.ar = p->ar; + p->gsc.v2d = p->v2d; + + p->gsc.subrect_data = p->subrect_data; + p->gsc.subrect = p->subrect; + + copy_m4_m4(p->gsc.mat, p->mat); + + /* check if points will need to be made in view-aligned space */ if (p->gpd->flag & GP_DATA_VIEWALIGN) { switch (p->sa->spacetype) { @@ -1333,7 +1327,7 @@ static void gp_paint_initstroke(tGPsdata *p, short paintmode) /* finish off a stroke (clears buffer, but doesn't finish the paint operation) */ static void gp_paint_strokeend(tGPsdata *p) { - /* for surface sketching, need to set the right OpenGL context stuff so that + /* for surface sketching, need to set the right OpenGL context stuff so that * the conversions will project the values correctly... */ if (gpencil_project_check(p)) { @@ -1388,13 +1382,18 @@ static void gpencil_draw_eraser(bContext *UNUSED(C), int x, int y, void *p_ptr) glTranslatef((float)x, (float)y, 0.0f); - glColor4ub(255, 255, 255, 128); - glEnable(GL_LINE_SMOOTH); glEnable(GL_BLEND); + glColor4ub(255, 100, 100, 20); + glutil_draw_filled_arc(0.0, M_PI * 2.0, p->radius, 40); + + setlinestyle(6); + + glColor4ub(255, 100, 100, 200); glutil_draw_lined_arc(0.0, M_PI * 2.0, p->radius, 40); + setlinestyle(0); glDisable(GL_BLEND); glDisable(GL_LINE_SMOOTH); @@ -1410,9 +1409,9 @@ static void gpencil_draw_toggle_eraser_cursor(bContext *C, tGPsdata *p, short en WM_paint_cursor_end(CTX_wm_manager(C), p->erasercursor); p->erasercursor = NULL; } - else if (enable) { + else if (enable && !p->erasercursor) { /* enable cursor */ - p->erasercursor = WM_paint_cursor_activate(CTX_wm_manager(C), + p->erasercursor = WM_paint_cursor_activate(CTX_wm_manager(C), NULL, /* XXX */ gpencil_draw_eraser, p); } @@ -1437,12 +1436,14 @@ static void gpencil_draw_exit(bContext *C, wmOperator *op) if (p->paintmode == GP_PAINTMODE_ERASER) { /* turn off radial brush cursor */ gpencil_draw_toggle_eraser_cursor(C, p, false); - - /* if successful, store the new eraser size to be used again next time */ - if (p->status == GP_STATUS_DONE) - U.gp_eraser = p->radius; } + /* always store the new eraser size to be used again next time + * NOTE: Do this even when not in eraser mode, as eraser may + * have been toggled at some point. + */ + U.gp_eraser = p->radius; + /* cleanup */ gp_paint_cleanup(p); gp_session_cleanup(p); @@ -1493,6 +1494,15 @@ static int gpencil_draw_init(bContext *C, wmOperator *op) /* ------------------------------- */ +/* ensure that the correct cursor icon is set */ +static void gpencil_draw_cursor_set(tGPsdata *p) +{ + if (p->paintmode == GP_PAINTMODE_ERASER) + WM_cursor_modal_set(p->win, BC_CROSSCURSOR); /* XXX need a better cursor */ + else + WM_cursor_modal_set(p->win, BC_PAINTBRUSHCURSOR); +} + /* update UI indicators of status, including cursor and header prints */ static void gpencil_draw_status_indicators(tGPsdata *p) { @@ -1519,13 +1529,13 @@ static void gpencil_draw_status_indicators(tGPsdata *p) ED_area_headerprint(p->sa, IFACE_("Grease Pencil Freehand Session: Hold and drag LMB to draw | " "ESC/Enter to end")); break; - + default: /* unhandled future cases */ ED_area_headerprint(p->sa, IFACE_("Grease Pencil Session: ESC/Enter to end")); break; } break; - + case GP_STATUS_ERROR: case GP_STATUS_DONE: /* clear status string */ @@ -1608,7 +1618,7 @@ static void gpencil_draw_apply_event(wmOperator *op, const wmEvent *event) /* handle pressure sensitivity (which is supplied by tablets) */ if (event->tablet_data) { - wmTabletData *wmtab = event->tablet_data; + const wmTabletData *wmtab = event->tablet_data; tablet = (wmtab->Active != EVT_TABLET_NONE); p->pressure = wmtab->Pressure; @@ -1619,15 +1629,6 @@ static void gpencil_draw_apply_event(wmOperator *op, const wmEvent *event) else p->pressure = 1.0f; - /* fill in stroke data (not actually used directly by gpencil_draw_apply) */ - RNA_collection_add(op->ptr, "stroke", &itemptr); - - mousef[0] = p->mval[0]; - mousef[1] = p->mval[1]; - RNA_float_set_array(&itemptr, "mouse", mousef); - RNA_float_set(&itemptr, "pressure", p->pressure); - RNA_boolean_set(&itemptr, "is_start", (p->flags & GP_PAINTFLAG_FIRSTRUN)); - /* special exception for start of strokes (i.e. maybe for just a dot) */ if (p->flags & GP_PAINTFLAG_FIRSTRUN) { p->flags &= ~GP_PAINTFLAG_FIRSTRUN; @@ -1644,6 +1645,15 @@ static void gpencil_draw_apply_event(wmOperator *op, const wmEvent *event) return; } + /* fill in stroke data (not actually used directly by gpencil_draw_apply) */ + RNA_collection_add(op->ptr, "stroke", &itemptr); + + mousef[0] = p->mval[0]; + mousef[1] = p->mval[1]; + RNA_float_set_array(&itemptr, "mouse", mousef); + RNA_float_set(&itemptr, "pressure", p->pressure); + RNA_boolean_set(&itemptr, "is_start", (p->flags & GP_PAINTFLAG_FIRSTRUN) != 0); + RNA_float_set(&itemptr, "time", p->curtime - p->inittime); /* apply the current latest drawing point */ @@ -1714,7 +1724,7 @@ static int gpencil_draw_exec(bContext *C, wmOperator *op) gpencil_draw_apply(op, p); } RNA_END; - + /* printf("\tGP - done\n"); */ /* cleanup */ @@ -1733,14 +1743,13 @@ static int gpencil_draw_exec(bContext *C, wmOperator *op) static int gpencil_draw_invoke(bContext *C, wmOperator *op, const wmEvent *event) { tGPsdata *p = NULL; - wmWindow *win = CTX_wm_window(C); if (G.debug & G_DEBUG) printf("GPencil - Starting Drawing\n"); /* try to initialize context data needed while drawing */ if (!gpencil_draw_init(C, op)) { - if (op->customdata) + if (op->customdata) MEM_freeN(op->customdata); if (G.debug & G_DEBUG) printf("\tGP - no valid data\n"); @@ -1748,28 +1757,25 @@ static int gpencil_draw_invoke(bContext *C, wmOperator *op, const wmEvent *event } else p = op->customdata; - + /* TODO: set any additional settings that we can take from the events? * TODO? if tablet is erasing, force eraser to be on? */ - + /* TODO: move cursor setting stuff to stroke-start so that paintmode can be changed midway... */ - + /* if eraser is on, draw radial aid */ if (p->paintmode == GP_PAINTMODE_ERASER) { gpencil_draw_toggle_eraser_cursor(C, p, true); } - /* set cursor */ - if (p->paintmode == GP_PAINTMODE_ERASER) - WM_cursor_modal_set(win, BC_CROSSCURSOR); /* XXX need a better cursor */ - else - WM_cursor_modal_set(win, BC_PAINTBRUSHCURSOR); - - /* special hack: if there was an initial event, then we were invoked via a hotkey, and - * painting should start immediately. Otherwise, this was called from a toolbar, in which - * case we should wait for the mouse to be clicked. + /* set cursor + * NOTE: This may change later (i.e. intentionally via brush toggle, + * or unintentionally if the user scrolls outside the area)... */ - if (event->val == KM_PRESS) { + gpencil_draw_cursor_set(p); + + /* only start drawing immediately if we're allowed to do so... */ + if (RNA_boolean_get(op->ptr, "wait_for_input") == false) { /* hotkey invoked - start drawing */ /* printf("\tGP - set first spot\n"); */ p->status = GP_STATUS_PAINTING; @@ -1780,6 +1786,7 @@ static int gpencil_draw_invoke(bContext *C, wmOperator *op, const wmEvent *event else { /* toolbar invoked - don't start drawing yet... */ /* printf("\tGP - hotkey invoked... waiting for click-drag\n"); */ + op->flag |= OP_IS_MODAL_CURSOR_REGION; } WM_event_add_notifier(C, NC_GPENCIL | NA_EDITED, NULL); @@ -1817,8 +1824,10 @@ static tGPsdata *gpencil_stroke_begin(bContext *C, wmOperator *op) if (gp_session_initdata(C, p)) gp_paint_initstroke(p, p->paintmode); - if (p->status != GP_STATUS_ERROR) + if (p->status != GP_STATUS_ERROR) { p->status = GP_STATUS_PAINTING; + op->flag &= ~OP_IS_MODAL_CURSOR_REGION; + } return op->customdata; } @@ -1826,15 +1835,16 @@ static tGPsdata *gpencil_stroke_begin(bContext *C, wmOperator *op) static void gpencil_stroke_end(wmOperator *op) { tGPsdata *p = op->customdata; - + gp_paint_cleanup(p); - + gpencil_undo_push(p->gpd); - + gp_session_cleanup(p); - + p->status = GP_STATUS_IDLING; - + op->flag |= OP_IS_MODAL_CURSOR_REGION; + p->gpd = NULL; p->gpl = NULL; p->gpf = NULL; @@ -1858,11 +1868,19 @@ static int gpencil_draw_modal(bContext *C, wmOperator *op, const wmEvent *event) * better in tools that immediately apply * in 3D space. */ + + if (p->status == GP_STATUS_IDLING) { + ARegion *ar = CTX_wm_region(C); + p->ar = ar; + } /* we don't pass on key events, GP is used with key-modifiers - prevents Dkey to insert drivers */ if (ISKEYBOARD(event->type)) { - if (ELEM(event->type, LEFTARROWKEY, DOWNARROWKEY, RIGHTARROWKEY, UPARROWKEY)) { - /* allow some keys - for frame changing: [#33412] */ + if (ELEM(event->type, LEFTARROWKEY, DOWNARROWKEY, RIGHTARROWKEY, UPARROWKEY, ZKEY)) { + /* allow some keys: + * - for frame changing [#33412] + * - for undo (during sketching sessions) + */ } else { estate = OPERATOR_RUNNING_MODAL; @@ -1871,7 +1889,7 @@ static int gpencil_draw_modal(bContext *C, wmOperator *op, const wmEvent *event) //printf("\tGP - handle modal event...\n"); - /* exit painting mode (and/or end current stroke) + /* exit painting mode (and/or end current stroke) * NOTE: cannot do RIGHTMOUSE (as is standard for canceling) as that would break polyline [#32647] */ if (ELEM(event->type, RETKEY, PADENTER, ESCKEY, SPACEKEY)) { @@ -1881,17 +1899,18 @@ static int gpencil_draw_modal(bContext *C, wmOperator *op, const wmEvent *event) estate = OPERATOR_FINISHED; } - /* toggle painting mode upon mouse-button movement + /* toggle painting mode upon mouse-button movement * - LEFTMOUSE = standard drawing (all) / straight line drawing (all) / polyline (toolbox only) * - RIGHTMOUSE = polyline (hotkey) / eraser (all) * (Disabling RIGHTMOUSE case here results in bugs like [#32647]) + * also making sure we have a valid event value, to not exit too early */ - if (ELEM(event->type, LEFTMOUSE, RIGHTMOUSE)) { + if (ELEM(event->type, LEFTMOUSE, RIGHTMOUSE) && (event->val != KM_NOTHING)) { /* if painting, end stroke */ if (p->status == GP_STATUS_PAINTING) { int sketch = 0; - /* basically, this should be mouse-button up = end stroke + /* basically, this should be mouse-button up = end stroke * BUT what happens next depends on whether we 'painting sessions' is enabled */ sketch |= GPENCIL_SKETCH_SESSIONS_ON(p->scene); @@ -1903,6 +1922,27 @@ static int gpencil_draw_modal(bContext *C, wmOperator *op, const wmEvent *event) /* printf("\t\tGP - end stroke only\n"); */ gpencil_stroke_end(op); + /* If eraser mode is on, turn it off after the stroke finishes + * NOTE: This just makes it nicer to work with drawing sessions + */ + if (p->paintmode == GP_PAINTMODE_ERASER) { + p->paintmode = RNA_enum_get(op->ptr, "mode"); + + /* if the original mode was *still* eraser, + * we'll let it say for now, since this gives + * users an opportunity to have visual feedback + * when adjusting eraser size + */ + if (p->paintmode != GP_PAINTMODE_ERASER) { + /* turn off cursor... + * NOTE: this should be enough for now + * Just hiding this makes it seem like + * you can paint again... + */ + gpencil_draw_toggle_eraser_cursor(C, p, false); + } + } + /* we've just entered idling state, so this event was processed (but no others yet) */ estate = OPERATOR_RUNNING_MODAL; @@ -1916,15 +1956,88 @@ static int gpencil_draw_modal(bContext *C, wmOperator *op, const wmEvent *event) } } else if (event->val == KM_PRESS) { - /* not painting, so start stroke (this should be mouse-button down) */ - p = gpencil_stroke_begin(C, op); + bool in_bounds = false; - if (p->status == GP_STATUS_ERROR) { + /* Check if we're outside the bounds of the active region + * NOTE: An exception here is that if launched from the toolbar, + * whatever region we're now in should become the new region + */ + if ((p->ar) && (p->ar->regiontype == RGN_TYPE_TOOLS)) { + /* Change to whatever region is now under the mouse */ + ARegion *current_region = BKE_area_find_region_xy(p->sa, RGN_TYPE_ANY, event->x, event->y); + + if (G.debug & G_DEBUG) { + printf("found alternative region %p (old was %p) - at %d %d (sa: %d %d -> %d %d)\n", + current_region, p->ar, event->x, event->y, + p->sa->totrct.xmin, p->sa->totrct.ymin, p->sa->totrct.xmax, p->sa->totrct.ymax); + } + + if (current_region) { + /* Assume that since we found the cursor in here, it is in bounds + * and that this should be the region that we begin drawing in + */ + p->ar = current_region; + in_bounds = true; + } + else { + /* Out of bounds, or invalid in some other way */ + p->status = GP_STATUS_ERROR; + estate = OPERATOR_CANCELLED; + + if (G.debug & G_DEBUG) + printf("%s: Region under cursor is out of bounds, so cannot be drawn on\n", __func__); + } + } + else if (p->ar) { + rcti region_rect; + + /* Perform bounds check using */ + ED_region_visible_rect(p->ar, ®ion_rect); + in_bounds = BLI_rcti_isect_pt_v(®ion_rect, event->mval); + } + else { + /* No region */ + p->status = GP_STATUS_ERROR; estate = OPERATOR_CANCELLED; + + if (G.debug & G_DEBUG) + printf("%s: No active region found in GP Paint session data\n", __func__); + } + + if (in_bounds) { + /* Switch paintmode (temporarily if need be) based on which button was used + * NOTE: This is to make it more convenient to erase strokes when using drawing sessions + */ + if (event->type == LEFTMOUSE) { + /* restore drawmode to default */ + p->paintmode = RNA_enum_get(op->ptr, "mode"); + } + else if (event->type == RIGHTMOUSE) { + /* turn on eraser */ + p->paintmode = GP_PAINTMODE_ERASER; + } + + gpencil_draw_toggle_eraser_cursor(C, p, p->paintmode == GP_PAINTMODE_ERASER); + + /* not painting, so start stroke (this should be mouse-button down) */ + p = gpencil_stroke_begin(C, op); + + if (p->status == GP_STATUS_ERROR) { + estate = OPERATOR_CANCELLED; + } + } + else if (p->status != GP_STATUS_ERROR) { + /* User clicked outside bounds of window while idling, so exit paintmode + * NOTE: Don't eter this case if an error occurred while finding the + * region (as above) + */ + p->status = GP_STATUS_DONE; + estate = OPERATOR_FINISHED; } } else { p->status = GP_STATUS_IDLING; + op->flag |= OP_IS_MODAL_CURSOR_REGION; } } @@ -1960,12 +2073,12 @@ static int gpencil_draw_modal(bContext *C, wmOperator *op, const wmEvent *event) case PADPLUSKEY: p->radius += 5; break; - + case WHEELUPMOUSE: /* smaller */ case PADMINUS: p->radius -= 5; - if (p->radius < 0) + if (p->radius < 0) p->radius = 0; break; } @@ -1976,7 +2089,7 @@ static int gpencil_draw_modal(bContext *C, wmOperator *op, const wmEvent *event) /* event handled, so just tag as running modal */ estate = OPERATOR_RUNNING_MODAL; } - /* there shouldn't be any other events, but just in case there are, let's swallow them + /* there shouldn't be any other events, but just in case there are, let's swallow them * (i.e. to prevent problems with undo) */ else { @@ -1988,9 +2101,11 @@ static int gpencil_draw_modal(bContext *C, wmOperator *op, const wmEvent *event) /* gpencil modal operator stores area, which can be removed while using it (like fullscreen) */ if (0 == gpencil_area_exists(C, p->sa)) estate = OPERATOR_CANCELLED; - else + else { /* update status indicators - cursor, header, etc. */ gpencil_draw_status_indicators(p); + gpencil_draw_cursor_set(p); /* cursor may have changed outside our control - T44084 */ + } /* process last operations before exiting */ switch (estate) { @@ -1999,7 +2114,7 @@ static int gpencil_draw_modal(bContext *C, wmOperator *op, const wmEvent *event) gpencil_draw_exit(C, op); WM_event_add_notifier(C, NC_GPENCIL | NA_EDITED, NULL); break; - + case OPERATOR_CANCELLED: gpencil_draw_exit(C, op); break; @@ -2046,6 +2161,8 @@ void GPENCIL_OT_draw(wmOperatorType *ot) /* settings for drawing */ ot->prop = RNA_def_enum(ot->srna, "mode", prop_gpencil_drawmodes, 0, "Mode", "Way to interpret mouse movements"); - RNA_def_collection_runtime(ot->srna, "stroke", &RNA_OperatorStrokeElement, "Stroke", ""); + + /* NOTE: wait for input is enabled by default, so that all UI code can work properly without needing users to know about this */ + RNA_def_boolean(ot->srna, "wait_for_input", true, "Wait for Input", "Wait for first click instead of painting immediately"); } diff --git a/source/blender/editors/gpencil/gpencil_select.c b/source/blender/editors/gpencil/gpencil_select.c new file mode 100644 index 00000000000..0dd91019a8c --- /dev/null +++ b/source/blender/editors/gpencil/gpencil_select.c @@ -0,0 +1,930 @@ +/* + * ***** BEGIN GPL LICENSE BLOCK ***** + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + * + * The Original Code is Copyright (C) 2014, Blender Foundation + * This is a new part of Blender + * + * Contributor(s): Joshua Leung + * + * ***** END GPL LICENSE BLOCK ***** + */ + +/** \file blender/editors/gpencil/gpencil_select.c + * \ingroup edgpencil + */ + +#include <stdio.h> +#include <string.h> +#include <stdlib.h> +#include <stddef.h> +#include <math.h> + +#include "MEM_guardedalloc.h" + +#include "BLI_blenlib.h" +#include "BLI_lasso.h" +#include "BLI_utildefines.h" +#include "BLI_math_vector.h" + +#include "DNA_gpencil_types.h" +#include "DNA_screen_types.h" + +#include "BKE_context.h" +#include "BKE_gpencil.h" +#include "BKE_report.h" + +#include "UI_interface.h" + +#include "WM_api.h" +#include "WM_types.h" + +#include "RNA_access.h" +#include "RNA_define.h" + +#include "UI_view2d.h" + +#include "ED_gpencil.h" + +#include "gpencil_intern.h" + +/* ********************************************** */ +/* Polling callbacks */ + +static int gpencil_select_poll(bContext *C) +{ + bGPdata *gpd = ED_gpencil_data_get_active(C); + bGPDlayer *gpl = gpencil_layer_getactive(gpd); + + /* only if there's an active layer with an active frame */ + return (gpl && gpl->actframe); +} + +/* ********************************************** */ +/* Select All Operator */ + +static int gpencil_select_all_exec(bContext *C, wmOperator *op) +{ + bGPdata *gpd = ED_gpencil_data_get_active(C); + int action = RNA_enum_get(op->ptr, "action"); + + if (gpd == NULL) { + BKE_report(op->reports, RPT_ERROR, "No Grease Pencil data"); + return OPERATOR_CANCELLED; + } + + /* for "toggle", test for existing selected strokes */ + if (action == SEL_TOGGLE) { + action = SEL_SELECT; + + CTX_DATA_BEGIN(C, bGPDstroke *, gps, editable_gpencil_strokes) + { + if (gps->flag & GP_STROKE_SELECT) { + action = SEL_DESELECT; + break; // XXX: this only gets out of the inner loop... + } + } + CTX_DATA_END; + } + + /* if deselecting, we need to deselect strokes across all frames + * - Currently, an exception is only given for deselection + * Selecting and toggling should only affect what's visible, + * while deselecting helps clean up unintended/forgotten + * stuff on other frames + */ + if (action == SEL_DESELECT) { + /* deselect strokes across editable layers + * NOTE: we limit ourselves to editable layers, since once a layer is "locked/hidden + * nothing should be able to touch it + */ + CTX_DATA_BEGIN(C, bGPDlayer *, gpl, editable_gpencil_layers) + { + bGPDframe *gpf; + + /* deselect all strokes on all frames */ + for (gpf = gpl->frames.first; gpf; gpf = gpf->next) { + bGPDstroke *gps; + + for (gps = gpf->strokes.first; gps; gps = gps->next) { + bGPDspoint *pt; + int i; + + /* only edit strokes that are valid in this view... */ + if (ED_gpencil_stroke_can_use(C, gps)) { + for (i = 0, pt = gps->points; i < gps->totpoints; i++, pt++) { + pt->flag &= ~GP_SPOINT_SELECT; + } + + gps->flag &= ~GP_STROKE_SELECT; + } + } + } + } + CTX_DATA_END; + } + else { + /* select or deselect all strokes */ + CTX_DATA_BEGIN(C, bGPDstroke *, gps, editable_gpencil_strokes) + { + bGPDspoint *pt; + int i; + bool selected = false; + + /* Change selection status of all points, then make the stroke match */ + for (i = 0, pt = gps->points; i < gps->totpoints; i++, pt++) { + switch (action) { + case SEL_SELECT: + pt->flag |= GP_SPOINT_SELECT; + break; + //case SEL_DESELECT: + // pt->flag &= ~GP_SPOINT_SELECT; + // break; + case SEL_INVERT: + pt->flag ^= GP_SPOINT_SELECT; + break; + } + + if (pt->flag & GP_SPOINT_SELECT) + selected = true; + } + + /* Change status of stroke */ + if (selected) + gps->flag |= GP_STROKE_SELECT; + else + gps->flag &= ~GP_STROKE_SELECT; + } + CTX_DATA_END; + } + + /* updates */ + WM_event_add_notifier(C, NC_GPENCIL | NA_SELECTED, NULL); + return OPERATOR_FINISHED; +} + +void GPENCIL_OT_select_all(wmOperatorType *ot) +{ + /* identifiers */ + ot->name = "(De)select All Strokes"; + ot->idname = "GPENCIL_OT_select_all"; + ot->description = "Change selection of all Grease Pencil strokes currently visible"; + + /* callbacks */ + ot->exec = gpencil_select_all_exec; + ot->poll = gpencil_select_poll; + + /* flags */ + ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO; + + WM_operator_properties_select_all(ot); +} + +/* ********************************************** */ +/* Select Linked */ + +static int gpencil_select_linked_exec(bContext *C, wmOperator *op) +{ + bGPdata *gpd = ED_gpencil_data_get_active(C); + + if (gpd == NULL) { + BKE_report(op->reports, RPT_ERROR, "No Grease Pencil data"); + return OPERATOR_CANCELLED; + } + + /* select all points in selected strokes */ + CTX_DATA_BEGIN(C, bGPDstroke *, gps, editable_gpencil_strokes) + { + if (gps->flag & GP_STROKE_SELECT) { + bGPDspoint *pt; + int i; + + for (i = 0, pt = gps->points; i < gps->totpoints; i++, pt++) { + pt->flag |= GP_SPOINT_SELECT; + } + } + } + CTX_DATA_END; + + /* updates */ + WM_event_add_notifier(C, NC_GPENCIL | NA_SELECTED, NULL); + return OPERATOR_FINISHED; +} + +void GPENCIL_OT_select_linked(wmOperatorType *ot) +{ + /* identifiers */ + ot->name = "Select Linked"; + ot->idname = "GPENCIL_OT_select_linked"; + ot->description = "Select all points in same strokes as already selected points"; + + /* callbacks */ + ot->exec = gpencil_select_linked_exec; + ot->poll = gpencil_select_poll; + + /* flags */ + ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO; +} + +/* ********************************************** */ +/* Select More */ + +static int gpencil_select_more_exec(bContext *C, wmOperator *UNUSED(op)) +{ + CTX_DATA_BEGIN(C, bGPDstroke *, gps, editable_gpencil_strokes) + { + if (gps->flag & GP_STROKE_SELECT) { + bGPDspoint *pt; + int i; + bool prev_sel; + + /* First Pass: Go in forward order, expanding selection if previous was selected (pre changes)... + * - This pass covers the "after" edges of selection islands + */ + prev_sel = false; + for (i = 0, pt = gps->points; i < gps->totpoints; i++, pt++) { + if (pt->flag & GP_SPOINT_SELECT) { + /* selected point - just set flag for next point */ + prev_sel = true; + } + else { + /* unselected point - expand selection if previous was selected... */ + if (prev_sel) { + pt->flag |= GP_SPOINT_SELECT; + } + prev_sel = false; + } + } + + /* Second Pass: Go in reverse order, doing the same as before (except in opposite order) + * - This pass covers the "before" edges of selection islands + */ + prev_sel = false; + for (pt -= 1; i > 0; i--, pt--) { + if (pt->flag & GP_SPOINT_SELECT) { + prev_sel = true; + } + else { + /* unselected point - expand selection if previous was selected... */ + if (prev_sel) { + pt->flag |= GP_SPOINT_SELECT; + } + prev_sel = false; + } + } + } + } + CTX_DATA_END; + + /* updates */ + WM_event_add_notifier(C, NC_GPENCIL | NA_SELECTED, NULL); + return OPERATOR_FINISHED; +} + +void GPENCIL_OT_select_more(wmOperatorType *ot) +{ + /* identifiers */ + ot->name = "Select More"; + ot->idname = "GPENCIL_OT_select_more"; + ot->description = "Grow sets of selected Grease Pencil points"; + + /* callbacks */ + ot->exec = gpencil_select_more_exec; + ot->poll = gpencil_select_poll; + + /* flags */ + ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO; +} + +/* ********************************************** */ +/* Select Less */ + +static int gpencil_select_less_exec(bContext *C, wmOperator *UNUSED(op)) +{ + CTX_DATA_BEGIN(C, bGPDstroke *, gps, editable_gpencil_strokes) + { + if (gps->flag & GP_STROKE_SELECT) { + bGPDspoint *pt; + int i; + bool prev_sel; + + /* First Pass: Go in forward order, shrinking selection if previous was not selected (pre changes)... + * - This pass covers the "after" edges of selection islands + */ + prev_sel = false; + for (i = 0, pt = gps->points; i < gps->totpoints; i++, pt++) { + if (pt->flag & GP_SPOINT_SELECT) { + /* shrink if previous wasn't selected */ + if (prev_sel == false) { + pt->flag &= ~GP_SPOINT_SELECT; + } + prev_sel = true; + } + else { + /* mark previous as being unselected - and hence, is trigger for shrinking */ + prev_sel = false; + } + } + + /* Second Pass: Go in reverse order, doing the same as before (except in opposite order) + * - This pass covers the "before" edges of selection islands + */ + prev_sel = false; + for (pt -= 1; i > 0; i--, pt--) { + if (pt->flag & GP_SPOINT_SELECT) { + /* shrink if previous wasn't selected */ + if (prev_sel == false) { + pt->flag &= ~GP_SPOINT_SELECT; + } + prev_sel = true; + } + else { + /* mark previous as being unselected - and hence, is trigger for shrinking */ + prev_sel = false; + } + } + } + } + CTX_DATA_END; + + /* updates */ + WM_event_add_notifier(C, NC_GPENCIL | NA_SELECTED, NULL); + return OPERATOR_FINISHED; +} + +void GPENCIL_OT_select_less(wmOperatorType *ot) +{ + /* identifiers */ + ot->name = "Select Less"; + ot->idname = "GPENCIL_OT_select_less"; + ot->description = "Shrink sets of selected Grease Pencil points"; + + /* callbacks */ + ot->exec = gpencil_select_less_exec; + ot->poll = gpencil_select_poll; + + /* flags */ + ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO; +} + +/* ********************************************** */ +/* Circle Select Operator */ + +/* Helper to check if a given stroke is within the area */ +/* NOTE: Code here is adapted (i.e. copied directly) from gpencil_paint.c::gp_stroke_eraser_dostroke() + * It would be great to de-duplicate the logic here sometime, but that can wait... + */ +static bool gp_stroke_do_circle_sel(bGPDstroke *gps, GP_SpaceConversion *gsc, + const int mx, const int my, const int radius, + const bool select, rcti *rect) +{ + bGPDspoint *pt1, *pt2; + int x0 = 0, y0 = 0, x1 = 0, y1 = 0; + int i; + bool changed = false; + + if (gps->totpoints == 1) { + gp_point_to_xy(gsc, gps, gps->points, &x0, &y0); + + /* do boundbox check first */ + if ((!ELEM(V2D_IS_CLIPPED, x0, y0)) && BLI_rcti_isect_pt(rect, x0, y0)) { + /* only check if point is inside */ + if (((x0 - mx) * (x0 - mx) + (y0 - my) * (y0 - my)) <= radius * radius) { + /* change selection */ + if (select) { + gps->points->flag |= GP_SPOINT_SELECT; + gps->flag |= GP_STROKE_SELECT; + } + else { + gps->points->flag &= ~GP_SPOINT_SELECT; + gps->flag &= ~GP_STROKE_SELECT; + } + + return true; + } + } + } + else { + /* Loop over the points in the stroke, checking for intersections + * - an intersection means that we touched the stroke + */ + for (i = 0; (i + 1) < gps->totpoints; i++) { + /* get points to work with */ + pt1 = gps->points + i; + pt2 = gps->points + i + 1; + + gp_point_to_xy(gsc, gps, pt1, &x0, &y0); + gp_point_to_xy(gsc, gps, pt2, &x1, &y1); + + /* check that point segment of the boundbox of the selection stroke */ + if (((!ELEM(V2D_IS_CLIPPED, x0, y0)) && BLI_rcti_isect_pt(rect, x0, y0)) || + ((!ELEM(V2D_IS_CLIPPED, x1, y1)) && BLI_rcti_isect_pt(rect, x1, y1))) + { + int mval[2] = {mx, my}; + int mvalo[2] = {mx, my}; /* dummy - this isn't used... */ + + /* check if point segment of stroke had anything to do with + * eraser region (either within stroke painted, or on its lines) + * - this assumes that linewidth is irrelevant + */ + if (gp_stroke_inside_circle(mval, mvalo, radius, x0, y0, x1, y1)) { + /* change selection of stroke, and then of both points + * (as the last point otherwise wouldn't get selected + * as we only do n-1 loops through) + */ + if (select) { + pt1->flag |= GP_SPOINT_SELECT; + pt2->flag |= GP_SPOINT_SELECT; + + changed = true; + } + else { + pt1->flag &= ~GP_SPOINT_SELECT; + pt2->flag &= ~GP_SPOINT_SELECT; + + changed = true; + } + } + } + } + + /* Ensure that stroke selection is in sync with its points */ + gpencil_stroke_sync_selection(gps); + } + + return changed; +} + + +static int gpencil_circle_select_exec(bContext *C, wmOperator *op) +{ + ScrArea *sa = CTX_wm_area(C); + + const int mx = RNA_int_get(op->ptr, "x"); + const int my = RNA_int_get(op->ptr, "y"); + const int radius = RNA_int_get(op->ptr, "radius"); + + const int gesture_mode = RNA_int_get(op->ptr, "gesture_mode"); + const bool select = (gesture_mode == GESTURE_MODAL_SELECT); + + GP_SpaceConversion gsc = {NULL}; + rcti rect = {0}; /* for bounding rect around circle (for quicky intersection testing) */ + + bool changed = false; + + + /* sanity checks */ + if (sa == NULL) { + BKE_report(op->reports, RPT_ERROR, "No active area"); + return OPERATOR_CANCELLED; + } + + /* init space conversion stuff */ + gp_point_conversion_init(C, &gsc); + + + /* rect is rectangle of selection circle */ + rect.xmin = mx - radius; + rect.ymin = my - radius; + rect.xmax = mx + radius; + rect.ymax = my + radius; + + + /* find visible strokes, and select if hit */ + CTX_DATA_BEGIN(C, bGPDstroke *, gps, editable_gpencil_strokes) + { + changed |= gp_stroke_do_circle_sel(gps, &gsc, mx, my, radius, select, &rect); + } + CTX_DATA_END; + + /* updates */ + if (changed) { + WM_event_add_notifier(C, NC_GPENCIL | NA_SELECTED, NULL); + } + + return OPERATOR_FINISHED; +} + +void GPENCIL_OT_select_circle(wmOperatorType *ot) +{ + /* identifiers */ + ot->name = "Circle Select"; + ot->description = "Select Grease Pencil strokes using brush selection"; + ot->idname = "GPENCIL_OT_select_circle"; + + /* callbacks */ + ot->invoke = WM_gesture_circle_invoke; + ot->modal = WM_gesture_circle_modal; + ot->exec = gpencil_circle_select_exec; + ot->poll = gpencil_select_poll; + ot->cancel = WM_gesture_circle_cancel; + + /* flags */ + ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO; + + /* properties */ + RNA_def_int(ot->srna, "x", 0, INT_MIN, INT_MAX, "X", "", INT_MIN, INT_MAX); + RNA_def_int(ot->srna, "y", 0, INT_MIN, INT_MAX, "Y", "", INT_MIN, INT_MAX); + RNA_def_int(ot->srna, "radius", 1, 1, INT_MAX, "Radius", "", 1, INT_MAX); + RNA_def_int(ot->srna, "gesture_mode", 0, INT_MIN, INT_MAX, "Gesture Mode", "", INT_MIN, INT_MAX); +} + +/* ********************************************** */ +/* Box Selection */ + +static int gpencil_border_select_exec(bContext *C, wmOperator *op) +{ + ScrArea *sa = CTX_wm_area(C); + + const int gesture_mode = RNA_int_get(op->ptr, "gesture_mode"); + const bool select = (gesture_mode == GESTURE_MODAL_SELECT); + const bool extend = RNA_boolean_get(op->ptr, "extend"); + + GP_SpaceConversion gsc = {NULL}; + rcti rect = {0}; + + bool changed = false; + + + /* sanity checks */ + if (sa == NULL) { + BKE_report(op->reports, RPT_ERROR, "No active area"); + return OPERATOR_CANCELLED; + } + + /* init space conversion stuff */ + gp_point_conversion_init(C, &gsc); + + + /* deselect all strokes first? */ + if (select && !extend) { + CTX_DATA_BEGIN(C, bGPDstroke *, gps, editable_gpencil_strokes) + { + bGPDspoint *pt; + int i; + + for (i = 0, pt = gps->points; i < gps->totpoints; i++, pt++) { + pt->flag &= ~GP_SPOINT_SELECT; + } + + gps->flag &= ~GP_STROKE_SELECT; + } + CTX_DATA_END; + } + + /* get settings from operator */ + WM_operator_properties_border_to_rcti(op, &rect); + + /* select/deselect points */ + CTX_DATA_BEGIN(C, bGPDstroke *, gps, editable_gpencil_strokes) + { + bGPDspoint *pt; + int i; + + for (i = 0, pt = gps->points; i < gps->totpoints; i++, pt++) { + int x0, y0; + + /* convert point coords to screenspace */ + gp_point_to_xy(&gsc, gps, pt, &x0, &y0); + + /* test if in selection rect */ + if ((!ELEM(V2D_IS_CLIPPED, x0, y0)) && BLI_rcti_isect_pt(&rect, x0, y0)) { + if (select) { + pt->flag |= GP_SPOINT_SELECT; + } + else { + pt->flag &= ~GP_SPOINT_SELECT; + } + + changed = true; + } + } + + /* Ensure that stroke selection is in sync with its points */ + gpencil_stroke_sync_selection(gps); + } + CTX_DATA_END; + + /* updates */ + if (changed) { + WM_event_add_notifier(C, NC_GPENCIL | NA_SELECTED, NULL); + } + + return OPERATOR_FINISHED; +} + +void GPENCIL_OT_select_border(wmOperatorType *ot) +{ + /* identifiers */ + ot->name = "Border Select"; + ot->description = "Select Grease Pencil strokes within a rectangular region"; + ot->idname = "GPENCIL_OT_select_border"; + + /* callbacks */ + ot->invoke = WM_border_select_invoke; + ot->exec = gpencil_border_select_exec; + ot->modal = WM_border_select_modal; + ot->cancel = WM_border_select_cancel; + + ot->poll = gpencil_select_poll; + + /* flags */ + ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO; + + /* rna */ + WM_operator_properties_gesture_border(ot, true); +} + +/* ********************************************** */ +/* Lasso */ + +static int gpencil_lasso_select_exec(bContext *C, wmOperator *op) +{ + GP_SpaceConversion gsc = {NULL}; + rcti rect = {0}; + + const bool extend = RNA_boolean_get(op->ptr, "extend"); + const bool select = !RNA_boolean_get(op->ptr, "deselect"); + + int mcords_tot; + const int (*mcords)[2] = WM_gesture_lasso_path_to_array(C, op, &mcords_tot); + + bool changed = false; + + /* sanity check */ + if (mcords == NULL) + return OPERATOR_PASS_THROUGH; + + /* compute boundbox of lasso (for faster testing later) */ + BLI_lasso_boundbox(&rect, mcords, mcords_tot); + + /* init space conversion stuff */ + gp_point_conversion_init(C, &gsc); + + /* deselect all strokes first? */ + if (select && !extend) { + CTX_DATA_BEGIN(C, bGPDstroke *, gps, editable_gpencil_strokes) + { + bGPDspoint *pt; + int i; + + for (i = 0, pt = gps->points; i < gps->totpoints; i++, pt++) { + pt->flag &= ~GP_SPOINT_SELECT; + } + + gps->flag &= ~GP_STROKE_SELECT; + } + CTX_DATA_END; + } + + /* select/deselect points */ + CTX_DATA_BEGIN(C, bGPDstroke *, gps, editable_gpencil_strokes) + { + bGPDspoint *pt; + int i; + + for (i = 0, pt = gps->points; i < gps->totpoints; i++, pt++) { + int x0, y0; + + /* convert point coords to screenspace */ + gp_point_to_xy(&gsc, gps, pt, &x0, &y0); + + /* test if in lasso boundbox + within the lasso noose */ + if ((!ELEM(V2D_IS_CLIPPED, x0, y0)) && BLI_rcti_isect_pt(&rect, x0, y0) && + BLI_lasso_is_point_inside(mcords, mcords_tot, x0, y0, INT_MAX)) + { + if (select) { + pt->flag |= GP_SPOINT_SELECT; + } + else { + pt->flag &= ~GP_SPOINT_SELECT; + } + + changed = true; + } + } + + /* Ensure that stroke selection is in sync with its points */ + gpencil_stroke_sync_selection(gps); + } + CTX_DATA_END; + + /* cleanup */ + MEM_freeN((void *)mcords); + + /* updates */ + if (changed) { + WM_event_add_notifier(C, NC_GPENCIL | NA_SELECTED, NULL); + } + + return OPERATOR_FINISHED; +} + +void GPENCIL_OT_select_lasso(wmOperatorType *ot) +{ + ot->name = "Lasso Select Strokes"; + ot->description = "Select Grease Pencil strokes using lasso selection"; + ot->idname = "GPENCIL_OT_select_lasso"; + + ot->invoke = WM_gesture_lasso_invoke; + ot->modal = WM_gesture_lasso_modal; + ot->exec = gpencil_lasso_select_exec; + ot->poll = gpencil_select_poll; + ot->cancel = WM_gesture_lasso_cancel; + + /* flags */ + ot->flag = OPTYPE_UNDO; + + RNA_def_collection_runtime(ot->srna, "path", &RNA_OperatorMousePath, "Path", ""); + RNA_def_boolean(ot->srna, "deselect", 0, "Deselect", "Deselect rather than select items"); + RNA_def_boolean(ot->srna, "extend", 1, "Extend", "Extend selection instead of deselecting everything first"); +} + +/* ********************************************** */ +/* Mouse Click to Select */ + +static int gpencil_select_exec(bContext *C, wmOperator *op) +{ + ScrArea *sa = CTX_wm_area(C); + + /* "radius" is simply a threshold (screen space) to make it easier to test with a tolerance */ + const float radius = 0.75f * U.widget_unit; + const int radius_squared = (int)(radius * radius); + + bool extend = RNA_boolean_get(op->ptr, "extend"); + bool deselect = RNA_boolean_get(op->ptr, "deselect"); + bool toggle = RNA_boolean_get(op->ptr, "toggle"); + bool whole = RNA_boolean_get(op->ptr, "entire_strokes"); + + int mval[2] = {0}; + + GP_SpaceConversion gsc = {NULL}; + + bGPDstroke *hit_stroke = NULL; + bGPDspoint *hit_point = NULL; + int hit_distance = radius_squared; + + /* sanity checks */ + if (sa == NULL) { + BKE_report(op->reports, RPT_ERROR, "No active area"); + return OPERATOR_CANCELLED; + } + + /* init space conversion stuff */ + gp_point_conversion_init(C, &gsc); + + /* get mouse location */ + RNA_int_get_array(op->ptr, "location", mval); + + /* First Pass: Find stroke point which gets hit */ + /* XXX: maybe we should go from the top of the stack down instead... */ + CTX_DATA_BEGIN(C, bGPDstroke *, gps, editable_gpencil_strokes) + { + bGPDspoint *pt; + int i; + + /* firstly, check for hit-point */ + for (i = 0, pt = gps->points; i < gps->totpoints; i++, pt++) { + int xy[2]; + + gp_point_to_xy(&gsc, gps, pt, &xy[0], &xy[1]); + + /* do boundbox check first */ + if (!ELEM(V2D_IS_CLIPPED, xy[0], xy[1])) { + const int pt_distance = len_manhattan_v2v2_int(mval, xy); + + /* check if point is inside */ + if (pt_distance <= radius_squared) { + /* only use this point if it is a better match than the current hit - T44685 */ + if (pt_distance < hit_distance) { + hit_stroke = gps; + hit_point = pt; + hit_distance = pt_distance; + } + } + } + } + } + CTX_DATA_END; + + /* Abort if nothing hit... */ + if (ELEM(NULL, hit_stroke, hit_point)) { + return OPERATOR_CANCELLED; + } + + /* adjust selection behaviour - for toggle option */ + if (toggle) { + deselect = (hit_point->flag & GP_SPOINT_SELECT) != 0; + } + + /* If not extending selection, deselect everything else */ + if (extend == false) { + CTX_DATA_BEGIN(C, bGPDstroke *, gps, editable_gpencil_strokes) + { + /* deselect stroke and its points if selected */ + if (gps->flag & GP_STROKE_SELECT) { + bGPDspoint *pt; + int i; + + /* deselect points */ + for (i = 0, pt = gps->points; i < gps->totpoints; i++, pt++) { + pt->flag &= ~GP_SPOINT_SELECT; + } + + /* deselect stroke itself too */ + gps->flag &= ~GP_STROKE_SELECT; + } + } + CTX_DATA_END; + } + + /* Perform selection operations... */ + if (whole) { + bGPDspoint *pt; + int i; + + /* entire stroke's points */ + for (i = 0, pt = hit_stroke->points; i < hit_stroke->totpoints; i++, pt++) { + if (deselect == false) + pt->flag |= GP_SPOINT_SELECT; + else + pt->flag &= ~GP_SPOINT_SELECT; + } + + /* stroke too... */ + if (deselect == false) + hit_stroke->flag |= GP_STROKE_SELECT; + else + hit_stroke->flag &= ~GP_STROKE_SELECT; + } + else { + /* just the point (and the stroke) */ + if (deselect == false) { + /* we're adding selection, so selection must be true */ + hit_point->flag |= GP_SPOINT_SELECT; + hit_stroke->flag |= GP_STROKE_SELECT; + } + else { + /* deselect point */ + hit_point->flag &= ~GP_SPOINT_SELECT; + + /* ensure that stroke is selected correctly */ + gpencil_stroke_sync_selection(hit_stroke); + } + } + + /* updates */ + if (hit_point != NULL) { + WM_event_add_notifier(C, NC_GPENCIL | NA_SELECTED, NULL); + } + + return OPERATOR_FINISHED; +} + +static int gpencil_select_invoke(bContext *C, wmOperator *op, const wmEvent *event) +{ + RNA_int_set_array(op->ptr, "location", event->mval); + return gpencil_select_exec(C, op); +} + +void GPENCIL_OT_select(wmOperatorType *ot) +{ + PropertyRNA *prop; + + /* identifiers */ + ot->name = "Select"; + ot->description = "Select Grease Pencil strokes and/or stroke points"; + ot->idname = "GPENCIL_OT_select"; + + /* callbacks */ + ot->invoke = gpencil_select_invoke; + ot->exec = gpencil_select_exec; + ot->poll = gpencil_select_poll; + + /* flag */ + ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO; + + /* properties */ + WM_operator_properties_mouse_select(ot); + + prop = RNA_def_boolean(ot->srna, "entire_strokes", false, "Entire Strokes", "Select entire strokes instead of just the nearest stroke vertex"); + RNA_def_property_flag(prop, PROP_SKIP_SAVE); + + prop = RNA_def_int_vector(ot->srna, "location", 2, NULL, INT_MIN, INT_MAX, "Location", "Mouse location", INT_MIN, INT_MAX); + RNA_def_property_flag(prop, PROP_HIDDEN); +} + +/* ********************************************** */ diff --git a/source/blender/editors/gpencil/gpencil_undo.c b/source/blender/editors/gpencil/gpencil_undo.c index ff92d69f39d..34e640a4b7b 100644 --- a/source/blender/editors/gpencil/gpencil_undo.c +++ b/source/blender/editors/gpencil/gpencil_undo.c @@ -29,6 +29,7 @@ * \ingroup edgpencil */ + #include <stdlib.h> #include <string.h> @@ -53,7 +54,7 @@ typedef struct bGPundonode { struct bGPundonode *next, *prev; - + char name[BKE_UNDO_STR_MAX]; struct bGPdata *gpd; } bGPundonode; @@ -69,13 +70,13 @@ int ED_gpencil_session_active(void) int ED_undo_gpencil_step(bContext *C, int step, const char *name) { bGPdata **gpd_ptr = NULL, *new_gpd = NULL; - + gpd_ptr = ED_gpencil_data_get_pointers(C, NULL); - + if (step == 1) { /* undo */ //printf("\t\tGP - undo step\n"); if (cur_node->prev) { - if (!name || strcmp(cur_node->name, name) == 0) { + if (!name || STREQ(cur_node->name, name)) { cur_node = cur_node->prev; new_gpd = cur_node->gpd; } @@ -84,24 +85,24 @@ int ED_undo_gpencil_step(bContext *C, int step, const char *name) else if (step == -1) { //printf("\t\tGP - redo step\n"); if (cur_node->next) { - if (!name || strcmp(cur_node->name, name) == 0) { + if (!name || STREQ(cur_node->name, name)) { cur_node = cur_node->next; new_gpd = cur_node->gpd; } } } - + if (new_gpd) { if (gpd_ptr) { if (*gpd_ptr) { bGPdata *gpd = *gpd_ptr; bGPDlayer *gpl, *gpld; - + free_gpencil_layers(&gpd->layers); - + /* copy layers */ BLI_listbase_clear(&gpd->layers); - + for (gpl = new_gpd->layers.first; gpl; gpl = gpl->next) { /* make a copy of source layer and its data */ gpld = gpencil_layer_duplicate(gpl); @@ -110,9 +111,9 @@ int ED_undo_gpencil_step(bContext *C, int step, const char *name) } } } - + WM_event_add_notifier(C, NC_GPENCIL | NA_EDITED, NULL); - + return OPERATOR_FINISHED; } @@ -124,46 +125,56 @@ void gpencil_undo_init(bGPdata *gpd) void gpencil_undo_push(bGPdata *gpd) { bGPundonode *undo_node; - + //printf("\t\tGP - undo push\n"); - + if (cur_node) { /* remove all un-done nodes from stack */ undo_node = cur_node->next; - + while (undo_node) { bGPundonode *next_node = undo_node->next; - + + /* HACK: animdata wasn't duplicated, so it shouldn't be freed here, + * or else the real copy will segfault when accessed + */ + undo_node->gpd->adt = NULL; + BKE_gpencil_free(undo_node->gpd); MEM_freeN(undo_node->gpd); - + BLI_freelinkN(&undo_nodes, undo_node); - + undo_node = next_node; } } - + /* create new undo node */ undo_node = MEM_callocN(sizeof(bGPundonode), "gpencil undo node"); - undo_node->gpd = gpencil_data_duplicate(gpd); - + undo_node->gpd = gpencil_data_duplicate(gpd, true); + cur_node = undo_node; - + BLI_addtail(&undo_nodes, undo_node); } void gpencil_undo_finish(void) { bGPundonode *undo_node = undo_nodes.first; - + while (undo_node) { + /* HACK: animdata wasn't duplicated, so it shouldn't be freed here, + * or else the real copy will segfault when accessed + */ + undo_node->gpd->adt = NULL; + BKE_gpencil_free(undo_node->gpd); MEM_freeN(undo_node->gpd); - + undo_node = undo_node->next; } - + BLI_freelistN(&undo_nodes); - + cur_node = NULL; } diff --git a/source/blender/editors/gpencil/gpencil_utils.c b/source/blender/editors/gpencil/gpencil_utils.c new file mode 100644 index 00000000000..0d7aac7f48f --- /dev/null +++ b/source/blender/editors/gpencil/gpencil_utils.c @@ -0,0 +1,375 @@ +/* + * ***** BEGIN GPL LICENSE BLOCK ***** + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + * + * The Original Code is Copyright (C) 2014, Blender Foundation + * + * Contributor(s): Joshua Leung + * + * ***** END GPL LICENSE BLOCK ***** + */ + +/** \file blender/editors/gpencil/gpencil_utils.c + * \ingroup edgpencil + */ + +#include <stdio.h> +#include <string.h> +#include <stdlib.h> +#include <stddef.h> +#include <math.h> + +#include "BLI_math.h" +#include "BLI_blenlib.h" +#include "BLI_utildefines.h" + +#include "DNA_gpencil_types.h" +#include "DNA_object_types.h" +#include "DNA_scene_types.h" +#include "DNA_screen_types.h" +#include "DNA_space_types.h" +#include "DNA_view3d_types.h" + +#include "BKE_context.h" +#include "BKE_gpencil.h" +#include "BKE_tracking.h" + +#include "WM_api.h" + +#include "RNA_access.h" +#include "RNA_define.h" + +#include "UI_view2d.h" + +#include "ED_gpencil.h" +#include "ED_clip.h" +#include "ED_view3d.h" + +#include "gpencil_intern.h" + +/* ******************************************************** */ +/* Context Wrangling... */ + +/* Get pointer to active Grease Pencil datablock, and an RNA-pointer to trace back to whatever owns it, + * when context info is not available. + */ +bGPdata **ED_gpencil_data_get_pointers_direct(ID *screen_id, Scene *scene, ScrArea *sa, Object *ob, PointerRNA *ptr) +{ + /* if there's an active area, check if the particular editor may + * have defined any special Grease Pencil context for editing... + */ + if (sa) { + SpaceLink *sl = sa->spacedata.first; + + switch (sa->spacetype) { + case SPACE_VIEW3D: /* 3D-View */ + case SPACE_TIME: /* Timeline - XXX: this is a hack to get it to show GP keyframes for 3D view */ + case SPACE_ACTION: /* DepeSheet - XXX: this is a hack to get the keyframe jump operator to take GP Keyframes into account */ + { + BLI_assert(scene && ELEM(scene->toolsettings->gpencil_src, + GP_TOOL_SOURCE_SCENE, GP_TOOL_SOURCE_OBJECT)); + + if (scene->toolsettings->gpencil_src == GP_TOOL_SOURCE_OBJECT) { + /* legacy behaviour for usage with old addons requiring object-linked to objects */ + + /* just in case no active/selected object... */ + if (ob && (ob->flag & SELECT)) { + /* for now, as long as there's an object, default to using that in 3D-View */ + if (ptr) RNA_id_pointer_create(&ob->id, ptr); + return &ob->gpd; + } + /* else: defaults to scene... */ + } + else { + if (ptr) RNA_id_pointer_create(&scene->id, ptr); + return &scene->gpd; + } + break; + } + case SPACE_NODE: /* Nodes Editor */ + { + SpaceNode *snode = (SpaceNode *)sl; + + /* return the GP data for the active node block/node */ + if (snode && snode->nodetree) { + /* for now, as long as there's an active node tree, default to using that in the Nodes Editor */ + if (ptr) RNA_id_pointer_create(&snode->nodetree->id, ptr); + return &snode->nodetree->gpd; + } + + /* even when there is no node-tree, don't allow this to flow to scene */ + return NULL; + } + case SPACE_SEQ: /* Sequencer */ + { + SpaceSeq *sseq = (SpaceSeq *)sl; + + /* for now, Grease Pencil data is associated with the space (actually preview region only) */ + /* XXX our convention for everything else is to link to data though... */ + if (ptr) RNA_pointer_create(screen_id, &RNA_SpaceSequenceEditor, sseq, ptr); + return &sseq->gpd; + } + case SPACE_IMAGE: /* Image/UV Editor */ + { + SpaceImage *sima = (SpaceImage *)sl; + + /* for now, Grease Pencil data is associated with the space... */ + /* XXX our convention for everything else is to link to data though... */ + if (ptr) RNA_pointer_create(screen_id, &RNA_SpaceImageEditor, sima, ptr); + return &sima->gpd; + } + case SPACE_CLIP: /* Nodes Editor */ + { + SpaceClip *sc = (SpaceClip *)sl; + MovieClip *clip = ED_space_clip_get_clip(sc); + + if (clip) { + if (sc->gpencil_src == SC_GPENCIL_SRC_TRACK) { + MovieTrackingTrack *track = BKE_tracking_track_get_active(&clip->tracking); + + if (!track) + return NULL; + + if (ptr) + RNA_pointer_create(&clip->id, &RNA_MovieTrackingTrack, track, ptr); + + return &track->gpd; + } + else { + if (ptr) + RNA_id_pointer_create(&clip->id, ptr); + + return &clip->gpd; + } + } + break; + } + default: /* unsupported space */ + return NULL; + } + } + + /* just fall back on the scene's GP data */ + if (ptr) RNA_id_pointer_create((ID *)scene, ptr); + return (scene) ? &scene->gpd : NULL; +} + +/* Get pointer to active Grease Pencil datablock, and an RNA-pointer to trace back to whatever owns it */ +bGPdata **ED_gpencil_data_get_pointers(const bContext *C, PointerRNA *ptr) +{ + ID *screen_id = (ID *)CTX_wm_screen(C); + Scene *scene = CTX_data_scene(C); + ScrArea *sa = CTX_wm_area(C); + Object *ob = CTX_data_active_object(C); + + return ED_gpencil_data_get_pointers_direct(screen_id, scene, sa, ob, ptr); +} + +/* -------------------------------------------------------- */ + +/* Get the active Grease Pencil datablock, when context is not available */ +bGPdata *ED_gpencil_data_get_active_direct(ID *screen_id, Scene *scene, ScrArea *sa, Object *ob) +{ + bGPdata **gpd_ptr = ED_gpencil_data_get_pointers_direct(screen_id, scene, sa, ob, NULL); + return (gpd_ptr) ? *(gpd_ptr) : NULL; +} + +/* Get the active Grease Pencil datablock */ +bGPdata *ED_gpencil_data_get_active(const bContext *C) +{ + bGPdata **gpd_ptr = ED_gpencil_data_get_pointers(C, NULL); + return (gpd_ptr) ? *(gpd_ptr) : NULL; +} + +/* -------------------------------------------------------- */ + +// XXX: this should be removed... We really shouldn't duplicate logic like this! +bGPdata *ED_gpencil_data_get_active_v3d(Scene *scene, View3D *v3d) +{ + Base *base = scene->basact; + bGPdata *gpd = NULL; + /* We have to make sure active object is actually visible and selected, else we must use default scene gpd, + * to be consistent with ED_gpencil_data_get_active's behavior. + */ + + if (base && TESTBASE(v3d, base)) { + gpd = base->object->gpd; + } + return gpd ? gpd : scene->gpd; +} + +/* ******************************************************** */ +/* Poll Callbacks */ + +/* poll callback for adding data/layers - special */ +int gp_add_poll(bContext *C) +{ + /* the base line we have is that we have somewhere to add Grease Pencil data */ + return ED_gpencil_data_get_pointers(C, NULL) != NULL; +} + +/* poll callback for checking if there is an active layer */ +int gp_active_layer_poll(bContext *C) +{ + bGPdata *gpd = ED_gpencil_data_get_active(C); + bGPDlayer *gpl = gpencil_layer_getactive(gpd); + + return (gpl != NULL); +} + +/* ******************************************************** */ +/* Brush Tool Core */ + +/* Check if part of stroke occurs within last segment drawn by eraser */ +bool gp_stroke_inside_circle(const int mval[2], const int UNUSED(mvalo[2]), + int rad, int x0, int y0, int x1, int y1) +{ + /* simple within-radius check for now */ + const float mval_fl[2] = {mval[0], mval[1]}; + const float screen_co_a[2] = {x0, y0}; + const float screen_co_b[2] = {x1, y1}; + + if (edge_inside_circle(mval_fl, rad, screen_co_a, screen_co_b)) { + return true; + } + + /* not inside */ + return false; +} + +/* ******************************************************** */ +/* Stroke Validity Testing */ + +/* Check whether given stroke can be edited given the supplied context */ +// XXX: do we need additional flags for screenspace vs dataspace? +bool ED_gpencil_stroke_can_use_direct(const ScrArea *sa, const bGPDstroke *gps) +{ + /* sanity check */ + if (ELEM(NULL, sa, gps)) + return false; + + /* filter stroke types by flags + spacetype */ + if (gps->flag & GP_STROKE_3DSPACE) { + /* 3D strokes - only in 3D view */ + return (sa->spacetype == SPACE_VIEW3D); + } + else if (gps->flag & GP_STROKE_2DIMAGE) { + /* Special "image" strokes - only in Image Editor */ + return (sa->spacetype == SPACE_IMAGE); + } + else if (gps->flag & GP_STROKE_2DSPACE) { + /* 2D strokes (dataspace) - for any 2D view (i.e. everything other than 3D view) */ + return (sa->spacetype != SPACE_VIEW3D); + } + else { + /* view aligned - anything goes */ + return true; + } +} + +/* Check whether given stroke can be edited in the current context */ +bool ED_gpencil_stroke_can_use(const bContext *C, const bGPDstroke *gps) +{ + ScrArea *sa = CTX_wm_area(C); + return ED_gpencil_stroke_can_use_direct(sa, gps); +} + +/* ******************************************************** */ +/* Space Conversion */ + +/* Init handling for space-conversion function (from passed-in parameters) */ +void gp_point_conversion_init(bContext *C, GP_SpaceConversion *r_gsc) +{ + ScrArea *sa = CTX_wm_area(C); + ARegion *ar = CTX_wm_region(C); + + /* zero out the storage (just in case) */ + memset(r_gsc, 0, sizeof(GP_SpaceConversion)); + unit_m4(r_gsc->mat); + + /* store settings */ + r_gsc->sa = sa; + r_gsc->ar = ar; + r_gsc->v2d = &ar->v2d; + + /* init region-specific stuff */ + if (sa->spacetype == SPACE_VIEW3D) { + wmWindow *win = CTX_wm_window(C); + Scene *scene = CTX_data_scene(C); + View3D *v3d = (View3D *)CTX_wm_space_data(C); + RegionView3D *rv3d = ar->regiondata; + + /* init 3d depth buffers */ + view3d_operator_needs_opengl(C); + + view3d_region_operator_needs_opengl(win, ar); + ED_view3d_autodist_init(scene, ar, v3d, 0); + + /* for camera view set the subrect */ + if (rv3d->persp == RV3D_CAMOB) { + ED_view3d_calc_camera_border(scene, ar, v3d, rv3d, &r_gsc->subrect_data, true); /* no shift */ + r_gsc->subrect = &r_gsc->subrect_data; + } + } +} + + +/* Convert Grease Pencil points to screen-space values + * WARNING: This assumes that the caller has already checked whether the stroke in question can be drawn + */ +void gp_point_to_xy(GP_SpaceConversion *gsc, bGPDstroke *gps, bGPDspoint *pt, + int *r_x, int *r_y) +{ + ARegion *ar = gsc->ar; + View2D *v2d = gsc->v2d; + rctf *subrect = gsc->subrect; + int xyval[2]; + + /* sanity checks */ + BLI_assert(!(gps->flag & GP_STROKE_3DSPACE) || (gsc->sa->spacetype == SPACE_VIEW3D)); + BLI_assert(!(gps->flag & GP_STROKE_2DSPACE) || (gsc->sa->spacetype != SPACE_VIEW3D)); + + + if (gps->flag & GP_STROKE_3DSPACE) { + if (ED_view3d_project_int_global(ar, &pt->x, xyval, V3D_PROJ_TEST_NOP) == V3D_PROJ_RET_OK) { + *r_x = xyval[0]; + *r_y = xyval[1]; + } + else { + *r_x = V2D_IS_CLIPPED; + *r_y = V2D_IS_CLIPPED; + } + } + else if (gps->flag & GP_STROKE_2DSPACE) { + float vec[3] = {pt->x, pt->y, 0.0f}; + mul_m4_v3(gsc->mat, vec); + UI_view2d_view_to_region_clip(v2d, vec[0], vec[1], r_x, r_y); + } + else { + if (subrect == NULL) { + /* normal 3D view (or view space) */ + *r_x = (int)(pt->x / 100 * ar->winx); + *r_y = (int)(pt->y / 100 * ar->winy); + } + else { + /* camera view, use subrect */ + *r_x = (int)((pt->x / 100) * BLI_rctf_size_x(subrect)) + subrect->xmin; + *r_y = (int)((pt->y / 100) * BLI_rctf_size_y(subrect)) + subrect->ymin; + } + } +} + +/* ******************************************************** */ |