Using G4P on processing.py

RazRaz
edited April 2017 in Python Mode

Hi, I want to add to my sketch (processing python sketch) handling of text area from library G4P, but it doesn't work. Here's my code:

add_library('G4P')

text_area = None

def setup():
    size(600, 600)
    global text_area
    text_area = GTextArea(this, 80, 20, 290, 300, G4P.SCROLLBARS_BOTH | G4P.SCROLLBARS_AUTOHIDE)
    text_area.setPromptText("Please enter some text");

def draw():
    background(100)
    text_area.draw()

Answers

  • edited April 2017

    3rd libraries under Python Mode can't find out where their callbacks are by reflection! :-&
    So default handleTextEvents() won't work there w/o heavy hacking! :ar!

    You're gonna need to ask its dev for it.
    However, he doesn't seem fond to fix G4P as you can see below: :-O
    https://GitHub.com/jdf/Processing.py-Bugs/issues/106

    I've tried to convert this Java Mode attempt I've come up w/:

    // forum.Processing.org/two/discussion/21913/using-g4p-on-processing-py#Item_1
    // 2017-Apr-10
    
    import g4p_controls.*;
    
    GTextArea txtArea;
    
    void setup() {
      size(600, 400);
    
      txtArea = new GTextArea(this, 80, 20, 
        width - width/4, height - height/4, 
        G4P.SCROLLBARS_BOTH | G4P.SCROLLBARS_AUTOHIDE);
    
      txtArea.setPromptText("Please enter some text");
      txtArea.addEventHandler(new MyTxtAreaHandler(), "textAreaEvent");
    }
    
    void draw() {
      background(0200);
      txtArea.draw();
    }
    
    static public class MyTxtAreaHandler {
      static public void textAreaEvent(GTextArea txtArea, GEvent ent) {
        println(txtArea.getText());
      }
    }
    

    To Python Mode:

    # forum.Processing.org/two/discussion/21913/using-g4p-on-processing-py#Item_1
    # 2017-Apr-10
    
    add_library('G4P')
    
    def setup():
        size(600, 400)
    
        global txtArea
    
        txtArea = GTextArea(this, 80, 20,
                            width - width/4, height - height/4,
                            G4P.SCROLLBARS_BOTH | G4P.SCROLLBARS_AUTOHIDE)
    
        txtArea.setPromptText('Please enter some text')
        txtArea.addEventHandler(MyTxtAreaHandler(), 'textAreaEvent')
    
    
    def draw():
        background(0200)
        txtArea.draw()
    
    
    class MyTxtAreaHandler:
        @staticmethod
        def textAreaEvent(txtArea, evt):
            print txtArea.getText()
    

    No success though... =((

  • edited April 2017

    G4P was created a long time before Python mode appeared so we should not be surprised if they don't work well together.

    G4P uses reflection quite extensively in the event handlers and GoToLoop tells us that Python cannot use them so it looks as if using G4P in Python mode is a non-starter.

  • edited April 2017

    @quark, I still think it'd be possible to make G4P more compatible w/ Python Mode.

    For example, even though ControlP5 relies on reflection a lot, it's still possible to use it under Python Mode w/ some extra effort: #:-S https://GitHub.com/jdf/processing.py/issues/52

    In the example from the link above, addListener() accepts a ControlListener instance, which wraps up 1 abstract method called controlEvent().

    In Java, those kinda 1-method interfaces are annotated as @FunctionalInterface: ~O)
    http://Docs.Oracle.com/javase/8/docs/api/java/lang/FunctionalInterface.html

    The problem w/ your addEventHandler() is that it accepts an extra String w/ the name of the method for reflection rather than have 1 already pre-determined name. :-<

    Seems like you'd have to create some kinda GListener @FunctionalInterface interface w/ 1 abstract method in it, so we can @Override its method and pass it as a handler for G4P's many controllers. >-)

Sign In or Register to comment.