ActionsBuilderForm.cs 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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 (web view)
  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. #if MAX2017
  51. var indexer = i;
  52. #else
  53. var indexer = new IntPtr(i);
  54. #endif
  55. var node = list[indexer];
  56. names[i] = node.MaxNode.Name;
  57. }
  58. _document.InvokeScript(scriptName, names);
  59. }
  60. private void fillSoundsList(ITab<IIGameNode> list, string scriptName)
  61. {
  62. object[] names = new object[list.Count];
  63. for (int i = 0; i < list.Count; i++)
  64. {
  65. #if MAX2017
  66. var indexer = i;
  67. #else
  68. var indexer = new IntPtr(i);
  69. #endif
  70. var node = list[indexer].MaxNode;
  71. string soundFile = "";
  72. soundFile = Tools.GetStringProperty(node, "babylonjs_sound_filename", soundFile);
  73. names[i] = Path.GetFileName(soundFile);
  74. }
  75. _document.InvokeScript(scriptName, names);
  76. }
  77. private void ActionsBuilderWebView_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
  78. {
  79. // Set common properties (name, is scene or object, etc.)
  80. _document = ActionsBuilderWebView.Document;
  81. // Update version (no buttons for 3ds Max)
  82. _document.InvokeScript("hideButtons");
  83. // Set object name
  84. _document.GetElementById("ActionsBuilderObjectName").SetAttribute("value", _objectName);
  85. _document.InvokeScript("updateObjectName");
  86. if (isRootNode)
  87. _document.InvokeScript("setIsScene");
  88. else
  89. _document.InvokeScript("setIsObject");
  90. //_document.InvokeScript("updateObjectName");
  91. if (getProperty())
  92. {
  93. _document.GetElementById("ActionsBuilderJSON").SetAttribute("value", _jsonResult);
  94. _document.InvokeScript("loadFromJSON");
  95. }
  96. // Set lists of meshes, lights, cameras etc.
  97. var gameScene = Loader.Global.IGameInterface;
  98. gameScene.InitialiseIGame(false);
  99. var meshes = gameScene.GetIGameNodeByType(Autodesk.Max.IGameObject.ObjectTypes.Mesh);
  100. fillObjectsList(meshes, "setMeshesNames");
  101. var lights = gameScene.GetIGameNodeByType(Autodesk.Max.IGameObject.ObjectTypes.Light);
  102. fillObjectsList(lights, "setLightsNames");
  103. var cameras = gameScene.GetIGameNodeByType(Autodesk.Max.IGameObject.ObjectTypes.Camera);
  104. fillObjectsList(cameras, "setCamerasNames");
  105. fillSoundsList(meshes, "setSoundsNames");
  106. // Finish
  107. _document.InvokeScript("resetList");
  108. // Need to subclass this, then allow 3ds Max usage
  109. //Win32.SubClass(this.ActionsBuilderWebView.Handle);
  110. }
  111. private void butOK_Click(object sender, EventArgs e)
  112. {
  113. _document.InvokeScript("createJSON");
  114. _jsonResult = _document.GetElementById("ActionsBuilderJSON").GetAttribute("value");
  115. setProperty();
  116. _babylonActionsBuilderAction.Close();
  117. }
  118. private void ActionsBuilderForm_Activated(object sender, EventArgs e)
  119. {
  120. Loader.Global.DisableAccelerators();
  121. }
  122. private void ActionsBuilderForm_Deactivate(object sender, EventArgs e)
  123. {
  124. Loader.Global.EnableAccelerators();
  125. }
  126. private void setProperty()
  127. {
  128. if (_node != null)
  129. Tools.SetStringProperty(_node, _propertyName, _jsonResult);
  130. }
  131. private bool getProperty()
  132. {
  133. if (_node != null)
  134. _jsonResult = Tools.GetStringProperty(_node, _propertyName, _jsonResult);
  135. else
  136. return false;
  137. return true;
  138. }
  139. private void butCancel_Click(object sender, EventArgs e)
  140. {
  141. _babylonActionsBuilderAction.Close();
  142. }
  143. }
  144. }