mirror of
https://github.com/bancika/diy-layout-creator
synced 2026-11-11 01:32:08 +10:00
improved d&d
This commit is contained in:
+2
-2
@@ -57,7 +57,7 @@ public class ResistorInstance implements IComponentInstance {
|
|||||||
this.name = name;
|
this.name = name;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ControlPoint(visibilityPolicy = VisibilityPolicy.WHEN_SELECTED)
|
@ControlPoint(visibilityPolicy = VisibilityPolicy.ALWAYS)
|
||||||
public Point getLeftTopCorner() {
|
public Point getLeftTopCorner() {
|
||||||
return leftTopCorner;
|
return leftTopCorner;
|
||||||
}
|
}
|
||||||
@@ -66,7 +66,7 @@ public class ResistorInstance implements IComponentInstance {
|
|||||||
this.leftTopCorner = leftTopCorner;
|
this.leftTopCorner = leftTopCorner;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ControlPoint(visibilityPolicy = VisibilityPolicy.WHEN_SELECTED)
|
@ControlPoint(visibilityPolicy = VisibilityPolicy.ALWAYS)
|
||||||
public Point getOtherCorner() {
|
public Point getOtherCorner() {
|
||||||
return otherCorner;
|
return otherCorner;
|
||||||
}
|
}
|
||||||
|
|||||||
+11
-2
@@ -139,13 +139,22 @@ public interface IPlugInPort {
|
|||||||
void setZoomLevel(double zoomLevel);
|
void setZoomLevel(double zoomLevel);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Note: point coordinates are scaled for zoom factor.
|
* Notifies the presenter that mouse is clicked.
|
||||||
|
*
|
||||||
|
* Note: point coordinates are display based, i.e. scaled for zoom factor.
|
||||||
*
|
*
|
||||||
* @param point
|
* @param point
|
||||||
*/
|
*/
|
||||||
void mouseClicked(Point point, boolean ctrlDown, boolean shiftDown, boolean altDown);
|
void mouseClicked(Point point, boolean ctrlDown, boolean shiftDown, boolean altDown);
|
||||||
|
|
||||||
void mouseMoved(Point point);
|
/**
|
||||||
|
* Notifies the presenter that mouse is moved.
|
||||||
|
*
|
||||||
|
* Note: point coordinates are display based, i.e. scaled for zoom factor.
|
||||||
|
*
|
||||||
|
* @param point
|
||||||
|
*/
|
||||||
|
void mouseMoved(Point point, boolean ctrlDown, boolean shiftDown, boolean altDown);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns the current {@link ComponentSelection}.
|
* Returns the current {@link ComponentSelection}.
|
||||||
|
|||||||
+2
-1
@@ -144,7 +144,8 @@ class CanvasPanel extends JComponent implements Autoscroll {
|
|||||||
@Override
|
@Override
|
||||||
public void mouseMoved(MouseEvent e) {
|
public void mouseMoved(MouseEvent e) {
|
||||||
setCursor(plugInPort.getCursorAt(e.getPoint()));
|
setCursor(plugInPort.getCursorAt(e.getPoint()));
|
||||||
plugInPort.mouseMoved(e.getPoint());
|
plugInPort.mouseMoved(e.getPoint(), e.isControlDown(), e.isShiftDown(), e
|
||||||
|
.isAltDown());
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
addMouseListener(new MouseAdapter() {
|
addMouseListener(new MouseAdapter() {
|
||||||
|
|||||||
+32
-10
@@ -5,8 +5,10 @@ import java.awt.GridBagConstraints;
|
|||||||
import java.awt.GridBagLayout;
|
import java.awt.GridBagLayout;
|
||||||
import java.awt.event.ActionEvent;
|
import java.awt.event.ActionEvent;
|
||||||
import java.awt.event.ActionListener;
|
import java.awt.event.ActionListener;
|
||||||
|
import java.util.ArrayList;
|
||||||
import java.util.EnumSet;
|
import java.util.EnumSet;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
import javax.swing.BorderFactory;
|
import javax.swing.BorderFactory;
|
||||||
import javax.swing.JComboBox;
|
import javax.swing.JComboBox;
|
||||||
@@ -15,6 +17,7 @@ import javax.swing.JPanel;
|
|||||||
import javax.swing.SwingUtilities;
|
import javax.swing.SwingUtilities;
|
||||||
|
|
||||||
import com.diyfever.diylc.common.BadPositionException;
|
import com.diyfever.diylc.common.BadPositionException;
|
||||||
|
import com.diyfever.diylc.common.ControlPointWrapper;
|
||||||
import com.diyfever.diylc.common.EventType;
|
import com.diyfever.diylc.common.EventType;
|
||||||
import com.diyfever.diylc.common.IPlugIn;
|
import com.diyfever.diylc.common.IPlugIn;
|
||||||
import com.diyfever.diylc.common.IPlugInPort;
|
import com.diyfever.diylc.common.IPlugInPort;
|
||||||
@@ -159,7 +162,8 @@ public class StatusBar extends JPanel implements IPlugIn {
|
|||||||
refreshStatusText();
|
refreshStatusText();
|
||||||
break;
|
break;
|
||||||
case AVAILABLE_CTRL_POINTS_CHANGED:
|
case AVAILABLE_CTRL_POINTS_CHANGED:
|
||||||
componentsUnderCursor = (List<IComponentInstance>) params[0];
|
componentsUnderCursor = new ArrayList<IComponentInstance>(
|
||||||
|
((Map<IComponentInstance, ControlPointWrapper>) params[0]).keySet());
|
||||||
refreshStatusText();
|
refreshStatusText();
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
@@ -170,16 +174,34 @@ public class StatusBar extends JPanel implements IPlugIn {
|
|||||||
if ((componentsUnderCursor == null) || (componentsUnderCursor.isEmpty())) {
|
if ((componentsUnderCursor == null) || (componentsUnderCursor.isEmpty())) {
|
||||||
getStatusLabel().setText("");
|
getStatusLabel().setText("");
|
||||||
} else {
|
} else {
|
||||||
String formattedNames = "";
|
// String formattedNames = "";
|
||||||
int n = 1;
|
// int n = 1;
|
||||||
for (IComponentInstance component : componentsUnderCursor) {
|
// for (IComponentInstance component : componentsUnderCursor) {
|
||||||
if (n > 1) {
|
// if (n > 1) {
|
||||||
formattedNames += ", ";
|
// formattedNames += ", ";
|
||||||
|
// }
|
||||||
|
// formattedNames +=
|
||||||
|
// ComponentProcessor.getInstance().extractBomName(component);//
|
||||||
|
// +
|
||||||
|
// // " (<b><font color=\"blue\">"
|
||||||
|
// // +
|
||||||
|
// // n++
|
||||||
|
// // +
|
||||||
|
// // "</font></b>)";
|
||||||
|
// }
|
||||||
|
if (componentsUnderCursor.size() == 1) {
|
||||||
|
getStatusLabel().setText(
|
||||||
|
"Drag \""
|
||||||
|
+ ComponentProcessor.getInstance().extractBomName(
|
||||||
|
componentsUnderCursor.get(0)) + "\"");
|
||||||
|
} else {
|
||||||
|
getStatusLabel().setText(
|
||||||
|
"Hold CTRL key to drag all "
|
||||||
|
+ componentsUnderCursor.size()
|
||||||
|
+ " components under the cursor or drag only \""
|
||||||
|
+ ComponentProcessor.getInstance().extractBomName(
|
||||||
|
componentsUnderCursor.get(0)) + "\"");
|
||||||
}
|
}
|
||||||
formattedNames += ComponentProcessor.getInstance().extractBomName(component)
|
|
||||||
+ " (<b><font color=\"blue\">" + n++ + "</font></b>)";
|
|
||||||
}
|
|
||||||
getStatusLabel().setText("<html>" + "Edit: " + formattedNames + "</html>");
|
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
getStatusLabel().setText(
|
getStatusLabel().setText(
|
||||||
|
|||||||
+42
-13
@@ -19,6 +19,7 @@ import java.util.HashMap;
|
|||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
|
import java.util.Map.Entry;
|
||||||
|
|
||||||
import javax.swing.Action;
|
import javax.swing.Action;
|
||||||
import javax.swing.JComponent;
|
import javax.swing.JComponent;
|
||||||
@@ -70,7 +71,7 @@ public class Presenter implements IPlugInPort {
|
|||||||
private ComponentSelection selectedComponents;
|
private ComponentSelection selectedComponents;
|
||||||
// List of component names that have at least one of their control points
|
// List of component names that have at least one of their control points
|
||||||
// under the last recorded mouse position.
|
// under the last recorded mouse position.
|
||||||
private List<IComponentInstance> componentsUnderCursor;
|
private Map<IComponentInstance, ControlPointWrapper> componentsUnderCursor;
|
||||||
|
|
||||||
private Cloner cloner;
|
private Cloner cloner;
|
||||||
|
|
||||||
@@ -141,6 +142,9 @@ public class Presenter implements IPlugInPort {
|
|||||||
return Cursor.getPredefinedCursor(Cursor.HAND_CURSOR);
|
return Cursor.getPredefinedCursor(Cursor.HAND_CURSOR);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
if (componentsUnderCursor != null && !componentsUnderCursor.isEmpty()) {
|
||||||
|
return Cursor.getPredefinedCursor(Cursor.HAND_CURSOR);
|
||||||
|
}
|
||||||
return Cursor.getDefaultCursor();
|
return Cursor.getDefaultCursor();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -376,8 +380,8 @@ public class Presenter implements IPlugInPort {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void mouseMoved(Point point) {
|
public void mouseMoved(Point point, boolean ctrlDown, boolean shiftDown, boolean altDown) {
|
||||||
List<IComponentInstance> components = new ArrayList<IComponentInstance>();
|
Map<IComponentInstance, ControlPointWrapper> components = new HashMap<IComponentInstance, ControlPointWrapper>();
|
||||||
Point scaledPoint = scalePoint(point);
|
Point scaledPoint = scalePoint(point);
|
||||||
for (IComponentInstance component : currentProject.getComponents()) {
|
for (IComponentInstance component : currentProject.getComponents()) {
|
||||||
List<ControlPointWrapper> currentPoints = ComponentProcessor.getInstance()
|
List<ControlPointWrapper> currentPoints = ComponentProcessor.getInstance()
|
||||||
@@ -387,7 +391,7 @@ public class Presenter implements IPlugInPort {
|
|||||||
try {
|
try {
|
||||||
controlPoint.readFrom(component);
|
controlPoint.readFrom(component);
|
||||||
if (scaledPoint.distance(controlPoint.getValue()) < CONTROL_POINT_SENSITIVITY) {
|
if (scaledPoint.distance(controlPoint.getValue()) < CONTROL_POINT_SENSITIVITY) {
|
||||||
components.add(component);
|
components.put(component, controlPoint);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
@@ -425,8 +429,10 @@ public class Presenter implements IPlugInPort {
|
|||||||
dragStartPoint = point;
|
dragStartPoint = point;
|
||||||
Point scaledPoint = scalePoint(point);
|
Point scaledPoint = scalePoint(point);
|
||||||
List<IComponentInstance> components = findComponentsAt(scaledPoint);
|
List<IComponentInstance> components = findComponentsAt(scaledPoint);
|
||||||
|
if (!componentsUnderCursor.isEmpty()) {
|
||||||
|
// If there are control points under the cursor, drag them.
|
||||||
|
} else if (components.isEmpty()) {
|
||||||
// If no components are under the cursor, reset selection.
|
// If no components are under the cursor, reset selection.
|
||||||
if (components.isEmpty()) {
|
|
||||||
selectedComponents.clear();
|
selectedComponents.clear();
|
||||||
messageDispatcher.dispatchMessage(EventType.SELECTION_CHANGED, selectedComponents);
|
messageDispatcher.dispatchMessage(EventType.SELECTION_CHANGED, selectedComponents);
|
||||||
messageDispatcher.dispatchMessage(EventType.REPAINT);
|
messageDispatcher.dispatchMessage(EventType.REPAINT);
|
||||||
@@ -445,18 +451,35 @@ public class Presenter implements IPlugInPort {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean dragOver(Point point) {
|
public boolean dragOver(Point point) {
|
||||||
// If there's no selection, the only thing to do is update the selection
|
if (!componentsUnderCursor.isEmpty()) {
|
||||||
// rectangle and refresh.
|
int dx = (int) ((point.x - dragStartPoint.x) / zoomLevel);
|
||||||
if (selectedComponents.isEmpty()) {
|
int dy = (int) ((point.y - dragStartPoint.y) / zoomLevel);
|
||||||
|
for (Entry<IComponentInstance, ControlPointWrapper> entry : componentsUnderCursor
|
||||||
|
.entrySet()) {
|
||||||
|
try {
|
||||||
|
ControlPointWrapper controlPoint = entry.getValue();
|
||||||
|
IComponentInstance component = entry.getKey();
|
||||||
|
controlPoint.readFrom(component);
|
||||||
|
if (controlPoint.isEditable()) {
|
||||||
|
controlPoint.getValue().translate(dx, dy);
|
||||||
|
}
|
||||||
|
controlPoint.writeTo(component);
|
||||||
|
} catch (Exception e) {
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
dragStartPoint = point;
|
||||||
|
} else if (selectedComponents.isEmpty()) {
|
||||||
|
// If there's no selection, the only thing to do is update the
|
||||||
|
// selection rectangle and refresh.
|
||||||
this.selectionRect = Utils.createRectangle(point, dragStartPoint);
|
this.selectionRect = Utils.createRectangle(point, dragStartPoint);
|
||||||
messageDispatcher.dispatchMessage(EventType.SELECTION_RECT_CHANGED, selectionRect);
|
messageDispatcher.dispatchMessage(EventType.SELECTION_RECT_CHANGED, selectionRect);
|
||||||
messageDispatcher.dispatchMessage(EventType.REPAINT);
|
|
||||||
} else {
|
} else {
|
||||||
// If there are components selected translate their control points.
|
// If there are components selected translate their control points.
|
||||||
translateSelection(dragStartPoint, point);
|
translateSelectedComponents(dragStartPoint, point);
|
||||||
dragStartPoint = point;
|
dragStartPoint = point;
|
||||||
messageDispatcher.dispatchMessage(EventType.REPAINT);
|
|
||||||
}
|
}
|
||||||
|
messageDispatcher.dispatchMessage(EventType.REPAINT);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -490,7 +513,7 @@ public class Presenter implements IPlugInPort {
|
|||||||
Project oldProject = cloner.deepClone(currentProject);
|
Project oldProject = cloner.deepClone(currentProject);
|
||||||
|
|
||||||
// If there are components selected translate their control points.
|
// If there are components selected translate their control points.
|
||||||
translateSelection(dragStartPoint, point);
|
translateSelectedComponents(dragStartPoint, point);
|
||||||
if (!oldProject.equals(currentProject)) {
|
if (!oldProject.equals(currentProject)) {
|
||||||
messageDispatcher.dispatchMessage(EventType.PROJECT_MODIFIED, oldProject, cloner
|
messageDispatcher.dispatchMessage(EventType.PROJECT_MODIFIED, oldProject, cloner
|
||||||
.deepClone(currentProject), "Move");
|
.deepClone(currentProject), "Move");
|
||||||
@@ -500,7 +523,7 @@ public class Presenter implements IPlugInPort {
|
|||||||
dragInProgress = false;
|
dragInProgress = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
private void translateSelection(Point fromPoint, Point toPoint) {
|
private void translateSelectedComponents(Point fromPoint, Point toPoint) {
|
||||||
if (toPoint == null) {
|
if (toPoint == null) {
|
||||||
LOG.debug("Drag ended outside the drawing area.");
|
LOG.debug("Drag ended outside the drawing area.");
|
||||||
return;
|
return;
|
||||||
@@ -668,6 +691,12 @@ public class Presenter implements IPlugInPort {
|
|||||||
messageDispatcher.dispatchMessage(EventType.SLOT_CHANGED, componentSlot);
|
messageDispatcher.dispatchMessage(EventType.SLOT_CHANGED, componentSlot);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Scales point from display base to actual base.
|
||||||
|
*
|
||||||
|
* @param point
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
private Point scalePoint(Point point) {
|
private Point scalePoint(Point point) {
|
||||||
return new Point((int) (point.x / zoomLevel), (int) (point.y / zoomLevel));
|
return new Point((int) (point.x / zoomLevel), (int) (point.y / zoomLevel));
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user