Skip to content

Commit

Permalink
Merge pull request #12 from Huzifa1/master
Browse files Browse the repository at this point in the history
Added multiple switches functionality
  • Loading branch information
deffel authored Jul 24, 2024
2 parents 6058e18 + 43b0559 commit cc76724
Show file tree
Hide file tree
Showing 6 changed files with 205 additions and 46 deletions.
38 changes: 34 additions & 4 deletions gui/SelectionMenuPolicy.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,18 +67,20 @@ var SelectionMenuPolicy = draw2d.policy.figure.SelectionPolicy.extend({
let rotateRightBtn = $("<div class='overlayMenuItem'>&#10227;</div>");

this.overlay.append(deleteBtn);
this.overlay.append(rotateLeftBtn);
this.overlay.append(rotateRightBtn);
// if (figure instanceof NodeShape) {
this.overlay.append(rotateLeftBtn);
this.overlay.append(rotateRightBtn);
// }
$("body").append(this.overlay);

rotateLeftBtn.on("click", function () {
if (figure instanceof NodeShape) {
if (figure instanceof NodeShape || figure instanceof SwitchShape) {
figure.setOrientation(OrientationEnum.next(figure.getOrientation()));
}
});

rotateRightBtn.on("click", function () {
if (figure instanceof NodeShape) {
if (figure instanceof NodeShape || figure instanceof SwitchShape) {
figure.setOrientation(OrientationEnum.prev(figure.getOrientation()));
}
});
Expand All @@ -96,6 +98,7 @@ var SelectionMenuPolicy = draw2d.policy.figure.SelectionPolicy.extend({

var i = 0;
var num_to_delete = parseInt(figure.getName().substring(1));

for (i = 0; i < fpganodes.getSize(); i++) {
var node = fpganodes.get(i);

Expand All @@ -118,6 +121,33 @@ var SelectionMenuPolicy = draw2d.policy.figure.SelectionPolicy.extend({

var command_rename = new draw2d.command.CommandAttr(nodeLabel, { text: newname });

command.add(command_rename);
}
}
} else if (figure instanceof SwitchShape) {
var switches = canvas.getFigures();

var i = 0;
var num_to_delete = parseInt(figure.getText().split(" ")[2]);
for (i = 0; i < switches.getSize(); i++) {
var ethSwitch = switches.get(i);

if (ethSwitch.NAME != "SwitchShape") {
continue;
}

var ethSwitchNum = parseInt(ethSwitch.getText().split(" ")[2]);
// Update all Switchs that have greater num than the ethSwitch to delete.
if (num_to_delete < ethSwitchNum) {
var newnum = ethSwitchNum - 1;

var newname = "Ethernet Switch ";
if (newnum < 10) {
newname += "0";
}
newname += newnum;

var command_rename = new draw2d.command.CommandAttr(ethSwitch, { text: newname });
command.add(command_rename);
}
}
Expand Down
81 changes: 69 additions & 12 deletions gui/SwitchShape.js
Original file line number Diff line number Diff line change
@@ -1,43 +1,46 @@
//
SwitchShape = draw2d.shape.basic.Label.extend({
NAME: "SwitchShape",

ORIENTATION_PROPERTIES: Object.freeze({
[OrientationEnum.north]: {
"rotationAngle": 0,
"padding": { left: 25, top: 5, right: 25, bottom: 5 },
"padding": { left: 40, top: 5, right: 40, bottom: 5 },
"locator": new draw2d.layout.locator.BottomLocator(),
},
[OrientationEnum.east]: {
"rotationAngle": 0,
"padding": { left: 25, top: 5, right: 25, bottom: 5 },
"rotationAngle": -90,
"padding": { left: 40, top: 5, right: 40, bottom: 5 },
"locator": new draw2d.layout.locator.RightLocator(),
},
[OrientationEnum.south]: {
"rotationAngle": 0,
"padding": { left: 25, top: 5, right: 25, bottom: 5 },
"padding": { left: 40, top: 5, right: 40, bottom: 5 },
"locator": new draw2d.layout.locator.TopLocator(),
},
[OrientationEnum.west]: {
"rotationAngle": 0,
"padding": { left: 25, top: 5, right: 25, bottom: 5 },
"rotationAngle": -270,
"padding": { left: 40, top: 5, right: 40, bottom: 5 },
"locator": new draw2d.layout.locator.LeftLocator(),
},
}),

init: function (attr) {
this._super($.extend({}, attr));

this.setText("Ethernet Switch");

// Allows multiple connections.
this.linksToChannel = [];

// Shape for view.
this.connector;
this.isDrawn = false;
let port = this.createPort("hybrid");
port.setName("inout_" + this.id);

// Create 7 ports
for (let i = 0; i < 8; i++) {
let port = this.createPort("hybrid");
port.setName(`inout_${i}` + this.id);
}


// port.setMaxFanOut(1);
// port.on("connect", function () {
// this.setVisible(false);
Expand All @@ -48,6 +51,8 @@ SwitchShape = draw2d.shape.basic.Label.extend({

// this.orientation = attr.orientation;
this.setOrientation(attr.orientation, false);

this.installEditPolicy(new SelectionMenuPolicy());
// this.getFPGA().channelLayout.add(this);
},

Expand Down Expand Up @@ -80,11 +85,63 @@ SwitchShape = draw2d.shape.basic.Label.extend({
return this.orientation;
},

setPortLocator: function (original_port, port, coordinates) {

var originalLocator = this.getHybridPort(original_port).getLocator();
var CustomLocator = draw2d.layout.locator.Locator.extend({
init: function () {
this._super();
},

relocate: function (index, target) {
originalLocator.relocate(index, target);

var x = target.getX() + coordinates.x ?? 0;
var y = target.getY() + coordinates.y ?? 0;

target.setPosition(x, y);
}
});
let orientationToDirection = {
"south": 0,
"east": 1,
"north": 2,
"west": 3,
}
port.setConnectionDirection(orientationToDirection[this.getOrientation()]);
port.setLocator(new CustomLocator());
},

setOrientation: function (orientation, repaint = true) {
this.orientation = orientation;
let prop = this.ORIENTATION_PROPERTIES[orientation];
let self = this;

function setAllPortsLocator(flipCoord) {
let c1 = flipCoord ? 'x' : 'y';
let c2 = flipCoord ? 'y' : 'x';
self.setPortLocator(0, self.getHybridPort(1), { [c1]: 20, [c2]: 0 });
self.setPortLocator(0, self.getHybridPort(2), { [c1]: 40, [c2]: 0 });
self.setPortLocator(0, self.getHybridPort(3), { [c1]: 60, [c2]: 0 });
self.setPortLocator(0, self.getHybridPort(4), { [c1]: 80, [c2]: 0 });
self.setPortLocator(0, self.getHybridPort(5), { [c1]: 100, [c2]: 0 });
self.setPortLocator(0, self.getHybridPort(6), { [c1]: 120, [c2]: 0 });
self.setPortLocator(0, self.getHybridPort(7), { [c1]: 140, [c2]: 0 });
}

this.getHybridPort(0).setLocator(prop.locator);
this.rotationAngle = prop.rotationAngle;
if (this.orientation == OrientationEnum.north || this.orientation == OrientationEnum.south) {
this.setPortLocator(0, this.getHybridPort(0), { x: -70, y: 0 });
setAllPortsLocator(true);
} else if (this.orientation == OrientationEnum.east) {
this.setPortLocator(0, this.getHybridPort(0), { x: -77, y: -70 });
setAllPortsLocator(false);
} else if (this.orientation == OrientationEnum.west) {
this.setPortLocator(0, this.getHybridPort(0), { x: 77, y: -70 });
setAllPortsLocator(false);
}

this.setRotationAngle(prop.rotationAngle);
this.height = 0;
this.setPadding(prop.padding);

Expand Down
Loading

0 comments on commit cc76724

Please sign in to comment.