Skip to content

Commit

Permalink
Fixes to the designer (#2414)
Browse files Browse the repository at this point in the history
* Now only does one pass if the start and target depth are the same
* Added undoable actions for the shape settings
  • Loading branch information
breiler authored Jan 2, 2024
1 parent faa684c commit 1f5b917
Show file tree
Hide file tree
Showing 19 changed files with 477 additions and 59 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
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.actions;

import com.willwinder.ugs.nbp.designer.entities.cuttable.Text;

public class ChangeFontAction implements UndoableAction {

private final Text entity;
private final String newFont;
private final String oldFont;

public ChangeFontAction(Text entity, String font) {
this.entity = entity;
this.newFont = font;
this.oldFont = entity.getFontFamily();
}

@Override
public void redo() {
entity.setFontFamily(newFont);
}

@Override
public void undo() {
entity.setFontFamily(oldFont);
}

@Override
public String toString() {
return "changed font";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
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.actions;

import com.willwinder.ugs.nbp.designer.entities.cuttable.Text;

public class ChangeTextAction implements UndoableAction {

private final Text entity;
private final String newText;
private final String oldText;

public ChangeTextAction(Text entity, String text) {
this.entity = entity;
this.newText = text;
this.oldText = entity.getText();
}

@Override
public void redo() {
entity.setText(newText);
}

@Override
public void undo() {
entity.setText(oldText);
}

@Override
public String toString() {
return "changed text";
}
}
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 All @@ -21,6 +21,7 @@ This file is part of Universal Gcode Sender (UGS).
import com.willwinder.ugs.nbp.designer.entities.Entity;

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

/**
Expand All @@ -40,12 +41,12 @@ public class MoveAction implements DrawAction, UndoableAction {
* original position.
*
* @param entityList a selection which contains the shapes to be moved
* @param m the amount the shapes should be moved, relative to the
* @param deltaMovement the amount the shapes should be moved, relative to the
* original position
*/
public MoveAction(List<Entity> entityList, Point2D m) {
this.entityList = entityList;
this.movement = m;
public MoveAction(List<Entity> entityList, Point2D deltaMovement) {
this.entityList = new ArrayList<>(entityList);
this.movement = deltaMovement;
}

public void execute() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
Copyright 2022 Will Winder
Copyright 2022-2024 Will Winder
This file is part of Universal Gcode Sender (UGS).
Expand Down Expand Up @@ -59,4 +59,9 @@ public void undo() {
entityGroup.addAll(entities);
ResizeUtils.performScaling(entityGroup, location, newSize, originalSize);
}

@Override
public String toString() {
return "resize entity";
}
}
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 All @@ -21,6 +21,7 @@ This file is part of Universal Gcode Sender (UGS).
import com.willwinder.ugs.nbp.designer.entities.Entity;

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

/**
Expand All @@ -45,9 +46,9 @@ public class RotateAction implements DrawAction, UndoableAction {
* @param rotation the amount the shapes should be rotated, relative to the
*/
public RotateAction(List<Entity> entityList, Point2D center, double rotation) {
this.entityList = entityList;
this.entityList = new ArrayList<>(entityList);
this.rotation = rotation;
this.center = center;
this.center = new Point2D.Double(center.getX(), center.getY());
}

public void execute() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
Copyright 2022 Will Winder
Copyright 2022-2024 Will Winder
This file is part of Universal Gcode Sender (UGS).
Expand Down Expand Up @@ -47,7 +47,7 @@ public static Point2D getDeltaMovement(Location location, Size size, Size newSiz

public static void performScaling(Entity target, Location location, Size originalSize, Size newSize) {
// Do not scale if the entity will become too small after operation
if (newSize.getWidth() < 1 || newSize.getHeight() < 1) {
if (newSize.getWidth() <= 0 || newSize.getHeight() <= 0) {
return;
}

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 @@ -91,7 +91,7 @@ public void render(Graphics2D graphics, Drawing drawing) {
BasicStroke dashedStroke = new BasicStroke(strokeWidth, BasicStroke.CAP_BUTT, BasicStroke.JOIN_MITER, 1, new float[]{dashWidth, dashWidth}, 0);

Shape shape = getShape();
if (getCutType() != CutType.NONE && getTargetDepth() == 0) {
if (getCutType() == CutType.NONE) {
drawShape(graphics, dashedStroke, Colors.SHAPE_HINT, shape);
} else if (getCutType() == CutType.POCKET) {
graphics.setStroke(new BasicStroke(strokeWidth));
Expand Down Expand Up @@ -137,7 +137,7 @@ public Rectangle2D getBounds() {
}

private Color getCutColor() {
int color = Math.max(0, Math.min(255, (int) Math.round(255d * getCutAlpha()) - 25));
int color = Math.max(0, Math.min(255, (int) Math.round(255d * getCutAlpha()) - 50));
return new Color(color, color, color);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,26 @@
/*
Copyright 2021-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.entities.cuttable;

import com.willwinder.ugs.nbp.designer.entities.Entity;
import com.willwinder.ugs.nbp.designer.entities.EntityEvent;
import com.willwinder.ugs.nbp.designer.entities.EventType;
import org.apache.commons.lang3.StringUtils;

import java.awt.Font;
Expand All @@ -11,6 +31,11 @@
import java.awt.geom.Rectangle2D;
import java.awt.image.BufferedImage;

/**
* A cuttable text shape
*
* @author Joacim Breiler
*/
public class Text extends AbstractCuttable {
private String text;
private String fontFamily;
Expand Down Expand Up @@ -41,6 +66,8 @@ private void regenerateShape() {
// Create a temporary shape
shape = transform.createTransformedShape(new Rectangle2D.Double(0, 0, 2, 12));
}

notifyEvent(new EntityEvent(this, EventType.RESIZED));
}

@Override
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 All @@ -23,22 +23,30 @@ This file is part of Universal Gcode Sender (UGS).
import com.willwinder.ugs.nbp.designer.logic.Controller;
import net.miginfocom.swing.MigLayout;

import javax.swing.*;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSpinner;
import javax.swing.SpinnerNumberModel;
import javax.swing.SwingConstants;
import javax.swing.SwingUtilities;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import java.awt.*;
import java.awt.Dimension;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.awt.geom.Point2D;
import java.util.List;
import java.util.stream.Collectors;

/**
* @author Joacim Breiler
*/
public class MultiplyDialog extends JDialog implements ChangeListener, WindowListener {
public static final int PADDING = 2;
public static final String PANEL_LAYOUT_CONFIG = "fill, insets 2";
public static final String SPINNER_COL_CONSTRAINTS = "width 60:100:100, wrap";

private JSpinner xCountSpinner;
private JSpinner xSpacingSpinner;
Expand All @@ -56,9 +64,9 @@ public MultiplyDialog(Controller controller) {
this.controller.getDrawing().insertEntity(entityGroup);
entityGroup.setName("Temporary multiplier group");
setTitle("Multiply object");
setPreferredSize(new Dimension(360, 200));
setPreferredSize(new Dimension(400, 200));
setLayout(new MigLayout("fill, insets 5", "", ""));
setResizable(false);
setResizable(true);

createComponents();
addEventListeners();
Expand All @@ -84,11 +92,11 @@ private void createComponents() {
BorderFactory.createEmptyBorder(PADDING, PADDING, PADDING, PADDING)));
horizontalPanel.add(new JLabel("X Columns", SwingConstants.TRAILING), "grow");
xCountSpinner = new JSpinner(new SpinnerNumberModel(1, 1, 1000, 1));
horizontalPanel.add(xCountSpinner, "wrap");
horizontalPanel.add(xCountSpinner, SPINNER_COL_CONSTRAINTS);

horizontalPanel.add(new JLabel("X Spacing", SwingConstants.TRAILING), "grow");
xSpacingSpinner = new JSpinner(new SpinnerNumberModel(1d, 0.001, 1000, .1));
horizontalPanel.add(xSpacingSpinner, "wrap");
horizontalPanel.add(xSpacingSpinner, SPINNER_COL_CONSTRAINTS);
add(horizontalPanel, "grow");

JPanel verticalPanel = new JPanel(new MigLayout(PANEL_LAYOUT_CONFIG));
Expand All @@ -97,14 +105,14 @@ private void createComponents() {
BorderFactory.createEmptyBorder(PADDING, PADDING, PADDING, PADDING)));
verticalPanel.add(new JLabel("Y Rows", SwingConstants.TRAILING), "grow");
yCountSpinner = new JSpinner(new SpinnerNumberModel(1, 1, 1000, 1));
verticalPanel.add(yCountSpinner, "wrap");
verticalPanel.add(yCountSpinner, SPINNER_COL_CONSTRAINTS);

verticalPanel.add(new JLabel("Y Spacing", SwingConstants.TRAILING), "grow");
ySpacingSpinner = new JSpinner(new SpinnerNumberModel(1d, 0.001, 1000, .1));
verticalPanel.add(ySpacingSpinner, "wrap");
verticalPanel.add(ySpacingSpinner, SPINNER_COL_CONSTRAINTS);
add(verticalPanel, "grow, wrap");

JPanel buttonPanel = new JPanel(new MigLayout("insets 0", "[center, grow]"));
JPanel buttonPanel = new JPanel(new MigLayout("insets 5", "[center, grow]"));
cancelButton = new JButton("Cancel");
buttonPanel.add(cancelButton);

Expand Down Expand Up @@ -144,7 +152,7 @@ public void stateChanged(ChangeEvent e) {
EntityGroup selection = new EntityGroup();
selection.addAll(controller.getSelectionManager().getSelection().stream()
.map(Entity::copy)
.collect(Collectors.toList()));
.toList());

for (int x = 0; x < (int) xCountSpinner.getValue(); x++) {
for (int y = 0; y < (int) yCountSpinner.getValue(); y++) {
Expand Down
Loading

0 comments on commit 1f5b917

Please sign in to comment.