Skip to content

Commit

Permalink
Implement moting rotation selection
Browse files Browse the repository at this point in the history
Fix new GUI, fix saving trackers config
  • Loading branch information
Eirenliel committed Jul 4, 2021
1 parent 5d5ca9f commit 4033278
Show file tree
Hide file tree
Showing 8 changed files with 161 additions and 150 deletions.
6 changes: 6 additions & 0 deletions src/main/java/io/eiren/gui/EJPanel.java
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,12 @@ public static GridBagConstraints c(int x, int y, Insets insets) {
return c;
}

public static GridBagConstraints s(GridBagConstraints c, int gridwidth, int gridheight) {
c.gridwidth = gridwidth;
c.gridheight = gridheight;
return c;
}

public static Insets i(int s) {
return new Insets(s, s, s, s);
}
Expand Down
224 changes: 93 additions & 131 deletions src/main/java/io/eiren/gui/TrackersList.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
package io.eiren.gui;

import java.awt.Color;
import java.awt.GridBagConstraints;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;

import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.border.BevelBorder;

import com.jme3.math.FastMath;
import com.jme3.math.Quaternion;
Expand All @@ -25,10 +30,11 @@
import io.eiren.vr.trackers.IMUTracker;
import io.eiren.vr.trackers.Tracker;
import io.eiren.vr.trackers.TrackerConfig;
import io.eiren.vr.trackers.TrackerMountingRotation;
import io.eiren.vr.trackers.TrackerWithBattery;
import io.eiren.vr.trackers.TrackerWithTPS;

public class TrackersList extends EJBag {
public class TrackersList extends EJBox {

Quaternion q = new Quaternion();
Vector3f v = new Vector3f();
Expand All @@ -40,149 +46,159 @@ public class TrackersList extends EJBag {
private final VRServerGUI gui;

public TrackersList(VRServer server, VRServerGUI gui) {
super();
super(BoxLayout.PAGE_AXIS);
this.server = server;
this.gui = gui;

setAlignmentY(TOP_ALIGNMENT);


server.addNewTrackerConsumer(this::newTrackerAdded);
}

@AWTThread
private void build() {
removeAll();

add(new JLabel("Tracker"), c(0, 0, 2));
add(new JLabel("Designation"), c(1, 0, 2));
add(new JLabel("X"), c(2, 0, 2));
add(new JLabel("Y"), c(3, 0, 2));
add(new JLabel("Z"), c(4, 0, 2));
add(new JLabel("Pitch"), c(5, 0, 2));
add(new JLabel("Yaw"), c(6, 0, 2));
add(new JLabel("Roll"), c(7, 0, 2));
add(new JLabel("Status"), c(8, 0, 2));
add(new JLabel("TPS"), c(9, 0, 2));
add(new JLabel("Bat"), c(10, 0, 2));
add(new JLabel("Ping"), c(11, 0, 2));


trackers.sort((tr1, tr2) -> getTrackerSort(tr1.t) - getTrackerSort(tr2.t));

Class<? extends Tracker> currentClass = null;

int n = 1;

for(int i = 0; i < trackers.size(); ++i) {
add(Box.createVerticalStrut(3));
TrackerRow tr = trackers.get(i);
Tracker t = tr.t;
if(currentClass != t.getClass()) {
currentClass = t.getClass();
add(new JLabel(currentClass.getSimpleName()), c(0, n, 5, GridBagConstraints.CENTER));
n++;
add(new JLabel(currentClass.getSimpleName()));
}

tr.build(n);
if(t.userEditable()) {
TrackerConfig cfg = server.getTrackerConfig(t);
JComboBox<String> desSelect;
add(desSelect = new JComboBox<>(), c(1, n, 2));
for(TrackerBodyPosition p : TrackerBodyPosition.values) {
desSelect.addItem(p.name());
}
if(cfg.designation != null) {
TrackerBodyPosition p = TrackerBodyPosition.getByDesignation(cfg.designation);
if(p != null)
desSelect.setSelectedItem(p.name());
}
desSelect.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
TrackerBodyPosition p = TrackerBodyPosition.valueOf(String.valueOf(desSelect.getSelectedItem()));
t.setBodyPosition(p);
server.trackerUpdated(t);
}
});
}
n += tr.getSize();
tr.build();
}
validate();
gui.refresh();
}

@VRServerThread
@ThreadSafe
public void updateTrackers() {
java.awt.EventQueue.invokeLater(() -> {
for(int i = 0; i < trackers.size(); ++i)
trackers.get(i).update();
gui.refresh();
});
}

@ThreadSafe
public void newTrackerAdded(Tracker t) {
java.awt.EventQueue.invokeLater(() -> {
if(t instanceof IMUTracker)
trackers.add(new IMUTrackerRow((IMUTracker) t));
else
trackers.add(new TrackerRow(t));
trackers.add(new TrackerRow(t));
build();
});
}

private class TrackerRow {
private class TrackerRow extends EJBag {

final Tracker t;
JLabel x;
JLabel y;
JLabel z;
JLabel a1;
JLabel a2;
JLabel a3;
JLabel position;
JLabel rotation;
JLabel status;
JLabel tps;
JLabel bat;
JLabel ping;

@AWTThread
public TrackerRow(Tracker t) {
super();
this.t = t;
}

@SuppressWarnings("unchecked")
@AWTThread
public TrackerRow build(int n) {
add(new JLabel(t.getName()), c(0, n, 2, GridBagConstraints.FIRST_LINE_START));
add(x = new JLabel("0"), c(2, n, 2, GridBagConstraints.FIRST_LINE_START));
add(y = new JLabel("0"), c(3, n, 2, GridBagConstraints.FIRST_LINE_START));
add(z = new JLabel("0"), c(4, n, 2, GridBagConstraints.FIRST_LINE_START));
add(a1 = new JLabel("0"), c(5, n, 2, GridBagConstraints.FIRST_LINE_START));
add(a2 = new JLabel("0"), c(6, n, 2, GridBagConstraints.FIRST_LINE_START));
add(a3 = new JLabel("0"), c(7, n, 2, GridBagConstraints.FIRST_LINE_START));
add(status = new JLabel(t.getStatus().toString()), c(8, n, 2, GridBagConstraints.FIRST_LINE_START));
public TrackerRow build() {
removeAll();
add(new JLabel(t.getName()), s(c(0, 0, 0, GridBagConstraints.FIRST_LINE_START), 4, 1));
if(t.userEditable()) {
TrackerConfig cfg = server.getTrackerConfig(t);
JComboBox<String> desSelect;
add(desSelect = new JComboBox<>(), s(c(0, 1, 0, GridBagConstraints.FIRST_LINE_START), 2, 1));
for(TrackerBodyPosition p : TrackerBodyPosition.values) {
desSelect.addItem(p.name());
}
if(cfg.designation != null) {
TrackerBodyPosition p = TrackerBodyPosition.getByDesignation(cfg.designation);
if(p != null)
desSelect.setSelectedItem(p.name());
}
desSelect.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
TrackerBodyPosition p = TrackerBodyPosition.valueOf(String.valueOf(desSelect.getSelectedItem()));
t.setBodyPosition(p);
server.trackerUpdated(t);
}
});
Tracker realTracker = t;
if(t instanceof ReferenceAdjustedTracker<?>)
realTracker = ((ReferenceAdjustedTracker<? extends Tracker>) t).getTracker();
if(realTracker instanceof IMUTracker) {
IMUTracker imu = (IMUTracker) realTracker;
TrackerMountingRotation tr = imu.getMountingRotation();
JComboBox<String> mountSelect;
add(mountSelect = new JComboBox<>(), s(c(2, 1, 0, GridBagConstraints.FIRST_LINE_START), 2, 1));
for(TrackerMountingRotation p : TrackerMountingRotation.values) {
mountSelect.addItem(p.name());
}
if(tr != null) {
mountSelect.setSelectedItem(tr.name());
} else {
mountSelect.setSelectedItem(TrackerMountingRotation.BACK.name());
}
mountSelect.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
TrackerMountingRotation tr = TrackerMountingRotation.valueOf(String.valueOf(mountSelect.getSelectedItem()));
imu.setMountingRotation(tr);
server.trackerUpdated(t);
}
});
}
}
add(new JLabel("Rotation"), c(0, 2, 0, GridBagConstraints.FIRST_LINE_START));
add(new JLabel("Postion"), c(1, 2, 0, GridBagConstraints.FIRST_LINE_START));
add(new JLabel("Ping"), c(2, 2, 0, GridBagConstraints.FIRST_LINE_START));
add(new JLabel("TPS"), c(3, 2, 0, GridBagConstraints.FIRST_LINE_START));
add(rotation = new JLabel("0 0 0"), c(0, 3, 0, GridBagConstraints.FIRST_LINE_START));
add(position = new JLabel("0 0 0"), c(1, 3, 0, GridBagConstraints.FIRST_LINE_START));
add(ping = new JLabel(""), c(2, 3, 0, GridBagConstraints.FIRST_LINE_START));
if(t instanceof TrackerWithTPS) {
add(tps = new JLabel("0"), c(9, n, 2, GridBagConstraints.FIRST_LINE_START));
add(tps = new JLabel("0"), c(3, 3, 0, GridBagConstraints.FIRST_LINE_START));
} else {
add(new JLabel(""), c(9, n, 2, GridBagConstraints.FIRST_LINE_START));
add(new JLabel(""), c(3, 3, 0, GridBagConstraints.FIRST_LINE_START));
}
add(bat = new JLabel("0"), c(10, n, 2, GridBagConstraints.FIRST_LINE_START));
add(ping = new JLabel(""), c(11, n, 2, GridBagConstraints.FIRST_LINE_START));
add(new JLabel("Status:"), c(0, 4, 0, GridBagConstraints.FIRST_LINE_START));
add(status = new JLabel(t.getStatus().toString().toLowerCase()), c(1, 4, 0, GridBagConstraints.FIRST_LINE_START));
add(new JLabel("Battery:"), c(2, 4, 0, GridBagConstraints.FIRST_LINE_START));
add(bat = new JLabel("0"), c(3, 4, 0, GridBagConstraints.FIRST_LINE_START));

setBorder(BorderFactory.createLineBorder(new Color(0x663399), 4, true));
TrackersList.this.add(this);
return this;
}

@AWTThread
public void update() {
if(x == null)
if(position == null)
return;
t.getRotation(q);
t.getPosition(v);
q.toAngles(angles);

x.setText(StringUtils.prettyNumber(v.x, 2));
y.setText(StringUtils.prettyNumber(v.y, 2));
z.setText(StringUtils.prettyNumber(v.z, 2));
a1.setText(StringUtils.prettyNumber(angles[0] * FastMath.RAD_TO_DEG, 0));
a2.setText(StringUtils.prettyNumber(angles[1] * FastMath.RAD_TO_DEG, 0));
a3.setText(StringUtils.prettyNumber(angles[2] * FastMath.RAD_TO_DEG, 0));
status.setText(t.getStatus().toString());
position.setText(StringUtils.prettyNumber(v.x, 1)
+ " " + StringUtils.prettyNumber(v.y, 1)
+ " " + StringUtils.prettyNumber(v.z, 1));
rotation.setText(StringUtils.prettyNumber(angles[0] * FastMath.RAD_TO_DEG, 0)
+ " " + StringUtils.prettyNumber(angles[1] * FastMath.RAD_TO_DEG, 0)
+ " " + StringUtils.prettyNumber(angles[2] * FastMath.RAD_TO_DEG, 0));
status.setText(t.getStatus().toString().toLowerCase());

if(t instanceof TrackerWithTPS) {
tps.setText(StringUtils.prettyNumber(((TrackerWithTPS) t).getTPS(), 1));
Expand All @@ -192,60 +208,6 @@ public void update() {
if(t instanceof IMUTracker)
ping.setText(String.valueOf(((IMUTracker) t).ping));
}

public int getSize() {
return 1;
}
}

private class IMUTrackerRow extends TrackerRow {

IMUTracker it;
JLabel mx;
JLabel my;
JLabel mz;
JLabel ax;
JLabel ay;
JLabel az;

public IMUTrackerRow(IMUTracker t) {
super(t);
it = t;
}

@Override
public TrackerRow build(int n) {
super.build(n);
/*n++;
add(new JLabel("Mag / Accel:"), c(0, n, 2, GridBagConstraints.FIRST_LINE_END));
add(mx = new JLabel("0"), c(2, n, 2, GridBagConstraints.FIRST_LINE_START));
add(my = new JLabel("0"), c(3, n, 2, GridBagConstraints.FIRST_LINE_START));
add(mz = new JLabel("0"), c(4, n, 2, GridBagConstraints.FIRST_LINE_START));
add(ax = new JLabel("0"), c(5, n, 2, GridBagConstraints.FIRST_LINE_START));
add(ay = new JLabel("0"), c(6, n, 2, GridBagConstraints.FIRST_LINE_START));
add(az = new JLabel("0"), c(7, n, 2, GridBagConstraints.FIRST_LINE_START));*/
return this;
}

@Override
public void update() {
super.update();
/*q.lookAt(it.magVector, Vector3f.UNIT_Y);
q.toAngles(angles);
mx.setText(StringUtils.prettyNumber(angles[0] * FastMath.RAD_TO_DEG, 0));
my.setText(StringUtils.prettyNumber(angles[1] * FastMath.RAD_TO_DEG, 0));
mz.setText(StringUtils.prettyNumber(angles[2] * FastMath.RAD_TO_DEG, 0));
q.lookAt(it.accelVector, Vector3f.UNIT_Y);
q.toAngles(angles);
ax.setText(StringUtils.prettyNumber(angles[0] * FastMath.RAD_TO_DEG, 0));
ay.setText(StringUtils.prettyNumber(angles[1] * FastMath.RAD_TO_DEG, 0));
az.setText(StringUtils.prettyNumber(angles[2] * FastMath.RAD_TO_DEG, 0));*/
}

@Override
public int getSize() {
return super.getSize() + 1;
}
}

private static int getTrackerSort(Tracker t) {
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/io/eiren/vr/VRServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,8 @@ public void addNewTrackerConsumer(Consumer<Tracker> consumer) {
public void trackerUpdated(Tracker tracker) {
queueTask(() -> {
humanPoseProcessor.trackerUpdated(tracker);
TrackerConfig tc = getTrackerConfig(tracker);
tracker.saveConfig(tc);
saveConfig();
});
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,33 +1,29 @@
package io.eiren.vr.trackers;

public class IMUReferenceAdjustedTracker<T extends IMUTracker & TrackerWithTPS & TrackerWithBattery> extends ReferenceAdjustedTracker implements TrackerWithTPS, TrackerWithBattery {
public class IMUReferenceAdjustedTracker<T extends IMUTracker & TrackerWithTPS & TrackerWithBattery> extends ReferenceAdjustedTracker<T> implements TrackerWithTPS, TrackerWithBattery {

public IMUReferenceAdjustedTracker(T tracker) {
super(tracker);
}

@SuppressWarnings("unchecked")
@Override
public float getBatteryLevel() {
return ((T) tracker).getBatteryLevel();
return tracker.getBatteryLevel();
}

@SuppressWarnings("unchecked")
@Override
public float getBatteryVoltage() {
return ((T) tracker).getBatteryVoltage();
return tracker.getBatteryVoltage();
}

@SuppressWarnings("unchecked")
@Override
public float getTPS() {
return ((T) tracker).getTPS();
return tracker.getTPS();
}

@SuppressWarnings("unchecked")
@Override
public void dataTick() {
((T) tracker).dataTick();
tracker.dataTick();
}

}
Loading

0 comments on commit 4033278

Please sign in to comment.