[Bf-blender-cvs] [1369e9d298a] greasepencil-object: Palette Slots - Add operators to Add/Remove Palette Slots

Joshua Leung noreply at git.blender.org
Wed Oct 4 14:17:21 CEST 2017


Commit: 1369e9d298aa5d2b137516dfcf65525ac48178f1
Author: Joshua Leung
Date:   Wed Oct 4 17:12:51 2017 +1300
Branches: greasepencil-object
https://developer.blender.org/rB1369e9d298aa5d2b137516dfcf65525ac48178f1

Palette Slots - Add operators to Add/Remove Palette Slots

* Add will create a new (empty) slot by default

* Remove will shown an error if the slot is currently still used by
  any strokes. Perhaps in future, it should remap the strokes instead?

* Added icon for Palette Slots (it uses the same icon as Palettes now)

===================================================================

M	release/scripts/startup/bl_ui/properties_material_gpencil.py
M	source/blender/blenkernel/BKE_gpencil.h
M	source/blender/blenkernel/intern/gpencil.c
M	source/blender/editors/gpencil/gpencil_data.c
M	source/blender/editors/gpencil/gpencil_intern.h
M	source/blender/editors/gpencil/gpencil_ops.c
M	source/blender/editors/include/ED_gpencil.h
M	source/blender/makesrna/intern/rna_gpencil.c

===================================================================

diff --git a/release/scripts/startup/bl_ui/properties_material_gpencil.py b/release/scripts/startup/bl_ui/properties_material_gpencil.py
index 5d6856a9fa1..7d48e3af07b 100644
--- a/release/scripts/startup/bl_ui/properties_material_gpencil.py
+++ b/release/scripts/startup/bl_ui/properties_material_gpencil.py
@@ -75,8 +75,8 @@ class MATERIAL_PT_gpencil_palette_slots(Panel):
         col = row.column()
 
         sub = col.column(align=True)
-        sub.operator("palette.color_add", icon='ZOOMIN', text="").grease_pencil = True
-        sub.operator("palette.color_delete", icon='ZOOMOUT', text="")
+        sub.operator("gpencil.palette_slot_add", icon='ZOOMIN', text="")
+        sub.operator("gpencil.palette_slot_remove", icon='ZOOMOUT', text="")
 
         sub = col.column(align=True)
         sub.operator_menu_enum("gpencil.stroke_change_palette", text="", icon='ARROW_LEFTRIGHT', property="type")
diff --git a/source/blender/blenkernel/BKE_gpencil.h b/source/blender/blenkernel/BKE_gpencil.h
index 60627b0d6ed..00772ac6a38 100644
--- a/source/blender/blenkernel/BKE_gpencil.h
+++ b/source/blender/blenkernel/BKE_gpencil.h
@@ -150,6 +150,7 @@ void BKE_gpencil_brush_delete(struct ToolSettings *ts, struct bGPDbrush *brush);
 void BKE_gpencil_palette_slot_free(struct bGPdata *gpd, struct bGPDpaletteref *palslot);
 
 struct bGPDpaletteref *BKE_gpencil_paletteslot_find(struct bGPdata *gpd, const struct Palette *palette);
+bool BKE_gpencil_paletteslot_has_users(const struct bGPdata *gpd, const struct bGPDpaletteref *palslot);
 
 struct bGPDpaletteref *BKE_gpencil_paletteslot_get_active(const struct bGPdata *gpd);
 void BKE_gpencil_paletteslot_set_active(struct bGPdata *gpd, const struct bGPDpaletteref *palslot);
diff --git a/source/blender/blenkernel/intern/gpencil.c b/source/blender/blenkernel/intern/gpencil.c
index cacd97ea5db..391450627b4 100644
--- a/source/blender/blenkernel/intern/gpencil.c
+++ b/source/blender/blenkernel/intern/gpencil.c
@@ -1463,6 +1463,34 @@ bGPDpaletteref *BKE_gpencil_paletteslot_find(bGPdata *gpd, const Palette *palett
 	return NULL;
 }
 
+/* Check if palette is used by any strokes... */
+bool BKE_gpencil_paletteslot_has_users(const bGPdata *gpd, const bGPDpaletteref *palslot)
+{
+	Palette *palette;
+	bGPDlayer *gpl;
+	bGPDframe *gpf;
+	bGPDstroke *gps;
+	
+	/* quick-exit checks */
+	if (ELEM(NULL, gpd, palslot, palslot->palette))
+		return false;
+	
+	palette = palslot->palette;
+	
+	/* check if any strokes use this */
+	for (gpl = gpd->layers.first; gpl; gpl = gpl->next) {
+		for (gpf = gpl->frames.first; gpf; gpf = gpf->next) {
+			for (gps = gpf->strokes.first; gps; gps = gps->next) {
+				if (gps->palette == palette)
+					return true;
+			}
+		}
+	}
+	
+	/* nothing found */
+	return false;
+}
+
 /* Add Slots --------------------------------------- */
 
 /* Create a new palette slot (and optionally assign a palette to it) */
@@ -1493,6 +1521,9 @@ bGPDpaletteref *BKE_gpencil_paletteslot_add(bGPdata *gpd, Palette *palette)
 		id_us_plus(&palette->id);
 	}
 	
+	/* update active palette */
+	gpd->active_palette_slot = BLI_listbase_count(&gpd->palette_slots) - 1;
+	
 	/* return new slot */
 	return palslot;
 }
diff --git a/source/blender/editors/gpencil/gpencil_data.c b/source/blender/editors/gpencil/gpencil_data.c
index b4d9422252c..f9f4280f1dd 100644
--- a/source/blender/editors/gpencil/gpencil_data.c
+++ b/source/blender/editors/gpencil/gpencil_data.c
@@ -1103,6 +1103,88 @@ void GPENCIL_OT_stroke_lock_color(wmOperatorType *ot)
 }
 
 /* ************************************************ */
+/* Palette Slot Operators */
+
+/* ********************* Add Palette SLot ************************* */
+
+static int gp_paletteslot_add_exec(bContext *C, wmOperator *UNUSED(op))
+{
+	bGPdata *gpd = CTX_data_gpencil_data(C);
+	
+	/* just add an empty slot */
+	BKE_gpencil_paletteslot_add(gpd, NULL);
+	WM_event_add_notifier(C, NC_GPENCIL | ND_DATA | NA_ADDED, NULL);
+	
+	return OPERATOR_FINISHED;
+}
+
+void GPENCIL_OT_palette_slot_add(wmOperatorType *ot)
+{
+	/* identifiers */
+	ot->name = "Add Palette Slot";
+	ot->idname = "GPENCIL_OT_palette_slot_add";
+	ot->description = "Add new Palette Slot to refer to a Palette used by this Grease Pencil object";
+	
+	/* callbacks */
+	ot->exec = gp_paletteslot_add_exec;
+	ot->poll = gp_active_layer_poll; // XXX
+	
+	/* flags */
+	ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
+}
+
+/* ******************* Remove Palette Slot *********************** */
+
+static int gp_paletteslot_active_poll(bContext *C)
+{
+	bGPdata *gpd = ED_gpencil_data_get_active(C);
+	bGPDpaletteref *palslot = BKE_gpencil_paletteslot_get_active(gpd);
+
+	return (palslot != NULL);
+}
+
+static int gp_paletteslot_remove_exec(bContext *C, wmOperator *op)
+{
+	bGPdata *gpd = ED_gpencil_data_get_active(C);
+	bGPDpaletteref *palslot = BKE_gpencil_paletteslot_get_active(gpd);
+	
+	/* 1) Check if palette is still used anywhere */
+	if (BKE_gpencil_paletteslot_has_users(gpd, palslot)) {
+		/* XXX: Change strokes to the new active slot's palette instead? */
+		BKE_report(op->reports, RPT_ERROR, "Cannot remove, Palette still in use");
+		return OPERATOR_CANCELLED;
+	}
+	
+	/* 2) Remove the slot (will unlink user and free it) */
+	if ((palslot->next == NULL) && (gpd->active_palette_slot > 0)) {
+		/* fix active slot index */
+		gpd->active_palette_slot--;
+	}
+	
+	BKE_gpencil_palette_slot_free(gpd, palslot);
+		
+	/* updates */
+	WM_event_add_notifier(C, NC_GPENCIL | ND_DATA | NA_REMOVED, NULL);
+	return OPERATOR_FINISHED;
+}
+
+void GPENCIL_OT_palette_slot_remove(wmOperatorType *ot)
+{
+	/* identifiers */
+	ot->name = "Remove Palette Slot";
+	ot->idname = "GPENCIL_OT_palette_slot_remove";
+	ot->description = "Remove active Palette Slot to refer to a Palette used by this Grease Pencil object";
+	
+	/* callbacks */
+	ot->exec = gp_paletteslot_remove_exec;
+	ot->poll = gp_paletteslot_active_poll;
+	
+	/* flags */
+	ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
+}
+
+
+/* ************************************************ */
 /* Drawing Brushes Operators */
 
 /* ******************* Add New Brush ************************ */
diff --git a/source/blender/editors/gpencil/gpencil_intern.h b/source/blender/editors/gpencil/gpencil_intern.h
index 79ab410a828..87daefd1bd4 100644
--- a/source/blender/editors/gpencil/gpencil_intern.h
+++ b/source/blender/editors/gpencil/gpencil_intern.h
@@ -257,6 +257,8 @@ void GPENCIL_OT_brush_select(struct wmOperatorType *ot);
 
 void GPENCIL_OT_sculpt_select(struct wmOperatorType *ot);
 
+void GPENCIL_OT_palette_slot_add(struct wmOperatorType *ot);
+void GPENCIL_OT_palette_slot_remove(struct wmOperatorType *ot);
 
 void GPENCIL_OT_convert_old_palettes(struct wmOperatorType *ot);
 
diff --git a/source/blender/editors/gpencil/gpencil_ops.c b/source/blender/editors/gpencil/gpencil_ops.c
index 3e1bb684123..3f43caab975 100644
--- a/source/blender/editors/gpencil/gpencil_ops.c
+++ b/source/blender/editors/gpencil/gpencil_ops.c
@@ -685,6 +685,10 @@ void ED_operatortypes_gpencil(void)
 	WM_operatortype_append(GPENCIL_OT_brush_select);
 
 	WM_operatortype_append(GPENCIL_OT_sculpt_select);
+	
+	/* palette slots */
+	WM_operatortype_append(GPENCIL_OT_palette_slot_add);
+	WM_operatortype_append(GPENCIL_OT_palette_slot_remove);
 
 	/* conversion of old palettes */
 	WM_operatortype_append(GPENCIL_OT_convert_old_palettes);
diff --git a/source/blender/editors/include/ED_gpencil.h b/source/blender/editors/include/ED_gpencil.h
index d165d019df2..fae3e7c9922 100644
--- a/source/blender/editors/include/ED_gpencil.h
+++ b/source/blender/editors/include/ED_gpencil.h
@@ -142,6 +142,7 @@ bool ED_gpencil_stroke_color_use(const struct bGPDlayer *gpl, const struct bGPDs
 bool ED_gpencil_stroke_minmax(
         const struct bGPDstroke *gps, const bool use_select,
         float r_min[3], float r_max[3]);
+
 /* ----------- Grease Pencil Operators ----------------- */
 
 void ED_keymap_gpencil(struct wmKeyConfig *keyconf);
diff --git a/source/blender/makesrna/intern/rna_gpencil.c b/source/blender/makesrna/intern/rna_gpencil.c
index b36cbee58d9..8aa8ad29839 100644
--- a/source/blender/makesrna/intern/rna_gpencil.c
+++ b/source/blender/makesrna/intern/rna_gpencil.c
@@ -737,6 +737,7 @@ static void rna_def_gpencil_palette_slot(BlenderRNA *brna)
 	RNA_def_struct_sdna(srna, "bGPDpaletteref");
 	RNA_def_struct_path_func(srna, "rna_GPencilPaletteSlot_path");
 	RNA_def_struct_ui_text(srna, "Grease Pencil Palette Slot", "Reference for a Palette used in Grease Pencil datablock");
+	RNA_def_struct_ui_icon(srna, ICON_COLOR);
 	
 	prop = RNA_def_property(srna, "name", PROP_STRING, PROP_NONE);
 	RNA_def_property_string_funcs(prop, "rna_GPencilPaletteSlot_name_get", "rna_GPencilPaletteSlot_name_length", NULL);



More information about the Bf-blender-cvs mailing list