Skip to content

Commit

Permalink
Fixed selection bug overwriting when selecting in the entities tree
Browse files Browse the repository at this point in the history
Made an attempt to fix the entities tree selection bug where it would overwrite the settings when selecting multiple entities. The design settings has now been moved from the design file to the platform settings.
  • Loading branch information
breiler authored May 27, 2024
1 parent f30af36 commit 431203d
Show file tree
Hide file tree
Showing 22 changed files with 341 additions and 260 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,7 @@ public boolean isWithin(Point2D point) {

@Override
public void onEvent(EntityEvent entityEvent) {
if (entityEvent instanceof MouseEntityEvent) {
MouseEntityEvent mouseEntityEvent = (MouseEntityEvent) entityEvent;
if (entityEvent instanceof MouseEntityEvent mouseEntityEvent) {
Point2D mousePosition = mouseEntityEvent.getCurrentMousePosition();

if (mouseEntityEvent.getType() == EventType.MOUSE_PRESSED) {
Expand Down Expand Up @@ -109,12 +108,11 @@ private void selectIntersection(Shape shape, boolean selectMultiple) {
.filter(e -> e != this)
.filter(e -> !(e instanceof Control))
.filter(e -> !(e instanceof Cuttable && ((Cuttable) e).isHidden()))
.filter(e -> e != controller.getSelectionManager())
.collect(Collectors.toSet());

if (selectMultiple) {
if (!entitiesIntersecting.isEmpty()) {
entitiesIntersecting.forEach(e -> controller.getSelectionManager().toggleSelection(e));
}
controller.getSelectionManager().toggleSelection(entitiesIntersecting);
} else {
controller.getSelectionManager().setSelection(new ArrayList<>(entitiesIntersecting));
}
Expand All @@ -127,6 +125,7 @@ private void selectOne(Point2D mousePosition, boolean selectMultiple) {
.filter(e -> e != this)
.filter(e -> !(e instanceof Control))
.filter(e -> !(e instanceof Cuttable && ((Cuttable) e).isHidden()))
.filter(e -> e != controller.getSelectionManager())
.collect(Collectors.toSet());

if (selectMultiple) {
Expand All @@ -136,7 +135,7 @@ private void selectOne(Point2D mousePosition, boolean selectMultiple) {
.sorted(Comparator.comparingDouble(e -> e.getBounds().getWidth() * e.getBounds().getHeight()))
.limit(1)
.filter(e -> !controller.getSelectionManager().isSelected(e))
.collect(Collectors.toList());
.toList();
controller.getSelectionManager().setSelection(selection);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ This file is part of Universal Gcode Sender (UGS).
import com.willwinder.ugs.nbp.designer.entities.EntityGroup;
import com.willwinder.ugs.nbp.designer.entities.EntitySetting;

import java.util.Arrays;
import java.util.List;
import java.util.Optional;
import java.util.stream.Stream;
Expand Down Expand Up @@ -202,18 +201,9 @@ public Optional<Entity> getFirstChild() {

@Override
public List<EntitySetting> getSettings() {
return Arrays.asList(
EntitySetting.ANCHOR,
EntitySetting.POSITION_X,
EntitySetting.POSITION_Y,
EntitySetting.WIDTH,
EntitySetting.HEIGHT,
EntitySetting.CUT_TYPE,
EntitySetting.START_DEPTH,
EntitySetting.TARGET_DEPTH,
EntitySetting.SPINDLE_SPEED,
EntitySetting.PASSES,
EntitySetting.FEED_RATE
);
return getCuttableStream()
.flatMap(cuttable -> cuttable.getSettings().stream())
.distinct()
.toList();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,14 @@ public void removeSelection(Entity entity) {
fireSelectionEvent(new SelectionEvent());
}

public void removeSelection(List<Entity> nodes) {
nodes.forEach(e -> {
entityGroup.removeChild(e);
e.removeListener(this);
});
fireSelectionEvent(new SelectionEvent());
}

public void addSelectionListener(SelectionListener selectionListener) {
this.listeners.add(selectionListener);
}
Expand Down Expand Up @@ -225,6 +233,15 @@ public void toggleSelection(Entity entity) {
}
}

public void toggleSelection(Set<Entity> entitiesIntersecting) {
entitiesIntersecting.stream().filter(c -> entityGroup.getChildren().contains(c)).forEach(e -> {
entityGroup.removeChild(e);
e.removeListener(this);
});
entitiesIntersecting.stream().filter(c -> !entityGroup.getChildren().contains(c)).forEach(entityGroup::addChild);
fireSelectionEvent(new SelectionEvent());
}

@Override
public void onEvent(EntityEvent entityEvent) {
notifyEvent(entityEvent);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
Copyright 2021 Will Winder
Copyright 2021-2024 Will Winder
This file is part of Universal Gcode Sender (UGS).
Expand Down Expand Up @@ -33,10 +33,10 @@ This file is part of Universal Gcode Sender (UGS).
* centered.
*
* @author Alex Lagerstedt
* @author Joacim Breiler
*/
public class DrawingContainer extends JPanel implements ComponentListener, MouseWheelListener {

private static final long serialVersionUID = 0;
private final transient Controller controller;
private JScrollPane scrollPane;
private JPanel buttonPanel;
Expand Down Expand Up @@ -86,10 +86,9 @@ protected void processMouseWheelEvent(MouseWheelEvent e) {
buttonPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT));
buttonPanel.setOpaque(false);

PanelButton toolButton = new PanelButton("Tool", controller.getSettings().getToolDescription());
ToolButton toolButton = new ToolButton(controller);
toolButton.setMinimumSize(new Dimension(60, 40));
toolButton.setMaximumSize(new Dimension(100, 40));
controller.getSettings().addListener(() -> toolButton.setText(controller.getSettings().getToolDescription()));
toolButton.addActionListener(new OpenToolSettingsAction(controller));
buttonPanel.add(toolButton);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,17 +29,18 @@ public class PanelButton extends JButton {
public static final float TITLE_SCALE = 0.6f;
public static final float TEXT_SCALE = 1.0f;
private final JLabel textLabel;
private final JLabel titleLabel;

public PanelButton(String title, String text) {
super();
setLayout(new BorderLayout());
setMaximumSize(new Dimension(100, 100));
JLabel titleLabel = new JLabel(title);
titleLabel = new JLabel(title);
Font font = titleLabel.getFont();
font = font.deriveFont(font.getSize() * TITLE_SCALE);
titleLabel.setFont(font);

add(titleLabel, BorderLayout.NORTH);

textLabel = new JLabel(text);
font = textLabel.getFont();
font = font.deriveFont(font.getSize() * TEXT_SCALE);
Expand All @@ -51,4 +52,8 @@ public PanelButton(String title, String text) {
public void setText(String text) {
this.textLabel.setText(text);
}

public void setTitle(String title) {
this.titleLabel.setText(title);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
/*
Copyright 2024 Will Winder
This file is part of Universal Gcode Sender (UGS).
UGS 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.
UGS 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 UGS. If not, see <http://www.gnu.org/licenses/>.
*/
package com.willwinder.ugs.nbp.designer.gui;

import com.willwinder.ugs.nbp.designer.entities.cuttable.CutType;
import com.willwinder.ugs.nbp.designer.entities.cuttable.Cuttable;
import com.willwinder.ugs.nbp.designer.logic.Controller;
import com.willwinder.universalgcodesender.Utils;
import com.willwinder.universalgcodesender.model.UnitUtils;

/**
* A button that displays the currently used tool in the design
*
* @author Joacim Breiler
*/
public class ToolButton extends PanelButton {
private final transient Controller controller;

public ToolButton(Controller controller) {
super("", "");
this.controller = controller;
controller.getSettings().addListener(this::updateText);
controller.getDrawing().addListener(e -> updateText());
controller.getDrawing().getRootEntity().addListener(e -> updateText());
updateText();
}

private static boolean isMillOperation(Cuttable c) {
return c.getCutType() == CutType.ON_PATH || c.getCutType() == CutType.CENTER_DRILL || c.getCutType() == CutType.INSIDE_PATH || c.getCutType() == CutType.OUTSIDE_PATH || c.getCutType() == CutType.POCKET;
}

private static boolean isLaserOperation(Cuttable c) {
return c.getCutType() == CutType.LASER_FILL || c.getCutType() == CutType.LASER_ON_PATH;
}

private void updateText() {
setTitle("Tool");
boolean hasLaserOperations = controller.getDrawing().getEntities().stream()
.filter(Cuttable.class::isInstance)
.map(e -> (Cuttable) e)
.anyMatch(ToolButton::isLaserOperation);

boolean hasMillOperations = controller.getDrawing().getEntities().stream()
.filter(Cuttable.class::isInstance)
.map(e -> (Cuttable) e)
.anyMatch(ToolButton::isMillOperation);


boolean hasMixedOperations = hasLaserOperations && hasMillOperations;

if (hasMixedOperations) {
setText("Mixed");
} else if (hasLaserOperations) {
setText("Laser");
} else {
setText(getMillToolDescription());
}
}

public String getMillToolDescription() {
double scale = UnitUtils.scaleUnits(UnitUtils.Units.MM, controller.getSettings().getPreferredUnits());
return Utils.formatter.format(controller.getSettings().getToolDiameter() * scale) + " " + controller.getSettings().getPreferredUnits().abbreviation;
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
Copyright 2021 Will Winder
Copyright 2021-2024 Will Winder
This file is part of Universal Gcode Sender (UGS).
Expand Down Expand Up @@ -37,6 +37,7 @@ This file is part of Universal Gcode Sender (UGS).
* @author Joacim Breiler
*/
public class ToolSettingsPanel extends JPanel {
public static final String TOOL_FIELD_CONSTRAINT = "grow, wrap";
private final transient Controller controller;
private JTextField toolDiameter;
private JTextField feedSpeed;
Expand All @@ -60,44 +61,43 @@ private void initComponents() {

add(new JLabel("Tool diameter" ));
toolDiameter = new TextFieldWithUnit(TextFieldUnit.MM, 3, controller.getSettings().getToolDiameter());
add(toolDiameter, "grow, wrap" );
add(toolDiameter, TOOL_FIELD_CONSTRAINT);

add(new JLabel("Feed speed" ));
feedSpeed = new TextFieldWithUnit(TextFieldUnit.MM_PER_MINUTE, 0, controller.getSettings().getFeedSpeed());
add(feedSpeed, "grow, wrap" );
add(feedSpeed, TOOL_FIELD_CONSTRAINT);

add(new JLabel("Plunge speed" ));
plungeSpeed = new TextFieldWithUnit(TextFieldUnit.MM_PER_MINUTE, 0, controller.getSettings().getPlungeSpeed());
add(plungeSpeed, "grow, wrap" );
add(plungeSpeed, TOOL_FIELD_CONSTRAINT);

add(new JLabel("Depth per pass" ));
depthPerPass = new TextFieldWithUnit(TextFieldUnit.MM, 2, controller.getSettings().getDepthPerPass());
add(depthPerPass, "grow, wrap" );
add(depthPerPass, TOOL_FIELD_CONSTRAINT);

add(new JLabel("Step over" ));
stepOver = new TextFieldWithUnit(TextFieldUnit.PERCENT, 2,
controller.getSettings().getToolStepOver());
add(stepOver, "grow, wrap" );
add(stepOver, TOOL_FIELD_CONSTRAINT);

add(new JLabel("Safe height" ));
safeHeight = new TextFieldWithUnit(TextFieldUnit.MM, 2, controller.getSettings().getSafeHeight());
add(safeHeight, "grow, wrap" );
add(safeHeight, TOOL_FIELD_CONSTRAINT);

add(new JSeparator(SwingConstants.HORIZONTAL), "spanx, grow, wrap" );
add(new JSeparator(SwingConstants.HORIZONTAL), "spanx, grow, wrap, hmin 2" );
add(new JLabel("Spindle speed" ));
spindleSpeed = new TextFieldWithUnit(TextFieldUnit.ROTATIONS_PER_MINUTE, 0, controller.getSettings().getSpindleSpeed());
add(spindleSpeed, "grow, wrap" );
add(spindleSpeed, TOOL_FIELD_CONSTRAINT);

add(new JSeparator(SwingConstants.HORIZONTAL), "spanx, grow, wrap" );
add(new JLabel("Max spindle speed" ));
maxSpindleSpeed = new TextFieldWithUnit(TextFieldUnit.ROTATIONS_PER_MINUTE, 0, controller.getSettings().getMaxSpindleSpeed());
add(maxSpindleSpeed, "grow, wrap" );
add(maxSpindleSpeed, TOOL_FIELD_CONSTRAINT);

add(new JSeparator(SwingConstants.HORIZONTAL), "spanx, grow, wrap" );
add(new JSeparator(SwingConstants.HORIZONTAL), "spanx, grow, wrap, hmin 2" );

add(new JLabel("Laser diameter" ));
laserDiameter = new TextFieldWithUnit(TextFieldUnit.MM, 3, controller.getSettings().getLaserDiameter());
add(laserDiameter, "grow, wrap" );
add(laserDiameter, TOOL_FIELD_CONSTRAINT);
}

public double getToolDiameter() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ This file is part of Universal Gcode Sender (UGS).
import com.willwinder.ugs.nbp.designer.entities.Anchor;
import com.willwinder.ugs.nbp.designer.entities.EntitySetting;
import com.willwinder.ugs.nbp.designer.entities.cuttable.CutType;
import com.willwinder.ugs.nbp.designer.entities.cuttable.Group;
import com.willwinder.ugs.nbp.designer.entities.cuttable.Text;

import java.awt.Font;
import java.io.Serializable;
Expand Down Expand Up @@ -268,4 +270,27 @@ public void setLockRatio(boolean lockRatio) {
notifyListeners(EntitySetting.LOCK_RATIO);
}
}

public void updateFromEntity(Group selectionGroup) {
if(selectionGroup.getChildren().size() > 1) {
return;
}
boolean isTextCuttable = selectionGroup.getChildren().get(0) instanceof Text;
if (isTextCuttable) {
Text textEntity = (Text) selectionGroup.getChildren().get(0);
setText(textEntity.getText());
setFontFamily(textEntity.getFontFamily());
}
setPositionX(selectionGroup.getPosition(getAnchor()).getX());
setPositionY(selectionGroup.getPosition(getAnchor()).getY());
setWidth(selectionGroup.getSize().getWidth());
setHeight(selectionGroup.getSize().getHeight());
setRotation(selectionGroup.getRotation());
setStartDepth(selectionGroup.getStartDepth());
setTargetDepth(selectionGroup.getTargetDepth());
setCutType(selectionGroup.getCutType());
setSpindleSpeed(selectionGroup.getSpindleSpeed());
setPasses(selectionGroup.getPasses());
setFeedRate(selectionGroup.getFeedRate());
}
}
Loading

0 comments on commit 431203d

Please sign in to comment.