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

80
LINES

< > BotCompany Repo | #1035442 // G22NetworkElement - element in a G22Network

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

Transpiled version (52691L) is out of date.

persistable sclass G22NetworkElement > MetaWithChangeListeners {
  // network element belongs to
  settable G22Network network;
  
  // position in visual representation
  settable Rect bounds;
  
  // identifier in network (optional)
  settableWithVar S identifier;
  
  // script code to adapt element and to create instance
  settableWithVar S code;
  
  {
    onChange(-> { network?.change(); });
  }
  
  class Port {
    // position on element's border (0 to 1, 0 to 1)
    settable DoublePt position;
    
    // name of port
    settable S name;
    
    // cable attached to this port
    settable G22NetworkCable cable;
    
    G22NetworkElement element() { ret G22NetworkElement.this; }
  }
  
  L<Port> ports = syncL();
  
  LASCompileResult newCompileResult() { ret new LASCompileResult; }
  
  transient LASCompileResult compileResult;
  LASCompileResult compile(G22Utils g22utils) {
    S code = this.code;
    if (code == null) null;
    var cr = compileResult;
    if (cr != null && eq(cr.script, code)) ret cr;
    cr = newCompileResult();
    var parser = g22utils.leftArrowParser();
    configureParser(parser);
    cr.script(code).parser(parser).compile();
    ret compileResult = cr;
  }
  
  void configureParser(GazelleV_LeftArrowScriptParser parser) {
    parser.addVar("blueprint");
  }
  
  O makeInstance(G22Utils g22utils) null on exception {
    var compiled = compile(g22utils);
    if (compiled == null) null;
    var compiled2 = compiled!;
    if (!compiled.runnable()) null;
    new FlexibleVarContext ctx;
    ctx.put("blueprint", this);
    ret compiled2.get(ctx);
  }
  
  toString { ret or2(identifier(), "Unnamed element"); }
  
  Port getPort(S name) {
    ret firstThat(ports, p -> eq(p.name, name));
  }
  
  void addPort aka addSlot(S name, DoublePt etc position) {
    printFunctionCall("addPort", +name, +position);
    Port p = getPort(name);
    if (p != null) ret;
    ports.add(new Port().name(name).position(position));
    change();
  }
  
  void removePorts aka deletePorts aka removeSlots aka deleteSlots() {
    ports.clear();
    change();
  }
}

Author comment

Began life as a copy of #1035441

download  show line numbers  debug dex  old transpilations   

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

No comments. add comment

Snippet ID: #1035442
Snippet name: G22NetworkElement - element in a G22Network
Eternal ID of this version: #1035442/29
Text MD5: a149f2a27f05634557acd6f0b677b1fb
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-14 02:59:39
Source code size: 2180 bytes / 80 lines
Pitched / IR pitched: No / No
Views / Downloads: 48 / 126
Version history: 28 change(s)
Referenced in: [show references]