|
| 1 | +/* |
| 2 | + * This file is part of BlueMap, licensed under the MIT License (MIT). |
| 3 | + * |
| 4 | + * Copyright (c) Blue (Lukas Rieger) <https://bluecolored.de> |
| 5 | + * Copyright (c) contributors |
| 6 | + * |
| 7 | + * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 8 | + * of this software and associated documentation files (the "Software"), to deal |
| 9 | + * in the Software without restriction, including without limitation the rights |
| 10 | + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 11 | + * copies of the Software, and to permit persons to whom the Software is |
| 12 | + * furnished to do so, subject to the following conditions: |
| 13 | + * |
| 14 | + * The above copyright notice and this permission notice shall be included in |
| 15 | + * all copies or substantial portions of the Software. |
| 16 | + * |
| 17 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 18 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 19 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 20 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 21 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 22 | + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 23 | + * THE SOFTWARE. |
| 24 | + */ |
| 25 | +package de.bluecolored.bluemap.common.api.marker; |
| 26 | + |
| 27 | +import com.flowpowered.math.vector.Vector2d; |
| 28 | +import com.flowpowered.math.vector.Vector3d; |
| 29 | +import com.google.common.base.Preconditions; |
| 30 | +import de.bluecolored.bluemap.api.BlueMapAPI; |
| 31 | +import de.bluecolored.bluemap.api.BlueMapMap; |
| 32 | +import de.bluecolored.bluemap.api.marker.ExtrudeMarker; |
| 33 | +import de.bluecolored.bluemap.api.marker.Shape; |
| 34 | +import ninja.leaping.configurate.ConfigurationNode; |
| 35 | + |
| 36 | +import java.awt.*; |
| 37 | +import java.util.List; |
| 38 | + |
| 39 | +public class ExtrudeMarkerImpl extends ObjectMarkerImpl implements ExtrudeMarker { |
| 40 | + public static final String MARKER_TYPE = "extrude"; |
| 41 | + |
| 42 | + private Shape shape; |
| 43 | + private float shapeMinY; |
| 44 | + private float shapeMaxY; |
| 45 | + private boolean depthTest; |
| 46 | + private int lineWidth; |
| 47 | + private Color lineColor, fillColor; |
| 48 | + |
| 49 | + private boolean hasUnsavedChanges; |
| 50 | + |
| 51 | + public ExtrudeMarkerImpl(String id, BlueMapMap map, Vector3d position, Shape shape, float shapeMinY, float shapeMaxY) { |
| 52 | + super(id, map, position); |
| 53 | + |
| 54 | + Preconditions.checkNotNull(shape); |
| 55 | + |
| 56 | + this.shape = shape; |
| 57 | + this.shapeMinY = shapeMinY; |
| 58 | + this.shapeMaxY = shapeMaxY; |
| 59 | + this.lineWidth = 2; |
| 60 | + this.lineColor = new Color(255, 0, 0, 200); |
| 61 | + this.fillColor = new Color(200, 0, 0, 100); |
| 62 | + |
| 63 | + this.hasUnsavedChanges = true; |
| 64 | + } |
| 65 | + |
| 66 | + @Override |
| 67 | + public String getType() { |
| 68 | + return MARKER_TYPE; |
| 69 | + } |
| 70 | + |
| 71 | + @Override |
| 72 | + public Shape getShape() { |
| 73 | + return this.shape; |
| 74 | + } |
| 75 | + |
| 76 | + @Override |
| 77 | + public float getShapeMinY() { |
| 78 | + return shapeMinY; |
| 79 | + } |
| 80 | + |
| 81 | + @Override |
| 82 | + public float getShapeMaxY() { |
| 83 | + return shapeMaxY; |
| 84 | + } |
| 85 | + |
| 86 | + @Override |
| 87 | + public synchronized void setShape(Shape shape, float shapeMinY, float shapeMaxY) { |
| 88 | + Preconditions.checkNotNull(shape); |
| 89 | + |
| 90 | + this.shape = shape; |
| 91 | + this.shapeMinY = shapeMinY; |
| 92 | + this.shapeMaxY = shapeMaxY; |
| 93 | + this.hasUnsavedChanges = true; |
| 94 | + } |
| 95 | + |
| 96 | + @Override |
| 97 | + public boolean isDepthTestEnabled() { |
| 98 | + return this.depthTest; |
| 99 | + } |
| 100 | + |
| 101 | + @Override |
| 102 | + public void setDepthTestEnabled(boolean enabled) { |
| 103 | + this.depthTest = enabled; |
| 104 | + this.hasUnsavedChanges = true; |
| 105 | + } |
| 106 | + |
| 107 | + @Override |
| 108 | + public int getLineWidth() { |
| 109 | + return lineWidth; |
| 110 | + } |
| 111 | + |
| 112 | + @Override |
| 113 | + public void setLineWidth(int lineWidth) { |
| 114 | + this.lineWidth = lineWidth; |
| 115 | + this.hasUnsavedChanges = true; |
| 116 | + } |
| 117 | + |
| 118 | + @Override |
| 119 | + public Color getLineColor() { |
| 120 | + return this.lineColor; |
| 121 | + } |
| 122 | + |
| 123 | + @Override |
| 124 | + public synchronized void setLineColor(Color color) { |
| 125 | + Preconditions.checkNotNull(color); |
| 126 | + |
| 127 | + this.lineColor = color; |
| 128 | + this.hasUnsavedChanges = true; |
| 129 | + } |
| 130 | + |
| 131 | + @Override |
| 132 | + public Color getFillColor() { |
| 133 | + return this.fillColor; |
| 134 | + } |
| 135 | + |
| 136 | + @Override |
| 137 | + public synchronized void setFillColor(Color color) { |
| 138 | + Preconditions.checkNotNull(color); |
| 139 | + |
| 140 | + this.fillColor = color; |
| 141 | + this.hasUnsavedChanges = true; |
| 142 | + } |
| 143 | + |
| 144 | + @Override |
| 145 | + public void load(BlueMapAPI api, ConfigurationNode markerNode, boolean overwriteChanges) throws MarkerFileFormatException { |
| 146 | + super.load(api, markerNode, overwriteChanges); |
| 147 | + |
| 148 | + if (!overwriteChanges && hasUnsavedChanges) return; |
| 149 | + this.hasUnsavedChanges = false; |
| 150 | + |
| 151 | + this.shape = readShape(markerNode.getNode("shape")); |
| 152 | + this.shapeMinY = markerNode.getNode("shapeMinY").getFloat(0); |
| 153 | + this.shapeMaxY = markerNode.getNode("shapeMaxY").getFloat(255); |
| 154 | + this.depthTest = markerNode.getNode("depthTest").getBoolean(true); |
| 155 | + this.lineWidth = markerNode.getNode("lineWidth").getInt(2); |
| 156 | + this.lineColor = readColor(markerNode.getNode("lineColor")); |
| 157 | + this.fillColor = readColor(markerNode.getNode("fillColor")); |
| 158 | + } |
| 159 | + |
| 160 | + @Override |
| 161 | + public void save(ConfigurationNode markerNode) { |
| 162 | + super.save(markerNode); |
| 163 | + |
| 164 | + writeShape(markerNode.getNode("shape"), this.shape); |
| 165 | + markerNode.getNode("shapeMinY").setValue(Math.round(shapeMinY * 1000f) / 1000f); |
| 166 | + markerNode.getNode("shapeMaxY").setValue(Math.round(shapeMaxY * 1000f) / 1000f); |
| 167 | + markerNode.getNode("depthTest").setValue(this.depthTest); |
| 168 | + markerNode.getNode("lineWidth").setValue(this.lineWidth); |
| 169 | + writeColor(markerNode.getNode("lineColor"), this.lineColor); |
| 170 | + writeColor(markerNode.getNode("fillColor"), this.fillColor); |
| 171 | + |
| 172 | + hasUnsavedChanges = false; |
| 173 | + } |
| 174 | + |
| 175 | + private Shape readShape(ConfigurationNode node) throws MarkerFileFormatException { |
| 176 | + List<? extends ConfigurationNode> posNodes = node.getChildrenList(); |
| 177 | + |
| 178 | + if (posNodes.size() < 3) throw new MarkerFileFormatException("Failed to read shape: point-list has fewer than 3 entries!"); |
| 179 | + |
| 180 | + Vector2d[] positions = new Vector2d[posNodes.size()]; |
| 181 | + for (int i = 0; i < positions.length; i++) { |
| 182 | + positions[i] = readShapePos(posNodes.get(i)); |
| 183 | + } |
| 184 | + |
| 185 | + return new Shape(positions); |
| 186 | + } |
| 187 | + |
| 188 | + private static Vector2d readShapePos(ConfigurationNode node) throws MarkerFileFormatException { |
| 189 | + ConfigurationNode nx, nz; |
| 190 | + nx = node.getNode("x"); |
| 191 | + nz = node.getNode("z"); |
| 192 | + |
| 193 | + if (nx.isVirtual() || nz.isVirtual()) throw new MarkerFileFormatException("Failed to read shape position: Node x or z is not set!"); |
| 194 | + |
| 195 | + return new Vector2d( |
| 196 | + nx.getDouble(), |
| 197 | + nz.getDouble() |
| 198 | + ); |
| 199 | + } |
| 200 | + |
| 201 | + private static Color readColor(ConfigurationNode node) throws MarkerFileFormatException { |
| 202 | + ConfigurationNode nr, ng, nb, na; |
| 203 | + nr = node.getNode("r"); |
| 204 | + ng = node.getNode("g"); |
| 205 | + nb = node.getNode("b"); |
| 206 | + na = node.getNode("a"); |
| 207 | + |
| 208 | + if (nr.isVirtual() || ng.isVirtual() || nb.isVirtual()) throw new MarkerFileFormatException("Failed to read color: Node r,g or b is not set!"); |
| 209 | + |
| 210 | + float alpha = na.getFloat(1); |
| 211 | + if (alpha < 0 || alpha > 1) throw new MarkerFileFormatException("Failed to read color: alpha value out of range (0-1)!"); |
| 212 | + |
| 213 | + try { |
| 214 | + return new Color(nr.getInt(), ng.getInt(), nb.getInt(), (int)(alpha * 255)); |
| 215 | + } catch (IllegalArgumentException ex) { |
| 216 | + throw new MarkerFileFormatException("Failed to read color: " + ex.getMessage(), ex); |
| 217 | + } |
| 218 | + } |
| 219 | + |
| 220 | + private static void writeShape(ConfigurationNode node, Shape shape) { |
| 221 | + for (int i = 0; i < shape.getPointCount(); i++) { |
| 222 | + ConfigurationNode pointNode = node.appendListNode(); |
| 223 | + Vector2d point = shape.getPoint(i); |
| 224 | + pointNode.getNode("x").setValue(Math.round(point.getX() * 1000d) / 1000d); |
| 225 | + pointNode.getNode("z").setValue(Math.round(point.getY() * 1000d) / 1000d); |
| 226 | + } |
| 227 | + } |
| 228 | + |
| 229 | + private static void writeColor(ConfigurationNode node, Color color) { |
| 230 | + int r = color.getRed(); |
| 231 | + int g = color.getGreen(); |
| 232 | + int b = color.getBlue(); |
| 233 | + float a = color.getAlpha() / 255f; |
| 234 | + |
| 235 | + node.getNode("r").setValue(r); |
| 236 | + node.getNode("g").setValue(g); |
| 237 | + node.getNode("b").setValue(b); |
| 238 | + node.getNode("a").setValue(a); |
| 239 | + } |
| 240 | + |
| 241 | +} |
0 commit comments