Search in sources :

Example 1 with Handle

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();
}
Also used : MouseListener(java.awt.event.MouseListener) MouseEvent(java.awt.event.MouseEvent) WindowEvent(java.awt.event.WindowEvent) WindowAdapter(java.awt.event.WindowAdapter) ColorControlBox(processing.mode.java.tweak.ColorControlBox) ColorSelector(processing.mode.java.tweak.ColorSelector) MouseMotionListener(java.awt.event.MouseMotionListener) Cursor(java.awt.Cursor) Handle(processing.mode.java.tweak.Handle)

Example 2 with Handle

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));
    }
}
Also used : ColorControlBox(processing.mode.java.tweak.ColorControlBox) Cursor(java.awt.Cursor) Point(java.awt.Point) Handle(processing.mode.java.tweak.Handle)

Example 3 with Handle

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;
}
Also used : TweakClient(processing.mode.java.tweak.TweakClient) ArrayList(java.util.ArrayList) StringList(processing.data.StringList) List(java.util.List) LineBreakpoint(processing.mode.java.debug.LineBreakpoint) Handle(processing.mode.java.tweak.Handle)

Example 4 with Handle

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());
}
Also used : LineBreakpoint(processing.mode.java.debug.LineBreakpoint) Handle(processing.mode.java.tweak.Handle)

Example 5 with Handle

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);
}
Also used : SketchCode(processing.app.SketchCode) ColorControlBox(processing.mode.java.tweak.ColorControlBox) Point(java.awt.Point) Handle(processing.mode.java.tweak.Handle)

Aggregations

Handle (processing.mode.java.tweak.Handle)7 Point (java.awt.Point)4 ColorControlBox (processing.mode.java.tweak.ColorControlBox)4 Cursor (java.awt.Cursor)2 SketchCode (processing.app.SketchCode)2 LineBreakpoint (processing.mode.java.debug.LineBreakpoint)2 Graphics2D (java.awt.Graphics2D)1 MouseEvent (java.awt.event.MouseEvent)1 MouseListener (java.awt.event.MouseListener)1 MouseMotionListener (java.awt.event.MouseMotionListener)1 WindowAdapter (java.awt.event.WindowAdapter)1 WindowEvent (java.awt.event.WindowEvent)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 StringList (processing.data.StringList)1 ColorSelector (processing.mode.java.tweak.ColorSelector)1 TweakClient (processing.mode.java.tweak.TweakClient)1