Z-order component handling

This commit is contained in:
bancika
2011-01-08 10:16:17 +00:00
parent e0a3a64696
commit d2cc7cd214
2 changed files with 37 additions and 22 deletions
@@ -50,7 +50,7 @@ public class MainFrame extends JFrame implements IView {
private CanvasPlugin canvasPlugin; private CanvasPlugin canvasPlugin;
public MainFrame() { public MainFrame() {
super("DIYLC4"); super("DIYLC 3");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setPreferredSize(new Dimension(800, 600)); setPreferredSize(new Dimension(800, 600));
createBasePanels(); createBasePanels();
@@ -59,7 +59,7 @@ public class MainFrame extends JFrame implements IView {
this.presenter = new Presenter(this); this.presenter = new Presenter(this);
setTitle(getTitle() + " " + presenter.getCurrentVersionNumber()); setTitle(getTitle() + " v." + presenter.getCurrentVersionNumber());
presenter.installPlugin(new ToolBox()); presenter.installPlugin(new ToolBox());
presenter.installPlugin(new FileManager()); presenter.installPlugin(new FileManager());
@@ -427,7 +427,7 @@ public class Presenter implements IPlugInPort {
if (components.isEmpty()) { if (components.isEmpty()) {
selectedComponents.clear(); selectedComponents.clear();
} else { } else {
IDIYComponent<?> component = components.get(0); IDIYComponent<?> component = components.get(components.size() - 1);
// If ctrl is pressed just toggle the component under mouse // If ctrl is pressed just toggle the component under mouse
// cursor. // cursor.
if (ctrlDown) { if (ctrlDown) {
@@ -451,10 +451,11 @@ public class Presenter implements IPlugInPort {
public void mouseMoved(Point point, boolean ctrlDown, boolean shiftDown, boolean altDown) { public void mouseMoved(Point point, boolean ctrlDown, boolean shiftDown, boolean altDown) {
Map<IDIYComponent<?>, Set<Integer>> components = new HashMap<IDIYComponent<?>, Set<Integer>>(); Map<IDIYComponent<?>, Set<Integer>> components = new HashMap<IDIYComponent<?>, Set<Integer>>();
Point scaledPoint = scalePoint(point); Point scaledPoint = scalePoint(point);
for (IDIYComponent<?> component : currentProject.getComponents()) { // Go backwards so we take the highest z-order components first.
// Go backwards so we take the highest z-order components first. for (int i = currentProject.getComponents().size() - 1; i >= 0; i--) {
for (int i = component.getControlPointCount() - 1; i >= 0; i--) { IDIYComponent<?> component = currentProject.getComponents().get(i);
Point controlPoint = component.getControlPoint(i); for (int pointIndex = 0; pointIndex < component.getControlPointCount(); pointIndex++) {
Point controlPoint = component.getControlPoint(pointIndex);
if (shouldShowControlPointsFor(component)) { if (shouldShowControlPointsFor(component)) {
try { try {
if (scaledPoint.distance(controlPoint) < CONTROL_POINT_SENSITIVITY) { if (scaledPoint.distance(controlPoint) < CONTROL_POINT_SENSITIVITY) {
@@ -462,7 +463,7 @@ public class Presenter implements IPlugInPort {
ComponentType componentType = componentTypeMap ComponentType componentType = componentTypeMap
.get(component.getClass()); .get(component.getClass());
if (componentType.isStretchable()) { if (componentType.isStretchable()) {
indices.add(i); indices.add(pointIndex);
} else { } else {
for (int j = 0; j < component.getControlPointCount(); j++) { for (int j = 0; j < component.getControlPointCount(); j++) {
indices.add(j); indices.add(j);
@@ -523,7 +524,7 @@ public class Presenter implements IPlugInPort {
messageDispatcher.dispatchMessage(EventType.SELECTION_CHANGED, selectedComponents); messageDispatcher.dispatchMessage(EventType.SELECTION_CHANGED, selectedComponents);
messageDispatcher.dispatchMessage(EventType.REPAINT); messageDispatcher.dispatchMessage(EventType.REPAINT);
} else { } else {
IDIYComponent<?> component = components.get(0); IDIYComponent<?> component = components.get(components.size() - 1);
// If the component under the cursor is not already selected, make // If the component under the cursor is not already selected, make
// it into the only selected component. // it into the only selected component.
if (!selectedComponents.contains(component)) { if (!selectedComponents.contains(component)) {
@@ -591,7 +592,9 @@ public class Presenter implements IPlugInPort {
repaint = translateSelectedComponents(previousDragPoint, scaledPoint); repaint = translateSelectedComponents(previousDragPoint, scaledPoint);
// dragStartPoint = point; // dragStartPoint = point;
} }
messageDispatcher.dispatchMessage(EventType.REPAINT); if (repaint) {
messageDispatcher.dispatchMessage(EventType.REPAINT);
}
return true; return true;
} }
@@ -657,7 +660,9 @@ public class Presenter implements IPlugInPort {
public void addComponents(List<IDIYComponent<?>> components, Point preferredPoint) { public void addComponents(List<IDIYComponent<?>> components, Point preferredPoint) {
LOG.debug(String.format("addComponents(%s)", components)); LOG.debug(String.format("addComponents(%s)", components));
Project oldProject = cloner.deepClone(currentProject); Project oldProject = cloner.deepClone(currentProject);
currentProject.getComponents().addAll(components); for (IDIYComponent<?> component : components) {
addComponent(component, componentTypeMap.get(component.getClass()));
}
messageDispatcher.dispatchMessage(EventType.PROJECT_MODIFIED, oldProject, cloner messageDispatcher.dispatchMessage(EventType.PROJECT_MODIFIED, oldProject, cloner
.deepClone(currentProject), "Add"); .deepClone(currentProject), "Add");
messageDispatcher.dispatchMessage(EventType.REPAINT); messageDispatcher.dispatchMessage(EventType.REPAINT);
@@ -713,6 +718,26 @@ public class Presenter implements IPlugInPort {
} }
} }
/**
* Adds a component to the project taking z-order into account.
*
* @param component
* @param componentType
*/
private void addComponent(IDIYComponent<?> component, ComponentType componentType) {
int index = 0;
while (index < currentProject.getComponents().size()
&& componentType.getLayer().ordinal() >= componentTypeMap.get(
currentProject.getComponents().get(index).getClass()).getLayer().ordinal()) {
index++;
}
if (index < currentProject.getComponents().size()) {
currentProject.getComponents().add(index, component);
} else {
currentProject.getComponents().add(component);
}
}
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
private void instantiateComponent(ComponentType componentType, Point point) throws Exception { private void instantiateComponent(ComponentType componentType, Point point) throws Exception {
LOG.info("Instatiating component of type: " + componentType.getInstanceClass().getName()); LOG.info("Instatiating component of type: " + componentType.getInstanceClass().getName());
@@ -739,17 +764,7 @@ public class Presenter implements IPlugInPort {
component.setName(componentType.getNamePrefix() + i); component.setName(componentType.getNamePrefix() + i);
// Add it to the project taking z-order into account. // Add it to the project taking z-order into account.
int index = 0; addComponent(component, componentType);
while (index < currentProject.getComponents().size()
&& componentType.getLayer().ordinal() >= componentTypeMap.get(
currentProject.getComponents().get(index).getClass()).getLayer().ordinal()) {
index++;
}
if (index < currentProject.getComponents().size()) {
currentProject.getComponents().add(index, component);
} else {
currentProject.getComponents().add(component);
}
// Translate them to the desired location. // Translate them to the desired location.
if (point != null) { if (point != null) {