Skip to content

Commit

Permalink
Add tikzpicture generator to the viewer (key=T)
Browse files Browse the repository at this point in the history
  • Loading branch information
Rob--W committed Jul 4, 2014
1 parent 8531e36 commit ac888bd
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/tue/algorithms/viewer/Engine.java
Expand Up @@ -121,6 +121,7 @@ private void showHelpDialog() {
+ "F: Flip the screen \n"
+ "O: Open input file \n"
+ "S: Save input file \n\n"
+ "T: Save output as a tikz picture \n\n"
+ "Left mouse button: Add node \n"
+ "Right mouse button: Delete nodes \n\n"
+ "ESC: Close Simulation \n"
Expand Down
74 changes: 74 additions & 0 deletions src/tue/algorithms/viewer/Simulation.java
Expand Up @@ -107,6 +107,7 @@ public static NetworkImplementation getNetworkImplementation() {
private boolean twoKeyDown;
private boolean threeKeyDown;
private boolean saveKeyDown;
private boolean tikzKeyDown;
private boolean openKeyDown;
private boolean escKeyDown;
private boolean flipKeyDown;
Expand All @@ -128,6 +129,7 @@ public Simulation() {
clearKeyDown = false;
showSegments = true;
saveKeyDown = false;
tikzKeyDown = false;
openKeyDown = false;
oneKeyDown = false;
twoKeyDown = false;
Expand Down Expand Up @@ -207,6 +209,11 @@ public void getInput() throws IOException {
if (Keyboard.getEventKey() == Keyboard.KEY_S) {
saveKeyDown = !saveKeyDown && Keyboard.getEventKeyState();
}

// Save as tikz
if (Keyboard.getEventKey() == Keyboard.KEY_T) {
tikzKeyDown = !tikzKeyDown && Keyboard.getEventKeyState();
}

// Open
if (Keyboard.getEventKey() == Keyboard.KEY_O) {
Expand Down Expand Up @@ -256,6 +263,12 @@ public KeyboardValue processInput() throws FileNotFoundException{
save();
saveKeyDown = false;
}

if (tikzKeyDown) {
saveAsTikzPicture();
tikzKeyDown = false;
}


if (openKeyDown){
open();
Expand Down Expand Up @@ -406,6 +419,49 @@ private void buildFile(File file){
Logger.getLogger(Simulation.class.getName()).log(Level.SEVERE, null, ex);
}
}

private void buildTikzPicture(PrintStream os) {
// How to incliude them? Pick whatever you prefer:
// http://tex.stackexchange.com/questions/79594/outsourcing-tikz-code
// os.println("\\documentclass{standalone}");
// os.println("\\usepackage{tikz}");
// os.println("\\begin{document}");
os.println("%% Include the picture as follows: \\");
os.println("% \\begingroup");
os.println("% \\tikzset{every picture/.style={scale=1,radius=0.05,line width=0.4}}%");
os.println("% \\input{tikz/[filename].tex}%");
os.println("% \\endgroup");
os.println("%");
os.println("%% Want to add a label? Use node[<position>]{label},");
os.println("%% where <position> is one of: below / above / left / right");
os.println("% \\fill (0,0) circle node[below]{$p_1$};");
os.println("%% Want to give the point a different color?");
os.println("% \\fill[red] (0,0) circle;");
os.println("%");
os.println("\\begin{tikzpicture}");
for (Segment seg : segments) {
os.printf("\\draw (%.4f,%.4f) -- (%.4f,%.4f);\n",
tikzX(seg.node1), tikzY(seg.node1),
tikzX(seg.node2), tikzY(seg.node2));
}
for (Node node : nodes) {
os.printf("\\fill (%.4f,%.4f) circle;\n", tikzX(node), tikzY(node));
}
os.println("\\end{tikzpicture}");
// os.println("\\end{document}")
}
// Helper functions, float formatted as int (multiplier to be kept in sync with printf flag).
// Assuming that most if not all coordinates are [0,1)
private float tikzX(Node node) {
return node.x * 10f;
}
private float tikzY(Node node) {
float y = node.y;
if (Camera.flipped) {
y = 1f - y;
}
return y * 10f;
}

private void save() {
JFileChooser saveFile = new JFileChooser(prefs.get("loc", null));
Expand All @@ -421,6 +477,24 @@ private void save() {
}
}
}
private void saveAsTikzPicture() {
JFileChooser exportFile = new JFileChooser(prefs.get("exportLoc", prefs.get("loc", null)));
exportFile.showSaveDialog(null);
if (exportFile.getSelectedFile() != null) {
File file = exportFile.getSelectedFile();
try {
prefs.put("exportLoc", file.getParent());
file.createNewFile();
try (PrintStream fileStream = new PrintStream(file)) {
buildTikzPicture(fileStream);
} catch (IOException e) {
throw e;
}
} catch (IOException ex) {
Logger.getLogger(Simulation.class.getName()).log(Level.SEVERE, null, ex);
}
}
}

private void open() throws FileNotFoundException {
JFileChooser openFile = new JFileChooser(prefs.get("loc", null));
Expand Down

0 comments on commit ac888bd

Please sign in to comment.