actionsbuilder.max.js 117 KB

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