actionsbuilder.max.js 121 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623
  1. var ActionsBuilder;
  2. (function (ActionsBuilder) {
  3. var Node = (function () {
  4. function Node() {
  5. this.rect = null;
  6. this.text = null;
  7. this.line = null;
  8. this.detached = false;
  9. this.minimized = false;
  10. }
  11. /**
  12. * Returns if the point (x, y) is inside the text or rect
  13. * @param x: the x position of the point
  14. * @param y: the y position of the point
  15. */
  16. Node.prototype.isPointInside = function (x, y) {
  17. return this.rect.isPointInside(x, y) || this.text.isPointInside(x, y);
  18. };
  19. return Node;
  20. })();
  21. ActionsBuilder.Node = Node;
  22. var Action = (function () {
  23. /**
  24. * Constructor
  25. * @param node: The associated node to draw in the viewer
  26. */
  27. function Action(node) {
  28. this.parent = null;
  29. this.children = new Array();
  30. this.name = "";
  31. this.type = ActionsBuilder.Type.OBJECT;
  32. this.properties = new Array();
  33. this.propertiesResults = new Array();
  34. this.combineArray = null;
  35. this.hub = null;
  36. this.combineAction = null;
  37. this.node = node;
  38. }
  39. /*
  40. * Removes a combined action from the combine array
  41. * @param action: the action to remove
  42. */
  43. Action.prototype.removeCombinedAction = function (action) {
  44. if (action === null || this.combineArray === null) {
  45. return false;
  46. }
  47. var index = this.combineArray.indexOf(action);
  48. if (index !== -1) {
  49. this.combineArray.splice(index, 1);
  50. }
  51. return false;
  52. };
  53. /*
  54. * Adds a child
  55. * @param child: the action to add as child
  56. */
  57. Action.prototype.addChild = function (child) {
  58. if (child === null) {
  59. return false;
  60. }
  61. this.children.push(child);
  62. child.parent = this;
  63. return true;
  64. };
  65. /*
  66. * Removes the given action to children
  67. * @param child: the child to remove
  68. */
  69. Action.prototype.removeChild = function (child) {
  70. var indice = this.children.indexOf(child);
  71. if (indice !== -1) {
  72. this.children.splice(indice, 1);
  73. return true;
  74. }
  75. return false;
  76. };
  77. /*
  78. * Clears the children's array
  79. */
  80. Action.prototype.clearChildren = function () {
  81. this.children = new Array();
  82. };
  83. return Action;
  84. })();
  85. ActionsBuilder.Action = Action;
  86. })(ActionsBuilder || (ActionsBuilder = {}));
  87. var ActionsBuilder;
  88. (function (ActionsBuilder) {
  89. var ContextMenu = (function () {
  90. /*
  91. * Constructor
  92. * @param viewer: the graph viewer
  93. */
  94. function ContextMenu(viewer) {
  95. this.showing = false;
  96. this.savedColor = Raphael.rgb(255, 255, 255);
  97. this.overColor = Raphael.rgb(140, 200, 230);
  98. this._viewer = null;
  99. this.elements = [
  100. { text: "Reduce", node: null, action: "onReduce" },
  101. { text: "Delete", node: null, action: "onRemoveNode" },
  102. { text: "Delete branch", node: null, action: "onRemoveBranch" },
  103. { text: "Connect / Disconnect", node: null, action: "onDetachAction" },
  104. { text: "Copy", node: null, action: "onCopyStructure" },
  105. { text: "Paste", node: null, action: "onPasteStructure" },
  106. // Add other elements here
  107. { text: "", node: null, action: null } // Color separator (top)
  108. ];
  109. // Members
  110. this._viewer = viewer;
  111. // Configure
  112. this.attachControl(this._viewer.paper.canvas);
  113. }
  114. ContextMenu.prototype.attachControl = function (element) {
  115. var _this = this;
  116. var onClick = function (event) {
  117. var x = _this._viewer.mousex;
  118. var y = _this._viewer.mousey;
  119. // Remove all context menu nodes, and run action if selected
  120. if (_this.showing) {
  121. for (var i = 0; i < _this.elements.length; i++) {
  122. var element = _this.elements[i];
  123. if (element.action && element.node.rect.isPointInside(x, y)) {
  124. _this._viewer.utils[element.action]();
  125. _this._viewer.update();
  126. }
  127. element.node.rect.remove();
  128. element.node.text.remove();
  129. }
  130. }
  131. _this.showing = false;
  132. };
  133. var onMouseMove = function (event) {
  134. // Override context menu's node color if mouse is inside
  135. if (_this.showing) {
  136. for (var i = 0; i < _this.elements.length; i++) {
  137. var element = _this.elements[i];
  138. if (element.text === "")
  139. continue;
  140. var x = _this._viewer.mousex;
  141. var y = _this._viewer.mousey;
  142. if (element.node.rect.isPointInside(x, y)) {
  143. element.node.rect.attr("fill", _this.overColor);
  144. }
  145. else {
  146. element.node.rect.attr("fill", _this.savedColor);
  147. }
  148. }
  149. }
  150. };
  151. var onRightClick = function (event) {
  152. var x = _this._viewer.mousex;
  153. var y = _this._viewer.mousey;
  154. _this._viewer.onClick(event);
  155. // Set selected node
  156. var result = _this._viewer.traverseGraph(null, x, y, true);
  157. if (result.hit) {
  158. }
  159. // Properly draw the context menu on the screen
  160. if (y + (ActionsBuilder.Viewer.NODE_HEIGHT * _this.elements.length) > _this._viewer.viewerElement.offsetHeight + _this._viewer.viewerElement.scrollTop) {
  161. y = (ActionsBuilder.Viewer.NODE_HEIGHT * _this.elements.length);
  162. }
  163. if (x + ActionsBuilder.Viewer.NODE_WIDTH > _this._viewer.viewerElement.offsetWidth + _this._viewer.viewerElement.scrollLeft) {
  164. x -= ActionsBuilder.Viewer.NODE_WIDTH;
  165. }
  166. if (!_this.showing) {
  167. if (_this._viewer.selectedNode === null)
  168. return;
  169. // Create elements
  170. var yOffset = 10;
  171. for (var i = 0; i < _this.elements.length - 1; i++) {
  172. var element = _this.elements[i];
  173. element.node = _this._viewer._createNode(element.text, ActionsBuilder.Type.OBJECT, true);
  174. element.node.rect.attr("fill", Raphael.rgb(216, 216, 216));
  175. element.node.rect.attr("x", x);
  176. element.node.rect.attr("y", y + yOffset);
  177. element.node.text.attr("x", x + 5);
  178. element.node.text.attr("y", y + yOffset + element.node.rect.attr("height") / 2);
  179. yOffset += ActionsBuilder.Viewer.NODE_HEIGHT;
  180. }
  181. // Color separator
  182. var separator = _this.elements[_this.elements.length - 1];
  183. separator.node = _this._viewer._createNode("", ActionsBuilder.Type.OBJECT, true);
  184. separator.node.rect.attr("fill", _this._viewer.getNodeColor(_this._viewer.selectedNode.type, _this._viewer.selectedNode.node.detached));
  185. separator.node.rect.attr("x", x);
  186. separator.node.rect.attr("y", y);
  187. separator.node.rect.attr("height", 10);
  188. // Finish
  189. _this.showing = true;
  190. }
  191. else {
  192. onClick(event);
  193. onRightClick(event);
  194. }
  195. window.event.returnValue = false;
  196. };
  197. document.addEventListener("click", onClick);
  198. document.addEventListener("mousemove", onMouseMove);
  199. element.addEventListener("contextmenu", onRightClick);
  200. };
  201. return ContextMenu;
  202. })();
  203. ActionsBuilder.ContextMenu = ContextMenu;
  204. })(ActionsBuilder || (ActionsBuilder = {}));
  205. var ActionsBuilder;
  206. (function (ActionsBuilder) {
  207. var ListElement = (function () {
  208. function ListElement() {
  209. this.rect = null;
  210. this.text = null;
  211. this.name = "";
  212. this.type = ActionsBuilder.Type.TRIGGER;
  213. this.element = null;
  214. }
  215. return ListElement;
  216. })();
  217. ActionsBuilder.ListElement = ListElement;
  218. var List = (function () {
  219. /**
  220. * Constructor
  221. */
  222. function List(viewer) {
  223. var _this = this;
  224. this._listElements = new Array();
  225. // Get HTML elements
  226. this.listElement = document.getElementById("ListsElementID");
  227. this.triggersElement = document.getElementById("TriggersListID");
  228. this.actionsElement = document.getElementById("ActionsListID");
  229. this.flowControlsElement = document.getElementById("FlowActionsListID");
  230. this._parentContainer = document.getElementById("ParentContainerID");
  231. // Configure this
  232. this._viewer = viewer;
  233. // Create elements (lists)
  234. this.triggersList = Raphael("TriggersListID", (25 * screen.width) / 100, 400);
  235. this.actionsList = Raphael("ActionsListID", (25 * screen.width) / 100, 400);
  236. this.flowControlsList = Raphael("FlowActionsListID", (25 * screen.width) / 100, 400);
  237. // Manage events
  238. window.addEventListener("resize", function (event) {
  239. _this.onResize(event);
  240. });
  241. }
  242. Object.defineProperty(List, "ELEMENT_HEIGHT", {
  243. get: function () {
  244. return 25;
  245. },
  246. enumerable: true,
  247. configurable: true
  248. });
  249. /**
  250. * Resize event that resizes the list element dynamically
  251. * @param event: the resize event
  252. */
  253. List.prototype.onResize = function (event) {
  254. var tools = document.getElementById("ToolsButtonsID");
  255. this.listElement.style.height = window.innerHeight - tools.getBoundingClientRect().height - 25 + "px";
  256. var listElementWidth = this.listElement.getBoundingClientRect().width;
  257. for (var i = 0; i < this._listElements.length; i++) {
  258. var rect = this._listElements[i].rect;
  259. rect.attr("width", listElementWidth - 40);
  260. }
  261. this.triggersList.setSize(listElementWidth, this.triggersList.height);
  262. this.actionsList.setSize(listElementWidth, this.triggersList.height);
  263. this.flowControlsList.setSize(listElementWidth, this.triggersList.height);
  264. };
  265. List.prototype.createListsElements = function () {
  266. var excludedTriggers = [6, 9, 10];
  267. var yPosition = 10;
  268. var textColor = Raphael.rgb(61, 72, 76);
  269. var whiteColor = Raphael.rgb(255, 255, 255);
  270. var configureTitle = function (listElement, rectColor) {
  271. listElement.text.attr("x", 15);
  272. listElement.rect.attr("fill", rectColor);
  273. listElement.text.attr("font-family", "Sinkin Sans Medium");
  274. listElement.text.attr("font-size", "11");
  275. };
  276. // Create triggers
  277. var triggers = this._createListElement(this.triggersList, yPosition, "TRIGGERS", ActionsBuilder.Type.TRIGGER, whiteColor, false);
  278. yPosition += List.ELEMENT_HEIGHT;
  279. configureTitle(triggers, Raphael.rgb(41, 129, 255));
  280. for (var i = 0; i < ActionsBuilder.Elements.TRIGGERS.length; i++) {
  281. var element = ActionsBuilder.Elements.TRIGGERS[i];
  282. if (this._viewer.root.type === ActionsBuilder.Type.OBJECT && excludedTriggers.indexOf(i) !== -1) {
  283. continue;
  284. }
  285. else if (this._viewer.root.type === ActionsBuilder.Type.SCENE && excludedTriggers.indexOf(i) === -1) {
  286. continue;
  287. }
  288. var trigger = this._createListElement(this.triggersList, yPosition, element.text, ActionsBuilder.Type.TRIGGER, textColor, true, element);
  289. trigger.rect.attr("fill", Raphael.rgb(133, 154, 185));
  290. yPosition += List.ELEMENT_HEIGHT;
  291. }
  292. yPosition += List.ELEMENT_HEIGHT;
  293. this.triggersElement.style.height = this.triggersList.canvas.style.height = yPosition + "px";
  294. this._createCollapseAnimation(this.triggersList, this.triggersElement, triggers, yPosition);
  295. // Create actions
  296. yPosition = 10;
  297. var actions = this._createListElement(this.actionsList, yPosition, "ACTIONS", ActionsBuilder.Type.ACTION, textColor, false);
  298. yPosition += List.ELEMENT_HEIGHT;
  299. configureTitle(actions, Raphael.rgb(255, 220, 42));
  300. for (var i = 0; i < ActionsBuilder.Elements.ACTIONS.length; i++) {
  301. var element = ActionsBuilder.Elements.ACTIONS[i];
  302. var action = this._createListElement(this.actionsList, yPosition, element.text, ActionsBuilder.Type.ACTION, textColor, true, element);
  303. action.rect.attr("fill", Raphael.rgb(182, 185, 132));
  304. yPosition += List.ELEMENT_HEIGHT;
  305. }
  306. yPosition += List.ELEMENT_HEIGHT;
  307. this.actionsElement.style.height = this.actionsList.canvas.style.height = yPosition + "px";
  308. this._createCollapseAnimation(this.actionsList, this.actionsElement, actions, yPosition);
  309. // Create flow controls
  310. yPosition = 10;
  311. var flowControls = this._createListElement(this.flowControlsList, yPosition, "FLOW CONTROLS", ActionsBuilder.Type.FLOW_CONTROL, whiteColor, false);
  312. yPosition += List.ELEMENT_HEIGHT;
  313. configureTitle(flowControls, Raphael.rgb(255, 41, 53));
  314. for (var i = 0; i < ActionsBuilder.Elements.FLOW_CONTROLS.length - 1; i++) {
  315. var element = ActionsBuilder.Elements.FLOW_CONTROLS[i];
  316. var flowControl = this._createListElement(this.flowControlsList, yPosition, element.text, ActionsBuilder.Type.FLOW_CONTROL, textColor, true, element);
  317. flowControl.rect.attr("fill", Raphael.rgb(185, 132, 140));
  318. yPosition += List.ELEMENT_HEIGHT;
  319. }
  320. yPosition += List.ELEMENT_HEIGHT;
  321. this.flowControlsElement.style.height = this.flowControlsList.canvas.style.height = yPosition + "px";
  322. this._createCollapseAnimation(this.flowControlsList, this.flowControlsElement, flowControls, yPosition);
  323. };
  324. /**
  325. * Clears the list of elements and removes the elements
  326. */
  327. List.prototype.clearLists = function () {
  328. for (var i = 0; i < this._listElements.length; i++) {
  329. this._removeListElement(this._listElements[i]);
  330. }
  331. this._listElements.splice(0, this._listElements.length - 1);
  332. };
  333. /**
  334. * Sets the color theme of the lists
  335. * @param color: the theme color
  336. */
  337. List.prototype.setColorTheme = function (color) {
  338. this.triggersList.canvas.style.backgroundColor = color;
  339. this.actionsList.canvas.style.backgroundColor = color;
  340. this.flowControlsList.canvas.style.backgroundColor = color;
  341. };
  342. /**
  343. * Creates a list element
  344. * @param paper: the Raphael.js paper
  345. * @param yPosition: the y position of the element
  346. * @param text: the element text
  347. * @param type: the element type (trigger, action, flow control)
  348. * @param textColor: the text color
  349. * @param drag: if the element should be drag'n'dropped
  350. */
  351. List.prototype._createListElement = function (paper, yPosition, text, type, textColor, drag, element) {
  352. var object = new ListElement();
  353. object.rect = paper.rect(10, yPosition, 300, List.ELEMENT_HEIGHT);
  354. object.text = paper.text(30, yPosition + object.rect.attr("height") / 2, text);
  355. object.text.attr("fill", textColor);
  356. object.text.attr("text-anchor", "start");
  357. object.text.attr("font-size", "12");
  358. object.text.attr("text-anchor", "start");
  359. object.text.attr("font-family", "Sinkin Sans Light");
  360. if (drag) {
  361. this._createListElementAnimation(object);
  362. }
  363. object.type = type;
  364. object.element = element;
  365. this._listElements.push(object);
  366. return object;
  367. };
  368. /**
  369. * Removes a list element
  370. * @param element: the element to remove
  371. */
  372. List.prototype._removeListElement = function (element) {
  373. element.rect.remove();
  374. element.text.remove();
  375. };
  376. /*
  377. * Creates the collapse animation of a list
  378. * @param paper: the list paper
  379. * @param htmlElement: the list div container
  380. * @param element: the list element to click on
  381. * @param expandedHeight: the height when the list is expanded
  382. */
  383. List.prototype._createCollapseAnimation = function (paper, htmlElement, element, expandedHeight) {
  384. var onClick = function (event) {
  385. var height = htmlElement.style.height;
  386. if (height === expandedHeight + "px") {
  387. htmlElement.style.height = paper.canvas.style.height = 35 + "px";
  388. }
  389. else {
  390. htmlElement.style.height = paper.canvas.style.height = expandedHeight + "px";
  391. }
  392. };
  393. element.rect.click(onClick);
  394. };
  395. /*
  396. * Creates the animation of a list element
  397. * @param element: the list element to animate
  398. */
  399. List.prototype._createListElementAnimation = function (element) {
  400. var _this = this;
  401. var onMove = function (dx, dy, x, y) { };
  402. var onStart = function (x, y, event) {
  403. _this._parentContainer.style.cursor = "copy";
  404. element.rect.animate({
  405. x: -10,
  406. opacity: 0.25
  407. }, 500, ">");
  408. element.text.animate({
  409. x: 10,
  410. opacity: 0.25
  411. }, 500, ">");
  412. };
  413. var onEnd = function (event) {
  414. _this._parentContainer.style.cursor = "default";
  415. element.rect.animate({
  416. x: 10,
  417. opacity: 1.0
  418. }, 500, "<");
  419. element.text.animate({
  420. x: 30,
  421. opacity: 1.0
  422. }, 500, "<");
  423. var dragResult = _this._viewer.traverseGraph(null, _this._viewer.mousex, _this._viewer.mousey, false);
  424. if (dragResult.hit) {
  425. if (element.type === ActionsBuilder.Type.TRIGGER && dragResult.action !== _this._viewer.root) {
  426. alert("Triggers can be dragged only on the root node (the mesh)");
  427. return;
  428. }
  429. if (element.type === ActionsBuilder.Type.ACTION && dragResult.action === _this._viewer.root) {
  430. alert("Please add a trigger before.");
  431. return;
  432. }
  433. if (element.type === ActionsBuilder.Type.FLOW_CONTROL && (dragResult.action === _this._viewer.root || (dragResult.action.type === ActionsBuilder.Type.FLOW_CONTROL && dragResult.action.parent.hub === null))) {
  434. return;
  435. }
  436. if (element.type === ActionsBuilder.Type.FLOW_CONTROL && dragResult.action.combineArray !== null) {
  437. alert("A condition cannot be handled by a Combine Action.");
  438. return;
  439. }
  440. if ((element.type === ActionsBuilder.Type.FLOW_CONTROL || element.type === ActionsBuilder.Type.ACTION) && dragResult.action.type === ActionsBuilder.Type.TRIGGER && dragResult.action.children.length > 0) {
  441. alert("Triggers can have only one child. Please add another trigger of same type.");
  442. return;
  443. }
  444. if (!(dragResult.action.combineArray !== null) && dragResult.action.children.length > 0 && dragResult.action.type !== ActionsBuilder.Type.TRIGGER && dragResult.action !== _this._viewer.root) {
  445. alert("An action can have only one child.");
  446. return;
  447. }
  448. _this._viewer.addAction(dragResult.action, element.type, element.element);
  449. _this._viewer.update();
  450. }
  451. };
  452. element.rect.drag(onMove, onStart, onEnd);
  453. element.text.drag(onMove, onStart, onEnd);
  454. };
  455. return List;
  456. })();
  457. ActionsBuilder.List = List;
  458. })(ActionsBuilder || (ActionsBuilder = {}));
  459. /*
  460. Global functions called by the plugins (3ds Max, etc.)
  461. */
  462. // Elements
  463. var list = null;
  464. var viewer = null;
  465. var actionsBuilderJsonInput = document.getElementById("ActionsBuilderJSON");
  466. this.createJSON = function () {
  467. var structure = viewer.utils.createJSON(viewer.root);
  468. var asText = JSON.stringify(structure);
  469. actionsBuilderJsonInput.value = asText;
  470. console.log(asText);
  471. };
  472. this.loadFromJSON = function () {
  473. var json = actionsBuilderJsonInput.value;
  474. if (json !== "") {
  475. var structure = JSON.parse(json);
  476. viewer.utils.loadFromJSON(structure, null);
  477. }
  478. };
  479. this.updateObjectName = function () {
  480. var element = document.getElementById("ActionsBuilderObjectName");
  481. var name = element.value;
  482. viewer.objectName = name;
  483. if (viewer.root.type === ActionsBuilder.Type.OBJECT) {
  484. name += " - Mesh";
  485. }
  486. else {
  487. name += " - Scene";
  488. }
  489. viewer.root.node.text.attr("text", name);
  490. };
  491. this.resetList = function () {
  492. list.clearLists();
  493. list.createListsElements();
  494. };
  495. this.setMeshesNames = function () {
  496. var args = [];
  497. for (var _i = 0; _i < arguments.length; _i++) {
  498. args[_i - 0] = arguments[_i];
  499. }
  500. for (var i = 0; i < args.length; i++) {
  501. ActionsBuilder.SceneElements.MESHES.push(args[i]);
  502. }
  503. };
  504. this.setLightsNames = function () {
  505. var args = [];
  506. for (var _i = 0; _i < arguments.length; _i++) {
  507. args[_i - 0] = arguments[_i];
  508. }
  509. for (var i = 0; i < args.length; i++) {
  510. ActionsBuilder.SceneElements.LIGHTS.push(args[i]);
  511. }
  512. };
  513. this.setCamerasNames = function () {
  514. var args = [];
  515. for (var _i = 0; _i < arguments.length; _i++) {
  516. args[_i - 0] = arguments[_i];
  517. }
  518. for (var i = 0; i < args.length; i++) {
  519. ActionsBuilder.SceneElements.CAMERAS.push(args[i]);
  520. }
  521. };
  522. this.setSoundsNames = function () {
  523. var args = [];
  524. for (var _i = 0; _i < arguments.length; _i++) {
  525. args[_i - 0] = arguments[_i];
  526. }
  527. for (var i = 0; i < args.length; i++) {
  528. var sound = args[i];
  529. if (sound !== "" && ActionsBuilder.SceneElements.SOUNDS.indexOf(sound) === -1) {
  530. ActionsBuilder.SceneElements.SOUNDS.push(args[i]);
  531. }
  532. }
  533. };
  534. this.hideButtons = function () {
  535. // Empty
  536. };
  537. this.setIsObject = function () {
  538. viewer.root.type = ActionsBuilder.Type.OBJECT;
  539. };
  540. this.setIsScene = function () {
  541. viewer.root.type = ActionsBuilder.Type.SCENE;
  542. };
  543. this.run = function () {
  544. // Configure viewer
  545. viewer = new ActionsBuilder.Viewer(ActionsBuilder.Type.OBJECT);
  546. viewer.setColorTheme("-ms-linear-gradient(top, rgba(38, 38, 38,1) 0%, rgba(125, 126, 125, 1) 100%)");
  547. viewer.setColorTheme("linear-gradient(top, rgba(38, 38, 38,1) 0%, rgba(125, 126, 125, 1) 100%)");
  548. viewer.setColorTheme("-webkit-linear-gradient(top, rgba(38, 38, 38,1) 0%, rgba(125, 126, 125, 1) 100%)");
  549. viewer.setColorTheme("-o-linear-gradient(top, rgba(38, 38, 38,1) 0%, rgba(125, 126, 125, 1) 100%)");
  550. viewer.onResize();
  551. viewer.update();
  552. // Configure list
  553. list = new ActionsBuilder.List(viewer);
  554. list.setColorTheme("rgb(64, 64, 64)");
  555. list.createListsElements();
  556. list.onResize();
  557. };
  558. var ActionsBuilder;
  559. (function (ActionsBuilder) {
  560. var Parameters = (function () {
  561. /*
  562. * Constructor
  563. */
  564. function Parameters(viewer) {
  565. var _this = this;
  566. this._action = null;
  567. // Get HTML elements
  568. this.parametersContainer = document.getElementById("ParametersElementID");
  569. this.parametersHelpElement = document.getElementById("ParametersHelpElementID");
  570. // Configure this
  571. this._viewer = viewer;
  572. // Configure events
  573. window.addEventListener("resize", function (event) {
  574. _this.onResize(event);
  575. });
  576. }
  577. /*
  578. * Clears the parameters fileds in the parameters view
  579. */
  580. Parameters.prototype.clearParameters = function () {
  581. if (this.parametersContainer.children === null) {
  582. return;
  583. }
  584. while (this.parametersContainer.children.length > 0) {
  585. this.parametersContainer.removeChild(this.parametersContainer.firstChild);
  586. }
  587. };
  588. /*
  589. * Creates parameters fields
  590. * @param action: the action to configure
  591. */
  592. Parameters.prototype.createParameters = function (action) {
  593. // Clear parameters fields and draw help description
  594. this._action = action;
  595. this.clearParameters();
  596. if (action === null) {
  597. return;
  598. }
  599. this._createHelpSection(action);
  600. this._createNodeSection(action);
  601. // Get properties
  602. var properties = action.properties;
  603. var propertiesResults = action.propertiesResults;
  604. var targetParameterSelect = null;
  605. var targetParameterNameSelect = null;
  606. var propertyPathSelect = null;
  607. var propertyPathOptionalSelect = null;
  608. var booleanSelect = null;
  609. var propertyInput = null;
  610. var propertyPathIndice = 0;
  611. if (properties.length === 0) {
  612. return;
  613. }
  614. // Draw properties
  615. for (var i = 0; i < properties.length; i++) {
  616. // Create separator
  617. var separator = document.createElement("hr");
  618. separator.noShade = true;
  619. separator.className = "ParametersElementSeparatorClass";
  620. this.parametersContainer.appendChild(separator);
  621. // Create parameter text
  622. var parameterName = document.createElement("a");
  623. parameterName.text = properties[i].text;
  624. parameterName.className = "ParametersElementTitleClass";
  625. this.parametersContainer.appendChild(parameterName);
  626. if (properties[i].text === "parameter" || properties[i].text === "target" || properties[i].text === "parent") {
  627. // Create target select element
  628. targetParameterSelect = document.createElement("select");
  629. targetParameterSelect.className = "ParametersElementSelectClass";
  630. this.parametersContainer.appendChild(targetParameterSelect);
  631. // Create target name select element
  632. targetParameterNameSelect = document.createElement("select");
  633. targetParameterNameSelect.className = "ParametersElementSelectClass";
  634. this.parametersContainer.appendChild(targetParameterNameSelect);
  635. // Events and configure
  636. (this._parameterTargetChanged(targetParameterSelect, targetParameterNameSelect, propertyPathSelect, propertyPathOptionalSelect, i))(null);
  637. targetParameterSelect.value = propertiesResults[i].targetType;
  638. targetParameterNameSelect.value = propertiesResults[i].value;
  639. targetParameterSelect.onchange = this._parameterTargetChanged(targetParameterSelect, targetParameterNameSelect, propertyPathSelect, propertyPathOptionalSelect, i);
  640. targetParameterNameSelect.onchange = this._parameterTargetNameChanged(targetParameterSelect, targetParameterNameSelect, i);
  641. }
  642. else if (properties[i].text === "propertyPath") {
  643. propertyPathIndice = i;
  644. // Create property path select
  645. propertyPathSelect = document.createElement("select");
  646. propertyPathSelect.className = "ParametersElementSelectClass";
  647. this.parametersContainer.appendChild(propertyPathSelect);
  648. // Create additional select
  649. propertyPathOptionalSelect = document.createElement("select");
  650. propertyPathOptionalSelect.className = "ParametersElementSelectClass";
  651. this.parametersContainer.appendChild(propertyPathOptionalSelect);
  652. // Events and configure
  653. (this._propertyPathSelectChanged(targetParameterSelect, propertyPathSelect, propertyPathOptionalSelect, null, null, i))(null);
  654. var property = this._action.propertiesResults[i].value.split(".");
  655. if (property.length > 0) {
  656. if (property.length === 1) {
  657. propertyPathSelect.value = property[0];
  658. }
  659. else {
  660. var completePropertyPath = "";
  661. for (var j = 0; j < property.length - 1; j++) {
  662. completePropertyPath += property[j];
  663. completePropertyPath += (j === property.length - 2) ? "" : ".";
  664. }
  665. propertyPathSelect.value = completePropertyPath;
  666. this._viewer.utils.setElementVisible(propertyPathOptionalSelect, true);
  667. }
  668. this._fillAdditionalPropertyPath(targetParameterSelect, propertyPathSelect, propertyPathOptionalSelect);
  669. propertyPathOptionalSelect.value = property[property.length - 1];
  670. if (propertyPathOptionalSelect.options.length === 0 || propertyPathOptionalSelect.options[0].text === "") {
  671. this._viewer.utils.setElementVisible(propertyPathOptionalSelect, false);
  672. }
  673. }
  674. targetParameterSelect.onchange = this._parameterTargetChanged(targetParameterSelect, targetParameterNameSelect, propertyPathSelect, propertyPathOptionalSelect, i - 1);
  675. propertyPathSelect.onchange = this._propertyPathSelectChanged(targetParameterSelect, propertyPathSelect, propertyPathOptionalSelect, null, null, i);
  676. propertyPathOptionalSelect.onchange = this._additionalPropertyPathSelectChanged(propertyPathSelect, propertyPathOptionalSelect, i);
  677. }
  678. else if (properties[i].text === "operator") {
  679. var conditionOperatorSelect = document.createElement("select");
  680. conditionOperatorSelect.className = "ParametersElementSelectClass";
  681. this.parametersContainer.appendChild(conditionOperatorSelect);
  682. // Configure event
  683. (this._conditionOperatorSelectChanged(conditionOperatorSelect, i))(null);
  684. conditionOperatorSelect.value = propertiesResults[i].value;
  685. conditionOperatorSelect.onchange = this._conditionOperatorSelectChanged(conditionOperatorSelect, i);
  686. }
  687. else if (properties[i].text === "sound") {
  688. var soundSelect = document.createElement("select");
  689. soundSelect.className = "ParametersElementSelectClass";
  690. this.parametersContainer.appendChild(soundSelect);
  691. // Configure event
  692. (this._soundSelectChanged(soundSelect, i))(null);
  693. soundSelect.value = propertiesResults[i].value;
  694. soundSelect.onchange = this._soundSelectChanged(soundSelect, i);
  695. }
  696. else {
  697. var isBoolean = propertiesResults[i].value === "true" || propertiesResults[i].value === "false";
  698. var object = this._getObjectFromType(targetParameterSelect.value);
  699. if (object !== null) {
  700. var property = this._action.propertiesResults[i - 1].value.split(".");
  701. for (var j = 0; j < property.length && object !== undefined; j++) {
  702. object = object[property[j]];
  703. if (j === property.length - 1) {
  704. isBoolean = isBoolean || typeof object === "boolean";
  705. }
  706. }
  707. }
  708. booleanSelect = document.createElement("select");
  709. booleanSelect.className = "ParametersElementSelectClass";
  710. this.parametersContainer.appendChild(booleanSelect);
  711. // Configure event
  712. (this._booleanSelectChanged(booleanSelect, i))(null);
  713. booleanSelect.value = propertiesResults[i].value;
  714. booleanSelect.onchange = this._booleanSelectChanged(booleanSelect, i);
  715. propertyInput = document.createElement("input");
  716. propertyInput.value = propertiesResults[i].value;
  717. propertyInput.className = "ParametersElementInputClass";
  718. this.parametersContainer.appendChild(propertyInput);
  719. // Configure event
  720. propertyInput.onkeyup = this._propertyInputChanged(propertyInput, i);
  721. if (properties[i].text === "value") {
  722. propertyPathSelect.onchange = this._propertyPathSelectChanged(targetParameterSelect, propertyPathSelect, propertyPathOptionalSelect, booleanSelect, propertyInput, propertyPathIndice);
  723. }
  724. if (isBoolean) {
  725. this._viewer.utils.setElementVisible(booleanSelect, true);
  726. this._viewer.utils.setElementVisible(propertyInput, false);
  727. }
  728. else {
  729. this._viewer.utils.setElementVisible(booleanSelect, false);
  730. this._viewer.utils.setElementVisible(propertyInput, true);
  731. }
  732. }
  733. }
  734. };
  735. /*
  736. * Resizes the parameters view
  737. * @param: the resize event
  738. */
  739. Parameters.prototype.onResize = function (event) {
  740. var tools = document.getElementById("ToolsButtonsID");
  741. this.parametersContainer.style.height = window.innerHeight - tools.getBoundingClientRect().height - 25 - 200 + "px";
  742. this.parametersHelpElement.style.height = 200 + "px";
  743. };
  744. /*
  745. * Returns the boolean select change event
  746. * @param booleanSelect: the boolean select element
  747. * @param indice: the properties result indice
  748. */
  749. Parameters.prototype._booleanSelectChanged = function (booleanSelect, indice) {
  750. var _this = this;
  751. return function (ev) {
  752. if (booleanSelect.options.length === 0) {
  753. var values = ["true", "false"];
  754. for (var i = 0; i < values.length; i++) {
  755. var option = document.createElement("option");
  756. option.value = option.text = values[i];
  757. booleanSelect.options.add(option);
  758. }
  759. }
  760. else {
  761. _this._action.propertiesResults[indice].value = booleanSelect.value;
  762. }
  763. };
  764. };
  765. /*
  766. * Returns the sound select change event
  767. * @param soundSelect: the sound select element
  768. * @param indice: the properties result indice
  769. */
  770. Parameters.prototype._soundSelectChanged = function (soundSelect, indice) {
  771. var _this = this;
  772. return function (ev) {
  773. if (soundSelect.options.length === 0) {
  774. for (var i = 0; i < ActionsBuilder.SceneElements.SOUNDS.length; i++) {
  775. var option = document.createElement("option");
  776. option.value = option.text = ActionsBuilder.SceneElements.SOUNDS[i];
  777. soundSelect.options.add(option);
  778. }
  779. _this._sortList(soundSelect);
  780. }
  781. else {
  782. _this._action.propertiesResults[indice].value = soundSelect.value;
  783. }
  784. };
  785. };
  786. /*
  787. * Returns the condition opeator select changed event
  788. * @param conditionOperatorSelect: the condition operator select element
  789. * @param indice: the properties result indice
  790. */
  791. Parameters.prototype._conditionOperatorSelectChanged = function (conditionOperatorSelect, indice) {
  792. var _this = this;
  793. return function (ev) {
  794. if (conditionOperatorSelect.options.length === 0) {
  795. for (var i = 0; i < ActionsBuilder.SceneElements.OPERATORS.length; i++) {
  796. var option = document.createElement("option");
  797. option.value = option.text = ActionsBuilder.SceneElements.OPERATORS[i];
  798. conditionOperatorSelect.options.add(option);
  799. }
  800. }
  801. else {
  802. _this._action.propertiesResults[indice].value = conditionOperatorSelect.value;
  803. }
  804. };
  805. };
  806. /*
  807. * Returns the property input changed event
  808. * @param propertyInput: the property input
  809. * @param indice: the properties result indice
  810. */
  811. Parameters.prototype._propertyInputChanged = function (propertyInput, indice) {
  812. var _this = this;
  813. return function (ev) {
  814. _this._action.propertiesResults[indice].value = propertyInput.value;
  815. };
  816. };
  817. /*
  818. * Returns the propertyPath select changed event
  819. * @param targetParameterSelect: the target/parameter select element
  820. * @param propertyPathSelect: the propertyPath select element
  821. * @param additionalPropertyPathSelect: the additional propertyPath select element
  822. * @param indice: the properties indice in action.properties
  823. */
  824. Parameters.prototype._propertyPathSelectChanged = function (targetParameterSelect, propertyPathSelect, additionalPropertyPathSelect, booleanSelect, propertyInput, indice) {
  825. var _this = this;
  826. return function (event) {
  827. if (propertyPathSelect.options.length === 0) {
  828. // Configure start values
  829. var properties = _this._getPropertiesFromType(targetParameterSelect.value);
  830. if (properties !== null) {
  831. for (var i = 0; i < properties.length; i++) {
  832. var option = document.createElement("option");
  833. option.value = option.text = properties[i];
  834. propertyPathSelect.options.add(option);
  835. }
  836. }
  837. }
  838. else {
  839. // Set property
  840. _this._action.propertiesResults[indice].value = propertyPathSelect.value;
  841. if (booleanSelect !== null && propertyInput !== null) {
  842. var object = _this._getObjectFromType(targetParameterSelect.value);
  843. var isBoolean = false;
  844. if (object !== null) {
  845. var property = _this._action.propertiesResults[indice].value.split(".");
  846. for (var j = 0; j < property.length; j++) {
  847. object = object[property[j]];
  848. if (j === property.length - 1) {
  849. isBoolean = isBoolean || typeof object === "boolean";
  850. }
  851. }
  852. }
  853. if (isBoolean) {
  854. _this._viewer.utils.setElementVisible(booleanSelect, true);
  855. _this._viewer.utils.setElementVisible(propertyInput, false);
  856. }
  857. else {
  858. _this._viewer.utils.setElementVisible(booleanSelect, false);
  859. _this._viewer.utils.setElementVisible(propertyInput, true);
  860. }
  861. }
  862. }
  863. // Configure addition property
  864. _this._fillAdditionalPropertyPath(targetParameterSelect, propertyPathSelect, additionalPropertyPathSelect);
  865. // Sort
  866. _this._sortList(propertyPathSelect);
  867. };
  868. };
  869. Parameters.prototype._fillAdditionalPropertyPath = function (targetParameterSelect, propertyPathSelect, additionalPropertyPathSelect) {
  870. additionalPropertyPathSelect.options.length = 0;
  871. var object = this._getObjectFromType(targetParameterSelect.value);
  872. if (object !== null) {
  873. var propertyPath = propertyPathSelect.value.split(".");
  874. for (var i = 0; i < propertyPath.length; i++) {
  875. object = object[propertyPath[i]];
  876. }
  877. }
  878. if (object === null || object === undefined || (typeof (object)).toLowerCase() === "string") {
  879. this._viewer.utils.setElementVisible(additionalPropertyPathSelect, false);
  880. return;
  881. }
  882. // Add options
  883. var emptyOption = document.createElement("option");
  884. emptyOption.value = emptyOption.text = "";
  885. additionalPropertyPathSelect.add(emptyOption);
  886. for (var thing in object) {
  887. var type = ActionsBuilder.SceneElements.GetInstanceOf(object[thing]);
  888. var index = ActionsBuilder.SceneElements.TYPES.indexOf(type);
  889. if (index !== -1) {
  890. var option = document.createElement("option");
  891. option.value = option.text = thing;
  892. additionalPropertyPathSelect.options.add(option);
  893. emptyOption.text += thing + ", ";
  894. }
  895. }
  896. if (additionalPropertyPathSelect.options.length === 0 || additionalPropertyPathSelect.options[0].text === "") {
  897. this._viewer.utils.setElementVisible(additionalPropertyPathSelect, false);
  898. }
  899. else {
  900. this._viewer.utils.setElementVisible(additionalPropertyPathSelect, true);
  901. }
  902. };
  903. /*
  904. * Returns the additional propertyPath select changed event
  905. * @param propertyPathSelect: the propertyPath select element
  906. * @param additionalPropertyPathSelect: the additional propertyPath select element
  907. * @param indice: the properties indice in action.properties
  908. */
  909. Parameters.prototype._additionalPropertyPathSelectChanged = function (propertyPathSelect, additionalPropertyPathSelect, indice) {
  910. var _this = this;
  911. return function (event) {
  912. var property = propertyPathSelect.value;
  913. var additionalProperty = additionalPropertyPathSelect.value;
  914. if (additionalProperty !== "") {
  915. property += ".";
  916. property += additionalPropertyPathSelect.value;
  917. }
  918. _this._action.propertiesResults[indice].value = property;
  919. };
  920. };
  921. /*
  922. * Returns the parameter/target select changed event
  923. * @param targetParameterSelect: the target/parameter select element
  924. * @param targetParameterNameSelect: the target/parameter name select element
  925. * @param propertyPathSelect: the propertyPath select element
  926. * @param additionalPropertyPathSelect: the additional propertyPath select element
  927. * @param indice: the properties indice in action.properties
  928. */
  929. Parameters.prototype._parameterTargetChanged = function (targetParameterSelect, targetParameterNameSelect, propertyPathSelect, additionalPropertyPathSelect, indice) {
  930. var _this = this;
  931. return function (event) {
  932. if (targetParameterSelect.options.length === 0) {
  933. // Configure start values
  934. var options = [
  935. { text: "Mesh", targetType: "MeshProperties" },
  936. { text: "Light", targetType: "LightProperties" },
  937. { text: "Camera", targetType: "CameraProperties" },
  938. { text: "Scene", targetType: "SceneProperties" }
  939. ];
  940. targetParameterSelect.options.length = 0;
  941. for (var i = 0; i < options.length; i++) {
  942. var option = document.createElement("option");
  943. option.text = options[i].text;
  944. option.value = options[i].targetType;
  945. targetParameterSelect.options.add(option);
  946. }
  947. targetParameterSelect.value = _this._action.propertiesResults[indice].targetType;
  948. }
  949. else {
  950. _this._action.propertiesResults[indice].targetType = targetParameterSelect.value;
  951. var names = _this._getListFromType(targetParameterSelect.value);
  952. if (names !== null && names.length > 0) {
  953. _this._action.propertiesResults[indice].value = names[0];
  954. }
  955. else {
  956. _this._action.propertiesResults[indice].value = "";
  957. }
  958. if (propertyPathSelect !== null) {
  959. _this._action.propertiesResults[indice + 1].value = ""; // propertyPath
  960. }
  961. }
  962. // Configure target names
  963. var targetParameterProperties = _this._getTargetFromType(targetParameterSelect.value);
  964. targetParameterNameSelect.options.length = 0;
  965. if (targetParameterProperties !== null) {
  966. for (var i = 0; i < targetParameterProperties.length; i++) {
  967. var option = document.createElement("option");
  968. option.text = option.value = targetParameterProperties[i];
  969. targetParameterNameSelect.options.add(option);
  970. }
  971. }
  972. targetParameterNameSelect.value = _this._action.propertiesResults[indice].value;
  973. // Clear property path
  974. if (propertyPathSelect !== null) {
  975. propertyPathSelect.options.length = 0;
  976. additionalPropertyPathSelect.options.length = 0;
  977. _this._propertyPathSelectChanged(targetParameterSelect, propertyPathSelect, additionalPropertyPathSelect, null, null, indice + 1)(null);
  978. }
  979. _this._sortList(targetParameterNameSelect);
  980. _this._sortList(targetParameterSelect);
  981. };
  982. };
  983. /*
  984. * Returns the parameter/target name select changed
  985. * @param indice: the properties indice to change
  986. */
  987. Parameters.prototype._parameterTargetNameChanged = function (targetParameterSelect, targetParameterNameSelect, indice) {
  988. var _this = this;
  989. return function (event) {
  990. _this._action.propertiesResults[indice].value = targetParameterNameSelect.value;
  991. };
  992. };
  993. /*
  994. * Returns the array of objects names in function of its type
  995. * @param type: the target type
  996. */
  997. Parameters.prototype._getTargetFromType = function (type) {
  998. if (type === "MeshProperties" || type === "Mesh") {
  999. return ActionsBuilder.SceneElements.MESHES;
  1000. }
  1001. if (type === "LightProperties" || type === "Light") {
  1002. return ActionsBuilder.SceneElements.LIGHTS;
  1003. }
  1004. if (type === "CameraProperties" || type === "Camera") {
  1005. return ActionsBuilder.SceneElements.CAMERAS;
  1006. }
  1007. return null;
  1008. };
  1009. /*
  1010. * Returns the properties in function of its type
  1011. * @param type: the target type
  1012. */
  1013. Parameters.prototype._getPropertiesFromType = function (type) {
  1014. if (type === "MeshProperties" || type === "Mesh") {
  1015. return ActionsBuilder.SceneElements.MESH_PROPERTIES;
  1016. }
  1017. if (type === "LightProperties" || type === "Light") {
  1018. return ActionsBuilder.SceneElements.LIGHT_PROPERTIES;
  1019. }
  1020. if (type === "CameraProperties" || type === "Camera") {
  1021. return ActionsBuilder.SceneElements.CAMERA_PROPERTIES;
  1022. }
  1023. if (type === "SceneProperties" || type === "Scene") {
  1024. return ActionsBuilder.SceneElements.SCENE_PROPERTIES;
  1025. }
  1026. return null;
  1027. };
  1028. Parameters.prototype._getListFromType = function (type) {
  1029. if (type === "MeshProperties" || type === "Mesh") {
  1030. return ActionsBuilder.SceneElements.MESHES;
  1031. }
  1032. if (type === "LightProperties" || type === "Light") {
  1033. return ActionsBuilder.SceneElements.LIGHTS;
  1034. }
  1035. if (type === "CameraProperties" || type === "Camera") {
  1036. return ActionsBuilder.SceneElements.CAMERAS;
  1037. }
  1038. return null;
  1039. };
  1040. /*
  1041. * Returns the object in function of the given type
  1042. * @param type: the target type
  1043. */
  1044. Parameters.prototype._getObjectFromType = function (type) {
  1045. if (type === "MeshProperties" || type === "Mesh") {
  1046. this._currentObject = ActionsBuilder.SceneElements.MESH;
  1047. return ActionsBuilder.SceneElements.MESH;
  1048. }
  1049. if (type === "LightProperties" || type === "Light") {
  1050. this._currentObject = ActionsBuilder.SceneElements.LIGHT;
  1051. return ActionsBuilder.SceneElements.LIGHT;
  1052. }
  1053. if (type === "CameraProperties" || type === "Camera") {
  1054. this._currentObject = ActionsBuilder.SceneElements.CAMERA;
  1055. return ActionsBuilder.SceneElements.CAMERA;
  1056. }
  1057. if (type === "SceneProperties" || type === "Scene") {
  1058. this._currentObject = ActionsBuilder.SceneElements.SCENE;
  1059. return ActionsBuilder.SceneElements.SCENE;
  1060. }
  1061. return null;
  1062. };
  1063. /*
  1064. * Creates the node section (top of parameters)
  1065. * @param action: the action element to get color, text, name etc.
  1066. */
  1067. Parameters.prototype._createNodeSection = function (action) {
  1068. var element = document.createElement("div");
  1069. element.style.background = this._viewer.getSelectedNodeColor(action.type, action.node.detached);
  1070. element.className = "ParametersElementNodeClass";
  1071. var text = document.createElement("a");
  1072. text.text = action.name;
  1073. text.className = "ParametersElementNodeTextClass";
  1074. element.appendChild(text);
  1075. this.parametersContainer.appendChild(element);
  1076. };
  1077. /*
  1078. * Creates the help section
  1079. * @param action : the action containing the description
  1080. */
  1081. Parameters.prototype._createHelpSection = function (action) {
  1082. // Get description
  1083. var element = ActionsBuilder.Elements.GetElementFromName(action.name);
  1084. if (element !== null) {
  1085. this.parametersHelpElement.textContent = element.description;
  1086. }
  1087. };
  1088. /*
  1089. * Alphabetically sorts a HTML select element options
  1090. * @param element : the HTML select element to sort
  1091. */
  1092. Parameters.prototype._sortList = function (element) {
  1093. var options = [];
  1094. for (var i = element.options.length - 1; i >= 0; i--) {
  1095. options.push(element.removeChild(element.options[i]));
  1096. }
  1097. options.sort(function (a, b) {
  1098. return a.innerHTML.localeCompare(b.innerHTML);
  1099. });
  1100. for (var i = 0; i < options.length; i++) {
  1101. element.options.add(options[i]);
  1102. }
  1103. };
  1104. return Parameters;
  1105. })();
  1106. ActionsBuilder.Parameters = Parameters;
  1107. })(ActionsBuilder || (ActionsBuilder = {}));
  1108. var ActionsBuilder;
  1109. (function (ActionsBuilder) {
  1110. var Toolbar = (function () {
  1111. function Toolbar(viewer) {
  1112. var _this = this;
  1113. // Get HTML elements
  1114. this.toolbarElement = document.getElementById("ToolbarElementID");
  1115. // Configure this
  1116. this._viewer = viewer;
  1117. // Manage events
  1118. window.addEventListener("resize", function (event) {
  1119. _this.onResize();
  1120. });
  1121. // Bottom toolbar
  1122. document.getElementById("ViewerDeZoomID").addEventListener("click", function (event) {
  1123. if (_this._viewer.zoom > 0.1) {
  1124. _this._viewer.zoom -= 0.1;
  1125. }
  1126. _this._viewer.update();
  1127. });
  1128. document.getElementById("ViewerZoomID").addEventListener("click", function (event) {
  1129. if (_this._viewer.zoom < 1.0) {
  1130. _this._viewer.zoom += 0.1;
  1131. }
  1132. _this._viewer.update();
  1133. });
  1134. document.getElementById("ViewerReconnectAll").addEventListener("click", function (event) {
  1135. for (var i = 0; i < _this._viewer.root.children.length; i++) {
  1136. _this._viewer.selectedNode = _this._viewer.root.children[i];
  1137. _this._viewer.utils.onDetachAction(false, true);
  1138. }
  1139. _this._viewer.update();
  1140. _this._viewer.selectedNode = null;
  1141. });
  1142. document.getElementById("ViewerDisconnectAll").addEventListener("click", function (event) {
  1143. for (var i = 0; i < _this._viewer.root.children.length; i++) {
  1144. _this._viewer.selectedNode = _this._viewer.root.children[i];
  1145. _this._viewer.utils.onDetachAction(true, false);
  1146. }
  1147. _this._viewer.update();
  1148. _this._viewer.selectedNode = null;
  1149. });
  1150. document.getElementById("ViewerReduceAll").addEventListener("click", function (event) {
  1151. for (var i = 0; i < _this._viewer.root.children.length; i++) {
  1152. _this._viewer.selectedNode = _this._viewer.root.children[i];
  1153. _this._viewer.utils.onReduceAll(false);
  1154. }
  1155. _this._viewer.update();
  1156. _this._viewer.selectedNode = null;
  1157. });
  1158. document.getElementById("ViewerExpandAll").addEventListener("click", function (event) {
  1159. for (var i = 0; i < _this._viewer.root.children.length; i++) {
  1160. _this._viewer.selectedNode = _this._viewer.root.children[i];
  1161. _this._viewer.utils.onReduceAll(true);
  1162. }
  1163. _this._viewer.update();
  1164. _this._viewer.selectedNode = null;
  1165. });
  1166. // Top toolbar
  1167. this.saveActionGraphElement = document.getElementById("ToolsButtonIDSaveActionGraph");
  1168. this.drawSaveActionGraphButton(false);
  1169. document.getElementById("ResetActionGraphID").addEventListener("click", function (event) {
  1170. if (confirm("Are you sure?")) {
  1171. for (var i = 0; i < _this._viewer.root.children.length; i++) {
  1172. _this._viewer.selectedNode = _this._viewer.root.children[i];
  1173. _this._viewer.utils.onRemoveBranch();
  1174. }
  1175. _this._viewer.update();
  1176. _this._viewer.selectedNode = null;
  1177. }
  1178. });
  1179. document.getElementById("TestActionGraphID").addEventListener("click", function (event) {
  1180. _this._viewer.utils.onTestGraph();
  1181. });
  1182. }
  1183. Toolbar.prototype.onResize = function () {
  1184. this.toolbarElement.style.top = this._viewer.viewerElement.clientHeight + 20 + "px";
  1185. };
  1186. Toolbar.prototype.drawSaveActionGraphButton = function (draw) {
  1187. this.saveActionGraphElement.style.display = draw ? "block" : "none";
  1188. };
  1189. return Toolbar;
  1190. })();
  1191. ActionsBuilder.Toolbar = Toolbar;
  1192. })(ActionsBuilder || (ActionsBuilder = {}));
  1193. var ActionsBuilder;
  1194. (function (ActionsBuilder) {
  1195. /**
  1196. * Defines static types
  1197. */
  1198. var Type = (function () {
  1199. function Type() {
  1200. }
  1201. Object.defineProperty(Type, "TRIGGER", {
  1202. get: function () {
  1203. return Type._TRIGGER;
  1204. },
  1205. enumerable: true,
  1206. configurable: true
  1207. });
  1208. Object.defineProperty(Type, "ACTION", {
  1209. get: function () {
  1210. return Type._ACTION;
  1211. },
  1212. enumerable: true,
  1213. configurable: true
  1214. });
  1215. Object.defineProperty(Type, "FLOW_CONTROL", {
  1216. get: function () {
  1217. return Type._FLOW_CONTROL;
  1218. },
  1219. enumerable: true,
  1220. configurable: true
  1221. });
  1222. Object.defineProperty(Type, "OBJECT", {
  1223. get: function () {
  1224. return Type._OBJECT;
  1225. },
  1226. enumerable: true,
  1227. configurable: true
  1228. });
  1229. Object.defineProperty(Type, "SCENE", {
  1230. get: function () {
  1231. return Type._SCENE;
  1232. },
  1233. enumerable: true,
  1234. configurable: true
  1235. });
  1236. Type._TRIGGER = 0;
  1237. Type._ACTION = 1;
  1238. Type._FLOW_CONTROL = 2;
  1239. Type._OBJECT = 3;
  1240. Type._SCENE = 4;
  1241. return Type;
  1242. })();
  1243. ActionsBuilder.Type = Type;
  1244. /*
  1245. * Defines the BABYLON.JS elements
  1246. */
  1247. var SceneElements = (function () {
  1248. function SceneElements() {
  1249. }
  1250. Object.defineProperty(SceneElements, "ENGINE", {
  1251. get: function () {
  1252. return SceneElements._ENGINE;
  1253. },
  1254. enumerable: true,
  1255. configurable: true
  1256. });
  1257. Object.defineProperty(SceneElements, "SCENE", {
  1258. get: function () {
  1259. return SceneElements._SCENE;
  1260. },
  1261. enumerable: true,
  1262. configurable: true
  1263. });
  1264. Object.defineProperty(SceneElements, "MESH", {
  1265. get: function () {
  1266. return SceneElements._MESH;
  1267. },
  1268. enumerable: true,
  1269. configurable: true
  1270. });
  1271. Object.defineProperty(SceneElements, "LIGHT", {
  1272. get: function () {
  1273. return SceneElements._LIGHT;
  1274. },
  1275. enumerable: true,
  1276. configurable: true
  1277. });
  1278. Object.defineProperty(SceneElements, "CAMERA", {
  1279. get: function () {
  1280. return SceneElements._CAMERA;
  1281. },
  1282. enumerable: true,
  1283. configurable: true
  1284. });
  1285. Object.defineProperty(SceneElements, "MESHES", {
  1286. get: function () {
  1287. return SceneElements._MESHES;
  1288. },
  1289. enumerable: true,
  1290. configurable: true
  1291. });
  1292. Object.defineProperty(SceneElements, "LIGHTS", {
  1293. get: function () {
  1294. return SceneElements._LIGHTS;
  1295. },
  1296. enumerable: true,
  1297. configurable: true
  1298. });
  1299. Object.defineProperty(SceneElements, "CAMERAS", {
  1300. get: function () {
  1301. return SceneElements._CAMERAS;
  1302. },
  1303. enumerable: true,
  1304. configurable: true
  1305. });
  1306. Object.defineProperty(SceneElements, "SOUNDS", {
  1307. get: function () {
  1308. return SceneElements._SOUNDS;
  1309. },
  1310. enumerable: true,
  1311. configurable: true
  1312. });
  1313. Object.defineProperty(SceneElements, "MESH_PROPERTIES", {
  1314. get: function () {
  1315. return SceneElements._MESH_PROPERTIES;
  1316. },
  1317. enumerable: true,
  1318. configurable: true
  1319. });
  1320. Object.defineProperty(SceneElements, "LIGHT_PROPERTIES", {
  1321. get: function () {
  1322. return SceneElements._LIGHT_PROPERTIES;
  1323. },
  1324. enumerable: true,
  1325. configurable: true
  1326. });
  1327. Object.defineProperty(SceneElements, "CAMERA_PROPERTIES", {
  1328. get: function () {
  1329. return SceneElements._CAMERA_PROPERTIES;
  1330. },
  1331. enumerable: true,
  1332. configurable: true
  1333. });
  1334. Object.defineProperty(SceneElements, "SCENE_PROPERTIES", {
  1335. get: function () {
  1336. return SceneElements._SCENE_PROPERTIES;
  1337. },
  1338. enumerable: true,
  1339. configurable: true
  1340. });
  1341. Object.defineProperty(SceneElements, "TYPES", {
  1342. get: function () {
  1343. return SceneElements._TYPES;
  1344. },
  1345. enumerable: true,
  1346. configurable: true
  1347. });
  1348. Object.defineProperty(SceneElements, "OPERATORS", {
  1349. get: function () {
  1350. return SceneElements._OPERATORS;
  1351. },
  1352. enumerable: true,
  1353. configurable: true
  1354. });
  1355. /*
  1356. * Methods
  1357. */
  1358. SceneElements.GetInstanceOf = function (object) {
  1359. if (object === null || object === undefined) {
  1360. return "";
  1361. }
  1362. return object.constructor.toString().match(/function (\w*)/)[1];
  1363. };
  1364. SceneElements.TestInstanceOf = function (object, propertyName) {
  1365. if (object === null || object.constructor === null) {
  1366. return false;
  1367. }
  1368. if (propertyName.length > 0 && propertyName[0] === "_")
  1369. return false;
  1370. var name = SceneElements.GetInstanceOf(object);
  1371. for (var i = 0; i < SceneElements.TYPES.length; i++) {
  1372. if (name === SceneElements.TYPES[i]) {
  1373. return true;
  1374. }
  1375. }
  1376. return false;
  1377. };
  1378. /*
  1379. * BabylonJS objects
  1380. */
  1381. SceneElements._ENGINE = new BABYLON.Engine(document.getElementById("RenderCanvasID"));
  1382. SceneElements._SCENE = new BABYLON.Scene(SceneElements.ENGINE);
  1383. SceneElements._MESH = new BABYLON.Mesh("mesh", SceneElements._SCENE);
  1384. SceneElements._LIGHT = new BABYLON.Light("light", SceneElements._SCENE);
  1385. SceneElements._CAMERA = new BABYLON.Camera("camera", BABYLON.Vector3.Zero(), SceneElements._SCENE);
  1386. /*
  1387. * Objects names
  1388. */
  1389. SceneElements._MESHES = new Array();
  1390. SceneElements._LIGHTS = new Array();
  1391. SceneElements._CAMERAS = new Array();
  1392. SceneElements._SOUNDS = new Array();
  1393. /*
  1394. * Properties
  1395. */
  1396. SceneElements._MESH_PROPERTIES = new Array();
  1397. SceneElements._LIGHT_PROPERTIES = new Array();
  1398. SceneElements._CAMERA_PROPERTIES = new Array();
  1399. SceneElements._SCENE_PROPERTIES = new Array();
  1400. /*
  1401. * Types
  1402. */
  1403. SceneElements._TYPES = new Array();
  1404. /*
  1405. * Operators
  1406. */
  1407. SceneElements._OPERATORS = new Array();
  1408. return SceneElements;
  1409. })();
  1410. ActionsBuilder.SceneElements = SceneElements;
  1411. // Functions
  1412. var specialTypes = [
  1413. "StandardMaterial"
  1414. ];
  1415. SceneElements.MESH.material = new BABYLON.StandardMaterial("material", SceneElements.SCENE);
  1416. var addSpecialType = function (object, properties, thing) {
  1417. for (var specialThing in object[thing]) {
  1418. if (object[thing].hasOwnProperty(specialThing) && SceneElements.TestInstanceOf(object[thing][specialThing], specialThing)) {
  1419. properties.push(thing + "." + specialThing);
  1420. }
  1421. }
  1422. };
  1423. // Configure types
  1424. SceneElements.TYPES.push("Color3");
  1425. SceneElements.TYPES.push("Boolean");
  1426. SceneElements.TYPES.push("Number");
  1427. SceneElements.TYPES.push("Vector2");
  1428. SceneElements.TYPES.push("Vector3");
  1429. SceneElements.TYPES.push("String");
  1430. // Configure operators
  1431. SceneElements.OPERATORS.push("IsEqual");
  1432. SceneElements.OPERATORS.push("IsDifferent");
  1433. SceneElements.OPERATORS.push("IsGreater");
  1434. SceneElements.OPERATORS.push("IsLesser");
  1435. // Configure properties
  1436. for (var thing in SceneElements.MESH) {
  1437. var instance = SceneElements.GetInstanceOf(SceneElements.MESH[thing]);
  1438. if (SceneElements.MESH.hasOwnProperty(thing)) {
  1439. if (specialTypes.indexOf(instance) !== -1) {
  1440. addSpecialType(SceneElements.MESH, SceneElements.MESH_PROPERTIES, thing);
  1441. }
  1442. else if (SceneElements.TestInstanceOf(SceneElements.MESH[thing], thing)) {
  1443. SceneElements.MESH_PROPERTIES.push(thing);
  1444. }
  1445. }
  1446. }
  1447. for (var thing in SceneElements.LIGHT) {
  1448. if (SceneElements.LIGHT.hasOwnProperty(thing) && SceneElements.TestInstanceOf(SceneElements.LIGHT[thing], thing)) {
  1449. SceneElements.LIGHT_PROPERTIES.push(thing);
  1450. }
  1451. }
  1452. for (var thing in SceneElements.CAMERA) {
  1453. if (SceneElements.CAMERA.hasOwnProperty(thing) && SceneElements.TestInstanceOf(SceneElements.CAMERA[thing], thing)) {
  1454. SceneElements.CAMERA_PROPERTIES.push(thing);
  1455. }
  1456. }
  1457. for (var thing in SceneElements.SCENE) {
  1458. if (SceneElements.SCENE.hasOwnProperty(thing) && SceneElements.TestInstanceOf(SceneElements.SCENE[thing], thing)) {
  1459. SceneElements.SCENE_PROPERTIES.push(thing);
  1460. }
  1461. }
  1462. /**
  1463. * Actions Builder elements (triggers, actions & flow controls) that are
  1464. * arrays of Element
  1465. */
  1466. var Elements = (function () {
  1467. function Elements() {
  1468. }
  1469. Object.defineProperty(Elements, "TRIGGERS", {
  1470. get: function () {
  1471. return Elements._TRIGGERS;
  1472. },
  1473. enumerable: true,
  1474. configurable: true
  1475. });
  1476. Object.defineProperty(Elements, "ACTIONS", {
  1477. get: function () {
  1478. return Elements._ACTIONS;
  1479. },
  1480. enumerable: true,
  1481. configurable: true
  1482. });
  1483. Object.defineProperty(Elements, "FLOW_CONTROLS", {
  1484. get: function () {
  1485. return Elements._FLOW_CONTROLS;
  1486. },
  1487. enumerable: true,
  1488. configurable: true
  1489. });
  1490. Elements.GetElementFromName = function (name) {
  1491. for (var i = 0; i < Elements.TRIGGERS.length; i++) {
  1492. if (Elements.TRIGGERS[i].name === name) {
  1493. return Elements._TRIGGERS[i];
  1494. }
  1495. }
  1496. for (var i = 0; i < Elements.ACTIONS.length; i++) {
  1497. if (Elements.ACTIONS[i].name === name) {
  1498. return Elements._ACTIONS[i];
  1499. }
  1500. }
  1501. for (var i = 0; i < Elements.FLOW_CONTROLS.length; i++) {
  1502. if (Elements.FLOW_CONTROLS[i].name === name) {
  1503. return Elements._FLOW_CONTROLS[i];
  1504. }
  1505. }
  1506. return null;
  1507. };
  1508. Elements._TRIGGERS = new Array();
  1509. Elements._ACTIONS = new Array();
  1510. Elements._FLOW_CONTROLS = new Array();
  1511. return Elements;
  1512. })();
  1513. ActionsBuilder.Elements = Elements;
  1514. // Configure triggers
  1515. Elements.TRIGGERS.push({ name: "OnPickTrigger", text: "pick", properties: [], description: "When the user picks the edited mesh" });
  1516. Elements.TRIGGERS.push({ name: "OnLeftPickTrigger", text: "left pick", properties: [], description: "When the user picks the edited mesh using the left click" });
  1517. Elements.TRIGGERS.push({ name: "OnRightPickTrigger", text: "right pick", properties: [], description: "When the user picks the edited mesh using the right click" });
  1518. Elements.TRIGGERS.push({ name: "OnCenterPickTrigger", text: "center pick", properties: [], description: "When the user picks the edited mesh using the click of the mouse wheel" });
  1519. Elements.TRIGGERS.push({ name: "OnPointerOverTrigger", text: "pointer over", properties: [], description: "When the user's mouse is over the edited mesh" });
  1520. Elements.TRIGGERS.push({ name: "OnPointerOutTrigger", text: "pointer out", properties: [], description: "When the user's mouse is out of the edited mesh" });
  1521. Elements.TRIGGERS.push({ name: "OnEveryFrameTrigger", text: "every frame", properties: [], description: "This trigger is called each frame (only on scene)" });
  1522. Elements.TRIGGERS.push({ name: "OnIntersectionEnterTrigger", text: "intersection enter", properties: [{ targetType: "MeshProperties", text: "parameter", value: "Object name?" }], description: "When the edited mesh intersects the another mesh predefined in the options" });
  1523. Elements.TRIGGERS.push({ name: "OnIntersectionExitTrigger", text: "intersection exit", properties: [{ targetType: "MeshProperties", text: "parameter", value: "Object name?" }], description: "When the edited mesh exits intersection with the another mesh predefined in the options" });
  1524. Elements.TRIGGERS.push({ name: "OnKeyDownTrigger", text: "key down", properties: [{ targetType: null, text: "parameter:", value: "a" }], description: "When the user pressed a key (enter the key character, example: \"r\")" });
  1525. Elements.TRIGGERS.push({ name: "OnKeyUpTrigger", text: "key up", properties: [{ targetType: null, text: "parameter:", value: "a" }], description: "When the user unpressed a key (enter the key character, example: \"p\")" });
  1526. // Configure actions
  1527. Elements.ACTIONS.push({ name: "SwitchBooleanAction", text: "switch boolean", properties: [{ targetType: "MeshProperties", text: "target", value: "" }, { text: "propertyPath", value: "" }], description: "Switches the boolean value of a given parameter of the target object: true to false, or false to true" });
  1528. Elements.ACTIONS.push({ name: "SetStateAction", text: "set state", properties: [{ targetType: "MeshProperties", text: "target", value: "" }, { text: "value", value: "" }], description: "Sets a new state value for the target object (example: \"off\" or \"on\")" });
  1529. Elements.ACTIONS.push({ name: "SetValueAction", text: "set value", properties: [{ targetType: "MeshProperties", text: "target", value: "" }, { text: "propertyPath", value: "" }, { text: "value", value: "" }], description: "Sets a new value to the specified parameter of the target object (example: position.x to 0.0)" });
  1530. Elements.ACTIONS.push({ name: "SetParentAction", text: "set parent", properties: [{ targetType: "MeshProperties", text: "target", value: "" }, { text: "parent", value: "" }], description: "Sets the new parent of the target object (example: a mesh or a light)" });
  1531. Elements.ACTIONS.push({ name: "IncrementValueAction", text: "increment value", properties: [{ targetType: "MeshProperties", text: "target", value: "" }, { text: "propertyPath", value: "" }, { text: "value", value: "" }], description: "Increments the value of the given parameter of the target object. The value can be negative. (example: increment position.x of 5.0)" });
  1532. Elements.ACTIONS.push({ name: "PlayAnimationAction", text: "play animation", properties: [{ targetType: "MeshProperties", text: "target", value: "" }, { text: "from", value: "0" }, { text: "to", value: "150" }, { text: "loop", value: "false" }], description: "Plays an animation of the target object. Specify the start frame, the end frame and if the animation should loop." });
  1533. Elements.ACTIONS.push({ name: "StopAnimationAction", text: "stop animation", properties: [{ targetType: "MeshProperties", text: "target", value: "" }], description: "Stops the animations of the target object." });
  1534. Elements.ACTIONS.push({ name: "DoNothingAction", text: "do nothing", properties: [], description: "Does nothing, can be used to balance/equilibrate the actions graph." });
  1535. Elements.ACTIONS.push({ name: "InterpolateValueAction", text: "interpolate value", properties: [{ targetType: "MeshProperties", text: "target", value: "" }, { text: "propertyPath", value: "" }, { text: "value", value: "0" }, { text: "duration", value: "1000" }, { text: "stopOtherAnimations", value: "false" }], description: "Creates an animation (key frames) that animates the target object by interpolating the given parameter of the target value." });
  1536. Elements.ACTIONS.push({ name: "PlaySoundAction", text: "play sound", properties: [{ text: "sound", value: "" }], description: "Plays the specified sound." });
  1537. Elements.ACTIONS.push({ name: "StopSoundAction", text: "stop sound", properties: [{ text: "sound", value: "" }], description: "Stops the specified sound." });
  1538. Elements.ACTIONS.push({ name: "CombineAction", text: "combine", properties: [], description: "Special action that combines multiple actions. The combined actions are executed at the same time. Drag'n'drop the new actions inside to combine actions." });
  1539. // Configure flow control
  1540. Elements.FLOW_CONTROLS.push({ name: "ValueCondition", text: "value condition", properties: [{ targetType: "MeshProperties", text: "target", value: "" }, { text: "propertyPath", value: "" }, { text: "value", value: "" }, { text: "operator", value: SceneElements.OPERATORS[0] }], description: "A condition checking if a given value is equal, different, lesser or greater than the given parameter of the target object" });
  1541. Elements.FLOW_CONTROLS.push({ name: "StateCondition", text: "state condition", properties: [{ targetType: "MeshProperties", text: "target", value: "" }, { text: "value", value: "" }], description: "A condition checking if the target object's state is equal to the given state. See \"set state\" action to set a state to an object." });
  1542. Elements.FLOW_CONTROLS.push({ name: "Hub", text: "hub", properties: [], description: "The hub is internally used by the Combine Action. It allows to add children to the Combine Action" });
  1543. })(ActionsBuilder || (ActionsBuilder = {}));
  1544. var ActionsBuilder;
  1545. (function (ActionsBuilder) {
  1546. var Utils = (function () {
  1547. /*
  1548. * Constructor
  1549. * @param viewer: the viewer instance
  1550. */
  1551. function Utils(viewer) {
  1552. // Members
  1553. this.copiedStructure = null;
  1554. // Configure this
  1555. this._viewer = viewer;
  1556. }
  1557. /*
  1558. * Tests the graph and reports errors
  1559. */
  1560. Utils.prototype.onTestGraph = function () {
  1561. var _this = this;
  1562. if (this._viewer.root.children.length === 0) {
  1563. alert("Please add at least a Trigger and an Action to test the graph");
  1564. }
  1565. var onTestTarget = function (targetType, target) {
  1566. var targetExists = false;
  1567. var array = _this._viewer.parameters._getTargetFromType(targetType);
  1568. if (array === null) {
  1569. return targetExists;
  1570. }
  1571. for (var i = 0; i < array.length; i++) {
  1572. if (array[i] === target) {
  1573. targetExists = true;
  1574. break;
  1575. }
  1576. }
  1577. return targetExists;
  1578. };
  1579. var onNodeError = function (action) {
  1580. var node = action.node;
  1581. node.rect.attr("fill", Raphael.rgb(255, 0, 0));
  1582. return false;
  1583. };
  1584. var onTestAction = function (action) {
  1585. console.log("Testing " + action.name);
  1586. if (action.combineArray !== null) {
  1587. var foundError = false;
  1588. for (var i = 0; i < action.combineArray.length; i++) {
  1589. if (!onTestAction(action.combineArray[i])) {
  1590. foundError = true;
  1591. }
  1592. }
  1593. if (foundError) {
  1594. return false;
  1595. }
  1596. }
  1597. else {
  1598. // Test properties
  1599. var properties = action.properties;
  1600. var propertiesResults = action.propertiesResults;
  1601. if (properties !== null) {
  1602. var object = null;
  1603. var propertyPath = null;
  1604. for (var i = 0; i < properties.length; i++) {
  1605. // Target
  1606. if (properties[i].text === "target" || properties[i].text === "parent") {
  1607. object = _this._viewer.parameters._getObjectFromType(properties[i].targetType);
  1608. var targetExists = onTestTarget(propertiesResults[i].targetType, propertiesResults[i].value);
  1609. if (!targetExists) {
  1610. return onNodeError(action);
  1611. }
  1612. }
  1613. else if (properties[i].text === "propertyPath") {
  1614. var property = propertiesResults[i].value;
  1615. var effectiveProperty = object;
  1616. var p = property.split(".");
  1617. for (var j = 0; j < p.length && effectiveProperty !== undefined; j++) {
  1618. effectiveProperty = effectiveProperty[p[j]];
  1619. }
  1620. if (effectiveProperty === undefined) {
  1621. return onNodeError(action);
  1622. }
  1623. else {
  1624. propertyPath = effectiveProperty;
  1625. }
  1626. }
  1627. else if (properties[i].text == "value" && propertyPath != null) {
  1628. var value = propertiesResults[i].value;
  1629. if (!isNaN(propertyPath)) {
  1630. var num = parseFloat(value);
  1631. if (isNaN(num) || value === "") {
  1632. return onNodeError(action);
  1633. }
  1634. }
  1635. }
  1636. }
  1637. }
  1638. var foundError = false;
  1639. for (var i = 0; i < action.children.length; i++) {
  1640. if (!onTestAction(action.children[i])) {
  1641. foundError = true;
  1642. }
  1643. }
  1644. return !foundError;
  1645. }
  1646. };
  1647. var root = this._viewer.root;
  1648. var foundError = false;
  1649. for (var i = 0; i < root.children.length; i++) {
  1650. var trigger = root.children[i];
  1651. var properties = trigger.properties;
  1652. // Test properties of trigger (parameter)
  1653. if (properties !== null && properties.length > 0) {
  1654. // Only one property
  1655. var parameter = trigger.propertiesResults[0].value;
  1656. if (properties[0].targetType !== null) {
  1657. // Intersection trigger
  1658. if (!onTestTarget("MeshProperties", parameter)) {
  1659. foundError = onNodeError(trigger);
  1660. }
  1661. }
  1662. else {
  1663. // Key trigger
  1664. if (!parameter.match(/[a-z]/)) {
  1665. foundError = onNodeError(trigger);
  1666. }
  1667. }
  1668. }
  1669. for (var j = 0; j < trigger.children.length; j++) {
  1670. var child = trigger.children[j];
  1671. var result = onTestAction(child);
  1672. if (!result) {
  1673. foundError = true;
  1674. }
  1675. }
  1676. }
  1677. if (foundError) {
  1678. alert("Found error(s). the red nodes contain the error.");
  1679. }
  1680. else {
  1681. alert("No error found.");
  1682. }
  1683. };
  1684. /*
  1685. * Recursively reduce/expand nodes
  1686. */
  1687. Utils.prototype.onReduceAll = function (forceExpand) {
  1688. if (forceExpand === void 0) { forceExpand = false; }
  1689. if (this._viewer.selectedNode === null) {
  1690. return;
  1691. }
  1692. var action = this._viewer.selectedNode;
  1693. if (action.combineArray !== null) {
  1694. for (var i = 0; i < action.combineArray.length; i++) {
  1695. this._viewer.selectedNode = action.combineArray[i];
  1696. this.onReduce(forceExpand, !forceExpand);
  1697. }
  1698. }
  1699. else {
  1700. this.onReduce(forceExpand, !forceExpand);
  1701. }
  1702. for (var i = 0; i < action.children.length; i++) {
  1703. this._viewer.selectedNode = action.children[i];
  1704. this.onReduceAll(forceExpand);
  1705. }
  1706. };
  1707. /*
  1708. * Reduces the selected node
  1709. */
  1710. Utils.prototype.onReduce = function (forceExpand, forceReduce) {
  1711. if (forceExpand === void 0) { forceExpand = false; }
  1712. if (forceReduce === void 0) { forceReduce = false; }
  1713. if (this._viewer.selectedNode === null) {
  1714. return;
  1715. }
  1716. var node = this._viewer.selectedNode.node;
  1717. node.rect.stop(node.rect.animation);
  1718. // Set minimized
  1719. if (forceExpand === true) {
  1720. node.minimized = false;
  1721. }
  1722. else if (forceReduce === true) {
  1723. node.minimized = true;
  1724. }
  1725. else {
  1726. node.minimized = !node.minimized;
  1727. }
  1728. // Set size
  1729. if (node.minimized) {
  1730. node.text.hide();
  1731. node.rect.attr("width", ActionsBuilder.Viewer.NODE_MINIMIZED_WIDTH * this._viewer.zoom);
  1732. }
  1733. else {
  1734. node.text.show();
  1735. node.rect.attr("width", ActionsBuilder.Viewer.NODE_WIDTH * this._viewer.zoom);
  1736. }
  1737. };
  1738. /*
  1739. * Detaches the selected action
  1740. */
  1741. Utils.prototype.onDetachAction = function (forceDetach, forceAttach) {
  1742. var _this = this;
  1743. if (forceDetach === void 0) { forceDetach = false; }
  1744. if (forceAttach === void 0) { forceAttach = false; }
  1745. if (this._viewer.selectedNode === null) {
  1746. return;
  1747. }
  1748. var action = this._viewer.selectedNode;
  1749. if (forceDetach === true) {
  1750. action.node.detached = true;
  1751. }
  1752. else if (forceAttach === true) {
  1753. action.node.detached = false;
  1754. }
  1755. else {
  1756. action.node.detached = !action.node.detached;
  1757. }
  1758. var onSetColor = function (root, detached) {
  1759. var rootNode = root.node;
  1760. rootNode.rect.attr("fill", _this._viewer.getNodeColor(root.type, detached));
  1761. if (root.combineArray !== null) {
  1762. for (var i = 0; i < root.combineArray.length; i++) {
  1763. var combineNode = root.combineArray[i].node;
  1764. combineNode.rect.attr("fill", _this._viewer.getNodeColor(root.combineArray[i].type, detached));
  1765. }
  1766. }
  1767. for (var i = 0; i < root.children.length; i++) {
  1768. onSetColor(root.children[i], detached);
  1769. }
  1770. };
  1771. onSetColor(action, action.node.detached);
  1772. };
  1773. /*
  1774. * Removes the selected node
  1775. */
  1776. Utils.prototype.onRemoveNode = function () {
  1777. if (this._viewer.selectedNode === null) {
  1778. return;
  1779. }
  1780. var action = this._viewer.selectedNode;
  1781. var parent = action.parent;
  1782. // If trigger, remove branch
  1783. if (action.type === ActionsBuilder.Type.TRIGGER) {
  1784. this.onRemoveBranch();
  1785. return;
  1786. }
  1787. // If it is a combine hub
  1788. if (action.type === ActionsBuilder.Type.FLOW_CONTROL && parent !== null && parent.combineArray !== null) {
  1789. action = parent;
  1790. parent = action.parent;
  1791. }
  1792. // Remove
  1793. if (parent !== null && parent.combineArray !== null) {
  1794. parent.removeCombinedAction(action);
  1795. if (parent.combineArray.length === 0) {
  1796. parent.node.text.attr("text", "combine");
  1797. }
  1798. }
  1799. else {
  1800. if (action.combineArray !== null) {
  1801. action.removeChild(action.hub);
  1802. }
  1803. action.parent.removeChild(action);
  1804. }
  1805. if (action.combineArray !== null) {
  1806. this._viewer.removeAction(action.hub, false);
  1807. }
  1808. this._viewer.removeAction(action, false);
  1809. // Finish
  1810. this._viewer.update();
  1811. this._viewer.parameters.clearParameters();
  1812. this._viewer.selectedNode = null;
  1813. };
  1814. /*
  1815. * Removes a branch starting from the selected node
  1816. */
  1817. Utils.prototype.onRemoveBranch = function () {
  1818. if (this._viewer.selectedNode === null) {
  1819. return;
  1820. }
  1821. if (this._viewer.selectedNode === this._viewer.root) {
  1822. alert("Cannot remove the root node");
  1823. return;
  1824. }
  1825. var action = this._viewer.selectedNode;
  1826. var parent = action.parent;
  1827. // If combine
  1828. if (action.parent !== null && action.parent.combineArray !== null) {
  1829. action = parent;
  1830. parent = action.parent;
  1831. }
  1832. // Remove
  1833. if (action.combineArray !== null) {
  1834. action.removeChild(action.hub);
  1835. }
  1836. action.parent.removeChild(action);
  1837. this._viewer.removeAction(action, true);
  1838. // Finish
  1839. this._viewer.update();
  1840. this._viewer.parameters.clearParameters();
  1841. this._viewer.selectedNode = null;
  1842. };
  1843. /*
  1844. * Copies the selected structure
  1845. */
  1846. Utils.prototype.onCopyStructure = function () {
  1847. if (this._viewer.selectedNode === null) {
  1848. return;
  1849. }
  1850. var structure = this.createJSON(this._viewer.selectedNode);
  1851. var asText = JSON.stringify(structure);
  1852. if (window.clipboardData !== undefined) {
  1853. window.clipboardData.setData("text", asText);
  1854. }
  1855. else {
  1856. this.copiedStructure = asText;
  1857. }
  1858. };
  1859. /*
  1860. * Pastes the graph structure previously copied
  1861. */
  1862. Utils.prototype.onPasteStructure = function () {
  1863. if (this._viewer.selectedNode === null) {
  1864. return;
  1865. }
  1866. var asText = (window.clipboardData !== undefined) ? window.clipboardData.getData("text") : this.copiedStructure;
  1867. var isJson = asText.length > 0 && asText[0] == "{" && asText[asText.length - 1] == "}";
  1868. var structure = JSON.parse(asText);
  1869. var action = this._viewer.selectedNode;
  1870. if (structure.type === ActionsBuilder.Type.TRIGGER && action !== this._viewer.root) {
  1871. alert("You can't paste a trigger if the selected node isn't the root object");
  1872. return;
  1873. }
  1874. if (structure.type !== ActionsBuilder.Type.TRIGGER && action === this._viewer.root) {
  1875. alert("You can't paste an action or condition if the selected node is the root object");
  1876. return;
  1877. }
  1878. this.loadFromJSON(structure, action);
  1879. this._viewer.update();
  1880. };
  1881. /*
  1882. * Loads a graph from JSON
  1883. * @pram graph: the graph structure
  1884. * @param startAction: the action to start load
  1885. */
  1886. Utils.prototype.loadFromJSON = function (graph, startAction) {
  1887. var _this = this;
  1888. // If startNode is null, means it replaces all the graph
  1889. // If not, it comes from a copy/paste
  1890. if (startAction === null) {
  1891. for (var i = 0; i < this._viewer.root.children.length; i++) {
  1892. this._viewer.removeAction(this._viewer.root.children[i], true);
  1893. }
  1894. this._viewer.root.clearChildren();
  1895. }
  1896. var load = function (root, parent, detached, combine) {
  1897. if (parent === null) {
  1898. parent = _this._viewer.root;
  1899. }
  1900. var newAction = null;
  1901. if (root.type !== ActionsBuilder.Type.OBJECT && root.type !== ActionsBuilder.Type.SCENE) {
  1902. var action = _this._viewer.addAction(parent, root.type, ActionsBuilder.Elements.GetElementFromName(root.name));
  1903. for (var i = 0; i < root.properties.length; i++) {
  1904. var targetType = root.properties[i].targetType;
  1905. if (targetType === undefined) {
  1906. targetType = "MeshProperties"; // Default is mesh properties
  1907. }
  1908. action.propertiesResults[i] = { value: root.properties[i].value, targetType: targetType };
  1909. }
  1910. var node = action.node;
  1911. node.detached = root.detached;
  1912. if (detached) {
  1913. node.rect.attr("fill", _this._viewer.getNodeColor(action.type, detached));
  1914. }
  1915. // If combine array
  1916. if (root.combine !== undefined) {
  1917. for (var i = 0; i < root.combine.length; i++) {
  1918. load(root.combine[i], action, detached, true);
  1919. }
  1920. }
  1921. if (!combine) {
  1922. parent = parent.children[parent.children.length - 1];
  1923. }
  1924. }
  1925. for (var i = 0; i < root.children.length; i++) {
  1926. load(root.children[i], newAction !== null && newAction.combineArray !== null ? newAction.hub : parent, root.detached, false);
  1927. }
  1928. };
  1929. // Finish
  1930. load(graph, startAction, false, false);
  1931. this._viewer.update();
  1932. };
  1933. /*
  1934. * Creates a JSON object starting from a root action
  1935. * @param root: the root action
  1936. */
  1937. Utils.prototype.createJSON = function (root) {
  1938. var action = {
  1939. type: root.type,
  1940. name: root.name,
  1941. detached: root.node.detached,
  1942. children: new Array(),
  1943. combine: new Array(),
  1944. properties: new Array()
  1945. };
  1946. // Set properties
  1947. for (var i = 0; i < root.properties.length; i++) {
  1948. action.properties.push({
  1949. name: root.properties[i].text,
  1950. value: root.propertiesResults[i].value,
  1951. targetType: root.propertiesResults[i].targetType
  1952. });
  1953. }
  1954. // If combine
  1955. if (root.combineArray !== null) {
  1956. for (var i = 0; i < root.combineArray.length; i++) {
  1957. var combinedAction = root.combineArray[i];
  1958. action.combine.push(this.createJSON(combinedAction));
  1959. }
  1960. root = root.children[0]; // Hub
  1961. }
  1962. for (var i = 0; i < root.children.length; i++) {
  1963. action.children.push(this.createJSON(root.children[i]));
  1964. }
  1965. return action;
  1966. };
  1967. /*
  1968. *
  1969. */
  1970. Utils.prototype.setElementVisible = function (element, visible) {
  1971. element.style.display = visible ? "block" : "none";
  1972. };
  1973. return Utils;
  1974. })();
  1975. ActionsBuilder.Utils = Utils;
  1976. })(ActionsBuilder || (ActionsBuilder = {}));
  1977. var ActionsBuilder;
  1978. (function (ActionsBuilder) {
  1979. var Viewer = (function () {
  1980. /*
  1981. * Constructor
  1982. * @param type: the root type object (OBJECT or SCENE)
  1983. */
  1984. function Viewer(type) {
  1985. var _this = this;
  1986. this.objectName = "Unnamed Object";
  1987. this.zoom = 1.0;
  1988. this._firstUpdate = true;
  1989. // Get HTML elements
  1990. this.viewerContainer = document.getElementById("GraphContainerID");
  1991. this.viewerElement = document.getElementById("GraphElementID");
  1992. // Create element
  1993. this.paper = Raphael("GraphElementID", screen.width, screen.height);
  1994. // Configure this
  1995. //var name = type === Type.OBJECT ? "Unnamed object" : "Scene";
  1996. this.root = this.addAction(null, type, { name: this.objectName, text: this.objectName, properties: [], description: "" });
  1997. this.selectedNode = null;
  1998. // Configure events
  1999. window.addEventListener("resize", function (event) {
  2000. _this.onResize(event);
  2001. });
  2002. window.addEventListener("mousemove", function (event) {
  2003. _this.onMove(event);
  2004. });
  2005. this.paper.canvas.addEventListener("click", function (event) {
  2006. _this.onClick(event);
  2007. });
  2008. // Load modules
  2009. this._toolbar = new ActionsBuilder.Toolbar(this);
  2010. this._contextMenu = new ActionsBuilder.ContextMenu(this);
  2011. this.parameters = new ActionsBuilder.Parameters(this);
  2012. this.utils = new ActionsBuilder.Utils(this);
  2013. // Finish
  2014. this.parameters.parametersHelpElement.textContent = Viewer._DEFAULT_INFO_MESSAGE;
  2015. this.onResize(null);
  2016. }
  2017. Object.defineProperty(Viewer, "NODE_WIDTH", {
  2018. get: function () {
  2019. return Viewer._NODE_WIDTH;
  2020. },
  2021. enumerable: true,
  2022. configurable: true
  2023. });
  2024. Object.defineProperty(Viewer, "NODE_HEIGHT", {
  2025. get: function () {
  2026. return Viewer._NODE_HEIGHT;
  2027. },
  2028. enumerable: true,
  2029. configurable: true
  2030. });
  2031. Object.defineProperty(Viewer, "NODE_MINIMIZED_WIDTH", {
  2032. get: function () {
  2033. return Viewer._NODE_MINIMIZE_WIDTH;
  2034. },
  2035. enumerable: true,
  2036. configurable: true
  2037. });
  2038. Object.defineProperty(Viewer, "VERTICAL_OFFSET", {
  2039. get: function () {
  2040. return Viewer._VERTICAL_OFFSET;
  2041. },
  2042. enumerable: true,
  2043. configurable: true
  2044. });
  2045. /*
  2046. * Resize event
  2047. * @param event: the resize event
  2048. */
  2049. Viewer.prototype.onResize = function (event) {
  2050. var tools = document.getElementById("ToolsButtonsID");
  2051. this.viewerContainer.style.height = window.innerHeight - tools.getBoundingClientRect().height - 25 - 50 + "px";
  2052. this.viewerElement.style.height = window.innerHeight - tools.getBoundingClientRect().height - 25 - 50 + "px";
  2053. this.parameters.onResize();
  2054. this._toolbar.onResize();
  2055. if (this.paper.height < window.innerHeight) {
  2056. this.paper.setSize(this.paper.width, window.innerHeight);
  2057. }
  2058. if (this._firstUpdate) {
  2059. this.viewerElement.scrollLeft = ((this.viewerElement.scrollWidth / 2) - (this.viewerElement.getBoundingClientRect().width / 2));
  2060. this._firstUpdate = false;
  2061. }
  2062. };
  2063. /*
  2064. * Handles the onMove event
  2065. * @param event: the onMove mouse event
  2066. */
  2067. Viewer.prototype.onMove = function (event) {
  2068. this.mousex = event.clientX - this.paper.canvas.getBoundingClientRect().left;
  2069. this.mousey = event.clientY - this.paper.canvas.getBoundingClientRect().top;
  2070. };
  2071. /*
  2072. * Handles the onClick event to get selected node
  2073. * @param event: the onClick mouse event
  2074. */
  2075. Viewer.prototype.onClick = function (event) {
  2076. if (this._contextMenu.showing) {
  2077. return;
  2078. }
  2079. // Reset selected node
  2080. if (this.selectedNode !== null) {
  2081. var node = this.selectedNode.node;
  2082. node.rect.attr("fill", this.getNodeColor(this.selectedNode.type, node.detached));
  2083. }
  2084. // Configure new selected node
  2085. var result = this.traverseGraph(null, this.mousex, this.mousey, true);
  2086. if (result.hit) {
  2087. this.selectedNode = result.action;
  2088. var node = this.selectedNode.node;
  2089. node.rect.attr("fill", this.getSelectedNodeColor(this.selectedNode.type, node.detached));
  2090. }
  2091. else {
  2092. this.selectedNode = null;
  2093. this.parameters.clearParameters();
  2094. this.parameters.parametersHelpElement.textContent = Viewer._DEFAULT_INFO_MESSAGE;
  2095. }
  2096. };
  2097. /*
  2098. * Set the color theme of the viewer
  2099. * @param color: the color theme ( ex: "rgb(64, 64, 64)" )
  2100. */
  2101. Viewer.prototype.setColorTheme = function (color) {
  2102. this.paper.canvas.style.background = color;
  2103. };
  2104. /*
  2105. * Returns the color according to the given parameters
  2106. * @param action: the action used to select the color
  2107. * @param detached: if the node is attached to its parent or not
  2108. */
  2109. Viewer.prototype.getNodeColor = function (type, detached) {
  2110. if (detached) {
  2111. return Raphael.rgb(96, 122, 14);
  2112. }
  2113. switch (type) {
  2114. case ActionsBuilder.Type.TRIGGER:
  2115. return Raphael.rgb(133, 154, 185);
  2116. break;
  2117. case ActionsBuilder.Type.ACTION:
  2118. return Raphael.rgb(182, 185, 132);
  2119. break;
  2120. case ActionsBuilder.Type.FLOW_CONTROL:
  2121. return Raphael.rgb(185, 132, 140);
  2122. break;
  2123. case ActionsBuilder.Type.OBJECT:
  2124. case ActionsBuilder.Type.SCENE:
  2125. return Raphael.rgb(255, 255, 255);
  2126. break;
  2127. default: break;
  2128. }
  2129. return null;
  2130. };
  2131. /*
  2132. * Returns the selected node color according to the given parameters
  2133. * @param action: the action used to select the color
  2134. * @param detached: if the node is attached to its parent or not
  2135. */
  2136. Viewer.prototype.getSelectedNodeColor = function (type, detached) {
  2137. if (detached) {
  2138. return Raphael.rgb(96, 122, 14);
  2139. }
  2140. switch (type) {
  2141. case ActionsBuilder.Type.TRIGGER:
  2142. return Raphael.rgb(41, 129, 255);
  2143. break;
  2144. case ActionsBuilder.Type.ACTION:
  2145. return Raphael.rgb(255, 220, 42);
  2146. break;
  2147. case ActionsBuilder.Type.FLOW_CONTROL:
  2148. return Raphael.rgb(255, 41, 53);
  2149. break;
  2150. case ActionsBuilder.Type.OBJECT:
  2151. case ActionsBuilder.Type.SCENE:
  2152. return Raphael.rgb(255, 255, 255);
  2153. break;
  2154. default: break;
  2155. }
  2156. return null;
  2157. };
  2158. /*
  2159. * Removes the given action from the graph
  2160. * @param action: the action to remove
  2161. * @param removeChildren: if remove the branch or not
  2162. */
  2163. Viewer.prototype.removeAction = function (action, removeChildren) {
  2164. // If selected node is combine
  2165. if (action.parent !== null && action.parent.hub === action) {
  2166. this.removeAction(action.parent, false);
  2167. return;
  2168. }
  2169. // Basic suppress
  2170. this.removeNode(action.node);
  2171. if (action.combineArray !== null) {
  2172. this.removeNode(action.hub.node);
  2173. // Remove combine array
  2174. for (var i = 0; i < action.combineArray.length; i++) {
  2175. this.removeNode(action.combineArray[i].node);
  2176. }
  2177. action.combineArray.length = 0;
  2178. }
  2179. if (removeChildren) {
  2180. for (var i = 0; i < action.children.length; i++) {
  2181. this.removeAction(action.children[i], removeChildren);
  2182. }
  2183. action.clearChildren();
  2184. }
  2185. else {
  2186. for (var i = 0; i < action.children.length; i++) {
  2187. action.parent.addChild(action.children[i]);
  2188. action.children[i].parent = action.parent;
  2189. }
  2190. }
  2191. };
  2192. /*
  2193. * Removes the given node (not the action)
  2194. * @param node: the node to remove
  2195. */
  2196. Viewer.prototype.removeNode = function (node) {
  2197. node.rect.remove();
  2198. node.text.remove();
  2199. if (node.line !== null) {
  2200. node.line.remove();
  2201. }
  2202. };
  2203. /*
  2204. * Updates the graph viewer
  2205. */
  2206. Viewer.prototype.update = function () {
  2207. var _this = this;
  2208. // Set root position
  2209. this._setActionPosition(this.root, (this.paper.width / 2) - (Viewer.NODE_WIDTH / 2) * this.zoom, 10);
  2210. // Sets node size
  2211. var onSetNodeSize = function (node) {
  2212. node.rect.attr("width", node.minimized ? Viewer.NODE_MINIMIZED_WIDTH : Viewer.NODE_WIDTH * _this.zoom);
  2213. node.rect.attr("height", Viewer.NODE_HEIGHT * _this.zoom);
  2214. node.text.attr("font-size", 11 * _this.zoom);
  2215. };
  2216. // First pass: set actions positions according to parents
  2217. var onSetPositionPass = function (action, yPosition) {
  2218. var node = action.node;
  2219. var parent = action.parent !== null ? action.parent : null;
  2220. // Set node properties (size, text size, etc.)
  2221. if (action.combineArray !== null) {
  2222. for (var i = 0; i < action.combineArray.length; i++) {
  2223. var combinedNode = action.combineArray[i].node;
  2224. onSetNodeSize(combinedNode);
  2225. }
  2226. }
  2227. onSetNodeSize(node);
  2228. // Set position from parent
  2229. if (parent) {
  2230. var parentx = parent.node.rect.attr("x");
  2231. if (parent.combineArray !== null && parent.combineArray.length > 1) {
  2232. parentx += parent.node.rect.attr("width") / 2;
  2233. }
  2234. _this._setActionPosition(action, parentx, yPosition);
  2235. _this._setActionLine(action);
  2236. }
  2237. // Calculate total width for current action
  2238. var totalSize = 0;
  2239. for (var i = 0; i < action.children.length; i++) {
  2240. var childNode = action.children[i].node;
  2241. totalSize += childNode.rect.attr("width");
  2242. }
  2243. // Get values to place nodes according to the parent position
  2244. var nodeWidth = node.rect.attr("width");
  2245. var startingPositionX = node.rect.attr("x");
  2246. // Set children positions
  2247. for (var i = 0; i < action.children.length; i++) {
  2248. var childAction = action.children[i];
  2249. var childNode = childAction.node;
  2250. var newPositionX = startingPositionX;
  2251. if (childAction.combineArray !== null && childAction.combineArray.length > 1) {
  2252. newPositionX -= (childNode.rect.attr("width") / 2) - nodeWidth / 2;
  2253. }
  2254. var newPositionY = yPosition + Viewer.VERTICAL_OFFSET * _this.zoom;
  2255. onSetPositionPass(childAction, newPositionY);
  2256. _this._setActionPosition(childAction, newPositionX, newPositionY);
  2257. _this._setActionLine(childAction);
  2258. }
  2259. };
  2260. onSetPositionPass(this.root, 10 * this.zoom);
  2261. // Seconds pass, get sizes of groups
  2262. var onGetSizePass = function (action, maxSize) {
  2263. var mySize = 0;
  2264. if (action.combineArray !== null) {
  2265. for (var i = 0; i < action.combineArray.length; i++) {
  2266. mySize += action.combineArray[i].node.rect.attr("width");
  2267. }
  2268. }
  2269. else {
  2270. mySize = action.node.rect.attr("width");
  2271. }
  2272. if (mySize > maxSize) {
  2273. maxSize = mySize;
  2274. }
  2275. for (var i = 0; i < action.children.length; i++) {
  2276. maxSize = onGetSizePass(action.children[i], maxSize);
  2277. }
  2278. return maxSize;
  2279. };
  2280. // Resize canvas
  2281. var onResizeCanvas = function (action) {
  2282. var node = action.node;
  2283. var nodex = node.rect.attr("x");
  2284. var nodey = node.rect.attr("y");
  2285. if (nodex < 0 || nodex > _this.paper.width) {
  2286. _this.paper.setSize(_this.paper.width + 1000, _this.paper.height);
  2287. _this._setActionPosition(_this.root, (_this.paper.width / 2) - (Viewer.NODE_WIDTH / 2) * _this.zoom, 10);
  2288. }
  2289. if (nodey > _this.paper.height) {
  2290. _this.paper.setSize(_this.paper.width, _this.paper.height + 1000);
  2291. _this._setActionPosition(_this.root, (_this.paper.width / 2) - (Viewer.NODE_WIDTH / 2) * _this.zoom, 10);
  2292. }
  2293. };
  2294. var widths = new Array();
  2295. for (var i = 0; i < this.root.children.length; i++) {
  2296. var trigger = this.root.children[i];
  2297. var triggerResult = { triggerWidth: onGetSizePass(trigger, 0), childrenWidths: new Array() };
  2298. if (trigger.children.length > 0) {
  2299. triggerResult.triggerWidth = 0;
  2300. }
  2301. for (var j = 0; j < trigger.children.length; j++) {
  2302. var actionWidth = onGetSizePass(trigger.children[j], 0);
  2303. triggerResult.triggerWidth += actionWidth + 15;
  2304. triggerResult.childrenWidths.push({
  2305. triggerWidth: actionWidth,
  2306. childrenWidths: null
  2307. });
  2308. }
  2309. widths.push(triggerResult);
  2310. }
  2311. // Third pass, set positions of nodes
  2312. var onSetNodePosition = function (action, widthArray, isChild) {
  2313. var actionsCount = action.children.length;
  2314. var actionsMiddle = actionsCount % 2;
  2315. var actionsHasMiddle = actionsMiddle !== 0;
  2316. var actionsLeftOffset = 0;
  2317. var actionsRightOffset = 0;
  2318. var actionWidth = action.node.rect.attr("width");
  2319. if (actionsHasMiddle && actionsCount > 1) {
  2320. var middle = Math.floor(actionsCount / 2);
  2321. actionsLeftOffset += widthArray[middle].triggerWidth / 2;
  2322. actionsRightOffset += widthArray[middle].triggerWidth / 2;
  2323. }
  2324. // Move left
  2325. var leftStart = actionsHasMiddle ? Math.floor(actionsCount / 2) - 1 : (actionsCount / 2) - 1;
  2326. for (var i = leftStart; i >= 0; i--) {
  2327. var child = action.children[i];
  2328. var node = child.node;
  2329. var width = (widthArray[i].triggerWidth) + 15;
  2330. _this._setActionPosition(action.children[i], node.rect.attr("x") - actionsLeftOffset - (width / 2), node.rect.attr("y"));
  2331. _this._setActionLine(child);
  2332. onResizeCanvas(child);
  2333. actionsLeftOffset += width;
  2334. }
  2335. // Move right
  2336. var rightStart = actionsHasMiddle ? Math.round(actionsCount / 2) : actionsCount / 2;
  2337. for (var i = rightStart; i < actionsCount; i++) {
  2338. var child = action.children[i];
  2339. var node = child.node;
  2340. var width = (widthArray[i].triggerWidth) + 15;
  2341. _this._setActionPosition(action.children[i], node.rect.attr("x") + actionsRightOffset + (width / 2), node.rect.attr("y"));
  2342. _this._setActionLine(child);
  2343. onResizeCanvas(child);
  2344. actionsRightOffset += width;
  2345. }
  2346. };
  2347. onSetNodePosition(this.root, widths, false);
  2348. for (var i = 0; i < this.root.children.length; i++) {
  2349. onSetNodePosition(this.root.children[i], widths[i].childrenWidths, true);
  2350. }
  2351. };
  2352. /*
  2353. * Adds an action to the graph viewer and returns it
  2354. * @param parent: the parent action
  2355. * @param type: the action type
  2356. * @param element: the Actions Builder type (TRIGGERS, ACTIONS, FLOW_CONTROLS)
  2357. */
  2358. Viewer.prototype.addAction = function (parent, type, element) {
  2359. var node = this._createNode(element.text, type, parent === null);
  2360. var action = new ActionsBuilder.Action(node);
  2361. if (element.name === "CombineAction") {
  2362. action.combineArray = new Array();
  2363. var hubElement = ActionsBuilder.Elements.FLOW_CONTROLS[ActionsBuilder.Elements.FLOW_CONTROLS.length - 1];
  2364. var hub = this.addAction(action, ActionsBuilder.Type.FLOW_CONTROL, hubElement);
  2365. action.hub = hub;
  2366. action.addChild(hub);
  2367. this._createActionAnimation(hub);
  2368. }
  2369. action.name = element.name;
  2370. action.properties = element.properties;
  2371. action.type = type;
  2372. // Configure properties
  2373. for (var i = 0; i < action.properties.length; i++) {
  2374. action.propertiesResults.push({ targetType: action.properties[i].targetType, value: action.properties[i].value });
  2375. }
  2376. if (action.properties !== null && action.properties.length > 0) {
  2377. if (action.properties[0].text === "target") {
  2378. action.propertiesResults[0].value = this.objectName;
  2379. }
  2380. }
  2381. if (parent !== null) {
  2382. if (parent.combineArray === null) {
  2383. parent.addChild(action);
  2384. }
  2385. else if (parent.combineArray !== null && action.name !== "Hub") {
  2386. parent.combineArray.push(action);
  2387. action.parent = parent;
  2388. action.combineAction = parent;
  2389. parent.node.text.attr("text", "");
  2390. }
  2391. }
  2392. // Create animation
  2393. this._createActionAnimation(action);
  2394. return action;
  2395. };
  2396. /*
  2397. * Traverses the graph viewer and returns if an action
  2398. * is selected at coordinates (x, y)
  2399. * @param start: the start node. Can be null
  2400. * @param x: the x coordinate
  2401. * @param y: the y coordinate
  2402. * @param traverseCombine: if we traverse combine actions children
  2403. */
  2404. Viewer.prototype.traverseGraph = function (start, x, y, traverseCombine) {
  2405. if (start === null)
  2406. start = this.root;
  2407. var result = { action: start, hit: true };
  2408. if (start.node.isPointInside(x, y)) {
  2409. return result;
  2410. }
  2411. for (var i = 0; i < start.children.length; i++) {
  2412. var action = start.children[i];
  2413. if (action.node.isPointInside(x, y)) {
  2414. result.hit = true;
  2415. result.action = start.children[i];
  2416. if (traverseCombine && action.combineArray !== null) {
  2417. for (var j = 0; j < action.combineArray.length; j++) {
  2418. if (action.combineArray[j].node.isPointInside(x, y)) {
  2419. result.action = action.combineArray[j];
  2420. break;
  2421. }
  2422. }
  2423. }
  2424. return result;
  2425. }
  2426. result = this.traverseGraph(action, x, y, traverseCombine);
  2427. if (result.hit) {
  2428. return result;
  2429. }
  2430. }
  2431. result.hit = false;
  2432. result.action = null;
  2433. return result;
  2434. };
  2435. /*
  2436. * Sets the action's position (node)
  2437. * @param action: the action to place
  2438. * @param x: the x position of the action
  2439. * @param y: the y position of the action
  2440. */
  2441. Viewer.prototype._setActionPosition = function (action, x, y) {
  2442. var node = action.node;
  2443. var offsetx = node.rect.attr("x") - x;
  2444. var parent = action.parent;
  2445. if (parent !== null && parent.combineArray !== null && parent.combineArray.length > 1) {
  2446. var parentNode = parent.node;
  2447. x = parentNode.rect.attr("x") + (parent.node.rect.attr("width") / 2) - (node.rect.attr("width") / 2);
  2448. }
  2449. node.rect.attr("x", x);
  2450. node.rect.attr("y", y);
  2451. var textBBox = node.text.getBBox();
  2452. var textWidth = 0;
  2453. if (textBBox !== null && textBBox !== undefined) {
  2454. textWidth = textBBox.width;
  2455. }
  2456. node.text.attr("x", x + node.rect.attr("width") / 2 - textWidth / 2);
  2457. node.text.attr("y", y + node.rect.attr("height") / 2);
  2458. if (action.combineArray !== null && action.combineArray.length > 0) {
  2459. var length = 0;
  2460. for (var i = 0; i < action.combineArray.length; i++) {
  2461. var combinedAction = action.combineArray[i];
  2462. var combinedNode = combinedAction.node;
  2463. combinedNode.rect.attr("x", node.rect.attr("x") + length);
  2464. combinedNode.rect.attr("y", node.rect.attr("y"));
  2465. textBBox = combinedNode.text.getBBox();
  2466. if (textBBox !== null) {
  2467. textWidth = textBBox.width;
  2468. }
  2469. combinedNode.text.attr("x", combinedNode.rect.attr("x") + combinedNode.rect.attr("width") / 2 - textWidth / 2);
  2470. combinedNode.text.attr("y", y + combinedNode.rect.attr("height") / 2);
  2471. length += combinedNode.rect.attr("width");
  2472. }
  2473. node.rect.attr("width", length);
  2474. }
  2475. for (var i = 0; i < action.children.length; i++) {
  2476. var child = action.children[i];
  2477. this._setActionPosition(child, child.node.rect.attr("x") - offsetx, y + Viewer.VERTICAL_OFFSET * this.zoom);
  2478. this._setActionLine(child);
  2479. }
  2480. };
  2481. /*
  2482. * Configures the line (link) between the action and its parent
  2483. * @param action: the action to configure
  2484. */
  2485. Viewer.prototype._setActionLine = function (action) {
  2486. if (action.node.line === null) {
  2487. return;
  2488. }
  2489. var node = action.node;
  2490. var nodex = node.rect.attr("x");
  2491. var nodey = node.rect.attr("y");
  2492. var nodeWidth = node.rect.attr("width");
  2493. var nodeHeight = node.rect.attr("height");
  2494. var parent = action.parent.node;
  2495. var parentx = parent.rect.attr("x");
  2496. var parenty = parent.rect.attr("y");
  2497. var parentWidth = parent.rect.attr("width");
  2498. var parentHeight = parent.rect.attr("height");
  2499. if (node.detached) {
  2500. node.line.attr("path", ["M", nodex, nodey, "L", nodex, nodey]);
  2501. return;
  2502. }
  2503. var line1x = nodex + (nodeWidth / 2);
  2504. var line1y = nodey;
  2505. var line2y = line1y - (line1y - parenty - parentHeight) / 2;
  2506. var line3x = parentx + (parentWidth / 2);
  2507. var line4y = parenty + parentHeight;
  2508. node.line.attr("path", ["M", line1x, line1y, "L", line1x, line2y, "L", line3x, line2y, "L", line3x, line4y]);
  2509. };
  2510. /*
  2511. * Creates and returns a node
  2512. * @param text: the text to draw in the nde
  2513. * @param color: the node's color
  2514. * @param noLine: if draw a line to the parent or not
  2515. */
  2516. Viewer.prototype._createNode = function (text, type, noLine) {
  2517. var node = new ActionsBuilder.Node();
  2518. var color = this.getNodeColor(type, false);
  2519. node.rect = this.paper.rect(20, 20, Viewer.NODE_WIDTH, Viewer.NODE_HEIGHT, 0);
  2520. node.rect.attr("fill", color);
  2521. node.text = this.paper.text(20, 20, text);
  2522. node.text.attr("font-size", 11);
  2523. node.text.attr("text-anchor", "start");
  2524. node.text.attr("font-family", "Sinkin Sans Light");
  2525. if (!noLine) {
  2526. node.line = this.paper.path("");
  2527. node.line.attr("stroke", color);
  2528. }
  2529. return node;
  2530. };
  2531. /*
  2532. * Creates the drag animation
  2533. * @param action: the action to animate
  2534. */
  2535. Viewer.prototype._createActionAnimation = function (action) {
  2536. var _this = this;
  2537. var node = action.node;
  2538. var finished = true;
  2539. var nodex = 0;
  2540. var nodey = 0;
  2541. var onMove = function (dx, dy, x, y) { };
  2542. var onStart = function (x, y, event) {
  2543. if (node.minimized) {
  2544. return;
  2545. }
  2546. if (finished) {
  2547. nodex = node.rect.attr("x");
  2548. nodey = node.rect.attr("y");
  2549. }
  2550. finished = false;
  2551. node.rect.animate({
  2552. x: node.rect.attr("x") - 10,
  2553. y: node.rect.attr("y"),
  2554. width: (Viewer.NODE_WIDTH + 20) * _this.zoom,
  2555. height: (Viewer.NODE_HEIGHT + 10) * _this.zoom,
  2556. opacity: 0.25
  2557. }, 500, ">");
  2558. };
  2559. var onEnd = function (event) {
  2560. if (!node.minimized) {
  2561. node.rect.animate({
  2562. x: nodex,
  2563. y: nodey,
  2564. width: Viewer.NODE_WIDTH * _this.zoom,
  2565. height: Viewer.NODE_HEIGHT * _this.zoom,
  2566. opacity: 1.0
  2567. }, 500, ">", function () { finished = true; });
  2568. }
  2569. var dragResult = _this.traverseGraph(null, _this.mousex, _this.mousey, true);
  2570. if (dragResult.hit && dragResult.action === action || !dragResult.hit) {
  2571. // Create parameters. Action can be null
  2572. _this.parameters.createParameters(action);
  2573. }
  2574. else {
  2575. // Manage drag'n'drop
  2576. if (dragResult.action.children.length > 0 && action.type !== ActionsBuilder.Type.TRIGGER) {
  2577. return;
  2578. }
  2579. if (action.type === ActionsBuilder.Type.TRIGGER && dragResult.action !== _this.root) {
  2580. return;
  2581. }
  2582. if (action.type === ActionsBuilder.Type.ACTION && dragResult.action === _this.root) {
  2583. return;
  2584. }
  2585. if (action.type === ActionsBuilder.Type.FLOW_CONTROL && (dragResult.action === _this.root || dragResult.action.type === ActionsBuilder.Type.FLOW_CONTROL)) {
  2586. return;
  2587. }
  2588. if (action === dragResult.action.parent) {
  2589. return;
  2590. }
  2591. if (action.parent !== null && action.parent.combineArray !== null) {
  2592. return;
  2593. }
  2594. // Reset node
  2595. node.rect.stop(node.rect.animation);
  2596. node.text.stop(node.text.animation);
  2597. node.rect.undrag();
  2598. node.text.undrag();
  2599. node.rect.attr("opacity", 1.0);
  2600. node.rect.attr("width", Viewer.NODE_WIDTH);
  2601. node.rect.attr("height", Viewer.NODE_HEIGHT);
  2602. if (action.parent !== null) {
  2603. // Configure drag'n'drop
  2604. action.parent.removeChild(action);
  2605. dragResult.action.addChild(action);
  2606. _this.update();
  2607. _this._createActionAnimation(action);
  2608. }
  2609. }
  2610. };
  2611. node.rect.drag(onMove, onStart, onEnd);
  2612. node.text.drag(onMove, onStart, onEnd);
  2613. };
  2614. // Statics
  2615. Viewer._NODE_WIDTH = 150;
  2616. Viewer._NODE_HEIGHT = 25;
  2617. Viewer._NODE_MINIMIZE_WIDTH = 50;
  2618. Viewer._VERTICAL_OFFSET = 70;
  2619. Viewer._DEFAULT_INFO_MESSAGE = "Select or add a node to customize actions";
  2620. return Viewer;
  2621. })();
  2622. ActionsBuilder.Viewer = Viewer;
  2623. })(ActionsBuilder || (ActionsBuilder = {}));