Action that influence previous and future actions. Reopend!

edited January 2014 in How To...

Does anybody have examples of drawing programmation or programs that work in the way that any action is influenced by the former or latter action.

example.

keypressed 1 gives you a line, when keypressed 2 gives you an other line based on what the former key was that has been pressed.

I don't know what to search for to find what I want. So this question was necessary to continue in my project.

Hopefully someone can point me in the wright direction. Or even help me out!

Answers

  • Sounds like "Key Combinations" or "Combos" (from a gamer's perspective), google it or try the following sketch (not commented, but should be pretty self explanary):

    boolean[] isKeyPressed = new boolean[256];
    int[] keyHistory = new int[10];
    ArrayList<Combo> combos = new ArrayList();
    
    
    void setup() {
    
        size(600, 600);
    
        combos.add(new ComboA());
        combos.add(new ComboB());
        combos.add(new ComboC());
    
    }
    
    
    void draw() {
    
        fill(0, 10);
        rect(0, 0, width, height);
    
        if(keyPressed) {
            for(Combo combo : combos)
                if(combo.isPressed()) {
                    combo.draw();
                    break;
                }
    
        }
    
    }
    
    
    void keyPressed() {
    
        if(!isKeyPressed[keyCode]) {
            isKeyPressed[keyCode] = true;
            for(int n = 1; n < keyHistory.length; n++)
                keyHistory[n - 1] = keyHistory[n];
            keyHistory[keyHistory.length - 1] = keyCode;
        }
    
    }
    
    
    void keyReleased() {
        isKeyPressed[keyCode] = false;
    }
    
    
    class Combo {
    
        int[] keySeries;
    
        Combo(int[] keySeries) {
            this.keySeries = keySeries;
        }
    
        void draw() { }
    
        boolean isPressed() {
            for(int n = keyHistory.length - keySeries.length, i = 0; n < keyHistory.length; n++, i++)
                if(keyHistory[n] != keySeries[i])
                    return false;
            return true;
        }
    
    }
    
    
    class ComboA extends Combo {
    
        ComboA() {
            super( new int[] { UP, DOWN, LEFT, RIGHT } );
        }
    
        void draw() {
            fill(#ff0000);
            rect(0, 0, width, height);
        }
    
    }
    
    
    class ComboB extends Combo {
    
        ComboB() {
            super( new int[] { UP, UP, DOWN, DOWN } );
        }
    
        void draw() {
            fill(#0000ff);
            rect(0, 0, width, height);
        }
    
    }
    
    
    class ComboC extends Combo {
    
        ComboC() {
            super( new int[] { LEFT, LEFT, RIGHT, LEFT, RIGHT, RIGHT } );
        }
    
        void draw() {
            fill(#00ff00);
            rect(0, 0, width, height);
        }
    
    }
    
  • I'm not a pro programmer! So bare with me.

    But I can't change the values of up and down, etc.. to key's ex. 1,2,3 etc...

  • edited January 2014

    could be instead of

    super( new int[] { UP, UP, DOWN, DOWN } );

    now

            super( new int[] { '1', '2', '3', '4' } );
    

    in all 3 lines where you see it...

  • edited January 2014

    Well I've done that before I asked the question and it says can't give int to boolean.

  • Answer ✓

    Chrisir is right, simply replace UP/DOWN/LEFT/RIGHT with chars ('1', '2', 'a', 'b', '#'...) or key codes.

    If you still get errors, please post the complete error message plus the code you've changed.

  • edited January 2014

    Just to make the example I just changed it.

    Normally the combination of the chars 1,2 etc should give the same result as the left up down.

    Whith following code nothing happens.

    boolean[] isKeyPressed = new boolean[256];
    int[] keyHistory = new int[10];
    ArrayList<Combo> combos = new ArrayList();
    
    
    void setup() {
    
        size(600, 600);
    
        combos.add(new ComboA());
        combos.add(new ComboB());
        combos.add(new ComboC());
    
    }
    
    
    void draw() {
    
        fill(0, 10);
        rect(0, 0, width, height);
    
        if(keyPressed) {
            for(Combo combo : combos)
                if(combo.isPressed()) {
                    combo.draw();
                    break;
                }
    
        }
    
    }
    
    
    void keyPressed() {
    
        if(!isKeyPressed[keyCode]) {
            isKeyPressed[keyCode] = true;
            for(int n = 1; n < keyHistory.length; n++)
                keyHistory[n - 1] = keyHistory[n];
            keyHistory[keyHistory.length - 1] = keyCode;
        }
    
    }
    
    
    void keyReleased() {
        isKeyPressed[keyCode] = false;
    }
    
    
    class Combo {
    
        int[] keySeries;
    
        Combo(int[] keySeries) {
            this.keySeries = keySeries;
        }
    
        void draw() { }
    
        boolean isPressed() {
            for(int n = keyHistory.length - keySeries.length, i = 0; n < keyHistory.length; n++, i++)
                if(keyHistory[n] != keySeries[i])
                    return false;
            return true;
        }
    
    }
    
    
    class ComboA extends Combo {
    
        ComboA() {
            super( new int[] {'1'} );
        }
    
        void draw() {
            fill(#ff0222);
            rect(0, 0, width, height);
        }
    
    }
    
    
    class ComboB extends Combo {
    
        ComboB() {
            super( new int[] { '1', '2', '3', '7' } );
        }
    
        void draw() {
            fill(#0000ff);
            rect(0, 0, width, height);
        }
    
    }
    
    
    class ComboC extends Combo {
    
        ComboC() {
            super( new int[] {'5', '6', '4', '1', '2', '4' } );
        }
    
        void draw() {
            fill(#00ff00);
            rect(0, 0, width, height);
        }
    
    }
    

    Forgot when I do the keycode it gives me :

    processing.app.SketchException: Badly formed character constant (expecting quote, got 6) at processing.mode.java.preproc.PdePreprocessor.checkForUnterminatedMultilineComment(PdePreprocessor.java:478) at processing.mode.java.preproc.PdePreprocessor.write(PdePreprocessor.java:515) at processing.mode.java.JavaBuild.preprocess(JavaBuild.java:270) at processing.mode.java.JavaBuild.preprocess(JavaBuild.java:185) at processing.mode.java.JavaBuild.build(JavaBuild.java:144) at processing.mode.java.JavaBuild.build(JavaBuild.java:123) at processing.mode.java.JavaMode.handleRun(JavaMode.java:120) at processing.mode.java.JavaEditor$18.run(JavaEditor.java:468) at java.lang.Thread.run(Unknown Source)

  • Ok it's seems work with the number that are not in then numpad. keycode still doesn't work for me.

  • edited January 2014 Answer ✓

    Did you try something like '37'?

    If yes, single quotes are reserved for chars ('1', 'a', ':'), you are not allowed to put multiple chars in one single quotation block. Key codes are unsigned bytes (1, 120, 37) or in Processing's case int (1, 12000, 37000). Just remove the quotation marks and your code should work. Example:

    super( new int[] { 37, 38, 39, 40 } );

  • edited January 2014

    When I add a comboD it give me that their is no such class?

    combos.add(new ComboD());
        combos.add(new ComboE()); 
        combos.add(new ComboF());
    

    Anybody an idea what's wrong?

  • I have to stop accepting answer to quick, i've noticed that once I do it I get no reaction any more :) I'll open a new topic, maybe it's more appropriated!

  • Because you have to actually a class ComboD in order to use it. Just take the following code and change ComboC to ComboD:

    class ComboC extends Combo {
    
        ComboC() {
            super( new int[] {'5', '6', '4', '1', '2', '4' } );
        }
    
        void draw() {
            fill(#00ff00);
            rect(0, 0, width, height);
        }
    
    }
    

    Maybe you should start smaller and read some tutorials about classes and object oriented programming in general.

  • edited January 2014

    Hi, I'm reading tutorials aswell.

    And I've better put my code on too. But the example you gave I did ofcourse if I add something or declare something I should find it somewhere further down the line.

    So what you wrote I've already done.

    But it gave me the former error.

    void setup() {
    
        size(801, 534);
    
        combos.add(new ComboA());
        combos.add(new ComboB());
        combos.add(new ComboC());
        combos.add(new ComboD());
        combos.add(new ComboE()); 
    

    followed by

    class ComboC extends Combo {
    
        ComboC() {
            super( new int[] {98} );
        }
    
        void draw() {
    
    
          }
        }
    }       
    
    class ComboD extends Combo {
    
        ComboD() {
            super( new int[] {99} );
        }
    
        void draw() {
    

    } } }

  • edited January 2014 Answer ✓

    I can already see two errors in the code you've posted:

    class ComboC extends Combo {
        ComboC() {
            super( new int[] {98} );
        }
        void draw() {
        // } this bracket will "kill" your class definition 
        }
    } 
    
    
    class ComboD extends Combo {
        ComboD() {
            super( new int[] {99} );
        }
        void draw() {
        // } this bracket will "kill" your class definition 
        }
    } 
    

    Please post your complete code. The following works perfectly for me:

    boolean[] isKeyPressed = new boolean[256];
    int[] keyHistory = new int[10];
    ArrayList<Combo> combos = new ArrayList();
    
    
    void setup() {
    
        size(600, 600);
    
        combos.add(new ComboA());
        combos.add(new ComboB());
        combos.add(new ComboC());
        combos.add(new ComboD());
        combos.add(new ComboE());
        combos.add(new ComboF());
    
    }
    
    
    void draw() {
    
        fill(0, 10);
        rect(0, 0, width, height);
    
        if(keyPressed) {
            for(Combo combo : combos)
                if(combo.isPressed()) {
                    combo.draw();
                    break;
                }
    
        }
    
    }
    
    
    void keyPressed() {
    
        if(!isKeyPressed[keyCode]) {
            isKeyPressed[keyCode] = true;
            for(int n = 1; n < keyHistory.length; n++)
                keyHistory[n - 1] = keyHistory[n];
            keyHistory[keyHistory.length - 1] = keyCode;
        }
    
    }
    
    
    void keyReleased() {
        isKeyPressed[keyCode] = false;
    }
    
    
    class Combo {
    
        int[] keySeries;
    
        Combo(int[] keySeries) {
            this.keySeries = keySeries;
        }
    
        void draw() { }
    
        boolean isPressed() {
            for(int n = keyHistory.length - keySeries.length, i = 0; n < keyHistory.length; n++, i++)
                if(keyHistory[n] != keySeries[i])
                    return false;
            return true;
        }
    
    }
    
    
    class ComboA extends Combo {
    
        ComboA() {
            super( new int[] {'1'} );
        }
    
        void draw() {
            fill(#ff0222);
            rect(0, 0, width, height);
        }
    
    }
    
    
    class ComboB extends Combo {
    
        ComboB() {
            super( new int[] { '1', '2', '3', '7' } );
        }
    
        void draw() {
            fill(#0000ff);
            rect(0, 0, width, height);
        }
    
    }
    
    
    class ComboC extends Combo {
    
        ComboC() {
            super( new int[] { '5', '6', '4', '1', '2', '4' } );
        }
    
        void draw() {
            fill(#00ff00);
            rect(0, 0, width, height);
        }
    
    }
    
    
    class ComboD extends Combo {
    
        ComboD() {
            super( new int[] { 98 } );
        }
    
        void draw() {
            fill(#ffffff);
            rect(0, 0, width, height);
        }
    
    }
    
    
    class ComboE extends Combo {
    
        ComboE() {
            super( new int[] { 99 } );
        }
    
        void draw() {
            fill(#00ffff);
            rect(0, 0, width, height);
        }
    
    }
    
    
    class ComboF extends Combo {
    
        ComboF() {
            super( new int[] { 100 } );
        }
    
        void draw() {
            fill(#ffff00);
            rect(0, 0, width, height);
        }
    
    }
    
  • It was all because a dubble keypressed declaration. Soo stupid of me to didn't notice it.

    I've fixed it, and ofcourse now it works. But now I can't save.

    NullpointerExecption that show my beginrecord(pdf); marked in yellow.

    void keyPressed() {
    
        if(!isKeyPressed[keyCode]) {
            isKeyPressed[keyCode] = true;
            for(int n = 1; n < keyHistory.length; n++)
                keyHistory[n - 1] = keyHistory[n];
            keyHistory[keyHistory.length - 1] = keyCode;
    
            if (key == 'r') {
        if (recording) {
          endRecord();
    
          println("Recording stopped." + d);
          recording = false;
        } else {
          setup();
          beginRecord(pdf);
    
          println("Recording started." + d);
          recording = true;
        }
      } else if (key == 'q') {
        if (recording) {
          endRecord();
        }
    
        exit();
      }
        }
    
    }
    
    
    void keyReleased() {
        isKeyPressed[keyCode] = false;
    }
    

    And thus it won't save anymore.

    Thanks for all your help with the combos!

  • edited January 2014

    And you initiated pdf properly in your setup() function? Example:

    pdf = createGraphics(width, height, PDF, "test.pdf");

  • This is before my setup

    PGraphicsPDF pdf;
    Date d;
    

    If I put your implement in my setup it says can convert PGgraphics to graphicsPDF

  • Simply change PGraphicsPDF pdf; to PGraphics pdf;

  • This is the code, it either gives me the static active error or a missing } or a nullpointerexception.

    boolean[] isKeyPressed = new boolean[256];
    int[] keyHistory = new int[10];
    ArrayList<Combo> combos = new ArrayList();
     import processing.pdf.*;
    import java.util.Calendar;
    import java.util.Date;
    boolean recording = false;
    float x,y,px,py;
    float zachter = 0.02;
    float diameter = 50;
    int  weight;
    String pdfname ;
    PGraphics pdf;
    Date d;
    float r =20;
    float a =0;
    PImage bg; 
    
     // 0 tot 9 = 96 tot 105
    void setup() {
    
        size(801, 534);
    
        combos.add(new ComboA());
        combos.add(new ComboB());
        combos.add(new ComboC());
        combos.add(new ComboD());
    
    
        bg = loadImage ("Filterbg.jpg");
        background(bg);
    
    }
    
    void draw() {
    
    
        if(keyPressed) {
            for(Combo combo : combos)
                if(combo.isPressed()) {
                    combo.draw();
                    break;
                }
    
        }
    
    }
    
    
    void keyPressed() {
    
        if(!isKeyPressed[keyCode]) {
            isKeyPressed[keyCode] = true;
            for(int n = 1; n < keyHistory.length; n++)
                keyHistory[n - 1] = keyHistory[n];
            keyHistory[keyHistory.length - 1] = keyCode;
        }       
           else while (key == 'r'){ 
        if (recording) {
          endRecord();
    
          println("Recording stopped." + d);
          recording = false;
        } else {
          setup();
          beginRecord(pdf);
    
          println("Recording started." + d);
          recording = true;
        }
           }
    
    if (key == 'q'){ 
    
        if (recording) {
          endRecord();
        }
    
      }
    }
        exit();
      }
        }
    
    }
    
  • Okay, I corrected your code - you used too many closing brackets and an else while instead of else if. These errors have nothing to do with your initial question, please open another thread if you get any more errors.

    import processing.pdf.*;
    import java.util.Calendar;
    import java.util.Date;
    
    boolean[] isKeyPressed = new boolean[256];
    int[] keyHistory = new int[10];
    ArrayList<Combo> combos = new ArrayList();
    boolean recording = false;
    float x, y, px, py;
    float zachter = 0.02;
    float diameter = 50;
    int  weight;
    String pdfname ;
    PGraphics pdf;
    Date d;
    float r =20;
    float a =0;
    PImage bg; 
    
    void setup() {
        size(801, 534);
        combos.add(new ComboA());
        combos.add(new ComboB());
        combos.add(new ComboC());
        combos.add(new ComboD());
        bg = loadImage ("Filterbg.jpg");
        background(bg);
    }
    
    void draw() {
        if(keyPressed) {
            for(Combo combo : combos)
                if(combo.isPressed()) {
                    combo.draw();
                    break;
                }
    
        }
    }
    
    void keyPressed() {
        if(!isKeyPressed[keyCode]) {
            isKeyPressed[keyCode] = true;
            for(int n = 1; n < keyHistory.length; n++)
                keyHistory[n - 1] = keyHistory[n];
            keyHistory[keyHistory.length - 1] = keyCode;
        }       
        if(key == 'r') {
            if(recording) {
                endRecord();
                println("Recording stopped." + d);
                recording = false;
            } else {
                setup();
                beginRecord(pdf);
                println("Recording started." + d);
                recording = true;
            }
        } else if(key == 'q') {
            if(recording)
                endRecord();
            exit();
        }
    }
    
    void keyReleased() {
        isKeyPressed[keyCode] = false;
    }
    
    class Combo {
        int[] keySeries;
        Combo(int[] keySeries) {
            this.keySeries = keySeries;
        }
        void draw() { }
        boolean isPressed() {
            for(int n = keyHistory.length - keySeries.length, i = 0; n < keyHistory.length; n++, i++)
                if(keyHistory[n] != keySeries[i])
                    return false;
            return true;
        }
    }
    
    class ComboA extends Combo {
        ComboA() {
            super( new int[] {'2', '1'} );
        }
        void draw() {
            fill(#ff0222);
            rect(0, 0, width, height);
        }
    }
    
    class ComboB extends Combo {
        ComboB() {
            super( new int[] { '1', '2', '3', '7' } );
        }
        void draw() {
            fill(#0000ff);
            rect(0, 0, width, height);
        }
    }
    
    class ComboC extends Combo {
        ComboC() {
            super( new int[] { '5', '6', '4', '1', '2', '4' } );
        }
        void draw() {
            fill(#00ff00);
            rect(0, 0, width, height);
        }
    }
    
    class ComboD extends Combo {
        ComboD() {
            super( new int[] { 98 } );
        }
        void draw() {
            fill(#ffffff);
            rect(0, 0, width, height);
        }
    }
    
  • It works so that I can choose every 'combo', but after I used every 'combo' once. Then I can't go back to the former it stopt at the last combo.

    import processing.pdf.*;
    import java.text.SimpleDateFormat;
    import java.util.Calendar;
    import java.util.Date;
    
    boolean[] isKeyPressed = new boolean[256];
    int[] keyHistory = new int[10];
    ArrayList<Combo> combos = new ArrayList();
    boolean recording = false;
    float x, y, px, py;
    float zachter = 0.02;
    float diameter = 50;
    int  weight;
    String pdfname ;
    PGraphics pdf;
    
    float r =20;
    float a =0;
    PImage bg; 
    
    Date d;
    SimpleDateFormat sdf;
    
    
     // 0 tot 9 = 96 tot 105
    void setup() {
    
        size(displayHeight, displayWidth);
        //bg = loadImage ("Filterbg.jpg"); 
        combos.add(new ComboA());
        combos.add(new ComboB());
        combos.add(new ComboC());
        combos.add(new ComboD());
        d = new Date();
        sdf= new SimpleDateFormat("yyyyMMdd_HHmmss");
        pdfname =  sdf + ".pdf";
        pdf = (PGraphics) createGraphics(width, height, PDF, pdfname);
    
        //background(bg);
    
    }
    void draw() {
        if(keyPressed) {
            for(Combo combo : combos)
                if(combo.isPressed()) {
                    combo.draw();
                    break;
                }
    
        }
    }
    
    void keyPressed() {
        if(!isKeyPressed[keyCode]) {
            isKeyPressed[keyCode] = true;
            for(int n = 1; n < keyHistory.length; n++)
                keyHistory[n - 1] = keyHistory[n];
            keyHistory[keyHistory.length - 1] = keyCode;
        }       
        if(key == 'r') {
            if(recording) {
                endRecord();
                println("Recording stopped." + pdf);
                recording = false;
            } else { 
                beginRecord(pdf);            
                println("Recording started." + pdf);
                recording = true;
            }
        } else if(key == 'q') {
            if(recording)
                endRecord();
            exit();
        }
    }
    
    void keyReleased() {
        isKeyPressed[keyCode] = true;
    }
    
    class Combo {
        int[] keySeries;
        Combo(int[] keySeries) {
            this.keySeries = keySeries;
        }
        void draw() { }
        boolean isPressed() {
            for(int n = keyHistory.length - keySeries.length, i = 0; n < keyHistory.length; n++, i++)
                if(keyHistory[n] != keySeries[i])
                    return false;
            return true;
        }
    }
    
    String timestamp() {
      Calendar now = Calendar.getInstance();
      return String.format("%1$ty%1$tm%1$td_%1$tH%1$tM%1$tS", now);
    
    }
    
    class ComboA extends Combo {
    
        ComboA() {
            super( new int[] {96} );
        }
    
        void draw() {
    
        fill(0, 0, 0,0);
        triangle(mouseX, mouseY, dist (pmouseX, pmouseY, mouseX, mouseY), dist (pmouseX, pmouseY, mouseX, mouseY), pmouseY, pmouseX);
    
    
        triangle(pmouseX, mouseY, pmouseY, pmouseX, pmouseY, pmouseX);
        fill(255,0,0,0);
        stroke(0.1,0.1,0.1);
    
                    }
        }
    
    
    class ComboB extends Combo {
    
        ComboB() {
            super( new int[] {97} );
        }
    
        void draw() {
    
    
        float targetX = pmouseX;
        float targetY = pmouseY;
    
    
        y = y + (targetY - y) * zachter;
        x = x + (targetX - x) * zachter;
        strokeWeight(weight);
    
        line(x, y , px, py);
    
        px=y;
        py=x;
    
                    }
    
    }
    
    
    class ComboC extends Combo {
    
        ComboC() {
            super( new int[] {98} );
        }
    
        void draw() {
    
        if (random(0,200) > 25) { 
          noFill();
          strokeWeight(0.01);
          float x = random(-width,width*2);
          float y = random(-height,height*2);
          line(x/2, y/2, mouseX, mouseY);
          for (float w=0.5; w < 0.5; w += 0.9) {
          float lerpDist = 0.9 + (w * 0.5/3);
    
          strokeWeight(w);
          line( lerp(x, mouseX, lerpDist),
          lerp(y, mouseY, lerpDist),mouseX,mouseY );
                                                 }
        }
    }
    }      
    
    class ComboD extends Combo {
    
        ComboD() {
            super( new int[] {99} );
        }
    
        void draw() {
    
    
        if (random(0,100) > 50) { 
          noFill();
          strokeWeight(0.5);
          float x = random(-width,width*2);
          float y = random(-height,height*2);
          line(x, y, mouseX, mouseY);
          for (float w=0.5; w < 3; w += 0.1) {
            float lerpDist = 0.8 + (w * 0.2/2);
    
            strokeWeight(w);
            line( lerp(x, mouseX, lerpDist),
                  lerp(y, mouseY, lerpDist),
                  mouseX,
                  mouseY );
          }
        }
      }
    }
    
  • Nobody an idea of how I can go further with this project technically.

  • First of all, one button stroke isn't a combo, I wrote the Combo class with combinations of keys in mind. For this kind of "combo" a simple switch() block would suffice.

    I don't understand where exactly your problem is, do you want to reset the screen after every combo, because everything else seems to work as expected - the Combos are executable in any combination.

  • Looking up the switch() reference!

    And want I want is that I can always go back to an other combo, because at the moment when I pressed al 5combo's once. It stays at the last one.

  • Replace:

    void keyReleased() {
        isKeyPressed[keyCode] = true;
    }
    

    With this (like shown in my last example):

    void keyReleased() {
        isKeyPressed[keyCode] = false;
    }
    
  • edited February 2014

    it looks like things have gotten a bit off track. The question, in the way it's worded, seems to be asking about modal interface design. If you want to read a very wordy wikipedia article on the topic check out the Mode article

    An example of a simple mode might be caps lock: normally you're in normal mode, but then you hit caps lock, and now YOU'RE IN ALL CAPS MODE, even though you're just hitting the same buttons.

    The question did say "any action is influenced by the former or latter action", but unfortunately, without the ability to predict the future, it's hard to say what latter actions will happen ;)

    Here's an example of how to write a modal program. In this program, we start out in circle mode: when the user clicks on the canvas, we draw a circle. If the user hits space bar while in circle mode, we transition to square mode. While in square mode, if the user clicks on the canvas, we draw a square. If they hit space bar, we transition to circle mode.

    ClickMode currentMode; // the mode that we're currently in
    
    void setup() {
      size(800, 600);
      background(255);
      // we need to start in *some* mode, so let's start in circle mode.
      currentMode = new CircleMode();
      println("starting in circle mode");
    }
    
    void draw() {
      // normally we'd reset the background here but we'll just leave it;
      // it makes the example simpler if we don't clear the background.
    }
    
    void mousePressed() {
      // when we see that the mouse was clicked, we call the current mode's 
      // mousePressed method.  That way, we pass on the responsibility to the current
      // mode.  It's a bit cleaner than doing a bit block like "if we're in this mode, do 
      // this, if we're in that mode, do that"; that would get really messy really quickly!
      currentMode.mousePressed();
    }
    
    void keyPressed() {
      // we do the same thing with keyboard presses that we did with mouse presses.
      currentMode.keyPressed();
    }
    
    // any class that has a mousePressed and a keyPressed method can be considered a 
    // ClickMode.  That means that we can make it the current mode!
    interface ClickMode {
      void mousePressed();
      void keyPressed();
    }
    
    // in this mode, we'll draw squares.
    class SquareMode implements ClickMode {
      void mousePressed() {
        rectMode(CENTER);
        rect(mouseX, mouseY, 20, 20);
      }
    
      void keyPressed() {
        if (key == ' ') {
          // this is how we get from one mode to another :)
          currentMode = new CircleMode();
          println("going to circle mode");
        }
      }
    }
    
    // this mode lets us draw circles.
    class CircleMode implements ClickMode {
      void mousePressed() {
        ellipseMode(CENTER);
        ellipse(mouseX, mouseY, 20, 20);
      }
    
      void keyPressed() {
        if (key == ' ') {
          currentMode = new SquareMode();
          println("going to square mode");
        }
      }
    }
    
  • edited February 2014

    First of all, one button stroke isn't a combo, I wrote the Combo class with combinations of keys in mind. For this kind of "combo" a simple switch() block would suffice.

    The swithc() is it, I think. You could make switch()'s with numerous cases!? correct?

  • The question did say "any action is influenced by the former or latter action", but unfortunately, without the ability to predict the future, it's hard to say what latter actions will happen

    Well I mean like a modulo when you give 0 and then follows a 4 you get the lines to behave divide in lengt. When you give 0 and then follows a 2 you get a multiply of the line length?

    Anybody wants to help me with this or has an idea of how he would do it?

  • For the switch/case it's possible but cumbersome when there are 9x9x9 possibilities.

    Isn't their a way to work with an automated random option?

  • edited February 2014

    Dear Layzfat,

    maybe you should write longer posts and explain more what you want.

    Your posts now are very short. Or is it because english is not your first language?

    Anyway, I don't quite get what you want.

    The guys helped you a lot and made a huge program with Combo for you.

    Assuming that a combination of keys leads to a given result.

    Now you suddenly ignore the Combo approach and ask for random.

    Well, with random, you don't have a given result but end with a random result.

    So why do you ignore what others wrote and made for you? This is not very polite.

    Please be more specific about what you want to achieve. What do you want to achieve?

    Thanks.

    Greetings, Chrisir

  • edited February 2014

    anyway

    I made a new version which collects 3 keys in an array.

    If it has all 3 keys it evaluates. Then you can start all over.

    To do the evaluation, it stitches together all 3 keys into one number

    e.g. you enter 9, 8 and 7 you get the number 987.

    At the moment, only 987 and 986 is evaluated.

    For all other inputs it goes for random.

    So you don't need to write down all 999 cases but need to write down only some...

    (Please enter only values between 1 and 9)

    of course

    of course you can evaluate in other ways

    e.g. multiply the first number with 10 and use as y-value for the 1st line

    stitch together the 2nd two numbers and use this plus the 1st y-value as an y-value for the 2nd line

    and stitch together number 1 and 3 use this as a color for the line.....

    Does this help?

    Greetings, Chrisir

    int [] theKeyStrokes = new int [3];
    int currentIndex; 
    boolean firstTime=true;
    
    void setup() {
      size(444, 444);
    }
    
    void draw () {
      // cycle over?
      if (currentIndex==0 && !firstTime) {
        // yes, eval now
        evaluateInput();
        firstTime=true;
        println ("wait for new command");
      }
    }
    
    void keyPressed () {
      // record the keys
      theKeyStrokes [currentIndex] = key; 
      currentIndex++;
      firstTime=false; 
      // end of array, go to begin 
      if (currentIndex==3)
        currentIndex=0;
    }
    
    // ------------------------
    
    void evaluateInput() {
      int result =  (100*(theKeyStrokes[0]-48)) + 
        (10* (theKeyStrokes[1]-48)) +
        theKeyStrokes[2]-48;
      //
      println (result);
      switch (result) {
      case 987:
        line(12, 5, 4, 156);
        break;
      case 986:
        line(103, 122, 134, 133);
        break;
      default:
        line(random(444), random(444), 34, 56);
        break;
      }
    }
    //
    
  • Man that's exactly it!

    I'm going to implement the drawings I have, when it's finished I'll post it here.

    Thanks!

  • you're welcome!

Sign In or Register to comment.