-
Notifications
You must be signed in to change notification settings - Fork 138
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for using fixed-size arrays in configuration variables.
- Loading branch information
1 parent
42256c7
commit 0191cfd
Showing
3 changed files
with
158 additions
and
8 deletions.
There are no files selected for viewing
54 changes: 54 additions & 0 deletions
54
DashboardCore/src/main/java/com/acmerobotics/dashboard/config/reflection/ArrayProvider.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package com.acmerobotics.dashboard.config.reflection; | ||
|
||
import com.acmerobotics.dashboard.config.ValueProvider; | ||
|
||
import java.lang.reflect.Array; | ||
import java.lang.reflect.Field; | ||
|
||
public class ArrayProvider<T> implements ValueProvider<T> { | ||
private final Field field; | ||
private final Object parent; | ||
private final int[] indices; | ||
public ArrayProvider(Field field, Object parent, int... indices) { | ||
this.field = field; | ||
this.parent = parent; | ||
this.indices = indices; | ||
} | ||
@SuppressWarnings("unchecked") | ||
@Override | ||
public T get() { | ||
try { | ||
return (T) getArrayRecursive(field.get(parent), indices); | ||
} catch (IllegalAccessException e) { | ||
throw new RuntimeException(e); | ||
} catch(ArrayIndexOutOfBoundsException e) | ||
{ | ||
return null; | ||
} | ||
} | ||
@Override | ||
public void set(T value) { | ||
try { | ||
setArrayRecursive(field.get(parent), value, indices); | ||
} catch (IllegalAccessException e) { | ||
throw new RuntimeException(e); | ||
}catch(ArrayIndexOutOfBoundsException ignored) { } | ||
} | ||
|
||
|
||
public static Object getArrayRecursive(Object object, int[] indices) throws ArrayIndexOutOfBoundsException, IllegalAccessException | ||
{ | ||
for (int index : indices) { | ||
object = Array.get(object, index); | ||
} | ||
return object; | ||
} | ||
public static void setArrayRecursive(Object object, Object value, int[] indices) | ||
{ | ||
for(int i=0;i< indices.length-1;i++) | ||
{ | ||
object=Array.get(object,indices[i]); | ||
} | ||
Array.set(object,indices[indices.length-1], value); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
34 changes: 34 additions & 0 deletions
34
TeamCode/src/main/java/org/firstinspires/ftc/teamcode/ArrayTestOpMode.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package org.firstinspires.ftc.teamcode; | ||
|
||
import com.acmerobotics.dashboard.FtcDashboard; | ||
import com.acmerobotics.dashboard.config.Config; | ||
import com.acmerobotics.dashboard.telemetry.MultipleTelemetry; | ||
import com.qualcomm.robotcore.eventloop.opmode.LinearOpMode; | ||
import com.qualcomm.robotcore.eventloop.opmode.TeleOp; | ||
|
||
@SuppressWarnings("unused") | ||
@Config | ||
@TeleOp | ||
public class ArrayTestOpMode extends LinearOpMode { | ||
public static class NestedClass { | ||
public int a,b; | ||
} | ||
public static int[] array = new int[3]; | ||
public static NestedClass[] innerArray = new NestedClass[] { new NestedClass(), new NestedClass(), new NestedClass()}; | ||
@Override | ||
public void runOpMode() { | ||
telemetry = new MultipleTelemetry(telemetry, FtcDashboard.getInstance().getTelemetry()); | ||
|
||
waitForStart(); | ||
array=new int[2]; | ||
while(opModeIsActive()) | ||
{ | ||
telemetry.addData("array0", array[0]); | ||
telemetry.addData("array1", array[1]); | ||
telemetry.addData("innerArray0A", innerArray[0].a); | ||
telemetry.addData("innerArray1B", innerArray[1].b); | ||
telemetry.addData("innerArray2A", innerArray[2].a); | ||
telemetry.update(); | ||
} | ||
} | ||
} |