setup sticky and visibility policy for each point inidividually

This commit is contained in:
bancika
2011-01-17 19:46:42 +00:00
parent 7ad87076d3
commit f37e0dfc95
25 changed files with 198 additions and 57 deletions
@@ -12,7 +12,6 @@ import org.diylc.core.IDIYComponent;
*
* @see IDIYComponent
*/
@SuppressWarnings("rawtypes")
public class ComponentType {
private String name;
@@ -25,12 +24,10 @@ public class ComponentType {
private Class<? extends IDIYComponent> instanceClass;
private double zOrder;
private boolean stretchable;
private boolean sticky;
public ComponentType(String name, String description, CreationMethod creationMethod,
String category, String namePrefix, String author, Icon icon,
Class<? extends IDIYComponent> instanceClass, double zOrder, boolean stretchable,
boolean sticky) {
Class<? extends IDIYComponent> instanceClass, double zOrder, boolean stretchable) {
super();
this.name = name;
this.description = description;
@@ -42,7 +39,6 @@ public class ComponentType {
this.instanceClass = instanceClass;
this.zOrder = zOrder;
this.stretchable = stretchable;
this.sticky = sticky;
}
public String getName() {
@@ -85,10 +81,6 @@ public class ComponentType {
return stretchable;
}
public boolean isSticky() {
return sticky;
}
@Override
public String toString() {
return name;
@@ -11,6 +11,7 @@ import java.awt.geom.CubicCurve2D;
import org.diylc.core.ComponentState;
import org.diylc.core.IDrawingObserver;
import org.diylc.core.Project;
import org.diylc.core.VisibilityPolicy;
import org.diylc.core.annotations.EditableProperty;
import org.diylc.core.measures.Size;
import org.diylc.core.measures.SizeUnit;
@@ -98,6 +99,16 @@ public abstract class AbstractCurvedComponent<T> extends AbstractTransparentComp
return controlPoints.length;
}
@Override
public boolean isControlPointSticky(int index) {
return index == 0 || index == 3;
}
@Override
public VisibilityPolicy getControlPointVisibilityPolicy(int index) {
return index == 0 || index == 3 ? VisibilityPolicy.ALWAYS : VisibilityPolicy.WHEN_SELECTED;
}
@Override
public Point getControlPoint(int index) {
return controlPoints[index];
@@ -15,6 +15,7 @@ import org.diylc.common.Display;
import org.diylc.core.ComponentState;
import org.diylc.core.IDrawingObserver;
import org.diylc.core.Project;
import org.diylc.core.VisibilityPolicy;
import org.diylc.core.annotations.EditableProperty;
import org.diylc.core.measures.Size;
import org.diylc.core.measures.SizeUnit;
@@ -167,9 +168,22 @@ public abstract class AbstractLeadedComponent<T> extends AbstractTransparentComp
return (Point) points[index];
}
@Override
public boolean isControlPointSticky(int index) {
return true;
}
@Override
public VisibilityPolicy getControlPointVisibilityPolicy(int index) {
return VisibilityPolicy.ALWAYS;
}
@Override
public void setControlPoint(Point point, int index) {
points[index].setLocation(point);
// Only accept the change if ending points are not the same.
if (!point.equals(points[1 - index])) {
points[index].setLocation(point);
}
}
@EditableProperty(name = "Color")
@@ -86,6 +86,10 @@ public interface IDIYComponent<T> extends Serializable {
* @param index
*/
void setControlPoint(Point point, int index);
boolean isControlPointSticky(int index);
VisibilityPolicy getControlPointVisibilityPolicy(int index);
/**
* Draws the component onto the {@link Graphics2D}.
@@ -59,9 +59,4 @@ public @interface ComponentDescriptor {
*/
boolean stretchable() default true;
/**
* @return true, if control points may stick to other components' control
* points.
*/
boolean sticky() default false;
}
@@ -62,7 +62,6 @@ public class ComponentProcessor {
Icon icon;
double zOrder;
boolean stretchable;
boolean sticky;
if (clazz.isAnnotationPresent(ComponentDescriptor.class)) {
ComponentDescriptor annotation = clazz.getAnnotation(ComponentDescriptor.class);
name = annotation.name();
@@ -73,7 +72,6 @@ public class ComponentProcessor {
author = annotation.author();
zOrder = annotation.zOrder();
stretchable = annotation.stretchable();
sticky = annotation.sticky();
} else {
name = clazz.getSimpleName();
description = "";
@@ -83,7 +81,6 @@ public class ComponentProcessor {
author = "Unknown";
zOrder = IDIYComponent.COMPONENT;
stretchable = true;
sticky = false;
}
icon = null;
// Draw component icon.
@@ -92,7 +89,9 @@ public class ComponentProcessor {
Image image = new BufferedImage(Presenter.ICON_SIZE, Presenter.ICON_SIZE,
java.awt.image.BufferedImage.TYPE_INT_ARGB);
Graphics2D g2d = (Graphics2D) image.getGraphics();
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g2d
.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
componentInstance.drawIcon(g2d, Presenter.ICON_SIZE, Presenter.ICON_SIZE);
icon = new ImageIcon(image);
} catch (InstantiationException e) {
@@ -101,7 +100,7 @@ public class ComponentProcessor {
e.printStackTrace();
}
ComponentType componentType = new ComponentType(name, description, creationMethod,
category, namePrefix, author, icon, clazz, zOrder, stretchable, sticky);
category, namePrefix, author, icon, clazz, zOrder, stretchable);
return componentType;
}
@@ -306,14 +306,15 @@ public class Presenter implements IPlugInPort {
if (currentProject == null) {
return;
}
Set<IDIYComponent<?>> drawControlPoints = new HashSet<IDIYComponent<?>>();
Set<IDIYComponent<?>> groupedComponents = new HashSet<IDIYComponent<?>>();
for (IDIYComponent<?> component : currentProject.getComponents()) {
if (shouldShowControlPointsFor(component)) {
drawControlPoints.add(component);
// Only try to draw control points of ungrouped
if (findAllGroupedComponents(component).size() > 1) {
groupedComponents.add(component);
}
}
projectPainter.drawProject(g2d, currentProject, drawOptions, filter, selectionRect,
selectedComponents, drawControlPoints, Arrays.asList(controlPointSlot,
selectedComponents, groupedComponents, Arrays.asList(controlPointSlot,
potentialControlPoint), dragInProgress, zoomLevel);
}
@@ -569,13 +570,14 @@ public class Presenter implements IPlugInPort {
for (IDIYComponent<?> component : currentProject.getComponents()) {
// Do not process a component if it's already in the map.
ComponentType componentType = componentTypeMap.get(component.getClass().getName());
if (!controlPointMap.containsKey(component) && componentType.isSticky()) {
// Check if there's a control point in the current selection
// that matches with one of its control points.
for (int i = 0; i < component.getControlPointCount(); i++) {
if (controlPointMap.containsKey(component)) {
break;
}
// Check if there's a control point in the current selection
// that matches with one of its control points.
for (int i = 0; i < component.getControlPointCount(); i++) {
if (controlPointMap.containsKey(component)) {
break;
}
if (!controlPointMap.containsKey(component) && component.isControlPointSticky(i)) {
boolean componentMatches = false;
for (Map.Entry<IDIYComponent<?>, Set<Integer>> entry : controlPointMap
.entrySet()) {
@@ -1154,11 +1156,6 @@ public class Presenter implements IPlugInPort {
* component.
*/
private boolean shouldShowControlPointsFor(IDIYComponent<?> component) {
ComponentType componentType = componentTypeMap.get(component.getClass().getName());
// Do not show control points for non-stretchable components.
if (!componentType.isStretchable()) {
return false;
}
if (findAllGroupedComponents(component).size() > 1) {
return false;
}
@@ -22,6 +22,7 @@ import org.diylc.common.IComponentFiler;
import org.diylc.core.ComponentState;
import org.diylc.core.IDIYComponent;
import org.diylc.core.Project;
import org.diylc.core.VisibilityPolicy;
import org.diylc.utils.Constants;
/**
@@ -50,7 +51,7 @@ public class ProjectPainter {
public void drawProject(Graphics2D g2d, Project project, Set<DrawOption> drawOptions,
IComponentFiler filter, Rectangle selectionRect, ComponentSelection selectedComponents,
Set<IDIYComponent<?>> drawControlPoints, List<Point> controlPointSlot,
Set<IDIYComponent<?>> groupedComponents, List<Point> controlPointSlot,
boolean dragInProgress, double zoomLevel) {
if (project == null) {
return;
@@ -137,10 +138,17 @@ public class ProjectPainter {
if (drawOptions.contains(DrawOption.CONTROL_POINTS)) {
// Draw unselected points first to make sure they are below.
for (IDIYComponent<?> component : project.getComponents()) {
if (drawControlPoints.contains(component)
&& !selectedComponents.contains(component)) {
g2dWrapper.setColor(Constants.CONTROL_POINT_COLOR);
for (int i = 0; i < component.getControlPointCount(); i++) {
for (int i = 0; i < component.getControlPointCount(); i++) {
VisibilityPolicy visibilityPolicy = component
.getControlPointVisibilityPolicy(i);
if ((groupedComponents.contains(component)
&& (visibilityPolicy == VisibilityPolicy.ALWAYS || (selectedComponents
.contains(component) && visibilityPolicy == VisibilityPolicy.WHEN_SELECTED)) || (!groupedComponents
.contains(component)
&& !selectedComponents.contains(component) && component
.getControlPointVisibilityPolicy(i) == VisibilityPolicy.ALWAYS))) {
g2dWrapper.setColor(Constants.CONTROL_POINT_COLOR);
Point controlPoint = component.getControlPoint(i);
int pointSize = CONTROL_POINT_SIZE - 2;
g2dWrapper.fillOval(controlPoint.x - pointSize / 2, controlPoint.y
@@ -150,9 +158,12 @@ public class ProjectPainter {
}
// Then draw the selected ones.
for (IDIYComponent<?> component : selectedComponents) {
if (drawControlPoints.contains(component)) {
g2dWrapper.setColor(Constants.SELECTED_CONTROL_POINT_COLOR);
for (int i = 0; i < component.getControlPointCount(); i++) {
for (int i = 0; i < component.getControlPointCount(); i++) {
if (!groupedComponents.contains(component)
&& (component.getControlPointVisibilityPolicy(i) == VisibilityPolicy.WHEN_SELECTED || component
.getControlPointVisibilityPolicy(i) == VisibilityPolicy.ALWAYS)) {
g2dWrapper.setColor(Constants.SELECTED_CONTROL_POINT_COLOR);
Point controlPoint = component.getControlPoint(i);
int pointSize = CONTROL_POINT_SIZE;
g2dWrapper.fillOval(controlPoint.x - pointSize / 2, controlPoint.y
@@ -8,6 +8,7 @@ import org.diylc.core.ComponentState;
import org.diylc.core.IDIYComponent;
import org.diylc.core.IDrawingObserver;
import org.diylc.core.Project;
import org.diylc.core.VisibilityPolicy;
import org.diylc.core.annotations.ComponentDescriptor;
import org.diylc.core.annotations.EditableProperty;
import org.diylc.core.measures.Capacitance;
@@ -59,6 +60,16 @@ public class MockDIYComponent implements IDIYComponent<Capacitance> {
return 1;
}
@Override
public boolean isControlPointSticky(int index) {
return false;
}
@Override
public VisibilityPolicy getControlPointVisibilityPolicy(int index) {
return VisibilityPolicy.WHEN_SELECTED;
}
@Override
public Point getControlPoint(int index) {
return leftTopCorner;