ActionsBuilderForm.cs 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. using System;
  2. using System.IO;
  3. using System.Collections.Generic;
  4. using System.Windows.Forms;
  5. using Autodesk.Max;
  6. using System.Runtime.InteropServices;
  7. namespace Max2Babylon
  8. {
  9. public partial class ActionsBuilderForm : Form
  10. {
  11. private readonly BabylonActionsBuilderActionItem _babylonActionsBuilderAction;
  12. private IINode _node = null;
  13. private HtmlDocument _document;
  14. private string _objectName;
  15. private string _propertyName = "babylon_actionsbuilder";
  16. private string _jsonResult = "";
  17. private bool isRootNode;
  18. public ActionsBuilderForm(BabylonActionsBuilderActionItem babylonActionsBuilderAction)
  19. {
  20. InitializeComponent();
  21. // Finish
  22. _babylonActionsBuilderAction = babylonActionsBuilderAction;
  23. }
  24. private void ActionsBuilderForm_Load(object sender, EventArgs e)
  25. {
  26. if (Loader.Core.SelNodeCount > 0)
  27. {
  28. isRootNode = false;
  29. _node = Loader.Core.GetSelNode(0);
  30. }
  31. else
  32. {
  33. isRootNode = true;
  34. _node = Loader.Core.RootNode;
  35. }
  36. _objectName = _node.Name;
  37. // Set url (webview)
  38. string assemblyPath = Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().CodeBase);
  39. ActionsBuilderWebView.Url = new Uri(string.Format("{0}/BabylonActionsBuilder/index.html", assemblyPath), System.UriKind.Absolute);
  40. }
  41. private void ActionsBuilderForm_FormClosed(object sender, FormClosedEventArgs e)
  42. {
  43. _babylonActionsBuilderAction.Close();
  44. }
  45. private void fillObjectsList(ITab<IIGameNode> list, string scriptName)
  46. {
  47. object[] names = new object[list.Count];
  48. for (int i = 0; i < list.Count; i++)
  49. {
  50. var indexer = new IntPtr(i);
  51. var node = list[indexer];
  52. names[i] = node.MaxNode.Name;
  53. }
  54. _document.InvokeScript(scriptName, names);
  55. }
  56. private void fillSoundsList(ITab<IIGameNode> list, string scriptName)
  57. {
  58. object[] names = new object[list.Count];
  59. for (int i = 0; i < list.Count; i++)
  60. {
  61. var indexer = new IntPtr(i);
  62. var node = list[indexer].MaxNode;
  63. string soundFile = "";
  64. soundFile = Tools.GetStringProperty(node, "babylonjs_sound_filename", soundFile);
  65. names[i] = Path.GetFileName(soundFile);
  66. }
  67. _document.InvokeScript(scriptName, names);
  68. }
  69. private void ActionsBuilderWebView_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
  70. {
  71. // Set common properties (name, is scene or object, etc.)
  72. _document = ActionsBuilderWebView.Document;
  73. _document.GetElementById("ActionsBuilderObjectName").SetAttribute("value", _objectName);
  74. if (isRootNode)
  75. _document.InvokeScript("setIsScene");
  76. else
  77. _document.InvokeScript("setIsObject");
  78. _document.InvokeScript("updateObjectName");
  79. if (getProperty())
  80. {
  81. _document.GetElementById("ActionsBuilderJSON").SetAttribute("value", _jsonResult);
  82. _document.InvokeScript("updateGraphFromJSON");
  83. }
  84. // Set lists of meshes, lights, cameras etc.
  85. var gameScene = Loader.Global.IGameInterface;
  86. gameScene.InitialiseIGame(false);
  87. var meshes = gameScene.GetIGameNodeByType(Autodesk.Max.IGameObject.ObjectTypes.Mesh);
  88. fillObjectsList(meshes, "setMeshesNames");
  89. var lights = gameScene.GetIGameNodeByType(Autodesk.Max.IGameObject.ObjectTypes.Light);
  90. fillObjectsList(lights, "setLightsNames");
  91. var cameras = gameScene.GetIGameNodeByType(Autodesk.Max.IGameObject.ObjectTypes.Camera);
  92. fillObjectsList(cameras, "setCamerasNames");
  93. fillSoundsList(meshes, "setSoundsNames");
  94. // Need to subclass this, then allow 3ds Max usage
  95. //Win32.SubClass(this.ActionsBuilderWebView.Handle);
  96. }
  97. private void butOK_Click(object sender, EventArgs e)
  98. {
  99. _document.InvokeScript("updateJSONFromGraph");
  100. _jsonResult = _document.GetElementById("ActionsBuilderJSON").GetAttribute("value");
  101. setProperty();
  102. _babylonActionsBuilderAction.Close();
  103. }
  104. private void ActionsBuilderForm_Activated(object sender, EventArgs e)
  105. {
  106. Loader.Global.DisableAccelerators();
  107. }
  108. private void ActionsBuilderForm_Deactivate(object sender, EventArgs e)
  109. {
  110. Loader.Global.EnableAccelerators();
  111. }
  112. private void setProperty()
  113. {
  114. if (_node != null)
  115. Tools.SetStringProperty(_node, _propertyName, _jsonResult);
  116. }
  117. private bool getProperty()
  118. {
  119. if (_node != null)
  120. _jsonResult = Tools.GetStringProperty(_node, _propertyName, _jsonResult);
  121. else
  122. return false;
  123. return true;
  124. }
  125. private void butCancel_Click(object sender, EventArgs e)
  126. {
  127. _babylonActionsBuilderAction.Close();
  128. }
  129. }
  130. }