fixed d&d alignment issues

This commit is contained in:
bancika
2011-01-03 18:33:18 +00:00
parent 76d0646095
commit 602bd81bb4
4 changed files with 98 additions and 46 deletions
@@ -20,12 +20,12 @@
</entry> </entry>
</map> </map>
</entry> </entry>
<entry>
<string>metric</string>
<boolean>false</boolean>
</entry>
<entry> <entry>
<string>lastPath</string> <string>lastPath</string>
<string>C:\Documents and Settings\Branislav Stojkovic\Desktop</string> <string>C:\Documents and Settings\Branislav Stojkovic\Desktop</string>
</entry> </entry>
<entry>
<string>metric</string>
<boolean>true</boolean>
</entry>
</map> </map>
@@ -18,6 +18,7 @@ import com.diyfever.diylc.model.measures.Resistance;
import com.diyfever.diylc.model.measures.ResistanceUnit; import com.diyfever.diylc.model.measures.ResistanceUnit;
import com.diyfever.diylc.model.measures.Size; import com.diyfever.diylc.model.measures.Size;
import com.diyfever.diylc.model.measures.SizeUnit; import com.diyfever.diylc.model.measures.SizeUnit;
import com.diyfever.diylc.utils.Constants;
public class ResistorInstance implements IComponentInstance { public class ResistorInstance implements IComponentInstance {
@@ -29,7 +30,7 @@ public class ResistorInstance implements IComponentInstance {
private Size s = new Size(1d, SizeUnit.cm); private Size s = new Size(1d, SizeUnit.cm);
private Color color = Color.green; private Color color = Color.green;
private Point leftTopCorner = new Point(0, 0); private Point leftTopCorner = new Point(0, 0);
private Point otherCorner = new Point(100, 100); private Point otherCorner = new Point((int) (Constants.GRID * 10), (int) (Constants.GRID * 10));
private String name = "something"; private String name = "something";
public ResistorInstance(String testField, Capacitance c, Resistance r, Size s, Color color, public ResistorInstance(String testField, Capacitance c, Resistance r, Size s, Color color,
@@ -3,6 +3,8 @@ package com.diyfever.diylc.gui;
import java.awt.BorderLayout; import java.awt.BorderLayout;
import java.awt.Container; import java.awt.Container;
import java.awt.Dimension; import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter; import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent; import java.awt.event.WindowEvent;
import java.util.HashMap; import java.util.HashMap;
@@ -17,7 +19,7 @@ import javax.swing.JMenu;
import javax.swing.JMenuBar; import javax.swing.JMenuBar;
import javax.swing.JPanel; import javax.swing.JPanel;
import javax.swing.SwingConstants; import javax.swing.SwingConstants;
import javax.swing.SwingUtilities; import javax.swing.Timer;
import com.diyfever.diylc.common.BadPositionException; import com.diyfever.diylc.common.BadPositionException;
import com.diyfever.diylc.model.Project; import com.diyfever.diylc.model.Project;
@@ -95,14 +97,24 @@ public class MainFrame extends JFrame implements IView {
super.setVisible(b); super.setVisible(b);
// TODO: hack to prevent painting issues in the scroll bar rulers. Find // TODO: hack to prevent painting issues in the scroll bar rulers. Find
// a better fix if possible. // a better fix if possible.
if (b) { Timer timer = new Timer(50, new ActionListener() {
SwingUtilities.invokeLater(new Runnable() {
@Override @Override
public void run() { public void actionPerformed(ActionEvent e) {
canvasPlugin.refresh(); canvasPlugin.refresh();
} }
}); });
} timer.setRepeats(false);
timer.start();
// if (b) {
// SwingUtilities.invokeLater(new Runnable() {
// @Override
// public void run() {
//
// }
// });
// }
} }
private void createBasePanels() { private void createBasePanels() {
@@ -87,7 +87,8 @@ public class Presenter implements IPlugInPort {
// D&D // D&D
private boolean dragInProgress = false; private boolean dragInProgress = false;
private Point dragStartPoint = null; // Previous mouse location, not scaled for zoom factor.
private Point previousDragPoint = null;
private Project preDragProject = null; private Project preDragProject = null;
private IComponentType componentSlot; private IComponentType componentSlot;
@@ -136,8 +137,10 @@ public class Presenter implements IPlugInPort {
@Override @Override
public Cursor getCursorAt(Point point) { public Cursor getCursorAt(Point point) {
// Only change the cursor if we're not making a new component.
if (componentSlot == null) {
// Scale point to remove zoom factor. // Scale point to remove zoom factor.
Point2D scaledPoint = new Point2D.Double(point.getX() / zoomLevel, point.getY() / zoomLevel); Point2D scaledPoint = scalePoint(point);
for (Map.Entry<IComponentInstance, Area> entry : componentAreaMap.entrySet()) { for (Map.Entry<IComponentInstance, Area> entry : componentAreaMap.entrySet()) {
if (entry.getValue().contains(scaledPoint)) { if (entry.getValue().contains(scaledPoint)) {
return Cursor.getPredefinedCursor(Cursor.HAND_CURSOR); return Cursor.getPredefinedCursor(Cursor.HAND_CURSOR);
@@ -146,6 +149,7 @@ public class Presenter implements IPlugInPort {
if (componentsUnderCursor != null && !componentsUnderCursor.isEmpty()) { if (componentsUnderCursor != null && !componentsUnderCursor.isEmpty()) {
return Cursor.getPredefinedCursor(Cursor.HAND_CURSOR); return Cursor.getPredefinedCursor(Cursor.HAND_CURSOR);
} }
}
return Cursor.getDefaultCursor(); return Cursor.getDefaultCursor();
} }
@@ -297,17 +301,23 @@ public class Presenter implements IPlugInPort {
} }
g2d.setTransform(initialTx); g2d.setTransform(initialTx);
if ((drawOptions.contains(DrawOption.ZOOM)) && (Math.abs(1.0 - zoomLevel) > 1e-4)) {
g2d.scale(zoomLevel, zoomLevel);
}
// At the end draw selection rectangle. // At the end draw selection rectangle.
if (drawOptions.contains(DrawOption.SELECTION) && (selectionRect != null)) { if (drawOptions.contains(DrawOption.SELECTION) && (selectionRect != null)) {
g2d.setColor(Color.white); g2d.setColor(Color.white);
g2d.drawRect(selectionRect.x, selectionRect.y, selectionRect.width, g2d.draw(selectionRect);
selectionRect.height);
g2d.setColor(Color.black); g2d.setColor(Color.black);
g2d.setStroke(Constants.dashedStroke); g2d.setStroke(Constants.dashedStroke);
g2d.drawRect(selectionRect.x, selectionRect.y, selectionRect.width, g2d.draw(selectionRect);
selectionRect.height);
} }
// g2d.setColor(Color.red);
// for (Area area : componentAreaMap.values()) {
// g2d.draw(area);
// }
} }
@Override @Override
@@ -431,9 +441,9 @@ public class Presenter implements IPlugInPort {
public void dragStarted(Point point) { public void dragStarted(Point point) {
LOG.debug(String.format("dragStarted(%s)", point)); LOG.debug(String.format("dragStarted(%s)", point));
dragInProgress = true; dragInProgress = true;
dragStartPoint = point;
preDragProject = cloner.deepClone(currentProject); preDragProject = cloner.deepClone(currentProject);
Point scaledPoint = scalePoint(point); Point scaledPoint = scalePoint(point);
previousDragPoint = scaledPoint;
List<IComponentInstance> components = findComponentsAt(scaledPoint); List<IComponentInstance> components = findComponentsAt(scaledPoint);
if (!componentsUnderCursor.isEmpty()) { if (!componentsUnderCursor.isEmpty()) {
// If there are control points under the cursor, drag them. // If there are control points under the cursor, drag them.
@@ -457,33 +467,53 @@ public class Presenter implements IPlugInPort {
@Override @Override
public boolean dragOver(Point point) { public boolean dragOver(Point point) {
Point scaledPoint = scalePoint(point);
if (!componentsUnderCursor.isEmpty()) { if (!componentsUnderCursor.isEmpty()) {
int dx = (int) ((point.x - dragStartPoint.x) / zoomLevel); // We're dragging control point(s).
int dy = (int) ((point.y - dragStartPoint.y) / zoomLevel); // int dx = (int) ((point.x - dragStartPoint.x) / zoomLevel);
// int dy = (int) ((point.y - dragStartPoint.y) / zoomLevel);
//
IComponentInstance firstComponent = componentsUnderCursor.keySet().iterator().next();
ControlPointWrapper controlPoint = componentsUnderCursor.get(firstComponent);
// Re-read the value just in case.
try {
controlPoint.readFrom(firstComponent);
} catch (Exception e) {
e.printStackTrace();
}
int x = (int) (Math
.round((controlPoint.getValue().x + scaledPoint.x - previousDragPoint.x)
/ Constants.GRID) * Constants.GRID);
int y = (int) (Math
.round((controlPoint.getValue().y + scaledPoint.y - previousDragPoint.y)
/ Constants.GRID) * Constants.GRID);
previousDragPoint.setLocation(x, y);
for (Entry<IComponentInstance, ControlPointWrapper> entry : componentsUnderCursor for (Entry<IComponentInstance, ControlPointWrapper> entry : componentsUnderCursor
.entrySet()) { .entrySet()) {
try { try {
ControlPointWrapper controlPoint = entry.getValue(); controlPoint = entry.getValue();
IComponentInstance component = entry.getKey(); IComponentInstance component = entry.getKey();
controlPoint.readFrom(component); controlPoint.readFrom(component);
if (controlPoint.isEditable()) { if (controlPoint.isEditable()) {
controlPoint.getValue().translate(dx, dy); controlPoint.getValue().setLocation(x, y);
} }
controlPoint.writeTo(component); controlPoint.writeTo(component);
} catch (Exception e) { } catch (Exception e) {
} }
} }
dragStartPoint = point; // dragStartPoint = point;
} else if (selectedComponents.isEmpty()) { } else if (selectedComponents.isEmpty()) {
// If there's no selection, the only thing to do is update the // If there's no selection, the only thing to do is update the
// selection rectangle and refresh. // selection rectangle and refresh.
this.selectionRect = Utils.createRectangle(point, dragStartPoint); this.selectionRect = Utils.createRectangle(scaledPoint, previousDragPoint);
messageDispatcher.dispatchMessage(EventType.SELECTION_RECT_CHANGED, selectionRect); // messageDispatcher.dispatchMessage(EventType.SELECTION_RECT_CHANGED,
// selectionRect);
} else { } else {
// If there are components selected translate their control points. // If there are components selected translate their control points.
translateSelectedComponents(dragStartPoint, point); translateSelectedComponents(previousDragPoint, scaledPoint);
dragStartPoint = point; // dragStartPoint = point;
} }
messageDispatcher.dispatchMessage(EventType.REPAINT); messageDispatcher.dispatchMessage(EventType.REPAINT);
return true; return true;
@@ -495,19 +525,17 @@ public class Presenter implements IPlugInPort {
if (!dragInProgress) { if (!dragInProgress) {
return; return;
} }
Point scaledPoint = scalePoint(point);
if (selectedComponents.isEmpty()) { if (selectedComponents.isEmpty()) {
// If there's no selection finalize selectionRect and see which // If there's no selection finalize selectionRect and see which
// components intersect with it. // components intersect with it.
if (point != null) { if (scaledPoint != null) {
this.selectionRect = Utils.createRectangle(point, dragStartPoint); this.selectionRect = Utils.createRectangle(scaledPoint, previousDragPoint);
} }
selectedComponents.clear(); selectedComponents.clear();
for (IComponentInstance component : currentProject.getComponents()) { for (IComponentInstance component : currentProject.getComponents()) {
Area area = componentAreaMap.get(component); Area area = componentAreaMap.get(component);
if ((area != null) if ((area != null) && area.intersects(selectionRect)) {
&& area.intersects(selectionRect.x / zoomLevel,
selectionRect.y / zoomLevel, selectionRect.width / zoomLevel,
selectionRect.height / zoomLevel)) {
selectedComponents.add(component); selectedComponents.add(component);
} }
} }
@@ -530,8 +558,9 @@ public class Presenter implements IPlugInPort {
LOG.debug("Drag ended outside the drawing area."); LOG.debug("Drag ended outside the drawing area.");
return; return;
} }
int dx = (int) ((toPoint.x - fromPoint.x) / zoomLevel); int dx = (int) (Math.round((toPoint.x - fromPoint.x) / zoomLevel / Constants.GRID) * Constants.GRID);
int dy = (int) ((toPoint.y - fromPoint.y) / zoomLevel); int dy = (int) (Math.round((toPoint.y - fromPoint.y) / zoomLevel / Constants.GRID) * Constants.GRID);
fromPoint.translate(dx, dy);
for (IComponentInstance component : selectedComponents) { for (IComponentInstance component : selectedComponents) {
List<ControlPointWrapper> controlPoints = ComponentProcessor.getInstance() List<ControlPointWrapper> controlPoints = ComponentProcessor.getInstance()
.extractControlPoints(component.getClass()); .extractControlPoints(component.getClass());
@@ -539,7 +568,7 @@ public class Presenter implements IPlugInPort {
try { try {
controlPoint.readFrom(component); controlPoint.readFrom(component);
if (controlPoint.isEditable()) { if (controlPoint.isEditable()) {
controlPoint.getValue().translate(dx, dy); translateControlPoint(controlPoint, dx, dy);
} }
controlPoint.writeTo(component); controlPoint.writeTo(component);
} catch (Exception e) { } catch (Exception e) {
@@ -549,6 +578,12 @@ public class Presenter implements IPlugInPort {
} }
} }
private void translateControlPoint(ControlPointWrapper controlPoint, int dx, int dy) {
int x = controlPoint.getValue().x + dx;
int y = controlPoint.getValue().y + dy;
controlPoint.getValue().setLocation(x, y);
}
@Override @Override
public void addComponents(List<IComponentInstance> components, Point preferredPoint) { public void addComponents(List<IComponentInstance> components, Point preferredPoint) {
LOG.debug(String.format("addComponents(%s)", components)); LOG.debug(String.format("addComponents(%s)", components));
@@ -628,7 +663,11 @@ public class Presenter implements IPlugInPort {
if (point != null) { if (point != null) {
for (ControlPointWrapper controlPoint : controlPoints) { for (ControlPointWrapper controlPoint : controlPoints) {
controlPoint.readFrom(component); controlPoint.readFrom(component);
controlPoint.getValue().translate(point.x, point.y); int x = controlPoint.getValue().x + point.x;
int y = controlPoint.getValue().y + point.y;
x = (int) (Math.round(x / Constants.GRID) * Constants.GRID);
y = (int) (Math.round(y / Constants.GRID) * Constants.GRID);
controlPoint.getValue().setLocation(x, y);
controlPoint.writeTo(component); controlPoint.writeTo(component);
} }
} }