| 
									
										
										
										
											2018-03-19 14:49:59 +01:00
										 |  |  | /*
 | 
					
						
							|  |  |  |  * ***** BEGIN GPL LICENSE BLOCK ***** | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * This program is free software; you can redistribute it and/or | 
					
						
							|  |  |  |  * modify it under the terms of the GNU General Public License | 
					
						
							|  |  |  |  * as published by the Free Software Foundation; either version 2 | 
					
						
							|  |  |  |  * of the License, or (at your option) any later version. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * This program is distributed in the hope that it will be useful, | 
					
						
							|  |  |  |  * but WITHOUT ANY WARRANTY; without even the implied warranty of | 
					
						
							|  |  |  |  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the | 
					
						
							|  |  |  |  * GNU General Public License for more details. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * You should have received a copy of the GNU General Public License | 
					
						
							|  |  |  |  * along with this program; if not, write to the Free Software Foundation, | 
					
						
							|  |  |  |  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV. | 
					
						
							|  |  |  |  * All rights reserved. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * Contributor(s): Blender Foundation | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * ***** END GPL LICENSE BLOCK ***** | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /** \file blender/editors/lattice/editlattice_undo.c
 | 
					
						
							|  |  |  |  *  \ingroup edlattice | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include <stdlib.h>
 | 
					
						
							|  |  |  | #include <string.h>
 | 
					
						
							|  |  |  | #include <math.h>
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include "MEM_guardedalloc.h"
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-04-16 16:27:55 +02:00
										 |  |  | #include "CLG_log.h"
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-19 14:49:59 +01:00
										 |  |  | #include "BLI_utildefines.h"
 | 
					
						
							| 
									
										
										
										
											2018-03-19 14:17:59 +01:00
										 |  |  | #include "BLI_array_utils.h"
 | 
					
						
							| 
									
										
										
										
											2018-03-19 14:49:59 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | #include "DNA_curve_types.h"
 | 
					
						
							|  |  |  | #include "DNA_lattice_types.h"
 | 
					
						
							|  |  |  | #include "DNA_object_types.h"
 | 
					
						
							|  |  |  | #include "DNA_scene_types.h"
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include "BKE_context.h"
 | 
					
						
							| 
									
										
										
										
											2018-04-16 16:27:55 +02:00
										 |  |  | #include "BKE_layer.h"
 | 
					
						
							| 
									
										
										
										
											2018-03-19 14:17:59 +01:00
										 |  |  | #include "BKE_undo_system.h"
 | 
					
						
							| 
									
										
										
										
											2018-03-19 14:49:59 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-04-01 11:03:25 +02:00
										 |  |  | #include "DEG_depsgraph.h"
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-19 14:17:59 +01:00
										 |  |  | #include "ED_object.h"
 | 
					
						
							| 
									
										
										
										
											2018-03-19 14:49:59 +01:00
										 |  |  | #include "ED_lattice.h"
 | 
					
						
							| 
									
										
										
										
											2018-04-16 16:27:55 +02:00
										 |  |  | #include "ED_undo.h"
 | 
					
						
							| 
									
										
										
										
											2018-03-19 14:49:59 +01:00
										 |  |  | #include "ED_util.h"
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-19 14:17:59 +01:00
										 |  |  | #include "WM_types.h"
 | 
					
						
							|  |  |  | #include "WM_api.h"
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-19 14:49:59 +01:00
										 |  |  | #include "lattice_intern.h"
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-04-16 16:27:55 +02:00
										 |  |  | /** We only need this locally. */ | 
					
						
							|  |  |  | static CLG_LogRef LOG = {"ed.undo.lattice"}; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-19 14:17:59 +01:00
										 |  |  | /* -------------------------------------------------------------------- */ | 
					
						
							|  |  |  | /** \name Undo Conversion
 | 
					
						
							|  |  |  |  * \{ */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-19 14:49:59 +01:00
										 |  |  | typedef struct UndoLattice { | 
					
						
							|  |  |  | 	BPoint *def; | 
					
						
							|  |  |  | 	int pntsu, pntsv, pntsw, actbp; | 
					
						
							| 
									
										
										
										
											2018-03-19 14:17:59 +01:00
										 |  |  | 	size_t undo_size; | 
					
						
							| 
									
										
										
										
											2018-03-19 14:49:59 +01:00
										 |  |  | } UndoLattice; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-19 14:17:59 +01:00
										 |  |  | static void undolatt_to_editlatt(UndoLattice *ult, EditLatt *editlatt) | 
					
						
							| 
									
										
										
										
											2018-03-19 14:49:59 +01:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2018-03-19 14:17:59 +01:00
										 |  |  | 	int len = editlatt->latt->pntsu * editlatt->latt->pntsv * editlatt->latt->pntsw; | 
					
						
							| 
									
										
										
										
											2018-03-19 14:49:59 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-19 14:17:59 +01:00
										 |  |  | 	memcpy(editlatt->latt->def, ult->def, sizeof(BPoint) * len); | 
					
						
							| 
									
										
										
										
											2018-03-19 14:49:59 +01:00
										 |  |  | 	editlatt->latt->actbp = ult->actbp; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-19 14:17:59 +01:00
										 |  |  | static void *undolatt_from_editlatt(UndoLattice *ult, EditLatt *editlatt) | 
					
						
							| 
									
										
										
										
											2018-03-19 14:49:59 +01:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2018-03-19 14:17:59 +01:00
										 |  |  | 	BLI_assert(BLI_array_is_zeroed(ult, 1)); | 
					
						
							| 
									
										
										
										
											2018-03-19 14:49:59 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	ult->def = MEM_dupallocN(editlatt->latt->def); | 
					
						
							|  |  |  | 	ult->pntsu = editlatt->latt->pntsu; | 
					
						
							|  |  |  | 	ult->pntsv = editlatt->latt->pntsv; | 
					
						
							|  |  |  | 	ult->pntsw = editlatt->latt->pntsw; | 
					
						
							|  |  |  | 	ult->actbp = editlatt->latt->actbp; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-19 14:17:59 +01:00
										 |  |  | 	ult->undo_size += sizeof(*ult->def) * ult->pntsu * ult->pntsv * ult->pntsw; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-19 14:49:59 +01:00
										 |  |  | 	return ult; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-19 14:17:59 +01:00
										 |  |  | static void undolatt_free_data(UndoLattice *ult) | 
					
						
							| 
									
										
										
										
											2018-03-19 14:49:59 +01:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2018-03-19 14:17:59 +01:00
										 |  |  | 	if (ult->def) { | 
					
						
							|  |  |  | 		MEM_freeN(ult->def); | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2018-03-19 14:49:59 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-19 14:17:59 +01:00
										 |  |  | #if 0
 | 
					
						
							| 
									
										
										
										
											2018-03-19 14:49:59 +01:00
										 |  |  | static int validate_undoLatt(void *data, void *edata) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	UndoLattice *ult = (UndoLattice *)data; | 
					
						
							|  |  |  | 	EditLatt *editlatt = (EditLatt *)edata; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return (ult->pntsu == editlatt->latt->pntsu && | 
					
						
							|  |  |  | 	        ult->pntsv == editlatt->latt->pntsv && | 
					
						
							|  |  |  | 	        ult->pntsw == editlatt->latt->pntsw); | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2018-03-19 14:17:59 +01:00
										 |  |  | #endif
 | 
					
						
							| 
									
										
										
										
											2018-03-19 14:49:59 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-19 14:17:59 +01:00
										 |  |  | static Object *editlatt_object_from_context(bContext *C) | 
					
						
							| 
									
										
										
										
											2018-03-19 14:49:59 +01:00
										 |  |  | { | 
					
						
							|  |  |  | 	Object *obedit = CTX_data_edit_object(C); | 
					
						
							|  |  |  | 	if (obedit && obedit->type == OB_LATTICE) { | 
					
						
							|  |  |  | 		Lattice *lt = obedit->data; | 
					
						
							| 
									
										
										
										
											2018-03-19 14:17:59 +01:00
										 |  |  | 		if (lt->editlatt != NULL) { | 
					
						
							|  |  |  | 			return obedit; | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2018-03-19 14:49:59 +01:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return NULL; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-19 14:17:59 +01:00
										 |  |  | /** \} */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* -------------------------------------------------------------------- */ | 
					
						
							|  |  |  | /** \name Implements ED Undo System
 | 
					
						
							| 
									
										
										
										
											2018-04-16 16:27:55 +02:00
										 |  |  |  * | 
					
						
							|  |  |  |  * \note This is similar for all edit-mode types. | 
					
						
							| 
									
										
										
										
											2018-03-19 14:17:59 +01:00
										 |  |  |  * \{ */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-04-16 16:27:55 +02:00
										 |  |  | typedef struct LatticeUndoStep_Elem { | 
					
						
							| 
									
										
										
										
											2018-03-19 14:17:59 +01:00
										 |  |  | 	UndoRefID_Object obedit_ref; | 
					
						
							|  |  |  | 	UndoLattice data; | 
					
						
							| 
									
										
										
										
											2018-04-16 16:27:55 +02:00
										 |  |  | } LatticeUndoStep_Elem; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | typedef struct LatticeUndoStep { | 
					
						
							|  |  |  | 	UndoStep step; | 
					
						
							|  |  |  | 	LatticeUndoStep_Elem *elems; | 
					
						
							|  |  |  | 	uint                  elems_len; | 
					
						
							| 
									
										
										
										
											2018-03-19 14:17:59 +01:00
										 |  |  | } LatticeUndoStep; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static bool lattice_undosys_poll(bContext *C) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	return editlatt_object_from_context(C) != NULL; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static bool lattice_undosys_step_encode(struct bContext *C, UndoStep *us_p) | 
					
						
							| 
									
										
										
										
											2018-03-19 14:49:59 +01:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2018-03-19 14:17:59 +01:00
										 |  |  | 	LatticeUndoStep *us = (LatticeUndoStep *)us_p; | 
					
						
							| 
									
										
										
										
											2018-04-16 16:27:55 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	ViewLayer *view_layer = CTX_data_view_layer(C); | 
					
						
							|  |  |  | 	uint objects_len = 0; | 
					
						
							| 
									
										
										
										
											2018-11-25 09:50:34 -02:00
										 |  |  | 	Object **objects = BKE_view_layer_array_from_objects_in_edit_mode_unique_data(view_layer, CTX_wm_view3d(C), &objects_len); | 
					
						
							| 
									
										
										
										
											2018-04-16 16:27:55 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	us->elems = MEM_callocN(sizeof(*us->elems) * objects_len, __func__); | 
					
						
							|  |  |  | 	us->elems_len = objects_len; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	for (uint i = 0; i < objects_len; i++) { | 
					
						
							|  |  |  | 		Object *ob = objects[i]; | 
					
						
							|  |  |  | 		LatticeUndoStep_Elem *elem = &us->elems[i]; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		elem->obedit_ref.ptr = ob; | 
					
						
							|  |  |  | 		Lattice *lt = ob->data; | 
					
						
							|  |  |  | 		undolatt_from_editlatt(&elem->data, lt->editlatt); | 
					
						
							|  |  |  | 		us->step.data_size += elem->data.undo_size; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	MEM_freeN(objects); | 
					
						
							| 
									
										
										
										
											2018-03-19 14:17:59 +01:00
										 |  |  | 	return true; | 
					
						
							| 
									
										
										
										
											2018-03-19 14:49:59 +01:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2018-03-19 14:17:59 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | static void lattice_undosys_step_decode(struct bContext *C, UndoStep *us_p, int UNUSED(dir)) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	/* TODO(campbell): undo_system: use low-level API to set mode. */ | 
					
						
							|  |  |  | 	ED_object_mode_set(C, OB_MODE_EDIT); | 
					
						
							|  |  |  | 	BLI_assert(lattice_undosys_poll(C)); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	LatticeUndoStep *us = (LatticeUndoStep *)us_p; | 
					
						
							| 
									
										
										
										
											2018-04-16 16:27:55 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	for (uint i = 0; i < us->elems_len; i++) { | 
					
						
							|  |  |  | 		LatticeUndoStep_Elem *elem = &us->elems[i]; | 
					
						
							|  |  |  | 		Object *obedit = elem->obedit_ref.ptr; | 
					
						
							|  |  |  | 		Lattice *lt = obedit->data; | 
					
						
							|  |  |  | 		if (lt->editlatt == NULL) { | 
					
						
							|  |  |  | 			/* Should never fail, may not crash but can give odd behavior. */ | 
					
						
							|  |  |  | 			CLOG_ERROR(&LOG, "name='%s', failed to enter edit-mode for object '%s', undo state invalid", | 
					
						
							|  |  |  | 			           us_p->name, obedit->id.name); | 
					
						
							|  |  |  | 			continue; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		undolatt_to_editlatt(&elem->data, lt->editlatt); | 
					
						
							| 
									
										
										
										
											2018-12-06 17:52:37 +01:00
										 |  |  | 		DEG_id_tag_update(&obedit->id, ID_RECALC_GEOMETRY); | 
					
						
							| 
									
										
										
										
											2018-04-16 16:27:55 +02:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	/* The first element is always active */ | 
					
						
							|  |  |  | 	ED_undo_object_set_active_or_warn(CTX_data_view_layer(C), us->elems[0].obedit_ref.ptr, us_p->name, &LOG); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-19 14:17:59 +01:00
										 |  |  | 	WM_event_add_notifier(C, NC_GEOM | ND_DATA, NULL); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static void lattice_undosys_step_free(UndoStep *us_p) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	LatticeUndoStep *us = (LatticeUndoStep *)us_p; | 
					
						
							| 
									
										
										
										
											2018-04-16 16:27:55 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	for (uint i = 0; i < us->elems_len; i++) { | 
					
						
							|  |  |  | 		LatticeUndoStep_Elem *elem = &us->elems[i]; | 
					
						
							|  |  |  | 		undolatt_free_data(&elem->data); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	MEM_freeN(us->elems); | 
					
						
							| 
									
										
										
										
											2018-03-19 14:17:59 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static void lattice_undosys_foreach_ID_ref( | 
					
						
							|  |  |  |         UndoStep *us_p, UndoTypeForEachIDRefFn foreach_ID_ref_fn, void *user_data) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	LatticeUndoStep *us = (LatticeUndoStep *)us_p; | 
					
						
							| 
									
										
										
										
											2018-04-16 16:27:55 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	for (uint i = 0; i < us->elems_len; i++) { | 
					
						
							|  |  |  | 		LatticeUndoStep_Elem *elem = &us->elems[i]; | 
					
						
							|  |  |  | 		foreach_ID_ref_fn(user_data, ((UndoRefID *)&elem->obedit_ref)); | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2018-03-19 14:17:59 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* Export for ED_undo_sys. */ | 
					
						
							|  |  |  | void ED_lattice_undosys_type(UndoType *ut) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	ut->name = "Edit Lattice"; | 
					
						
							|  |  |  | 	ut->poll = lattice_undosys_poll; | 
					
						
							|  |  |  | 	ut->step_encode = lattice_undosys_step_encode; | 
					
						
							|  |  |  | 	ut->step_decode = lattice_undosys_step_decode; | 
					
						
							|  |  |  | 	ut->step_free = lattice_undosys_step_free; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	ut->step_foreach_ID_ref = lattice_undosys_foreach_ID_ref; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	ut->mode = BKE_UNDOTYPE_MODE_STORE; | 
					
						
							|  |  |  | 	ut->use_context = true; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	ut->step_size = sizeof(LatticeUndoStep); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /** \} */ |