Skip to content

Commit

Permalink
selection stroking => path stroking
Browse files Browse the repository at this point in the history
  • Loading branch information
lbalazscs committed Sep 28, 2018
1 parent 8bc7e13 commit 9f821d2
Show file tree
Hide file tree
Showing 15 changed files with 204 additions and 115 deletions.
7 changes: 0 additions & 7 deletions src/main/java/pixelitor/menus/MenuBar.java
Original file line number Diff line number Diff line change
Expand Up @@ -581,13 +581,6 @@ private static JMenu createSelectMenu() {
.enableIf(ACTION_ENABLED)
.add();

selectMenu.addSeparator();

selectMenu.buildAction(SelectionActions.getTraceWithBrush())
.enableIf(ACTION_ENABLED).add();
selectMenu.buildAction(SelectionActions.getTraceWithEraser())
.enableIf(ACTION_ENABLED).add();

return selectMenu;
}

Expand Down
56 changes: 0 additions & 56 deletions src/main/java/pixelitor/selection/SelectionActions.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,12 @@
import pixelitor.gui.utils.DialogBuilder;
import pixelitor.gui.utils.GridBagHelper;
import pixelitor.history.History;
import pixelitor.layers.Drawable;
import pixelitor.menus.MenuAction;
import pixelitor.menus.view.ShowHideAction;
import pixelitor.menus.view.ShowHideSelectionAction;
import pixelitor.tools.AbstractBrushTool;
import pixelitor.tools.Tools;
import pixelitor.tools.pen.Path;
import pixelitor.tools.pen.history.ConvertSelectionToPathEdit;
import pixelitor.utils.Messages;
import pixelitor.utils.Shapes;
import pixelitor.utils.test.RandomGUITest;

Expand Down Expand Up @@ -74,13 +71,6 @@ public void onClick() {

private static final ShowHideAction showHide = new ShowHideSelectionAction();

private static final Action traceWithBrush = new TraceAction(
"Stroke with Current Brush", Tools.BRUSH);
private static final Action traceWithEraser = new TraceAction(
"Stroke with Current Eraser", Tools.ERASER);
private static final Action traceWithSmudge = new TraceAction(
"Stroke with Current Smudge", Tools.SMUDGE);

private static final Action convertToPath = new AbstractAction("Convert to Path") {
@Override
public void actionPerformed(ActionEvent e) {
Expand Down Expand Up @@ -170,9 +160,6 @@ public static void setEnabled(boolean b, Composition comp) {
}

crop.setEnabled(b);
traceWithBrush.setEnabled(b);
traceWithEraser.setEnabled(b);
traceWithSmudge.setEnabled(b);
deselect.setEnabled(b);
invert.setEnabled(b);
showHide.setEnabled(b);
Expand All @@ -188,18 +175,6 @@ public static Action getCrop() {
return crop;
}

public static Action getTraceWithBrush() {
return traceWithBrush;
}

public static Action getTraceWithEraser() {
return traceWithEraser;
}

public static Action getTraceWithSmudge() {
return traceWithSmudge;
}

public static Action getDeselect() {
return deselect;
}
Expand All @@ -220,35 +195,4 @@ public static Action getModify() {
return modify;
}

/**
* Strokes a selection with an {@link AbstractBrushTool}
*/
private static class TraceAction extends MenuAction {
private final AbstractBrushTool brushTool;

private TraceAction(String name, AbstractBrushTool brushTool) {
super(name);
this.brushTool = brushTool;
}

@Override
public void onClick() {
ImageComponents.onActiveComp(this::trace);
}

private void trace(Composition comp) {
if (!comp.activeIsDrawable()) {
Messages.showNotDrawableError();
return;
}

if (comp.hasSelection()) {
Shape shape = comp.getSelectionShape();
if (shape != null) {
Drawable dr = comp.getActiveDrawableOrThrow();
brushTool.trace(dr, shape);
}
}
}
}
}
4 changes: 0 additions & 4 deletions src/main/java/pixelitor/tools/SelectionTool.java
Original file line number Diff line number Diff line change
Expand Up @@ -90,10 +90,6 @@ public void initSettingsPanel() {

settingsPanel.addSeparator();

settingsPanel.addButton(SelectionActions.getTraceWithBrush());
settingsPanel.addButton(SelectionActions.getTraceWithEraser());
settingsPanel.addButton(SelectionActions.getTraceWithSmudge());

settingsPanel.addButton(SelectionActions.getCrop());

settingsPanel.addButton(SelectionActions.getConvertToPath(),
Expand Down
14 changes: 11 additions & 3 deletions src/main/java/pixelitor/tools/pen/Path.java
Original file line number Diff line number Diff line change
Expand Up @@ -64,15 +64,17 @@ public class Path implements Serializable {
private BuildState prevBuildState;
private transient PenToolMode preferredPenToolMode;

public Path(Composition comp) {
public Path(Composition comp, boolean setAsActive) {
this.comp = comp;
comp.setActivePath(this);
if (setAsActive) {
comp.setActivePath(this);
}
id = "P" + (debugCounter++);
buildState = BuildState.NO_INTERACTION;
}

public Path copyForUndo() {
Path copy = new Path(comp);
Path copy = new Path(comp, false);
for (SubPath sp : subPaths) {
copy.subPaths.add(sp.copyForUndo(copy));
}
Expand Down Expand Up @@ -280,6 +282,8 @@ public SubPath getSubPath(int index) {
}

public void delete(SubPath subPath) {
assert comp.getActivePath() == this;

Path backup = copyForUndo();
subPaths.removeIf(sp -> sp == subPath);
assert subPaths.size() >= 1; // should never be called for the last subpath
Expand All @@ -288,9 +292,13 @@ public void delete(SubPath subPath) {

PathEdit edit = new PathEdit("Delete Subpath", comp, backup, this);
History.addEdit(edit);

assert comp.getActivePath() == this;
}

public void delete() {
assert comp.getActivePath() == this;

comp.setActivePath(null);

assert Tools.PEN.isActive();
Expand Down
7 changes: 4 additions & 3 deletions src/main/java/pixelitor/tools/pen/PathBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import static pixelitor.tools.pen.BuildState.MOVE_EDITING_PREVIOUS;
import static pixelitor.tools.pen.BuildState.MOVING_TO_NEXT_ANCHOR;
import static pixelitor.tools.pen.BuildState.NO_INTERACTION;
import static pixelitor.tools.pen.PenTool.hasPath;
import static pixelitor.tools.pen.PenTool.path;
import static pixelitor.tools.util.DraggablePoint.activePoint;

Expand Down Expand Up @@ -62,7 +63,7 @@ private PathBuilder() {
@Override
public void mousePressed(PMouseEvent e) {
if (path == null) {
path = new Path(e.getComp());
path = new Path(e.getComp(), true);
path.setPreferredPenToolMode(this);
}

Expand Down Expand Up @@ -343,7 +344,7 @@ public boolean mouseMoved(MouseEvent e, ImageComponent ic) {

@Override
public void paint(Graphics2D g) {
if (path != null) {
if (hasPath()) {
path.paintForBuilding(g);
}
}
Expand All @@ -360,7 +361,7 @@ public void start() {

@Override
public void modeEnded() {
if (path != null && !path.getActiveSubpath().isFinished()) {
if (hasPath() && !path.getActiveSubpath().isFinished()) {
path.finishActiveSubpath("PB.modeEnded");
} else {
assertStateIs(NO_INTERACTION);
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/pixelitor/tools/pen/PathEditor.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@

import static pixelitor.tools.pen.AnchorPointType.CUSP;
import static pixelitor.tools.pen.AnchorPointType.SYMMETRIC;
import static pixelitor.tools.pen.PenTool.hasPath;
import static pixelitor.tools.pen.PenTool.path;
import static pixelitor.tools.util.DraggablePoint.activePoint;

Expand All @@ -48,7 +49,7 @@ private PathEditor() {

@Override
public void paint(Graphics2D g) {
if (path != null) {
if (hasPath()) {
path.paintForEditing(g);
}
}
Expand Down
40 changes: 35 additions & 5 deletions src/main/java/pixelitor/tools/pen/PenTool.java
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,13 @@ public class PenTool extends Tool {

private boolean rubberBand = true;

private static final Action traceWithBrush = new TraceAction(
"Stroke with Current Brush", Tools.BRUSH);
private static final Action traceWithEraser = new TraceAction(
"Stroke with Current Eraser", Tools.ERASER);
private static final Action traceWithSmudge = new TraceAction(
"Stroke with Current Smudge", Tools.SMUDGE);

public PenTool() {
super("Pen", 'p', "pen_tool_icon.png",
"", // getStatusBarMessage() is overridden
Expand All @@ -90,7 +97,7 @@ public void actionPerformed(ActionEvent e) {
dumpPathAction = new AbstractAction("Dump") {
@Override
public void actionPerformed(ActionEvent e) {
assert path != null;
assert hasPath();
path.dump();
}
};
Expand All @@ -116,6 +123,10 @@ public void initSettingsPanel() {
// settingsPanel.addButton(traceAction, "traceAction",
// "Trace the path with a stroke or with a tool");

settingsPanel.addButton(traceWithBrush);
settingsPanel.addButton(traceWithEraser);
settingsPanel.addButton(traceWithSmudge);

if (Build.CURRENT.isDevelopment()) {
settingsPanel.addButton(dumpPathAction);
}
Expand Down Expand Up @@ -147,7 +158,7 @@ public void startBuilding(boolean calledFromModeChooser) {
ignoreModeChooser = false;
}
changeMode(BUILD, path);
enableActionsBasedOnFinishedPath(path != null);
enableActionsBasedOnFinishedPath(hasPath());
ImageComponents.repaintActive();

assert checkPathConsistency();
Expand Down Expand Up @@ -273,7 +284,7 @@ public void paintOverImage(Graphics2D g2, Canvas canvas, ImageComponent ic,

@Override
public void coCoordsChanged(ImageComponent ic) {
if (path != null) {
if (hasPath()) {
path.coCoordsChanged(ic);
}
}
Expand Down Expand Up @@ -317,18 +328,21 @@ protected void toolStarted() {
assert checkPathConsistency();
}

private static boolean checkPathConsistency() {
public static boolean checkPathConsistency() {
assert path == ImageComponents.getActivePathOrNull()
: "tool path = " + path + ", active path = " + ImageComponents.getActivePathOrNull();
Composition activeComp = ImageComponents.getActiveCompOrNull();
if (activeComp == null) {
return true;
}
if (path != null && path.getComp() != activeComp) {
if (hasPath() && path.getComp() != activeComp) {
throw new IllegalStateException("foreign path " + path
+ ", path comp = " + path.getComp().toPathDebugString()
+ ", active comp = " + activeComp.toPathDebugString());
}
if (hasPath()) {
path.checkConsistency();
}
return true;
}

Expand Down Expand Up @@ -403,6 +417,22 @@ public boolean showRubberBand() {
return rubberBand && mode == BUILD;
}

public static boolean hasPath() {
return path != null;
}

public static Action getTraceWithBrush() {
return traceWithBrush;
}

public static Action getTraceWithEraser() {
return traceWithEraser;
}

public static Action getTraceWithSmudge() {
return traceWithSmudge;
}

@Override
public DebugNode getDebugNode() {
DebugNode node = super.getDebugNode();
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/pixelitor/tools/pen/PenToolMode.java
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ default void modeStarted(Path path) {
}

default void modeEnded() {
if (path != null) {
if (PenTool.hasPath()) {
Composition comp = ImageComponents.getActiveCompOrNull();
if (comp != null) {
comp.repaint();
Expand All @@ -63,7 +63,7 @@ default void modeEnded() {
default DebugNode createDebugNode() {
DebugNode node = new DebugNode("PenToolMode " + toString(), this);

if (path != null) {
if (PenTool.hasPath()) {
node.add(new PathNode(path));
} else {
node.addBoolean("Has Path", false);
Expand Down
64 changes: 64 additions & 0 deletions src/main/java/pixelitor/tools/pen/TraceAction.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/*
* Copyright 2018 Laszlo Balazs-Csiki and Contributors
*
* This file is part of Pixelitor. Pixelitor is free software: you
* can redistribute it and/or modify it under the terms of the GNU
* General Public License, version 3 as published by the Free
* Software Foundation.
*
* Pixelitor 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 Pixelitor. If not, see <http://www.gnu.org/licenses/>.
*/

package pixelitor.tools.pen;

import pixelitor.Composition;
import pixelitor.gui.ImageComponents;
import pixelitor.layers.Drawable;
import pixelitor.menus.MenuAction;
import pixelitor.tools.AbstractBrushTool;
import pixelitor.utils.Messages;

import java.awt.Shape;

/**
* Strokes a shape with an {@link AbstractBrushTool}
*/
public class TraceAction extends MenuAction {
private final AbstractBrushTool brushTool;

public TraceAction(String name, AbstractBrushTool brushTool) {
super(name);
this.brushTool = brushTool;
}

@Override
public void onClick() {
ImageComponents.onActiveComp(this::trace);
}

private void trace(Composition comp) {
if (!comp.activeIsDrawable()) {
Messages.showNotDrawableError();
return;
}

Path path = comp.getActivePath();
if (path == null) {
Messages.showInfo("No path",
"There is no path in the composition");
return;
}

Shape shape = path.toImageSpaceShape();
if (shape != null) {
Drawable dr = comp.getActiveDrawableOrThrow();
brushTool.trace(dr, shape);
}
}
}
Loading

0 comments on commit 9f821d2

Please sign in to comment.