GroupNodeCanvas.java 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406
  1. package holeg.ui.view.canvas;
  2. import java.awt.BasicStroke;
  3. import java.awt.Color;
  4. import java.awt.Component;
  5. import java.awt.Graphics;
  6. import java.awt.Graphics2D;
  7. import java.awt.RenderingHints;
  8. import java.awt.event.MouseEvent;
  9. import java.util.Optional;
  10. import java.util.logging.Logger;
  11. import holeg.model.AbstractCanvasObject;
  12. import holeg.model.Edge;
  13. import holeg.model.GroupNode;
  14. import holeg.model.HolonObject;
  15. import holeg.model.HolonSwitch;
  16. import holeg.model.Node;
  17. import holeg.preferences.ColorPreference;
  18. import holeg.ui.controller.Control;
  19. import holeg.ui.model.Consumer;
  20. import holeg.ui.model.DecoratedGroupNode;
  21. import holeg.ui.model.DecoratedSwitch;
  22. import holeg.ui.model.ExitCable;
  23. import holeg.ui.model.GuiSettings;
  24. import holeg.ui.model.Model;
  25. import holeg.ui.model.Passiv;
  26. import holeg.ui.model.Supplier;
  27. import holeg.ui.model.VisualRepresentationalState;
  28. import holeg.ui.view.inspector.UnitGraph;
  29. import holeg.utility.ImageImport;
  30. import holeg.utility.Vector2Int;
  31. //TODO(Tom2021-12-1) delete GroupNodeCanvas completely and only have canvas Class
  32. public class GroupNodeCanvas extends Canvas {
  33. private static final Logger log = Logger.getLogger(GroupNodeCanvas.class.getName());
  34. private String parentPath;
  35. private Component parentComponent;
  36. public GroupNodeCanvas(Model mod, Control control, UnitGraph unitGraph, GroupNode groupNode, String parentPath,
  37. Component parentComponent) {
  38. super(mod, control, unitGraph);
  39. this.groupNode = groupNode;
  40. this.parentPath = parentPath;
  41. this.parentComponent = parentComponent;
  42. }
  43. public void setGroupNode(GroupNode upperNode) {
  44. this.groupNode = upperNode;
  45. }
  46. public GroupNode getGroupNode() {
  47. return groupNode;
  48. }
  49. public String getParentPath() {
  50. return parentPath;
  51. }
  52. public Component getParentComponent() {
  53. return parentComponent;
  54. }
  55. public void paintComponent(Graphics g) {
  56. // super.paintComponent(g);
  57. Graphics2D g2d = (Graphics2D) g;
  58. g2d.clearRect(0, 0, this.getWidth(), this.getHeight());
  59. g2d.setRenderingHints(new RenderingHints(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON));
  60. // -->Old code
  61. if (drawEdge) {
  62. g2d.setColor(Color.BLACK);
  63. g2d.setStroke(new BasicStroke(1));
  64. g2d.drawLine(tempCps.getPosition().getX(), tempCps.getPosition().getY(), x, y);
  65. }
  66. // <--
  67. // SelectedCable
  68. Optional<VisualRepresentationalState> visState = control.getSimManager()
  69. .getActualVisualRepresentationalState();
  70. if (visState.isEmpty()) {
  71. return;
  72. }
  73. DecoratedGroupNode actualGroupNode = visState.get().getCreatedGroupNodes().get(groupNode);
  74. // VisualState Representation:
  75. for (ExitCable cable : actualGroupNode.getExitCableList()) {
  76. paintExitCable(g2d, cable);
  77. }
  78. for (Edge cable : actualGroupNode.getInternCableList()) {
  79. paintCable(g2d, cable, GuiSettings.getSelectedEdges().contains(cable));
  80. }
  81. for (DecoratedGroupNode dGroupNode : actualGroupNode.getGroupNodeList()) {
  82. paintGroupNode(g2d, dGroupNode);
  83. }
  84. for (Consumer con : actualGroupNode.getConsumerList()) {
  85. paintConsumer(g2d, con);
  86. }
  87. for (Supplier sup : actualGroupNode.getSupplierList()) {
  88. paintSupplier(g2d, sup);
  89. }
  90. for (Passiv pas : actualGroupNode.getPassivList()) {
  91. paintCanvasObject(g2d, pas);
  92. }
  93. for (DecoratedSwitch dSwitch : actualGroupNode.getSwitchList()) {
  94. paintSwitch(g2d, dSwitch);
  95. }
  96. for (Node node : actualGroupNode.getNodeList()) {
  97. drawCanvasObject(g2d, "/Images/node.png", node.getPosition());
  98. }
  99. // -->oldCode
  100. if (doMark) {
  101. g2d.setColor(Color.BLACK);
  102. g2d.setStroke(new BasicStroke(0));
  103. drawMarker(g2d);
  104. }
  105. // Test Selection
  106. // Objects:
  107. g2d.setStroke(new BasicStroke(1));
  108. Color transparentGrey = ColorPreference.Panel.ObjectSelection;
  109. for (AbstractCanvasObject aCps : GuiSettings.getSelectedObjects()) {
  110. if (aCps instanceof Node) {
  111. Vector2Int pos = aCps.getPosition();
  112. g2d.setColor(transparentGrey);
  113. g2d.fillOval(pos.getX() - (int) (GuiSettings.getPictureScaleDiv2()),
  114. pos.getY() - (int) (GuiSettings.getPictureScaleDiv2()), GuiSettings.getPictureScale(), GuiSettings.getPictureScale());
  115. g2d.setColor(ColorPreference.Panel.ObjectSelectionBorder);
  116. g2d.setStroke(new BasicStroke(2));
  117. g2d.drawOval(pos.getX() - (int) (GuiSettings.getPictureScaleDiv2()),
  118. pos.getY() - (int) (GuiSettings.getPictureScaleDiv2()), GuiSettings.getPictureScale(), GuiSettings.getPictureScale());
  119. } else {
  120. Vector2Int pos = aCps.getPosition();
  121. g2d.setColor(transparentGrey);
  122. g2d.fillRect(pos.getX() - (int) (GuiSettings.getPictureScaleDiv2() * 1.5f),
  123. pos.getY() - (int) (GuiSettings.getPictureScaleDiv2() * 1.5f), (int) (GuiSettings.getPictureScale() * 1.5f),
  124. (int) (GuiSettings.getPictureScale() * 1.5f));
  125. g2d.setColor(ColorPreference.Panel.ObjectSelectionBorder);
  126. g2d.setStroke(new BasicStroke(2));
  127. g2d.drawRect(pos.getX() - (int) (GuiSettings.getPictureScaleDiv2() * 1.5f),
  128. pos.getY() - (int) (GuiSettings.getPictureScaleDiv2() * 1.5f), (int) (GuiSettings.getPictureScale() * 1.5f),
  129. (int) (GuiSettings.getPictureScale() * 1.5f));
  130. }
  131. }
  132. // maybeReplace:
  133. if (mayBeReplaced != null) {
  134. Vector2Int pos = mayBeReplaced.getPosition();
  135. g.drawImage(ImageImport.loadImage("/Images/replace.png"), pos.getX() + GuiSettings.getPictureScaleDiv2(),
  136. pos.getY() - GuiSettings.getPictureScale(), GuiSettings.getPictureScaleDiv2(), GuiSettings.getPictureScaleDiv2(), null);
  137. }
  138. // <-- OldCode
  139. }
  140. @Override
  141. public void mousePressed(MouseEvent e) {
  142. this.grabFocus();
  143. if (!disabled) {
  144. tempCps = null;
  145. dataSelected = null;
  146. edgeHighlight = null;
  147. GuiSettings.getSelectedEdges().clear();
  148. // Object Selection
  149. if (e.getX() > 0) {
  150. for (AbstractCanvasObject cps : groupNode.getObjectsInThisLayer().toList()) {
  151. cx = cps.getPosition().getX() - GuiSettings.getPictureScaleDiv2();
  152. cy = cps.getPosition().getY() - GuiSettings.getPictureScaleDiv2();
  153. if (x - GuiSettings.getPictureScale() <= cx && y - GuiSettings.getPictureScale() <= cy && x >= cx && y >= cy) {
  154. tempCps = cps;
  155. dragging = true;
  156. if (e.isControlDown() && tempCps != null) {
  157. log.info("Add Remove Selection GROUPNODECANVAS");
  158. if (GuiSettings.getSelectedObjects().contains(tempCps)) {
  159. control.removeObjectFromSelection(tempCps);
  160. } else {
  161. control.addSelectedObject(tempCps);
  162. if (tempCps instanceof GroupNode)
  163. control.getObjectsInDepth();
  164. }
  165. }
  166. // If drawing an Edge (CTRL down)
  167. if (tempCps.getClass() == HolonObject.class) {
  168. HolonObject tempObj = ((HolonObject) tempCps);
  169. dataSelected = tempObj.getElements();
  170. }
  171. if (e.isShiftDown()) {
  172. drawEdge = true;
  173. dragging = false;
  174. }
  175. break;
  176. }
  177. }
  178. }
  179. // Edge Selection
  180. if (e.getButton() == MouseEvent.BUTTON1) {
  181. if (tempCps == null) {
  182. edgeHighlight = mousePositionOnEdge(x, y);
  183. GuiSettings.getSelectedEdges().add(edgeHighlight);
  184. if (!e.isControlDown() && e.getButton() != MouseEvent.BUTTON3) {
  185. control.clearSelection();
  186. }
  187. }
  188. if (edgeHighlight == null && tempCps == null) {
  189. sx = e.getX();
  190. sy = e.getY();
  191. doMark = true;
  192. }
  193. repaint();
  194. }
  195. }
  196. }
  197. @Override
  198. public void mouseReleased(MouseEvent e) {
  199. if (!disabled) {
  200. x = e.getX();
  201. y = e.getY();
  202. dragging = false;
  203. if (drawEdge) {
  204. drawEdge = false;
  205. drawDeleteEdge();
  206. }
  207. if (dragged) {
  208. control.tryAutoSave();
  209. /**
  210. * check if tempCps could replace an Object on the UpperNodeanvas
  211. */
  212. if (GuiSettings.getSelectedObjects().size() == 1 && checkForReplacement(groupNode.getObjectsInThisLayer().toList(), tempCps,
  213. tempCps.getPosition().getX(), tempCps.getPosition().getY())) {
  214. /**
  215. * if UpperNode would be replaced, close its tabs
  216. */
  217. if (mayBeReplaced instanceof GroupNode)
  218. closeUpperNodeTab(mayBeReplaced.getId());
  219. /**
  220. * replace on canvas
  221. */
  222. control.replaceObjectInGroupNode(mayBeReplaced, tempCps, groupNode);
  223. mayBeReplaced = null;
  224. }
  225. }
  226. if (!e.isControlDown() && !dragged && tempCps != null && MouseEvent.BUTTON3 != e.getButton()) {
  227. control.clearSelection();
  228. control.addSelectedObject(tempCps);
  229. if (tempCps instanceof GroupNode)
  230. control.getObjectsInDepth();
  231. }
  232. dragged = false;
  233. // Rightclick List
  234. setRightClickMenu(e);
  235. markObjects();
  236. boolean doubleclick = doubleClick();
  237. if (doubleclick && tempCps != null && tempCps instanceof HolonSwitch sw) {
  238. sw.switchState();
  239. }
  240. if (doubleclick && tempCps != null && tempCps instanceof GroupNode groupnode) {
  241. control.getGui().openNewUpperNodeTab(groupnode);
  242. }
  243. control.calculateStateAndVisualForTimeStep(model.getCurrentIteration());
  244. repaint();
  245. }
  246. }
  247. @Override
  248. public void mouseDragged(MouseEvent e) {
  249. if (!disabled) {
  250. // If Edge is drawn
  251. x = e.getX();
  252. y = e.getY();
  253. if (!GuiSettings.getSelectedObjects().contains(tempCps) && !doMark) {
  254. control.clearSelection();
  255. if (tempCps != null) {
  256. control.addSelectedObject(tempCps);
  257. }
  258. }
  259. if (dragging) {
  260. try {
  261. // tempCps in the upperNode? else its a connected Object from
  262. // outside
  263. if (groupNode.getObjectsInThisLayer().anyMatch(obj -> obj.equals(tempCps))) {
  264. dragged = true;
  265. float xDist, yDist; // Distance
  266. x = e.getX();
  267. y = e.getY();
  268. // Make sure its in bounds
  269. if (e.getX() < GuiSettings.getPictureScaleDiv2() + +5)
  270. x = GuiSettings.getPictureScaleDiv2() + +5;
  271. else if (e.getX() > this.getWidth() - GuiSettings.getPictureScaleDiv2())
  272. x = this.getWidth() - GuiSettings.getPictureScaleDiv2();
  273. if (e.getY() < GuiSettings.getPictureScaleDiv2())
  274. y = GuiSettings.getPictureScaleDiv2();
  275. else if (e.getY() > this.getHeight() - GuiSettings.getPictureScaleDiv2())
  276. y = this.getHeight() - GuiSettings.getPictureScaleDiv2();
  277. // Distance
  278. xDist = x - tempCps.getPosition().getX();
  279. yDist = y - tempCps.getPosition().getY();
  280. tempCps.setPosition(x, y); // Drag Position
  281. // TipText Position and name
  282. toolTip = true;
  283. toolTipText = tempCps.getName() + ", " + tempCps.getId();
  284. toolTipPos.setX(tempCps.getPosition().getX() - GuiSettings.getPictureScaleDiv2());
  285. toolTipPos.setY(tempCps.getPosition().getY() - GuiSettings.getPictureScaleDiv2());
  286. // All Selected Objects
  287. for (AbstractCanvasObject cps : GuiSettings.getSelectedObjects()) {
  288. if (cps != tempCps) {
  289. x = (int) (cps.getPosition().getX() + xDist);
  290. y = (int) (cps.getPosition().getY() + yDist);
  291. // Make sure its in bounds
  292. if (x < +5 + GuiSettings.getPictureScaleDiv2())
  293. x = GuiSettings.getPictureScaleDiv2() + +5;
  294. else if (x > this.getWidth() - GuiSettings.getPictureScaleDiv2())
  295. x = this.getWidth() - GuiSettings.getPictureScaleDiv2();
  296. if (y <= GuiSettings.getPictureScaleDiv2())
  297. y = GuiSettings.getPictureScaleDiv2();
  298. else if (y > this.getHeight() - GuiSettings.getPictureScaleDiv2())
  299. y = this.getHeight() - GuiSettings.getPictureScaleDiv2();
  300. cps.setPosition(x, y);
  301. }
  302. }
  303. }
  304. /**
  305. * check if something would be replaced
  306. */
  307. if (GuiSettings.getSelectedObjects().size() == 1)
  308. checkForReplacement(groupNode.getObjectsInThisLayer().toList(), tempCps, x, y);
  309. repaint();
  310. } catch (Exception eex) {
  311. }
  312. }
  313. // Mark Objects
  314. if (doMark) {
  315. tempSelected.clear();
  316. groupNode.getObjectsInThisLayer().forEach(cps ->
  317. {
  318. int x1 = sx, x2 = x, y1 = sy, y2 = y;
  319. if (sx >= x) {
  320. x1 = x;
  321. x2 = sx;
  322. }
  323. if (sy >= y) {
  324. y1 = y;
  325. y2 = sy;
  326. }
  327. if (x1 <= cps.getPosition().getX() + GuiSettings.getPictureScaleDiv2()
  328. && y1 <= cps.getPosition().getY() + GuiSettings.getPictureScaleDiv2() && x2 >= cps.getPosition().getX()
  329. && y2 >= cps.getPosition().getY()) {
  330. tempSelected.add(cps);
  331. }
  332. });
  333. }
  334. repaint();
  335. }
  336. }
  337. @Override
  338. public void mouseMoved(MouseEvent e) {
  339. {
  340. x = e.getX();
  341. y = e.getY();
  342. // Everything for the tooltip :)
  343. boolean on = false;
  344. Optional<AbstractCanvasObject> objectUnderCursor = groupNode.getObjectsInThisLayer().filter(cps -> {
  345. cx = cps.getPosition().getX() - GuiSettings.getPictureScaleDiv2();
  346. cy = cps.getPosition().getY() - GuiSettings.getPictureScaleDiv2();
  347. boolean mouseOverObject = x - GuiSettings.getPictureScale() <= cx && y - GuiSettings.getPictureScale() <= cy && x >= cx
  348. && y >= cy;
  349. return mouseOverObject;
  350. }).findAny();
  351. on |= objectUnderCursor.isPresent();
  352. objectUnderCursor.ifPresent(cps -> {
  353. toolTipPos.setX(cps.getPosition().getX() - GuiSettings.getPictureScaleDiv2());
  354. toolTipPos.setY(cps.getPosition().getY() + GuiSettings.getPictureScaleDiv2());
  355. toolTipText = cps.getName() + ", " + cps.getId();
  356. });
  357. toolTip = on;
  358. repaint();
  359. }
  360. }
  361. }