Skip to content

Commit

Permalink
Simplify the NavigationControlsSettings
Browse files Browse the repository at this point in the history
* Set min/max/step in the annotation rather than via MutableModule API.
* Replace checkbox hack logic with a refresh button.
* Add a reset button to put the values back to defaults.
* Add constants for magic numeric values.
* Simplify initial control parameter value loading.
* Fix case of FPS functions to match elsewhere in the codebase.
* Downcase sciview.
  • Loading branch information
ctrueden committed Jul 22, 2023
1 parent fe268e4 commit af2d419
Showing 1 changed file with 62 additions and 130 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
import org.scijava.plugin.Parameter;
import org.scijava.plugin.Plugin;
import org.scijava.prefs.PrefService;
import org.scijava.widget.Button;
import org.scijava.widget.NumberWidget;
import sc.iview.SciView;

Expand All @@ -45,179 +46,110 @@
* A command for interactively editing step sizes and mouse sensitivity of all navigation controls.
* @author Vladimir Ulman
*/
@Plugin(type = Command.class, initializer = "setupBoundsFromSciView", menuRoot = "SciView",
@Plugin(type = Command.class, initializer = "refreshWidgets", menuRoot = "SciView",
menu = { @Menu(label = "Edit", weight = EDIT),
@Menu(label = "Settings", weight = EDIT_SETTINGS),
@Menu(label = "Controls", weight = EDIT_SETTINGS_CONTROLS) },
label = "Input Controls Step Sizes and Mouse Sensitivities")
public class NavigationControlsSettings extends InteractiveCommand
{
@Parameter
private LogService logService;
public class NavigationControlsSettings extends InteractiveCommand {

//these follow SciView.setFPSSpeed()
private static final float BASE_SPEED_INCR = 0.01f;
private static final float FAST_TO_SLOW_RATIO = 20.0f;
private static final float VERY_FAST_TO_SLOW_RATIO = 500.0f;
private static final float MOUSE_MOVE_INCR = 0.02f;
private static final float MOUSE_SCROLL_INCR = 0.3f;

@Parameter
private SciView sciView;
private SciView sciview;

@Parameter(label = "Small step size:", style = NumberWidget.SCROLL_BAR_STYLE, callback = "processSlowSpeed",
description = "How much of a world coordinate the camera moves with W,A,S,D keys or mouse right click&drag.")
@Parameter(label = "Small step size", style = NumberWidget.SCROLL_BAR_STYLE, callback = "processSlowSpeed",
description = "How much of a world coordinate the camera moves with W,A,S,D keys or mouse right click&drag.",
min = "" + SciView.FPSSPEED_MINBOUND_SLOW, max = "" + SciView.FPSSPEED_MAXBOUND_SLOW, stepSize = "" + BASE_SPEED_INCR)
private Float fpsSlowSpeed;

@Parameter(label = "Large step size:", style = NumberWidget.SCROLL_BAR_STYLE, callback = "processFastSpeed",
description = "How much of a world coordinate the camera moves with shift+ W,A,S,D keys or shift+ mouse right click&drag.")
@Parameter(label = "Large step size", style = NumberWidget.SCROLL_BAR_STYLE, callback = "processFastSpeed",
description = "How much of a world coordinate the camera moves with shift+ W,A,S,D keys or shift+ mouse right click&drag.",
min = "" + SciView.FPSSPEED_MINBOUND_FAST, max = "" + SciView.FPSSPEED_MAXBOUND_FAST,
stepSize = "" + (FAST_TO_SLOW_RATIO * BASE_SPEED_INCR))
private Float fpsFastSpeed;

@Parameter(label = "Very large step size:", style = NumberWidget.SCROLL_BAR_STYLE, callback = "processVeryFastSpeed",
description = "How much of a world coordinate the camera moves with ctrl+shift+ W,A,S,D keys.")
@Parameter(label = "Very large step size", style = NumberWidget.SCROLL_BAR_STYLE, callback = "processVeryFastSpeed",
description = "How much of a world coordinate the camera moves with ctrl+shift+ W,A,S,D keys.",
min = "" + SciView.FPSSPEED_MINBOUND_VERYFAST, max = "" + SciView.FPSSPEED_MAXBOUND_VERYFAST,
stepSize = "" + (VERY_FAST_TO_SLOW_RATIO * BASE_SPEED_INCR))
private Float fpsVeryFastSpeed;

@Parameter(label = "Adjust all step sizes together:",
@Parameter(label = "Adjust all step sizes together",
description = "When locked (checked), all step sizes above are updated simultaneously.")
private boolean adjustStepsLock = true;

@Parameter(label = "Mouse move sensitivity:", style = NumberWidget.SCROLL_BAR_STYLE, callback = "processMouseMove",
description = "Influences proportionally how much of a mouse move is required for an action in SciView.")
@Parameter(label = "Mouse move sensitivity", style = NumberWidget.SCROLL_BAR_STYLE, callback = "processMouseMove",
description = "Influences proportionally how much of a mouse move is required for an action in SciView.",
min = "" + SciView.MOUSESPEED_MINBOUND, max = "" + SciView.MOUSESPEED_MAXBOUND, stepSize = "" + MOUSE_MOVE_INCR)
private Float mouseMoveSensitivity;

@Parameter(label = "Mouse scroll sensitivity:", style = NumberWidget.SCROLL_BAR_STYLE, callback = "processMouseScroll",
description = "Influences proportionally how much of a mouse wheel scrolling is required for an action in SciView.")
@Parameter(label = "Mouse scroll sensitivity", style = NumberWidget.SCROLL_BAR_STYLE, callback = "processMouseScroll",
description = "Influences proportionally how much of a mouse wheel scrolling is required for an action in SciView.",
min = "" + SciView.MOUSESCROLL_MINBOUND, max = "" + SciView.MOUSESCROLL_MAXBOUND, stepSize = "" + MOUSE_SCROLL_INCR)
private Float mouseScrollSensitivity;


//these follow SciView.setFPSSpeed()
private final float baseSpeedIncr = 0.01f;
private final float fastToSlowRatio = 20.0f;
private final float veryFastToSlowRatio = 500.0f;
private final float mouseMoveIncr = 0.02f;
private final float mouseScrollIncr = 0.3f;

//initiates GUI, all the spinners (scroll bars)
private void setupBoundsFromSciView()
{
MutableModuleItem<Float> menuItem = getInfo().getMutableInput("fpsSlowSpeed", Float.class);
if (menuItem == null) logService.error("Should never get here: Cannot find fpsSlowSpeed param.");
//
menuItem.setMinimumValue(SciView.FPSSPEED_MINBOUND_SLOW);
menuItem.setMaximumValue(SciView.FPSSPEED_MAXBOUND_SLOW);
menuItem.setStepSize(baseSpeedIncr);

menuItem = getInfo().getMutableInput("fpsFastSpeed", Float.class);
if (menuItem == null) logService.error("Should never get here: Cannot find fpsFastSpeed param.");
//
menuItem.setMinimumValue(SciView.FPSSPEED_MINBOUND_FAST);
menuItem.setMaximumValue(SciView.FPSSPEED_MAXBOUND_FAST);
menuItem.setStepSize(fastToSlowRatio * baseSpeedIncr);

menuItem = getInfo().getMutableInput("fpsVeryFastSpeed", Float.class);
if (menuItem == null) logService.error("Should never get here: Cannot find fpsVeryFastSpeed param.");
//
menuItem.setMinimumValue(SciView.FPSSPEED_MINBOUND_VERYFAST);
menuItem.setMaximumValue(SciView.FPSSPEED_MAXBOUND_VERYFAST);
menuItem.setStepSize(veryFastToSlowRatio * baseSpeedIncr);

menuItem = getInfo().getMutableInput("mouseMoveSensitivity", Float.class);
if (menuItem == null) logService.error("Should never get here: Cannot find mouseMoveSensitivity param.");
//
menuItem.setMinimumValue(SciView.MOUSESPEED_MINBOUND);
menuItem.setMaximumValue(SciView.MOUSESPEED_MAXBOUND);
menuItem.setStepSize(mouseMoveIncr);

menuItem = getInfo().getMutableInput("mouseScrollSensitivity", Float.class);
if (menuItem == null) logService.error("Should never get here: Cannot find mouseScrollSensitivity param.");
//
menuItem.setMinimumValue(SciView.MOUSESCROLL_MINBOUND);
menuItem.setMaximumValue(SciView.MOUSESCROLL_MAXBOUND);
menuItem.setStepSize(mouseScrollIncr);

//backup the current state of SciView before we eventually override it
//so that there is something to return to with the "first toggle"
orig_fpsSlowSpeed = sciView.getControls().getParameters().getFpsSpeedSlow();
orig_fpsFastSpeed = sciView.getControls().getParameters().getFpsSpeedFast();
orig_fpsVeryFastSpeed = sciView.getControls().getParameters().getFpsSpeedVeryFast();
orig_mouseMoveSensitivity = sciView.getControls().getParameters().getMouseSpeedMult();
orig_mouseScrollSensitivity = sciView.getControls().getParameters().getMouseScrollMult();

//try to retrieve stored dialog state and push it to SciView
//so that the SciView and dialog states match
final PrefService ps = getContext().getService(PrefService.class);
if (ps == null) return;
sciView.setFPSSpeedSlow( ps.getFloat( NavigationControlsSettings.class, "fpsSlowSpeed", orig_fpsSlowSpeed) );
sciView.setFPSSpeedFast( ps.getFloat( NavigationControlsSettings.class, "fpsFastSpeed", orig_fpsFastSpeed) );
sciView.setFPSSpeedVeryFast( ps.getFloat( NavigationControlsSettings.class, "fpsVeryFastSpeed", orig_fpsVeryFastSpeed) );
sciView.setMouseSpeed( ps.getFloat( NavigationControlsSettings.class, "mouseMoveSensitivity", orig_mouseMoveSensitivity) );
sciView.setMouseScrollSpeed( ps.getFloat( NavigationControlsSettings.class, "mouseScrollSensitivity", orig_mouseScrollSensitivity) );
}

//updates GUI with fresh values
private void updateDialogSpeedsAndMouseParams()
{
fpsSlowSpeed = sciView.getControls().getParameters().getFpsSpeedSlow();
fpsFastSpeed = sciView.getControls().getParameters().getFpsSpeedFast();
fpsVeryFastSpeed = sciView.getControls().getParameters().getFpsSpeedVeryFast();
mouseMoveSensitivity = sciView.getControls().getParameters().getMouseSpeedMult();
mouseScrollSensitivity = sciView.getControls().getParameters().getMouseScrollMult();
private void refreshWidgets() {
fpsSlowSpeed = sciview.getFPSSpeedSlow();
fpsFastSpeed = sciview.getFPSSpeedFast();
fpsVeryFastSpeed = sciview.getFPSSpeedVeryFast();
mouseMoveSensitivity = sciview.getMouseSpeed();
mouseScrollSensitivity = sciview.getMouseScrollSpeed();
}


private void processSlowSpeed()
{
if (!adjustStepsLock)
sciView.setFPSSpeedSlow( fpsSlowSpeed );
if (adjustStepsLock) {
sciview.setFPSSpeed(fpsSlowSpeed);
refreshWidgets();
}
else
sciView.setFPSSpeed( fpsSlowSpeed );
updateDialogSpeedsAndMouseParams();
sciview.setFPSSpeedSlow( fpsSlowSpeed );

}
private void processFastSpeed()
{
if (!adjustStepsLock)
sciView.setFPSSpeedFast( fpsFastSpeed );
if (adjustStepsLock) {
sciview.setFPSSpeed(fpsFastSpeed / FAST_TO_SLOW_RATIO);
refreshWidgets();
}
else
sciView.setFPSSpeed( fpsFastSpeed / fastToSlowRatio );
updateDialogSpeedsAndMouseParams();
sciview.setFPSSpeedFast( fpsFastSpeed );
}
private void processVeryFastSpeed()
{
if (!adjustStepsLock)
sciView.setFPSSpeedVeryFast( fpsVeryFastSpeed );
if (adjustStepsLock) {
sciview.setFPSSpeed(fpsVeryFastSpeed / VERY_FAST_TO_SLOW_RATIO);
refreshWidgets();
}
else
sciView.setFPSSpeed( fpsVeryFastSpeed / veryFastToSlowRatio );
updateDialogSpeedsAndMouseParams();
sciview.setFPSSpeedVeryFast( fpsVeryFastSpeed );
}

private void processMouseMove()
{
sciView.setMouseSpeed( mouseMoveSensitivity );
//updateDialogSpeedsAndMouseParams();
sciview.setMouseSpeed( mouseMoveSensitivity );
}
private void processMouseScroll()
{
sciView.setMouseScrollSpeed( mouseScrollSensitivity );
//updateDialogSpeedsAndMouseParams();
sciview.setMouseScrollSpeed( mouseScrollSensitivity );
}

@Parameter(label = "Refresh", callback = "refreshWidgets",
description = "Click to refresh the dialog from sciview's current state -- useful when, e.g., step size is changed which keyboard shortcuts.")
private Button refreshButton;

@Parameter(label = "Click to re-read current state:", callback = "refreshDialog",
description = "Changing its state triggers the dialog update -- useful when, e.g., step size is changed which keyboard shortcuts.")
private boolean refreshToggle = false;
private boolean firstHitOfRefreshToggle = true; //the "first toggle" flag

private float orig_fpsSlowSpeed, orig_fpsFastSpeed, orig_fpsVeryFastSpeed;
private float orig_mouseMoveSensitivity, orig_mouseScrollSensitivity;

private void refreshDialog()
{
//only the "first toggle" will restore values in SciView
//as they were at the time this dialog started,
if (firstHitOfRefreshToggle)
{
sciView.setFPSSpeedSlow( orig_fpsSlowSpeed );
sciView.setFPSSpeedFast( orig_fpsFastSpeed );
sciView.setFPSSpeedVeryFast( orig_fpsVeryFastSpeed );
sciView.setMouseSpeed( orig_mouseMoveSensitivity );
sciView.setMouseScrollSpeed( orig_mouseScrollSensitivity );
firstHitOfRefreshToggle = false;
}
@Parameter(label = "Reset", callback = "resetValues",
description = "Click to set controls back to default values.")
private Button resetButton;

//in any case, update the dialog to the current state of SciView
updateDialogSpeedsAndMouseParams();
private void resetValues() {
sciview.controls.resetParameters();
refreshWidgets();
}
}

0 comments on commit af2d419

Please sign in to comment.