Skip to content

Commit

Permalink
Feature/designer text (#1727)
Browse files Browse the repository at this point in the history
* Added a text tool to the designer
* Added possibility to change font and and save/load designs with texts
* Prevent sending files when in alarm mode
* Made some functions generic and fixed saving and loading geometries so that it will preserve rotation and scale.
* Added measurements to the grid
* Updated test file to reflect new format
* Fixed broken tests
  • Loading branch information
breiler authored Nov 24, 2021
1 parent d97daf8 commit ffb4342
Show file tree
Hide file tree
Showing 38 changed files with 2,247 additions and 2,562 deletions.
3,650 changes: 1,331 additions & 2,319 deletions test_files/designer.ugsd

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -658,7 +658,9 @@ public boolean canCancel() {

@Override
public boolean canSend() {
return isIdle() && (this.gcodeFile != null);
return isIdle() &&
controller.getControllerStatus().getState() != ControllerState.ALARM &&
this.gcodeFile != null;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
Copyright 2021 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.logic.Controller;
import com.willwinder.ugs.nbp.designer.logic.Tool;
import org.openide.util.ImageUtilities;

import javax.swing.*;
import java.awt.event.ActionEvent;

/**
* @author Joacim Breiler
*/
public class ToolDrawTextAction extends AbstractAction {
private static final String SMALL_ICON_PATH = "img/text.png";
private static final String LARGE_ICON_PATH = "img/text24.png";
private final Controller controller;

public ToolDrawTextAction(Controller controller) {
putValue("iconBase", SMALL_ICON_PATH);
putValue(SMALL_ICON, ImageUtilities.loadImageIcon(SMALL_ICON_PATH, false));
putValue(LARGE_ICON_KEY, ImageUtilities.loadImageIcon(LARGE_ICON_PATH, false));
putValue("menuText", "Draw text");
putValue(NAME, "Draw text");
this.controller = controller;
}

@Override
public void actionPerformed(ActionEvent e) {
controller.setTool(Tool.TEXT);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,20 @@ public Size getSize() {
return new Size(bounds.getWidth(), bounds.getHeight());
}

@Override
public void setSize(Size size) {
if (size.getWidth() < 0.1) {
size = new Size(0.1, size.getHeight());
}

if (size.getHeight() < 0.1) {
size = new Size(size.getWidth(), 0.1);
}

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

@Override
public Rectangle2D getBounds() {
return getShape().getBounds2D();
Expand All @@ -93,7 +107,8 @@ public Shape getShape() {

@Override
public Point2D getPosition() {
return new Point2D.Double(getBounds().getX(), getBounds().getY());
Rectangle2D bounds = getBounds();
return new Point2D.Double(bounds.getX(), bounds.getY());
}

@Override
Expand All @@ -110,7 +125,8 @@ public Point2D getCenter() {

@Override
public void setCenter(Point2D center) {
setPosition(new Point2D.Double(center.getX() - (getSize().getWidth() / 2d), center.getY() - (getSize().getHeight() / 2d)));
Size size = getSize();
setPosition(new Point2D.Double(center.getX() - (size.getWidth() / 2d), center.getY() - (size.getHeight() / 2d)));
}


Expand Down Expand Up @@ -164,8 +180,9 @@ public void rotate(double angle) {
@Override
public void scale(double sx, double sy) {
Point2D originalPosition = getPosition();
transform.concatenate(AffineTransform.getScaleInstance(sx, sy));
transform.preConcatenate(AffineTransform.getScaleInstance(sx, sy));

// Restore position
Point2D currentPosition = getPosition();
transform.preConcatenate(AffineTransform.getTranslateInstance(originalPosition.getX() - currentPosition.getX(), originalPosition.getY() - currentPosition.getY()));
notifyEvent(new EntityEvent(this, EventType.RESIZED));
Expand Down Expand Up @@ -203,4 +220,14 @@ public String getName() {
public String toString() {
return getName();
}

public void setWidth(double width) {
Size size = getSize();
setSize(new Size(width, size.getHeight()));
}

public void setHeight(double height) {
Size size = getSize();
setSize(new Size(size.getWidth(), height));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
Copyright 2021 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;

/**
* @author Joacim Breiler
*/
public class EntityException extends RuntimeException {
public EntityException(Exception e) {
super(e);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,6 @@ public enum EventType {
MOUSE_PRESSED,
MOUSE_DRAGGED,
MOUSE_RELEASED,
SETTINGS_CHANGED;
SETTINGS_CHANGED,
KEY_PRESSED
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,6 @@ protected AbstractControl(SelectionManager selectionManager) {
this.selectionManager = selectionManager;
}

@Override
public void setSize(Size size) {
// Never mind...
}

@Override
public Optional<Cursor> getHoverCursor() {
return Optional.empty();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,6 @@ private void createEntity() {

@Override
public String toString() {
return "CreateRectangleControl";
return "CreateEllipseControl";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
/*
Copyright 2021 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.controls;

import com.willwinder.ugs.nbp.designer.actions.AddAction;
import com.willwinder.ugs.nbp.designer.entities.EntityEvent;
import com.willwinder.ugs.nbp.designer.entities.EventType;
import com.willwinder.ugs.nbp.designer.entities.cuttable.Text;
import com.willwinder.ugs.nbp.designer.gui.Drawing;
import com.willwinder.ugs.nbp.designer.gui.MouseEntityEvent;
import com.willwinder.ugs.nbp.designer.logic.Controller;
import com.willwinder.ugs.nbp.designer.logic.Tool;
import com.willwinder.universalgcodesender.uielements.helpers.ThemeColors;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.geom.Point2D;
import java.awt.geom.Rectangle2D;

/**
* A control that will create a new ellipse
*
* @author Joacim Breiler
*/
public class CreateTextControl extends AbstractControl {

private final Controller controller;
private Point2D startPosition;
private Point2D endPosition;
private boolean isPressed;

public CreateTextControl(Controller controller) {
super(controller.getSelectionManager());
this.controller = controller;
}

@Override
public void render(Graphics2D graphics, Drawing drawing) {
if (isPressed) {
double startX = Math.min(startPosition.getX(), endPosition.getX());
double endX = Math.max(startPosition.getX(), endPosition.getX());
double startY = Math.min(startPosition.getY(), endPosition.getY());
double endY = Math.max(startPosition.getY(), endPosition.getY());
Rectangle2D.Double rect = new Rectangle2D.Double(startX, startY, endX - startX, endY - startY);
graphics.setColor(ThemeColors.LIGHT_BLUE_GREY);
graphics.draw(rect);
}
}

@Override
public boolean isWithin(Point2D point) {
return controller.getTool() == Tool.TEXT;
}

@Override
public void onEvent(EntityEvent entityEvent) {
if (entityEvent instanceof MouseEntityEvent) {
MouseEntityEvent mouseEntityEvent = (MouseEntityEvent) entityEvent;
startPosition = mouseEntityEvent.getStartMousePosition();
endPosition = mouseEntityEvent.getCurrentMousePosition();

if (mouseEntityEvent.getType() == EventType.MOUSE_PRESSED) {
isPressed = true;
} else if (mouseEntityEvent.getType() == EventType.MOUSE_DRAGGED) {
isPressed = true;
} else if (mouseEntityEvent.getType() == EventType.MOUSE_RELEASED) {
isPressed = false;
createEntity();
}
}
}

private void createEntity() {
double startX = Math.min(startPosition.getX(), endPosition.getX());
double startY = Math.min(startPosition.getY(), endPosition.getY());

Text text = new Text(startX, startY);
AddAction addAction = new AddAction(controller, text);
addAction.actionPerformed(new ActionEvent(this, 0, ""));
controller.addEntity(text);
controller.setTool(Tool.SELECT);
controller.getSelectionManager().addSelection(text);
}

@Override
public String toString() {
return "CreateTextControl";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package com.willwinder.ugs.nbp.designer.entities.controls;

import com.willwinder.ugs.nbp.designer.entities.Entity;
import com.willwinder.ugs.nbp.designer.entities.EntityEvent;
import com.willwinder.ugs.nbp.designer.entities.cuttable.Text;
import com.willwinder.ugs.nbp.designer.gui.Colors;
import com.willwinder.ugs.nbp.designer.gui.Drawing;
import com.willwinder.ugs.nbp.designer.logic.Controller;

import java.awt.*;
import java.awt.geom.Line2D;
import java.awt.geom.Point2D;
import java.awt.geom.Rectangle2D;
import java.util.List;

public class EditTextControl extends AbstractControl {

private final Controller controller;
private long previousTime = 0;

public EditTextControl(Controller controller) {
super(controller.getSelectionManager());
this.controller = controller;
this.previousTime = System.currentTimeMillis();
}

@Override
public boolean isWithin(Point2D point) {
return isTextEntitySelected();
}

private boolean isTextEntitySelected() {
boolean isTextEntity = false;
List<Entity> selection = controller.getSelectionManager().getSelection();
if (!selection.isEmpty()) {
Entity entity = selection.get(0);
isTextEntity = entity instanceof Text;
}
return isTextEntity;
}

@Override
public void render(Graphics2D graphics, Drawing drawing) {
if (!isTextEntitySelected()) {
return;
}

if (previousTime + 600 < System.currentTimeMillis()) {
graphics.setColor(Colors.CURSOR);
graphics.setStroke(new BasicStroke(Double.valueOf(1 / drawing.getScale()).floatValue()));
Rectangle2D bounds = getRelativeShape().getBounds2D();
Line2D line = new Line2D.Double(bounds.getX() + bounds.getWidth(), bounds.getY(), bounds.getX() + bounds.getWidth(), bounds.getY() + bounds.getHeight());
graphics.draw(getSelectionManager().getTransform().createTransformedShape(line));

}

if (previousTime + 1200 < System.currentTimeMillis()) {
previousTime = System.currentTimeMillis();
}
}

@Override
public void onEvent(EntityEvent entityEvent) {

}
}
Loading

0 comments on commit ffb4342

Please sign in to comment.