updated relational graph as well as fixed minor loop check issue

This commit is contained in:
Harrison Deng 2017-09-03 17:30:33 -05:00
parent db902e59e1
commit 3e41c17d10
2 changed files with 127 additions and 61 deletions

View File

@ -76,7 +76,7 @@ public class AudioGraph extends Actor {
case 1: case 1:
shapeRender.setColor(0f, 0f, 1f, 0.75f); shapeRender.setColor(0f, 0f, 1f, 0.75f);
for (int x = 0; x < getHeight() -1; x++) { for (int x = 0; x < getWidth(); x++) {
try { try {
shapeRender.line( shapeRender.line(
getX()+x, getX()+x,

View File

@ -2,81 +2,147 @@ package zero1hd.rhythmbullet.ui.builders;
import com.badlogic.gdx.Gdx; import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input.Keys; import com.badlogic.gdx.Input.Keys;
import com.badlogic.gdx.graphics.Pixmap;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.Pixmap.Format;
import com.badlogic.gdx.graphics.g2d.Batch; import com.badlogic.gdx.graphics.g2d.Batch;
import com.badlogic.gdx.graphics.glutils.ShapeRenderer;
import com.badlogic.gdx.graphics.glutils.ShapeRenderer.ShapeType;
import com.badlogic.gdx.math.MathUtils;
import com.badlogic.gdx.scenes.scene2d.Actor; import com.badlogic.gdx.scenes.scene2d.Actor;
import com.badlogic.gdx.utils.FloatArray; import com.badlogic.gdx.utils.FloatArray;
import zero1hd.rhythmbullet.audio.CoreMusicInfo;
public class AudioGraphRelation extends Actor { public class AudioGraphRelation extends Actor {
Pixmap audioGraph; private CoreMusicInfo audioData;
Texture textureOfGraph; ShapeRenderer shapeRender;
FloatArray mainGraph; FloatArray mainGraph;
FloatArray overlayGraph; FloatArray overlayGraph;
int audioDataIndex; int dataIndex;
int displayMode;
public AudioGraphRelation(FloatArray mainGraph, FloatArray overlayGraph, int graphSizeW, int graphSizeH) { public float normalDataG1 = 1f, normalDataG2 = 1f, avgG1 = 1f, avgG2 = 1f;
audioGraph = new Pixmap(graphSizeW, graphSizeH, Format.RGBA8888); float scale;
audioGraph.setColor(0.1f, 0.1f, 0.1f, 0.75f); public AudioGraphRelation(int graphSizeW, int graphSizeH) {
audioGraph.fill(); shapeRender = new ShapeRenderer();
setSize(graphSizeW, graphSizeH);
textureOfGraph = new Texture(audioGraph); scale = graphSizeH;
setWidth(Gdx.graphics.getWidth());
setHeight(280);
this.mainGraph = mainGraph;
this.overlayGraph = overlayGraph;
} }
public void setAudioDataIndex(int audioDataIndex) { public void setAudioDataIndex(int audioDataIndex) {
this.audioDataIndex = audioDataIndex; this.dataIndex = audioDataIndex;
}
float scale = 0.2f;
@Override
public void act(float delta) {
audioGraph.setColor(0f, 0f, 0f, 0.75f);
audioGraph.fill();
if (Gdx.input.isKeyPressed(Keys.COMMA)) {
if (scale > 0.02) {
scale -= 0.005f;
}
} else if (Gdx.input.isKeyPressed(Keys.PERIOD)) {
if (scale < 1) {
scale += 0.005f;
}
}
audioGraph.setColor(0f, 1f, 1f, 0.75f);
for (int x = 0; x < audioGraph.getWidth() -1; x++) {
try {
audioGraph.drawLine(x, (int) (audioGraph.getHeight()-mainGraph.get(audioDataIndex+x-audioGraph.getWidth()/2)*scale), x+1, (int) (audioGraph.getHeight()-mainGraph.get(audioDataIndex+x+1-audioGraph.getWidth()/2)*scale));
} catch (IndexOutOfBoundsException e) {
}
}
audioGraph.setColor(0f, 0.25f, 1f, 0.75f);
for (int x = 0; x < audioGraph.getWidth() -1; x++) {
try {
audioGraph.drawLine(x, (int) (audioGraph.getHeight()-overlayGraph.get(audioDataIndex+x-audioGraph.getWidth()/2)*scale), x+1, (int) (audioGraph.getHeight()-overlayGraph.get(audioDataIndex+x+1-audioGraph.getWidth()/2)*scale));
} catch (IndexOutOfBoundsException e) {
}
}
audioGraph.setColor(0f, 0f, 1, 0.95f);
audioGraph.drawLine(audioGraph.getWidth()/2, 0, audioGraph.getWidth()/2, audioGraph.getHeight());
textureOfGraph.draw(audioGraph, 0, 0);
super.act(delta);
} }
@Override @Override
public void draw(Batch batch, float parentAlpha) { public void draw(Batch batch, float parentAlpha) {
batch.draw(textureOfGraph, getX(), getY(), getWidth(), getHeight()); batch.end();
shapeRender.begin(ShapeType.Line);
shapeRender.setColor(0f, 0f, 0f, 0.35f);
shapeRender.rect(getX(), getY(), getWidth(), getHeight());
if (overlayGraph != null) {
if (Gdx.input.isKeyPressed(Keys.COMMA)) {
if (scale > 0.05f) {
scale -= 0.25f;
}
} else if (Gdx.input.isKeyPressed(Keys.PERIOD)) {
if (scale < getHeight()*2) {
scale += 0.4f;
}
}
if (Gdx.input.isKeyJustPressed(Keys.M)) {
Gdx.app.debug("Graph", "Switching to another graph.");
displayMode++;
if (displayMode == 3) {
displayMode = 0;
}
}
}
switch (displayMode) {
case 0:
shapeRender.setColor(1f, 0f, 0f, 1f);
for (int x = 0; x < getHeight() -1; x++) {
try {
shapeRender.line(
getX()+x,
getY(),
getX()+x+1,
(int) ((mainGraph.get((int) (dataIndex+x +1 -getWidth()/2))/normalDataG1)*scale)+getY());
} catch (NullPointerException | IndexOutOfBoundsException e) {
}
shapeRender.line(
getX(),
MathUtils.round(scale*(avgG1/normalDataG1))+getY(),
getWidth() + getX(),
MathUtils.round(scale*(avgG1/normalDataG1))+getY());
}
case 1:
shapeRender.setColor(0f, 0f, 1f, 0.75f);
for (int x = 0; x < getHeight() -1; x++) {
try {
shapeRender.line(
getX()+x,
getY(),
getX()+x+1,
(int) ((overlayGraph.get((int) (dataIndex+x +1 -getWidth()/2))/normalDataG2)*scale)+getY());
} catch (NullPointerException | IndexOutOfBoundsException e) {
}
shapeRender.line(
getX(),
MathUtils.round(scale*(avgG2/normalDataG2)) + getY(),
getWidth() + getX(),
MathUtils.round(scale*(avgG2/normalDataG2))+getY());
}
break;
case 2:
shapeRender.setColor(1f, 0f, 0f, 0.75f);
for (int x = 0; x < getWidth(); x++) {
try {
shapeRender.line(
getX()+x,
getY(),
getX()+x+1,
(int) ((mainGraph.get((int) (dataIndex+x +1 -getWidth()/2))/normalDataG1)*scale)+getY());
} catch (NullPointerException | IndexOutOfBoundsException e) {
}
shapeRender.line(
getX(),
MathUtils.round(scale*(avgG1/normalDataG1))+getY(),
getWidth() + getX(),
MathUtils.round(scale*(avgG1/normalDataG1))+getY());
}
break;
}
shapeRender.setColor(0f, 1f, 0f, 1f);
shapeRender.line(getX()+getWidth()/2, 0, getX()+getWidth()/2, getHeight()+getY());
shapeRender.end();
batch.begin();
super.draw(batch, parentAlpha); super.draw(batch, parentAlpha);
} }
@Override
public void act(float delta) {
if (audioData != null) {
setAudioDataIndex(audioData.getReadIndex());
}
super.act(delta);
}
/**
* Set what arrays to graph
* @param dataSet1 first audio data set
* @param dataSet2 overlay graph. This one can be null.
* @param audioData actual basic audio information for index position
*/
public void setGraphingData(FloatArray dataSet1, FloatArray dataSet2, CoreMusicInfo audioData) {
this.mainGraph = dataSet1;
this.overlayGraph = dataSet2;
if (dataSet2 == null) {
displayMode = 2;
} else {
displayMode = 0;
}
this.audioData = audioData;
}
} }