Rendering.java 13 KB

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