Skip to content

Commit

Permalink
Added actions for jogging to the entities extents
Browse files Browse the repository at this point in the history
  • Loading branch information
breiler committed May 27, 2024
1 parent 431203d commit ee2b0f6
Show file tree
Hide file tree
Showing 59 changed files with 3,331 additions and 167 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package com.willwinder.ugs.nbp.designer.actions;

import com.willwinder.ugs.nbp.designer.entities.selection.SelectionEvent;
import com.willwinder.ugs.nbp.designer.entities.selection.SelectionListener;
import com.willwinder.ugs.nbp.designer.entities.selection.SelectionManager;
import com.willwinder.ugs.nbp.designer.logic.ControllerFactory;
import com.willwinder.ugs.nbp.lib.lookup.CentralLookup;
import com.willwinder.universalgcodesender.listeners.ControllerState;
import com.willwinder.universalgcodesender.listeners.UGSEventListener;
import com.willwinder.universalgcodesender.model.BackendAPI;
import com.willwinder.universalgcodesender.model.UGSEvent;
import com.willwinder.universalgcodesender.model.events.ControllerStateEvent;

import javax.swing.SwingUtilities;

public abstract class JogMachineAbstractAction extends AbstractDesignAction implements SelectionListener, UGSEventListener {

private final transient BackendAPI backend;

protected JogMachineAbstractAction() {
backend = CentralLookup.getDefault().lookup(BackendAPI.class);
backend.addUGSEventListener(this);
registerControllerListener();
setEnabled(isEnabled());
}

private void registerControllerListener() {
SelectionManager selectionManager = ControllerFactory.getController().getSelectionManager();
selectionManager.addSelectionListener(this);
setEnabled(isEnabled());
}

@Override
public boolean isEnabled() {
SelectionManager selectionManager = ControllerFactory.getController().getSelectionManager();
boolean hasSelection = !selectionManager.getSelection().isEmpty();
boolean isIdle = backend.getControllerState() == ControllerState.IDLE;
return hasSelection && isIdle;
}

@Override
public void onSelectionEvent(SelectionEvent selectionEvent) {
setEnabled(isEnabled());
}

@Override
public void UGSEvent(UGSEvent event) {
if (event instanceof ControllerStateEvent) {
SwingUtilities.invokeLater(() -> setEnabled(isEnabled()));
}
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
Copyright 2023 Will Winder
Copyright 2023-2024 Will Winder
This file is part of Universal Gcode Sender (UGS).
Expand All @@ -18,30 +18,20 @@ This file is part of Universal Gcode Sender (UGS).
*/
package com.willwinder.ugs.nbp.designer.actions;

import com.willwinder.ugs.nbp.designer.entities.selection.SelectionEvent;
import com.willwinder.ugs.nbp.designer.entities.selection.SelectionListener;
import com.willwinder.ugs.nbp.designer.entities.selection.SelectionManager;
import com.willwinder.ugs.nbp.designer.logic.ControllerFactory;
import com.willwinder.ugs.nbp.lib.lookup.CentralLookup;
import com.willwinder.ugs.nbp.lib.services.LocalizingService;
import com.willwinder.universalgcodesender.listeners.ControllerState;
import com.willwinder.universalgcodesender.listeners.UGSEventListener;
import com.willwinder.universalgcodesender.model.BackendAPI;
import com.willwinder.universalgcodesender.model.PartialPosition;
import com.willwinder.universalgcodesender.model.UGSEvent;
import com.willwinder.universalgcodesender.model.events.ControllerStateEvent;
import static com.willwinder.universalgcodesender.model.UnitUtils.Units.MM;
import com.willwinder.universalgcodesender.services.JogService;
import com.willwinder.universalgcodesender.utils.ThreadHelper;
import org.openide.awt.ActionID;
import org.openide.awt.ActionRegistration;
import org.openide.util.ImageUtilities;

import javax.swing.SwingUtilities;
import java.awt.event.ActionEvent;
import java.awt.geom.Point2D;

import static com.willwinder.universalgcodesender.model.UnitUtils.Units.MM;

/**
* An action that will jog the machine to the center of the selected objects
*
Expand All @@ -52,38 +42,19 @@ This file is part of Universal Gcode Sender (UGS).
id = "JogMachineToCenterAction")
@ActionRegistration(
iconBase = JogMachineToCenterAction.SMALL_ICON_PATH,
displayName = "Jog machine to center",
displayName = "Jog to center",
lazy = false)
public class JogMachineToCenterAction extends AbstractDesignAction implements SelectionListener, UGSEventListener {
public class JogMachineToCenterAction extends JogMachineAbstractAction {
public static final String SMALL_ICON_PATH = "img/jog-to.svg";
public static final String LARGE_ICON_PATH = "img/jog-to24.svg";
private final transient BackendAPI backend;

public JogMachineToCenterAction() {
super();
putValue("menuText", "Jog machine to center");
putValue(NAME, "Jog machine to center");
putValue("iconBase", SMALL_ICON_PATH);
putValue(SMALL_ICON, ImageUtilities.loadImageIcon(SMALL_ICON_PATH, false));
putValue(LARGE_ICON_KEY, ImageUtilities.loadImageIcon(LARGE_ICON_PATH, false));

backend = CentralLookup.getDefault().lookup(BackendAPI.class);
backend.addUGSEventListener(this);
registerControllerListener();
setEnabled(isEnabled());
}

private void registerControllerListener() {
SelectionManager selectionManager = ControllerFactory.getController().getSelectionManager();
selectionManager.addSelectionListener(this);
setEnabled(isEnabled());
}

@Override
public boolean isEnabled() {
SelectionManager selectionManager = ControllerFactory.getController().getSelectionManager();
boolean hasSelection = !selectionManager.getSelection().isEmpty();
boolean isIdle = backend.getControllerState() == ControllerState.IDLE;
return hasSelection && isIdle;
}

@Override
Expand All @@ -96,16 +67,4 @@ public void actionPerformed(ActionEvent e) {
jogService.jogTo(centerPosition);
});
}

@Override
public void onSelectionEvent(SelectionEvent selectionEvent) {
setEnabled(isEnabled());
}

@Override
public void UGSEvent(UGSEvent event) {
if (event instanceof ControllerStateEvent) {
SwingUtilities.invokeLater(() -> setEnabled(isEnabled()));
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/*
Copyright 2023 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.ControllerFactory;
import com.willwinder.ugs.nbp.lib.lookup.CentralLookup;
import com.willwinder.ugs.nbp.lib.services.LocalizingService;
import com.willwinder.universalgcodesender.model.PartialPosition;
import static com.willwinder.universalgcodesender.model.UnitUtils.Units.MM;
import com.willwinder.universalgcodesender.services.JogService;
import com.willwinder.universalgcodesender.utils.ThreadHelper;
import org.openide.awt.ActionID;
import org.openide.awt.ActionRegistration;
import org.openide.util.ImageUtilities;

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

/**
* @author Joacim Breiler
*/
@ActionID(
category = LocalizingService.CATEGORY_DESIGNER,
id = "JogMachineToLowerLeftCornerAction")
@ActionRegistration(
iconBase = JogMachineToLowerLeftCornerAction.SMALL_ICON_PATH,
displayName = "Jog machine to lower left",
lazy = false)
public class JogMachineToLowerLeftCornerAction extends JogMachineAbstractAction {
public static final String SMALL_ICON_PATH = "img/jog-to-lower-left.svg";
public static final String LARGE_ICON_PATH = "img/jog-to-lower-left24.svg";

public JogMachineToLowerLeftCornerAction() {
super();
putValue("menuText", "Jog to lower left corner");
putValue(NAME, "Jog to lower left corner");
putValue("iconBase", SMALL_ICON_PATH);
putValue(SMALL_ICON, ImageUtilities.loadImageIcon(SMALL_ICON_PATH, false));
putValue(LARGE_ICON_KEY, ImageUtilities.loadImageIcon(LARGE_ICON_PATH, false));
}

@Override
public void actionPerformed(ActionEvent e) {
ThreadHelper.invokeLater(() -> {
Rectangle2D bounds = ControllerFactory.getController().getSelectionManager().getBounds();
PartialPosition centerPosition = new PartialPosition(bounds.getMinX(), bounds.getMinY(), MM);

JogService jogService = CentralLookup.getDefault().lookup(JogService.class);
jogService.jogTo(centerPosition);
});
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/*
Copyright 2023 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.ControllerFactory;
import com.willwinder.ugs.nbp.lib.lookup.CentralLookup;
import com.willwinder.ugs.nbp.lib.services.LocalizingService;
import com.willwinder.universalgcodesender.model.PartialPosition;
import static com.willwinder.universalgcodesender.model.UnitUtils.Units.MM;
import com.willwinder.universalgcodesender.services.JogService;
import com.willwinder.universalgcodesender.utils.ThreadHelper;
import org.openide.awt.ActionID;
import org.openide.awt.ActionRegistration;
import org.openide.util.ImageUtilities;

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

/**
* @author Joacim Breiler
*/
@ActionID(
category = LocalizingService.CATEGORY_DESIGNER,
id = "JogMachineToLowerRightCornerAction")
@ActionRegistration(
iconBase = JogMachineToLowerRightCornerAction.SMALL_ICON_PATH,
displayName = "Jog machine to lower right",
lazy = false)
public class JogMachineToLowerRightCornerAction extends JogMachineAbstractAction {
public static final String SMALL_ICON_PATH = "img/jog-to-lower-right.svg";
public static final String LARGE_ICON_PATH = "img/jog-to-lower-right24.svg";

public JogMachineToLowerRightCornerAction() {
super();
putValue("menuText", "Jog to lower right corner");
putValue(NAME, "Jog to lower right corner");
putValue("iconBase", SMALL_ICON_PATH);
putValue(SMALL_ICON, ImageUtilities.loadImageIcon(SMALL_ICON_PATH, false));
putValue(LARGE_ICON_KEY, ImageUtilities.loadImageIcon(LARGE_ICON_PATH, false));
}

@Override
public void actionPerformed(ActionEvent e) {
ThreadHelper.invokeLater(() -> {
Rectangle2D bounds = ControllerFactory.getController().getSelectionManager().getBounds();
PartialPosition centerPosition = new PartialPosition(bounds.getMaxX(), bounds.getMinY(), MM);

JogService jogService = CentralLookup.getDefault().lookup(JogService.class);
jogService.jogTo(centerPosition);
});
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/*
Copyright 2023 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.ControllerFactory;
import com.willwinder.ugs.nbp.lib.lookup.CentralLookup;
import com.willwinder.ugs.nbp.lib.services.LocalizingService;
import com.willwinder.universalgcodesender.model.PartialPosition;
import static com.willwinder.universalgcodesender.model.UnitUtils.Units.MM;
import com.willwinder.universalgcodesender.services.JogService;
import com.willwinder.universalgcodesender.utils.ThreadHelper;
import org.openide.awt.ActionID;
import org.openide.awt.ActionRegistration;
import org.openide.util.ImageUtilities;

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

/**
* @author Joacim Breiler
*/
@ActionID(
category = LocalizingService.CATEGORY_DESIGNER,
id = "JogMachineToTopLeftCornerAction")
@ActionRegistration(
iconBase = JogMachineToTopLeftCornerAction.SMALL_ICON_PATH,
displayName = "Jog machine to top left",
lazy = false)
public class JogMachineToTopLeftCornerAction extends JogMachineAbstractAction {
public static final String SMALL_ICON_PATH = "img/jog-to-top-left.svg";
public static final String LARGE_ICON_PATH = "img/jog-to-top-left24.svg";

public JogMachineToTopLeftCornerAction() {
super();
putValue("menuText", "Jog to top left corner");
putValue(NAME, "Jog to top left corner");
putValue("iconBase", SMALL_ICON_PATH);
putValue(SMALL_ICON, ImageUtilities.loadImageIcon(SMALL_ICON_PATH, false));
putValue(LARGE_ICON_KEY, ImageUtilities.loadImageIcon(LARGE_ICON_PATH, false));
}

@Override
public void actionPerformed(ActionEvent e) {
ThreadHelper.invokeLater(() -> {
Rectangle2D bounds = ControllerFactory.getController().getSelectionManager().getBounds();
PartialPosition centerPosition = new PartialPosition(bounds.getMinX(), bounds.getMaxY(), MM);

JogService jogService = CentralLookup.getDefault().lookup(JogService.class);
jogService.jogTo(centerPosition);
});
}
}
Loading

0 comments on commit ee2b0f6

Please sign in to comment.