Not logged in.  Login/Logout/Register | List snippets | | Create snippet | Upload image | Upload data

96
LINES

< > BotCompany Repo | #1033725 // JGazelleVScriptRunner

JavaX fragment (include) [tags: use-pretranspiled]

Libraryless. Click here for Pure Java version (30734L/196K).

srecord noeq JGazelleVScriptRunner(SimpleLiveValue<S> lvScript) {
  GazelleVScript parsedScript;
  GazelleVScript.Run run;
  O result;
  SingleComponentPanel scpScriptResult = singleComponentPanel();
  new WeakVar<ImageSurface> isScriptResult;
  new WeakVar<JTextArea> taResult;
  JLabel lblScore = jlabel();

  JComponent scriptAndResultPanel() {
    ret northAndCenter(
      scriptInputField(),
      scpScriptResult = singleComponentPanel()
    );
  }
  
  JComponent scriptInputField() {
    ret withMargin(centerAndEastWithMargin(
      withLabel("Gazelle \"Linear\" Script:", jLiveTextField(lvScript)),
      jPopDownButton_noText(
        "Show Scripting Help", rThread { showText("Gazelle 'Linear Script' Help", GazelleVScript.helpText) })
    ));
  }
  
  void showScriptOutput {
    if (result cast BWImage) {
      showImageResult(result);
    } else if (result cast BufferedImage) {
      showImageResult(result);
    } else if (result cast Animation) {
      showImageResult(result.toImage());
    } else {
      showTextResult(str(result));
    }
  }
  
  void showTextResult(S text) {
    text = shorten(1000, text);
    
    if (isShowing(taResult!)) {
      setTextKeepCaret(taResult!, text);
    } else {
      var ta = jTextArea_noUndo(text);
      scpScriptResult.set(jscroll(ta));
      taResult.set(ta);
    }
  }

  void showImageResult(BufferedImage etc img) {
    if (img == null) ret;
    if (isShowing(isScriptResult!)) {
      isScriptResult->setImage(img);
    } else {
      var is = stdImageSurface();
      is.setImage(img);
      scpScriptResult.set(jscroll_centered(is));
      isScriptResult.set(is);
    }
    
    if (isShowing(lblScore)) {
      S score = "";
      if (sameSize(run.inputImage, img)) {
        double distance = bwDistance(toBWImage(run.inputImage), toBWImage(img));
        score = "Score: " + formatDoubleX(100*(1-distance), 1);
      }
      setText(lblScore, score);
    }
  }
  
  ImageSurface stdImageSurface() {
    ret pixelatedImageSurface().setAutoZoomToDisplay(true).repaintInThread(false);
  }
  
  void parse {
    parsedScript = new GazelleVScript(lvScript!);
  }
  
  void runOn(IBWIntegralImage ii) {
    run = parsedScript.new Run();
    run.setInputImage(ii);
    run.setIntegralImage(ii);
    run.run();
    result = run.result();
  }

  void parseAndRunOn(IBWIntegralImage ii) {
    try {
      parse();
      runOn(ii);
    } catch e {
      result = exceptionToStringShort(e);
      printStackTrace(e);
    }
    showScriptOutput();
  }
}

Author comment

Began life as a copy of #1033717

download  show line numbers  debug dex  old transpilations   

Travelled to 4 computer(s): bhatertpkbcr, ekrmjmnbrukm, mowyntqkapby, mqqgnosmbjvj

No comments. add comment

Snippet ID: #1033725
Snippet name: JGazelleVScriptRunner
Eternal ID of this version: #1033725/31
Text MD5: c614a6d60be67ea62aa273a8de2bd419
Transpilation MD5: 76e3947a92792048bfce72abfda534e0
Author: stefan
Category: javax / gazelle v
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2022-01-18 15:37:12
Source code size: 2611 bytes / 96 lines
Pitched / IR pitched: No / No
Views / Downloads: 219 / 438
Version history: 30 change(s)
Referenced in: [show references]