EditorActions.as 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867
  1. class EditAction
  2. {
  3. void Undo()
  4. {
  5. }
  6. void Redo()
  7. {
  8. }
  9. }
  10. class EditActionGroup
  11. {
  12. Array<EditAction@> actions;
  13. }
  14. class CreateNodeAction : EditAction
  15. {
  16. uint nodeID;
  17. uint parentID;
  18. XMLFile@ nodeData;
  19. void Define(Node@ node)
  20. {
  21. nodeID = node.id;
  22. parentID = node.parent.id;
  23. nodeData = XMLFile();
  24. XMLElement rootElem = nodeData.CreateRoot("node");
  25. node.SaveXML(rootElem);
  26. }
  27. void Undo()
  28. {
  29. Node@ parent = editorScene.GetNode(parentID);
  30. Node@ node = editorScene.GetNode(nodeID);
  31. if (parent !is null && node !is null)
  32. {
  33. parent.RemoveChild(node);
  34. hierarchyList.ClearSelection();
  35. }
  36. }
  37. void Redo()
  38. {
  39. Node@ parent = editorScene.GetNode(parentID);
  40. if (parent !is null)
  41. {
  42. Node@ node = parent.CreateChild("", nodeID < FIRST_LOCAL_ID ? REPLICATED : LOCAL, nodeID);
  43. node.LoadXML(nodeData.root);
  44. FocusNode(node);
  45. }
  46. }
  47. }
  48. class DeleteNodeAction : EditAction
  49. {
  50. uint nodeID;
  51. uint parentID;
  52. XMLFile@ nodeData;
  53. void Define(Node@ node)
  54. {
  55. nodeID = node.id;
  56. parentID = node.parent.id;
  57. nodeData = XMLFile();
  58. XMLElement rootElem = nodeData.CreateRoot("node");
  59. node.SaveXML(rootElem);
  60. rootElem.SetUInt("listItemIndex", GetListIndex(node));
  61. }
  62. void Undo()
  63. {
  64. Node@ parent = editorScene.GetNode(parentID);
  65. if (parent !is null)
  66. {
  67. // Handle update manually so that the node can be reinserted back into its previous list index
  68. suppressSceneChanges = true;
  69. Node@ node = parent.CreateChild("", nodeID < FIRST_LOCAL_ID ? REPLICATED : LOCAL, nodeID);
  70. if (node.LoadXML(nodeData.root))
  71. {
  72. uint listItemIndex = nodeData.root.GetUInt("listItemIndex");
  73. UIElement@ parentItem = hierarchyList.items[GetListIndex(parent)];
  74. UpdateHierarchyItem(listItemIndex, node, parentItem);
  75. FocusNode(node);
  76. }
  77. suppressSceneChanges = false;
  78. }
  79. }
  80. void Redo()
  81. {
  82. Node@ parent = editorScene.GetNode(parentID);
  83. Node@ node = editorScene.GetNode(nodeID);
  84. if (parent !is null && node !is null)
  85. {
  86. parent.RemoveChild(node);
  87. hierarchyList.ClearSelection();
  88. }
  89. }
  90. }
  91. class ReparentNodeAction : EditAction
  92. {
  93. uint nodeID;
  94. uint oldParentID;
  95. uint newParentID;
  96. Array<uint> nodeList; // 2 uints get inserted per node (node, node.parent)
  97. bool multiple;
  98. void Define(Node@ node, Node@ newParent)
  99. {
  100. multiple = false;
  101. nodeID = node.id;
  102. oldParentID = node.parent.id;
  103. newParentID = newParent.id;
  104. }
  105. void Define(Array<Node@> nodes, Node@ newParent)
  106. {
  107. multiple = true;
  108. newParentID = newParent.id;
  109. for(uint i = 0; i < nodes.length; ++i)
  110. {
  111. Node@ node = nodes[i];
  112. nodeList.Push(node.id);
  113. nodeList.Push(node.parent.id);
  114. }
  115. }
  116. void Undo()
  117. {
  118. if (multiple)
  119. {
  120. for (uint i = 0; i < nodeList.length; i+=2)
  121. {
  122. uint nodeID_ = nodeList[i];
  123. uint oldParentID_ = nodeList[i+1];
  124. Node@ parent = editorScene.GetNode(oldParentID_);
  125. Node@ node = editorScene.GetNode(nodeID_);
  126. if (parent !is null && node !is null)
  127. node.parent = parent;
  128. }
  129. }
  130. else
  131. {
  132. Node@ parent = editorScene.GetNode(oldParentID);
  133. Node@ node = editorScene.GetNode(nodeID);
  134. if (parent !is null && node !is null)
  135. node.parent = parent;
  136. }
  137. }
  138. void Redo()
  139. {
  140. if (multiple)
  141. {
  142. Node@ parent = editorScene.GetNode(newParentID);
  143. if (parent is null)
  144. return;
  145. for (uint i = 0; i < nodeList.length; i+=2)
  146. {
  147. uint nodeID_ = nodeList[i];
  148. Node@ node = editorScene.GetNode(nodeID_);
  149. if (node !is null)
  150. node.parent = parent;
  151. }
  152. }
  153. else
  154. {
  155. Node@ parent = editorScene.GetNode(newParentID);
  156. Node@ node = editorScene.GetNode(nodeID);
  157. if (parent !is null && node !is null)
  158. node.parent = parent;
  159. }
  160. }
  161. }
  162. class CreateComponentAction : EditAction
  163. {
  164. uint nodeID;
  165. uint componentID;
  166. XMLFile@ componentData;
  167. void Define(Component@ component)
  168. {
  169. componentID = component.id;
  170. nodeID = component.node.id;
  171. componentData = XMLFile();
  172. XMLElement rootElem = componentData.CreateRoot("component");
  173. component.SaveXML(rootElem);
  174. }
  175. void Undo()
  176. {
  177. Node@ node = editorScene.GetNode(nodeID);
  178. Component@ component = editorScene.GetComponent(componentID);
  179. if (node !is null && component !is null)
  180. {
  181. node.RemoveComponent(component);
  182. hierarchyList.ClearSelection();
  183. }
  184. }
  185. void Redo()
  186. {
  187. Node@ node = editorScene.GetNode(nodeID);
  188. if (node !is null)
  189. {
  190. Component@ component = node.CreateComponent(componentData.root.GetAttribute("type"), componentID < FIRST_LOCAL_ID ?
  191. REPLICATED : LOCAL, componentID);
  192. component.LoadXML(componentData.root);
  193. component.ApplyAttributes();
  194. FocusComponent(component);
  195. }
  196. }
  197. }
  198. class DeleteComponentAction : EditAction
  199. {
  200. uint nodeID;
  201. uint componentID;
  202. XMLFile@ componentData;
  203. void Define(Component@ component)
  204. {
  205. componentID = component.id;
  206. nodeID = component.node.id;
  207. componentData = XMLFile();
  208. XMLElement rootElem = componentData.CreateRoot("component");
  209. component.SaveXML(rootElem);
  210. rootElem.SetUInt("listItemIndex", GetComponentListIndex(component));
  211. }
  212. void Undo()
  213. {
  214. Node@ node = editorScene.GetNode(nodeID);
  215. if (node !is null)
  216. {
  217. // Handle update manually so that the component can be reinserted back into its previous list index
  218. suppressSceneChanges = true;
  219. Component@ component = node.CreateComponent(componentData.root.GetAttribute("type"), componentID < FIRST_LOCAL_ID ?
  220. REPLICATED : LOCAL, componentID);
  221. if (component.LoadXML(componentData.root))
  222. {
  223. component.ApplyAttributes();
  224. uint listItemIndex = componentData.root.GetUInt("listItemIndex");
  225. UIElement@ parentItem = hierarchyList.items[GetListIndex(node)];
  226. UpdateHierarchyItem(listItemIndex, component, parentItem);
  227. FocusComponent(component);
  228. }
  229. suppressSceneChanges = false;
  230. }
  231. }
  232. void Redo()
  233. {
  234. Node@ node = editorScene.GetNode(nodeID);
  235. Component@ component = editorScene.GetComponent(componentID);
  236. if (node !is null && component !is null)
  237. {
  238. node.RemoveComponent(component);
  239. hierarchyList.ClearSelection();
  240. }
  241. }
  242. }
  243. class EditAttributeAction : EditAction
  244. {
  245. int targetType;
  246. uint targetID;
  247. uint attrIndex;
  248. Variant undoValue;
  249. Variant redoValue;
  250. void Define(Serializable@ target, uint index, const Variant&in oldValue)
  251. {
  252. attrIndex = index;
  253. undoValue = oldValue;
  254. redoValue = target.attributes[index];
  255. targetType = GetType(target);
  256. targetID = GetID(target, targetType);
  257. }
  258. Serializable@ GetTarget()
  259. {
  260. switch (targetType)
  261. {
  262. case ITEM_NODE:
  263. return editorScene.GetNode(targetID);
  264. case ITEM_COMPONENT:
  265. return editorScene.GetComponent(targetID);
  266. case ITEM_UI_ELEMENT:
  267. return GetUIElementByID(targetID);
  268. }
  269. return null;
  270. }
  271. void Undo()
  272. {
  273. Serializable@ target = GetTarget();
  274. if (target !is null)
  275. {
  276. target.attributes[attrIndex] = undoValue;
  277. target.ApplyAttributes();
  278. // Can't know if need a full update, so assume true
  279. attributesFullDirty = true;
  280. // Apply side effects
  281. PostEditAttribute(target, attrIndex);
  282. if (targetType == ITEM_UI_ELEMENT)
  283. SetUIElementModified(target);
  284. else
  285. SetSceneModified();
  286. EditScriptAttributes(target, attrIndex);
  287. }
  288. }
  289. void Redo()
  290. {
  291. Serializable@ target = GetTarget();
  292. if (target !is null)
  293. {
  294. target.attributes[attrIndex] = redoValue;
  295. target.ApplyAttributes();
  296. // Can't know if need a full update, so assume true
  297. attributesFullDirty = true;
  298. // Apply side effects
  299. PostEditAttribute(target, attrIndex);
  300. if (targetType == ITEM_UI_ELEMENT)
  301. SetUIElementModified(target);
  302. else
  303. SetSceneModified();
  304. EditScriptAttributes(target, attrIndex);
  305. }
  306. }
  307. }
  308. class ResetAttributesAction : EditAction
  309. {
  310. int targetType;
  311. uint targetID;
  312. Array<Variant> undoValues;
  313. VariantMap internalVars; // UIElement specific
  314. void Define(Serializable@ target)
  315. {
  316. for (uint i = 0; i < target.numAttributes; ++i)
  317. undoValues.Push(target.attributes[i]);
  318. targetType = GetType(target);
  319. targetID = GetID(target, targetType);
  320. if (targetType == ITEM_UI_ELEMENT)
  321. {
  322. // Special handling for UIElement to preserve the internal variables containing the element's generated ID among others
  323. UIElement@ element = target;
  324. Array<StringHash> keys = element.vars.keys;
  325. for (uint i = 0; i < keys.length; ++i)
  326. {
  327. // If variable name is empty (or unregistered) then it is an internal variable and should be preserved
  328. String name = GetVarName(keys[i]);
  329. if (name.empty)
  330. internalVars[keys[i]] = element.vars[keys[i]];
  331. }
  332. }
  333. }
  334. Serializable@ GetTarget()
  335. {
  336. switch (targetType)
  337. {
  338. case ITEM_NODE:
  339. return editorScene.GetNode(targetID);
  340. case ITEM_COMPONENT:
  341. return editorScene.GetComponent(targetID);
  342. case ITEM_UI_ELEMENT:
  343. return GetUIElementByID(targetID);
  344. }
  345. return null;
  346. }
  347. void SetInternalVars(UIElement@ element)
  348. {
  349. // Revert back internal variables
  350. Array<StringHash> keys = internalVars.keys;
  351. for (uint i = 0; i < keys.length; ++i)
  352. element.vars[keys[i]] = internalVars[keys[i]];
  353. if (element.vars.Contains(FILENAME_VAR))
  354. CenterDialog(element);
  355. }
  356. void Undo()
  357. {
  358. ui.cursor.shape = CS_BUSY;
  359. Serializable@ target = GetTarget();
  360. if (target !is null)
  361. {
  362. for (uint i = 0; i < target.numAttributes; ++i)
  363. {
  364. AttributeInfo info = target.attributeInfos[i];
  365. if (info.mode & AM_NOEDIT != 0 || info.mode & AM_NODEID != 0 || info.mode & AM_COMPONENTID != 0)
  366. continue;
  367. target.attributes[i] = undoValues[i];
  368. }
  369. target.ApplyAttributes();
  370. // Apply side effects
  371. for (uint i = 0; i < target.numAttributes; ++i)
  372. PostEditAttribute(target, i);
  373. if (targetType == ITEM_UI_ELEMENT)
  374. SetUIElementModified(target);
  375. else
  376. SetSceneModified();
  377. attributesFullDirty = true;
  378. }
  379. }
  380. void Redo()
  381. {
  382. ui.cursor.shape = CS_BUSY;
  383. Serializable@ target = GetTarget();
  384. if (target !is null)
  385. {
  386. for (uint i = 0; i < target.numAttributes; ++i)
  387. {
  388. AttributeInfo info = target.attributeInfos[i];
  389. if (info.mode & AM_NOEDIT != 0 || info.mode & AM_NODEID != 0 || info.mode & AM_COMPONENTID != 0)
  390. continue;
  391. target.attributes[i] = target.attributeDefaults[i];
  392. }
  393. if (targetType == ITEM_UI_ELEMENT)
  394. SetInternalVars(target);
  395. target.ApplyAttributes();
  396. // Apply side effects
  397. for (uint i = 0; i < target.numAttributes; ++i)
  398. PostEditAttribute(target, i);
  399. if (targetType == ITEM_UI_ELEMENT)
  400. SetUIElementModified(target);
  401. else
  402. SetSceneModified();
  403. attributesFullDirty = true;
  404. }
  405. }
  406. }
  407. class ToggleNodeEnabledAction : EditAction
  408. {
  409. uint nodeID;
  410. bool undoValue;
  411. void Define(Node@ node, bool oldEnabled)
  412. {
  413. nodeID = node.id;
  414. undoValue = oldEnabled;
  415. }
  416. void Undo()
  417. {
  418. Node@ node = editorScene.GetNode(nodeID);
  419. if (node !is null)
  420. node.SetEnabledRecursive(undoValue);
  421. }
  422. void Redo()
  423. {
  424. Node@ node = editorScene.GetNode(nodeID);
  425. if (node !is null)
  426. node.SetEnabledRecursive(!undoValue);
  427. }
  428. }
  429. class Transform
  430. {
  431. Vector3 position;
  432. Quaternion rotation;
  433. Vector3 scale;
  434. void Define(Node@ node)
  435. {
  436. position = node.position;
  437. rotation = node.rotation;
  438. scale = node.scale;
  439. }
  440. void Apply(Node@ node)
  441. {
  442. node.SetTransform(position, rotation, scale);
  443. }
  444. }
  445. class EditNodeTransformAction : EditAction
  446. {
  447. uint nodeID;
  448. Transform undoTransform;
  449. Transform redoTransform;
  450. void Define(Node@ node, const Transform&in oldTransform)
  451. {
  452. nodeID = node.id;
  453. undoTransform = oldTransform;
  454. redoTransform.Define(node);
  455. }
  456. void Undo()
  457. {
  458. Node@ node = editorScene.GetNode(nodeID);
  459. if (node !is null)
  460. {
  461. undoTransform.Apply(node);
  462. UpdateNodeAttributes();
  463. }
  464. }
  465. void Redo()
  466. {
  467. Node@ node = editorScene.GetNode(nodeID);
  468. if (node !is null)
  469. {
  470. redoTransform.Apply(node);
  471. UpdateNodeAttributes();
  472. }
  473. }
  474. }
  475. class CreateUIElementAction : EditAction
  476. {
  477. Variant elementID;
  478. Variant parentID;
  479. XMLFile@ elementData;
  480. XMLFile@ styleFile;
  481. void Define(UIElement@ element)
  482. {
  483. elementID = GetUIElementID(element);
  484. parentID = GetUIElementID(element.parent);
  485. elementData = XMLFile();
  486. XMLElement rootElem = elementData.CreateRoot("element");
  487. element.SaveXML(rootElem);
  488. styleFile = element.defaultStyle;
  489. }
  490. void Undo()
  491. {
  492. UIElement@ parent = GetUIElementByID(parentID);
  493. UIElement@ element = GetUIElementByID(elementID);
  494. if (parent !is null && element !is null)
  495. {
  496. parent.RemoveChild(element);
  497. hierarchyList.ClearSelection();
  498. SetUIElementModified(parent);
  499. }
  500. }
  501. void Redo()
  502. {
  503. UIElement@ parent = GetUIElementByID(parentID);
  504. if (parent !is null)
  505. {
  506. // Have to update manually because the element ID var is not set yet when the E_ELEMENTADDED event is sent
  507. suppressUIElementChanges = true;
  508. if (parent.LoadChildXML(elementData.root, styleFile))
  509. {
  510. UIElement@ element = parent.children[parent.numChildren - 1];
  511. UpdateHierarchyItem(element);
  512. FocusUIElement(element);
  513. SetUIElementModified(parent);
  514. }
  515. suppressUIElementChanges = false;
  516. }
  517. }
  518. }
  519. class DeleteUIElementAction : EditAction
  520. {
  521. Variant elementID;
  522. Variant parentID;
  523. XMLFile@ elementData;
  524. XMLFile@ styleFile;
  525. void Define(UIElement@ element)
  526. {
  527. elementID = GetUIElementID(element);
  528. parentID = GetUIElementID(element.parent);
  529. elementData = XMLFile();
  530. XMLElement rootElem = elementData.CreateRoot("element");
  531. element.SaveXML(rootElem);
  532. rootElem.SetUInt("index", element.parent.FindChild(element));
  533. rootElem.SetUInt("listItemIndex", GetListIndex(element));
  534. styleFile = element.defaultStyle;
  535. }
  536. void Undo()
  537. {
  538. UIElement@ parent = GetUIElementByID(parentID);
  539. if (parent !is null)
  540. {
  541. // Have to update manually because the element ID var is not set yet when the E_ELEMENTADDED event is sent
  542. suppressUIElementChanges = true;
  543. if (parent.LoadChildXML(elementData.root, styleFile))
  544. {
  545. XMLElement rootElem = elementData.root;
  546. uint index = rootElem.GetUInt("index");
  547. uint listItemIndex = rootElem.GetUInt("listItemIndex");
  548. UIElement@ element = parent.children[index];
  549. UIElement@ parentItem = hierarchyList.items[GetListIndex(parent)];
  550. UpdateHierarchyItem(listItemIndex, element, parentItem);
  551. FocusUIElement(element);
  552. SetUIElementModified(parent);
  553. }
  554. suppressUIElementChanges = false;
  555. }
  556. }
  557. void Redo()
  558. {
  559. UIElement@ parent = GetUIElementByID(parentID);
  560. UIElement@ element = GetUIElementByID(elementID);
  561. if (parent !is null && element !is null)
  562. {
  563. parent.RemoveChild(element);
  564. hierarchyList.ClearSelection();
  565. SetUIElementModified(parent);
  566. }
  567. }
  568. }
  569. class ReparentUIElementAction : EditAction
  570. {
  571. Variant elementID;
  572. Variant oldParentID;
  573. uint oldChildIndex;
  574. Variant newParentID;
  575. void Define(UIElement@ element, UIElement@ newParent)
  576. {
  577. elementID = GetUIElementID(element);
  578. oldParentID = GetUIElementID(element.parent);
  579. oldChildIndex = element.parent.FindChild(element);
  580. newParentID = GetUIElementID(newParent);
  581. }
  582. void Undo()
  583. {
  584. UIElement@ parent = GetUIElementByID(oldParentID);
  585. UIElement@ element = GetUIElementByID(elementID);
  586. if (parent !is null && element !is null)
  587. {
  588. element.SetParent(parent, oldChildIndex);
  589. SetUIElementModified(parent);
  590. }
  591. }
  592. void Redo()
  593. {
  594. UIElement@ parent = GetUIElementByID(newParentID);
  595. UIElement@ element = GetUIElementByID(elementID);
  596. if (parent !is null && element !is null)
  597. {
  598. element.parent = parent;
  599. SetUIElementModified(parent);
  600. }
  601. }
  602. }
  603. class ApplyUIElementStyleAction : EditAction
  604. {
  605. Variant elementID;
  606. Variant parentID;
  607. XMLFile@ elementData;
  608. XMLFile@ styleFile;
  609. String elementOldStyle;
  610. String elementNewStyle;
  611. void Define(UIElement@ element, const String&in newStyle)
  612. {
  613. elementID = GetUIElementID(element);
  614. parentID = GetUIElementID(element.parent);
  615. elementData = XMLFile();
  616. XMLElement rootElem = elementData.CreateRoot("element");
  617. element.SaveXML(rootElem);
  618. rootElem.SetUInt("index", element.parent.FindChild(element));
  619. rootElem.SetUInt("listItemIndex", GetListIndex(element));
  620. styleFile = element.defaultStyle;
  621. elementOldStyle = element.style;
  622. elementNewStyle = newStyle;
  623. }
  624. void ApplyStyle(const String&in style)
  625. {
  626. UIElement@ parent = GetUIElementByID(parentID);
  627. UIElement@ element = GetUIElementByID(elementID);
  628. if (parent !is null && element !is null)
  629. {
  630. // Apply the style in the XML data
  631. elementData.root.SetAttribute("style", style);
  632. // Have to update manually because the element ID var is not set yet when the E_ELEMENTADDED event is sent
  633. suppressUIElementChanges = true;
  634. parent.RemoveChild(element);
  635. if (parent.LoadChildXML(elementData.root, styleFile))
  636. {
  637. XMLElement rootElem = elementData.root;
  638. uint index = rootElem.GetUInt("index");
  639. uint listItemIndex = rootElem.GetUInt("listItemIndex");
  640. UIElement@ element = parent.children[index];
  641. UIElement@ parentItem = hierarchyList.items[GetListIndex(parent)];
  642. UpdateHierarchyItem(listItemIndex, element, parentItem);
  643. SetUIElementModified(element);
  644. hierarchyUpdateSelections.Push(listItemIndex);
  645. }
  646. suppressUIElementChanges = false;
  647. }
  648. }
  649. void Undo()
  650. {
  651. ApplyStyle(elementOldStyle);
  652. }
  653. void Redo()
  654. {
  655. ApplyStyle(elementNewStyle);
  656. }
  657. }
  658. class EditMaterialAction : EditAction
  659. {
  660. XMLFile@ oldState;
  661. XMLFile@ newState;
  662. WeakHandle material;
  663. void Define(Material@ material_, XMLFile@ oldState_)
  664. {
  665. material = material_;
  666. oldState = oldState_;
  667. newState = XMLFile();
  668. XMLElement materialElem = newState.CreateRoot("material");
  669. material_.Save(materialElem);
  670. }
  671. void Undo()
  672. {
  673. Material@ mat = material.Get();
  674. if (mat !is null)
  675. {
  676. mat.Load(oldState.root);
  677. RefreshMaterialEditor();
  678. }
  679. }
  680. void Redo()
  681. {
  682. Material@ mat = material.Get();
  683. if (mat !is null)
  684. {
  685. mat.Load(newState.root);
  686. RefreshMaterialEditor();
  687. }
  688. }
  689. }
  690. class AssignMaterialAction : EditAction
  691. {
  692. WeakHandle model;
  693. Array<String> oldMaterials;
  694. String newMaterialName;
  695. void Define(StaticModel@ model_, Array<String> oldMaterials_, Material@ newMaterial_)
  696. {
  697. model = model_;
  698. oldMaterials = oldMaterials_;
  699. newMaterialName = newMaterial_.name;
  700. }
  701. void Undo()
  702. {
  703. StaticModel@ staticModel = model.Get();
  704. if (staticModel is null)
  705. return;
  706. for(uint i=0; i<oldMaterials.length; ++i)
  707. {
  708. Material@ material = cache.GetResource("Material", oldMaterials[i]);
  709. staticModel.materials[i] = material;
  710. }
  711. }
  712. void Redo()
  713. {
  714. StaticModel@ staticModel = model.Get();
  715. if (staticModel is null)
  716. return;
  717. Material@ material = cache.GetResource("Material", newMaterialName);
  718. staticModel.material = material;
  719. }
  720. }
  721. class AssignModelAction : EditAction
  722. {
  723. WeakHandle staticModel;
  724. String oldModel;
  725. String newModel;
  726. void Define(StaticModel@ staticModel_, Model@ oldModel_, Model@ newModel_)
  727. {
  728. staticModel = staticModel_;
  729. oldModel = oldModel_.name;
  730. newModel = newModel_.name;
  731. }
  732. void Undo()
  733. {
  734. StaticModel@ staticModel_ = staticModel.Get();
  735. if (staticModel_ is null)
  736. return;
  737. Model@ model = cache.GetResource("Model", oldModel);
  738. if (model is null)
  739. return;
  740. staticModel_.model = model;
  741. }
  742. void Redo()
  743. {
  744. StaticModel@ staticModel_ = staticModel.Get();
  745. if (staticModel_ is null)
  746. return;
  747. Model@ model = cache.GetResource("Model", newModel);
  748. if (model is null)
  749. return;
  750. staticModel_.model = model;
  751. }
  752. }