Rendering.java 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. package holeg.ui.view.canvas;
  2. import holeg.model.*;
  3. import holeg.preferences.ColorPreference;
  4. import holeg.preferences.ImagePreference;
  5. import holeg.ui.model.GuiSettings;
  6. import holeg.ui.view.image.Import;
  7. import holeg.utility.math.vector.Vec2i;
  8. import java.awt.*;
  9. class Rendering {
  10. private static final RenderingHints RenderingHint = new RenderingHints(RenderingHints.KEY_ANTIALIASING,
  11. RenderingHints.VALUE_ANTIALIAS_ON);
  12. private static final Font CanvasFont = new Font("TimesNewRoman", Font.PLAIN,
  13. Math.max((int) (GuiSettings.getPictureScale() / 3.5f), 10));
  14. private static final Color[] colors = {ColorPreference.HolonObject.Producer, ColorPreference.HolonObject.NotSupplied,
  15. ColorPreference.HolonObject.PartiallySupplied, ColorPreference.HolonObject.Supplied,
  16. ColorPreference.HolonObject.OverSupplied, ColorPreference.HolonObject.NoEnergy};
  17. private static final BasicStroke OnePixelStroke = new BasicStroke(1);
  18. private static final BasicStroke TwoPixelStroke = new BasicStroke(2);
  19. private static final Dimension SupplyBarDimensions = new Dimension(GuiSettings.getPictureScale(), GuiSettings.getPictureScale() / 5);
  20. private static final Font SupplyBarFont = new Font("TimesNewRoman", Font.PLAIN, (int) (GuiSettings.getPictureScale() * 0.3) - 2);
  21. private static final Dimension LeavingEdgeTunnelSize = new Dimension(20, 20);
  22. static Graphics2D initGraphics2D(Graphics g) {
  23. Graphics2D g2d = (Graphics2D) g;
  24. g2d.setRenderingHints(RenderingHint);
  25. g2d.setFont(CanvasFont);
  26. return g2d;
  27. }
  28. static void drawSwitchObject(Graphics2D g, HolonSwitch hS) {
  29. drawCanvasObject(g, hS);
  30. }
  31. static void drawHolonObject(Graphics2D g, HolonObject hO) {
  32. Vec2i pos = hO.getPosition();
  33. Color stateColor = ColorPreference.HolonObject.getStateColor(hO.getState());
  34. g.setColor(stateColor);
  35. g.fillRect(pos.getX() - GuiSettings.getPictureScaleDiv2(), pos.getY() - GuiSettings.getPictureScaleDiv2(),
  36. GuiSettings.getPictureScale(), GuiSettings.getPictureScale());
  37. drawCanvasObject(g, hO.getImagePath(), pos);
  38. if(GuiSettings.showSupplyBars && hO.getActualEnergy() <= 0){
  39. drawSupplyBar(g, hO.getSupplyBarPercentage(), stateColor, pos);
  40. }
  41. }
  42. static void drawCanvasObject(Graphics2D g, AbstractCanvasObject obj) {
  43. drawCanvasObject(g, obj.getImagePath(), obj.getPosition());
  44. }
  45. static void drawCanvasObject(Graphics2D g, String imageName, Vec2i pos) {
  46. int pictureScale = GuiSettings.getPictureScale();
  47. int pictureScaleDiv2 = GuiSettings.getPictureScaleDiv2();
  48. Image image = Import.loadImage(imageName, pictureScale, pictureScale);
  49. g.drawImage(image, pos.getX() - pictureScaleDiv2, pos.getY() - pictureScaleDiv2, pictureScale, pictureScale,
  50. null);
  51. }
  52. static void drawNode(Graphics2D g, Node node) {
  53. Vec2i pos = node.getPosition();
  54. drawCanvasObject(g, ImagePreference.Canvas.Node.Unselected, pos);
  55. }
  56. static void drawEdge(Graphics2D g, Edge edge, AbstractCanvasObject a, AbstractCanvasObject b) {
  57. Vec2i start = a.getPosition();
  58. Vec2i end = b.getPosition();
  59. float currentEnergy = edge.getActualFlow();
  60. float capacity = edge.maxCapacity;
  61. boolean unlimited = edge.mode == Edge.EdgeMode.Unlimited;
  62. switch (edge.getState()) {
  63. case Burned -> {
  64. g.setColor(ColorPreference.Edge.Burned);
  65. g.setStroke(TwoPixelStroke);
  66. }
  67. case Working -> {
  68. g.setColor(ColorPreference.Edge.Working);
  69. g.setStroke(new BasicStroke(unlimited ? 2f : (currentEnergy / capacity * 2f) + 1));
  70. }
  71. }
  72. g.drawLine(start.getX(), start.getY(), end.getX(), end.getY());
  73. Vec2i middle = new Vec2i((start.getX() + end.getX()) / 2, (start.getY() + end.getY()) / 2);
  74. g.drawString(currentEnergy + "/" + (unlimited ? "\u221E" : capacity), middle.getX(), middle.getY());
  75. }
  76. static void drawExternConnection(Graphics2D g, AbstractCanvasObject object) {
  77. Vec2i pos = object.getPosition();
  78. Dimension size = new Dimension(16, 16);
  79. Image image = Import.loadImage(ImagePreference.Canvas.ExternSymbol, size.width, size.height);
  80. g.drawImage(image, pos.getX() + GuiSettings.getPictureScaleDiv2(), pos.getY() - GuiSettings.getPictureScaleDiv2() - size.height,
  81. size.width, size.height, null);
  82. }
  83. static void drawNewEdgeLine(Graphics2D g, Vec2i start, Vec2i end){
  84. g.setStroke(TwoPixelStroke);
  85. g.setColor(ColorPreference.Edge.Working);
  86. g.drawLine(start.getX(), start.getY(), end.getX(), end.getY());
  87. }
  88. static void drawGroupNode(Graphics2D g, GroupNode groupNode) {
  89. Vec2i pos = groupNode.getPosition();
  90. g.setColor(Color.gray);
  91. g.fillRect(pos.getX() - GuiSettings.getPictureScaleDiv2(), pos.getY() - GuiSettings.getPictureScaleDiv2(),
  92. GuiSettings.getPictureScale(), GuiSettings.getPictureScale());
  93. drawCanvasObject(g, groupNode.getImagePath(), pos);
  94. }
  95. static void drawSelection(Graphics2D g) {
  96. g.setStroke(OnePixelStroke);
  97. for (AbstractCanvasObject aCps : GuiSettings.getSelectedObjects()) {
  98. Vec2i pos = aCps.getPosition();
  99. if (aCps instanceof Node) {
  100. g.setColor(ColorPreference.Canvas.ObjectSelectionFill);
  101. g.fillOval(pos.getX() - (GuiSettings.getPictureScaleDiv2()),
  102. pos.getY() - (GuiSettings.getPictureScaleDiv2()), GuiSettings.getPictureScale(), GuiSettings.getPictureScale());
  103. g.setColor(ColorPreference.Canvas.ObjectSelectionBorder);
  104. g.drawOval(pos.getX() - (GuiSettings.getPictureScaleDiv2()),
  105. pos.getY() - (GuiSettings.getPictureScaleDiv2()), GuiSettings.getPictureScale(), GuiSettings.getPictureScale());
  106. } else {
  107. g.setColor(ColorPreference.Canvas.ObjectSelectionFill);
  108. g.fillRect(pos.getX() - (int) (GuiSettings.getPictureScaleDiv2() * 1.5f),
  109. pos.getY() - (int) (GuiSettings.getPictureScaleDiv2() * 1.5f), (int) (GuiSettings.getPictureScale() * 1.5f),
  110. (int) (GuiSettings.getPictureScale() * 1.5f));
  111. g.setColor(ColorPreference.Canvas.ObjectSelectionBorder);
  112. g.drawRect(pos.getX() - (int) (GuiSettings.getPictureScaleDiv2() * 1.5f),
  113. pos.getY() - (int) (GuiSettings.getPictureScaleDiv2() * 1.5f), (int) (GuiSettings.getPictureScale() * 1.5f),
  114. (int) (GuiSettings.getPictureScale() * 1.5f));
  115. }
  116. }
  117. }
  118. static void drawSelectionBox(Graphics2D g, Rectangle selectionBox) {
  119. g.setStroke(OnePixelStroke);
  120. g.setColor(ColorPreference.Canvas.MouseSelectionBorder);
  121. g.draw(selectionBox);
  122. g.setColor(ColorPreference.Canvas.MouseSelectionFill);
  123. g.fill(selectionBox);
  124. }
  125. //protected void drawCanvasObjectString(Graphics2D g, Vec2i posOfCanvasObject, float energy) {
  126. // g.setColor(Color.BLACK);
  127. // g.setFont(new Font("TimesNewRoman", Font.PLAIN, (int) (GuiSettings.getPictureScale() / 4f)));
  128. // g.drawString((energy > 0) ? "+" + Float.toString(energy) : Float.toString(energy),
  129. // posOfCanvasObject.getX() - GuiSettings.getPictureScaleDiv2(),
  130. // posOfCanvasObject.getY() - GuiSettings.getPictureScaleDiv2() - 1);
  131. //}
  132. //
  133. //protected void paintConsumer(Graphics2D g, Consumer con) {
  134. // paintCanvasObject(g, con);
  135. // drawCanvasObjectString(g, con.getModel().getPosition(), -con.getEnergyNeededFromNetwork());
  136. // if (GuiSettings.showSupplyBars) {
  137. // paintSupplyBar(g, con.getSupplyBarPercentage(), ColorPreference.HolonObject.getStateColor(con.getState()),
  138. // con.getModel().getPosition());
  139. // }
  140. //}
  141. //
  142. //protected void paintSupplier(Graphics2D g, Supplier sup) {
  143. // paintCanvasObject(g, sup);
  144. // drawCanvasObjectString(g, sup.getModel().getPosition(), sup.getEnergyToSupplyNetwork());
  145. //}
  146. //
  147. //protected void drawCanvasObject(Graphics2D g, String Image, Vec2i pos) {
  148. // g.drawImage(Import.loadImage(Image, GuiSettings.getPictureScale(), GuiSettings.getPictureScale()),
  149. // pos.getX() - GuiSettings.getPictureScaleDiv2(), pos.getY() - GuiSettings.getPictureScaleDiv2(), GuiSettings.getPictureScale(),
  150. // GuiSettings.getPictureScale(), null);
  151. //}
  152. //
  153. //protected void paintCable(Graphics2D g, Edge cable, boolean isSelected) {
  154. // Vec2i start = cable.getA().getPosition();
  155. // Vec2i end = cable.getB().getPosition();
  156. // float currentEnergy = cable.getActualFlow();
  157. // float capacity = cable.maxCapacity;
  158. // boolean unlimited = cable.isUnlimitedCapacity();
  159. // switch (cable.getState()) {
  160. // case Burned:
  161. // g.setColor(ColorPreference.Edge.Burned);
  162. // g.setStroke(new BasicStroke(2));
  163. // break;
  164. // case Working:
  165. // g.setColor(ColorPreference.Edge.Working);
  166. // g.setStroke(new BasicStroke(unlimited ? 2f : (currentEnergy / capacity * 2f) + 1));
  167. // break;
  168. // }
  169. // if (isSelected) {
  170. // g.setColor(Color.lightGray);
  171. // }
  172. // g.drawLine(start.getX(), start.getY(), end.getX(), end.getY());
  173. // if (showConnectionInformation) {
  174. // Vec2i middle = new Vec2i((start.getX() + end.getX()) / 2, (start.getY() + end.getY()) / 2);
  175. // g.setFont(new Font("TimesRoman", Font.PLAIN, Math.max((int) (GuiSettings.getPictureScale() / 3.5f), 10)));
  176. // g.drawString(currentEnergy + "/" + (unlimited ? "\u221E" : capacity), middle.getX(), middle.getY());
  177. // }
  178. //}
  179. //protected void paintExitCable(Graphics2D g, ExitCable eCable) {
  180. // Vec2i start = eCable.getStart().getPosition();
  181. // Vec2i end = eCable.getFinish().getPosition();
  182. // float currentEnergy;
  183. // float capacity = eCable.getEdge().maxCapacity;
  184. // boolean unlimited = eCable.getEdge().isUnlimitedCapacity();
  185. // if(eCable.getEdge().getState() == null) {
  186. // System.err.print(eCable.getEdge());
  187. // }
  188. // switch (eCable.getEdge().getState()) {
  189. // case Burned:
  190. // currentEnergy = 0.0f;
  191. // g.setColor(Color.RED);
  192. // g.setStroke(new BasicStroke(2));
  193. // break;
  194. // case Working:
  195. // default:
  196. // currentEnergy = eCable.getEdge().getActualFlow();
  197. // g.setColor(new Color(13, 175, 28));
  198. // g.setStroke(new BasicStroke(unlimited ? 2f : (currentEnergy / capacity * 2f) + 1));
  199. // break;
  200. // }
  201. // g.drawLine(start.getX(), start.getY(), end.getX(), end.getY());
  202. // Vec2i middle = new Vec2i((start.getX() + end.getX()) / 2, (start.getY() + end.getY()) / 2);
  203. // g.setFont(new Font("TimesRoman", Font.PLAIN, Math.max((int) (GuiSettings.getPictureScale() / 3.5f), 10)));
  204. // g.drawString(currentEnergy + "/" + (unlimited ? "\u221E" : capacity), middle.getX(), middle.getY());
  205. //}
  206. //private void paintGroupNodeBar(Graphics2D g, DecoratedGroupNode dGroupNode, Vec2i pos) {
  207. // // +1, -2, -1 little Adjustment for pixel perfect alignment
  208. // int barWidth = (int) (GuiSettings.getPictureScale());
  209. // int barHeight = (int) (GuiSettings.getPictureScale() / 5);
  210. // g.setColor(Color.WHITE);
  211. // g.fillRect(pos.getX() - GuiSettings.getPictureScaleDiv2(), pos.getY() + GuiSettings.getPictureScaleDiv2() - 1, (int) barWidth,
  212. // barHeight);
  213. // float[] percentages = getGroupNodeBarPercentages(dGroupNode);
  214. //
  215. // for (int i = 5; i >= 0; i--) {
  216. // g.setColor(colors[i]);
  217. // g.fillRect(pos.getX() - GuiSettings.getPictureScaleDiv2(), pos.getY() + GuiSettings.getPictureScaleDiv2() - 1,
  218. // (int) (barWidth * percentages[i] - 1), barHeight);
  219. // }
  220. //// g.setColor(color);
  221. //// g.fillRect(pos.getX() - GuiSettings.GetPictureScaleDiv2(), pos.getY() + GuiSettings.GetPictureScaleDiv2() - 1, (int) (barWidth * (percentage < 1 ? percentage : 1.0f) - 1), barHeight);
  222. // g.setColor(Color.BLACK);
  223. // g.setStroke(new BasicStroke(1));
  224. // g.drawRect(pos.getX() - GuiSettings.getPictureScaleDiv2(), pos.getY() + GuiSettings.getPictureScaleDiv2() - 1, barWidth - 1,
  225. // barHeight);
  226. //}
  227. //
  228. ///**
  229. // * HardCoded Stuff dont try at Home ;)
  230. // *
  231. // * @param dGroupNode
  232. // * @return
  233. // */
  234. //public float[] getGroupNodeBarPercentages(DecoratedGroupNode dGroupNode) {
  235. // int[] amountOfObjects = new int[6];
  236. // amountOfObjects[0] = dGroupNode.getAmountOfSupplier();
  237. // amountOfObjects[1] = dGroupNode.getAmountOfConsumerWithState(HolonObjectState.NOT_SUPPLIED);
  238. // amountOfObjects[2] = dGroupNode.getAmountOfConsumerWithState(HolonObjectState.PARTIALLY_SUPPLIED);
  239. // amountOfObjects[3] = dGroupNode.getAmountOfConsumerWithState(HolonObjectState.SUPPLIED);
  240. // amountOfObjects[4] = dGroupNode.getAmountOfConsumerWithState(HolonObjectState.OVER_SUPPLIED);
  241. // amountOfObjects[5] = dGroupNode.getAmountOfPassiv();
  242. // int countHolonObjects = amountOfObjects[0] + amountOfObjects[1] + amountOfObjects[2] + amountOfObjects[3]
  243. // + amountOfObjects[4] + amountOfObjects[5];
  244. // float[] percentages = new float[6];
  245. // int count = 0;
  246. // for (int i = 0; i < 6; i++) {
  247. // count += amountOfObjects[i];
  248. // percentages[i] = (float) count / (float) countHolonObjects;
  249. // }
  250. // return percentages;
  251. //}
  252. private static void drawSupplyBar(Graphics2D g, float percentage, Color color, Vec2i pos) {
  253. // +1, -2, -1 little Adjustment for pixel perfect alignment
  254. g.setColor(Color.WHITE);
  255. g.fillRect(pos.getX() - GuiSettings.getPictureScaleDiv2(), pos.getY() + GuiSettings.getPictureScaleDiv2() - 1, SupplyBarDimensions.width,
  256. SupplyBarDimensions.height);
  257. g.setColor(color);
  258. g.fillRect(pos.getX() - GuiSettings.getPictureScaleDiv2(), pos.getY() + GuiSettings.getPictureScaleDiv2() - 1,
  259. (int) (SupplyBarDimensions.width * (percentage < 1 ? percentage : 1.0f) - 1), SupplyBarDimensions.height);
  260. g.setColor(Color.BLACK);
  261. g.setStroke(new BasicStroke(1));
  262. g.drawRect(pos.getX() - GuiSettings.getPictureScaleDiv2(), pos.getY() + GuiSettings.getPictureScaleDiv2() - 1, SupplyBarDimensions.width - 1,
  263. SupplyBarDimensions.height);
  264. g.setFont(SupplyBarFont);
  265. String percentageString = (Math.round((percentage * 100))) + "%";
  266. int stringWidth = (int) g.getFontMetrics().getStringBounds(percentageString, g).getWidth();
  267. if (percentage > 1.0f){
  268. g.setColor(Color.WHITE); // Just to see better on purple
  269. }
  270. g.drawString(percentageString, pos.getX() + 1 - stringWidth / 2,
  271. pos.getY() + GuiSettings.getPictureScaleDiv2() - 1 + SupplyBarDimensions.height);
  272. }
  273. }