actionsbuilder.max.js 121 KB

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