Skip to content

Commit 605cdc8

Browse files
authored
Merge pull request #39 from EdNutting/master
Fixes and improvements
2 parents eb244d3 + 44f22b8 commit 605cdc8

File tree

10 files changed

+39
-23
lines changed

10 files changed

+39
-23
lines changed

src/com/modsim/gui/Menu.java

+2
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,8 @@ private void addEditMenu() {
6464
edit.add(new JMenuItem(Ops.rotateCCW));
6565
edit.add(new JMenuItem(Ops.rotate180));
6666
edit.addSeparator();
67+
edit.add(new JMenuItem(Ops.toggleSnap));
68+
edit.addSeparator();
6769
edit.add(new JMenuItem(Ops.labelEdit));
6870

6971
app_menu.add(edit);

src/com/modsim/gui/view/View.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -300,7 +300,7 @@ public void setTool(BaseTool newTool) {
300300
* @param y Y-coordinate
301301
*/
302302
public void zoom(int x, int y, double amount) {
303-
Vec2 zmPt = ViewUtil.screenToWorld(new Vec2(x, y));
303+
Vec2 zmPt = ViewUtil.screenToWorld(new Vec2(x, y), true);
304304

305305
zoom -= zoom * amount * ZOOM_MULTIPLIER;
306306
if (zoom < minZoom) {

src/com/modsim/gui/view/ViewUtil.java

+14-4
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@ public class ViewUtil implements MouseListener, MouseMotionListener, MouseWheelL
2727
private double oldCX, oldCY;
2828
private boolean camDrag = false;
2929

30+
public static boolean snap = true;
31+
public static int snapGridSize = 25;
32+
3033
/**
3134
* Finds a clicked link by approximate (binary-search) closest point on curve
3235
* @param pt World-space point to check
@@ -208,14 +211,19 @@ public static List<PickableEntity> screenSpace_entitiesWithin(double x, double y
208211
/**
209212
* Adjusts an on-screen point to world-space
210213
*/
211-
public static Vec2 screenToWorld(Vec2 p) {
214+
public static Vec2 screenToWorld(Vec2 p, boolean noSnap) {
212215
double[] pt = p.asArray();
213216

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

222+
if (snap && !noSnap) {
223+
pt[0] = snapGridSize*(Math.round(pt[0]/snapGridSize));
224+
pt[1] = snapGridSize*(Math.round(pt[1]/snapGridSize));
225+
}
226+
219227
return new Vec2(pt);
220228
}
221229

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

280-
Main.selection.clear();
281-
282288
//Link behaviour
283289
if (p != null) {
290+
Main.selection.clear();
291+
284292
tool = new MakeLinkTool();
285293
Main.ui.view.curTool = tool.lbDown(e.getX(), e.getY(), e.isShiftDown());
286294
}
287295
else {
288296
// Link edit if we haven't hit a module
289297
if (targ == null) {
290-
Vec2 worldSpace = screenToWorld(new Vec2(e.getX(), e.getY()));
298+
Vec2 worldSpace = screenToWorld(new Vec2(e.getX(), e.getY()) , true);
291299
Link l = worldSpace_linkAt(worldSpace);
292300

293301
if (l != null) {
302+
Main.selection.clear();
303+
294304
tool = new EditLinkTool(l);
295305
Main.ui.view.curTool = tool.mouseMove(e.getX(), e.getY());
296306
return;

src/com/modsim/operations/Ops.java

+8-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import com.modsim.Main;
44
import com.modsim.gui.HelpWindow;
5+
import com.modsim.gui.view.ViewUtil;
56
import com.modsim.modules.BaseModule;
67
import com.modsim.simulator.PickableEntity;
78
import com.modsim.tools.PlaceTool;
@@ -134,7 +135,11 @@ public static void fileNew() {
134135
}
135136

136137
// Core application actions
137-
public static final DesignAction undo, redo, copy, paste, delete, rotateCW, rotateCCW, rotate180,
138+
public static final DesignAction
139+
undo, redo,
140+
copy, paste, delete,
141+
rotateCW, rotateCCW, rotate180,
142+
toggleSnap,
138143
labelEdit, labelBig, labelSmall,
139144
pause, run, step, toggleRun, zoomIn, zoomOut, resetView, toggleAA, open, save, saveAs, fileNew, quit;
140145

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

212+
toggleSnap = new DesignAction(event -> ViewUtil.snap = !ViewUtil.snap, "Toggle Snap");
213+
207214
// Label editing
208215
labelEdit = new DesignAction(event -> {
209216
String labelStr = JOptionPane.showInputDialog(Main.ui.frame, "");

src/com/modsim/tools/EditLinkTool.java

+4-4
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ public EditLinkTool(Link link) {
3838

3939
@Override
4040
public BaseTool mouseMove(int x, int y) {
41-
Vec2 worldPoint = ViewUtil.screenToWorld(new Vec2(x, y));
41+
Vec2 worldPoint = ViewUtil.screenToWorld(new Vec2(x, y), false);
4242

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

68-
Vec2 worldPoint = ViewUtil.screenToWorld(new Vec2(x, y));
68+
Vec2 worldPoint = ViewUtil.screenToWorld(new Vec2(x, y), false);
6969
CtrlPt pickCtrl = link.path.closestCtrlPt(worldPoint);
7070

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

8585
if (pickCtrl != null && pickCtrl.pos.dist(worldPoint) < 25) {
@@ -109,7 +109,7 @@ else if (nearbyInfo.pt.dist(worldPoint) < 20) {
109109
@Override
110110
public BaseTool mouseDrag(int x, int y) {
111111
if (placePoint != null) {
112-
Vec2 worldPoint = ViewUtil.screenToWorld(new Vec2(x, y));
112+
Vec2 worldPoint = ViewUtil.screenToWorld(new Vec2(x, y), false);
113113
placePoint.pos.set(worldPoint);
114114
editPoint.set(placePoint.pos);
115115
link.path.calcCurves();

src/com/modsim/tools/MakeLinkTool.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ public BaseTool lbDown(int x, int y, boolean isShiftDown) {
4848
@Override
4949
public BaseTool mouseMove(int x, int y) {
5050
if (working) {
51-
current = ViewUtil.screenToWorld(new Vec2(x, y));
51+
current = ViewUtil.screenToWorld(new Vec2(x, y), false);
5252

5353
Port p = ViewUtil.screenSpace_portAt(x, y);
5454
if (p != null) {

src/com/modsim/tools/MoveTool.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ public class MoveTool extends BaseTool {
1717
* Start a move operation. Assumes a selection exists in the View.
1818
*/
1919
public MoveTool(int x, int y) {
20-
startPt = ViewUtil.screenToWorld(new Vec2(x,y));
20+
startPt = ViewUtil.screenToWorld(new Vec2(x,y), false);
2121
entities = Main.selection.getEntities();
2222

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

2929
@Override
3030
public BaseTool mouseDrag(int x, int y) {
31-
Vec2 p = ViewUtil.screenToWorld(new Vec2(x, y));
31+
Vec2 p = ViewUtil.screenToWorld(new Vec2(x, y), false);
3232
p.sub(startPt);
3333

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

4141
@Override
4242
public BaseTool lbUp(int x, int y) {
43-
Vec2 p = ViewUtil.screenToWorld(new Vec2(x, y));
43+
Vec2 p = ViewUtil.screenToWorld(new Vec2(x, y), false);
4444
p.sub(startPt);
4545

4646
// We're done - store the operation

src/com/modsim/tools/PlaceTool.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ public PlaceTool(PickableEntity e) {
2828
Main.opStack.pushOp(new CreateOperation(e));
2929

3030
Vec2 p = new Vec2(-200, -200);
31-
p = ViewUtil.screenToWorld(p);
31+
p = ViewUtil.screenToWorld(p, false);
3232
e.move(p);
3333
}
3434

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

4848
@Override
4949
public BaseTool mouseMove(int x, int y) {
50-
Vec2 cur = ViewUtil.screenToWorld(new Vec2(x, y));
50+
Vec2 cur = ViewUtil.screenToWorld(new Vec2(x, y), false);
5151

5252
if (start == null) {
5353
start = new Vec2(cur);

src/com/modsim/tools/SelectTool.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ public BaseTool lbDown(int x, int y, boolean isShiftDown) {
6565
synchronized (this) {
6666
// Don't actually set dragging=true till we get a mouseDrag() event
6767
screenDragStart.set(x, y);
68-
dragStart.set(ViewUtil.screenToWorld(screenDragStart));
68+
dragStart.set(ViewUtil.screenToWorld(screenDragStart, true));
6969

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

9797
// N.B. dragStart is already set thanks to lbDown() handler
98-
dragPos.set(ViewUtil.screenToWorld(new Vec2(x, y)));
98+
dragPos.set(ViewUtil.screenToWorld(new Vec2(x, y), true));
9999
}
100100
}
101101
}
@@ -127,7 +127,7 @@ else if (y > Main.ui.view.getHeight() - edgeWidth) {
127127
}
128128

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

132132
Vec2 delta = new Vec2(dragPos);
133133
delta.sub(dragStart);

src/com/modsim/util/BinData.java

+1-4
Original file line numberDiff line numberDiff line change
@@ -243,11 +243,8 @@ else if (b0 == 2) {
243243
else if (b1 == 2) {
244244
return b0;
245245
}
246-
else if (b0 == b1) {
247-
return b0;
248-
}
249246
else {
250-
return 2;
247+
return b0 | b1;
251248
}
252249
}
253250
}

0 commit comments

Comments
 (0)