Ability to rotate (almost) all components using keyboard shortcuts and 'Transform Selection' menu

This commit is contained in:
Bane Stojkovic
2023-04-26 17:32:50 +02:00
parent 1cbdf505fe
commit ece6015a7f
9 changed files with 1374 additions and 1237 deletions
@@ -0,0 +1,116 @@
package org.diylc.common;
import java.awt.geom.AffineTransform;
import java.awt.geom.Point2D;
import java.lang.reflect.InvocationTargetException;
import java.util.List;
import org.apache.log4j.Logger;
import org.diylc.core.Angle;
import org.diylc.core.IDIYComponent;
import org.diylc.presenter.ComponentProcessor;
public class DefaultComponentTransformer implements IComponentTransformer {
private static final Logger LOG = Logger.getLogger(DefaultComponentTransformer.class);
@Override
public boolean canRotate(IDIYComponent<?> component) {
List<PropertyWrapper> properties =
ComponentProcessor.getInstance().extractProperties(component.getClass());
for (PropertyWrapper property : properties) {
if (property.getType().isAssignableFrom(Orientation.class)
|| property.getType().isAssignableFrom(OrientationHV.class)
|| property.getType().isAssignableFrom(Angle.class))
return true;
}
return false;
}
@Override
public boolean canMirror(IDIYComponent<?> component) {
return false;
}
@Override
public boolean mirroringChangesCircuit() {
return false;
}
@Override
public void rotate(IDIYComponent<?> component, Point2D center, int direction) {
boolean canRotate = false;
List<PropertyWrapper> properties =
ComponentProcessor.getInstance().extractProperties(component.getClass());
for (PropertyWrapper property : properties) {
try {
property.readFrom(component);
} catch (IllegalArgumentException | IllegalAccessException | InvocationTargetException
| SecurityException | NoSuchMethodException e) {
LOG.warn("Error while rotating component " + component.getName(), e);
}
try {
if (property.getType().isAssignableFrom(Orientation.class)) {
canRotate = true;
Orientation o = (Orientation) property.getValue();
int oValue = o.ordinal();
oValue += direction;
if (oValue < 0)
oValue = Orientation.values().length - 1;
if (oValue >= Orientation.values().length)
oValue = 0;
o = Orientation.values()[oValue];
property.setValue(o);
}
if (property.getType().isAssignableFrom(OrientationHV.class)) {
canRotate = true;
OrientationHV o = (OrientationHV) property.getValue();
int oValue = o.ordinal();
oValue += direction;
if (oValue < 0)
oValue = OrientationHV.values().length - 1;
if (oValue >= OrientationHV.values().length)
oValue = 0;
o = OrientationHV.values()[oValue];
property.setValue(o);
}
if (property.getType().isAssignableFrom(Angle.class)) {
canRotate = true;
Angle angle = (Angle) property.getValue();
Angle newAngle = angle.rotate(direction);
property.setValue(newAngle);
}
} catch (Exception e) {
LOG.warn("Error while rotating component " + component.getName(), e);
}
if (canRotate) {
AffineTransform rotate = AffineTransform.getRotateInstance(Math.PI / 2 * direction,
center.getX(), center.getY());
for (int index = 0; index < component.getControlPointCount(); index++) {
Point2D p = new Point2D.Double();
rotate.transform(component.getControlPoint(index), p);
component.setControlPoint(p, index);
}
try {
property.writeTo(component);
} catch (IllegalArgumentException | IllegalAccessException | InvocationTargetException
| SecurityException | NoSuchMethodException e) {
LOG.warn("Error while rotating component " + component.getName(), e);
}
break;
}
}
}
@Override
public void mirror(IDIYComponent<?> component, Point2D center, int direction) {}
}
@@ -1,52 +0,0 @@
/*
DIY Layout Creator (DIYLC).
Copyright (c) 2009-2018 held jointly by the individual authors.
This file is part of DIYLC.
DIYLC is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
DIYLC is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with DIYLC. If not, see <http://www.gnu.org/licenses/>.
*/
package org.diylc.common;
import java.awt.geom.Point2D;
import org.diylc.core.IDIYComponent;
public class DefaultTransformer implements IComponentTransformer {
@Override
public boolean canRotate(IDIYComponent<?> component) {
return false;
}
@Override
public boolean canMirror(IDIYComponent<?> component) {
return false;
}
@Override
public boolean mirroringChangesCircuit() {
return false;
}
@Override
public void rotate(IDIYComponent<?> component, Point2D center, int direction) {
}
@Override
public void mirror(IDIYComponent<?> component, Point2D center, int direction) {
}
}
@@ -23,7 +23,7 @@ package org.diylc.core.annotations;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import org.diylc.common.DefaultTransformer;
import org.diylc.common.DefaultComponentTransformer;
import org.diylc.common.IComponentTransformer;
import org.diylc.core.CreationMethod;
import org.diylc.core.IDIYComponent;
@@ -94,7 +94,7 @@ public @interface ComponentDescriptor {
/**
* @return if the component can be rotated and/or mirrored, returns a class of the transformer that can do it
*/
Class<? extends IComponentTransformer> transformer() default DefaultTransformer.class;
Class<? extends IComponentTransformer> transformer() default DefaultComponentTransformer.class;
/**
* Defines if and how a component should appear in auto-generated project keywords. See
@@ -4349,5 +4349,25 @@
</changes>
<url>https://github.com/bancika/diy-layout-creator/releases</url>
</org.diylc.appframework.update.Version>
<org.diylc.appframework.update.Version>
<versionNumber>
<major>4</major>
<minor>37</minor>
<build>0</build>
</versionNumber>
<releaseDate>2023-03-30 00:00:00.000 CET</releaseDate>
<name></name>
<changes>
<org.diylc.appframework.update.Change>
<changeType>NEW_FEATURE</changeType>
<description>'Proto Board 780 Holes' added with two variations (sponsored by Pete Olivier)</description>
</org.diylc.appframework.update.Change>
<org.diylc.appframework.update.Change>
<changeType>IMPROVEMENT</changeType>
<description>Ability to rotate (almost) all components using keyboard shortcuts and 'Transform Selection' menu</description>
</org.diylc.appframework.update.Change>
</changes>
<url>https://github.com/bancika/diy-layout-creator/releases</url>
</org.diylc.appframework.update.Version>
</a>
</java.util.Arrays_-ArrayList>