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.

1  
srecord noeq JG22NetworkPort(G22Utils g22utils, JG22NetworkElement element, G22NetworkElement.Port port) > JPanel {
2  
  int size = 10;
3  
  
4  
  Color outputColor() { ret Color.green; }
5  
  Color inputColor() { ret Color.red; }
6  
  
7  
  void init {
8  
    bindChangeListenerToComponent(this, port, l0 update);
9  
    componentPopupMenuItems(this,
10  
      "Show value", rThread showValue,
11  
      "Disconnect", rThread disconnect,
12  
      "Delete port", rThread delete);
13  
      
14  
    onMouseDown(this, event -> {
15  
      var dragger = new ComponentDragger(this, event);
16  
      dragger.updatePosition = (x, y) -> {
17  
        Pt p = pt(x, y);
18  
        Rect r = toRect(main getBounds(element.visualize()));
19  
        
20  
        double dTop = distancePointToLineSegment(pt(r.x, r.y), pt(r.x2(), r.y), p);
21  
        double dBottom = distancePointToLineSegment(pt(r.x, r.y2()), pt(r.x2(), r.y2()), p);
22  
        double dLeft = distancePointToLineSegment(pt(r.x, r.y), pt(r.x, r.y2()), p);
23  
        double dRight = distancePointToLineSegment(pt(r.x2(), r.y), pt(r.x2(), r.y2()), p);
24  
        
25  
        double min = doubleMin(dTop, dBottom, dLeft, dRight);
26  
        DoublePt pos = doublePt(
27  
          clampZeroToOne(doubleRatio(x-r.x, r.w)),
28  
          clampZeroToOne(doubleRatio(y-r.y, r.h)));
29  
        
30  
        if (min == dTop)
31  
          pos.y = 0;
32  
        else if (min == dBottom)
33  
          pos.y = 1;
34  
        else if (min == dLeft)
35  
          pos.x = 0;
36  
        else
37  
          pos.x = 1;
38  
39  
        port.position(pos);
40  
      };
41  
      dragger.bringToFront(true).start();
42  
    });
43  
    
44  
    update();
45  
  }
46  
  
47  
  void update {
48  
    _print("Updating port " + port);
49  
    setOpaque(true);
50  
    setBackground(port.isOutput() ? outputColor() : inputColor());
51  
    setSize(size, size);
52  
    // TODO: merge with backend computation
53  
    Rect r = toRect(main getBounds(element.visualize()));
54  
    r = growRectTopAndLeft(r, size);
55  
    setLocation(r.x+iround(port.position.x*r.w),
56  
      r.y+iround(port.position.y*r.h));
57  
    setToolTip(this, str(port));
58  
    _print("Port " + port.name + " : " + getBounds());
59  
  }
60  
  
61  
  void showValue {
62  
    G22NetworkElement e;
63  
    if (port.isOutput())
64  
      e = port.element();
65  
    else
66  
      e = port.cable.from.element();
67  
    var value = element.parent.networkInstance.getObjectForBlueprint(e);
68  
    showFrame("Port value", G22JavaObjectVisualizer(g22utils, value));
69  
  }
70  
  
71  
  void disconnect {
72  
    port.disconnect();
73  
  }
74  
  
75  
  void delete {
76  
    port.delete();
77  
  }
78  
}

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: 170 / 283
Version history: 32 change(s)
Referenced in: [show references]