[#2102] Add "Select components of same color" option

This commit is contained in:
SiboVG 2023-03-20 22:44:23 +01:00
parent 98fb726c4b
commit daf6189c8f
3 changed files with 108 additions and 0 deletions

View File

@ -39,6 +39,9 @@ RocketActions.DuplicateAct.Duplicate = Duplicate
RocketActions.DuplicateAct.ttip.Duplicate = Duplicate this component (and subcomponents).
RocketActions.EditAct.Edit = Edit
RocketActions.EditAct.ttip.Edit = Edit the selected component.
RocketActions.Select = Select
RocketActions.Select.SelectSameColorAct = Components with same color
RocketActions.Select.SelectSameColorAct.ttip = Select all components with the same color as this component.
RocketActions.ScaleAct.Scale = Scale
RocketActions.ScaleAct.ttip.Scale = Scale parts of the rocket design
RocketActions.NewStageAct.Newstage = New stage

View File

@ -236,6 +236,12 @@ public class BasicFrame extends JFrame {
popupMenu.add(actions.getPasteAction());
popupMenu.add(actions.getDuplicateAction());
popupMenu.add(actions.getDeleteAction());
popupMenu.addSeparator();
JMenu selectMenu = new JMenu(trans.get("RocketActions.Select"));
selectMenu.add(actions.getSelectSameColorAction());
popupMenu.add(selectMenu);
popupMenu.addSeparator();
popupMenu.add(actions.getScaleAction());
}
@ -656,6 +662,13 @@ public class BasicFrame extends JFrame {
menu.addSeparator();
JMenu subMenu = new JMenu(trans.get("RocketActions.Select"));
item = new JMenuItem(actions.getSelectSameColorAction());
subMenu.add(item);
menu.add(subMenu);
menu.addSeparator();
item = new JMenuItem(actions.getScaleAction());
menu.add(item);

View File

@ -35,6 +35,7 @@ import net.sf.openrocket.rocketcomponent.RocketComponent;
import net.sf.openrocket.rocketcomponent.AxialStage;
import net.sf.openrocket.startup.Application;
import net.sf.openrocket.startup.Preferences;
import net.sf.openrocket.util.Color;
import net.sf.openrocket.util.Pair;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@ -74,6 +75,7 @@ public class RocketActions {
private final RocketAction pasteAction;
private final RocketAction duplicateAction;
private final RocketAction editAction;
private final RocketAction selectSameColorAction;
private final RocketAction scaleAction;
private final RocketAction moveUpAction;
private final RocketAction moveDownAction;
@ -98,6 +100,7 @@ public class RocketActions {
this.pasteAction = new PasteAction();
this.duplicateAction = new DuplicateAction();
this.editAction = new EditAction();
this.selectSameColorAction = new SelectSameColor();
this.scaleAction = new ScaleAction();
this.moveUpAction = new MoveUpAction();
this.moveDownAction = new MoveDownAction();
@ -131,6 +134,7 @@ public class RocketActions {
pasteAction.clipboardChanged();
duplicateAction.clipboardChanged();
editAction.clipboardChanged();
selectSameColorAction.clipboardChanged();
scaleAction.clipboardChanged();
moveUpAction.clipboardChanged();
moveDownAction.clipboardChanged();
@ -171,6 +175,10 @@ public class RocketActions {
return editAction;
}
public Action getSelectSameColorAction() {
return selectSameColorAction;
}
public Action getScaleAction() {
return scaleAction;
}
@ -1006,6 +1014,90 @@ public class RocketActions {
}
}
/**
* Action to select all components with the same color as the currently selected component.
*/
private class SelectSameColor extends RocketAction {
private static final long serialVersionUID = 1L;
public SelectSameColor() {
//// Select same color
this.putValue(NAME, trans.get("RocketActions.Select.SelectSameColorAct"));
this.putValue(SHORT_DESCRIPTION, trans.get("RocketActions.Select.SelectSameColorAct.ttip"));
clipboardChanged();
}
@Override
public void actionPerformed(ActionEvent e) {
List<RocketComponent> components = selectionModel.getSelectedComponents();
if (components.size() == 0) {
return;
}
RocketComponent component = components.get(0);
List<RocketComponent> sameColorComponents;
// Case 1: component has a default appearance (null)
if (component.getAppearance() == null) {
sameColorComponents = getComponentsDefaultColor(component);
}
// Case 2: component has a custom appearance
else {
sameColorComponents = getComponentsCustomColor(component);
}
selectionModel.setSelectedComponents(sameColorComponents);
}
private List<RocketComponent> getComponentsCustomColor(RocketComponent component) {
Color targetColor = component.getAppearance().getPaint();
List<RocketComponent> components = new ArrayList<>();
components.add(component);
for (RocketComponent c : rocket) {
if (c == component || c.getAppearance() == null || c.getAppearance().getPaint() == null) {
continue;
}
Color color = c.getAppearance().getPaint();
// Add components with the same RGB values (ignore alpha)
if (color.getRed() == targetColor.getRed() &&
color.getGreen() == targetColor.getGreen() &&
color.getBlue() == targetColor.getBlue()) {
components.add(c);
}
}
return components;
}
private List<RocketComponent> getComponentsDefaultColor(RocketComponent component) {
List<RocketComponent> components = new ArrayList<>();
components.add(component);
for (RocketComponent c : rocket) {
if (c == component) {
continue;
}
// Only add same components & components that also have the default color
if (c.getClass().equals(component.getClass()) && c.getAppearance() == null) {
components.add(c);
}
}
return components;
}
@Override
public void clipboardChanged() {
List<RocketComponent> components = selectionModel.getSelectedComponents();
this.setEnabled(components.size() > 0);
}
}
/**
* Action to scale the currently selected component.
*/