actionsbuilder.max.js 121 KB

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