Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make edit actions mappable to gamepad #2308

Merged
merged 2 commits into from
Sep 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions ugs-core/src/resources/MessagesBundle_en_US.properties
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ mainWindow.status.run = Run
platform.menu.runFrom = Run From...
platform.menu.toggleUnit = Toggle units
platform.menu.toggleUnit.tooltip = Toggles the preferred units in the application
platform.menu.insertPosition = Insert position
mainWindow.error.openingFile = Problem opening file
mainWindow.error.processingFile = Unknown IOException while processing file
mainWindow.error.pauseResume = Error while trying to pause/resume
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,8 +128,12 @@ private void initListeners() {
}

private void controlChangeListener() {
ProbeSettings.setSelectedTabIdx(jtp.getSelectedIndex());
probePreviewManager.setActive(jtp.getTitleAt(this.jtp.getSelectedIndex()));
if (isShowing()) {
ProbeSettings.setSelectedTabIdx(jtp.getSelectedIndex());
probePreviewManager.setActive(jtp.getTitleAt(this.jtp.getSelectedIndex()));
} else {
probePreviewManager.inactivate();
}
}

private void initComponents() {
Expand Down Expand Up @@ -163,17 +167,17 @@ public void componentOpened() {

@Override
public void componentClosed() {
probePreviewManager.inactivate();
controlChangeListener();
}

@Override
protected void componentHidden() {
probePreviewManager.inactivate();
controlChangeListener();
}

@Override
protected void componentShowing() {
probePreviewManager.activate();
controlChangeListener();
}

@OnStart
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
package com.willwinder.ugs.nbp.editor.actions;

import com.willwinder.ugs.nbp.lib.lookup.CentralLookup;
import com.willwinder.ugs.nbp.lib.services.LocalizingService;
import com.willwinder.universalgcodesender.i18n.Localization;
import com.willwinder.universalgcodesender.listeners.UGSEventListener;
import com.willwinder.universalgcodesender.model.Axis;
import com.willwinder.universalgcodesender.model.BackendAPI;
import com.willwinder.universalgcodesender.model.Position;
import com.willwinder.universalgcodesender.model.UGSEvent;
import com.willwinder.universalgcodesender.model.events.ControllerStateEvent;
import com.willwinder.universalgcodesender.model.events.FileStateEvent;
import org.apache.commons.lang3.StringUtils;
import org.netbeans.api.editor.EditorRegistry;
import org.openide.awt.ActionID;
import org.openide.awt.ActionReference;
import org.openide.awt.ActionReferences;
import org.openide.awt.ActionRegistration;
import org.openide.util.ImageUtilities;

import javax.swing.*;
import javax.swing.text.BadLocationException;
import javax.swing.text.Document;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.text.DecimalFormat;
import java.text.NumberFormat;

@ActionID(
category = LocalizingService.CATEGORY_EDIT,
id = "com.willwinder.ugs.nbp.editor.actions.InsertPositionAction")
@ActionRegistration(
iconBase = InsertPositionAction.SMALL_ICON_PATH,
displayName = "Insert position",
lazy = false)
@ActionReferences({
@ActionReference(
path = LocalizingService.MENU_EDIT,
position = 1900,
separatorAfter = 2000),
})
public class InsertPositionAction extends AbstractAction implements UGSEventListener {
public static final String NAME = LocalizingService.InsertPositionTitle;
public static final String SMALL_ICON_PATH = "icons/position.svg";
private static final String LARGE_ICON_PATH = "icons/position24.svg";
public static NumberFormat formatter = new DecimalFormat("#.###", Localization.dfs);
private final transient BackendAPI backend;

public InsertPositionAction() {
putValue("menuText", NAME);
putValue(Action.NAME, NAME);
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);
setEnabled(isEnabled());
}

@Override
public void UGSEvent(UGSEvent cse) {
if (cse instanceof ControllerStateEvent || cse instanceof FileStateEvent) {
EventQueue.invokeLater(() -> setEnabled(isEnabled()));
}
}

@Override
public boolean isEnabled() {
return backend.getGcodeFile() != null &&
backend.isConnected() &&
!backend.isSendingFile() &&
EditorRegistry.lastFocusedComponent() != null &&
EditorRegistry.lastFocusedComponent().getDocument() != null;
}

@Override
public void actionPerformed(ActionEvent e) {
if (!isEnabled()) {
return;
}

try {
Document document = EditorRegistry.lastFocusedComponent().getDocument();
int caretPosition = EditorRegistry.lastFocusedComponent().getCaretPosition();
document.insertString(caretPosition, getPositionAsString() + "\n", null);
} catch (BadLocationException ex) {
throw new RuntimeException(ex);
}
}

private String getPositionAsString() {
Position workPosition = backend.getWorkPosition();
StringBuilder position = new StringBuilder();
for (Axis axis : Axis.values()) {
if (!Double.isNaN(workPosition.get(axis))) {
position.append(axis.name()).append(formatter.format(workPosition.get(axis))).append(" ");
}
}
return StringUtils.trimToEmpty(position.toString());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ public void UGSEvent(UGSEvent cse) {

@Override
public boolean isEnabled() {
return backend.getGcodeFile() != null && backend.isConnected() && !backend.isSendingFile() && super.isEnabled();
return backend.getGcodeFile() != null && backend.isConnected() && !backend.isSendingFile();
}


Expand Down
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ public class BindActionButton extends JButton {
"Actions/Window",
"Actions/Refactoring",
"Actions/Tools",
"Actions/Edit",
"Actions/Window/SelectDocumentNode",
"Actions/Profile",
"Actions/Diff",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,9 @@ public class LocalizingService {
public final static String RunFromActionId = "com.willwinder.ugs.nbp.core.actions.RunFromAction";
public final static String RunFromCategory = CATEGORY_MACHINE;

public final static String InsertPositionTitleKey = "platform.menu.insertPosition";
public final static String InsertPositionTitle = Localization.getString(InsertPositionTitleKey, lang);

public final static String ToolboxTitle = Localization.getString("platform.plugin.toolbox.title", lang);
public final static String ToolboxTooltip = Localization.getString("platform.plugin.toolbox.tooltip", lang);
public final static String ToolboxSettingsTitle = Localization.getString("platform.plugin.toolbox.settings.title", lang);
Expand Down