Button-problem method()

edited December 2015 in How To...

I started a thread some time ago asking for neat ways to code buttons (https://forum.processing.org/two/discussion/13157/what-s-your-favourite-way-to-code-a-button#latest) And There were two great answers that used method() Which I didn't even knew existed. But is there a way to use method() or something similar to access a method inside a class?

Answers

  • edited December 2015 Answer ✓
    • Both method() & thread() can only invoke functions at the sketch's top level.
    • That is, we can't directly invoke some class' method via them.
    • But nothing disallows that once inside the invoked function, that function invokes another method.
    • Just be aware that we can't pass arguments to either method() or thread().

    https://Processing.org/reference/thread_.html

  • edited December 2015

    Ok, the only problem with that is that if I have 50 different buttons I will have to have 50 functions in the first tab, which is annoying and a really roundabout way of doing it...

    But if there is no other way I'l just have to work with it :)

  • edited January 2016 Answer ✓

    If Button is some class, you'd just have to figure out which 1 was clicked at.
    Once found, call its class method and you're done.

    However, if each Button is tasked to do a diff. thing, you're gonna need a diff. technique too.

    Let's name that method action(). Let's say you've got 3 Button objects.
    1st Button is tasked to draw a rect(). 2nd an ellipse() and 3rd a triangle().

    At the moment we instantiate each of those 3, we @Override their action() method.
    Even though all those 3 got a method called action(), each action()'s gonna act differently from each other! :ar!

    Pay specially close attention within class Button, the abstract method action() demands to be implemented lest the whole class can't be instantiated!

    The following example is a modification of this online sketch below:
    http://studio.ProcessingTogether.com/sp/pad/export/ro.9eDRvB4LRmLrr

    And from this old forum thread below:
    https://forum.Processing.org/two/discussion/558/creating-a-next-page-button

    /** 
     * Overriden Action Buttons (v1.02)
     * by GoToLoop (2015-Dec-17)
     * 
     * forum.Processing.org/two/discussion/14039/button-problem-method
     * forum.Processing.org/two/discussion/558/creating-a-next-page-button
     * studio.ProcessingTogether.com/sp/pad/export/ro.9eDRvB4LRmLrr
     */
    
    static final int BUTTONS = 3, GAP = 50, BTN_W = 80, BTN_H = 50;
    final Button[] btns = new Button[BUTTONS];
    Button btnAct;
    
    void setup() {
      size(600, 400);
      smooth(4);
      noLoop();
    
      rectMode(CORNER);
      strokeWeight(2.5);
      stroke(0);
    
      createButtons();
    }
    
    void draw() {
      background(0350);
      for (Button b : btns)  b.display();
      if (btnAct != null)  btnAct.action();
    }
    
    void mousePressed() {
      btnAct = null;
    
      for (Button b : btns)  if (b.isMouseInside()) {
        btnAct = b;
        break;
      }
    
      redraw = true;
    }
    
    void createButtons() {
      final int y = height - BTN_H - GAP;
    
      btns[0] = new Button(GAP, y, BTN_W, BTN_H, #FF0000) {
        @ Override public void action() {
          fill(#FFFF00);
          rect(width/2 - RAD, GAP*2 - RAD, DIM, DIM);
        }
      };
    
      btns[1] = new Button(width - BTN_W >> 1, y, BTN_W, BTN_H, #008000) {
        @ Override public void action() {
          fill(#FF00FF);
          ellipse(width>>1, GAP<<1, DIM, DIM);
        }
      };
    
      btns[2] = new Button(width - BTN_W - GAP, y, BTN_W, BTN_H, #0000FF) {
        @ Override public void action() {
          final int cx = width>>1;
          fill(#00FFFF);
          triangle(cx, GAP*2 - RAD, cx - RAD, GAP*2 + RAD, cx + RAD, GAP*2 + RAD);
        }
      };
    }
    
    abstract class Button {
      static final int DIM = 150, RAD = DIM>>1;
      final int x, y, w, h, c;
    
      Button(int xx, int yy, int ww, int hh, color cc) {
        x = xx;
        y = yy;
        w = ww;
        h = hh;
        c = cc;
      }
    
      abstract void action();
    
      void display() {
        fill(c);
        rect(x, y, w, h);
      }
    
      boolean isMouseInside() {
        return mouseX > x & mouseX < x+w & mouseY > y & mouseY < y+h;
      }
    }
    
  • edited June 2016

    Alternatively, give each button an unique id

    When clicked, read the id of the clicked Button

    write in a new tab a function doCommand() and pass it the id

    Now

    switch (id) {
    
        case 0:
    
            Rect....
    
            Break;
    
        Case 1:
    
            Ellipse....
    
            Break;
    
        .....
    
        ....
    
  • Ok, GoToLoop, so you instantiate the buttons in a new method/function and when you do that, you also defines what the method action() is going to do? That's actually pretty neat. But you have to instantiate it in a new method/function and not in setup?

    Thanks a lot for the help guys, this makes things much easier.

  • Answer ✓

    It's neat

    It's better in a extra function but also possible in setup()

  • edited December 2015 Answer ✓

    But you have to instantiate it in a new method/function and not in setup()?

    createButtons() is called from setup().
    Once createButtons() is finished, it goes back to setup(), which has called it.
    Of course you can pick createButtons()'s content and dump it all into setup().
    But that's not very modular & it's against "separation of concerns" (SoC):
    https://en.Wikipedia.org/wiki/Separation_of_concerns

Sign In or Register to comment.