EditorActions.as 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863
  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. }
  287. }
  288. void Redo()
  289. {
  290. Serializable@ target = GetTarget();
  291. if (target !is null)
  292. {
  293. target.attributes[attrIndex] = redoValue;
  294. target.ApplyAttributes();
  295. // Can't know if need a full update, so assume true
  296. attributesFullDirty = true;
  297. // Apply side effects
  298. PostEditAttribute(target, attrIndex);
  299. if (targetType == ITEM_UI_ELEMENT)
  300. SetUIElementModified(target);
  301. else
  302. SetSceneModified();
  303. }
  304. }
  305. }
  306. class ResetAttributesAction : EditAction
  307. {
  308. int targetType;
  309. uint targetID;
  310. Array<Variant> undoValues;
  311. VariantMap internalVars; // UIElement specific
  312. void Define(Serializable@ target)
  313. {
  314. for (uint i = 0; i < target.numAttributes; ++i)
  315. undoValues.Push(target.attributes[i]);
  316. targetType = GetType(target);
  317. targetID = GetID(target, targetType);
  318. if (targetType == ITEM_UI_ELEMENT)
  319. {
  320. // Special handling for UIElement to preserve the internal variables containing the element's generated ID among others
  321. UIElement@ element = target;
  322. Array<StringHash> keys = element.vars.keys;
  323. for (uint i = 0; i < keys.length; ++i)
  324. {
  325. // If variable name is empty (or unregistered) then it is an internal variable and should be preserved
  326. String name = GetVariableName(keys[i]);
  327. if (name.empty)
  328. internalVars[keys[i]] = element.vars[keys[i]];
  329. }
  330. }
  331. }
  332. Serializable@ GetTarget()
  333. {
  334. switch (targetType)
  335. {
  336. case ITEM_NODE:
  337. return editorScene.GetNode(targetID);
  338. case ITEM_COMPONENT:
  339. return editorScene.GetComponent(targetID);
  340. case ITEM_UI_ELEMENT:
  341. return GetUIElementByID(targetID);
  342. }
  343. return null;
  344. }
  345. void SetInternalVars(UIElement@ element)
  346. {
  347. // Revert back internal variables
  348. Array<StringHash> keys = internalVars.keys;
  349. for (uint i = 0; i < keys.length; ++i)
  350. element.vars[keys[i]] = internalVars[keys[i]];
  351. if (element.vars.Contains(FILENAME_VAR))
  352. CenterDialog(element);
  353. }
  354. void Undo()
  355. {
  356. ui.cursor.shape = CS_BUSY;
  357. Serializable@ target = GetTarget();
  358. if (target !is null)
  359. {
  360. for (uint i = 0; i < target.numAttributes; ++i)
  361. {
  362. AttributeInfo info = target.attributeInfos[i];
  363. if (info.mode & AM_NOEDIT != 0 || info.mode & AM_NODEID != 0 || info.mode & AM_COMPONENTID != 0)
  364. continue;
  365. target.attributes[i] = undoValues[i];
  366. }
  367. target.ApplyAttributes();
  368. // Apply side effects
  369. for (uint i = 0; i < target.numAttributes; ++i)
  370. PostEditAttribute(target, i);
  371. if (targetType == ITEM_UI_ELEMENT)
  372. SetUIElementModified(target);
  373. else
  374. SetSceneModified();
  375. attributesFullDirty = true;
  376. }
  377. }
  378. void Redo()
  379. {
  380. ui.cursor.shape = CS_BUSY;
  381. Serializable@ target = GetTarget();
  382. if (target !is null)
  383. {
  384. for (uint i = 0; i < target.numAttributes; ++i)
  385. {
  386. AttributeInfo info = target.attributeInfos[i];
  387. if (info.mode & AM_NOEDIT != 0 || info.mode & AM_NODEID != 0 || info.mode & AM_COMPONENTID != 0)
  388. continue;
  389. target.attributes[i] = target.attributeDefaults[i];
  390. }
  391. if (targetType == ITEM_UI_ELEMENT)
  392. SetInternalVars(target);
  393. target.ApplyAttributes();
  394. // Apply side effects
  395. for (uint i = 0; i < target.numAttributes; ++i)
  396. PostEditAttribute(target, i);
  397. if (targetType == ITEM_UI_ELEMENT)
  398. SetUIElementModified(target);
  399. else
  400. SetSceneModified();
  401. attributesFullDirty = true;
  402. }
  403. }
  404. }
  405. class ToggleNodeEnabledAction : EditAction
  406. {
  407. uint nodeID;
  408. bool undoValue;
  409. void Define(Node@ node, bool oldEnabled)
  410. {
  411. nodeID = node.id;
  412. undoValue = oldEnabled;
  413. }
  414. void Undo()
  415. {
  416. Node@ node = editorScene.GetNode(nodeID);
  417. if (node !is null)
  418. node.SetEnabledRecursive(undoValue);
  419. }
  420. void Redo()
  421. {
  422. Node@ node = editorScene.GetNode(nodeID);
  423. if (node !is null)
  424. node.SetEnabledRecursive(!undoValue);
  425. }
  426. }
  427. class Transform
  428. {
  429. Vector3 position;
  430. Quaternion rotation;
  431. Vector3 scale;
  432. void Define(Node@ node)
  433. {
  434. position = node.position;
  435. rotation = node.rotation;
  436. scale = node.scale;
  437. }
  438. void Apply(Node@ node)
  439. {
  440. node.SetTransform(position, rotation, scale);
  441. }
  442. }
  443. class EditNodeTransformAction : EditAction
  444. {
  445. uint nodeID;
  446. Transform undoTransform;
  447. Transform redoTransform;
  448. void Define(Node@ node, const Transform&in oldTransform)
  449. {
  450. nodeID = node.id;
  451. undoTransform = oldTransform;
  452. redoTransform.Define(node);
  453. }
  454. void Undo()
  455. {
  456. Node@ node = editorScene.GetNode(nodeID);
  457. if (node !is null)
  458. {
  459. undoTransform.Apply(node);
  460. UpdateNodeAttributes();
  461. }
  462. }
  463. void Redo()
  464. {
  465. Node@ node = editorScene.GetNode(nodeID);
  466. if (node !is null)
  467. {
  468. redoTransform.Apply(node);
  469. UpdateNodeAttributes();
  470. }
  471. }
  472. }
  473. class CreateUIElementAction : EditAction
  474. {
  475. Variant elementID;
  476. Variant parentID;
  477. XMLFile@ elementData;
  478. XMLFile@ styleFile;
  479. void Define(UIElement@ element)
  480. {
  481. elementID = GetUIElementID(element);
  482. parentID = GetUIElementID(element.parent);
  483. elementData = XMLFile();
  484. XMLElement rootElem = elementData.CreateRoot("element");
  485. element.SaveXML(rootElem);
  486. styleFile = element.defaultStyle;
  487. }
  488. void Undo()
  489. {
  490. UIElement@ parent = GetUIElementByID(parentID);
  491. UIElement@ element = GetUIElementByID(elementID);
  492. if (parent !is null && element !is null)
  493. {
  494. parent.RemoveChild(element);
  495. hierarchyList.ClearSelection();
  496. SetUIElementModified(parent);
  497. }
  498. }
  499. void Redo()
  500. {
  501. UIElement@ parent = GetUIElementByID(parentID);
  502. if (parent !is null)
  503. {
  504. // Have to update manually because the element ID var is not set yet when the E_ELEMENTADDED event is sent
  505. suppressUIElementChanges = true;
  506. if (parent.LoadChildXML(elementData.root, styleFile))
  507. {
  508. UIElement@ element = parent.children[parent.numChildren - 1];
  509. UpdateHierarchyItem(element);
  510. FocusUIElement(element);
  511. SetUIElementModified(parent);
  512. }
  513. suppressUIElementChanges = false;
  514. }
  515. }
  516. }
  517. class DeleteUIElementAction : EditAction
  518. {
  519. Variant elementID;
  520. Variant parentID;
  521. XMLFile@ elementData;
  522. XMLFile@ styleFile;
  523. void Define(UIElement@ element)
  524. {
  525. elementID = GetUIElementID(element);
  526. parentID = GetUIElementID(element.parent);
  527. elementData = XMLFile();
  528. XMLElement rootElem = elementData.CreateRoot("element");
  529. element.SaveXML(rootElem);
  530. rootElem.SetUInt("index", element.parent.FindChild(element));
  531. rootElem.SetUInt("listItemIndex", GetListIndex(element));
  532. styleFile = element.defaultStyle;
  533. }
  534. void Undo()
  535. {
  536. UIElement@ parent = GetUIElementByID(parentID);
  537. if (parent !is null)
  538. {
  539. // Have to update manually because the element ID var is not set yet when the E_ELEMENTADDED event is sent
  540. suppressUIElementChanges = true;
  541. if (parent.LoadChildXML(elementData.root, styleFile))
  542. {
  543. XMLElement rootElem = elementData.root;
  544. uint index = rootElem.GetUInt("index");
  545. uint listItemIndex = rootElem.GetUInt("listItemIndex");
  546. UIElement@ element = parent.children[index];
  547. UIElement@ parentItem = hierarchyList.items[GetListIndex(parent)];
  548. UpdateHierarchyItem(listItemIndex, element, parentItem);
  549. FocusUIElement(element);
  550. SetUIElementModified(parent);
  551. }
  552. suppressUIElementChanges = false;
  553. }
  554. }
  555. void Redo()
  556. {
  557. UIElement@ parent = GetUIElementByID(parentID);
  558. UIElement@ element = GetUIElementByID(elementID);
  559. if (parent !is null && element !is null)
  560. {
  561. parent.RemoveChild(element);
  562. hierarchyList.ClearSelection();
  563. SetUIElementModified(parent);
  564. }
  565. }
  566. }
  567. class ReparentUIElementAction : EditAction
  568. {
  569. Variant elementID;
  570. Variant oldParentID;
  571. uint oldChildIndex;
  572. Variant newParentID;
  573. void Define(UIElement@ element, UIElement@ newParent)
  574. {
  575. elementID = GetUIElementID(element);
  576. oldParentID = GetUIElementID(element.parent);
  577. oldChildIndex = element.parent.FindChild(element);
  578. newParentID = GetUIElementID(newParent);
  579. }
  580. void Undo()
  581. {
  582. UIElement@ parent = GetUIElementByID(oldParentID);
  583. UIElement@ element = GetUIElementByID(elementID);
  584. if (parent !is null && element !is null)
  585. {
  586. element.SetParent(parent, oldChildIndex);
  587. SetUIElementModified(parent);
  588. }
  589. }
  590. void Redo()
  591. {
  592. UIElement@ parent = GetUIElementByID(newParentID);
  593. UIElement@ element = GetUIElementByID(elementID);
  594. if (parent !is null && element !is null)
  595. {
  596. element.parent = parent;
  597. SetUIElementModified(parent);
  598. }
  599. }
  600. }
  601. class ApplyUIElementStyleAction : EditAction
  602. {
  603. Variant elementID;
  604. Variant parentID;
  605. XMLFile@ elementData;
  606. XMLFile@ styleFile;
  607. String elementOldStyle;
  608. String elementNewStyle;
  609. void Define(UIElement@ element, const String&in newStyle)
  610. {
  611. elementID = GetUIElementID(element);
  612. parentID = GetUIElementID(element.parent);
  613. elementData = XMLFile();
  614. XMLElement rootElem = elementData.CreateRoot("element");
  615. element.SaveXML(rootElem);
  616. rootElem.SetUInt("index", element.parent.FindChild(element));
  617. rootElem.SetUInt("listItemIndex", GetListIndex(element));
  618. styleFile = element.defaultStyle;
  619. elementOldStyle = element.style;
  620. elementNewStyle = newStyle;
  621. }
  622. void ApplyStyle(const String&in style)
  623. {
  624. UIElement@ parent = GetUIElementByID(parentID);
  625. UIElement@ element = GetUIElementByID(elementID);
  626. if (parent !is null && element !is null)
  627. {
  628. // Apply the style in the XML data
  629. elementData.root.SetAttribute("style", style);
  630. // Have to update manually because the element ID var is not set yet when the E_ELEMENTADDED event is sent
  631. suppressUIElementChanges = true;
  632. parent.RemoveChild(element);
  633. if (parent.LoadChildXML(elementData.root, styleFile))
  634. {
  635. XMLElement rootElem = elementData.root;
  636. uint index = rootElem.GetUInt("index");
  637. uint listItemIndex = rootElem.GetUInt("listItemIndex");
  638. UIElement@ element = parent.children[index];
  639. UIElement@ parentItem = hierarchyList.items[GetListIndex(parent)];
  640. UpdateHierarchyItem(listItemIndex, element, parentItem);
  641. SetUIElementModified(element);
  642. hierarchyUpdateSelections.Push(listItemIndex);
  643. }
  644. suppressUIElementChanges = false;
  645. }
  646. }
  647. void Undo()
  648. {
  649. ApplyStyle(elementOldStyle);
  650. }
  651. void Redo()
  652. {
  653. ApplyStyle(elementNewStyle);
  654. }
  655. }
  656. class EditMaterialAction : EditAction
  657. {
  658. XMLFile@ oldState;
  659. XMLFile@ newState;
  660. WeakHandle material;
  661. void Define(Material@ material_, XMLFile@ oldState_)
  662. {
  663. material = material_;
  664. oldState = oldState_;
  665. newState = XMLFile();
  666. XMLElement materialElem = newState.CreateRoot("material");
  667. material_.Save(materialElem);
  668. }
  669. void Undo()
  670. {
  671. Material@ mat = material.Get();
  672. if (mat !is null)
  673. {
  674. mat.Load(oldState.root);
  675. RefreshMaterialEditor();
  676. }
  677. }
  678. void Redo()
  679. {
  680. Material@ mat = material.Get();
  681. if (mat !is null)
  682. {
  683. mat.Load(newState.root);
  684. RefreshMaterialEditor();
  685. }
  686. }
  687. }
  688. class AssignMaterialAction : EditAction
  689. {
  690. WeakHandle model;
  691. Array<String> oldMaterials;
  692. String newMaterialName;
  693. void Define(StaticModel@ model_, Array<String> oldMaterials_, Material@ newMaterial_)
  694. {
  695. model = model_;
  696. oldMaterials = oldMaterials_;
  697. newMaterialName = newMaterial_.name;
  698. }
  699. void Undo()
  700. {
  701. StaticModel@ staticModel = model.Get();
  702. if (staticModel is null)
  703. return;
  704. for(uint i=0; i<oldMaterials.length; ++i)
  705. {
  706. Material@ material = cache.GetResource("Material", oldMaterials[i]);
  707. staticModel.materials[i] = material;
  708. }
  709. }
  710. void Redo()
  711. {
  712. StaticModel@ staticModel = model.Get();
  713. if (staticModel is null)
  714. return;
  715. Material@ material = cache.GetResource("Material", newMaterialName);
  716. staticModel.material = material;
  717. }
  718. }
  719. class AssignModelAction : EditAction
  720. {
  721. WeakHandle staticModel;
  722. String oldModel;
  723. String newModel;
  724. void Define(StaticModel@ staticModel_, Model@ oldModel_, Model@ newModel_)
  725. {
  726. staticModel = staticModel_;
  727. oldModel = oldModel_.name;
  728. newModel = newModel_.name;
  729. }
  730. void Undo()
  731. {
  732. StaticModel@ staticModel_ = staticModel.Get();
  733. if (staticModel_ is null)
  734. return;
  735. Model@ model = cache.GetResource("Model", oldModel);
  736. if (model is null)
  737. return;
  738. staticModel_.model = model;
  739. }
  740. void Redo()
  741. {
  742. StaticModel@ staticModel_ = staticModel.Get();
  743. if (staticModel_ is null)
  744. return;
  745. Model@ model = cache.GetResource("Model", newModel);
  746. if (model is null)
  747. return;
  748. staticModel_.model = model;
  749. }
  750. }