Skip to content

Commit

Permalink
resolves #8 + other improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
varun7654 committed Oct 7, 2021
1 parent b719f09 commit 03edd14
Show file tree
Hide file tree
Showing 9 changed files with 128 additions and 11 deletions.
1 change: 1 addition & 0 deletions core/src/me/varun/autobuilder/AutoBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,7 @@ private void draw() {

//Initialize our camera for the batch
batch.setProjectionMatrix(cam.combined);
shapeRenderer.setPixelSize(cam.zoom);

//Draw the image
batch.begin();
Expand Down
2 changes: 1 addition & 1 deletion core/src/me/varun/autobuilder/UndoHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public void update(Gui gui, @NotNull ShaderProgram fontShader, @NotNull BitmapFo
} else pointer = 0;
} else {
//Undoing
System.out.println(undoHistory);
//System.out.println(undoHistory);
pointer++;
if (pointer < undoHistory.size()) {
restoreState(undoHistory.get(pointer), gui, fontShader, font, inputEventThrower, cameraHandler);
Expand Down
6 changes: 5 additions & 1 deletion core/src/me/varun/autobuilder/gui/Gui.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import me.varun.autobuilder.gui.elements.AddPathButton;
import me.varun.autobuilder.gui.elements.AddScriptButton;
import me.varun.autobuilder.gui.elements.PushAutoButton;
import me.varun.autobuilder.gui.notification.NotificationHandler;
import me.varun.autobuilder.util.MathUntil;
import me.varun.autobuilder.util.RoundedShapeRenderer;
import org.jetbrains.annotations.NotNull;
Expand Down Expand Up @@ -60,6 +61,8 @@ public class Gui extends InputEventListener {
color.fromHsv(1, 1, 1);
}

NotificationHandler notificationHandler = new NotificationHandler();

public Gui(@NotNull Viewport viewport, @NotNull BitmapFont font, @NotNull ShaderProgram fontShader,
@NotNull InputEventThrower eventThrower, @NotNull ExecutorService executorService, @NotNull CameraHandler cameraHandler) {
this.viewport = viewport;
Expand All @@ -81,7 +84,6 @@ public Gui(@NotNull Viewport viewport, @NotNull BitmapFont font, @NotNull Shader
}

public void render(@NotNull ShapeDrawer shapeRenderer, @NotNull SpriteBatch spriteBatch, @NotNull Camera camera) {

shapeRenderer.setColor(Color.WHITE);
RoundedShapeRenderer.roundedRect(shapeRenderer, panelX, panelY, panelWidth, panelHeight, 5);

Expand Down Expand Up @@ -136,6 +138,8 @@ public void render(@NotNull ShapeDrawer shapeRenderer, @NotNull SpriteBatch spri
ScissorStack.popScissors();
}

notificationHandler.processNotification(shapeRenderer, spriteBatch, font, fontShader);

maxScroll = Math.max(0, -(yPos - (int) smoothScrollPos - 10));
//System.out.println(maxScroll);
}
Expand Down
5 changes: 0 additions & 5 deletions core/src/me/varun/autobuilder/gui/NotificationHandler.java

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ public PushAutoButton(int x, int y, int width, int height) {
@Override
public boolean checkClick(@NotNull Gui gui) {
if (super.checkClick(gui)) {

networkTables.pushData(gui.guiItems);
return true;
}
Expand Down
58 changes: 58 additions & 0 deletions core/src/me/varun/autobuilder/gui/notification/Notification.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package me.varun.autobuilder.gui.notification;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.g2d.Batch;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.g2d.GlyphLayout;
import com.badlogic.gdx.graphics.glutils.ShaderProgram;
import me.varun.autobuilder.util.RoundedShapeRenderer;
import space.earlygrey.shapedrawer.ShapeDrawer;

public class Notification {
private final long deleteTime;
private final long creationTime;
private final Color color;
private final String text;

private static final long ANIMATE_IN_OUT_TIME = 100; //ns

/**
*
* @param color Color of the background of the notification
* @param text Text in the notification
* @param duration duration of the notification (ms)
*/
public Notification(Color color, String text, long duration){
this.color = color;
this.text = text;
this.creationTime = System.currentTimeMillis();
this.deleteTime = this.creationTime + (duration);
}

@Override
public String toString() {
return "Notification{" +
"deleteTime=" + deleteTime +
", creationTime=" + creationTime +
", color=" + color +
", text='" + text + '\'' +
'}';
}

public boolean tick(ShapeDrawer drawer, Batch batch, BitmapFont font, ShaderProgram fontShader){
long now = System.currentTimeMillis();
float renderHeight = (Gdx.graphics.getHeight()) + Math.min(Math.min((float) (now - creationTime) / ANIMATE_IN_OUT_TIME, (float) (deleteTime - now) / ANIMATE_IN_OUT_TIME), 1) * (-50);
RoundedShapeRenderer.roundedRect(drawer, Gdx.graphics.getWidth()/2f - (700/2f), renderHeight, 700, 40, 5, color);

batch.setShader(fontShader);
batch.setColor(Color.WHITE);
font.getData().setScale(27 / 64f);
GlyphLayout glyphLayout = new GlyphLayout();
glyphLayout.setText(font, text, Color.WHITE, 700, 1, false);
font.draw(batch, glyphLayout, Gdx.graphics.getWidth()/2f - (700/2f), renderHeight+30);
batch.setShader(null);

return deleteTime < now;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package me.varun.autobuilder.gui.notification;

import com.badlogic.gdx.graphics.g2d.Batch;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.glutils.ShaderProgram;
import space.earlygrey.shapedrawer.ShapeDrawer;

import java.util.ArrayList;

public class NotificationHandler {
private final static ArrayList<Notification> notifications = new ArrayList<>();

public static void addNotification(Notification notification){
notifications.add(notification);
}

ArrayList<Notification> notificationsToDelete = new ArrayList<>();
public void processNotification(ShapeDrawer drawer, Batch batch, BitmapFont font, ShaderProgram fontShader){
notificationsToDelete.clear();
for (Notification notification : notifications) {
if (notification.tick(drawer, batch, font, fontShader)) {
notificationsToDelete.add(notification);
}
}

for (Notification notification : notificationsToDelete) {
notifications.remove(notification);
}
}

}
29 changes: 28 additions & 1 deletion core/src/me/varun/autobuilder/net/NetworkTablesHelper.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
package me.varun.autobuilder.net;

import com.badlogic.gdx.graphics.Color;
import edu.wpi.first.networktables.NetworkTable;
import edu.wpi.first.networktables.NetworkTableEntry;
import edu.wpi.first.networktables.NetworkTableInstance;
import me.varun.autobuilder.gui.elements.AbstractGuiItem;
import me.varun.autobuilder.gui.notification.Notification;
import me.varun.autobuilder.gui.notification.NotificationHandler;
import me.varun.autobuilder.serialization.Autonomous;
import me.varun.autobuilder.serialization.GuiSerializer;

Expand All @@ -23,7 +26,10 @@ public class NetworkTablesHelper {
NetworkTableEntry xPos = position.getEntry("x");
NetworkTableEntry yPos = position.getEntry("y");
NetworkTableEntry enabledTable = table.getEntry("enabled");
NetworkTableEntry processingTable = table.getEntry("processing");

private boolean enabled = false;
private double processing = 0;

private NetworkTablesHelper() {

Expand All @@ -35,9 +41,13 @@ public static NetworkTablesHelper getInstance() {

public void start() {
inst.startClientTeam(3476); // where TEAM=190, 294, etc, or use inst.startClient("hostname") or similar
//inst.startDSClient(); // recommended if running on DS computer; this gets the robot IP from the DS
inst.startDSClient(); // recommended if running on DS computer; this gets the robot IP from the DS
}


private static final Color LIGHT_GREEN = Color.valueOf("8FEC8F");


public void pushData(List<AbstractGuiItem> guiItemList) {

if (inst.isConnected()) {
Expand All @@ -47,12 +57,16 @@ public void pushData(List<AbstractGuiItem> guiItemList) {
Autonomous autonomous = Serializer.deserialize(autoPath.getString(null));
System.out.println("Sent Data: " + autonomous);

NotificationHandler.addNotification(new Notification(LIGHT_GREEN, "Auto Uploaded", 2000 ));

} catch (IOException | ClassNotFoundException e) {
e.printStackTrace();
System.exit(-1);
NotificationHandler.addNotification(new Notification(Color.RED, "Auto Failed to Upload", 2000 ));
}
} else {
System.out.println("Cannot Send Data; Not Connected");
NotificationHandler.addNotification(new Notification(Color.RED, "Auto Failed to Upload: NOT CONNECTED", 2000 ));
}

}
Expand All @@ -70,10 +84,23 @@ public void updateRobotPath() {
if (robotPositions.size() < 1 || (robotPositions.get(robotPositions.size() - 1)[0] != x || robotPositions.get(robotPositions.size() - 1)[1] != y)) {
robotPositions.add(new Float[]{x / INCHES_PER_METER, y / INCHES_PER_METER});
}

} else {
enabled = false;
}

if(processingTable.getDouble(0) != processing){
processing = processingTable.getDouble(0);
if(processing == 1){
NotificationHandler.addNotification(new Notification(Color.CORAL, "The Roborio has started deserializing the auto", 1500));
} else if (processing == 2){
NotificationHandler.addNotification(new Notification(LIGHT_GREEN, "The Roborio has finished deserializing the auto", 1500));
} else {
NotificationHandler.addNotification(new Notification(LIGHT_GREEN, "The Roborio has set: " + processing, 1500));
}
}


}

}
Expand Down
6 changes: 3 additions & 3 deletions core/src/me/varun/autobuilder/pathing/PathRenderer.java
Original file line number Diff line number Diff line change
Expand Up @@ -75,13 +75,13 @@ public void setPathChangeListener(@NotNull PathChangeListener pathChangeListener

public void render(@NotNull ShapeDrawer renderer, @NotNull OrthographicCamera cam) {
if (trajectory == null) return;
for (double i = 0.05; i < trajectory.getTotalTimeSeconds(); i += 0.05) {
Pose2d prev = trajectory.sample(i - 0.05).poseMeters;
for (double i = 0.01; i < trajectory.getTotalTimeSeconds(); i += 0.01) {
Pose2d prev = trajectory.sample(i - 0.01).poseMeters;
Pose2d cur = trajectory.sample(i).poseMeters;
double speed = Math.abs(trajectory.sample(i).velocityMetersPerSecond);
float[] color = new float[3];
this.color.toHsv(color);
color[1] = (float) (0.9 * (speed / maxVelocityMetersPerSecond) + 0.05);
color[1] = (float) (0.9 * (speed / maxVelocityMetersPerSecond) + 0.1);
Color speedColor = new Color().fromHsv(color);
speedColor.set(speedColor.r, speedColor.g, speedColor.b, 1);
renderer.line((float) prev.getX() * POINT_SCALE_FACTOR, (float) prev.getY() * POINT_SCALE_FACTOR,
Expand Down

0 comments on commit 03edd14

Please sign in to comment.