Skip to content

Commit

Permalink
Now only show the settings that are common between different entites
Browse files Browse the repository at this point in the history
  • Loading branch information
breiler committed Jun 17, 2024
1 parent da7d167 commit 458ab65
Show file tree
Hide file tree
Showing 6 changed files with 226 additions and 72 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ This file is part of Universal Gcode Sender (UGS).

import com.willwinder.ugs.nbp.designer.Utils;
import com.willwinder.ugs.nbp.designer.gui.Drawing;
import com.willwinder.ugs.nbp.designer.model.Size;

import java.awt.Graphics2D;
import java.awt.Shape;
Expand Down Expand Up @@ -54,16 +53,6 @@ public void render(Graphics2D graphics, Drawing drawing) {
children.forEach(node -> node.render(graphics, drawing));
}

@Override
public void setSize(Size size) {
if(size.getWidth() == 0 || size.getHeight() == 0) {
return;
}

Size originalSize = getSize();
scale(size.getWidth() / originalSize.getWidth(), size.getHeight() / originalSize.getHeight());
}

@Override
public void rotate(double angle) {
try {
Expand Down Expand Up @@ -94,12 +83,6 @@ public Shape getShape() {
return getBounds();
}

@Override
public Size getSize() {
Rectangle2D bounds = getBounds();
return new Size(bounds.getWidth(), bounds.getHeight());
}

@Override
public Rectangle2D getBounds() {
if (cachedBounds != null) {
Expand Down Expand Up @@ -143,12 +126,6 @@ private void invalidateBounds() {
cachedBounds = null;
}

@Override
public Point2D getCenter() {
Rectangle2D bounds = getBounds();
return new Point2D.Double(bounds.getCenterX(), bounds.getCenterY());
}

public void addAll(List<Entity> entities) {
entities.forEach(entity -> {
if (!containsChild(entity)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ 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.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.stream.Stream;
Expand Down Expand Up @@ -191,19 +192,44 @@ public Entity copy() {
return copy;
}

public Optional<Entity> getFirstChild() {
if (getChildren().isEmpty()) {
return Optional.empty();
@Override
public List<EntitySetting> getSettings() {
List<List<EntitySetting>> list = getCuttableStream().map(Entity::getSettings).toList();
if (list.isEmpty()) {
return List.of();
}

return Optional.of(getChildren().get(0));
}
List<EntitySetting> result = list.get(0);
for (List<EntitySetting> settings : list) {
result.retainAll(settings);
}

@Override
public List<EntitySetting> getSettings() {
return getCuttableStream()
.flatMap(cuttable -> cuttable.getSettings().stream())
.distinct()
.toList();
// Remove cut type if they are of differnt types
if (getCuttableStream().map(Cuttable::getCutType).distinct().toList().size() > 1) {
result = new ArrayList<>(result);
result.remove(EntitySetting.CUT_TYPE);
}

if (getCuttableStream().map(Cuttable::getStartDepth).distinct().toList().size() > 1) {
result = new ArrayList<>(result);
result.remove(EntitySetting.START_DEPTH);
}

if (getCuttableStream().map(Cuttable::getTargetDepth).distinct().toList().size() > 1) {
result = new ArrayList<>(result);
result.remove(EntitySetting.TARGET_DEPTH);
}

if (getCuttableStream().map(Cuttable::getSpindleSpeed).distinct().toList().size() > 1) {
result = new ArrayList<>(result);
result.remove(EntitySetting.SPINDLE_SPEED);
}

if (getCuttableStream().map(Cuttable::getFeedRate).distinct().toList().size() > 1) {
result = new ArrayList<>(result);
result.remove(EntitySetting.FEED_RATE);
}

return result;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ This file is part of Universal Gcode Sender (UGS).

import java.awt.Font;
import java.io.Serializable;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
Expand Down Expand Up @@ -288,25 +289,58 @@ public void setLockRatio(boolean lockRatio) {
}

public void updateFromEntity(Group selectionGroup) {
if (selectionGroup.getChildren().size() > 1) {
return;
}
boolean isTextCuttable = selectionGroup.getChildren().get(0) instanceof Text;
if (isTextCuttable) {
List<EntitySetting> settings = selectionGroup.getSettings();
if (settings.contains(EntitySetting.TEXT)) {
Text textEntity = (Text) selectionGroup.getChildren().get(0);
setText(textEntity.getText());
}

if (settings.contains(EntitySetting.FONT_FAMILY)) {
Text textEntity = (Text) selectionGroup.getChildren().get(0);
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());

if (settings.contains(EntitySetting.POSITION_X)) {
setPositionX(selectionGroup.getPosition(getAnchor()).getX());
}

if (settings.contains(EntitySetting.POSITION_X)) {
setPositionY(selectionGroup.getPosition(getAnchor()).getY());
}

if (settings.contains(EntitySetting.WIDTH)) {
setWidth(selectionGroup.getSize().getWidth());
}

if (settings.contains(EntitySetting.HEIGHT)) {
setHeight(selectionGroup.getSize().getHeight());
}

if (settings.contains(EntitySetting.ROTATION)) {
setRotation(selectionGroup.getRotation());
}

if (settings.contains(EntitySetting.START_DEPTH)) {
setStartDepth(selectionGroup.getStartDepth());
}
if (settings.contains(EntitySetting.TARGET_DEPTH)) {
setTargetDepth(selectionGroup.getTargetDepth());
}

if (settings.contains(EntitySetting.CUT_TYPE)) {
setCutType(selectionGroup.getCutType());
}

if (settings.contains(EntitySetting.SPINDLE_SPEED)) {
setSpindleSpeed(selectionGroup.getSpindleSpeed());
}

if (settings.contains(EntitySetting.PASSES)) {
setPasses(selectionGroup.getPasses());
}

if (settings.contains(EntitySetting.FEED_RATE)) {
setFeedRate(selectionGroup.getFeedRate());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ public class SelectionSettingsPanel extends JPanel implements SelectionListener,
private JSlider passesSlider;
private JLabel feedRateLabel;
private UnitSpinner feedRateSpinner;
private JLabel cutTypeLabel;

public SelectionSettingsPanel(Controller controller) {
fieldEventDispatcher = new FieldEventDispatcher();
Expand All @@ -97,10 +98,8 @@ public SelectionSettingsPanel(Controller controller) {
fieldEventDispatcher.addListener(fieldActionDispatcher);
}

private static Boolean firstChildHasSetting(Group selectionGroup, EntitySetting entitySetting) {
return selectionGroup.getFirstChild()
.map(firstChild -> firstChild.getSettings().contains(entitySetting))
.orElse(false);
private static Boolean selectionHasSetting(Group selectionGroup, EntitySetting entitySetting) {
return selectionGroup.getSettings().contains(entitySetting);
}

private void addPositionFields() {
Expand Down Expand Up @@ -146,7 +145,7 @@ private void addCutFields() {
cutTypeComboBox = new CutTypeCombo();
fieldEventDispatcher.registerListener(EntitySetting.CUT_TYPE, cutTypeComboBox);

createAndAddLabel(EntitySetting.CUT_TYPE);
cutTypeLabel = createAndAddLabel(EntitySetting.CUT_TYPE);
add(cutTypeComboBox, FIELD_CONSTRAINTS + ", spanx");

feedRateLabel = createAndAddLabel(EntitySetting.FEED_RATE);
Expand Down Expand Up @@ -319,50 +318,59 @@ public void onModelUpdate(EntitySetting entitySetting) {
private void handleComponentVisibility(Group selectionGroup) {
CutType cutType = selectionGroup.getCutType();

boolean hasCutType = selectionHasSetting(selectionGroup, EntitySetting.CUT_TYPE);
cutTypeComboBox.setVisible(hasCutType);
cutTypeLabel.setVisible(hasCutType);

final boolean hasCutTypeSelection = cutType != CutType.NONE;
startDepthSpinner.setEnabled(hasCutTypeSelection);
startDepthLabel.setEnabled(hasCutTypeSelection);
targetDepthSpinner.setEnabled(hasCutTypeSelection);
targetDepthLabel.setEnabled(hasCutTypeSelection);

boolean isTextCuttable = firstChildHasSetting(selectionGroup, EntitySetting.TEXT);
boolean isTextCuttable = selectionHasSetting(selectionGroup, EntitySetting.TEXT);
textTextField.setVisible(isTextCuttable);
textLabel.setVisible(isTextCuttable);
fontLabel.setVisible(isTextCuttable);
fontDropDown.setVisible(isTextCuttable);
fontSeparator.setVisible(isTextCuttable);

boolean hasWidth = firstChildHasSetting(selectionGroup, EntitySetting.WIDTH);
boolean hasWidth = selectionHasSetting(selectionGroup, EntitySetting.WIDTH);
widthLabel.setVisible(hasWidth);
widthTextField.setVisible(hasWidth);

boolean hasHeight = firstChildHasSetting(selectionGroup, EntitySetting.HEIGHT);
boolean hasHeight = selectionHasSetting(selectionGroup, EntitySetting.HEIGHT);
heightLabel.setVisible(hasHeight);
heightTextField.setVisible(hasHeight);

boolean hasAnchor = firstChildHasSetting(selectionGroup, EntitySetting.ANCHOR);
boolean hasAnchor = selectionHasSetting(selectionGroup, EntitySetting.ANCHOR);
anchorSelector.setVisible(hasAnchor);

boolean hasStartDepth = cutType.getSettings().contains(EntitySetting.START_DEPTH);
boolean hasStartDepth = selectionHasSetting(selectionGroup, EntitySetting.START_DEPTH) &&
cutType.getSettings().contains(EntitySetting.START_DEPTH);
startDepthSpinner.setVisible(hasStartDepth);
startDepthLabel.setVisible(hasStartDepth);

boolean hasTargetDepth = cutType.getSettings().contains(EntitySetting.TARGET_DEPTH);
boolean hasTargetDepth = selectionHasSetting(selectionGroup, EntitySetting.TARGET_DEPTH) &&
cutType.getSettings().contains(EntitySetting.TARGET_DEPTH);
targetDepthSpinner.setVisible(hasTargetDepth);
targetDepthLabel.setVisible(hasTargetDepth);

boolean hasLaserPower = cutType.getSettings().contains(EntitySetting.SPINDLE_SPEED);
boolean hasLaserPower = selectionHasSetting(selectionGroup, EntitySetting.SPINDLE_SPEED) &&
cutType.getSettings().contains(EntitySetting.SPINDLE_SPEED);
spindleSpeedLabel.setText(cutType == CutType.LASER_FILL || cutType == CutType.LASER_ON_PATH ? "Power" : EntitySetting.SPINDLE_SPEED.getLabel());
spindleSpeedLabel.setVisible(hasLaserPower);
spindleSpeedSlider.setVisible(hasLaserPower);

boolean hasLaserPasses = cutType.getSettings().contains(EntitySetting.PASSES);
boolean hasLaserPasses = selectionHasSetting(selectionGroup, EntitySetting.PASSES) &&
cutType.getSettings().contains(EntitySetting.PASSES);
laserPassesLabel.setVisible(hasLaserPasses);
passesSlider.setVisible(hasLaserPasses);

boolean hasLaserFeedRate = cutType.getSettings().contains(EntitySetting.FEED_RATE);
feedRateLabel.setVisible(hasLaserFeedRate);
feedRateSpinner.setVisible(hasLaserFeedRate);
boolean hasFeedRate = selectionHasSetting(selectionGroup, EntitySetting.FEED_RATE) &&
cutType.getSettings().contains(EntitySetting.FEED_RATE);
feedRateLabel.setVisible(hasFeedRate);
feedRateSpinner.setVisible(hasFeedRate);

lockRatioButton.setVisible(hasWidth && hasHeight);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,15 @@
import com.willwinder.ugs.nbp.designer.entities.cuttable.Point;
import com.willwinder.ugs.nbp.designer.entities.cuttable.Rectangle;
import com.willwinder.ugs.nbp.designer.model.Size;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import org.junit.Test;

import java.awt.geom.Point2D;
import java.util.List;
import java.util.Optional;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;

public class EntityGroupTest {

@Test
Expand Down Expand Up @@ -344,4 +343,22 @@ public void onEventShouldUpdateBounds() {
assertEquals(15, entityGroup.getBounds().getWidth(), 0.1);
assertEquals(15, entityGroup.getBounds().getHeight(), 0.1);
}

@Test
public void setSizeShouldChangeAllChildEntites() {
EntityGroup entityGroup = new EntityGroup();

Rectangle rectangle1 = new Rectangle(0, 0);
rectangle1.setSize(new Size(10, 10));
entityGroup.addChild(rectangle1);

Rectangle rectangle2 = new Rectangle(10, 0);
rectangle2.setSize(new Size(10, 10));
entityGroup.addChild(rectangle2);

entityGroup.setSize(new Size(10, 5));
assertEquals(new Size(10, 5), entityGroup.getSize());
assertEquals(new Size(5, 5), rectangle1.getSize());
assertEquals(new Size(5, 5), rectangle2.getSize());
}
}
Loading

0 comments on commit 458ab65

Please sign in to comment.