[Bf-blender-cvs] [2919519] master: RNA: palette colors api

Campbell Barton noreply at git.blender.org
Wed Mar 18 06:11:37 CET 2015


Commit: 29195197147a882849859ea963cae42484dfeaba
Author: Campbell Barton
Date:   Wed Mar 18 15:09:04 2015 +1100
Branches: master
https://developer.blender.org/rB29195197147a882849859ea963cae42484dfeaba

RNA: palette colors api

Methods so Python can manage colors.
palette.colors.new()/remove()/clear()/active

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

M	source/blender/blenkernel/BKE_paint.h
M	source/blender/blenkernel/intern/paint.c
M	source/blender/editors/sculpt_paint/paint_ops.c
M	source/blender/editors/sculpt_paint/paint_utils.c
M	source/blender/makesrna/intern/rna_palette.c

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

diff --git a/source/blender/blenkernel/BKE_paint.h b/source/blender/blenkernel/BKE_paint.h
index 3e4e6ab..58c0b49 100644
--- a/source/blender/blenkernel/BKE_paint.h
+++ b/source/blender/blenkernel/BKE_paint.h
@@ -102,8 +102,10 @@ void                 BKE_palette_free(struct Palette *palette);
 struct Palette      *BKE_palette_add(struct Main *bmain, const char *name);
 struct PaletteColor *BKE_palette_color_add(struct Palette *palette);
 bool                 BKE_palette_is_empty(const struct Palette *palette);
+void                 BKE_palette_color_remove_ex(struct Palette *palette, struct PaletteColor *color, bool use_free);
 void                 BKE_palette_color_remove(struct Palette *palette, struct PaletteColor *color);
 void                 BKE_palette_cleanup(struct Palette *palette);
+void                 BKE_palette_clear(struct Palette *palette);
 
 /* paint curves */
 struct PaintCurve *BKE_paint_curve_add(struct Main *bmain, const char *name);
diff --git a/source/blender/blenkernel/intern/paint.c b/source/blender/blenkernel/intern/paint.c
index 431eec0..2082066 100644
--- a/source/blender/blenkernel/intern/paint.c
+++ b/source/blender/blenkernel/intern/paint.c
@@ -314,18 +314,38 @@ void BKE_paint_curve_set(Brush *br, PaintCurve *pc)
 }
 
 /* remove colour from palette. Must be certain color is inside the palette! */
-void BKE_palette_color_remove(Palette *palette, PaletteColor *color)
+void BKE_palette_color_remove_ex(Palette *palette, PaletteColor *color, bool use_free)
 {
-	if (color) {
-		int numcolors = BLI_listbase_count(&palette->colors);
-		if ((numcolors == palette->active_color + 1) && (numcolors != 1))
-			palette->active_color--;
-		
-		BLI_remlink(&palette->colors, color);
+	if (BLI_listbase_count_ex(&palette->colors, palette->active_color) == palette->active_color) {
+		palette->active_color--;
+	}
+
+	BLI_remlink(&palette->colors, color);
+
+	if (palette->active_color < 0 && !BLI_listbase_is_empty(&palette->colors)) {
+		palette->active_color = 0;
+	}
+
+	if (use_free) {
+		MEM_freeN(color);
+	}
+	else {
 		BLI_addhead(&palette->deleted, color);
 	}
 }
 
+void BKE_palette_color_remove(Palette *palette, PaletteColor *color)
+{
+	BKE_palette_color_remove_ex(palette, color, false);
+}
+
+void BKE_palette_clear(Palette *palette)
+{
+	BLI_freelistN(&palette->colors);
+	BLI_freelistN(&palette->deleted);
+	palette->active_color = 0;
+}
+
 void BKE_palette_cleanup(Palette *palette)
 {
 	BLI_freelistN(&palette->deleted);
@@ -353,7 +373,6 @@ PaletteColor *BKE_palette_color_add(Palette *palette)
 {
 	PaletteColor *color = MEM_callocN(sizeof(*color), "Pallete Color");
 	BLI_addtail(&palette->colors, color);
-	palette->active_color = BLI_listbase_count(&palette->colors) - 1;
 	return color;
 }
 
diff --git a/source/blender/editors/sculpt_paint/paint_ops.c b/source/blender/editors/sculpt_paint/paint_ops.c
index 121b0b8..eebd498 100644
--- a/source/blender/editors/sculpt_paint/paint_ops.c
+++ b/source/blender/editors/sculpt_paint/paint_ops.c
@@ -196,7 +196,10 @@ static int palette_color_add_exec(bContext *C, wmOperator *UNUSED(op))
 	Brush *brush = paint->brush;
 	PaintMode mode = BKE_paintmode_get_active_from_context(C);
 	Palette *palette = paint->palette;
-	PaletteColor *color = BKE_palette_color_add(palette);
+	PaletteColor *color;
+
+	color = BKE_palette_color_add(palette);
+	palette->active_color = BLI_listbase_count(&palette->colors) - 1;
 
 	if (ELEM(mode, PAINT_TEXTURE_PROJECTIVE, PAINT_TEXTURE_2D, PAINT_VERTEX)) {
 		copy_v3_v3(color->rgb, BKE_brush_color_get(scene, brush));
@@ -231,7 +234,9 @@ static int palette_color_delete_exec(bContext *C, wmOperator *UNUSED(op))
 	Palette *palette = paint->palette;
 	PaletteColor *color = BLI_findlink(&palette->colors, palette->active_color);
 
-	BKE_palette_color_remove(palette, color);
+	if (color) {
+		BKE_palette_color_remove(palette, color);
+	}
 
 	return OPERATOR_FINISHED;
 }
diff --git a/source/blender/editors/sculpt_paint/paint_utils.c b/source/blender/editors/sculpt_paint/paint_utils.c
index c0ed500..b19cced 100644
--- a/source/blender/editors/sculpt_paint/paint_utils.c
+++ b/source/blender/editors/sculpt_paint/paint_utils.c
@@ -439,6 +439,7 @@ void paint_sample_color(bContext *C, ARegion *ar, int x, int y, bool texpaint_pr
 		}
 
 		color = BKE_palette_color_add(palette);
+		palette->active_color = BLI_listbase_count(&palette->colors) - 1;
 	}
 
 
diff --git a/source/blender/makesrna/intern/rna_palette.c b/source/blender/makesrna/intern/rna_palette.c
index 54e5881..7405df9 100644
--- a/source/blender/makesrna/intern/rna_palette.c
+++ b/source/blender/makesrna/intern/rna_palette.c
@@ -27,6 +27,7 @@
 #include "BLI_utildefines.h"
 
 #include "RNA_define.h"
+#include "RNA_access.h"
 
 #include "rna_internal.h"
 
@@ -36,8 +37,96 @@
 
 #include "DNA_brush_types.h"
 
+#include "BKE_paint.h"
+#include "BKE_report.h"
+
+static PaletteColor *rna_Palette_color_new(Palette *palette)
+{
+	PaletteColor *color = BKE_palette_color_add(palette);
+	return color;
+}
+
+static void rna_Palette_color_remove(Palette *palette, ReportList *reports, PointerRNA *color_ptr)
+{
+	PaletteColor *color = color_ptr->data;
+
+	if (BLI_findindex(&palette->colors, color) == -1) {
+		BKE_reportf(reports, RPT_ERROR, "Palette '%s' does not contain color given", palette->id.name + 2);
+		return;
+	}
+
+	BKE_palette_color_remove_ex(palette, color, true);
+
+	RNA_POINTER_INVALIDATE(color_ptr);
+}
+
+static void rna_Palette_color_clear(Palette *palette)
+{
+	BKE_palette_clear(palette);
+}
+
+static PointerRNA rna_Palette_active_color_get(PointerRNA *ptr)
+{
+	Palette *palette = ptr->data;
+	PaletteColor *color;
+
+	color = BLI_findlink(&palette->colors, palette->active_color);
+
+	if (color)
+		return rna_pointer_inherit_refine(ptr, &RNA_PaletteColor, color);
+
+	return rna_pointer_inherit_refine(ptr, NULL, NULL);
+}
+
+static void rna_Palette_active_color_set(PointerRNA *ptr, PointerRNA value)
+{
+	Palette *palette = ptr->data;
+	PaletteColor *color = value.data;
+
+	/* -1 is ok for an unset index */
+	if (color == NULL)
+		palette->active_color = -1;
+	else
+		palette->active_color = BLI_findindex(&palette->colors, color);
+}
+
 #else
 
+/* palette.colors */
+static void rna_def_palettecolors(BlenderRNA *brna, PropertyRNA *cprop)
+{
+	StructRNA *srna;
+	PropertyRNA *prop;
+
+	FunctionRNA *func;
+	PropertyRNA *parm;
+
+	RNA_def_property_srna(cprop, "PaletteColors");
+	srna = RNA_def_struct(brna, "PaletteColors", NULL);
+	RNA_def_struct_sdna(srna, "Palette");
+	RNA_def_struct_ui_text(srna, "Palette Splines", "Collection of palette colors");
+
+	func = RNA_def_function(srna, "new", "rna_Palette_color_new");
+	RNA_def_function_ui_description(func, "Add a new color to the palette");
+	parm = RNA_def_pointer(func, "color", "PaletteColor", "", "The newly created color");
+	RNA_def_function_return(func, parm);
+
+	func = RNA_def_function(srna, "remove", "rna_Palette_color_remove");
+	RNA_def_function_ui_description(func, "Remove a color from the palette");
+	RNA_def_function_flag(func, FUNC_USE_REPORTS);
+	parm = RNA_def_pointer(func, "color", "PaletteColor", "", "The color to remove");
+	RNA_def_property_flag(parm, PROP_REQUIRED | PROP_NEVER_NULL | PROP_RNAPTR);
+	RNA_def_property_clear_flag(parm, PROP_THICK_WRAP);
+
+	func = RNA_def_function(srna, "clear", "rna_Palette_color_clear");
+	RNA_def_function_ui_description(func, "Remove all colors from the palette");
+
+	prop = RNA_def_property(srna, "active", PROP_POINTER, PROP_NONE);
+	RNA_def_property_struct_type(prop, "PaletteColor");
+	RNA_def_property_pointer_funcs(prop, "rna_Palette_active_color_get", "rna_Palette_active_color_set", NULL, NULL);
+	RNA_def_property_flag(prop, PROP_EDITABLE);
+	RNA_def_property_ui_text(prop, "Active Palette Color", "");
+}
 
 static void rna_def_palettecolor(BlenderRNA *brna)
 {
@@ -77,8 +166,7 @@ static void rna_def_palette(BlenderRNA *brna)
 
 	prop = RNA_def_property(srna, "colors", PROP_COLLECTION, PROP_NONE);
 	RNA_def_property_struct_type(prop, "PaletteColor");
-	RNA_def_property_ui_text(prop, "Palette Color", "Colors that are part of this palette");
-	RNA_def_property_update(prop, NC_SCENE | ND_TOOLSETTINGS, NULL);
+	rna_def_palettecolors(brna, prop);
 }
 
 void RNA_def_palette(BlenderRNA *brna)




More information about the Bf-blender-cvs mailing list