CanvasController.java 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. package holeg.ui.controller;
  2. import holeg.model.AbstractCanvasObject;
  3. import holeg.model.Edge;
  4. import holeg.model.GroupNode;
  5. import holeg.ui.model.GuiSettings;
  6. import holeg.utility.math.vector.Vec2i;
  7. import java.util.Collection;
  8. import java.util.Set;
  9. import java.util.stream.Collectors;
  10. /**
  11. * Controller for the Canvas.
  12. *
  13. * @author Gruppe14
  14. */
  15. public class CanvasController {
  16. private final Control control;
  17. /**
  18. * Constructor.
  19. *
  20. * @param control the Controller
  21. */
  22. public CanvasController(Control control) {
  23. this.control = control;
  24. }
  25. /**
  26. * Add an CpsObject to the model and notify the ObjectListener for update.
  27. *
  28. * @param object CpsObject to be added.
  29. */
  30. public void addObject(GroupNode groupNode, AbstractCanvasObject object) {
  31. groupNode.add(object);
  32. }
  33. /**
  34. * Deletes an CpsObject on the Canvas and its connections.
  35. *
  36. * @param obj AbstractCpsObject
  37. */
  38. public void deleteObject(AbstractCanvasObject obj) {
  39. if (obj instanceof GroupNode groupNode) {
  40. deleteAllObjectsInGroupNode(groupNode);
  41. control.OnRemoveGroupNode.broadcast(groupNode);
  42. } else {
  43. removeAllConnectionsFromObject(obj);
  44. }
  45. obj.getGroupNode().ifPresent(groupNode -> groupNode.remove(obj));
  46. }
  47. public void deleteObjects(Collection<AbstractCanvasObject> objects) {
  48. objects.forEach(this::deleteObject);
  49. }
  50. /**
  51. * Replaces {@code toBeReplaced} by {@code by} on the canvas
  52. *
  53. * @param toBeReplaced the object that will be replaced
  54. * @param by the object that will replace it
  55. */
  56. public void replaceObject(AbstractCanvasObject toBeReplaced, AbstractCanvasObject by) {
  57. //Replace edges
  58. for (Edge edge : control.getModel().getEdgesOnCanvas()) {
  59. if (edge.getA() == toBeReplaced && edge.getB() != by) {
  60. if (control.doesEdgeExist(new Edge(by, edge.getB(), 0))) {
  61. continue;
  62. }
  63. edge.setA(by);
  64. } else if (edge.getB() == toBeReplaced && edge.getA() != by) {
  65. if (control.doesEdgeExist(new Edge(edge.getA(), by, 0))) {
  66. continue;
  67. }
  68. edge.setB(by);
  69. }
  70. }
  71. // set Position of by to exactly toBeReplaced
  72. by.setPosition(toBeReplaced.getPosition());
  73. deleteObject(toBeReplaced);
  74. }
  75. /**
  76. * Add an edge to the Canvas.
  77. *
  78. * @param edge the edge
  79. */
  80. public void addEdgeOnCanvas(Edge edge) {
  81. control.getModel().getEdgesOnCanvas().add(edge);
  82. }
  83. /**
  84. * Removes an Edge from the Canvas.
  85. *
  86. * @param edge the edge to remove
  87. */
  88. public void removeEdgesOnCanvas(Edge edge) {
  89. control.getModel().getEdgesOnCanvas().remove(edge);
  90. }
  91. /**
  92. * Some cleaning Algorithm which traverses the UpperNode through BFS Can be extended with other
  93. * cleaning stuff No need for coloring since there tree is only directed in one direction
  94. */
  95. public void deleteAllObjectsInGroupNode(GroupNode node) {
  96. Set<AbstractCanvasObject> objectsInGroupNode = node.getAllObjectsRecursive()
  97. .collect(Collectors.toSet());
  98. control.getModel().getEdgesOnCanvas().removeIf(
  99. edge -> objectsInGroupNode.contains(edge.getA()) || objectsInGroupNode.contains(
  100. edge.getB()));
  101. }
  102. public void removeAllConnectionsFromObject(AbstractCanvasObject obj) {
  103. control.getModel().getEdgesOnCanvas()
  104. .removeIf(edge -> edge.getA() == obj || edge.getB() == obj);
  105. }
  106. public void group(Set<AbstractCanvasObject> objects) {
  107. if (objects.isEmpty()) {
  108. return;
  109. }
  110. final GroupNode groupNode = new GroupNode("GroupNode");
  111. groupNode.setPosition(GroupNode.calculateMiddlePosition(objects));
  112. objects.stream().findFirst().flatMap(AbstractCanvasObject::getGroupNode)
  113. .ifPresent(parentGroupNode -> parentGroupNode.add(groupNode));
  114. objects.forEach(object -> object.getGroupNode().ifPresent(old -> old.remove(object)));
  115. groupNode.addAll(objects);
  116. }
  117. public void ungroup(Set<AbstractCanvasObject> objects) {
  118. for (AbstractCanvasObject obj : objects) {
  119. if (obj instanceof GroupNode groupNode) {
  120. Vec2i middle = GroupNode.calculateMiddlePosition(
  121. groupNode.getObjectsInThisLayer().collect(Collectors.toList()));
  122. groupNode.getObjectsInThisLayer().forEach(child -> {
  123. child.setPosition(groupNode.getPosition().subtract(middle).add(child.getPosition()));
  124. child.getPosition().clampX(0, GuiSettings.canvasSize.getX());
  125. child.getPosition().clampY(0, GuiSettings.canvasSize.getY());
  126. });
  127. groupNode.ungroup();
  128. control.OnRemoveGroupNode.broadcast(groupNode);
  129. }
  130. }
  131. }
  132. }