[Bf-blender-cvs] [8cbc7334f08] greasepencil-object: Palette Slots - Replace all places initialising GP palettes
Joshua Leung
noreply at git.blender.org
Wed Oct 4 14:17:01 CEST 2017
Commit: 8cbc7334f0885433638515fb0def273f28554472
Author: Joshua Leung
Date: Tue Oct 3 17:10:45 2017 +1300
Branches: greasepencil-object
https://developer.blender.org/rB8cbc7334f0885433638515fb0def273f28554472
Palette Slots - Replace all places initialising GP palettes
===================================================================
M source/blender/blenkernel/BKE_gpencil.h
M source/blender/blenkernel/BKE_paint.h
M source/blender/blenkernel/intern/gpencil.c
M source/blender/blenkernel/intern/paint.c
M source/blender/editors/gpencil/gpencil_edit.c
M source/blender/editors/gpencil/gpencil_paint.c
M source/blender/editors/gpencil/gpencil_utils.c
M source/blender/editors/sculpt_paint/paint_ops.c
M source/blender/editors/space_view3d/view3d_ruler.c
===================================================================
diff --git a/source/blender/blenkernel/BKE_gpencil.h b/source/blender/blenkernel/BKE_gpencil.h
index 07eaa9c4ec9..60627b0d6ed 100644
--- a/source/blender/blenkernel/BKE_gpencil.h
+++ b/source/blender/blenkernel/BKE_gpencil.h
@@ -149,13 +149,16 @@ void BKE_gpencil_brush_delete(struct ToolSettings *ts, struct bGPDbrush *brush);
/* Palette Slots */
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);
+
struct bGPDpaletteref *BKE_gpencil_paletteslot_get_active(const struct bGPdata *gpd);
void BKE_gpencil_paletteslot_set_active(struct bGPdata *gpd, const struct bGPDpaletteref *palslot);
void BKE_gpencil_paletteslot_set_active_palette(struct bGPdata *gpd, const struct Palette *palette);
-struct bGPDpaletteref *BKE_gpencil_paletteslot_find(struct bGPdata *gpd, const struct Palette *palette);
-struct bGPDpaletteref *BKE_gpencil_paletteslot_addnew(struct bGPdata *gpd, struct Palette *palette);
+void BKE_gpencil_paletteslot_set_palette(struct bGPdata *gpd, struct bGPDpaletteref *palslot, struct Palette *palette);
+struct bGPDpaletteref *BKE_gpencil_paletteslot_add(struct bGPdata *gpd, struct Palette *palette);
+struct bGPDpaletteref *BKE_gpencil_paletteslot_validate(struct Main *bmain, struct bGPdata *gpd);
/* Palettes - Deprecated (2.78-2.79) */
void BKE_gpencil_free_palettes(struct ListBase *list);
diff --git a/source/blender/blenkernel/BKE_paint.h b/source/blender/blenkernel/BKE_paint.h
index 7be8132aac2..57d506acbfa 100644
--- a/source/blender/blenkernel/BKE_paint.h
+++ b/source/blender/blenkernel/BKE_paint.h
@@ -101,7 +101,6 @@ void BKE_paint_set_overlay_override(enum OverlayFlags flag);
/* palettes */
void BKE_palette_free(struct Palette *palette);
struct Palette *BKE_palette_add(struct Main *bmain, const char *name);
-struct Palette *BKE_palette_add_gpencil(const struct bContext *C);
void BKE_palette_copy_data(
struct Main *bmain, struct Palette *palette_dst, const struct Palette *palette_src, const int flag);
struct Palette *BKE_palette_copy(struct Main *bmain, const struct Palette *palette);
diff --git a/source/blender/blenkernel/intern/gpencil.c b/source/blender/blenkernel/intern/gpencil.c
index dbf08955ec2..f87f35a8c27 100644
--- a/source/blender/blenkernel/intern/gpencil.c
+++ b/source/blender/blenkernel/intern/gpencil.c
@@ -1411,6 +1411,8 @@ void BKE_gpencil_brush_delete(ToolSettings *ts, bGPDbrush *brush)
/* ************************************************** */
/* GP Palette Slots API */
+/* Active Palette Slot ------------------------------ */
+
/* Get active palette slot */
bGPDpaletteref *BKE_gpencil_paletteslot_get_active(const bGPdata *gpd)
{
@@ -1440,6 +1442,8 @@ void BKE_gpencil_paletteslot_set_active_palette(bGPdata *gpd, const Palette *pal
BKE_gpencil_paletteslot_set_active(gpd, palslot);
}
+/* Slot Lookup ------------------------------------- */
+
/* Get palette slot that uses this Palette */
bGPDpaletteref *BKE_gpencil_paletteslot_find(bGPdata *gpd, const Palette *palette)
{
@@ -1459,8 +1463,10 @@ bGPDpaletteref *BKE_gpencil_paletteslot_find(bGPdata *gpd, const Palette *palett
return NULL;
}
+/* Add Slots --------------------------------------- */
+
/* Create a new palette slot (and optionally assign a palette to it) */
-bGPDpaletteref *BKE_gpencil_paletteslot_addnew(bGPdata *gpd, Palette *palette)
+bGPDpaletteref *BKE_gpencil_paletteslot_add(bGPdata *gpd, Palette *palette)
{
bGPDpaletteref *palslot;
@@ -1491,6 +1497,111 @@ bGPDpaletteref *BKE_gpencil_paletteslot_addnew(bGPdata *gpd, Palette *palette)
return palslot;
}
+/* Get active palette slot, and add all default settings if we don't find anything */
+bGPDpaletteref *BKE_gpencil_paletteslot_validate(Main *bmain, bGPdata *gpd)
+{
+ bGPDpaletteref *palslot;
+ Palette *palette;
+
+ /* sanity checks */
+ if (ELEM(NULL, bmain, gpd))
+ return NULL;
+
+ /* ensure a palette slot exists */
+ palslot = BKE_gpencil_paletteslot_get_active(gpd);
+ if (palslot == NULL) {
+ palslot = BKE_gpencil_paletteslot_add(gpd, NULL);
+ }
+
+ /* ensure a palette exists */
+ /* XXX: use "active palette" instead of making a new one each time? */
+ if (palslot->palette == NULL) {
+ palslot->palette = BKE_palette_add(bmain, "Palette");
+ id_us_plus(&palslot->palette->id);
+ }
+
+ /* ensure the palette has colors, and that the active one is usable */
+ palette = palslot->palette;
+ if (BKE_palette_is_empty(palette)) {
+ /* init the default set if none exist */
+ BKE_palette_color_add_default_set(palette);
+ }
+ else if (BKE_palette_color_get_active(palette) == NULL) {
+ /* sometimes the "active" color is unreachable
+ * (e.g. after deleting the previously active one)
+ */
+ palette->active_color = 0;
+ }
+
+ /* return the slot (this is more useful, as we can still get the palette from it) */
+ return palslot;
+}
+
+/* Palette Assignment ------------------------------- */
+
+/**
+ * Helper for BKE_gpencil_paletteslot_set_palette()
+ *
+ * Change all strokes using 'old_palette' to use 'new_palette' instead.
+ */
+/* XXX: This overlaps with the operators, but this one just does bulk replacement */
+/* TODO: Optimise with hash lookups? */
+static void gpencil_strokes_palette_change_all(bGPdata *gpd, Palette *old_palette, Palette *new_palette)
+{
+ if (BKE_palette_is_empty(old_palette))
+ return;
+
+ for (bGPDlayer *gpl = gpd->layers.first; gpl; gpl = gpl->next) {
+ for (bGPDframe *gpf = gpl->frames.first; gpf; gpf = gpf->next) {
+ for (bGPDstroke *gps = gpf->strokes.first; gps; gps = gps->next) {
+ /* check if this stroke uses the old palette */
+ if (gps->palette == old_palette) {
+ /* change palette */
+ gps->palette = new_palette;
+
+ /* find new matching color */
+ // XXX: optimise?
+ // XXX: what to do if not set?
+ gps->palcolor = BKE_palette_color_getbyname(new_palette, gps->colorname);
+ }
+ }
+ }
+ }
+}
+
+/* Set the palette used by this slot */
+void BKE_gpencil_paletteslot_set_palette(bGPdata *gpd, bGPDpaletteref *palslot, Palette *palette)
+{
+ /* sanity checks */
+ if (ELEM(NULL, gpd, palslot))
+ return;
+
+ /* Save effort if nothing changes... */
+ /* XXX: This does mean that "flushing" the property to fix any errors won't work... */
+ if (palslot->palette == palette)
+ return;
+
+ /* Unset existing palette */
+ if (palslot->palette) {
+ /* Unbind all strokes using this, as the slots *must*
+ * reflect all the palettes used by the strokes
+ *
+ * XXX: What happens if the new Palette doesn't support this new color?
+ */
+ gpencil_strokes_palette_change_all(gpd, palslot->palette, palette);
+
+ /* Now clear the old user... */
+ id_us_min(&palslot->palette->id);
+ palslot->palette = NULL;
+ }
+
+ /* Set new palette */
+ palslot->palette = palette;
+ if (palette) {
+ id_us_plus(&palette->id);
+ }
+}
+
/* ************************************************** */
/* GP Palettes API (Deprecated) */
diff --git a/source/blender/blenkernel/intern/paint.c b/source/blender/blenkernel/intern/paint.c
index e2663ddc6ac..821f67d7819 100644
--- a/source/blender/blenkernel/intern/paint.c
+++ b/source/blender/blenkernel/intern/paint.c
@@ -355,20 +355,6 @@ Palette *BKE_paint_palette(Paint *p)
return p ? p->palette : NULL;
}
-Palette *BKE_palette_add_gpencil(const bContext *C)
-{
- Main *bmain = CTX_data_main(C);
- ToolSettings *ts = CTX_data_tool_settings(C);
- Paint *paint = &ts->imapaint.paint;
- Palette *palette;
-
- palette = BKE_palette_add(bmain, "Palette");
-
- BKE_paint_palette_set(paint, palette);
-
- return palette;
-}
-
void BKE_paint_palette_set(Paint *p, Palette *palette)
{
if (p) {
diff --git a/source/blender/editors/gpencil/gpencil_edit.c b/source/blender/editors/gpencil/gpencil_edit.c
index 1b1a73529dc..02932c27116 100644
--- a/source/blender/editors/gpencil/gpencil_edit.c
+++ b/source/blender/editors/gpencil/gpencil_edit.c
@@ -790,11 +790,13 @@ GHash *gp_copybuf_validate_colormap(bContext *C)
GHash *new_colors = BLI_ghash_str_new("GPencil Paste Dst Colors");
GHashIterator gh_iter;
+ bGPdata *gpd = CTX_data_gpencil_data(C);
+ bGPDpaletteref *palslot;
+ Palette *palette;
+
/* If there's no active palette yet (i.e. new datablock), add one */
- Palette *palette = BKE_palette_get_active_from_context(C);
- if (palette == NULL) {
- palette = BKE_palette_add_gpencil(C);
- }
+ palslot = BKE_gpencil_paletteslot_validate(CTX_data_main(C), gpd);
+ palette = palslot->palette;
/* For each color, figure out what to map to... */
GHASH_ITER(gh_iter, gp_strokes_copypastebuf_colors) {
diff --git a/source/blender/editors/gpencil/gpencil_paint.c b/source/blender/editors/gpencil/gpencil_paint.c
index a391e434450..5b151c2a115 100644
--- a/source/blender/editors/gpencil/gpencil_paint.c
+++ b/source/blender/editors/gpencil/gpencil_paint.c
@@ -121,6 +121,8 @@ typedef enum eGPencil_PaintFlags {
*/
typedef struct tGPsdata {
EvaluationContext eval_ctx;
+
+ Main *bmain; /* main database pointer */
Scene *scene; /* current scene from context */
struct Depsgraph *graph;
@@ -130,7 +132,6 @@ typedef struct tGPsdata {
View2D *v2d; /* needed for GP_STROKE_2DSPACE */
rctf *subrect; /* for using the camera rect within the 3d view */
rctf subrect_data;
- Palette *palette; /* current palette */
GP_SpaceConversion gsc; /* settings to pass to gp_points_to_xy() */
@@ -169,8 +170,11 @@ typedef struct tGPsdata {
void *erasercursor; /* radial cursor data for drawing eraser */
+ /* Palette settings are only used for 3D view */
+ Palette *palette; /* current palette */
PaletteColor *palettecolor; /* current palette color */
- bGPDbrush *brush; /* current drawing brush */
+
+ bGPDbrush *brush; /* current drawing brush */
short straight[2]; /* 1: line horizontal, 2: line vertical, other: not defined, second element position */
int lock_axis; /* lock drawing to one axis */
@@ -1001,10 +1005,12 @@ static void gp_stroke_newfrombuffer(tGPsdata *p)
if (depth_arr)
MEM_freeN(depth_arr);
}
+
/* Save palette color */
gps->pal
@@ Diff output truncated at 10240 characters. @@
More information about the Bf-blender-cvs
mailing list