Show and hide each individual layer

This commit is contained in:
bancika
2017-04-21 11:55:40 +02:00
parent f6ad118044
commit e342ed1c2c
6 changed files with 93 additions and 13 deletions
@@ -72,6 +72,11 @@ public enum EventType {
* layers. All layers not included are considered unlocked.
*/
LAYER_STATE_CHANGED,
/**
* Called when a layer visibility is changed. The first parameter is a set of indices of hidden
* layers. All layers not included are considered visible.
*/
LAYER_VISIBILITY_CHANGED,
/**
* Called to update the status message. New status message is passed as a first parameter.
*/
@@ -253,6 +253,15 @@ public interface IPlugInPort extends ISelectionProcessor, IMouseProcessor, IKeyP
* @param locked
*/
void setLayerLocked(int layerZOrder, boolean locked);
/**
* Shows or hides the specified layer. All components within +- 0.5 range will be affected by
* the change as well.
*
* @param layerZOrder
* @param visible
*/
void setLayerVisibility(int layerZOrder, boolean visible);
/**
* @return selection size expressed in both inches or centimeters, respectively
@@ -45,12 +45,14 @@ public class Project implements Serializable {
private List<IDIYComponent<?>> components;
private Set<Set<IDIYComponent<?>>> groups;
private Set<Integer> lockedLayers;
private Set<Integer> hiddenLayers;
private Font font = DEFAULT_FONT;
public Project() {
components = new ArrayList<IDIYComponent<?>>();
groups = new HashSet<Set<IDIYComponent<?>>>();
lockedLayers = new HashSet<Integer>();
hiddenLayers = new HashSet<Integer>();
title = DEFAULT_TITLE;
author = System.getProperty("user.name");
width = DEFAULT_WIDTH;
@@ -134,6 +136,12 @@ public class Project implements Serializable {
public Set<Integer> getLockedLayers() {
return lockedLayers;
}
public Set<Integer> getHiddenLayers() {
if (hiddenLayers == null)
hiddenLayers = new HashSet<Integer>();
return hiddenLayers;
}
public VersionNumber getFileVersion() {
return fileVersion;
@@ -261,6 +269,7 @@ public class Project implements Serializable {
project.setHeight(this.getHeight());
project.setWidth(this.getWidth());
project.getLockedLayers().addAll(this.getLockedLayers());
project.getHiddenLayers().addAll(this.getHiddenLayers());
Map<IDIYComponent<?>, IDIYComponent<?>> cloneMap = new HashMap<IDIYComponent<?>, IDIYComponent<?>>();
@@ -189,7 +189,7 @@ public class Presenter implements IPlugInPort {
return Cursor.getPredefinedCursor(Cursor.HAND_CURSOR);
}
for (IDIYComponent<?> component : currentProject.getComponents()) {
if (!isComponentLocked(component)) {
if (!isComponentLocked(component) && isComponentVisible(component)) {
Area area = drawingManager.getComponentArea(component);
if (area != null && area.contains(scaledPoint)) {
return Cursor.getPredefinedCursor(Cursor.HAND_CURSOR);
@@ -219,6 +219,7 @@ public class Presenter implements IPlugInPort {
messageDispatcher.dispatchMessage(EventType.PROJECT_LOADED, project, freshStart);
messageDispatcher.dispatchMessage(EventType.REPAINT);
messageDispatcher.dispatchMessage(EventType.LAYER_STATE_CHANGED, currentProject.getLockedLayers());
messageDispatcher.dispatchMessage(EventType.LAYER_VISIBILITY_CHANGED, currentProject.getHiddenLayers());
}
@Override
@@ -374,9 +375,16 @@ public class Presenter implements IPlugInPort {
return autoCreators;
}
@SuppressWarnings({"unchecked"})
private boolean isComponentVisible(IDIYComponent<?> component) {
ComponentType componentType =
ComponentProcessor.getInstance().extractComponentTypeFrom(
(Class<? extends IDIYComponent<?>>) component.getClass());
return !currentProject.getHiddenLayers().contains((int) Math.round(componentType.getZOrder()));
}
@Override
public void draw(Graphics2D g2d, Set<DrawOption> drawOptions, IComponentFiler filter) {
public void draw(Graphics2D g2d, Set<DrawOption> drawOptions, final IComponentFiler filter) {
if (currentProject == null) {
return;
}
@@ -387,6 +395,16 @@ public class Presenter implements IPlugInPort {
groupedComponents.add(component);
}
}
// Concatenate the specified filter with our own filter that removes hidden layers
IComponentFiler newFiler = new IComponentFiler() {
@Override
public boolean testComponent(IDIYComponent<?> component) {
return (filter == null || filter.testComponent(component)) && isComponentVisible(component);
}
};
// Don't draw the component in the slot if both control points
// match.
List<IDIYComponent<?>> componentSlotToDraw;
@@ -402,7 +420,7 @@ public class Presenter implements IPlugInPort {
g2d,
currentProject,
drawOptions,
filter,
newFiler,
selectionRect,
selectedComponents,
getLockedComponents(),
@@ -433,7 +451,8 @@ public class Presenter implements IPlugInPort {
List<IDIYComponent<?>> components = drawingManager.findComponentsAt(point, currentProject);
Iterator<IDIYComponent<?>> iterator = components.iterator();
while (iterator.hasNext()) {
if (isComponentLocked(iterator.next())) {
IDIYComponent<?> component = iterator.next();
if (isComponentLocked(component) || !isComponentVisible(component)) {
iterator.remove();
}
}
@@ -530,8 +549,8 @@ public class Presenter implements IPlugInPort {
List<IDIYComponent<?>> newSelection = new ArrayList<IDIYComponent<?>>();
for (IDIYComponent<?> component : componentSlot) {
addComponent(component, true);
// Select the new component if it's not locked.
if (!isComponentLocked(component)) {
// Select the new component if it's not locked and invisible.
if (!isComponentLocked(component) && isComponentVisible(component)) {
newSelection.add(component);
}
}
@@ -933,7 +952,7 @@ public class Presenter implements IPlugInPort {
// Do not process a control point if it's already in the map and
// if it's locked.
if ((!controlPointMap.containsKey(component) || !controlPointMap.get(component).contains(i))
&& !isComponentLocked(component)) {
&& !isComponentLocked(component) && isComponentVisible(component)) {
if (component.isControlPointSticky(i)) {
boolean componentMatches = false;
for (Map.Entry<IDIYComponent<?>, Set<Integer>> entry : controlPointMap.entrySet()) {
@@ -1315,7 +1334,7 @@ public class Presenter implements IPlugInPort {
}
List<IDIYComponent<?>> newSelection = new ArrayList<IDIYComponent<?>>();
for (IDIYComponent<?> component : currentProject.getComponents()) {
if (!isComponentLocked(component)) {
if (!isComponentLocked(component) && isComponentVisible(component)) {
Area area = drawingManager.getComponentArea(component);
if ((area != null) && (selectionRect != null) && area.intersects(selectionRect)) {
newSelection.addAll(findAllGroupedComponents(component));
@@ -1438,6 +1457,19 @@ public class Presenter implements IPlugInPort {
messageDispatcher.dispatchMessage(EventType.LAYER_STATE_CHANGED, currentProject.getLockedLayers());
}
@Override
public void setLayerVisibility(int layerZOrder, boolean visible) {
LOG.info(String.format("setLayerVisibility(%s, %s)", layerZOrder, visible));
if (visible) {
currentProject.getHiddenLayers().remove(layerZOrder);
} else {
currentProject.getHiddenLayers().add(layerZOrder);
}
updateSelection(EMPTY_SELECTION);
messageDispatcher.dispatchMessage(EventType.REPAINT);
messageDispatcher.dispatchMessage(EventType.LAYER_VISIBILITY_CHANGED, currentProject.getHiddenLayers());
}
@SuppressWarnings("unchecked")
@Override
public void sendSelectionToBack() {
@@ -1711,7 +1743,7 @@ public class Presenter implements IPlugInPort {
if (selectedComponents.isEmpty()) {
return null;
}
Rectangle2D rect = getSelectionBounds();
Rectangle2D rect = getSelectionBounds();
double width = rect.getWidth();
double height = rect.getHeight();