Skip to content

Commit

Permalink
Use field images from allwpilib (#747)
Browse files Browse the repository at this point in the history
  • Loading branch information
pjreiniger authored Jan 6, 2024
1 parent 93bcc42 commit 56af2b7
Show file tree
Hide file tree
Showing 24 changed files with 27 additions and 191 deletions.
1 change: 1 addition & 0 deletions plugins/base/base.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@ description = "Base shuffleboard plugin that provides the default data types and

dependencies {
api "de.gsi.chart:chartfx-chart:11.1.5"
api wpilibTools.deps.wpilibJava("fieldImages")
}
Original file line number Diff line number Diff line change
@@ -1,26 +1,23 @@
package edu.wpi.first.shuffleboard.plugin.base.widget;

import com.google.common.collect.ImmutableList;
import com.google.gson.Gson;
import edu.wpi.fields.FieldConfig;
import edu.wpi.fields.Fields;
import edu.wpi.first.shuffleboard.api.prefs.Group;
import edu.wpi.first.shuffleboard.api.prefs.Setting;
import edu.wpi.first.shuffleboard.api.widget.Description;
import edu.wpi.first.shuffleboard.api.widget.ParametrizedController;
import edu.wpi.first.shuffleboard.api.widget.SimpleAnnotatedWidget;
import edu.wpi.first.shuffleboard.plugin.base.data.FieldData;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;

import java.io.IOException;
import java.io.Reader;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.nio.file.Paths;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

import javafx.beans.property.BooleanProperty;
import javafx.beans.property.DoubleProperty;
Expand Down Expand Up @@ -63,8 +60,8 @@ public class FieldWidget extends SimpleAnnotatedWidget<FieldData> {
private final Map<String, Paint> colors = new HashMap<>();
private final Map<String, Circle[]> objectCircles = new HashMap<>();
private Map<String, FieldData.SimplePose2d[]> previousObjects = new HashMap<>();
private final Property<Game> game =
new SimpleObjectProperty<>(Game.A2023_Charged_Up);
private final Property<Fields> game =
new SimpleObjectProperty<>(Fields.kDefaultField);
private final DoubleProperty robotSize = new SimpleDoubleProperty(50);
private final BooleanProperty showCirclesOutsideOfField = new SimpleBooleanProperty(false);

Expand Down Expand Up @@ -147,57 +144,28 @@ private void centerImage() {
}
}

private void setGame(Game game) {
InputStream stream = getClass().getResourceAsStream(game.json());

private void setGame(Fields field) {
try {
if (stream == null) {
throw new Exception("Cannot read JSON of " + game);
}
Gson gson = new Gson();
Reader reader = new BufferedReader(new InputStreamReader(stream, StandardCharsets.UTF_8));
Map<?, ?> map = gson.fromJson(reader, Map.class);

URL imagePath = getClass()
.getResource("field/" + map.get("field-image"));
if (imagePath == null) {
throw new Exception("Cannot get image at " + Paths.get("field", (String) map.get("field-image")));
}
Image image = new Image(imagePath.toExternalForm());
FieldConfig config = FieldConfig.loadField(field);
InputStream imageStream = config.getImageAsStream();
Objects.requireNonNull(imageStream);

Image image = new Image(imageStream);
backgroundImage.setImage(image);
imageStartX = config.m_fieldCorners.m_topLeft[0];
imageEndX = config.m_fieldCorners.m_bottomRight[0];
imageStartY = image.getHeight() - config.m_fieldCorners.m_bottomRight[1];
imageEndY = image.getHeight() - config.m_fieldCorners.m_topLeft[1];

imageStartX =
((List<Double>) ((Map<?, ?>) map.get("field-corners")).get("top-left"))
.get(0);
imageEndX = ((List<Double>) ((Map<?, ?>) map.get("field-corners"))
.get("bottom-right"))
.get(0);
imageStartY = image.getHeight()
- ((List<Double>) ((Map<?, ?>) map.get("field-corners"))
.get("bottom-right"))
.get(1);
imageEndY =
image.getHeight()
- ((List<Double>) ((Map<?, ?>) map.get("field-corners")).get("top-left"))
.get(1);

fieldWidth = ((List<Double>) map.get("field-size")).get(0);
fieldHeight = ((List<Double>) map.get("field-size")).get(1);

String fieldUnit = (String) map.get("field-unit");
if (fieldUnit.equals("feet") || fieldUnit.equals("foot")) {
fieldWidth = UltrasonicWidget.Unit.FOOT.as(fieldWidth,
UltrasonicWidget.Unit.METER);
fieldHeight = UltrasonicWidget.Unit.FOOT.as(
fieldHeight, UltrasonicWidget.Unit.METER);
fieldWidth = config.m_fieldSize[0];
fieldHeight = config.m_fieldSize[1];

if ("feet".equals(config.m_fieldUnit) || "foot".equals(config.m_fieldUnit)) {
fieldWidth = UltrasonicWidget.Unit.FOOT.as(fieldWidth, UltrasonicWidget.Unit.METER);
fieldHeight = UltrasonicWidget.Unit.FOOT.as(fieldHeight, UltrasonicWidget.Unit.METER);
}
} catch (Exception ignored) {
} finally {
try {
if (stream != null) {
stream.close();
}
} catch (IOException ignored) { }
} catch (IOException e) {
throw new RuntimeException(e);
}
}

Expand Down Expand Up @@ -299,7 +267,7 @@ public List<Group> getSettings() {
}

return ImmutableList.of(
Group.of("Game", Setting.of("Game", game, Game.class)),
Group.of("Game", Setting.of("Game", game, Fields.class)),
Group.of("Visuals",
Setting.of("Robot Icon Size", robotSize, Double.class),
Setting.of("Show Outside Circles", showCirclesOutsideOfField, Boolean.class)
Expand All @@ -312,27 +280,4 @@ public List<Group> getSettings() {
public Pane getView() {
return root;
}

private enum Game {
A2018_Power_Up,
A2019_Deep_Space,
A2020_Infinite_Recharge,
A2021_Barrel_Racing_Path,
A2021_Bounce_Path,
A2021_Galactic_Search_A,
A2021_Galactic_Search_B,
A2021_Infinite_Recharge,
A2021_Slalom_Path,
A2022_Rapid_React,
A2023_Charged_Up;

public String json() {
return "field/" + this.name().substring(1).toLowerCase().replaceFirst("_", "-").replaceAll("_", "") + ".json";
}

@Override
public String toString() {
return this.name().substring(1).replaceAll("_", " ");
}
}
}
Binary file not shown.

This file was deleted.

This file was deleted.

Binary file not shown.
Binary file not shown.

This file was deleted.

Binary file not shown.

This file was deleted.

Binary file not shown.

This file was deleted.

Binary file not shown.

This file was deleted.

Binary file not shown.

This file was deleted.

Binary file not shown.

This file was deleted.

Binary file not shown.

This file was deleted.

Binary file not shown.

This file was deleted.

This file was deleted.

Binary file not shown.

0 comments on commit 56af2b7

Please sign in to comment.