|
| 1 | +package de.geolykt.starloader.api.gui.graph; |
| 2 | + |
| 3 | +import java.util.Objects; |
| 4 | + |
| 5 | +import org.jetbrains.annotations.NotNull; |
| 6 | + |
| 7 | +import com.badlogic.gdx.graphics.Camera; |
| 8 | +import com.badlogic.gdx.graphics.Color; |
| 9 | + |
| 10 | +import de.geolykt.starloader.api.empire.Alliance; |
| 11 | +import de.geolykt.starloader.api.empire.Empire; |
| 12 | +import de.geolykt.starloader.api.gui.Drawing; |
| 13 | +import de.geolykt.starloader.api.gui.DrawingImpl; |
| 14 | +import de.geolykt.starloader.api.gui.screen.LineWrappingInfo; |
| 15 | +import de.geolykt.starloader.api.gui.screen.Screen; |
| 16 | +import de.geolykt.starloader.api.gui.screen.ScreenComponent; |
| 17 | + |
| 18 | +/** |
| 19 | + * A simple visualisation of a {@link ChartData} object. |
| 20 | + * This class only depends on API classes, and as such this class is safe to subclass. |
| 21 | + */ |
| 22 | +public class LineChart implements ScreenComponent { |
| 23 | + |
| 24 | + // FIXME I have no fully tested this class with the retractors that occurred with the screen API. |
| 25 | + // However I recall it having some issues with the component rendering too high. |
| 26 | + // I have however fixed an error I did, so this issue may no longer be present. |
| 27 | + |
| 28 | + /** |
| 29 | + * The parent screen as defined by {@link ScreenComponent#getParentScreen()}. |
| 30 | + */ |
| 31 | + protected @NotNull Screen parent; |
| 32 | + |
| 33 | + /** |
| 34 | + * The {@link LineWrappingInfo} as defined by {@link #getLineWrappingInfo()}. |
| 35 | + */ |
| 36 | + protected @NotNull LineWrappingInfo lwinfo; |
| 37 | + |
| 38 | + /** |
| 39 | + * The chart data that needs to be visualised. |
| 40 | + */ |
| 41 | + protected @NotNull ChartData<? extends Object> chart; |
| 42 | + |
| 43 | + /** |
| 44 | + * The total width of the component. |
| 45 | + */ |
| 46 | + protected final int height; |
| 47 | + |
| 48 | + /** |
| 49 | + * The total height of the component. |
| 50 | + */ |
| 51 | + protected final int width; |
| 52 | + |
| 53 | + /** |
| 54 | + * The constructor. |
| 55 | + * |
| 56 | + * @param parent The screen this component depends on. Used for {@link ScreenComponent#getParentScreen()}. |
| 57 | + * @param wrappingInfo The {@link LineWrappingInfo} that is used for the screen implementation. Returned later on by {@link ScreenComponent#getLineWrappingInfo()}. |
| 58 | + * @param chart The chart that should be visualised by this component. |
| 59 | + * @param width The width of the component. |
| 60 | + * @param height The height of the component. |
| 61 | + */ |
| 62 | + public LineChart(@NotNull Screen parent, @NotNull LineWrappingInfo wrappingInfo, @NotNull ChartData<? extends Object> chart, |
| 63 | + int width, int height) { |
| 64 | + this.parent = Objects.requireNonNull(parent, "parent may not be null."); |
| 65 | + this.lwinfo = Objects.requireNonNull(wrappingInfo, "wrappingInfo may not be null."); |
| 66 | + this.chart = Objects.requireNonNull(chart, "chart may not be null."); |
| 67 | + this.width = width; |
| 68 | + this.height = height; |
| 69 | + } |
| 70 | + |
| 71 | + @Override |
| 72 | + public int renderAt(float x, float y, @NotNull Camera camera) { |
| 73 | + @SuppressWarnings("null") |
| 74 | + @NotNull Color white = Color.WHITE; |
| 75 | + |
| 76 | + DrawingImpl graphics = Drawing.requireInstance(); |
| 77 | + graphics.fillWindow(x, y, getWidth(), -getHeight(), white, camera); |
| 78 | + |
| 79 | + float pixelsPerValue = getChartHeight() / chart.getHeight(); |
| 80 | + float pixelsPerIntervall; |
| 81 | + if (chart instanceof RollingChartData) { |
| 82 | + pixelsPerIntervall = getChartWidth() / (((RollingChartData<?>) chart).getCurrentPositon() - 1); |
| 83 | + } else { |
| 84 | + pixelsPerIntervall = getChartWidth() / chart.getWidth(); |
| 85 | + } |
| 86 | + float thickness = getLineThickness(); |
| 87 | + x += getChartXOffset(); |
| 88 | + y -= getChartYOffset(); |
| 89 | + |
| 90 | + for (ValueEdge<? extends Object> edge : chart.getEdges()) { |
| 91 | + if (edge.vertex1Position < 0) { |
| 92 | + continue; |
| 93 | + } |
| 94 | + float x1 = x + (edge.vertex1Position - 1) * pixelsPerIntervall; |
| 95 | + float x2 = x + (edge.vertex2Position - 1) * pixelsPerIntervall; |
| 96 | + float y1 = edge.vertex1Value * pixelsPerValue; |
| 97 | + float y2 = edge.vertex2Value * pixelsPerValue; |
| 98 | + if (y1 < 0) { |
| 99 | + y1 = 0; |
| 100 | + } |
| 101 | + if (y2 < 0) { |
| 102 | + y2 = 0; |
| 103 | + } |
| 104 | + // Galimulator draws from bottom-to-top, but SLAPI places components top-to-bottom (this should not be true). As such "y" is the MAXIMUM coordinate we may draw to. |
| 105 | + // Comment from Nov 11 2021: I have no idea what above comment refers to, other than that the y coordinates of drawing operations are confusing |
| 106 | + // It is best to assume that this is the right way. |
| 107 | + y1 = y - getHeight() + y1; |
| 108 | + y2 = y - getHeight() + y2; |
| 109 | + graphics.drawLine(x1, y1, x2, y2, thickness, getColor(edge.vertex1), camera); |
| 110 | + } |
| 111 | + chart.getEdges(); |
| 112 | + return getWidth(); |
| 113 | + } |
| 114 | + |
| 115 | + protected float getChartHeight() { |
| 116 | + return getHeight() - getChartYOffset(); |
| 117 | + } |
| 118 | + |
| 119 | + protected float getChartXOffset() { |
| 120 | + return 10.0F; |
| 121 | + } |
| 122 | + |
| 123 | + protected float getChartYOffset() { |
| 124 | + return 0.0F; |
| 125 | + } |
| 126 | + |
| 127 | + protected float getChartWidth() { |
| 128 | + return getWidth() - 20.0F; |
| 129 | + } |
| 130 | + |
| 131 | + protected float getLineThickness() { |
| 132 | + return 2.0F; |
| 133 | + } |
| 134 | + |
| 135 | + protected @NotNull Color getColor(Object o) { |
| 136 | + if (o instanceof Empire) { |
| 137 | + java.awt.Color awtColor = ((Empire) o).getAWTColor(); |
| 138 | + return new Color(awtColor.getRed() / 255.0F, awtColor.getGreen() / 255.0F, awtColor.getBlue() / 255.0F, awtColor.getAlpha() / 255.0F); |
| 139 | + } else if (o instanceof Alliance) { |
| 140 | + java.awt.Color awtColor = ((Alliance) o).getAWTColor(); |
| 141 | + return new Color(awtColor.getRed() / 255.0F, awtColor.getGreen() / 255.0F, awtColor.getBlue() / 255.0F, awtColor.getAlpha() / 255.0F); |
| 142 | + } else { |
| 143 | + Color c = new Color(o.hashCode()); |
| 144 | + c.a = 1.0F; |
| 145 | + return c; |
| 146 | + } |
| 147 | + } |
| 148 | + |
| 149 | + @Override |
| 150 | + public int getHeight() { |
| 151 | + return height; |
| 152 | + } |
| 153 | + |
| 154 | + @Override |
| 155 | + public @NotNull LineWrappingInfo getLineWrappingInfo() { |
| 156 | + return lwinfo; |
| 157 | + } |
| 158 | + |
| 159 | + @Override |
| 160 | + public @NotNull Screen getParentScreen() { |
| 161 | + return parent; |
| 162 | + } |
| 163 | + |
| 164 | + @Override |
| 165 | + public int getWidth() { |
| 166 | + return width; |
| 167 | + } |
| 168 | + |
| 169 | + @Override |
| 170 | + public boolean isSameType(@NotNull ScreenComponent component) { |
| 171 | + return component instanceof LineChart; |
| 172 | + } |
| 173 | +} |
0 commit comments