Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/com/modsim/gui/Menu.java
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ private void addEditMenu() {
edit.add(new JMenuItem(Ops.rotateCCW));
edit.add(new JMenuItem(Ops.rotate180));
edit.addSeparator();
edit.add(new JMenuItem(Ops.toggleSnap));
edit.addSeparator();
edit.add(new JMenuItem(Ops.labelEdit));

app_menu.add(edit);
Expand Down
2 changes: 1 addition & 1 deletion src/com/modsim/gui/view/View.java
Original file line number Diff line number Diff line change
Expand Up @@ -300,7 +300,7 @@ public void setTool(BaseTool newTool) {
* @param y Y-coordinate
*/
public void zoom(int x, int y, double amount) {
Vec2 zmPt = ViewUtil.screenToWorld(new Vec2(x, y));
Vec2 zmPt = ViewUtil.screenToWorld(new Vec2(x, y), true);

zoom -= zoom * amount * ZOOM_MULTIPLIER;
if (zoom < minZoom) {
Expand Down
18 changes: 14 additions & 4 deletions src/com/modsim/gui/view/ViewUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ public class ViewUtil implements MouseListener, MouseMotionListener, MouseWheelL
private double oldCX, oldCY;
private boolean camDrag = false;

public static boolean snap = true;
public static int snapGridSize = 25;

/**
* Finds a clicked link by approximate (binary-search) closest point on curve
* @param pt World-space point to check
Expand Down Expand Up @@ -208,14 +211,19 @@ public static List<PickableEntity> screenSpace_entitiesWithin(double x, double y
/**
* Adjusts an on-screen point to world-space
*/
public static Vec2 screenToWorld(Vec2 p) {
public static Vec2 screenToWorld(Vec2 p, boolean noSnap) {
double[] pt = p.asArray();

try {Main.ui.view.wToV.inverseTransform(pt, 0, pt, 0, 1);}
catch (Exception e) {
return null;
}

if (snap && !noSnap) {
pt[0] = snapGridSize*(Math.round(pt[0]/snapGridSize));
pt[1] = snapGridSize*(Math.round(pt[1]/snapGridSize));
}

return new Vec2(pt);
}

Expand Down Expand Up @@ -277,20 +285,22 @@ public void mousePressed(MouseEvent e) {
else {
Port p = screenSpace_portAt(e.getX(), e.getY());

Main.selection.clear();

//Link behaviour
if (p != null) {
Main.selection.clear();

tool = new MakeLinkTool();
Main.ui.view.curTool = tool.lbDown(e.getX(), e.getY(), e.isShiftDown());
}
else {
// Link edit if we haven't hit a module
if (targ == null) {
Vec2 worldSpace = screenToWorld(new Vec2(e.getX(), e.getY()));
Vec2 worldSpace = screenToWorld(new Vec2(e.getX(), e.getY()) , true);
Link l = worldSpace_linkAt(worldSpace);

if (l != null) {
Main.selection.clear();

tool = new EditLinkTool(l);
Main.ui.view.curTool = tool.mouseMove(e.getX(), e.getY());
return;
Expand Down
9 changes: 8 additions & 1 deletion src/com/modsim/operations/Ops.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.modsim.Main;
import com.modsim.gui.HelpWindow;
import com.modsim.gui.view.ViewUtil;
import com.modsim.modules.BaseModule;
import com.modsim.simulator.PickableEntity;
import com.modsim.tools.PlaceTool;
Expand Down Expand Up @@ -134,7 +135,11 @@ public static void fileNew() {
}

// Core application actions
public static final DesignAction undo, redo, copy, paste, delete, rotateCW, rotateCCW, rotate180,
public static final DesignAction
undo, redo,
copy, paste, delete,
rotateCW, rotateCCW, rotate180,
toggleSnap,
labelEdit, labelBig, labelSmall,
pause, run, step, toggleRun, zoomIn, zoomOut, resetView, toggleAA, open, save, saveAs, fileNew, quit;

Expand Down Expand Up @@ -204,6 +209,8 @@ public static void fileNew() {
rotate180 = new DesignAction(event -> doRotate(BaseModule.rotationDir.ROT_180),
"Rotate 180");

toggleSnap = new DesignAction(event -> ViewUtil.snap = !ViewUtil.snap, "Toggle Snap");

// Label editing
labelEdit = new DesignAction(event -> {
String labelStr = JOptionPane.showInputDialog(Main.ui.frame, "");
Expand Down
8 changes: 4 additions & 4 deletions src/com/modsim/tools/EditLinkTool.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public EditLinkTool(Link link) {

@Override
public BaseTool mouseMove(int x, int y) {
Vec2 worldPoint = ViewUtil.screenToWorld(new Vec2(x, y));
Vec2 worldPoint = ViewUtil.screenToWorld(new Vec2(x, y), false);

CtrlPt pickCtrl = link.path.closestCtrlPt(worldPoint);
nearbyInfo = link.path.approxClosestPoint(worldPoint, 10);
Expand All @@ -65,7 +65,7 @@ public boolean handlesRbDown() {
public BaseTool rbDown(int x, int y) {
if (placePoint != null) return this; // do nothing if we're placing a point

Vec2 worldPoint = ViewUtil.screenToWorld(new Vec2(x, y));
Vec2 worldPoint = ViewUtil.screenToWorld(new Vec2(x, y), false);
CtrlPt pickCtrl = link.path.closestCtrlPt(worldPoint);

if (pickCtrl != null && pickCtrl.pos.dist(worldPoint) < 25) {
Expand All @@ -79,7 +79,7 @@ public BaseTool rbDown(int x, int y) {
@Override
public BaseTool lbDown(int x, int y, boolean isShiftDown) {
// Try picking nearest control point
Vec2 worldPoint = ViewUtil.screenToWorld(new Vec2(x, y));
Vec2 worldPoint = ViewUtil.screenToWorld(new Vec2(x, y), false);
CtrlPt pickCtrl = link.path.closestCtrlPt(worldPoint);

if (pickCtrl != null && pickCtrl.pos.dist(worldPoint) < 25) {
Expand Down Expand Up @@ -109,7 +109,7 @@ else if (nearbyInfo.pt.dist(worldPoint) < 20) {
@Override
public BaseTool mouseDrag(int x, int y) {
if (placePoint != null) {
Vec2 worldPoint = ViewUtil.screenToWorld(new Vec2(x, y));
Vec2 worldPoint = ViewUtil.screenToWorld(new Vec2(x, y), false);
placePoint.pos.set(worldPoint);
editPoint.set(placePoint.pos);
link.path.calcCurves();
Expand Down
2 changes: 1 addition & 1 deletion src/com/modsim/tools/MakeLinkTool.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public BaseTool lbDown(int x, int y, boolean isShiftDown) {
@Override
public BaseTool mouseMove(int x, int y) {
if (working) {
current = ViewUtil.screenToWorld(new Vec2(x, y));
current = ViewUtil.screenToWorld(new Vec2(x, y), false);

Port p = ViewUtil.screenSpace_portAt(x, y);
if (p != null) {
Expand Down
6 changes: 3 additions & 3 deletions src/com/modsim/tools/MoveTool.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public class MoveTool extends BaseTool {
* Start a move operation. Assumes a selection exists in the View.
*/
public MoveTool(int x, int y) {
startPt = ViewUtil.screenToWorld(new Vec2(x,y));
startPt = ViewUtil.screenToWorld(new Vec2(x,y), false);
entities = Main.selection.getEntities();

for (PickableEntity e : entities) {
Expand All @@ -28,7 +28,7 @@ public MoveTool(int x, int y) {

@Override
public BaseTool mouseDrag(int x, int y) {
Vec2 p = ViewUtil.screenToWorld(new Vec2(x, y));
Vec2 p = ViewUtil.screenToWorld(new Vec2(x, y), false);
p.sub(startPt);

for (PickableEntity e : entities) {
Expand All @@ -40,7 +40,7 @@ public BaseTool mouseDrag(int x, int y) {

@Override
public BaseTool lbUp(int x, int y) {
Vec2 p = ViewUtil.screenToWorld(new Vec2(x, y));
Vec2 p = ViewUtil.screenToWorld(new Vec2(x, y), false);
p.sub(startPt);

// We're done - store the operation
Expand Down
4 changes: 2 additions & 2 deletions src/com/modsim/tools/PlaceTool.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public PlaceTool(PickableEntity e) {
Main.opStack.pushOp(new CreateOperation(e));

Vec2 p = new Vec2(-200, -200);
p = ViewUtil.screenToWorld(p);
p = ViewUtil.screenToWorld(p, false);
e.move(p);
}

Expand All @@ -47,7 +47,7 @@ public PlaceTool(ModuleClipboard clipboard) {

@Override
public BaseTool mouseMove(int x, int y) {
Vec2 cur = ViewUtil.screenToWorld(new Vec2(x, y));
Vec2 cur = ViewUtil.screenToWorld(new Vec2(x, y), false);

if (start == null) {
start = new Vec2(cur);
Expand Down
6 changes: 3 additions & 3 deletions src/com/modsim/tools/SelectTool.java
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public BaseTool lbDown(int x, int y, boolean isShiftDown) {
synchronized (this) {
// Don't actually set dragging=true till we get a mouseDrag() event
screenDragStart.set(x, y);
dragStart.set(ViewUtil.screenToWorld(screenDragStart));
dragStart.set(ViewUtil.screenToWorld(screenDragStart, true));

// Need to base contextual action on the item at the START of the drag
pickedEntity = ViewUtil.screenSpace_entityAt(x, y);
Expand Down Expand Up @@ -95,7 +95,7 @@ public BaseTool mouseDrag(int x, int y) {
dragging = true;

// N.B. dragStart is already set thanks to lbDown() handler
dragPos.set(ViewUtil.screenToWorld(new Vec2(x, y)));
dragPos.set(ViewUtil.screenToWorld(new Vec2(x, y), true));
}
}
}
Expand Down Expand Up @@ -127,7 +127,7 @@ else if (y > Main.ui.view.getHeight() - edgeWidth) {
}

//System.out.println("More dragging " + x + ", " + y);
dragPos.set(ViewUtil.screenToWorld(new Vec2(x, y)));
dragPos.set(ViewUtil.screenToWorld(new Vec2(x, y), true));

Vec2 delta = new Vec2(dragPos);
delta.sub(dragStart);
Expand Down
5 changes: 1 addition & 4 deletions src/com/modsim/util/BinData.java
Original file line number Diff line number Diff line change
Expand Up @@ -243,11 +243,8 @@ else if (b0 == 2) {
else if (b1 == 2) {
return b0;
}
else if (b0 == b1) {
return b0;
}
else {
return 2;
return b0 | b1;
}
}
}