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:
+11
-2
@@ -139,13 +139,22 @@ public interface IPlugInPort {
|
||||
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
|
||||
*/
|
||||
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}.
|
||||
|
||||
+2
-1
@@ -144,7 +144,8 @@ class CanvasPanel extends JComponent implements Autoscroll {
|
||||
@Override
|
||||
public void mouseMoved(MouseEvent e) {
|
||||
setCursor(plugInPort.getCursorAt(e.getPoint()));
|
||||
plugInPort.mouseMoved(e.getPoint());
|
||||
plugInPort.mouseMoved(e.getPoint(), e.isControlDown(), e.isShiftDown(), e
|
||||
.isAltDown());
|
||||
}
|
||||
});
|
||||
addMouseListener(new MouseAdapter() {
|
||||
|
||||
+32
-10
@@ -5,8 +5,10 @@ import java.awt.GridBagConstraints;
|
||||
import java.awt.GridBagLayout;
|
||||
import java.awt.event.ActionEvent;
|
||||
import java.awt.event.ActionListener;
|
||||
import java.util.ArrayList;
|
||||
import java.util.EnumSet;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import javax.swing.BorderFactory;
|
||||
import javax.swing.JComboBox;
|
||||
@@ -15,6 +17,7 @@ import javax.swing.JPanel;
|
||||
import javax.swing.SwingUtilities;
|
||||
|
||||
import com.diyfever.diylc.common.BadPositionException;
|
||||
import com.diyfever.diylc.common.ControlPointWrapper;
|
||||
import com.diyfever.diylc.common.EventType;
|
||||
import com.diyfever.diylc.common.IPlugIn;
|
||||
import com.diyfever.diylc.common.IPlugInPort;
|
||||
@@ -159,7 +162,8 @@ public class StatusBar extends JPanel implements IPlugIn {
|
||||
refreshStatusText();
|
||||
break;
|
||||
case AVAILABLE_CTRL_POINTS_CHANGED:
|
||||
componentsUnderCursor = (List<IComponentInstance>) params[0];
|
||||
componentsUnderCursor = new ArrayList<IComponentInstance>(
|
||||
((Map<IComponentInstance, ControlPointWrapper>) params[0]).keySet());
|
||||
refreshStatusText();
|
||||
break;
|
||||
}
|
||||
@@ -170,16 +174,34 @@ public class StatusBar extends JPanel implements IPlugIn {
|
||||
if ((componentsUnderCursor == null) || (componentsUnderCursor.isEmpty())) {
|
||||
getStatusLabel().setText("");
|
||||
} else {
|
||||
String formattedNames = "";
|
||||
int n = 1;
|
||||
for (IComponentInstance component : componentsUnderCursor) {
|
||||
if (n > 1) {
|
||||
formattedNames += ", ";
|
||||
}
|
||||
formattedNames += ComponentProcessor.getInstance().extractBomName(component)
|
||||
+ " (<b><font color=\"blue\">" + n++ + "</font></b>)";
|
||||
// String formattedNames = "";
|
||||
// int n = 1;
|
||||
// for (IComponentInstance component : componentsUnderCursor) {
|
||||
// if (n > 1) {
|
||||
// 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)) + "\"");
|
||||
}
|
||||
getStatusLabel().setText("<html>" + "Edit: " + formattedNames + "</html>");
|
||||
}
|
||||
} else {
|
||||
getStatusLabel().setText(
|
||||
|
||||
+43
-14
@@ -19,6 +19,7 @@ import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
import java.util.Map.Entry;
|
||||
|
||||
import javax.swing.Action;
|
||||
import javax.swing.JComponent;
|
||||
@@ -70,7 +71,7 @@ public class Presenter implements IPlugInPort {
|
||||
private ComponentSelection selectedComponents;
|
||||
// List of component names that have at least one of their control points
|
||||
// under the last recorded mouse position.
|
||||
private List<IComponentInstance> componentsUnderCursor;
|
||||
private Map<IComponentInstance, ControlPointWrapper> componentsUnderCursor;
|
||||
|
||||
private Cloner cloner;
|
||||
|
||||
@@ -141,6 +142,9 @@ public class Presenter implements IPlugInPort {
|
||||
return Cursor.getPredefinedCursor(Cursor.HAND_CURSOR);
|
||||
}
|
||||
}
|
||||
if (componentsUnderCursor != null && !componentsUnderCursor.isEmpty()) {
|
||||
return Cursor.getPredefinedCursor(Cursor.HAND_CURSOR);
|
||||
}
|
||||
return Cursor.getDefaultCursor();
|
||||
}
|
||||
|
||||
@@ -376,8 +380,8 @@ public class Presenter implements IPlugInPort {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void mouseMoved(Point point) {
|
||||
List<IComponentInstance> components = new ArrayList<IComponentInstance>();
|
||||
public void mouseMoved(Point point, boolean ctrlDown, boolean shiftDown, boolean altDown) {
|
||||
Map<IComponentInstance, ControlPointWrapper> components = new HashMap<IComponentInstance, ControlPointWrapper>();
|
||||
Point scaledPoint = scalePoint(point);
|
||||
for (IComponentInstance component : currentProject.getComponents()) {
|
||||
List<ControlPointWrapper> currentPoints = ComponentProcessor.getInstance()
|
||||
@@ -387,7 +391,7 @@ public class Presenter implements IPlugInPort {
|
||||
try {
|
||||
controlPoint.readFrom(component);
|
||||
if (scaledPoint.distance(controlPoint.getValue()) < CONTROL_POINT_SENSITIVITY) {
|
||||
components.add(component);
|
||||
components.put(component, controlPoint);
|
||||
break;
|
||||
}
|
||||
} catch (Exception e) {
|
||||
@@ -425,8 +429,10 @@ public class Presenter implements IPlugInPort {
|
||||
dragStartPoint = point;
|
||||
Point scaledPoint = scalePoint(point);
|
||||
List<IComponentInstance> components = findComponentsAt(scaledPoint);
|
||||
// If no components are under the cursor, reset selection.
|
||||
if (components.isEmpty()) {
|
||||
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.
|
||||
selectedComponents.clear();
|
||||
messageDispatcher.dispatchMessage(EventType.SELECTION_CHANGED, selectedComponents);
|
||||
messageDispatcher.dispatchMessage(EventType.REPAINT);
|
||||
@@ -445,18 +451,35 @@ public class Presenter implements IPlugInPort {
|
||||
|
||||
@Override
|
||||
public boolean dragOver(Point point) {
|
||||
// If there's no selection, the only thing to do is update the selection
|
||||
// rectangle and refresh.
|
||||
if (selectedComponents.isEmpty()) {
|
||||
if (!componentsUnderCursor.isEmpty()) {
|
||||
int dx = (int) ((point.x - dragStartPoint.x) / zoomLevel);
|
||||
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);
|
||||
messageDispatcher.dispatchMessage(EventType.SELECTION_RECT_CHANGED, selectionRect);
|
||||
messageDispatcher.dispatchMessage(EventType.REPAINT);
|
||||
} else {
|
||||
// If there are components selected translate their control points.
|
||||
translateSelection(dragStartPoint, point);
|
||||
translateSelectedComponents(dragStartPoint, point);
|
||||
dragStartPoint = point;
|
||||
messageDispatcher.dispatchMessage(EventType.REPAINT);
|
||||
}
|
||||
messageDispatcher.dispatchMessage(EventType.REPAINT);
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -490,7 +513,7 @@ public class Presenter implements IPlugInPort {
|
||||
Project oldProject = cloner.deepClone(currentProject);
|
||||
|
||||
// If there are components selected translate their control points.
|
||||
translateSelection(dragStartPoint, point);
|
||||
translateSelectedComponents(dragStartPoint, point);
|
||||
if (!oldProject.equals(currentProject)) {
|
||||
messageDispatcher.dispatchMessage(EventType.PROJECT_MODIFIED, oldProject, cloner
|
||||
.deepClone(currentProject), "Move");
|
||||
@@ -500,7 +523,7 @@ public class Presenter implements IPlugInPort {
|
||||
dragInProgress = false;
|
||||
}
|
||||
|
||||
private void translateSelection(Point fromPoint, Point toPoint) {
|
||||
private void translateSelectedComponents(Point fromPoint, Point toPoint) {
|
||||
if (toPoint == null) {
|
||||
LOG.debug("Drag ended outside the drawing area.");
|
||||
return;
|
||||
@@ -668,6 +691,12 @@ public class Presenter implements IPlugInPort {
|
||||
messageDispatcher.dispatchMessage(EventType.SLOT_CHANGED, componentSlot);
|
||||
}
|
||||
|
||||
/**
|
||||
* Scales point from display base to actual base.
|
||||
*
|
||||
* @param point
|
||||
* @return
|
||||
*/
|
||||
private Point scalePoint(Point point) {
|
||||
return new Point((int) (point.x / zoomLevel), (int) (point.y / zoomLevel));
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user