Welcome to mirror list, hosted at ThFree Co, Russian Federation.

git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorFalk David <falkdavid@gmx.de>2022-02-02 17:22:01 +0300
committerFalk David <falkdavid@gmx.de>2022-02-02 17:22:01 +0300
commitdfb8e1f895ecd9b2dd68a6caf405c922adc2f407 (patch)
tree0c0748493ffee6318e7a5a99fe3dccfb36b6c315 /source/blender/editors
parentb6097a659e4f6f2096357a1347d5380bdbbf6f8a (diff)
Initial gpencil undo system patch
Co-authored-by: @yann-lty
Diffstat (limited to 'source/blender/editors')
-rw-r--r--source/blender/editors/gpencil/gpencil_paint.c3
-rw-r--r--source/blender/editors/gpencil/gpencil_undo.c409
-rw-r--r--source/blender/editors/include/ED_gpencil.h3
-rw-r--r--source/blender/editors/undo/undo_system_types.c3
4 files changed, 411 insertions, 7 deletions
diff --git a/source/blender/editors/gpencil/gpencil_paint.c b/source/blender/editors/gpencil/gpencil_paint.c
index 082deab823b..cab44bbef5b 100644
--- a/source/blender/editors/gpencil/gpencil_paint.c
+++ b/source/blender/editors/gpencil/gpencil_paint.c
@@ -280,7 +280,7 @@ typedef struct tGPsdata {
static void gpencil_update_cache(bGPdata *gpd)
{
if (gpd) {
- DEG_id_tag_update(&gpd->id, ID_RECALC_TRANSFORM | ID_RECALC_GEOMETRY);
+ DEG_id_tag_update(&gpd->id, ID_RECALC_GEOMETRY);
gpd->flag |= GP_DATA_CACHE_IS_DIRTY;
}
}
@@ -2240,7 +2240,6 @@ static void gpencil_paint_initstroke(tGPsdata *p,
if (!IS_AUTOKEY_ON(scene)) {
BKE_report(p->reports, RPT_INFO, "No available frame for creating stroke");
}
-
return;
}
p->gpf->flag |= GP_FRAME_PAINT;
diff --git a/source/blender/editors/gpencil/gpencil_undo.c b/source/blender/editors/gpencil/gpencil_undo.c
index ec70febc80c..aed90ab1332 100644
--- a/source/blender/editors/gpencil/gpencil_undo.c
+++ b/source/blender/editors/gpencil/gpencil_undo.c
@@ -1,3 +1,4 @@
+
/*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
@@ -21,24 +22,27 @@
* \ingroup edgpencil
*/
+#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "MEM_guardedalloc.h"
+#include "BLI_listbase.h"
#include "DNA_gpencil_types.h"
#include "DNA_listBase.h"
#include "DNA_object_types.h"
+#include "DNA_scene_types.h"
#include "DNA_windowmanager_types.h"
-#include "BLI_listbase.h"
-
#include "BKE_blender_undo.h"
#include "BKE_context.h"
#include "BKE_gpencil.h"
+#include "BKE_gpencil_update_cache.h"
#include "BKE_undo_system.h"
#include "ED_gpencil.h"
+#include "ED_undo.h"
#include "WM_api.h"
#include "WM_types.h"
@@ -93,9 +97,9 @@ int ED_undo_gpencil_step(bContext *C, const int step)
/* copy layers */
BLI_listbase_clear(&gpd->layers);
- LISTBASE_FOREACH (bGPDlayer *, gpl, &gpd->layers) {
+ LISTBASE_FOREACH (bGPDlayer *, gpl_undo, &gpd->layers) {
/* make a copy of source layer and its data */
- gpld = BKE_gpencil_layer_duplicate(gpl, true, true);
+ gpld = BKE_gpencil_layer_duplicate(gpl_undo, true, true);
BLI_addtail(&gpd->layers, gpld);
}
}
@@ -130,6 +134,8 @@ void gpencil_undo_push(bGPdata *gpd)
{
bGPundonode *undo_node;
+ // printf("\t\tGP - undo push\n");
+
if (cur_node) {
/* Remove all undone nodes from stack. */
undo_node = cur_node->next;
@@ -170,7 +176,7 @@ void gpencil_undo_push(bGPdata *gpd)
/* create new undo node */
undo_node = MEM_callocN(sizeof(bGPundonode), "gpencil undo node");
- undo_node->gpd = BKE_gpencil_data_duplicate(NULL, gpd, true);
+ BKE_gpencil_data_duplicate(NULL, gpd, &undo_node->gpd);
cur_node = undo_node;
@@ -190,3 +196,396 @@ void gpencil_undo_finish(void)
cur_node = NULL;
}
+
+/* -------------------------------------------------------------------- */
+/** \name Implements ED Undo System
+ * \{ */
+
+typedef struct GPencilUndoData {
+ GPencilUpdateCache *gpd_cache_data;
+ /* Scene frame for this step. */
+ int cfra;
+ /* Whether this step is only a frame change. */
+ bool frame_changed;
+ /* Store the grease pencil mode we are in. */
+ eObjectMode mode;
+} GPencilUndoData;
+
+typedef struct GPencilUndoStep {
+ UndoStep step;
+ GPencilUndoData *undo_data;
+} GPencilUndoStep;
+
+static bool change_gpencil_mode(bContext *C, Object *ob, eObjectMode mode)
+{
+ if (ob->mode == mode) {
+ return false;
+ }
+ bGPdata *gpd = (bGPdata *)ob->data;
+ ob->mode = mode;
+ ED_gpencil_setup_modes(C, gpd, mode);
+
+ return true;
+}
+
+static void gpencil_data_to_undo_data(bGPdata *gpd, GPencilUndoData *gpd_undo_data)
+{
+ GPencilUpdateCache *update_cache = gpd->runtime.update_cache;
+
+ if (update_cache == NULL) {
+ /* Need a full-copy of the grease pencil undo_data. */
+ bGPdata *gpd_copy = NULL;
+ BKE_gpencil_data_duplicate(NULL, gpd, &gpd_copy);
+ gpd_copy->id.session_uuid = gpd->id.session_uuid;
+
+ gpd_undo_data->gpd_cache_data = BKE_gpencil_create_update_cache(gpd_copy, true);
+ }
+ else {
+ gpd_undo_data->gpd_cache_data = BKE_gpencil_duplicate_update_cache_and_data(update_cache);
+ }
+}
+
+typedef struct tGPencilUpdateCacheUndoTraverseData {
+ bGPdata *gpd;
+ bGPDlayer *gpl;
+ bGPDframe *gpf;
+ bGPDstroke *gps;
+ int gpl_index;
+ int gpf_index;
+ int gps_index;
+ bool tag_update_cache;
+} tGPencilUpdateCacheUndoTraverseData;
+
+static bool gpencil_decode_undo_data_layer_cb(GPencilUpdateCache *gpl_cache, void *user_data)
+{
+ tGPencilUpdateCacheUndoTraverseData *td = (tGPencilUpdateCacheUndoTraverseData *)user_data;
+ td->gpl = BLI_findlinkfrom((Link *)td->gpl, gpl_cache->index - td->gpl_index);
+ td->gpl_index = gpl_cache->index;
+ bGPDlayer *gpl_new = (bGPDlayer *)gpl_cache->data;
+
+ if (gpl_cache->flag == GP_UPDATE_NODE_FULL_COPY) {
+ /* Do a full copy of the layer. */
+ bGPDlayer *gpl_next = td->gpl->next;
+ BKE_gpencil_layer_delete(td->gpd, td->gpl);
+
+ td->gpl = BKE_gpencil_layer_duplicate(gpl_new, true, true);
+ BLI_insertlinkbefore(&td->gpd->layers, gpl_next, td->gpl);
+
+ if (td->tag_update_cache) {
+ /* Tag the layer here. */
+ BKE_gpencil_tag_full_update(td->gpd, td->gpl, NULL, NULL);
+ }
+ return true;
+ }
+ else if (gpl_cache->flag == GP_UPDATE_NODE_LIGHT_COPY) {
+ BKE_gpencil_layer_copy_settings(gpl_new, td->gpl);
+ if (td->tag_update_cache) {
+ BKE_gpencil_tag_light_update(td->gpd, td->gpl, NULL, NULL);
+ }
+ }
+
+ td->gpf = td->gpl->frames.first;
+ td->gpf_index = 0;
+ return false;
+}
+
+static bool gpencil_decode_undo_data_frame_cb(GPencilUpdateCache *gpf_cache, void *user_data)
+{
+ tGPencilUpdateCacheUndoTraverseData *td = (tGPencilUpdateCacheUndoTraverseData *)user_data;
+ td->gpf = BLI_findlinkfrom((Link *)td->gpf, gpf_cache->index - td->gpf_index);
+ td->gpf_index = gpf_cache->index;
+ bGPDframe *gpf_new = (bGPDframe *)gpf_cache->data;
+
+ if (gpf_cache->flag == GP_UPDATE_NODE_FULL_COPY) {
+ /* Do a full copy of the frame. */
+ bGPDframe *gpf_next = td->gpf->next;
+
+ bool update_actframe = (td->gpl->actframe == td->gpf) ? true : false;
+ BKE_gpencil_free_strokes(td->gpf);
+ BLI_freelinkN(&td->gpl->frames, td->gpf);
+
+ td->gpf = BKE_gpencil_frame_duplicate(gpf_new, true);
+ BLI_insertlinkbefore(&td->gpl->frames, gpf_next, td->gpf);
+
+ if (update_actframe) {
+ td->gpl->actframe = td->gpf;
+ }
+ if (td->tag_update_cache) {
+ /* Tag the frame here. */
+ BKE_gpencil_tag_full_update(td->gpd, td->gpl, td->gpf, NULL);
+ }
+ return true;
+ }
+ else if (gpf_cache->flag == GP_UPDATE_NODE_LIGHT_COPY) {
+ BKE_gpencil_frame_copy_settings(gpf_new, td->gpf);
+ if (td->tag_update_cache) {
+ BKE_gpencil_tag_light_update(td->gpd, td->gpl, td->gpf, NULL);
+ }
+ }
+
+ td->gps = td->gpf->strokes.first;
+ td->gps_index = 0;
+ return false;
+}
+
+static bool gpencil_decode_undo_data_stroke_cb(GPencilUpdateCache *gps_cache, void *user_data)
+{
+ tGPencilUpdateCacheUndoTraverseData *td = (tGPencilUpdateCacheUndoTraverseData *)user_data;
+ td->gps = BLI_findlinkfrom((Link *)td->gps, gps_cache->index - td->gps_index);
+ td->gps_index = gps_cache->index;
+ bGPDstroke *gps_new = (bGPDstroke *)gps_cache->data;
+
+ if (gps_cache->flag == GP_UPDATE_NODE_FULL_COPY) {
+ /* Do a full copy of the stroke. */
+ bGPDstroke *gps_next = td->gps->next;
+
+ BLI_remlink(&td->gpf->strokes, td->gps);
+ BKE_gpencil_free_stroke(td->gps);
+
+ td->gps = BKE_gpencil_stroke_duplicate(gps_new, true, true);
+ BLI_insertlinkbefore(&td->gpf->strokes, gps_next, td->gps);
+
+ if (td->tag_update_cache) {
+ /* Tag the stroke here. */
+ BKE_gpencil_tag_full_update(td->gpd, td->gpl, td->gpf, td->gps);
+ }
+ }
+ else if (gps_cache->flag == GP_UPDATE_NODE_LIGHT_COPY) {
+ BKE_gpencil_stroke_copy_settings(gps_new, td->gps);
+ if (td->tag_update_cache) {
+ BKE_gpencil_tag_light_update(td->gpd, td->gpl, td->gpf, td->gps);
+ }
+ }
+ return false;
+}
+
+static bool gpencil_undo_data_to_gpencil_data(GPencilUndoData *gpd_undo_data,
+ bGPdata *gpd,
+ bool tag_gpd_update_cache)
+{
+ GPencilUpdateCache *update_cache = gpd_undo_data->gpd_cache_data;
+
+ BLI_assert(update_cache != NULL);
+
+ if (update_cache->flag == GP_UPDATE_NODE_FULL_COPY) {
+ /* Full-copy. */
+ BKE_gpencil_free_data(gpd, true);
+ BKE_gpencil_data_duplicate(NULL, update_cache->data, &gpd);
+ if (tag_gpd_update_cache) {
+ BKE_gpencil_tag_full_update(gpd, NULL, NULL, NULL);
+ }
+ return true;
+ }
+ else if (update_cache->flag == GP_UPDATE_NODE_LIGHT_COPY) {
+ BKE_gpencil_data_copy_settings(update_cache->data, gpd);
+ if (tag_gpd_update_cache) {
+ BKE_gpencil_tag_light_update(gpd, NULL, NULL, NULL);
+ }
+ }
+
+ GPencilUpdateCacheTraverseSettings ts = {{
+ gpencil_decode_undo_data_layer_cb,
+ gpencil_decode_undo_data_frame_cb,
+ gpencil_decode_undo_data_stroke_cb,
+ }};
+
+ tGPencilUpdateCacheUndoTraverseData data = {
+ .gpd = gpd,
+ .gpl = gpd->layers.first,
+ .gpf = NULL,
+ .gps = NULL,
+ .gpl_index = 0,
+ .gpf_index = 0,
+ .gps_index = 0,
+ .tag_update_cache = tag_gpd_update_cache,
+ };
+
+ BKE_gpencil_traverse_update_cache(update_cache, &ts, &data);
+
+ return true;
+}
+
+static bool gpencil_undosys_poll(bContext *C)
+{
+ printf("gpencil_undosys_poll\n");
+ if (!U.experimental.use_gpencil_undo_system) {
+ return false;
+ }
+ bGPdata *gpd = ED_gpencil_data_get_active(C);
+ return GPENCIL_ANY_MODE(gpd);
+}
+
+static bool gpencil_undosys_step_encode(struct bContext *C, struct Main *bmain, UndoStep *us_p)
+{
+ printf("gpencil_undosys_step_encode: \n");
+ GPencilUndoStep *us = (GPencilUndoStep *)us_p;
+ us->undo_data = MEM_callocN(sizeof(GPencilUndoData), __func__);
+ us->undo_data->frame_changed = false;
+
+ Scene *scene = CTX_data_scene(C);
+ us->undo_data->cfra = scene->r.cfra;
+
+ Object *ob = CTX_data_active_object(C);
+ bGPdata *gpd = (bGPdata *)ob->data;
+ us->undo_data->mode = ob->mode;
+
+ wmWindowManager *wm = CTX_wm_manager(C);
+
+ // Inspect notifier queue to detect frame change
+ if (wm->notifier_queue.first) {
+ wmNotifier *note = (wmNotifier *)wm->notifier_queue.first;
+ // If frame changed, encode only this information
+ if (note->category == NC_SCENE && note->data == ND_FRAME) {
+ us->undo_data->frame_changed = true;
+ printf("Time changed: %d\n", us->undo_data->cfra);
+ return true;
+ }
+ }
+
+ gpencil_data_to_undo_data(gpd, us->undo_data);
+ gpd->flag |= GP_DATA_UPDATE_CACHE_UNDO_ENCODED;
+ return true;
+}
+
+static void gpencil_undosys_step_decode(
+ struct bContext *C, struct Main *bmain, UndoStep *us_p, const eUndoStepDir dir, bool is_final)
+{
+ printf("gpencil_undosys_step_decode\n");
+ GPencilUndoStep *us = (GPencilUndoStep *)us_p;
+ GPencilUndoData *undo_data = us->undo_data;
+
+ Object *ob = CTX_data_active_object(C);
+ bGPdata *gpd = (bGPdata *)ob->data;
+
+ if (gpd == NULL) {
+ return;
+ }
+
+ printf("Direction: %s\n", dir == STEP_UNDO ? "STEP_UNDO" : "STEP_REDO");
+
+ Scene *scene = CTX_data_scene(C);
+
+ if (undo_data->cfra != scene->r.cfra) {
+ printf("Restoring frame: %d\n", undo_data->cfra);
+ scene->r.cfra = undo_data->cfra;
+ /* TODO: what if we merged a full copy with a frame change? */
+ DEG_id_tag_update(&scene->id, ID_RECALC_AUDIO_SEEK);
+ WM_event_add_notifier(C, NC_SCENE | ND_FRAME, NULL);
+ }
+
+ if (change_gpencil_mode(C, ob, undo_data->mode)) {
+ DEG_id_tag_update(&gpd->id, ID_RECALC_TRANSFORM | ID_RECALC_GEOMETRY);
+ WM_event_add_notifier(C, NC_GPENCIL | ND_DATA | ND_GPENCIL_EDITMODE, NULL);
+ WM_event_add_notifier(C, NC_SCENE | ND_MODE, NULL);
+ return;
+ }
+
+ if (dir == STEP_UNDO) {
+ UndoStep *us_iter = us_p;
+ BLI_assert(us_iter->next != NULL);
+ UndoStep *us_next = us_p->next;
+
+ GPencilUndoData *data_iter = ((GPencilUndoStep *)us_iter)->undo_data;
+ GPencilUndoData *data_next = ((GPencilUndoStep *)us_next)->undo_data;
+
+ if (data_next->gpd_cache_data == NULL) {
+ return;
+ }
+
+ while (
+ data_iter->gpd_cache_data == NULL ||
+ BKE_gpencil_compare_update_caches(data_iter->gpd_cache_data, data_next->gpd_cache_data)) {
+ us_iter = us_iter->prev;
+ BLI_assert(us_iter != NULL && us_iter->type == BKE_UNDOSYS_TYPE_GPENCIL);
+ data_iter = ((GPencilUndoStep *)us_iter)->undo_data;
+ }
+
+ while (us_iter != us_next) {
+ if (data_iter->gpd_cache_data != NULL) {
+ gpencil_undo_data_to_gpencil_data(data_iter, gpd, true);
+ }
+ us_iter = us_iter->next;
+ data_iter = ((GPencilUndoStep *)us_iter)->undo_data;
+ }
+ }
+ else {
+ if (undo_data->gpd_cache_data == NULL) {
+ return;
+ }
+
+ gpencil_undo_data_to_gpencil_data(undo_data, gpd, true);
+ }
+ gpd->flag |= GP_DATA_UPDATE_CACHE_UNDO_ENCODED;
+
+ DEG_id_tag_update(&gpd->id, ID_RECALC_GEOMETRY);
+ WM_event_add_notifier(C, NC_GEOM | ND_DATA, NULL);
+}
+
+static void gpencil_undosys_step_free(UndoStep *us_p)
+{
+ printf("gpencil_undosys_step_free\n");
+ GPencilUndoStep *us = (GPencilUndoStep *)us_p;
+ GPencilUndoData *us_data = us->undo_data;
+
+ /**
+ * If this undo step is the first, we want to keep its full copy of the grease pencil undo_data
+ * (we assume that the first undo step always has this). Otherwise we free the step and its
+ * undo_data.
+ */
+ if ((us_p->prev == NULL || us_p->prev->type != BKE_UNDOSYS_TYPE_GPENCIL) && us_p->next != NULL &&
+ us_p->next->type == BKE_UNDOSYS_TYPE_GPENCIL) {
+ GPencilUndoStep *us_next = (GPencilUndoStep *)us_p->next;
+ GPencilUndoData *us_next_data = us_next->undo_data;
+ /* If e.g. a frame change happend, there is no cache so in this case we move the gpd pointer to
+ * that step. */
+ if (us_next_data->gpd_cache_data == NULL) {
+ bGPdata *gpd_copy = us_data->gpd_cache_data->data;
+ BLI_assert(gpd_copy != NULL);
+ BLI_assert(us_data->gpd_cache_data->flag == GP_UPDATE_NODE_FULL_COPY);
+
+ us_next_data->gpd_cache_data = BKE_gpencil_create_update_cache(gpd_copy, true);
+ /* Make sure the gpd_copy is not freed below. */
+ us_data->gpd_cache_data->data = NULL;
+ }
+ /* If the next step does not have a full copy, we need to apply the changes of the next step
+ * to our cached gpencil undo_data copy and move it to the next step (it will now be the
+ * full-copy). */
+ else if (us_next_data->gpd_cache_data->flag != GP_UPDATE_NODE_FULL_COPY) {
+ bGPdata *gpd_copy = us_data->gpd_cache_data->data;
+ BLI_assert(gpd_copy != NULL);
+ BLI_assert(us_data->gpd_cache_data->flag == GP_UPDATE_NODE_FULL_COPY);
+
+ gpencil_undo_data_to_gpencil_data(us_next_data, gpd_copy, false);
+ BKE_gpencil_free_update_cache_and_data(us_next_data->gpd_cache_data);
+
+ us_next_data->gpd_cache_data = BKE_gpencil_create_update_cache(gpd_copy, true);
+
+ /* Make sure the gpd_copy is not freed below. */
+ us_data->gpd_cache_data->data = NULL;
+ }
+ else {
+ /* If the next step is a full copy, we can safely free the current step (since the last step
+ * will be a full-copy). */
+ }
+ }
+
+ if (us_data->gpd_cache_data) {
+ BKE_gpencil_free_update_cache_and_data(us_data->gpd_cache_data);
+ }
+ MEM_freeN(us_data);
+}
+
+void ED_gpencil_undosys_type(UndoType *ut)
+{
+ ut->name = "Grease Pencil Draw";
+ ut->poll = gpencil_undosys_poll;
+ ut->step_encode = gpencil_undosys_step_encode;
+ ut->step_decode = gpencil_undosys_step_decode;
+ ut->step_free = gpencil_undosys_step_free;
+
+ ut->flags = UNDOTYPE_FLAG_NEED_CONTEXT_FOR_ENCODE;
+
+ ut->step_size = sizeof(GPencilUndoStep);
+}
+
+/** \} */
diff --git a/source/blender/editors/include/ED_gpencil.h b/source/blender/editors/include/ED_gpencil.h
index 919ea3e4a6b..ce98a43d937 100644
--- a/source/blender/editors/include/ED_gpencil.h
+++ b/source/blender/editors/include/ED_gpencil.h
@@ -52,6 +52,7 @@ struct SnapObjectContext;
struct ToolSettings;
struct View3D;
struct bContext;
+struct UndoType;
struct Material;
struct Object;
@@ -353,6 +354,8 @@ int ED_gpencil_session_active(void);
*/
int ED_undo_gpencil_step(struct bContext *C, int step); /* eUndoStepDir. */
+void ED_gpencil_undosys_type(struct UndoType *ut);
+
/* ------------ Grease-Pencil Armature ------------------ */
bool ED_gpencil_add_armature(const struct bContext *C,
struct ReportList *reports,
diff --git a/source/blender/editors/undo/undo_system_types.c b/source/blender/editors/undo/undo_system_types.c
index 300ab606a03..4c5e6c173a7 100644
--- a/source/blender/editors/undo/undo_system_types.c
+++ b/source/blender/editors/undo/undo_system_types.c
@@ -24,6 +24,7 @@
#include "ED_armature.h"
#include "ED_curve.h"
+#include "ED_gpencil.h"
#include "ED_lattice.h"
#include "ED_mball.h"
#include "ED_mesh.h"
@@ -46,6 +47,8 @@ void ED_undosys_type_init(void)
BKE_undosys_type_append(ED_lattice_undosys_type);
BKE_undosys_type_append(ED_mball_undosys_type);
BKE_undosys_type_append(ED_mesh_undosys_type);
+ /* Grease pencil */
+ BKE_UNDOSYS_TYPE_GPENCIL = BKE_undosys_type_append(ED_gpencil_undosys_type);
/* Paint Modes */
BKE_UNDOSYS_TYPE_IMAGE = BKE_undosys_type_append(ED_image_undosys_type);