mirror of
https://github.com/bancika/diy-layout-creator
synced 2026-11-10 17:22:09 +10:00
added mirror
This commit is contained in:
@@ -241,16 +241,6 @@ public interface IPlugInPort extends ISelectionProcessor, IMouseProcessor, IKeyP
|
||||
*/
|
||||
void setMetric(boolean isMetric);
|
||||
|
||||
/**
|
||||
* Groups all selected components.
|
||||
*/
|
||||
void groupSelectedComponents();
|
||||
|
||||
/**
|
||||
* Ungroups all selected components.
|
||||
*/
|
||||
void ungroupSelectedComponents();
|
||||
|
||||
/**
|
||||
* Locks or unlocks the specified layer. All components within +- 0.5 range will be affected by
|
||||
* the change as well.
|
||||
|
||||
@@ -1,19 +1,26 @@
|
||||
package org.diylc.common;
|
||||
|
||||
import java.awt.Point;
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
|
||||
import org.diylc.core.ExpansionMode;
|
||||
import org.diylc.core.IDIYComponent;
|
||||
|
||||
public interface ISelectionProcessor {
|
||||
|
||||
public static final int CLOCKWISE = 1;
|
||||
public static final int COUNTER_CLOCKWISE = 1;
|
||||
|
||||
public static final int HORIZONTAL = 0;
|
||||
public static final int VERTICAL = 1;
|
||||
|
||||
/**
|
||||
* Returns the current {@link ComponentTransferable}.
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
List<IDIYComponent<?>> getSelectedComponents();
|
||||
Collection<IDIYComponent<?>> getSelectedComponents();
|
||||
|
||||
/**
|
||||
* Updates the selection with the specified list of component. Also, updates control point map
|
||||
@@ -21,7 +28,7 @@ public interface ISelectionProcessor {
|
||||
*
|
||||
* @param newSelection
|
||||
*/
|
||||
void updateSelection(List<IDIYComponent<?>> newSelection);
|
||||
void updateSelection(Collection<IDIYComponent<?>> newSelection);
|
||||
|
||||
/**
|
||||
* Finds all components at the specified location, sorted by z-index from top to bottom.
|
||||
@@ -53,4 +60,16 @@ public interface ISelectionProcessor {
|
||||
* @param direction 1 for clockwise, -1 for counter-clockwise
|
||||
*/
|
||||
void rotateSelection(int direction);
|
||||
|
||||
/**
|
||||
* Groups all selected components.
|
||||
*/
|
||||
void groupSelectedComponents();
|
||||
|
||||
/**
|
||||
* Ungroups all selected components.
|
||||
*/
|
||||
void ungroupSelectedComponents();
|
||||
|
||||
void mirrorSelection(int direction);
|
||||
}
|
||||
|
||||
@@ -6,8 +6,10 @@ import java.awt.RenderingHints;
|
||||
import java.awt.image.BufferedImage;
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
import java.util.HashMap;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
@@ -185,19 +187,22 @@ public class ComponentProcessor {
|
||||
* @param selectedComponents
|
||||
* @return
|
||||
*/
|
||||
public List<PropertyWrapper> getMutualSelectionProperties(List<IDIYComponent<?>> selectedComponents) throws Exception {
|
||||
public List<PropertyWrapper> getMutualSelectionProperties(Collection<IDIYComponent<?>> selectedComponents) throws Exception {
|
||||
if (selectedComponents.isEmpty()) {
|
||||
return null;
|
||||
}
|
||||
List<PropertyWrapper> properties = new ArrayList<PropertyWrapper>();
|
||||
IDIYComponent<?> firstComponent = selectedComponents.get(0);
|
||||
|
||||
List<IDIYComponent<?>> selectedList = new ArrayList<IDIYComponent<?>>(selectedComponents);
|
||||
|
||||
IDIYComponent<?> firstComponent = selectedList.get(0);
|
||||
properties.addAll(extractProperties(firstComponent.getClass()));
|
||||
// Initialize values
|
||||
for (PropertyWrapper property : properties) {
|
||||
property.readFrom(firstComponent);
|
||||
}
|
||||
for (int i = 1; i < selectedComponents.size(); i++) {
|
||||
IDIYComponent<?> component = selectedComponents.get(i);
|
||||
IDIYComponent<?> component = selectedList.get(i);
|
||||
List<PropertyWrapper> newProperties = extractProperties(component.getClass());
|
||||
for (PropertyWrapper property : newProperties) {
|
||||
property.readFrom(component);
|
||||
@@ -219,14 +224,6 @@ public class ComponentProcessor {
|
||||
}
|
||||
}
|
||||
}
|
||||
// for (PropertyWrapper property : properties) {
|
||||
// try {
|
||||
// property.readUniqueFrom(component);
|
||||
// } catch (Exception e) {
|
||||
// // TODO Auto-generated catch block
|
||||
// e.printStackTrace();
|
||||
// }
|
||||
// }
|
||||
}
|
||||
Collections.sort(properties, ComparatorFactory.getInstance().getDefaultPropertyComparator());
|
||||
return properties;
|
||||
|
||||
@@ -11,6 +11,7 @@ import java.awt.Rectangle;
|
||||
import java.awt.RenderingHints;
|
||||
import java.awt.geom.Area;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
@@ -97,7 +98,7 @@ public class DrawingManager {
|
||||
* @return
|
||||
*/
|
||||
public List<IDIYComponent<?>> drawProject(Graphics2D g2d, Project project, Set<DrawOption> drawOptions,
|
||||
IComponentFiler filter, Rectangle selectionRect, List<IDIYComponent<?>> selectedComponents,
|
||||
IComponentFiler filter, Rectangle selectionRect, Collection<IDIYComponent<?>> selectedComponents,
|
||||
Set<IDIYComponent<?>> lockedComponents, Set<IDIYComponent<?>> groupedComponents, List<Point> controlPointSlot,
|
||||
List<IDIYComponent<?>> componentSlot, boolean dragInProgress) {
|
||||
failedComponents.clear();
|
||||
|
||||
@@ -41,6 +41,7 @@ import org.diylc.common.IComponentFiler;
|
||||
import org.diylc.common.IKeyProcessor;
|
||||
import org.diylc.common.IPlugIn;
|
||||
import org.diylc.common.IPlugInPort;
|
||||
import org.diylc.common.ISelectionProcessor;
|
||||
import org.diylc.common.Orientation;
|
||||
import org.diylc.common.OrientationHV;
|
||||
import org.diylc.common.PropertyWrapper;
|
||||
@@ -96,7 +97,7 @@ public class Presenter implements IPlugInPort {
|
||||
// Maps component class names to ComponentType objects.
|
||||
private List<IPlugIn> plugIns;
|
||||
|
||||
private List<IDIYComponent<?>> selectedComponents;
|
||||
private Set<IDIYComponent<?>> selectedComponents;
|
||||
// Maps components that have at least one dragged point to set of indices
|
||||
// that designate which of their control points are being dragged.
|
||||
private Map<IDIYComponent<?>, Set<Integer>> controlPointMap;
|
||||
@@ -131,7 +132,7 @@ public class Presenter implements IPlugInPort {
|
||||
this.view = view;
|
||||
plugIns = new ArrayList<IPlugIn>();
|
||||
messageDispatcher = new MessageDispatcher<EventType>(true);
|
||||
selectedComponents = new ArrayList<IDIYComponent<?>>();
|
||||
selectedComponents = new HashSet<IDIYComponent<?>>();
|
||||
lockedComponents = new HashSet<IDIYComponent<?>>();
|
||||
currentProject = new Project();
|
||||
// cloner = new Cloner();
|
||||
@@ -558,6 +559,7 @@ public class Presenter implements IPlugInPort {
|
||||
|| !newSelection.contains(topComponent)) {
|
||||
newSelection.clear();
|
||||
}
|
||||
|
||||
newSelection.addAll(findAllGroupedComponents(topComponent));
|
||||
}
|
||||
}
|
||||
@@ -754,7 +756,7 @@ public class Presenter implements IPlugInPort {
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<IDIYComponent<?>> getSelectedComponents() {
|
||||
public Collection<IDIYComponent<?>> getSelectedComponents() {
|
||||
return selectedComponents;
|
||||
}
|
||||
|
||||
@@ -1056,6 +1058,7 @@ public class Presenter implements IPlugInPort {
|
||||
@Override
|
||||
public void rotateSelection(int direction) {
|
||||
if (!selectedComponents.isEmpty()) {
|
||||
LOG.trace("Rotating selected components");
|
||||
Project oldProject = currentProject.clone();
|
||||
rotateComponents(this.selectedComponents, direction, isSnapToGrid());
|
||||
messageDispatcher.dispatchMessage(EventType.PROJECT_MODIFIED, oldProject, currentProject.clone(),
|
||||
@@ -1069,7 +1072,7 @@ public class Presenter implements IPlugInPort {
|
||||
* @param direction 1 for clockwise, -1 for counter-clockwise
|
||||
*/
|
||||
@SuppressWarnings("unchecked")
|
||||
private void rotateComponents(List<IDIYComponent<?>> components, int direction, boolean snapToGrid) {
|
||||
private void rotateComponents(Collection<IDIYComponent<?>> components, int direction, boolean snapToGrid) {
|
||||
Point center = getCenterOf(components, snapToGrid);
|
||||
|
||||
AffineTransform rotate = AffineTransform.getRotateInstance(Math.PI / 2 * direction, center.x, center.y);
|
||||
@@ -1129,7 +1132,56 @@ public class Presenter implements IPlugInPort {
|
||||
}
|
||||
}
|
||||
|
||||
private Point getCenterOf(List<IDIYComponent<?>> components, boolean snapToGrid) {
|
||||
@SuppressWarnings("unchecked")
|
||||
@Override
|
||||
public void mirrorSelection(int direction) {
|
||||
if (!selectedComponents.isEmpty()) {
|
||||
LOG.trace("Mirroring selected components");
|
||||
Project oldProject = currentProject.clone();
|
||||
|
||||
Point center = getCenterOf(selectedComponents, isSnapToGrid());
|
||||
|
||||
boolean canMirror = true;
|
||||
for (IDIYComponent<?> component : selectedComponents) {
|
||||
ComponentType type =
|
||||
ComponentProcessor.getInstance().extractComponentTypeFrom(
|
||||
(Class<? extends IDIYComponent<?>>) component.getClass());
|
||||
if (!type.isStretchable() && component.getControlPointCount() > 1) {
|
||||
canMirror = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (!canMirror)
|
||||
if (view.showConfirmDialog(
|
||||
"Selection contains components that cannot bi mirrored. Do you want to exclude them?", "Mirror Selection",
|
||||
IView.YES_NO_OPTION, IView.QUESTION_MESSAGE) != IView.YES_OPTION)
|
||||
return;
|
||||
|
||||
for (IDIYComponent<?> component : selectedComponents) {
|
||||
ComponentType type =
|
||||
ComponentProcessor.getInstance().extractComponentTypeFrom(
|
||||
(Class<? extends IDIYComponent<?>>) component.getClass());
|
||||
if (type.isStretchable() || component.getControlPointCount() == 1) {
|
||||
drawingManager.invalidateComponent(component);
|
||||
for (int i = 0; i < component.getControlPointCount(); i++) {
|
||||
Point p = component.getControlPoint(i);
|
||||
if (direction == ISelectionProcessor.HORIZONTAL) {
|
||||
component.setControlPoint(new Point(p.x - 2 * (p.x - center.x), p.y), i);
|
||||
} else {
|
||||
component.setControlPoint(new Point(p.x, p.y - 2 * (p.y - center.y)), i);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
messageDispatcher.dispatchMessage(EventType.PROJECT_MODIFIED, oldProject, currentProject.clone(),
|
||||
"Mirror Selection");
|
||||
messageDispatcher.dispatchMessage(EventType.REPAINT);
|
||||
}
|
||||
}
|
||||
|
||||
private Point getCenterOf(Collection<IDIYComponent<?>> components, boolean snapToGrid) {
|
||||
// Determine center of rotation
|
||||
int minX = Integer.MAX_VALUE;
|
||||
int minY = Integer.MAX_VALUE;
|
||||
@@ -1455,8 +1507,8 @@ public class Presenter implements IPlugInPort {
|
||||
messageDispatcher.dispatchMessage(EventType.REPAINT);
|
||||
}
|
||||
|
||||
public void updateSelection(List<IDIYComponent<?>> newSelection) {
|
||||
this.selectedComponents = newSelection;
|
||||
public void updateSelection(Collection<IDIYComponent<?>> newSelection) {
|
||||
this.selectedComponents = new HashSet<IDIYComponent<?>>(newSelection);
|
||||
Map<IDIYComponent<?>, Set<Integer>> controlPointMap = new HashMap<IDIYComponent<?>, Set<Integer>>();
|
||||
for (IDIYComponent<?> component : selectedComponents) {
|
||||
Set<Integer> indices = new HashSet<Integer>();
|
||||
@@ -1843,17 +1895,15 @@ public class Presenter implements IPlugInPort {
|
||||
if (this.selectedComponents.isEmpty()) {
|
||||
throw new RuntimeException("No components selected");
|
||||
}
|
||||
ComponentType selectedType =
|
||||
ComponentProcessor.getInstance().extractComponentTypeFrom(
|
||||
(Class<? extends IDIYComponent<?>>) this.selectedComponents.get(0).getClass());
|
||||
for (int i = 1; i < this.selectedComponents.size(); i++) {
|
||||
ComponentType newType =
|
||||
ComponentProcessor.getInstance().extractComponentTypeFrom(
|
||||
(Class<? extends IDIYComponent<?>>) this.selectedComponents.get(i).getClass());
|
||||
if (newType.getInstanceClass() != selectedType.getInstanceClass()) {
|
||||
ComponentType selectedType = null;
|
||||
Iterator<IDIYComponent<?>> iterator = this.selectedComponents.iterator();
|
||||
while (iterator.hasNext()) {
|
||||
ComponentType type = ComponentProcessor.getInstance().extractComponentTypeFrom((Class<? extends IDIYComponent<?>>) iterator.next().getClass());
|
||||
if (selectedType == null)
|
||||
selectedType = type;
|
||||
else if (selectedType.getInstanceClass() != type.getInstanceClass())
|
||||
throw new RuntimeException("Template can be applied on multiple components of the same type only");
|
||||
}
|
||||
}
|
||||
}
|
||||
return getTemplatesFor(selectedType.getCategory(), selectedType.getName());
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user