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

78
LINES

< > BotCompany Repo | #1035453 // JG22NetworkPort - Swing representation of a G22NetworkElement.Port

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

Transpiled version (56111L) is out of date.

srecord noeq JG22NetworkPort(G22Utils g22utils, JG22NetworkElement element, G22NetworkElement.Port port) > JPanel {
  int size = 10;
  
  Color outputColor() { ret Color.green; }
  Color inputColor() { ret Color.red; }
  
  void init {
    bindChangeListenerToComponent(this, port, l0 update);
    componentPopupMenuItems(this,
      "Show value", rThread showValue,
      "Disconnect", rThread disconnect,
      "Delete port", rThread delete);
      
    onMouseDown(this, event -> {
      var dragger = new ComponentDragger(this, event);
      dragger.updatePosition = (x, y) -> {
        Pt p = pt(x, y);
        Rect r = toRect(main getBounds(element.visualize()));
        
        double dTop = distancePointToLineSegment(pt(r.x, r.y), pt(r.x2(), r.y), p);
        double dBottom = distancePointToLineSegment(pt(r.x, r.y2()), pt(r.x2(), r.y2()), p);
        double dLeft = distancePointToLineSegment(pt(r.x, r.y), pt(r.x, r.y2()), p);
        double dRight = distancePointToLineSegment(pt(r.x2(), r.y), pt(r.x2(), r.y2()), p);
        
        double min = doubleMin(dTop, dBottom, dLeft, dRight);
        DoublePt pos = doublePt(
          clampZeroToOne(doubleRatio(x-r.x, r.w)),
          clampZeroToOne(doubleRatio(y-r.y, r.h)));
        
        if (min == dTop)
          pos.y = 0;
        else if (min == dBottom)
          pos.y = 1;
        else if (min == dLeft)
          pos.x = 0;
        else
          pos.x = 1;

        port.position(pos);
      };
      dragger.bringToFront(true).start();
    });
    
    update();
  }
  
  void update {
    _print("Updating port " + port);
    setOpaque(true);
    setBackground(port.isOutput() ? outputColor() : inputColor());
    setSize(size, size);
    // TODO: merge with backend computation
    Rect r = toRect(main getBounds(element.visualize()));
    r = growRectTopAndLeft(r, size);
    setLocation(r.x+iround(port.position.x*r.w),
      r.y+iround(port.position.y*r.h));
    setToolTip(this, str(port));
    _print("Port " + port.name + " : " + getBounds());
  }
  
  void showValue {
    G22NetworkElement e;
    if (port.isOutput())
      e = port.element();
    else
      e = port.cable.from.element();
    var value = element.parent.networkInstance.getObjectForBlueprint(e);
    showFrame("Port value", G22JavaObjectVisualizer(g22utils, value));
  }
  
  void disconnect {
    port.disconnect();
  }
  
  void delete {
    port.delete();
  }
}

Author comment

Began life as a copy of #1035449

download  show line numbers  debug dex  old transpilations   

Travelled to 3 computer(s): ekrmjmnbrukm, mowyntqkapby, mqqgnosmbjvj

No comments. add comment

Snippet ID: #1035453
Snippet name: JG22NetworkPort - Swing representation of a G22NetworkElement.Port
Eternal ID of this version: #1035453/33
Text MD5: f288ec532a6ead8e3f5afaa4ef750c00
Author: stefan
Category: javax / gazelle 22
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2022-05-30 17:10:14
Source code size: 2497 bytes / 78 lines
Pitched / IR pitched: No / No
Views / Downloads: 171 / 283
Version history: 32 change(s)
Referenced in: #1003674 - Standard Classes + Interfaces (LIVE continued in #1034167)