Przeglądaj źródła

Improves CanvasDragging

Adds GroupNodeBar
TomTroppmann 2 lat temu
rodzic
commit
53f7213c9b

+ 8 - 6
src/holeg/ui/view/canvas/Canvas.java

@@ -182,17 +182,19 @@ public class Canvas extends JPanel {
                 return;
             }
             log.finest(state.toString());
-            if (!e.isControlDown()) {
-                GuiSettings.getSelectedObjects().clear();
-            }
+
             Vec2i pos = new Vec2i(e.getPoint());
             getObjectAtPosition(pos).ifPresentOrElse(obj -> {
-                if (!GuiSettings.getSelectedObjects().contains(obj)) {
-                    state = State.Selection;
-                    GuiSettings.getSelectedObjects().add(obj);
+                if (!e.isControlDown() && !GuiSettings.getSelectedObjects().contains(obj)) {
+                    GuiSettings.getSelectedObjects().clear();
                 }
+                state = State.Selection;
+                GuiSettings.getSelectedObjects().add(obj);
                 selectedOnPressed = obj;
             }, () -> {
+                if (!e.isControlDown()) {
+                    GuiSettings.getSelectedObjects().clear();
+                }
                 state = State.BoxSelection;
                 selectionBeforeBoxSelection = Set.copyOf(GuiSettings.getSelectedObjects());
             });

+ 51 - 1
src/holeg/ui/view/canvas/Rendering.java

@@ -22,7 +22,9 @@ class Rendering {
     private static final Dimension SupplyBarDimensions = new Dimension(GuiSettings.getPictureScale(), GuiSettings.getPictureScale() / 5);
     private static final Font SupplyBarFont = new Font("TimesNewRoman", Font.PLAIN, (int) (GuiSettings.getPictureScale() * 0.3) - 2);
 
-
+    private static final Color[] GroupNodeBarColors = {ColorPreference.HolonObject.Producer, ColorPreference.HolonObject.NotSupplied,
+            ColorPreference.HolonObject.PartiallySupplied, ColorPreference.HolonObject.Supplied,
+            ColorPreference.HolonObject.OverSupplied, ColorPreference.HolonObject.NoEnergy};
 
     static Graphics2D initGraphics2D(Graphics g) {
         Graphics2D g2d = (Graphics2D) g;
@@ -116,6 +118,7 @@ class Rendering {
         g.fillRect(pos.getX() - GuiSettings.getPictureScaleDiv2(), pos.getY() - GuiSettings.getPictureScaleDiv2(),
                 GuiSettings.getPictureScale(), GuiSettings.getPictureScale());
         drawCanvasObject(g, groupNode.getImagePath(), pos);
+        drawGroupNodeBar(g, groupNode, pos);
     }
 
     static void drawSelection(Graphics2D g) {
@@ -173,6 +176,53 @@ class Rendering {
                 pos.getY() + GuiSettings.getPictureScaleDiv2() - 1 + SupplyBarDimensions.height);
     }
 
+    private static void drawGroupNodeBar(Graphics2D g, GroupNode groupNode, Vec2i pos) {
+	// +1, -2, -1 little Adjustment for pixel perfect alignment
+	g.setColor(Color.WHITE);
+	g.fillRect(pos.getX() - GuiSettings.getPictureScaleDiv2(), pos.getY() + GuiSettings.getPictureScaleDiv2() - 1, (int) SupplyBarDimensions.width,
+			SupplyBarDimensions.height);
+	float[] percentages = getGroupNodeBarPercentages(groupNode);
+
+	for (int i = 5; i >= 0; i--) {
+		g.setColor(GroupNodeBarColors[i]);
+		g.fillRect(pos.getX() - GuiSettings.getPictureScaleDiv2(), pos.getY() + GuiSettings.getPictureScaleDiv2() - 1,
+				(int) (SupplyBarDimensions.width * percentages[i] - 1), SupplyBarDimensions.height);
+	}
+    g.setColor(Color.BLACK);
+	g.setStroke(new BasicStroke(1));
+	g.drawRect(pos.getX() - GuiSettings.getPictureScaleDiv2(), pos.getY() + GuiSettings.getPictureScaleDiv2() - 1, SupplyBarDimensions.width - 1,
+			SupplyBarDimensions.height);
+}
+
+/**
+ * HardCoded Stuff
+ */
+public static float[] getGroupNodeBarPercentages(GroupNode groupNode) {
+	int[] amountOfObjects = new int[6];
+    groupNode.getAllHolonObjectsRecursive().forEach(hO -> {
+        switch (hO.getState()){
+            case PRODUCER -> amountOfObjects[0]++;
+            case NOT_SUPPLIED -> amountOfObjects[1]++;
+            case PARTIALLY_SUPPLIED -> amountOfObjects[2]++;
+            case SUPPLIED -> amountOfObjects[3]++;
+            case OVER_SUPPLIED -> amountOfObjects[4]++;
+            case NO_ENERGY -> amountOfObjects[5]++;
+        }
+    });
+	int countHolonObjects = amountOfObjects[0] + amountOfObjects[1] + amountOfObjects[2] + amountOfObjects[3]
+			+ amountOfObjects[4] + amountOfObjects[5];
+	float[] percentages = new float[6];
+	int count = 0;
+	for (int i = 0; i < 6; i++) {
+		count += amountOfObjects[i];
+		percentages[i] = (float) count / (float) countHolonObjects;
+	}
+	return percentages;
+}
+
+
+
+
     public static void drawReplacementSymbol(Graphics2D g, AbstractCanvasObject hoveredObject) {
         Vec2i pos = hoveredObject.getPosition();
         Dimension size = new Dimension(16, 16);