actionsbuilder.list.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. var ActionsBuilder;
  2. (function (ActionsBuilder) {
  3. var ListElement = (function () {
  4. function ListElement() {
  5. this.rect = null;
  6. this.text = null;
  7. this.name = "";
  8. this.type = ActionsBuilder.Type.TRIGGER;
  9. this.element = null;
  10. }
  11. return ListElement;
  12. })();
  13. ActionsBuilder.ListElement = ListElement;
  14. var List = (function () {
  15. /**
  16. * Constructor
  17. */
  18. function List(viewer) {
  19. var _this = this;
  20. this._listElements = new Array();
  21. // Get HTML elements
  22. this.listElement = document.getElementById("ListsElementID");
  23. this.triggersElement = document.getElementById("TriggersListID");
  24. this.actionsElement = document.getElementById("ActionsListID");
  25. this.flowControlsElement = document.getElementById("FlowActionsListID");
  26. this._parentContainer = document.getElementById("ParentContainerID");
  27. // Configure this
  28. this._viewer = viewer;
  29. // Create elements (lists)
  30. this.triggersList = Raphael("TriggersListID", (25 * screen.width) / 100, 400);
  31. this.actionsList = Raphael("ActionsListID", (25 * screen.width) / 100, 400);
  32. this.flowControlsList = Raphael("FlowActionsListID", (25 * screen.width) / 100, 400);
  33. // Manage events
  34. window.addEventListener("resize", function (event) {
  35. _this.onResize(event);
  36. });
  37. }
  38. Object.defineProperty(List, "ELEMENT_HEIGHT", {
  39. get: function () {
  40. return 25;
  41. },
  42. enumerable: true,
  43. configurable: true
  44. });
  45. /**
  46. * Resize event that resizes the list element dynamically
  47. * @param event: the resize event
  48. */
  49. List.prototype.onResize = function (event) {
  50. var tools = document.getElementById("ToolsButtonsID");
  51. this.listElement.style.height = window.innerHeight - tools.getBoundingClientRect().height - 25 + "px";
  52. var listElementWidth = this.listElement.getBoundingClientRect().width;
  53. for (var i = 0; i < this._listElements.length; i++) {
  54. var rect = this._listElements[i].rect;
  55. rect.attr("width", listElementWidth - 40);
  56. }
  57. this.triggersList.setSize(listElementWidth, this.triggersList.height);
  58. this.actionsList.setSize(listElementWidth, this.triggersList.height);
  59. this.flowControlsList.setSize(listElementWidth, this.triggersList.height);
  60. };
  61. List.prototype.createListsElements = function () {
  62. var excludedTriggers = [6, 9, 10];
  63. var yPosition = 10;
  64. var textColor = Raphael.rgb(61, 72, 76);
  65. var whiteColor = Raphael.rgb(255, 255, 255);
  66. var configureTitle = function (listElement, rectColor) {
  67. listElement.text.attr("x", 15);
  68. listElement.rect.attr("fill", rectColor);
  69. listElement.text.attr("font-family", "Sinkin Sans Medium");
  70. listElement.text.attr("font-size", "11");
  71. };
  72. // Create triggers
  73. var triggers = this._createListElement(this.triggersList, yPosition, "TRIGGERS", ActionsBuilder.Type.TRIGGER, whiteColor, false);
  74. yPosition += List.ELEMENT_HEIGHT;
  75. configureTitle(triggers, Raphael.rgb(41, 129, 255));
  76. for (var i = 0; i < ActionsBuilder.Elements.TRIGGERS.length; i++) {
  77. var element = ActionsBuilder.Elements.TRIGGERS[i];
  78. if (this._viewer.root.type === ActionsBuilder.Type.OBJECT && excludedTriggers.indexOf(i) !== -1) {
  79. continue;
  80. }
  81. else if (this._viewer.root.type === ActionsBuilder.Type.SCENE && excludedTriggers.indexOf(i) === -1) {
  82. continue;
  83. }
  84. var trigger = this._createListElement(this.triggersList, yPosition, element.text, ActionsBuilder.Type.TRIGGER, textColor, true, element);
  85. trigger.rect.attr("fill", Raphael.rgb(133, 154, 185));
  86. yPosition += List.ELEMENT_HEIGHT;
  87. }
  88. yPosition += List.ELEMENT_HEIGHT;
  89. this.triggersElement.style.height = this.triggersList.canvas.style.height = yPosition + "px";
  90. this._createCollapseAnimation(this.triggersList, this.triggersElement, triggers, yPosition);
  91. // Create actions
  92. yPosition = 10;
  93. var actions = this._createListElement(this.actionsList, yPosition, "ACTIONS", ActionsBuilder.Type.ACTION, textColor, false);
  94. yPosition += List.ELEMENT_HEIGHT;
  95. configureTitle(actions, Raphael.rgb(255, 220, 42));
  96. for (var i = 0; i < ActionsBuilder.Elements.ACTIONS.length; i++) {
  97. var element = ActionsBuilder.Elements.ACTIONS[i];
  98. var action = this._createListElement(this.actionsList, yPosition, element.text, ActionsBuilder.Type.ACTION, textColor, true, element);
  99. action.rect.attr("fill", Raphael.rgb(182, 185, 132));
  100. yPosition += List.ELEMENT_HEIGHT;
  101. }
  102. yPosition += List.ELEMENT_HEIGHT;
  103. this.actionsElement.style.height = this.actionsList.canvas.style.height = yPosition + "px";
  104. this._createCollapseAnimation(this.actionsList, this.actionsElement, actions, yPosition);
  105. // Create flow controls
  106. yPosition = 10;
  107. var flowControls = this._createListElement(this.flowControlsList, yPosition, "FLOW CONTROLS", ActionsBuilder.Type.FLOW_CONTROL, whiteColor, false);
  108. yPosition += List.ELEMENT_HEIGHT;
  109. configureTitle(flowControls, Raphael.rgb(255, 41, 53));
  110. for (var i = 0; i < ActionsBuilder.Elements.FLOW_CONTROLS.length - 1; i++) {
  111. var element = ActionsBuilder.Elements.FLOW_CONTROLS[i];
  112. var flowControl = this._createListElement(this.flowControlsList, yPosition, element.text, ActionsBuilder.Type.FLOW_CONTROL, textColor, true, element);
  113. flowControl.rect.attr("fill", Raphael.rgb(185, 132, 140));
  114. yPosition += List.ELEMENT_HEIGHT;
  115. }
  116. yPosition += List.ELEMENT_HEIGHT;
  117. this.flowControlsElement.style.height = this.flowControlsList.canvas.style.height = yPosition + "px";
  118. this._createCollapseAnimation(this.flowControlsList, this.flowControlsElement, flowControls, yPosition);
  119. };
  120. /**
  121. * Clears the list of elements and removes the elements
  122. */
  123. List.prototype.clearLists = function () {
  124. for (var i = 0; i < this._listElements.length; i++) {
  125. this._removeListElement(this._listElements[i]);
  126. }
  127. this._listElements.splice(0, this._listElements.length - 1);
  128. };
  129. /**
  130. * Sets the color theme of the lists
  131. * @param color: the theme color
  132. */
  133. List.prototype.setColorTheme = function (color) {
  134. this.triggersList.canvas.style.backgroundColor = color;
  135. this.actionsList.canvas.style.backgroundColor = color;
  136. this.flowControlsList.canvas.style.backgroundColor = color;
  137. };
  138. /**
  139. * Creates a list element
  140. * @param paper: the Raphael.js paper
  141. * @param yPosition: the y position of the element
  142. * @param text: the element text
  143. * @param type: the element type (trigger, action, flow control)
  144. * @param textColor: the text color
  145. * @param drag: if the element should be drag'n'dropped
  146. */
  147. List.prototype._createListElement = function (paper, yPosition, text, type, textColor, drag, element) {
  148. var object = new ListElement();
  149. object.rect = paper.rect(10, yPosition, 300, List.ELEMENT_HEIGHT);
  150. object.text = paper.text(30, yPosition + object.rect.attr("height") / 2, text);
  151. object.text.attr("fill", textColor);
  152. object.text.attr("text-anchor", "start");
  153. object.text.attr("font-size", "12");
  154. object.text.attr("text-anchor", "start");
  155. object.text.attr("font-family", "Sinkin Sans Light");
  156. if (drag) {
  157. this._createListElementAnimation(object);
  158. }
  159. object.type = type;
  160. object.element = element;
  161. this._listElements.push(object);
  162. return object;
  163. };
  164. /**
  165. * Removes a list element
  166. * @param element: the element to remove
  167. */
  168. List.prototype._removeListElement = function (element) {
  169. element.rect.remove();
  170. element.text.remove();
  171. };
  172. /*
  173. * Creates the collapse animation of a list
  174. * @param paper: the list paper
  175. * @param htmlElement: the list div container
  176. * @param element: the list element to click on
  177. * @param expandedHeight: the height when the list is expanded
  178. */
  179. List.prototype._createCollapseAnimation = function (paper, htmlElement, element, expandedHeight) {
  180. var onClick = function (event) {
  181. var height = htmlElement.style.height;
  182. if (height === expandedHeight + "px") {
  183. htmlElement.style.height = paper.canvas.style.height = 35 + "px";
  184. }
  185. else {
  186. htmlElement.style.height = paper.canvas.style.height = expandedHeight + "px";
  187. }
  188. };
  189. element.rect.click(onClick);
  190. };
  191. /*
  192. * Creates the animation of a list element
  193. * @param element: the list element to animate
  194. */
  195. List.prototype._createListElementAnimation = function (element) {
  196. var _this = this;
  197. var onMove = function (dx, dy, x, y) { };
  198. var onStart = function (x, y, event) {
  199. _this._parentContainer.style.cursor = "copy";
  200. element.rect.animate({
  201. x: -10,
  202. opacity: 0.25
  203. }, 500, ">");
  204. element.text.animate({
  205. x: 10,
  206. opacity: 0.25
  207. }, 500, ">");
  208. };
  209. var onEnd = function (event) {
  210. _this._parentContainer.style.cursor = "default";
  211. element.rect.animate({
  212. x: 10,
  213. opacity: 1.0
  214. }, 500, "<");
  215. element.text.animate({
  216. x: 30,
  217. opacity: 1.0
  218. }, 500, "<");
  219. var dragResult = _this._viewer.traverseGraph(null, _this._viewer.mousex, _this._viewer.mousey, false);
  220. if (dragResult.hit) {
  221. if (element.type === ActionsBuilder.Type.TRIGGER && dragResult.action !== _this._viewer.root) {
  222. alert("Triggers can be dragged only on the root node (the mesh)");
  223. return;
  224. }
  225. if (element.type === ActionsBuilder.Type.ACTION && dragResult.action === _this._viewer.root) {
  226. alert("Please add a trigger before.");
  227. return;
  228. }
  229. if (element.type === ActionsBuilder.Type.FLOW_CONTROL && (dragResult.action === _this._viewer.root || (dragResult.action.type === ActionsBuilder.Type.FLOW_CONTROL && dragResult.action.parent.hub === null))) {
  230. return;
  231. }
  232. if (element.type === ActionsBuilder.Type.FLOW_CONTROL && dragResult.action.combineArray !== null) {
  233. alert("A condition cannot be handled by a Combine Action.");
  234. return;
  235. }
  236. if ((element.type === ActionsBuilder.Type.FLOW_CONTROL || element.type === ActionsBuilder.Type.ACTION) && dragResult.action.type === ActionsBuilder.Type.TRIGGER && dragResult.action.children.length > 0) {
  237. alert("Triggers can have only one child. Please add another trigger of same type.");
  238. return;
  239. }
  240. if (!(dragResult.action.combineArray !== null) && dragResult.action.children.length > 0 && dragResult.action.type !== ActionsBuilder.Type.TRIGGER && dragResult.action !== _this._viewer.root) {
  241. alert("An action can have only one child.");
  242. return;
  243. }
  244. _this._viewer.addAction(dragResult.action, element.type, element.element);
  245. _this._viewer.update();
  246. }
  247. };
  248. element.rect.drag(onMove, onStart, onEnd);
  249. element.text.drag(onMove, onStart, onEnd);
  250. };
  251. return List;
  252. })();
  253. ActionsBuilder.List = List;
  254. })(ActionsBuilder || (ActionsBuilder = {}));
  255. //# sourceMappingURL=actionsbuilder.list.js.map