actionsbuilder.parameters.ts 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551
  1. module ActionsBuilder {
  2. export class Parameters {
  3. public parametersContainer: HTMLElement;
  4. public parametersHelpElement: HTMLElement;
  5. private _action: Action = null;
  6. private _viewer: Viewer;
  7. /*
  8. * Constructor
  9. */
  10. constructor(viewer: Viewer) {
  11. // Get HTML elements
  12. this.parametersContainer = document.getElementById("ParametersElementID");
  13. this.parametersHelpElement = document.getElementById("ParametersHelpElementID");
  14. // Configure this
  15. this._viewer = viewer;
  16. // Configure events
  17. window.addEventListener("resize", (event: Event) => {
  18. this.onResize(event);
  19. });
  20. }
  21. /*
  22. * Clears the parameters fileds in the parameters view
  23. */
  24. public clearParameters(): void {
  25. if (this.parametersContainer.children === null) {
  26. return;
  27. }
  28. while (this.parametersContainer.children.length > 0) {
  29. this.parametersContainer.removeChild(this.parametersContainer.firstChild);
  30. }
  31. }
  32. /*
  33. * Creates parameters fields
  34. * @param action: the action to configure
  35. */
  36. public createParameters(action: Action): void {
  37. // Clear parameters fields and draw help description
  38. this._action = action;
  39. this.clearParameters();
  40. if (action === null) {
  41. return;
  42. }
  43. this._createHelpSection(action);
  44. this._createNodeSection(action);
  45. // Get properties
  46. var properties = action.properties;
  47. var propertiesResults = action.propertiesResults;
  48. var targetParameterSelect: HTMLSelectElement = null;
  49. var targetParameterNameSelect: HTMLSelectElement = null;
  50. var propertyPathSelect: HTMLSelectElement = null;
  51. var propertyPathOptionalSelect: HTMLSelectElement = null;
  52. if (properties.length === 0) {
  53. return;
  54. }
  55. // Draw properties
  56. for (var i = 0; i < properties.length; i++) {
  57. // Create separator
  58. var separator = document.createElement("hr");
  59. separator.noShade = true;
  60. separator.className = "ParametersElementSeparatorClass";
  61. this.parametersContainer.appendChild(separator);
  62. // Create parameter text
  63. var parameterName = document.createElement("a");
  64. parameterName.text = properties[i].text;
  65. parameterName.className = "ParametersElementTitleClass";
  66. this.parametersContainer.appendChild(parameterName);
  67. if (properties[i].text === "parameter" || properties[i].text === "target" || properties[i].text === "parent") {
  68. // Create target select element
  69. targetParameterSelect = document.createElement("select");
  70. targetParameterSelect.className = "ParametersElementSelectClass";
  71. this.parametersContainer.appendChild(targetParameterSelect);
  72. // Create target name select element
  73. targetParameterNameSelect = document.createElement("select");
  74. targetParameterNameSelect.className = "ParametersElementSelectClass";
  75. this.parametersContainer.appendChild(targetParameterNameSelect);
  76. // Events and configure
  77. (this._parameterTargetChanged(targetParameterSelect, targetParameterNameSelect, propertyPathSelect, propertyPathOptionalSelect, i))(null);
  78. targetParameterSelect.value = propertiesResults[i].targetType;
  79. targetParameterNameSelect.value = propertiesResults[i].value;
  80. targetParameterSelect.onchange = this._parameterTargetChanged(targetParameterSelect, targetParameterNameSelect, propertyPathSelect, propertyPathOptionalSelect, i);
  81. targetParameterNameSelect.onchange = this._parameterTargetNameChanged(targetParameterSelect, targetParameterNameSelect, i);
  82. }
  83. else if (properties[i].text === "propertyPath") {
  84. // Create property path select
  85. propertyPathSelect = document.createElement("select");
  86. propertyPathSelect.className = "ParametersElementSelectClass";
  87. this.parametersContainer.appendChild(propertyPathSelect);
  88. // Create additional select
  89. propertyPathOptionalSelect = document.createElement("select");
  90. propertyPathOptionalSelect.className = "ParametersElementSelectClass";
  91. this.parametersContainer.appendChild(propertyPathOptionalSelect);
  92. // Events and configure
  93. (this._propertyPathSelectChanged(targetParameterSelect, propertyPathSelect, propertyPathOptionalSelect, i))(null);
  94. var property = this._action.propertiesResults[i].value.split(".");
  95. if (property.length > 0) {
  96. if (property.length === 1) {
  97. propertyPathSelect.value = property[0];
  98. }
  99. else {
  100. var completePropertyPath = "";
  101. for (var j = 0; j < property.length - 1; j++) {
  102. completePropertyPath += property[j];
  103. completePropertyPath += (j === property.length - 2) ? "" : ".";
  104. }
  105. propertyPathSelect.value = completePropertyPath;
  106. this._viewer.utils.setElementVisible(propertyPathOptionalSelect, true);
  107. }
  108. this._fillAdditionalPropertyPath(targetParameterSelect, propertyPathSelect, propertyPathOptionalSelect);
  109. propertyPathOptionalSelect.value = property[property.length - 1];
  110. if (propertyPathOptionalSelect.options.length === 0 || propertyPathOptionalSelect.options[0].text === "") {
  111. this._viewer.utils.setElementVisible(propertyPathOptionalSelect, false);
  112. }
  113. }
  114. targetParameterSelect.onchange = this._parameterTargetChanged(targetParameterSelect, targetParameterNameSelect, propertyPathSelect, propertyPathOptionalSelect, i - 1);
  115. propertyPathSelect.onchange = this._propertyPathSelectChanged(targetParameterSelect, propertyPathSelect, propertyPathOptionalSelect, i);
  116. propertyPathOptionalSelect.onchange = this._additionalPropertyPathSelectChanged(propertyPathSelect, propertyPathOptionalSelect, i);
  117. }
  118. else if (properties[i].text === "operator") {
  119. var conditionOperatorSelect = document.createElement("select");
  120. conditionOperatorSelect.className = "ParametersElementSelectClass";
  121. this.parametersContainer.appendChild(conditionOperatorSelect);
  122. // Configure event
  123. (this._conditionOperatorSelectChanged(conditionOperatorSelect, i))(null);
  124. conditionOperatorSelect.value = propertiesResults[i].value;
  125. conditionOperatorSelect.onchange = this._conditionOperatorSelectChanged(conditionOperatorSelect, i);
  126. }
  127. else if (properties[i].text === "sound") {
  128. var soundSelect = document.createElement("select");
  129. soundSelect.className = "ParametersElementSelectClass";
  130. this.parametersContainer.appendChild(soundSelect);
  131. // Configure event
  132. (this._soundSelectChanged(soundSelect, i))(null);
  133. soundSelect.value = propertiesResults[i].value;
  134. soundSelect.onchange = this._soundSelectChanged(soundSelect, i);
  135. }
  136. else {
  137. if (propertiesResults[i].value === "true" || propertiesResults[i].value === "false") {
  138. var booleanSelect = document.createElement("select");
  139. booleanSelect.className = "ParametersElementSelectClass";
  140. this.parametersContainer.appendChild(booleanSelect);
  141. // Configure event
  142. (this._booleanSelectChanged(booleanSelect, i))(null);
  143. booleanSelect.value = propertiesResults[i].value;
  144. booleanSelect.onchange = this._booleanSelectChanged(booleanSelect, i);
  145. }
  146. else {
  147. var propertyInput = document.createElement("input");
  148. propertyInput.value = propertiesResults[i].value;
  149. propertyInput.className = "ParametersElementInputClass";
  150. this.parametersContainer.appendChild(propertyInput);
  151. // Configure event
  152. propertyInput.onkeyup = this._propertyInputChanged(propertyInput, i);
  153. }
  154. }
  155. }
  156. }
  157. /*
  158. * Resizes the parameters view
  159. * @param: the resize event
  160. */
  161. public onResize(event?: Event): void {
  162. var tools = document.getElementById("ToolsButtonsID");
  163. this.parametersContainer.style.height = window.innerHeight - tools.getBoundingClientRect().height - 25 - 200 + "px";
  164. this.parametersHelpElement.style.height = 200 + "px";
  165. }
  166. /*
  167. * Returns the boolean select change event
  168. * @param booleanSelect: the boolean select element
  169. * @param indice: the properties result indice
  170. */
  171. private _booleanSelectChanged(booleanSelect: HTMLSelectElement, indice: number): (ev: Event) => void {
  172. return (ev: Event) => {
  173. if (booleanSelect.options.length === 0) {
  174. var values = ["true", "false"];
  175. for (var i = 0; i < values.length; i++) {
  176. var option = document.createElement("option");
  177. option.value = option.text = values[i];
  178. booleanSelect.options.add(option);
  179. }
  180. }
  181. else {
  182. this._action.propertiesResults[indice].value = booleanSelect.value;
  183. }
  184. };
  185. }
  186. /*
  187. * Returns the sound select change event
  188. * @param soundSelect: the sound select element
  189. * @param indice: the properties result indice
  190. */
  191. private _soundSelectChanged(soundSelect: HTMLSelectElement, indice: number): (ev: Event) => void {
  192. return (ev: Event) => {
  193. if (soundSelect.options.length === 0) {
  194. for (var i = 0; i < SceneElements.SOUNDS.length; i++) {
  195. var option = document.createElement("option");
  196. option.value = option.text = SceneElements.SOUNDS[i];
  197. soundSelect.options.add(option);
  198. }
  199. this._sortList(soundSelect);
  200. }
  201. else {
  202. this._action.propertiesResults[indice].value = soundSelect.value;
  203. }
  204. };
  205. }
  206. /*
  207. * Returns the condition opeator select changed event
  208. * @param conditionOperatorSelect: the condition operator select element
  209. * @param indice: the properties result indice
  210. */
  211. private _conditionOperatorSelectChanged(conditionOperatorSelect: HTMLSelectElement, indice: number): (ev: Event) => void {
  212. return (ev: Event) => {
  213. if (conditionOperatorSelect.options.length === 0) {
  214. for (var i = 0; i < SceneElements.OPERATORS.length; i++) {
  215. var option = document.createElement("option");
  216. option.value = option.text = SceneElements.OPERATORS[i];
  217. conditionOperatorSelect.options.add(option);
  218. }
  219. }
  220. else {
  221. this._action.propertiesResults[indice].value = conditionOperatorSelect.value;
  222. }
  223. };
  224. }
  225. /*
  226. * Returns the property input changed event
  227. * @param propertyInput: the property input
  228. * @param indice: the properties result indice
  229. */
  230. private _propertyInputChanged(propertyInput: HTMLInputElement, indice: number): (ev: Event) => void {
  231. return (ev: Event) => {
  232. this._action.propertiesResults[indice].value = propertyInput.value;
  233. };
  234. }
  235. /*
  236. * Returns the propertyPath select changed event
  237. * @param targetParameterSelect: the target/parameter select element
  238. * @param propertyPathSelect: the propertyPath select element
  239. * @param additionalPropertyPathSelect: the additional propertyPath select element
  240. * @param indice: the properties indice in action.properties
  241. */
  242. private _propertyPathSelectChanged(targetParameterSelect: HTMLSelectElement, propertyPathSelect: HTMLSelectElement,
  243. additionalPropertyPathSelect: HTMLSelectElement, indice: number): (event: Event) => void
  244. {
  245. return (event: Event) => {
  246. if (propertyPathSelect.options.length === 0) {
  247. // Configure start values
  248. var properties = this._getPropertiesFromType(targetParameterSelect.value);
  249. if (properties !== null) {
  250. for (var i = 0; i < properties.length; i++) {
  251. var option = document.createElement("option");
  252. option.value = option.text = properties[i];
  253. propertyPathSelect.options.add(option);
  254. }
  255. }
  256. } else {
  257. // Set property
  258. this._action.propertiesResults[indice].value = propertyPathSelect.value;
  259. }
  260. // Configure addition property
  261. this._fillAdditionalPropertyPath(targetParameterSelect, propertyPathSelect, additionalPropertyPathSelect);
  262. // Sort
  263. this._sortList(propertyPathSelect);
  264. };
  265. }
  266. private _fillAdditionalPropertyPath(targetParameterSelect: HTMLSelectElement, propertyPathSelect: HTMLSelectElement,
  267. additionalPropertyPathSelect: HTMLSelectElement): void
  268. {
  269. additionalPropertyPathSelect.options.length = 0;
  270. var object = this._getObjectFromType(targetParameterSelect.value);
  271. if (object !== null) {
  272. var propertyPath = propertyPathSelect.value.split(".");
  273. for (var i = 0; i < propertyPath.length; i++) {
  274. object = object[propertyPath[i]];
  275. }
  276. }
  277. if (object === null || object === undefined || (typeof (object)).toLowerCase() === "string") {
  278. this._viewer.utils.setElementVisible(additionalPropertyPathSelect, false);
  279. return;
  280. }
  281. // Add options
  282. var emptyOption = document.createElement("option");
  283. emptyOption.value = emptyOption.text = "";
  284. additionalPropertyPathSelect.add(emptyOption);
  285. for (var thing in object) {
  286. var type = SceneElements.GetInstanceOf(object[thing]);
  287. var index = SceneElements.TYPES.indexOf(type);
  288. if (index !== -1) {
  289. var option = document.createElement("option");
  290. option.value = option.text = thing;
  291. additionalPropertyPathSelect.options.add(option);
  292. emptyOption.text += thing + ", ";
  293. }
  294. }
  295. if (additionalPropertyPathSelect.options.length === 0 || additionalPropertyPathSelect.options[0].text === "") {
  296. this._viewer.utils.setElementVisible(additionalPropertyPathSelect, false);
  297. }
  298. else {
  299. this._viewer.utils.setElementVisible(additionalPropertyPathSelect, true);
  300. }
  301. }
  302. /*
  303. * Returns the additional propertyPath select changed event
  304. * @param propertyPathSelect: the propertyPath select element
  305. * @param additionalPropertyPathSelect: the additional propertyPath select element
  306. * @param indice: the properties indice in action.properties
  307. */
  308. private _additionalPropertyPathSelectChanged(propertyPathSelect: HTMLSelectElement, additionalPropertyPathSelect: HTMLSelectElement,
  309. indice: number): (event: Event) => void
  310. {
  311. return (event: Event) => {
  312. var property = propertyPathSelect.value;
  313. var additionalProperty = additionalPropertyPathSelect.value;
  314. if (additionalProperty !== "") {
  315. property += ".";
  316. property += additionalPropertyPathSelect.value;
  317. }
  318. this._action.propertiesResults[indice].value = property;
  319. };
  320. }
  321. /*
  322. * Returns the parameter/target select changed event
  323. * @param targetParameterSelect: the target/parameter select element
  324. * @param targetParameterNameSelect: the target/parameter name select element
  325. * @param propertyPathSelect: the propertyPath select element
  326. * @param additionalPropertyPathSelect: the additional propertyPath select element
  327. * @param indice: the properties indice in action.properties
  328. */
  329. private _parameterTargetChanged(targetParameterSelect: HTMLSelectElement, targetParameterNameSelect: HTMLSelectElement,
  330. propertyPathSelect: HTMLSelectElement, additionalPropertyPathSelect: HTMLSelectElement, indice: number): (event: Event) => void
  331. {
  332. return (event: Event) => {
  333. if (targetParameterSelect.options.length === 0) {
  334. // Configure start values
  335. var options = [
  336. { text: "Mesh", targetType: "MeshProperties" },
  337. { text: "Light", targetType: "LightProperties" },
  338. { text: "Camera", targetType: "CameraProperties" },
  339. { text: "Scene", targetType: "SceneProperties" }
  340. ];
  341. targetParameterSelect.options.length = 0;
  342. for (var i = 0; i < options.length; i++) {
  343. var option = document.createElement("option");
  344. option.text = options[i].text;
  345. option.value = options[i].targetType;
  346. targetParameterSelect.options.add(option);
  347. }
  348. } else {
  349. this._action.propertiesResults[indice].targetType = targetParameterSelect.value;
  350. this._action.propertiesResults[indice].value = "";
  351. if (propertyPathSelect !== null) {
  352. this._action.propertiesResults[indice + 1].value = ""; // propertyPath
  353. }
  354. }
  355. // Configure target names
  356. var targetParameterProperties = this._getTargetFromType(targetParameterSelect.value);
  357. targetParameterNameSelect.options.length = 0;
  358. if (targetParameterProperties !== null) {
  359. for (var i = 0; i < targetParameterProperties.length; i++) {
  360. var option = document.createElement("option");
  361. option.text = option.value = targetParameterProperties[i];
  362. targetParameterNameSelect.options.add(option);
  363. }
  364. }
  365. // Clear property path
  366. if (propertyPathSelect !== null) {
  367. propertyPathSelect.options.length = 0;
  368. additionalPropertyPathSelect.options.length = 0;
  369. this._propertyPathSelectChanged(targetParameterSelect, propertyPathSelect, additionalPropertyPathSelect, indice + 1)(null);
  370. }
  371. this._sortList(targetParameterNameSelect);
  372. this._sortList(targetParameterSelect);
  373. };
  374. }
  375. /*
  376. * Returns the parameter/target name select changed
  377. * @param indice: the properties indice to change
  378. */
  379. private _parameterTargetNameChanged(targetParameterSelect: HTMLSelectElement, targetParameterNameSelect: HTMLSelectElement, indice: number): (event: Event) => void {
  380. return (event: Event) => {
  381. this._action.propertiesResults[indice].value = targetParameterNameSelect.value;
  382. };
  383. }
  384. /*
  385. * Returns the array of objects names in function of its type
  386. * @param type: the target type
  387. */
  388. public _getTargetFromType(type: string): Array<string> {
  389. if (type === "MeshProperties" || type === "Mesh") {
  390. return SceneElements.MESHES;
  391. }
  392. if (type === "LightProperties" || type === "Light") {
  393. return SceneElements.LIGHTS;
  394. }
  395. if (type === "CameraProperties" || type === "Camera") {
  396. return SceneElements.CAMERAS;
  397. }
  398. return null;
  399. }
  400. /*
  401. * Returns the properties in function of its type
  402. * @param type: the target type
  403. */
  404. private _getPropertiesFromType(type: string): Array<string> {
  405. if (type === "MeshProperties" || type === "Mesh") {
  406. return SceneElements.MESH_PROPERTIES;
  407. }
  408. if (type === "LightProperties" || type === "Light") {
  409. return SceneElements.LIGHT_PROPERTIES;
  410. }
  411. if (type === "CameraProperties" || type === "Camera") {
  412. return SceneElements.CAMERA_PROPERTIES;
  413. }
  414. if (type === "SceneProperties" || type === "Scene") {
  415. return SceneElements.SCENE_PROPERTIES;
  416. }
  417. return null;
  418. }
  419. /*
  420. * Returns the object in function of the given type
  421. * @param type: the target type
  422. */
  423. public _getObjectFromType(type: string): any {
  424. if (type === "MeshProperties" || type === "Mesh") {
  425. return SceneElements.MESH;
  426. }
  427. if (type === "LightProperties" || type === "Light") {
  428. return SceneElements.LIGHT;
  429. }
  430. if (type === "CameraProperties" || type === "Camera") {
  431. return SceneElements.CAMERA;
  432. }
  433. if (type === "SceneProperties" || type === "Scene") {
  434. return SceneElements.SCENE;
  435. }
  436. return null;
  437. }
  438. /*
  439. * Creates the node section (top of parameters)
  440. * @param action: the action element to get color, text, name etc.
  441. */
  442. private _createNodeSection(action: Action): void {
  443. var element = document.createElement("div");
  444. element.style.background = <string>this._viewer.getSelectedNodeColor(action.type, action.node.detached);
  445. element.className = "ParametersElementNodeClass";
  446. var text = document.createElement("a");
  447. text.text = action.name;
  448. text.className = "ParametersElementNodeTextClass";
  449. element.appendChild(text);
  450. this.parametersContainer.appendChild(element);
  451. }
  452. /*
  453. * Creates the help section
  454. * @param action : the action containing the description
  455. */
  456. private _createHelpSection(action: Action): void {
  457. // Get description
  458. var element = Elements.GetElementFromName(action.name);
  459. if (element !== null) {
  460. this.parametersHelpElement.textContent = element.description;
  461. }
  462. }
  463. /*
  464. * Alphabetically sorts a HTML select element options
  465. * @param element : the HTML select element to sort
  466. */
  467. private _sortList(element: HTMLSelectElement): void {
  468. var options = [];
  469. for (var i = element.options.length - 1; i >= 0; i--) {
  470. options.push(element.removeChild(element.options[i]));
  471. }
  472. options.sort((a, b) => {
  473. return a.innerHTML.localeCompare(b.innerHTML);
  474. });
  475. for (var i = 0; i < options.length; i++) {
  476. element.options.add(options[i]);
  477. }
  478. }
  479. }
  480. }