| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863 |
- class EditAction
- {
- void Undo()
- {
- }
- void Redo()
- {
- }
- }
- class EditActionGroup
- {
- Array<EditAction@> actions;
- }
- class CreateNodeAction : EditAction
- {
- uint nodeID;
- uint parentID;
- XMLFile@ nodeData;
- void Define(Node@ node)
- {
- nodeID = node.id;
- parentID = node.parent.id;
- nodeData = XMLFile();
- XMLElement rootElem = nodeData.CreateRoot("node");
- node.SaveXML(rootElem);
- }
- void Undo()
- {
- Node@ parent = editorScene.GetNode(parentID);
- Node@ node = editorScene.GetNode(nodeID);
- if (parent !is null && node !is null)
- {
- parent.RemoveChild(node);
- hierarchyList.ClearSelection();
- }
- }
- void Redo()
- {
- Node@ parent = editorScene.GetNode(parentID);
- if (parent !is null)
- {
- Node@ node = parent.CreateChild("", nodeID < FIRST_LOCAL_ID ? REPLICATED : LOCAL, nodeID);
- node.LoadXML(nodeData.root);
- FocusNode(node);
- }
- }
- }
- class DeleteNodeAction : EditAction
- {
- uint nodeID;
- uint parentID;
- XMLFile@ nodeData;
- void Define(Node@ node)
- {
- nodeID = node.id;
- parentID = node.parent.id;
- nodeData = XMLFile();
- XMLElement rootElem = nodeData.CreateRoot("node");
- node.SaveXML(rootElem);
- rootElem.SetUInt("listItemIndex", GetListIndex(node));
- }
- void Undo()
- {
- Node@ parent = editorScene.GetNode(parentID);
- if (parent !is null)
- {
- // Handle update manually so that the node can be reinserted back into its previous list index
- suppressSceneChanges = true;
- Node@ node = parent.CreateChild("", nodeID < FIRST_LOCAL_ID ? REPLICATED : LOCAL, nodeID);
- if (node.LoadXML(nodeData.root))
- {
- uint listItemIndex = nodeData.root.GetUInt("listItemIndex");
- UIElement@ parentItem = hierarchyList.items[GetListIndex(parent)];
- UpdateHierarchyItem(listItemIndex, node, parentItem);
- FocusNode(node);
- }
- suppressSceneChanges = false;
- }
- }
- void Redo()
- {
- Node@ parent = editorScene.GetNode(parentID);
- Node@ node = editorScene.GetNode(nodeID);
- if (parent !is null && node !is null)
- {
- parent.RemoveChild(node);
- hierarchyList.ClearSelection();
- }
- }
- }
- class ReparentNodeAction : EditAction
- {
- uint nodeID;
- uint oldParentID;
- uint newParentID;
- Array<uint> nodeList; // 2 uints get inserted per node (node, node.parent)
- bool multiple;
- void Define(Node@ node, Node@ newParent)
- {
- multiple = false;
- nodeID = node.id;
- oldParentID = node.parent.id;
- newParentID = newParent.id;
- }
- void Define(Array<Node@> nodes, Node@ newParent)
- {
- multiple = true;
- newParentID = newParent.id;
- for(uint i = 0; i < nodes.length; ++i)
- {
- Node@ node = nodes[i];
- nodeList.Push(node.id);
- nodeList.Push(node.parent.id);
- }
- }
- void Undo()
- {
- if (multiple)
- {
- for (uint i = 0; i < nodeList.length; i+=2)
- {
- uint nodeID_ = nodeList[i];
- uint oldParentID_ = nodeList[i+1];
- Node@ parent = editorScene.GetNode(oldParentID_);
- Node@ node = editorScene.GetNode(nodeID_);
- if (parent !is null && node !is null)
- node.parent = parent;
- }
- }
- else
- {
- Node@ parent = editorScene.GetNode(oldParentID);
- Node@ node = editorScene.GetNode(nodeID);
- if (parent !is null && node !is null)
- node.parent = parent;
- }
- }
- void Redo()
- {
- if (multiple)
- {
- Node@ parent = editorScene.GetNode(newParentID);
- if (parent is null)
- return;
- for (uint i = 0; i < nodeList.length; i+=2)
- {
- uint nodeID_ = nodeList[i];
- Node@ node = editorScene.GetNode(nodeID_);
- if (node !is null)
- node.parent = parent;
- }
- }
- else
- {
- Node@ parent = editorScene.GetNode(newParentID);
- Node@ node = editorScene.GetNode(nodeID);
- if (parent !is null && node !is null)
- node.parent = parent;
- }
- }
- }
- class CreateComponentAction : EditAction
- {
- uint nodeID;
- uint componentID;
- XMLFile@ componentData;
- void Define(Component@ component)
- {
- componentID = component.id;
- nodeID = component.node.id;
- componentData = XMLFile();
- XMLElement rootElem = componentData.CreateRoot("component");
- component.SaveXML(rootElem);
- }
- void Undo()
- {
- Node@ node = editorScene.GetNode(nodeID);
- Component@ component = editorScene.GetComponent(componentID);
- if (node !is null && component !is null)
- {
- node.RemoveComponent(component);
- hierarchyList.ClearSelection();
- }
- }
- void Redo()
- {
- Node@ node = editorScene.GetNode(nodeID);
- if (node !is null)
- {
- Component@ component = node.CreateComponent(componentData.root.GetAttribute("type"), componentID < FIRST_LOCAL_ID ?
- REPLICATED : LOCAL, componentID);
- component.LoadXML(componentData.root);
- component.ApplyAttributes();
- FocusComponent(component);
- }
- }
- }
- class DeleteComponentAction : EditAction
- {
- uint nodeID;
- uint componentID;
- XMLFile@ componentData;
- void Define(Component@ component)
- {
- componentID = component.id;
- nodeID = component.node.id;
- componentData = XMLFile();
- XMLElement rootElem = componentData.CreateRoot("component");
- component.SaveXML(rootElem);
- rootElem.SetUInt("listItemIndex", GetComponentListIndex(component));
- }
- void Undo()
- {
- Node@ node = editorScene.GetNode(nodeID);
- if (node !is null)
- {
- // Handle update manually so that the component can be reinserted back into its previous list index
- suppressSceneChanges = true;
- Component@ component = node.CreateComponent(componentData.root.GetAttribute("type"), componentID < FIRST_LOCAL_ID ?
- REPLICATED : LOCAL, componentID);
- if (component.LoadXML(componentData.root))
- {
- component.ApplyAttributes();
- uint listItemIndex = componentData.root.GetUInt("listItemIndex");
- UIElement@ parentItem = hierarchyList.items[GetListIndex(node)];
- UpdateHierarchyItem(listItemIndex, component, parentItem);
- FocusComponent(component);
- }
- suppressSceneChanges = false;
- }
- }
- void Redo()
- {
- Node@ node = editorScene.GetNode(nodeID);
- Component@ component = editorScene.GetComponent(componentID);
- if (node !is null && component !is null)
- {
- node.RemoveComponent(component);
- hierarchyList.ClearSelection();
- }
- }
- }
- class EditAttributeAction : EditAction
- {
- int targetType;
- uint targetID;
- uint attrIndex;
- Variant undoValue;
- Variant redoValue;
- void Define(Serializable@ target, uint index, const Variant&in oldValue)
- {
- attrIndex = index;
- undoValue = oldValue;
- redoValue = target.attributes[index];
- targetType = GetType(target);
- targetID = GetID(target, targetType);
- }
- Serializable@ GetTarget()
- {
- switch (targetType)
- {
- case ITEM_NODE:
- return editorScene.GetNode(targetID);
- case ITEM_COMPONENT:
- return editorScene.GetComponent(targetID);
- case ITEM_UI_ELEMENT:
- return GetUIElementByID(targetID);
- }
- return null;
- }
- void Undo()
- {
- Serializable@ target = GetTarget();
- if (target !is null)
- {
- target.attributes[attrIndex] = undoValue;
- target.ApplyAttributes();
- // Can't know if need a full update, so assume true
- attributesFullDirty = true;
- // Apply side effects
- PostEditAttribute(target, attrIndex);
- if (targetType == ITEM_UI_ELEMENT)
- SetUIElementModified(target);
- else
- SetSceneModified();
- }
- }
- void Redo()
- {
- Serializable@ target = GetTarget();
- if (target !is null)
- {
- target.attributes[attrIndex] = redoValue;
- target.ApplyAttributes();
- // Can't know if need a full update, so assume true
- attributesFullDirty = true;
- // Apply side effects
- PostEditAttribute(target, attrIndex);
- if (targetType == ITEM_UI_ELEMENT)
- SetUIElementModified(target);
- else
- SetSceneModified();
- }
- }
- }
- class ResetAttributesAction : EditAction
- {
- int targetType;
- uint targetID;
- Array<Variant> undoValues;
- VariantMap internalVars; // UIElement specific
- void Define(Serializable@ target)
- {
- for (uint i = 0; i < target.numAttributes; ++i)
- undoValues.Push(target.attributes[i]);
- targetType = GetType(target);
- targetID = GetID(target, targetType);
- if (targetType == ITEM_UI_ELEMENT)
- {
- // Special handling for UIElement to preserve the internal variables containing the element's generated ID among others
- UIElement@ element = target;
- Array<StringHash> keys = element.vars.keys;
- for (uint i = 0; i < keys.length; ++i)
- {
- // If variable name is empty (or unregistered) then it is an internal variable and should be preserved
- String name = GetVariableName(keys[i]);
- if (name.empty)
- internalVars[keys[i]] = element.vars[keys[i]];
- }
- }
- }
- Serializable@ GetTarget()
- {
- switch (targetType)
- {
- case ITEM_NODE:
- return editorScene.GetNode(targetID);
- case ITEM_COMPONENT:
- return editorScene.GetComponent(targetID);
- case ITEM_UI_ELEMENT:
- return GetUIElementByID(targetID);
- }
- return null;
- }
- void SetInternalVars(UIElement@ element)
- {
- // Revert back internal variables
- Array<StringHash> keys = internalVars.keys;
- for (uint i = 0; i < keys.length; ++i)
- element.vars[keys[i]] = internalVars[keys[i]];
- if (element.vars.Contains(FILENAME_VAR))
- CenterDialog(element);
- }
- void Undo()
- {
- ui.cursor.shape = CS_BUSY;
- Serializable@ target = GetTarget();
- if (target !is null)
- {
- for (uint i = 0; i < target.numAttributes; ++i)
- {
- AttributeInfo info = target.attributeInfos[i];
- if (info.mode & AM_NOEDIT != 0 || info.mode & AM_NODEID != 0 || info.mode & AM_COMPONENTID != 0)
- continue;
- target.attributes[i] = undoValues[i];
- }
- target.ApplyAttributes();
- // Apply side effects
- for (uint i = 0; i < target.numAttributes; ++i)
- PostEditAttribute(target, i);
- if (targetType == ITEM_UI_ELEMENT)
- SetUIElementModified(target);
- else
- SetSceneModified();
- attributesFullDirty = true;
- }
- }
- void Redo()
- {
- ui.cursor.shape = CS_BUSY;
- Serializable@ target = GetTarget();
- if (target !is null)
- {
- for (uint i = 0; i < target.numAttributes; ++i)
- {
- AttributeInfo info = target.attributeInfos[i];
- if (info.mode & AM_NOEDIT != 0 || info.mode & AM_NODEID != 0 || info.mode & AM_COMPONENTID != 0)
- continue;
- target.attributes[i] = target.attributeDefaults[i];
- }
- if (targetType == ITEM_UI_ELEMENT)
- SetInternalVars(target);
- target.ApplyAttributes();
- // Apply side effects
- for (uint i = 0; i < target.numAttributes; ++i)
- PostEditAttribute(target, i);
- if (targetType == ITEM_UI_ELEMENT)
- SetUIElementModified(target);
- else
- SetSceneModified();
- attributesFullDirty = true;
- }
- }
- }
- class ToggleNodeEnabledAction : EditAction
- {
- uint nodeID;
- bool undoValue;
- void Define(Node@ node, bool oldEnabled)
- {
- nodeID = node.id;
- undoValue = oldEnabled;
- }
- void Undo()
- {
- Node@ node = editorScene.GetNode(nodeID);
- if (node !is null)
- node.SetEnabledRecursive(undoValue);
- }
- void Redo()
- {
- Node@ node = editorScene.GetNode(nodeID);
- if (node !is null)
- node.SetEnabledRecursive(!undoValue);
- }
- }
- class Transform
- {
- Vector3 position;
- Quaternion rotation;
- Vector3 scale;
- void Define(Node@ node)
- {
- position = node.position;
- rotation = node.rotation;
- scale = node.scale;
- }
- void Apply(Node@ node)
- {
- node.SetTransform(position, rotation, scale);
- }
- }
- class EditNodeTransformAction : EditAction
- {
- uint nodeID;
- Transform undoTransform;
- Transform redoTransform;
- void Define(Node@ node, const Transform&in oldTransform)
- {
- nodeID = node.id;
- undoTransform = oldTransform;
- redoTransform.Define(node);
- }
- void Undo()
- {
- Node@ node = editorScene.GetNode(nodeID);
- if (node !is null)
- {
- undoTransform.Apply(node);
- UpdateNodeAttributes();
- }
- }
- void Redo()
- {
- Node@ node = editorScene.GetNode(nodeID);
- if (node !is null)
- {
- redoTransform.Apply(node);
- UpdateNodeAttributes();
- }
- }
- }
- class CreateUIElementAction : EditAction
- {
- Variant elementID;
- Variant parentID;
- XMLFile@ elementData;
- XMLFile@ styleFile;
- void Define(UIElement@ element)
- {
- elementID = GetUIElementID(element);
- parentID = GetUIElementID(element.parent);
- elementData = XMLFile();
- XMLElement rootElem = elementData.CreateRoot("element");
- element.SaveXML(rootElem);
- styleFile = element.defaultStyle;
- }
- void Undo()
- {
- UIElement@ parent = GetUIElementByID(parentID);
- UIElement@ element = GetUIElementByID(elementID);
- if (parent !is null && element !is null)
- {
- parent.RemoveChild(element);
- hierarchyList.ClearSelection();
- SetUIElementModified(parent);
- }
- }
- void Redo()
- {
- UIElement@ parent = GetUIElementByID(parentID);
- if (parent !is null)
- {
- // Have to update manually because the element ID var is not set yet when the E_ELEMENTADDED event is sent
- suppressUIElementChanges = true;
- if (parent.LoadChildXML(elementData.root, styleFile))
- {
- UIElement@ element = parent.children[parent.numChildren - 1];
- UpdateHierarchyItem(element);
- FocusUIElement(element);
- SetUIElementModified(parent);
- }
- suppressUIElementChanges = false;
- }
- }
- }
- class DeleteUIElementAction : EditAction
- {
- Variant elementID;
- Variant parentID;
- XMLFile@ elementData;
- XMLFile@ styleFile;
- void Define(UIElement@ element)
- {
- elementID = GetUIElementID(element);
- parentID = GetUIElementID(element.parent);
- elementData = XMLFile();
- XMLElement rootElem = elementData.CreateRoot("element");
- element.SaveXML(rootElem);
- rootElem.SetUInt("index", element.parent.FindChild(element));
- rootElem.SetUInt("listItemIndex", GetListIndex(element));
- styleFile = element.defaultStyle;
- }
- void Undo()
- {
- UIElement@ parent = GetUIElementByID(parentID);
- if (parent !is null)
- {
- // Have to update manually because the element ID var is not set yet when the E_ELEMENTADDED event is sent
- suppressUIElementChanges = true;
- if (parent.LoadChildXML(elementData.root, styleFile))
- {
- XMLElement rootElem = elementData.root;
- uint index = rootElem.GetUInt("index");
- uint listItemIndex = rootElem.GetUInt("listItemIndex");
- UIElement@ element = parent.children[index];
- UIElement@ parentItem = hierarchyList.items[GetListIndex(parent)];
- UpdateHierarchyItem(listItemIndex, element, parentItem);
- FocusUIElement(element);
- SetUIElementModified(parent);
- }
- suppressUIElementChanges = false;
- }
- }
- void Redo()
- {
- UIElement@ parent = GetUIElementByID(parentID);
- UIElement@ element = GetUIElementByID(elementID);
- if (parent !is null && element !is null)
- {
- parent.RemoveChild(element);
- hierarchyList.ClearSelection();
- SetUIElementModified(parent);
- }
- }
- }
- class ReparentUIElementAction : EditAction
- {
- Variant elementID;
- Variant oldParentID;
- uint oldChildIndex;
- Variant newParentID;
- void Define(UIElement@ element, UIElement@ newParent)
- {
- elementID = GetUIElementID(element);
- oldParentID = GetUIElementID(element.parent);
- oldChildIndex = element.parent.FindChild(element);
- newParentID = GetUIElementID(newParent);
- }
- void Undo()
- {
- UIElement@ parent = GetUIElementByID(oldParentID);
- UIElement@ element = GetUIElementByID(elementID);
- if (parent !is null && element !is null)
- {
- element.SetParent(parent, oldChildIndex);
- SetUIElementModified(parent);
- }
- }
- void Redo()
- {
- UIElement@ parent = GetUIElementByID(newParentID);
- UIElement@ element = GetUIElementByID(elementID);
- if (parent !is null && element !is null)
- {
- element.parent = parent;
- SetUIElementModified(parent);
- }
- }
- }
- class ApplyUIElementStyleAction : EditAction
- {
- Variant elementID;
- Variant parentID;
- XMLFile@ elementData;
- XMLFile@ styleFile;
- String elementOldStyle;
- String elementNewStyle;
- void Define(UIElement@ element, const String&in newStyle)
- {
- elementID = GetUIElementID(element);
- parentID = GetUIElementID(element.parent);
- elementData = XMLFile();
- XMLElement rootElem = elementData.CreateRoot("element");
- element.SaveXML(rootElem);
- rootElem.SetUInt("index", element.parent.FindChild(element));
- rootElem.SetUInt("listItemIndex", GetListIndex(element));
- styleFile = element.defaultStyle;
- elementOldStyle = element.style;
- elementNewStyle = newStyle;
- }
- void ApplyStyle(const String&in style)
- {
- UIElement@ parent = GetUIElementByID(parentID);
- UIElement@ element = GetUIElementByID(elementID);
- if (parent !is null && element !is null)
- {
- // Apply the style in the XML data
- elementData.root.SetAttribute("style", style);
- // Have to update manually because the element ID var is not set yet when the E_ELEMENTADDED event is sent
- suppressUIElementChanges = true;
- parent.RemoveChild(element);
- if (parent.LoadChildXML(elementData.root, styleFile))
- {
- XMLElement rootElem = elementData.root;
- uint index = rootElem.GetUInt("index");
- uint listItemIndex = rootElem.GetUInt("listItemIndex");
- UIElement@ element = parent.children[index];
- UIElement@ parentItem = hierarchyList.items[GetListIndex(parent)];
- UpdateHierarchyItem(listItemIndex, element, parentItem);
- SetUIElementModified(element);
- hierarchyUpdateSelections.Push(listItemIndex);
- }
- suppressUIElementChanges = false;
- }
- }
- void Undo()
- {
- ApplyStyle(elementOldStyle);
- }
- void Redo()
- {
- ApplyStyle(elementNewStyle);
- }
- }
- class EditMaterialAction : EditAction
- {
- XMLFile@ oldState;
- XMLFile@ newState;
- WeakHandle material;
-
- void Define(Material@ material_, XMLFile@ oldState_)
- {
- material = material_;
- oldState = oldState_;
- newState = XMLFile();
-
- XMLElement materialElem = newState.CreateRoot("material");
- material_.Save(materialElem);
- }
- void Undo()
- {
- Material@ mat = material.Get();
- if (mat !is null)
- {
- mat.Load(oldState.root);
- RefreshMaterialEditor();
- }
- }
- void Redo()
- {
- Material@ mat = material.Get();
- if (mat !is null)
- {
- mat.Load(newState.root);
- RefreshMaterialEditor();
- }
- }
- }
- class AssignMaterialAction : EditAction
- {
- WeakHandle model;
- Array<String> oldMaterials;
- String newMaterialName;
- void Define(StaticModel@ model_, Array<String> oldMaterials_, Material@ newMaterial_)
- {
- model = model_;
- oldMaterials = oldMaterials_;
- newMaterialName = newMaterial_.name;
- }
- void Undo()
- {
- StaticModel@ staticModel = model.Get();
- if (staticModel is null)
- return;
- for(uint i=0; i<oldMaterials.length; ++i)
- {
- Material@ material = cache.GetResource("Material", oldMaterials[i]);
- staticModel.materials[i] = material;
- }
- }
- void Redo()
- {
- StaticModel@ staticModel = model.Get();
- if (staticModel is null)
- return;
- Material@ material = cache.GetResource("Material", newMaterialName);
- staticModel.material = material;
- }
- }
- class AssignModelAction : EditAction
- {
- WeakHandle staticModel;
- String oldModel;
- String newModel;
- void Define(StaticModel@ staticModel_, Model@ oldModel_, Model@ newModel_)
- {
- staticModel = staticModel_;
- oldModel = oldModel_.name;
- newModel = newModel_.name;
- }
- void Undo()
- {
- StaticModel@ staticModel_ = staticModel.Get();
- if (staticModel_ is null)
- return;
- Model@ model = cache.GetResource("Model", oldModel);
- if (model is null)
- return;
- staticModel_.model = model;
- }
- void Redo()
- {
- StaticModel@ staticModel_ = staticModel.Get();
- if (staticModel_ is null)
- return;
- Model@ model = cache.GetResource("Model", newModel);
- if (model is null)
- return;
- staticModel_.model = model;
- }
- }
|