public static void DetachEntityFromAllParents(SEntityId entityId) { CEngine.Instance.Dispatch(EEngineUpdatePriority.BeginFrame, () => { CEntity target = entityId.GetEntity(); if (target == null || target.RootComponent == null || target.RootComponent.ParentComponent == null) { return; } SEntityComponentId oldRootParent = new SEntityComponentId(target.RootComponent.ParentComponent); void Do() { CWorld world = CEngine.Instance.CurrentWorld; CEntity entity = entityId.GetEntity(); if (entity != null) { entity.Detach(); } } void Undo() { CEngine.Instance.Dispatch(EEngineUpdatePriority.BeginFrame, () => { CWorld world = CEngine.Instance.CurrentWorld; CEntity entity = entityId.GetEntity(); CSceneComponent oldParent = oldRootParent.GetComponent <CSceneComponent>(); if (oldParent == null) { LogUtility.Log("[UndoRedo] The old parent is invalid! Undo stack has been corrupted and cleared."); UndoRedoUtility.Purge(null); return; } if (entity != null) { entity.AttachToComponent(oldParent); } }); } void Redo() { CEngine.Instance.Dispatch(EEngineUpdatePriority.BeginFrame, () => { Do(); }); } Do(); CRelayUndoItem item = new CRelayUndoItem(Undo, Redo); UndoRedoUtility.Record(item); }); }
private void DefaultSetter(CObjectProperty property, object oldValue, object newValue, bool bRecordUndoAction) { if (!ReferenceEquals(oldValue, null) && oldValue.Equals(newValue)) { return; } void SetValueInternal(CObjectProperty prop, object value) { if (DispatchSetter) { CEngine.Instance.Dispatch(EEngineUpdatePriority.BeginFrame, () => { if (property.FieldInfo != null) { property.FieldInfo.SetValue(property.Target, value); } else { property.PropertyInfo.SetValue(property.Target, value); } }); } else { if (property.FieldInfo != null) { property.FieldInfo.SetValue(property.Target, value); } else { property.PropertyInfo.SetValue(property.Target, value); } } } void Undo() { SetValueInternal(property, oldValue); } void Redo() { SetValueInternal(property, newValue); } if (bRecordUndoAction) { CUndoItem item = new CRelayUndoItem(Undo, Redo); UndoRedoUtility.Record(item); } Redo(); }
public void AttachComponent(SEntityComponentId child, SEntityComponentId parent) { CEngine.Instance.Dispatch(EEngineUpdatePriority.BeginFrame, () => { CSceneComponent parentObj = parent.GetComponent <CSceneComponent>(); CSceneComponent childObj = child.GetComponent <CSceneComponent>(); if (parentObj != null && childObj != null) { CSceneComponent oldParent = childObj.ParentComponent; SEntityComponentId oldParentid = new SEntityComponentId(oldParent); if (childObj.AttachToComponent(parentObj)) { UpdateEntityInformation_EngineThread(m_selectedObject.GetTargetEntityId(), true); void Undo() { CEngine.Instance.Dispatch(EEngineUpdatePriority.BeginFrame, () => { CSceneComponent oldParentInst = oldParentid.GetComponent <CSceneComponent>(); CSceneComponent childInst = child.GetComponent <CSceneComponent>(); if (oldParentInst != null && childInst != null) { childInst.AttachToComponent(oldParentInst); UpdateEntityInformation_EngineThread(child.EntityId, true); } }); } void Redo() { CEngine.Instance.Dispatch(EEngineUpdatePriority.BeginFrame, () => { CSceneComponent newParentInst = parent.GetComponent <CSceneComponent>(); CSceneComponent childInst = child.GetComponent <CSceneComponent>(); if (newParentInst != null && childInst != null) { childInst.AttachToComponent(newParentInst); UpdateEntityInformation_EngineThread(child.EntityId, true); } }); } CRelayUndoItem item = new CRelayUndoItem(Undo, Redo); UndoRedoUtility.Record(item); } } }); }
private void InitUndoRedo() { CEngine.Instance.Dispatch(EEngineUpdatePriority.BeginFrame, () => { CTransformGizmo.OnTranslationChanged += (tr, oldPos, newPos) => { void undo() { tr.SetWorldPosition(oldPos); } void redo() { tr.SetWorldPosition(newPos); } CRelayUndoItem item = new CRelayUndoItem(undo, redo); UndoRedoUtility.Record(item); }; CTransformGizmo.OnRotationChanged += (tr, oldRot, newRot) => { void undo() { tr.SetWorldRotation(oldRot); } void redo() { tr.SetWorldRotation(newRot); } CRelayUndoItem item = new CRelayUndoItem(undo, redo); UndoRedoUtility.Record(item); }; CTransformGizmo.OnScaleChanged += (tr, oldScale, newScale) => { void undo() { tr.SetWorldScale(oldScale); } void redo() { tr.SetWorldScale(newScale); } CRelayUndoItem item = new CRelayUndoItem(undo, redo); UndoRedoUtility.Record(item); }; }); }
internal void AttachEntities(SEntityId child, SEntityId parent) { CEngine.Instance.Dispatch(EEngineUpdatePriority.BeginFrame, () => { CEntity childEntity = child.GetEntity(); CEntity parentEntity = parent.GetEntity(); if (childEntity != null && parentEntity != null) { CEntity oldParent = childEntity.Parent; bool bOldParentExists = oldParent != null; SEntityId oldParentId = bOldParentExists ? new SEntityId(oldParent.Id) : SEntityId.Invalid; if (parentEntity.RootComponent != null && childEntity.RootComponent != null) { if (!parentEntity.RootComponent.IsChildOf(childEntity.RootComponent)) { childEntity.AttachToEntity(parentEntity); void Undo() { CEngine.Instance.Dispatch(EEngineUpdatePriority.BeginFrame, () => { CEntity childInst = child.GetEntity(); if (childInst != null) { if (bOldParentExists) { CEntity oldParentInst = oldParentId.GetEntity(); if (oldParentInst != null && childInst != null) { childInst.AttachToEntity(oldParentInst); } } else { childInst.Detach(); } } }); } void Redo() { CEngine.Instance.Dispatch(EEngineUpdatePriority.BeginFrame, () => { CEntity newParentInst = parent.GetEntity(); CEntity childInst = child.GetEntity(); if (newParentInst != null && childInst != null) { childInst.AttachToEntity(newParentInst); } }); } CRelayUndoItem item = new CRelayUndoItem(Undo, Redo); UndoRedoUtility.Record(item); } } } }); }
public static void MakeComponentRoot(SEntityComponentId id, bool bDispatch = true) { void Command() { CSceneComponent newRoot = id.GetComponent <CSceneComponent>(); if (newRoot == null) { return; } CSceneComponent oldRoot = newRoot.Owner.RootComponent; if (oldRoot == null) { return; } SEntityComponentId oldId = new SEntityComponentId(oldRoot); void Do() { CSceneComponent component = id.GetComponent <CSceneComponent>(); if (component != null) { CEntity owner = component.Owner; owner.SetRootComponent(component); } } void Redo() { CEngine.Instance.Dispatch(EEngineUpdatePriority.BeginFrame, () => { Do(); Application.Current.Dispatcher.BeginInvoke(System.Windows.Threading.DispatcherPriority.Normal, (Action)(() => { CWorkspace space = CWorkspace.Instance; space.SetSelectedObject(space.SelectedEditableObject, true); })); }); } void Undo() { CEngine.Instance.Dispatch(EEngineUpdatePriority.BeginFrame, () => { CSceneComponent component = oldId.GetComponent <CSceneComponent>(); if (component != null) { CEntity owner = component.Owner; owner.SetRootComponent(component); Application.Current.Dispatcher.BeginInvoke(System.Windows.Threading.DispatcherPriority.Normal, (Action)(() => { CWorkspace space = CWorkspace.Instance; space.SetSelectedObject(space.SelectedEditableObject, true); })); } }); } Do(); CUndoItem item = new CRelayUndoItem(Undo, Redo); UndoRedoUtility.Record(item); } if (bDispatch) { CEngine.Instance.Dispatch(EEngineUpdatePriority.BeginFrame, () => { Command(); }); } else { Command(); } }