use of processing.mode.java.tweak.Handle in project processing by processing.
the class JavaTextAreaPainter method startTweakMode.
protected void startTweakMode() {
addMouseListener(new MouseListener() {
@Override
public void mouseReleased(MouseEvent e) {
if (mouseHandle != null) {
mouseHandle.resetProgress();
mouseHandle = null;
updateCursor(e.getX(), e.getY());
repaint();
}
}
@Override
public void mousePressed(MouseEvent e) {
int currentTab = getCurrentCodeIndex();
// check for clicks on number handles
for (Handle n : handles.get(currentTab)) {
if (n.pick(e.getX(), e.getY())) {
cursorType = -1;
JavaTextAreaPainter.this.setCursor(blankCursor);
mouseHandle = n;
mouseHandle.setCenterX(e.getX());
repaint();
return;
}
}
// check for clicks on color boxes
for (ColorControlBox box : colorBoxes.get(currentTab)) {
if (box.pick(e.getX(), e.getY())) {
if (colorSelector != null) {
// we already show a color selector, close it
colorSelector.frame.dispatchEvent(new WindowEvent(colorSelector.frame, WindowEvent.WINDOW_CLOSING));
}
colorSelector = new ColorSelector(box);
colorSelector.frame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
colorSelector.frame.setVisible(false);
colorSelector = null;
}
});
colorSelector.show(getLocationOnScreen().x + e.getX() + 30, getLocationOnScreen().y + e.getY() - 130);
}
}
}
@Override
public void mouseExited(MouseEvent e) {
}
@Override
public void mouseEntered(MouseEvent e) {
}
@Override
public void mouseClicked(MouseEvent e) {
}
});
addMouseMotionListener(new MouseMotionListener() {
@Override
public void mouseMoved(MouseEvent e) {
updateCursor(e.getX(), e.getY());
if (!Settings.alwaysShowColorBoxes) {
showHideColorBoxes(e.getY());
}
}
@Override
public void mouseDragged(MouseEvent e) {
if (mouseHandle != null) {
// set the current drag amount of the arrows
mouseHandle.setCurrentX(e.getX());
// update code text with the new value
updateCodeText();
if (colorSelector != null) {
colorSelector.refreshColor();
}
repaint();
}
}
});
tweakMode = true;
setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
repaint();
}
use of processing.mode.java.tweak.Handle in project processing by processing.
the class JavaTextAreaPainter method updateCursor.
private void updateCursor(int mouseX, int mouseY) {
int currentTab = getCurrentCodeIndex();
for (Handle n : handles.get(currentTab)) {
if (n.pick(mouseX, mouseY)) {
cursorType = Cursor.W_RESIZE_CURSOR;
setCursor(new Cursor(cursorType));
return;
}
}
for (ColorControlBox colorBox : colorBoxes.get(currentTab)) {
if (colorBox.pick(mouseX, mouseY)) {
cursorType = Cursor.HAND_CURSOR;
setCursor(new Cursor(cursorType));
return;
}
}
if (cursorType == Cursor.W_RESIZE_CURSOR || cursorType == Cursor.HAND_CURSOR || cursorType == -1) {
cursorType = Cursor.DEFAULT_CURSOR;
setCursor(new Cursor(cursorType));
}
}
use of processing.mode.java.tweak.Handle in project processing by processing.
the class JavaEditor method automateSketch.
/*
private void removeSpacesFromCode() {
SketchCode[] code = sketch.getCode();
for (int i=0; i<code.length; i++) {
String c = code[i].getProgram();
//c = c.substring(SPACE_AMOUNT, c.length() - SPACE_AMOUNT);
code[i].setProgram(c);
// TODO Wild Hack: set document to null so the text editor will refresh
// the program contents when the document tab is being clicked
code[i].setDocument(null);
}
// this will update the current code
setCode(sketch.getCurrentCode());
}
*/
/**
* Replace all numbers with variables and add code to initialize
* these variables and handle update messages.
*/
protected boolean automateSketch(Sketch sketch, SketchParser parser) {
SketchCode[] code = sketch.getCode();
List<List<Handle>> handles = parser.allHandles;
if (code.length < 1) {
return false;
}
if (handles.size() == 0) {
return false;
}
int afterSizePos = SketchParser.getAfterSizePos(baseCode[0]);
if (afterSizePos < 0) {
return false;
}
// get port number from preferences.txt
int port;
String portStr = Preferences.get(PREF_TWEAK_PORT);
if (portStr == null) {
Preferences.set(PREF_TWEAK_PORT, "auto");
portStr = "auto";
}
if (portStr.equals("auto")) {
// random port for udp (0xc000 - 0xffff)
port = (int) (Math.random() * 0x3fff) + 0xc000;
} else {
port = Preferences.getInteger(PREF_TWEAK_PORT);
}
// create the client that will send the new values to the sketch
tweakClient = new TweakClient(port);
// update handles with a reference to the client object
for (int tab = 0; tab < code.length; tab++) {
for (Handle h : handles.get(tab)) {
h.setTweakClient(tweakClient);
}
}
// loop through all tabs in the current sketch
for (int tab = 0; tab < code.length; tab++) {
int charInc = 0;
String c = baseCode[tab];
for (Handle n : handles.get(tab)) {
// replace number value with a variable
c = replaceString(c, n.startChar + charInc, n.endChar + charInc, n.name);
charInc += n.name.length() - n.strValue.length();
}
code[tab].setProgram(c);
}
// add the main header to the code in the first tab
String c = code[0].getProgram();
// header contains variable declaration, initialization,
// and OSC listener function
String header;
header = "\n\n" + "/*************************/\n" + "/* MODIFIED BY TWEAKMODE */\n" + "/*************************/\n" + "\n\n";
// add needed OSC imports and the global OSC object
header += "import java.net.*;\n";
header += "import java.io.*;\n";
header += "import java.nio.*;\n\n";
// write a declaration for int and float arrays
int numOfInts = howManyInts(handles);
int numOfFloats = howManyFloats(handles);
if (numOfInts > 0) {
header += "int[] tweakmode_int = new int[" + numOfInts + "];\n";
}
if (numOfFloats > 0) {
header += "float[] tweakmode_float = new float[" + numOfFloats + "];\n\n";
}
// add the server code that will receive the value change messages
// header += TweakClient.getServerCode(port, numOfInts>0, numOfFloats>0);
header += "TweakModeServer tweakmode_Server;\n";
header += "void tweakmode_initAllVars() {\n";
//for (int i=0; i<handles.length; i++) {
for (List<Handle> list : handles) {
//for (Handle n : handles[i]) {
for (Handle n : list) {
header += " " + n.name + " = " + n.strValue + ";\n";
}
}
header += "}\n\n";
header += "void tweakmode_initCommunication() {\n";
header += " tweakmode_Server = new TweakModeServer();\n";
header += " tweakmode_Server.setup();\n";
header += " tweakmode_Server.start();\n";
header += "}\n";
header += "\n\n\n\n\n";
// add call to our initAllVars and initOSC functions
// from the setup() function.
String addToSetup = "\n\n\n" + " /* TWEAKMODE */\n" + " tweakmode_initAllVars();\n" + " tweakmode_initCommunication();\n" + " /* TWEAKMODE */\n\n";
afterSizePos = SketchParser.getAfterSizePos(c);
c = replaceString(c, afterSizePos, afterSizePos, addToSetup);
// Server code defines a class, so it should go later in the sketch
String serverCode = TweakClient.getServerCode(port, numOfInts > 0, numOfFloats > 0);
code[0].setProgram(header + c + serverCode);
// print out modified code
String showModCode = Preferences.get(PREF_TWEAK_SHOW_CODE);
if (showModCode == null) {
Preferences.setBoolean(PREF_TWEAK_SHOW_CODE, false);
}
if (Preferences.getBoolean(PREF_TWEAK_SHOW_CODE)) {
System.out.println("\nTweakMode modified code:\n");
for (int i = 0; i < code.length; i++) {
System.out.println("tab " + i + "\n");
System.out.println("=======================================================\n");
System.out.println(code[i].getProgram());
}
}
return true;
}
use of processing.mode.java.tweak.Handle in project processing by processing.
the class JavaEditor method initEditorCode.
protected void initEditorCode(List<List<Handle>> handles, boolean withSpaces) {
SketchCode[] sketchCode = sketch.getCode();
for (int tab = 0; tab < baseCode.length; tab++) {
// beautify the numbers
int charInc = 0;
String code = baseCode[tab];
for (Handle n : handles.get(tab)) {
int s = n.startChar + charInc;
int e = n.endChar + charInc;
String newStr = n.strNewValue;
if (withSpaces) {
newStr = " " + newStr + " ";
}
code = replaceString(code, s, e, newStr);
n.newStartChar = n.startChar + charInc;
charInc += n.strNewValue.length() - n.strValue.length();
if (withSpaces) {
charInc += 4;
}
n.newEndChar = n.endChar + charInc;
}
sketchCode[tab].setProgram(code);
/* Wild Hack: set document to null so the text editor will refresh
the program contents when the document tab is being clicked */
sketchCode[tab].setDocument(null);
}
// this will update the current code
setCode(sketch.getCurrentCode());
}
use of processing.mode.java.tweak.Handle in project processing by processing.
the class JavaTextAreaPainter method updateTweakInterfacePositions.
/**
* Initialize all the number changing interfaces.
* synchronize this method to prevent the execution of 'paint' in the middle.
* (don't paint while we make changes to the text of the editor)
*/
private synchronized void updateTweakInterfacePositions() {
SketchCode[] code = getEditor().getSketch().getCode();
int prevScroll = textArea.getVerticalScrollPosition();
String prevText = textArea.getText();
for (int tab = 0; tab < code.length; tab++) {
String tabCode = getJavaEditor().baseCode[tab];
textArea.setText(tabCode);
for (Handle n : handles.get(tab)) {
int lineStartChar = textArea.getLineStartOffset(n.line);
int x = textArea.offsetToX(n.line, n.newStartChar - lineStartChar);
int end = textArea.offsetToX(n.line, n.newEndChar - lineStartChar);
int y = textArea.lineToY(n.line) + fm.getHeight() + 1;
n.initInterface(x, y, end - x, fm.getHeight());
}
for (ColorControlBox cBox : colorBoxes.get(tab)) {
int lineStartChar = textArea.getLineStartOffset(cBox.getLine());
int x = textArea.offsetToX(cBox.getLine(), cBox.getCharIndex() - lineStartChar);
int y = textArea.lineToY(cBox.getLine()) + fm.getDescent();
cBox.initInterface(this, x, y + 1, fm.getHeight() - 2, fm.getHeight() - 2);
}
}
textArea.setText(prevText);
textArea.scrollTo(prevScroll, 0);
}
Aggregations