actionsbuilder.list.ts 12 KB

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