mirror of
https://github.com/wpilibsuite/allwpilib
synced 2026-06-22 01:11:42 +00:00
[fieldImages] Move Java fields to edu.wpi.first.fields (#6694)
Doesn't make sense in edu.wpi.fields, and resources are already in edu.wpi.first.fields This will require changes in Shuffleboard and Pathweaver
This commit is contained in:
@@ -0,0 +1,88 @@
|
||||
// Copyright (c) FIRST and other WPILib contributors.
|
||||
// Open Source Software; you can modify and/or share it under the terms of
|
||||
// the WPILib BSD license file in the root directory of this project.
|
||||
|
||||
package edu.wpi.first.fields;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonProperty;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import java.io.BufferedReader;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.InputStreamReader;
|
||||
import java.net.URL;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
|
||||
public class FieldConfig {
|
||||
public static class Corners {
|
||||
@JsonProperty("top-left")
|
||||
public double[] m_topLeft;
|
||||
|
||||
@JsonProperty("bottom-right")
|
||||
public double[] m_bottomRight;
|
||||
}
|
||||
|
||||
@JsonProperty("game")
|
||||
public String m_game;
|
||||
|
||||
@JsonProperty("field-image")
|
||||
public String m_fieldImage;
|
||||
|
||||
@JsonProperty("field-corners")
|
||||
public Corners m_fieldCorners;
|
||||
|
||||
@JsonProperty("field-size")
|
||||
public double[] m_fieldSize;
|
||||
|
||||
@JsonProperty("field-unit")
|
||||
public String m_fieldUnit;
|
||||
|
||||
public FieldConfig() {}
|
||||
|
||||
public URL getImageUrl() {
|
||||
return getClass().getResource(Fields.kBaseResourceDir + m_fieldImage);
|
||||
}
|
||||
|
||||
public InputStream getImageAsStream() {
|
||||
return getClass().getResourceAsStream(Fields.kBaseResourceDir + m_fieldImage);
|
||||
}
|
||||
|
||||
/**
|
||||
* Loads a predefined field configuration from a resource file.
|
||||
*
|
||||
* @param field The predefined field
|
||||
* @return The field configuration
|
||||
* @throws IOException Throws if the file could not be loaded
|
||||
*/
|
||||
public static FieldConfig loadField(Fields field) throws IOException {
|
||||
return loadFromResource(field.m_resourceFile);
|
||||
}
|
||||
|
||||
/**
|
||||
* Loads a field configuration from a file on disk.
|
||||
*
|
||||
* @param file The json file to load
|
||||
* @return The field configuration
|
||||
* @throws IOException Throws if the file could not be loaded
|
||||
*/
|
||||
public static FieldConfig loadFromFile(Path file) throws IOException {
|
||||
try (BufferedReader reader = Files.newBufferedReader(file)) {
|
||||
return new ObjectMapper().readerFor(FieldConfig.class).readValue(reader);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Loads a field configuration from a resource file located inside the programs jar file.
|
||||
*
|
||||
* @param resourcePath The path to the resource file
|
||||
* @return The field configuration
|
||||
* @throws IOException Throws if the resource could not be loaded
|
||||
*/
|
||||
public static FieldConfig loadFromResource(String resourcePath) throws IOException {
|
||||
try (InputStream stream = FieldConfig.class.getResourceAsStream(resourcePath);
|
||||
InputStreamReader reader = new InputStreamReader(stream)) {
|
||||
return new ObjectMapper().readerFor(FieldConfig.class).readValue(reader);
|
||||
}
|
||||
}
|
||||
}
|
||||
31
fieldImages/src/main/java/edu/wpi/first/fields/Fields.java
Normal file
31
fieldImages/src/main/java/edu/wpi/first/fields/Fields.java
Normal file
@@ -0,0 +1,31 @@
|
||||
// Copyright (c) FIRST and other WPILib contributors.
|
||||
// Open Source Software; you can modify and/or share it under the terms of
|
||||
// the WPILib BSD license file in the root directory of this project.
|
||||
|
||||
package edu.wpi.first.fields;
|
||||
|
||||
public enum Fields {
|
||||
k2018PowerUp("2018-powerup.json"),
|
||||
k2019DeepSpace("2019-deepspace.json"),
|
||||
k2020InfiniteRecharge("2020-infiniterecharge.json"),
|
||||
k2021InfiniteRecharge("2021-infiniterecharge.json"),
|
||||
k2021Barrel("2021-barrelracingpath.json"),
|
||||
k2021Bounce("2021-bouncepath.json"),
|
||||
k2021GalacticSearchA("2021-galacticsearcha.json"),
|
||||
k2021GalacticSearchB("2021-galacticsearchb.json"),
|
||||
k2021Slalom("2021-slalompath.json"),
|
||||
k2022RapidReact("2022-rapidreact.json"),
|
||||
k2023ChargedUp("2023-chargedup.json"),
|
||||
k2024Crescendo("2024-crescendo.json");
|
||||
|
||||
public static final String kBaseResourceDir = "/edu/wpi/first/fields/";
|
||||
|
||||
/** Alias to the current game. */
|
||||
public static final Fields kDefaultField = k2024Crescendo;
|
||||
|
||||
public final String m_resourceFile;
|
||||
|
||||
Fields(String resourceFile) {
|
||||
m_resourceFile = kBaseResourceDir + resourceFile;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user