ActionsBuilderForm.cs 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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. 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. // Update version (no buttons for 3ds Max)
  74. _document.InvokeScript("hideButtons");
  75. // Set object name
  76. _document.GetElementById("ActionsBuilderObjectName").SetAttribute("value", _objectName);
  77. _document.InvokeScript("updateObjectName");
  78. if (isRootNode)
  79. _document.InvokeScript("setIsScene");
  80. else
  81. _document.InvokeScript("setIsObject");
  82. //_document.InvokeScript("updateObjectName");
  83. if (getProperty())
  84. {
  85. _document.GetElementById("ActionsBuilderJSON").SetAttribute("value", _jsonResult);
  86. _document.InvokeScript("loadFromJSON");
  87. }
  88. // Set lists of meshes, lights, cameras etc.
  89. var gameScene = Loader.Global.IGameInterface;
  90. gameScene.InitialiseIGame(false);
  91. var meshes = gameScene.GetIGameNodeByType(Autodesk.Max.IGameObject.ObjectTypes.Mesh);
  92. fillObjectsList(meshes, "setMeshesNames");
  93. var lights = gameScene.GetIGameNodeByType(Autodesk.Max.IGameObject.ObjectTypes.Light);
  94. fillObjectsList(lights, "setLightsNames");
  95. var cameras = gameScene.GetIGameNodeByType(Autodesk.Max.IGameObject.ObjectTypes.Camera);
  96. fillObjectsList(cameras, "setCamerasNames");
  97. fillSoundsList(meshes, "setSoundsNames");
  98. // Finish
  99. _document.InvokeScript("resetList");
  100. // Need to subclass this, then allow 3ds Max usage
  101. //Win32.SubClass(this.ActionsBuilderWebView.Handle);
  102. }
  103. private void butOK_Click(object sender, EventArgs e)
  104. {
  105. _document.InvokeScript("createJSON");
  106. _jsonResult = _document.GetElementById("ActionsBuilderJSON").GetAttribute("value");
  107. setProperty();
  108. _babylonActionsBuilderAction.Close();
  109. }
  110. private void ActionsBuilderForm_Activated(object sender, EventArgs e)
  111. {
  112. Loader.Global.DisableAccelerators();
  113. }
  114. private void ActionsBuilderForm_Deactivate(object sender, EventArgs e)
  115. {
  116. Loader.Global.EnableAccelerators();
  117. }
  118. private void setProperty()
  119. {
  120. if (_node != null)
  121. Tools.SetStringProperty(_node, _propertyName, _jsonResult);
  122. }
  123. private bool getProperty()
  124. {
  125. if (_node != null)
  126. _jsonResult = Tools.GetStringProperty(_node, _propertyName, _jsonResult);
  127. else
  128. return false;
  129. return true;
  130. }
  131. private void butCancel_Click(object sender, EventArgs e)
  132. {
  133. _babylonActionsBuilderAction.Close();
  134. }
  135. }
  136. }