Option not to select with un-hide
[blender.git] / source / blender / editors / gpencil / gpencil_data.c
index 01d388be658084b3fb9dff11390377bfafc42bfe..5bd5c9c74b96b98727c5ef1f53a0482f1798a6c0 100644 (file)
@@ -42,6 +42,7 @@
 #include "BLI_utildefines.h"
 #include "BLI_ghash.h"
 #include "BLI_math.h"
+#include "BLI_string_utils.h"
 
 #include "BLT_translation.h"
 
@@ -84,7 +85,8 @@
 static int gp_data_add_exec(bContext *C, wmOperator *op)
 {
        bGPdata **gpd_ptr = ED_gpencil_data_get_pointers(C, NULL);
-       
+       ToolSettings *ts = CTX_data_tool_settings(C);
+
        if (gpd_ptr == NULL) {
                BKE_report(op->reports, RPT_ERROR, "Nowhere for grease pencil data to go");
                return OPERATOR_CANCELLED;
@@ -95,6 +97,15 @@ static int gp_data_add_exec(bContext *C, wmOperator *op)
                
                id_us_min(&gpd->id);
                *gpd_ptr = BKE_gpencil_data_addnew(DATA_("GPencil"));
+
+               /* if not exist brushes, create a new set */
+               if (ts) {
+                       if (BLI_listbase_is_empty(&ts->gp_brushes)) {
+                               /* create new brushes */
+                               BKE_gpencil_brush_init_presets(ts);
+                       }
+               }
+
        }
        
        /* notifiers */
@@ -108,7 +119,7 @@ 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->description = "Add new Grease Pencil data-block";
        ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
        
        /* callbacks */
@@ -156,7 +167,7 @@ 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->description = "Unlink active Grease Pencil data-block";
        ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
        
        /* callbacks */
@@ -174,7 +185,8 @@ void GPENCIL_OT_data_unlink(wmOperatorType *ot)
 static int gp_layer_add_exec(bContext *C, wmOperator *op)
 {
        bGPdata **gpd_ptr = ED_gpencil_data_get_pointers(C, NULL);
-       
+       ToolSettings *ts = CTX_data_tool_settings(C);
+
        /* 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");
@@ -183,6 +195,14 @@ static int gp_layer_add_exec(bContext *C, wmOperator *op)
        if (*gpd_ptr == NULL)
                *gpd_ptr = BKE_gpencil_data_addnew(DATA_("GPencil"));
        
+       /* if not exist brushes, create a new set */
+       if (ts) {
+               if (BLI_listbase_is_empty(&ts->gp_brushes)) {
+                       /* create new brushes */
+                       BKE_gpencil_brush_init_presets(ts);
+               }
+       }
+
        /* add new layer now */
        BKE_gpencil_layer_addnew(*gpd_ptr, DATA_("GP_Layer"), true);
        
@@ -197,7 +217,7 @@ 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->description = "Add new Grease Pencil layer for the active Grease Pencil data-block";
        
        ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
        
@@ -272,27 +292,17 @@ static int gp_layer_move_exec(bContext *C, wmOperator *op)
        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);
+       BLI_assert(ELEM(direction, -1, 0, 1)); /* we use value below */
+       if (BLI_listbase_link_move(&gpd->layers, gpl, direction)) {
+               WM_event_add_notifier(C, NC_GPENCIL | ND_DATA | NA_EDITED, NULL);
        }
        
-       /* 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[] = {
+       static const EnumPropertyItem slot_move[] = {
                {GP_LAYER_MOVE_UP, "UP", 0, "Up", ""},
                {GP_LAYER_MOVE_DOWN, "DOWN", 0, "Down", ""},
                {0, NULL, 0, NULL, NULL}
@@ -413,18 +423,59 @@ 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))
+static void gp_reveal_select_frame(bContext *C, bGPDframe *frame, bool select)
+{
+       bGPDstroke *gps;
+       for (gps = frame->strokes.first; gps; gps = gps->next) {
+
+               /* only deselect strokes that are valid in this view */
+               if (ED_gpencil_stroke_can_use(C, gps)) {
+
+                       /* (de)select points */
+                       int i;
+                       bGPDspoint *pt;
+                       for (i = 0, pt = gps->points; i < gps->totpoints; i++, pt++) {
+                               SET_FLAG_FROM_TEST(pt->flag, select, GP_SPOINT_SELECT);
+                       }
+
+                       /* (de)select stroke */
+                       SET_FLAG_FROM_TEST(gps->flag, select, GP_STROKE_SELECT);
+               }
+       }
+}
+
+static int gp_reveal_exec(bContext *C, wmOperator *op)
 {
        bGPdata *gpd = ED_gpencil_data_get_active(C);
        bGPDlayer *gpl;
-       
+       const bool select = RNA_boolean_get(op->ptr, "select");
+
        /* 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;
+
+               if (gpl->flag & GP_LAYER_HIDE) {
+                       gpl->flag &= ~GP_LAYER_HIDE;
+
+                       /* select or deselect if requested, only on hidden layers */
+                       if (gpd->flag & GP_DATA_STROKE_EDITMODE) {
+                               if (select) {
+                                       /* select all strokes on active frame only (same as select all operator) */
+                                       if (gpl->actframe) {
+                                               gp_reveal_select_frame(C, gpl->actframe, true);
+                                       }
+                               }
+                               else {
+                                       /* deselect strokes on all frames (same as deselect all operator) */
+                                       bGPDframe *gpf;
+                                       for (gpf = gpl->frames.first; gpf; gpf = gpf->next) {
+                                               gp_reveal_select_frame(C, gpf, false);
+                                       }
+                               }
+                       }
+               }
        }
        
        /* notifiers */
@@ -446,6 +497,9 @@ void GPENCIL_OT_reveal(wmOperatorType *ot)
        
        /* flags */
        ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
+
+       /* props */
+       RNA_def_boolean(ot->srna, "select", true, "Select", "");
 }
 
 /* ***************** Lock/Unlock All Layers ************************ */
@@ -761,14 +815,12 @@ static int gp_stroke_arrange_exec(bContext *C, wmOperator *op)
                        /* some stroke is already at front*/
                        if ((direction == GP_STROKE_MOVE_TOP) || (direction == GP_STROKE_MOVE_UP)) {
                                if (gps == gpf->strokes.last) {
-                                       BKE_report(op->reports, RPT_ERROR, "Some selected stroke is already on top");
                                        return OPERATOR_CANCELLED;
                                }
                        }
                        /* some stroke is already at botom */
                        if ((direction == GP_STROKE_MOVE_BOTTOM) || (direction == GP_STROKE_MOVE_DOWN)) {
                                if (gps == gpf->strokes.first) {
-                                       BKE_report(op->reports, RPT_ERROR, "Some selected stroke is already on bottom");
                                        return OPERATOR_CANCELLED;
                                }
                        }
@@ -784,23 +836,21 @@ static int gp_stroke_arrange_exec(bContext *C, wmOperator *op)
                        for (LinkData *link = selected.first; link; link = link->next) {
                                gps = link->data;
                                BLI_remlink(&gpf->strokes, gps);
-                               BLI_insertlinkafter(&gpf->strokes, gpf->strokes.last, gps);
+                               BLI_addtail(&gpf->strokes, gps);
                        }
                        break;
                /* Bring Forward */
                case GP_STROKE_MOVE_UP:
                        for (LinkData *link = selected.last; link; link = link->prev) {
                                gps = link->data;
-                               BLI_remlink(&gpf->strokes, gps);
-                               BLI_insertlinkafter(&gpf->strokes, gps->next, gps);
+                               BLI_listbase_link_move(&gpf->strokes, gps, 1);
                        }
                        break;
-                       /* Send Backward */
+               /* Send Backward */
                case GP_STROKE_MOVE_DOWN:
                        for (LinkData *link = selected.first; link; link = link->next) {
                                gps = link->data;
-                               BLI_remlink(&gpf->strokes, gps);
-                               BLI_insertlinkbefore(&gpf->strokes, gps->prev, gps);
+                               BLI_listbase_link_move(&gpf->strokes, gps, -1);
                        }
                        break;
                /* Send to Back */
@@ -808,7 +858,7 @@ static int gp_stroke_arrange_exec(bContext *C, wmOperator *op)
                        for (LinkData *link = selected.last; link; link = link->prev) {
                                gps = link->data;
                                BLI_remlink(&gpf->strokes, gps);
-                               BLI_insertlinkbefore(&gpf->strokes, gpf->strokes.first, gps);
+                               BLI_addhead(&gpf->strokes, gps);
                        }
                        break;
                default:
@@ -825,7 +875,7 @@ static int gp_stroke_arrange_exec(bContext *C, wmOperator *op)
 
 void GPENCIL_OT_stroke_arrange(wmOperatorType *ot)
 {
-       static EnumPropertyItem slot_move[] = {
+       static const EnumPropertyItem slot_move[] = {
                {GP_STROKE_MOVE_UP, "UP", 0, "Bring Forward", ""},
                {GP_STROKE_MOVE_DOWN, "DOWN", 0, "Send Backward", ""},
                {GP_STROKE_MOVE_TOP, "TOP", 0, "Bring to Front", ""},
@@ -993,7 +1043,7 @@ void GPENCIL_OT_brush_add(wmOperatorType *ot)
        /* identifiers */
        ot->name = "Add Brush";
        ot->idname = "GPENCIL_OT_brush_add";
-       ot->description = "Add new Grease Pencil drawing brush for the active Grease Pencil datablock";
+       ot->description = "Add new Grease Pencil drawing brush for the active Grease Pencil data-block";
 
        ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
 
@@ -1159,7 +1209,7 @@ static int gp_brush_move_exec(bContext *C, wmOperator *op)
 
 void GPENCIL_OT_brush_move(wmOperatorType *ot)
 {
-       static EnumPropertyItem slot_move[] = {
+       static const EnumPropertyItem slot_move[] = {
                {GP_BRUSH_MOVE_UP, "UP", 0, "Up", ""},
                {GP_BRUSH_MOVE_DOWN, "DOWN", 0, "Down", ""},
                {0, NULL, 0, NULL, NULL }
@@ -1351,7 +1401,7 @@ void GPENCIL_OT_palette_add(wmOperatorType *ot)
        /* identifiers */
        ot->name = "Add Palette";
        ot->idname = "GPENCIL_OT_palette_add";
-       ot->description = "Add new Grease Pencil palette for the active Grease Pencil datablock";
+       ot->description = "Add new Grease Pencil palette for the active Grease Pencil data-block";
 
        ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
 
@@ -1568,7 +1618,7 @@ void GPENCIL_OT_palettecolor_add(wmOperatorType *ot)
        /* identifiers */
        ot->name = "Add Palette Color";
        ot->idname = "GPENCIL_OT_palettecolor_add";
-       ot->description = "Add new Grease Pencil palette color for the active Grease Pencil datablock";
+       ot->description = "Add new Grease Pencil palette color for the active Grease Pencil data-block";
 
        ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
 
@@ -1916,7 +1966,7 @@ static int gp_palettecolor_move_exec(bContext *C, wmOperator *op)
 
 void GPENCIL_OT_palettecolor_move(wmOperatorType *ot)
 {
-       static EnumPropertyItem slot_move[] = {
+       static const EnumPropertyItem slot_move[] = {
                {GP_COLOR_MOVE_UP, "UP", 0, "Up", ""},
                {GP_COLOR_MOVE_DOWN, "DOWN", 0, "Down", ""},
                {0, NULL, 0, NULL, NULL}