actionsbuilder.max.js 117 KB

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