mirror of
https://github.com/DSheirer/sdrtrunk.git
synced 2026-11-20 20:45:47 -05:00
The waterfall mapped 256 palette indexes over the absolute dBFS level of the mean bin value, about 2.5 steps per dB at a -100 dBFS floor, so weak signals were only a few steps above the noise. WaterfallScaler now tracks the noise floor as a smoothed 25th percentile of the bins (robust to occupied spans) and maps a fixed 45 dB span above it, so contrast no longer depends on sample size, DFT size or tuner gain. The classic palette's flat blue blocks are replaced with a blue ramp over the bottom quarter, where the noise floor now sits. Co-authored-by: denny <dennis.sheirer@gmail.com>
1422 lines
47 KiB
Java
1422 lines
47 KiB
Java
/*
|
|
* *****************************************************************************
|
|
* Copyright (C) 2014-2026 Dennis Sheirer
|
|
*
|
|
* This program 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.
|
|
*
|
|
* This program 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 this program. If not, see <http://www.gnu.org/licenses/>
|
|
* ****************************************************************************
|
|
*/
|
|
|
|
package io.github.dsheirer.gui.spectrum;
|
|
|
|
import java.util.ArrayList;
|
|
import java.util.HashMap;
|
|
import java.util.HashSet;
|
|
import java.util.IdentityHashMap;
|
|
import java.util.LinkedHashMap;
|
|
import java.util.Map;
|
|
import java.util.Set;
|
|
import java.util.List;
|
|
import java.util.concurrent.ConcurrentLinkedQueue;
|
|
import java.util.concurrent.CopyOnWriteArrayList;
|
|
import java.util.concurrent.atomic.AtomicBoolean;
|
|
import java.util.concurrent.atomic.AtomicReference;
|
|
import javafx.animation.AnimationTimer;
|
|
import javafx.beans.property.BooleanProperty;
|
|
import javafx.beans.property.DoubleProperty;
|
|
import javafx.beans.property.IntegerProperty;
|
|
import javafx.beans.property.ObjectProperty;
|
|
import javafx.beans.property.SimpleBooleanProperty;
|
|
import javafx.beans.property.SimpleDoubleProperty;
|
|
import javafx.beans.property.SimpleIntegerProperty;
|
|
import javafx.beans.property.SimpleObjectProperty;
|
|
import javafx.beans.property.SimpleStringProperty;
|
|
import javafx.beans.property.StringProperty;
|
|
import javafx.geometry.Insets;
|
|
import javafx.geometry.Pos;
|
|
import javafx.geometry.Orientation;
|
|
import javafx.geometry.VPos;
|
|
import javafx.scene.Cursor;
|
|
import javafx.scene.canvas.Canvas;
|
|
import javafx.scene.canvas.GraphicsContext;
|
|
import javafx.scene.control.CheckMenuItem;
|
|
import javafx.scene.control.Label;
|
|
import javafx.scene.control.ContextMenu;
|
|
import javafx.scene.control.Menu;
|
|
import javafx.scene.control.MenuItem;
|
|
import javafx.scene.control.RadioMenuItem;
|
|
import javafx.scene.control.SeparatorMenuItem;
|
|
import javafx.scene.control.SplitPane;
|
|
import javafx.scene.control.ToggleGroup;
|
|
import javafx.scene.image.PixelFormat;
|
|
import javafx.scene.image.WritableImage;
|
|
import javafx.scene.input.ContextMenuEvent;
|
|
import javafx.scene.input.MouseButton;
|
|
import javafx.scene.input.MouseEvent;
|
|
import javafx.scene.input.ScrollEvent;
|
|
import javafx.scene.layout.Background;
|
|
import javafx.scene.layout.BackgroundFill;
|
|
import javafx.scene.layout.Pane;
|
|
import javafx.scene.layout.StackPane;
|
|
import javafx.scene.paint.Color;
|
|
import javafx.scene.paint.CycleMethod;
|
|
import javafx.scene.paint.LinearGradient;
|
|
import javafx.scene.paint.Stop;
|
|
import javafx.scene.text.Font;
|
|
import javafx.scene.text.TextAlignment;
|
|
|
|
/**
|
|
* JavaFX spectral display with a spectrum (frequency domain) section above a scrolling waterfall section. Either
|
|
* section can be turned off independently. Optional layers draw on top of each section to add frequency labels,
|
|
* channels, cursor readouts, etc.
|
|
*
|
|
* Send DFT results in dB to receive(float[]) from any thread. Set the frequency, sample rate and sample size with
|
|
* the model on the JavaFX application thread. The display redraws on the JavaFX pulse only when something changed.
|
|
*
|
|
* Mouse: the scroll wheel zooms around the mouse position, a primary button drag pans the zoomed display and the
|
|
* secondary button opens a context menu with each layer's menu items.
|
|
*/
|
|
public class SpectralDisplay extends StackPane
|
|
{
|
|
public static final int AVERAGING_MINIMUM = SpectrumAverager.AVERAGING_MINIMUM;
|
|
public static final int AVERAGING_MAXIMUM = SpectrumAverager.AVERAGING_MAXIMUM;
|
|
public static final int AVERAGING_DEFAULT = SpectrumAverager.AVERAGING_DEFAULT;
|
|
private static final int WATERFALL_MIN_ROWS = 256;
|
|
private static final int WATERFALL_MAX_ROWS = 4096;
|
|
private static final int WATERFALL_QUEUE_LIMIT = 256;
|
|
//Below the smallest maximum texture size of common GPUs (8192)
|
|
private static final int WATERFALL_TILE_WIDTH = 4096;
|
|
private static final Font MESSAGE_FONT = Font.font(13);
|
|
private static final Font ZOOM_FONT = Font.font(11);
|
|
|
|
private final SpectralDisplayModel mModel = new SpectralDisplayModel();
|
|
private final ObjectProperty<SpectralDisplayTheme> mTheme =
|
|
new SimpleObjectProperty<>(SpectralDisplayTheme.getDefault());
|
|
private final List<SpectralDisplayLayer> mLayers = new CopyOnWriteArrayList<>();
|
|
private final SplitPane mSplitPane = new SplitPane();
|
|
private final CanvasPane mSpectrumPane = new CanvasPane(DisplaySection.SPECTRUM);
|
|
private final CanvasPane mWaterfallPane = new CanvasPane(DisplaySection.WATERFALL);
|
|
//Each section's canvas with overlay controls stacked on top
|
|
private final StackPane mSpectrumSection = new StackPane(mSpectrumPane);
|
|
private final StackPane mWaterfallSection = new StackPane(mWaterfallPane);
|
|
private final EdgeSlider mWaterfallRangeSlider;
|
|
private final StackPane mDisabledPane = new StackPane();
|
|
|
|
private final BooleanProperty mSpectrumEnabled = new SimpleBooleanProperty(true);
|
|
private final BooleanProperty mWaterfallEnabled = new SimpleBooleanProperty(true);
|
|
private final BooleanProperty mWaterfallPaused = new SimpleBooleanProperty(false);
|
|
private final IntegerProperty mAveraging = new SimpleIntegerProperty(AVERAGING_DEFAULT);
|
|
private final BooleanProperty mPeakHold = new SimpleBooleanProperty(false);
|
|
private final DoubleProperty mDividerPosition = new SimpleDoubleProperty(0.5);
|
|
private final IntegerProperty mWaterfallRange = new SimpleIntegerProperty(WaterfallScaler.RANGE_DEFAULT);
|
|
private final IntegerProperty mWaterfallAveraging = new SimpleIntegerProperty(WaterfallScaler.AVERAGING_DEFAULT);
|
|
private final StringProperty mMessage = new SimpleStringProperty();
|
|
|
|
//Values read on the DFT results thread
|
|
private volatile boolean mSpectrumActive = true;
|
|
private volatile boolean mWaterfallActive = true;
|
|
private volatile boolean mWaterfallPausedFlag = false;
|
|
private final SpectrumAverager mAverager = new SpectrumAverager();
|
|
private final WaterfallScaler mWaterfallScaler = new WaterfallScaler();
|
|
|
|
//Hand off from the DFT results thread to the JavaFX thread
|
|
private final AtomicReference<SpectrumAverager.Frame> mLatestSpectrum = new AtomicReference<>();
|
|
private final ConcurrentLinkedQueue<byte[]> mWaterfallRows = new ConcurrentLinkedQueue<>();
|
|
private final AtomicBoolean mRenderRequested = new AtomicBoolean(true);
|
|
private final AtomicBoolean mClearRequested = new AtomicBoolean();
|
|
|
|
//JavaFX thread state
|
|
private final AnimationTimer mAnimationTimer;
|
|
private SpectrumAverager.Frame mSpectrum;
|
|
//Waterfall image split into side by side tiles, since GPUs limit texture width (as low as 8192 pixels)
|
|
private WritableImage[] mWaterfallTiles;
|
|
private int mWaterfallWidth;
|
|
private int mWaterfallHeight;
|
|
private byte[] mWaterfallLevels;
|
|
private int[] mWaterfallPixels;
|
|
//ARGB color for each waterfall level code, for the current theme and range. Null when it needs to be rebuilt.
|
|
private int[] mWaterfallColors;
|
|
private int mWaterfallWriteRow = 0;
|
|
private double[] mPolygonX = new double[0];
|
|
private double[] mPolygonY = new double[0];
|
|
private DisplayCursor mCursor;
|
|
private ContextMenu mContextMenu;
|
|
private double mDragStartX;
|
|
private double mDragStartOffset;
|
|
private boolean mDragging;
|
|
private double mScrollAccumulator;
|
|
private boolean mDisposed;
|
|
|
|
/**
|
|
* Constructs an instance.
|
|
* @param layers to draw on the display, in drawing order
|
|
*/
|
|
public SpectralDisplay(SpectralDisplayLayer... layers)
|
|
{
|
|
mSplitPane.setOrientation(Orientation.VERTICAL);
|
|
mSplitPane.getStyleClass().add("spectral-display-split-pane");
|
|
java.net.URL stylesheet = SpectralDisplay.class.getResource("/spectral_display.css");
|
|
|
|
if(stylesheet != null)
|
|
{
|
|
getStylesheets().add(stylesheet.toExternalForm());
|
|
}
|
|
|
|
Label disabledLabel = new Label("Spectrum and waterfall are turned off");
|
|
mDisabledPane.getChildren().add(disabledLabel);
|
|
mTheme.addListener((ob, ov, nv) -> themeChanged(disabledLabel));
|
|
themeChanged(disabledLabel);
|
|
getChildren().addAll(mDisabledPane, mSplitPane);
|
|
setMinSize(0, 0);
|
|
mSpectrumSection.setMinSize(0, 0);
|
|
mWaterfallSection.setMinSize(0, 0);
|
|
|
|
//Waterfall gain slider along the right edge of the waterfall. The slider value is inverted so that moving it
|
|
//up narrows the dB range, which raises the contrast of weak signals.
|
|
int rangeSum = WaterfallScaler.RANGE_MINIMUM + WaterfallScaler.RANGE_MAXIMUM;
|
|
mWaterfallRangeSlider = new EdgeSlider("Gain", "Waterfall gain: dB range from the noise floor to the top " +
|
|
"of the color palette. Higher settings use a smaller range, which gives weak signals more contrast " +
|
|
"against the noise and saturates strong signals sooner.", WaterfallScaler.RANGE_MINIMUM,
|
|
WaterfallScaler.RANGE_MAXIMUM, 1, value -> (rangeSum - Math.round(value)) + " dB");
|
|
mWaterfallRangeSlider.valueProperty().set(rangeSum - mWaterfallRange.get());
|
|
mWaterfallRangeSlider.valueProperty().addListener((ob, ov, nv) ->
|
|
mWaterfallRange.set(rangeSum - (int)Math.round(nv.doubleValue())));
|
|
mWaterfallRange.addListener((ob, ov, nv) -> {
|
|
int range = Math.max(WaterfallScaler.RANGE_MINIMUM, Math.min(WaterfallScaler.RANGE_MAXIMUM,
|
|
nv.intValue()));
|
|
|
|
if(range != nv.intValue())
|
|
{
|
|
mWaterfallRange.set(range);
|
|
return;
|
|
}
|
|
|
|
mWaterfallRangeSlider.valueProperty().set(rangeSum - range);
|
|
mWaterfallColors = null;
|
|
repaintWaterfallImage();
|
|
});
|
|
StackPane.setAlignment(mWaterfallRangeSlider, Pos.CENTER_RIGHT);
|
|
mWaterfallSection.getChildren().add(mWaterfallRangeSlider);
|
|
|
|
mWaterfallAveraging.addListener((ob, ov, nv) -> mWaterfallScaler.setAveraging(nv.intValue()));
|
|
|
|
mSpectrumEnabled.addListener((ob, ov, nv) -> updateSections());
|
|
mWaterfallEnabled.addListener((ob, ov, nv) -> updateSections());
|
|
mWaterfallPaused.addListener((ob, ov, nv) -> {
|
|
mWaterfallPausedFlag = nv;
|
|
requestRender();
|
|
});
|
|
mMessage.addListener((ob, ov, nv) -> requestRender());
|
|
mAveraging.addListener((ob, ov, nv) -> mAverager.setAveraging(nv.intValue()));
|
|
mPeakHold.addListener((ob, ov, nv) -> {
|
|
mAverager.setPeakHold(nv);
|
|
requestRender();
|
|
});
|
|
mModel.centerFrequencyProperty().addListener((ob, ov, nv) -> {
|
|
mAverager.resetPeak();
|
|
requestRender();
|
|
});
|
|
mModel.sampleRateProperty().addListener((ob, ov, nv) -> {
|
|
mAverager.resetPeak();
|
|
requestRender();
|
|
});
|
|
mModel.zoomProperty().addListener((ob, ov, nv) -> requestRender());
|
|
mModel.zoomOffsetProperty().addListener((ob, ov, nv) -> requestRender());
|
|
mModel.binCountProperty().addListener((ob, ov, nv) -> requestRender());
|
|
mModel.sampleSizeProperty().addListener((ob, ov, nv) -> requestRender());
|
|
mModel.noiseBandwidthProperty().addListener((ob, ov, nv) -> requestRender());
|
|
|
|
//Track user changes to the divider position so it can be restored after the waterfall is turned off and on
|
|
mSplitPane.getDividers().addListener((javafx.collections.ListChangeListener<SplitPane.Divider>)change -> {
|
|
while(change.next())
|
|
{
|
|
for(SplitPane.Divider divider: change.getAddedSubList())
|
|
{
|
|
divider.positionProperty().addListener((ob, ov, nv) -> {
|
|
if(mSplitPane.getItems().size() == 2)
|
|
{
|
|
mDividerPosition.set(nv.doubleValue());
|
|
}
|
|
});
|
|
}
|
|
}
|
|
});
|
|
|
|
updateSections();
|
|
|
|
for(SpectralDisplayLayer layer: layers)
|
|
{
|
|
addLayer(layer);
|
|
}
|
|
|
|
mAnimationTimer = new AnimationTimer()
|
|
{
|
|
@Override
|
|
public void handle(long now)
|
|
{
|
|
pulse();
|
|
}
|
|
};
|
|
mAnimationTimer.start();
|
|
}
|
|
|
|
/**
|
|
* Frequency and zoom model. Access on the JavaFX application thread.
|
|
*/
|
|
public SpectralDisplayModel getModel()
|
|
{
|
|
return mModel;
|
|
}
|
|
|
|
/**
|
|
* Color theme for the display, its layers and the waterfall. JavaFX thread only.
|
|
*/
|
|
public ObjectProperty<SpectralDisplayTheme> themeProperty()
|
|
{
|
|
return mTheme;
|
|
}
|
|
|
|
/**
|
|
* Current color theme. JavaFX thread only.
|
|
*/
|
|
public SpectralDisplayTheme getTheme()
|
|
{
|
|
SpectralDisplayTheme theme = mTheme.get();
|
|
return theme != null ? theme : SpectralDisplayTheme.getDefault();
|
|
}
|
|
|
|
/**
|
|
* Applies a new theme: recolors the placeholder and the waterfall history, then redraws.
|
|
*/
|
|
private void themeChanged(Label disabledLabel)
|
|
{
|
|
SpectralDisplayTheme theme = getTheme();
|
|
mDisabledPane.setBackground(new Background(new BackgroundFill(theme.getBackground(), null, null)));
|
|
disabledLabel.setTextFill(theme.getLine());
|
|
mWaterfallColors = null;
|
|
repaintWaterfallImage();
|
|
requestRender();
|
|
}
|
|
|
|
/**
|
|
* Adds a layer that draws after the layers already added.
|
|
*/
|
|
public void addLayer(SpectralDisplayLayer layer)
|
|
{
|
|
if(layer != null && !mLayers.contains(layer))
|
|
{
|
|
mLayers.add(layer);
|
|
layer.attach(this);
|
|
requestRender();
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Removes the layer from the display.
|
|
*/
|
|
public void removeLayer(SpectralDisplayLayer layer)
|
|
{
|
|
if(mLayers.remove(layer))
|
|
{
|
|
layer.detach();
|
|
requestRender();
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Indicates if a layer draws its own callout in place of the cursor readout for the mouse position.
|
|
* @param cursor mouse position
|
|
* @param area of the section under the mouse
|
|
*/
|
|
public boolean isCursorReadoutReplaced(DisplayCursor cursor, RenderArea area)
|
|
{
|
|
for(SpectralDisplayLayer layer: mLayers)
|
|
{
|
|
if(layer.replacesCursorReadout(cursor, area))
|
|
{
|
|
return true;
|
|
}
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
/**
|
|
* Layers in drawing order.
|
|
*/
|
|
public List<SpectralDisplayLayer> getLayers()
|
|
{
|
|
return new ArrayList<>(mLayers);
|
|
}
|
|
|
|
/**
|
|
* Turns the spectrum section on or off.
|
|
*/
|
|
public BooleanProperty spectrumEnabledProperty()
|
|
{
|
|
return mSpectrumEnabled;
|
|
}
|
|
|
|
/**
|
|
* Turns the waterfall section on or off.
|
|
*/
|
|
public BooleanProperty waterfallEnabledProperty()
|
|
{
|
|
return mWaterfallEnabled;
|
|
}
|
|
|
|
/**
|
|
* Pauses the waterfall scrolling. New DFT results are dropped while paused.
|
|
*/
|
|
public BooleanProperty waterfallPausedProperty()
|
|
{
|
|
return mWaterfallPaused;
|
|
}
|
|
|
|
/**
|
|
* Spectrum trace averaging, from AVERAGING_MINIMUM (fast, no averaging) to AVERAGING_MAXIMUM (slow).
|
|
* @see SpectrumAverager
|
|
*/
|
|
public IntegerProperty averagingProperty()
|
|
{
|
|
return mAveraging;
|
|
}
|
|
|
|
/**
|
|
* Shows a peak hold trace: the highest recent value of each bin, falling slowly.
|
|
*/
|
|
public BooleanProperty peakHoldProperty()
|
|
{
|
|
return mPeakHold;
|
|
}
|
|
|
|
/**
|
|
* Waterfall dB range from the noise floor to the top of the color palette, from WaterfallScaler.RANGE_MINIMUM to
|
|
* RANGE_MAXIMUM. Smaller ranges give weak signals more contrast. JavaFX thread only.
|
|
*/
|
|
public IntegerProperty waterfallRangeProperty()
|
|
{
|
|
return mWaterfallRange;
|
|
}
|
|
|
|
/**
|
|
* Number of DFT frames averaged into each waterfall row, from WaterfallScaler.AVERAGING_MINIMUM to
|
|
* AVERAGING_MAXIMUM. Averaging steadies the noise floor so weak signals stand out, but the waterfall scrolls
|
|
* more slowly.
|
|
*/
|
|
public IntegerProperty waterfallAveragingProperty()
|
|
{
|
|
return mWaterfallAveraging;
|
|
}
|
|
|
|
/**
|
|
* Waterfall gain slider along the right edge of the waterfall section.
|
|
*/
|
|
public EdgeSlider getWaterfallRangeSlider()
|
|
{
|
|
return mWaterfallRangeSlider;
|
|
}
|
|
|
|
/**
|
|
* Adds a control on top of the spectrum or the waterfall section, for example an EdgeSlider. Set its StackPane
|
|
* alignment and margin before adding it. The control is hidden with its section.
|
|
*/
|
|
public void addSectionOverlay(DisplaySection section, javafx.scene.Node node)
|
|
{
|
|
(section == DisplaySection.SPECTRUM ? mSpectrumSection : mWaterfallSection).getChildren().add(node);
|
|
}
|
|
|
|
/**
|
|
* Divider position (0 - 1.0) between the spectrum and the waterfall when both are showing.
|
|
*/
|
|
public DoubleProperty dividerPositionProperty()
|
|
{
|
|
return mDividerPosition;
|
|
}
|
|
|
|
/**
|
|
* Message shown in the middle of the display, for example when no tuner is selected. Null for no message.
|
|
*/
|
|
public StringProperty messageProperty()
|
|
{
|
|
return mMessage;
|
|
}
|
|
|
|
/**
|
|
* Mouse cursor over the display, or null when the mouse is not over the display. JavaFX thread only.
|
|
*/
|
|
public DisplayCursor getDisplayCursor()
|
|
{
|
|
return mCursor;
|
|
}
|
|
|
|
/**
|
|
* Asks the display to redraw on the next pulse. Safe to call from any thread.
|
|
*/
|
|
public void requestRender()
|
|
{
|
|
mRenderRequested.set(true);
|
|
}
|
|
|
|
/**
|
|
* Clears the spectrum trace, the averaging history and the waterfall. Safe to call from any thread.
|
|
*/
|
|
public void clear()
|
|
{
|
|
mAverager.reset();
|
|
mWaterfallScaler.reset();
|
|
|
|
mLatestSpectrum.set(null);
|
|
mWaterfallRows.clear();
|
|
mClearRequested.set(true);
|
|
requestRender();
|
|
}
|
|
|
|
/**
|
|
* Receives DFT results in dB (0 dB is full scale) in display order. Safe to call from any thread.
|
|
*/
|
|
public void receive(float[] bins)
|
|
{
|
|
if(bins == null || bins.length == 0 || mDisposed)
|
|
{
|
|
return;
|
|
}
|
|
|
|
//The first DFT results on startup can be NaN or infinite - drop them. Replacing them with zeros would add
|
|
//full scale (0 dBFS) values to the spectrum averaging and the waterfall floor tracking.
|
|
if(Float.isNaN(bins[0]) || Float.isInfinite(bins[0]))
|
|
{
|
|
return;
|
|
}
|
|
|
|
if(mSpectrumActive)
|
|
{
|
|
mLatestSpectrum.set(mAverager.process(bins));
|
|
}
|
|
|
|
if(mWaterfallActive && !mWaterfallPausedFlag)
|
|
{
|
|
byte[] row = mWaterfallScaler.process(bins);
|
|
|
|
if(row != null)
|
|
{
|
|
mWaterfallRows.offer(row);
|
|
|
|
while(mWaterfallRows.size() > WATERFALL_QUEUE_LIMIT)
|
|
{
|
|
mWaterfallRows.poll();
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Stops rendering and releases the layers and listeners.
|
|
*/
|
|
public void dispose()
|
|
{
|
|
mDisposed = true;
|
|
mAnimationTimer.stop();
|
|
|
|
for(SpectralDisplayLayer layer: mLayers)
|
|
{
|
|
layer.detach();
|
|
layer.dispose();
|
|
}
|
|
|
|
mLayers.clear();
|
|
mLatestSpectrum.set(null);
|
|
mWaterfallRows.clear();
|
|
mWaterfallTiles = null;
|
|
mWaterfallLevels = null;
|
|
}
|
|
|
|
/**
|
|
* Updates the split pane to show the enabled sections.
|
|
*/
|
|
private void updateSections()
|
|
{
|
|
boolean spectrum = mSpectrumEnabled.get();
|
|
boolean waterfall = mWaterfallEnabled.get();
|
|
mSpectrumActive = spectrum;
|
|
mWaterfallActive = waterfall;
|
|
|
|
List<StackPane> items = new ArrayList<>();
|
|
|
|
if(spectrum)
|
|
{
|
|
items.add(mSpectrumSection);
|
|
}
|
|
else
|
|
{
|
|
mLatestSpectrum.set(null);
|
|
mSpectrum = null;
|
|
}
|
|
|
|
if(waterfall)
|
|
{
|
|
items.add(mWaterfallSection);
|
|
}
|
|
else
|
|
{
|
|
mWaterfallRows.clear();
|
|
}
|
|
|
|
//Keep the waterfall slider below the configuration panel tab when the waterfall is at the top
|
|
StackPane.setMargin(mWaterfallRangeSlider, new Insets(spectrum ? 8 : 44, 0, 8, 0));
|
|
|
|
mSplitPane.getItems().setAll(items);
|
|
mSplitPane.setVisible(!items.isEmpty());
|
|
|
|
if(items.size() == 2)
|
|
{
|
|
mSplitPane.setDividerPositions(mDividerPosition.get());
|
|
}
|
|
|
|
if(mCursor != null && ((mCursor.section() == DisplaySection.SPECTRUM && !spectrum) ||
|
|
(mCursor.section() == DisplaySection.WATERFALL && !waterfall)))
|
|
{
|
|
mCursor = null;
|
|
}
|
|
|
|
requestRender();
|
|
}
|
|
|
|
/**
|
|
* Called each JavaFX pulse to move new data to the display and redraw when needed.
|
|
*/
|
|
private void pulse()
|
|
{
|
|
boolean render = mRenderRequested.getAndSet(false);
|
|
|
|
if(mClearRequested.getAndSet(false))
|
|
{
|
|
mSpectrum = null;
|
|
|
|
if(mWaterfallTiles != null)
|
|
{
|
|
mWaterfallTiles = null;
|
|
mWaterfallLevels = null;
|
|
mWaterfallWriteRow = 0;
|
|
}
|
|
|
|
render = true;
|
|
}
|
|
|
|
SpectrumAverager.Frame spectrum = mLatestSpectrum.getAndSet(null);
|
|
|
|
if(spectrum != null)
|
|
{
|
|
mSpectrum = spectrum;
|
|
mModel.setBinCount(spectrum.average().length);
|
|
render = true;
|
|
}
|
|
|
|
byte[] row;
|
|
boolean waterfallUpdated = false;
|
|
|
|
while((row = mWaterfallRows.poll()) != null)
|
|
{
|
|
if(!mWaterfallPaused.get())
|
|
{
|
|
mModel.setBinCount(row.length);
|
|
writeWaterfallRow(row);
|
|
waterfallUpdated = true;
|
|
}
|
|
}
|
|
|
|
if(render || waterfallUpdated)
|
|
{
|
|
renderSpectrum();
|
|
renderWaterfall();
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Writes the row into the waterfall ring buffer. The newest row is at mWaterfallWriteRow and older rows follow
|
|
* it, wrapping around the end of the image. The level codes are kept alongside the image so the history
|
|
* can be recolored when the theme or the waterfall range changes.
|
|
*/
|
|
private void writeWaterfallRow(byte[] row)
|
|
{
|
|
int rows = getWaterfallRowsNeeded();
|
|
|
|
if(mWaterfallTiles == null || mWaterfallWidth != row.length)
|
|
{
|
|
mWaterfallLevels = new byte[row.length * rows];
|
|
createWaterfallTiles(row.length, rows);
|
|
mWaterfallWriteRow = 0;
|
|
repaintWaterfallImage();
|
|
}
|
|
else if(mWaterfallHeight < rows)
|
|
{
|
|
growWaterfall(rows);
|
|
}
|
|
|
|
int width = row.length;
|
|
mWaterfallWriteRow = (mWaterfallWriteRow - 1 + mWaterfallHeight) % mWaterfallHeight;
|
|
System.arraycopy(row, 0, mWaterfallLevels, mWaterfallWriteRow * width, width);
|
|
writeWaterfallPixels(mWaterfallWriteRow);
|
|
}
|
|
|
|
/**
|
|
* Creates the waterfall image tiles, each at most WATERFALL_TILE_WIDTH wide.
|
|
*/
|
|
private void createWaterfallTiles(int width, int height)
|
|
{
|
|
int count = (width + WATERFALL_TILE_WIDTH - 1) / WATERFALL_TILE_WIDTH;
|
|
mWaterfallTiles = new WritableImage[count];
|
|
|
|
for(int tile = 0; tile < count; tile++)
|
|
{
|
|
int tileWidth = Math.min(WATERFALL_TILE_WIDTH, width - (tile * WATERFALL_TILE_WIDTH));
|
|
mWaterfallTiles[tile] = new WritableImage(tileWidth, height);
|
|
}
|
|
|
|
mWaterfallWidth = width;
|
|
mWaterfallHeight = height;
|
|
}
|
|
|
|
/**
|
|
* Colors one row of the waterfall image tiles from the stored level codes.
|
|
*/
|
|
private void writeWaterfallPixels(int imageRow)
|
|
{
|
|
int width = mWaterfallWidth;
|
|
|
|
if(mWaterfallPixels == null || mWaterfallPixels.length != width)
|
|
{
|
|
mWaterfallPixels = new int[width];
|
|
}
|
|
|
|
int[] colors = getWaterfallColors();
|
|
int offset = imageRow * width;
|
|
|
|
for(int x = 0; x < width; x++)
|
|
{
|
|
mWaterfallPixels[x] = colors[mWaterfallLevels[offset + x] & 0xFF];
|
|
}
|
|
|
|
for(int tile = 0; tile < mWaterfallTiles.length; tile++)
|
|
{
|
|
WritableImage image = mWaterfallTiles[tile];
|
|
mWaterfallTiles[tile].getPixelWriter().setPixels(0, imageRow, (int)image.getWidth(), 1,
|
|
PixelFormat.getIntArgbInstance(), mWaterfallPixels, tile * WATERFALL_TILE_WIDTH, width);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* ARGB color for each waterfall level code, for the current theme and range.
|
|
*/
|
|
private int[] getWaterfallColors()
|
|
{
|
|
if(mWaterfallColors == null)
|
|
{
|
|
SpectralDisplayTheme theme = getTheme();
|
|
int range = mWaterfallRange.get();
|
|
int[] colors = new int[256];
|
|
|
|
for(int level = 0; level < colors.length; level++)
|
|
{
|
|
colors[level] = theme.getWaterfallColor(WaterfallScaler.paletteIndex(level, range));
|
|
}
|
|
|
|
mWaterfallColors = colors;
|
|
}
|
|
|
|
return mWaterfallColors;
|
|
}
|
|
|
|
/**
|
|
* Recolors the whole waterfall image from the stored level codes using the current theme and range.
|
|
*/
|
|
private void repaintWaterfallImage()
|
|
{
|
|
if(mWaterfallTiles != null && mWaterfallLevels != null)
|
|
{
|
|
for(int y = 0; y < mWaterfallHeight; y++)
|
|
{
|
|
writeWaterfallPixels(y);
|
|
}
|
|
|
|
requestRender();
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Number of waterfall history rows to keep, based on the height of the waterfall section.
|
|
*/
|
|
private int getWaterfallRowsNeeded()
|
|
{
|
|
int rows = (int)Math.ceil(mWaterfallPane.getHeight());
|
|
return Math.max(WATERFALL_MIN_ROWS, Math.min(WATERFALL_MAX_ROWS, rows));
|
|
}
|
|
|
|
/**
|
|
* Replaces the waterfall with a taller one and copies the history in newest to oldest order.
|
|
*/
|
|
private void growWaterfall(int rows)
|
|
{
|
|
int width = mWaterfallWidth;
|
|
int previousHeight = mWaterfallHeight;
|
|
byte[] indexes = new byte[width * rows];
|
|
|
|
for(int y = 0; y < previousHeight; y++)
|
|
{
|
|
int source = (mWaterfallWriteRow + y) % previousHeight;
|
|
System.arraycopy(mWaterfallLevels, source * width, indexes, y * width, width);
|
|
}
|
|
|
|
mWaterfallLevels = indexes;
|
|
createWaterfallTiles(width, rows);
|
|
mWaterfallWriteRow = 0;
|
|
repaintWaterfallImage();
|
|
}
|
|
|
|
/**
|
|
* Height of the spectrum plot area above the inset reserved by the layers.
|
|
*/
|
|
private double getSpectrumInset()
|
|
{
|
|
double inset = 0;
|
|
|
|
for(SpectralDisplayLayer layer: mLayers)
|
|
{
|
|
inset = Math.max(inset, layer.getSpectrumBottomInset());
|
|
}
|
|
|
|
return inset;
|
|
}
|
|
|
|
private void renderSpectrum()
|
|
{
|
|
if(!mSpectrumEnabled.get())
|
|
{
|
|
return;
|
|
}
|
|
|
|
Canvas canvas = mSpectrumPane.getCanvas();
|
|
double width = canvas.getWidth();
|
|
double height = canvas.getHeight();
|
|
|
|
if(width < 1 || height < 1)
|
|
{
|
|
return;
|
|
}
|
|
|
|
GraphicsContext gc = canvas.getGraphicsContext2D();
|
|
double plotHeight = Math.max(0, height - getSpectrumInset());
|
|
RenderArea area = new RenderArea(width, height, plotHeight);
|
|
|
|
gc.setFill(getTheme().getBackground());
|
|
gc.fillRect(0, 0, width, height);
|
|
|
|
for(SpectralDisplayLayer layer: mLayers)
|
|
{
|
|
gc.save();
|
|
layer.renderSpectrumBackground(gc, area);
|
|
gc.restore();
|
|
}
|
|
|
|
if(mSpectrum != null && mModel.hasSource() && plotHeight > 0)
|
|
{
|
|
renderTrace(gc, area, mSpectrum.average());
|
|
|
|
if(mSpectrum.peak() != null && mPeakHold.get())
|
|
{
|
|
renderPeak(gc, area, mSpectrum.peak());
|
|
}
|
|
}
|
|
|
|
gc.setStroke(getTheme().getLine());
|
|
gc.setLineWidth(1.0);
|
|
gc.strokeLine(0, Math.floor(plotHeight) + 0.5, width, Math.floor(plotHeight) + 0.5);
|
|
|
|
for(SpectralDisplayLayer layer: mLayers)
|
|
{
|
|
gc.save();
|
|
layer.renderSpectrumForeground(gc, area);
|
|
gc.restore();
|
|
}
|
|
|
|
renderZoomIndicator(gc, 8, 8);
|
|
renderMessage(gc, area);
|
|
}
|
|
|
|
/**
|
|
* Draws the filled spectrum trace.
|
|
*/
|
|
private void renderTrace(GraphicsContext gc, RenderArea area, float[] bins)
|
|
{
|
|
double width = area.width();
|
|
double plotHeight = area.plotHeight();
|
|
int count = buildTracePoints(bins, width, plotHeight);
|
|
|
|
LinearGradient gradient = new LinearGradient(0, plotHeight / 2.0, 0, area.height(), false,
|
|
CycleMethod.NO_CYCLE, new Stop(0, getTheme().getGradientTop()),
|
|
new Stop(1, getTheme().getGradientBottom()));
|
|
|
|
gc.save();
|
|
gc.beginPath();
|
|
gc.rect(0, 0, width, plotHeight);
|
|
gc.clip();
|
|
gc.setFill(gradient);
|
|
gc.fillPolygon(mPolygonX, mPolygonY, count);
|
|
gc.setStroke(getTheme().getTrace());
|
|
gc.setLineWidth(1.0);
|
|
strokeTracePoints(gc, count);
|
|
gc.restore();
|
|
}
|
|
|
|
/**
|
|
* Draws the peak hold trace as a line.
|
|
*/
|
|
private void renderPeak(GraphicsContext gc, RenderArea area, float[] bins)
|
|
{
|
|
double width = area.width();
|
|
double plotHeight = area.plotHeight();
|
|
int count = buildTracePoints(bins, width, plotHeight);
|
|
Color cursor = getTheme().getCursor();
|
|
|
|
gc.save();
|
|
gc.beginPath();
|
|
gc.rect(0, 0, width, plotHeight);
|
|
gc.clip();
|
|
gc.setStroke(Color.color(cursor.getRed(), cursor.getGreen(), cursor.getBlue(), 0.75));
|
|
gc.setLineWidth(1.0);
|
|
strokeTracePoints(gc, count);
|
|
gc.restore();
|
|
}
|
|
|
|
/**
|
|
* Strokes the trace points, skipping the two base points so the line only follows the trace.
|
|
*/
|
|
private void strokeTracePoints(GraphicsContext gc, int count)
|
|
{
|
|
gc.beginPath();
|
|
gc.moveTo(mPolygonX[1], mPolygonY[1]);
|
|
|
|
for(int x = 2; x < count - 1; x++)
|
|
{
|
|
gc.lineTo(mPolygonX[x], mPolygonY[x]);
|
|
}
|
|
|
|
gc.stroke();
|
|
}
|
|
|
|
/**
|
|
* Fills the polygon point arrays with the trace for the visible bins, starting and ending with points on the
|
|
* bottom of the plot. When there are more visible bins than pixels, each pixel column uses the largest bin value
|
|
* in that column so narrow signals stay visible.
|
|
* @return number of points
|
|
*/
|
|
private int buildTracePoints(float[] bins, double width, double plotHeight)
|
|
{
|
|
double scale = plotHeight / mModel.getDynamicRange();
|
|
double offset = mModel.getZoomOffset();
|
|
int visible = mModel.getVisibleBinCount();
|
|
double pixelsPerBin = mModel.getPixelsPerBin(width);
|
|
int count;
|
|
|
|
if(pixelsPerBin >= 1.0)
|
|
{
|
|
int first = Math.max(0, (int)Math.floor(offset) - 1);
|
|
int last = Math.min(bins.length - 1, (int)Math.ceil(offset + visible) + 1);
|
|
ensurePolygonCapacity(last - first + 5);
|
|
count = 0;
|
|
mPolygonX[count] = 0;
|
|
mPolygonY[count++] = plotHeight;
|
|
|
|
for(int bin = first; bin <= last; bin++)
|
|
{
|
|
mPolygonX[count] = (bin - offset + 0.5) * pixelsPerBin;
|
|
mPolygonY[count++] = getY(bins[bin], scale, plotHeight);
|
|
}
|
|
}
|
|
else
|
|
{
|
|
int columns = (int)Math.ceil(width);
|
|
ensurePolygonCapacity(columns + 5);
|
|
count = 0;
|
|
mPolygonX[count] = 0;
|
|
mPolygonY[count++] = plotHeight;
|
|
double binsPerPixel = 1.0 / pixelsPerBin;
|
|
|
|
for(int column = 0; column < columns; column++)
|
|
{
|
|
int start = (int)Math.floor(offset + (column * binsPerPixel));
|
|
int end = (int)Math.floor(offset + ((column + 1) * binsPerPixel));
|
|
start = Math.max(0, Math.min(bins.length - 1, start));
|
|
end = Math.max(start + 1, Math.min(bins.length, end));
|
|
float max = bins[start];
|
|
|
|
for(int bin = start + 1; bin < end; bin++)
|
|
{
|
|
if(bins[bin] > max)
|
|
{
|
|
max = bins[bin];
|
|
}
|
|
}
|
|
|
|
mPolygonX[count] = column + 0.5;
|
|
mPolygonY[count++] = getY(max, scale, plotHeight);
|
|
}
|
|
}
|
|
|
|
mPolygonX[count] = width;
|
|
mPolygonY[count++] = plotHeight;
|
|
return count;
|
|
}
|
|
|
|
private static double getY(float decibels, double scale, double plotHeight)
|
|
{
|
|
double y = -decibels * scale;
|
|
return Math.max(0, Math.min(plotHeight, y));
|
|
}
|
|
|
|
private void ensurePolygonCapacity(int size)
|
|
{
|
|
if(mPolygonX.length < size)
|
|
{
|
|
mPolygonX = new double[size];
|
|
mPolygonY = new double[size];
|
|
}
|
|
}
|
|
|
|
private void renderWaterfall()
|
|
{
|
|
if(!mWaterfallEnabled.get())
|
|
{
|
|
return;
|
|
}
|
|
|
|
Canvas canvas = mWaterfallPane.getCanvas();
|
|
double width = canvas.getWidth();
|
|
double height = canvas.getHeight();
|
|
|
|
if(width < 1 || height < 1)
|
|
{
|
|
return;
|
|
}
|
|
|
|
GraphicsContext gc = canvas.getGraphicsContext2D();
|
|
gc.setFill(getTheme().getWaterfallBackground());
|
|
gc.fillRect(0, 0, width, height);
|
|
|
|
WritableImage[] tiles = mWaterfallTiles;
|
|
|
|
if(tiles != null && mModel.hasSource() && mWaterfallWidth == mModel.getBinCount())
|
|
{
|
|
double visibleStart = mModel.getZoomOffset();
|
|
double visibleEnd = visibleStart + mModel.getVisibleBinCount();
|
|
double pixelsPerBin = mModel.getPixelsPerBin(width);
|
|
gc.setImageSmoothing(pixelsPerBin < 1.0);
|
|
|
|
//Newest rows run from the write row to the bottom of the image, then older rows wrap to the top
|
|
double firstRows = Math.min(mWaterfallHeight - mWaterfallWriteRow, height);
|
|
double secondRows = Math.min(mWaterfallWriteRow, height - firstRows);
|
|
|
|
for(int tile = 0; tile < tiles.length; tile++)
|
|
{
|
|
double tileStart = tile * WATERFALL_TILE_WIDTH;
|
|
double start = Math.max(visibleStart, tileStart);
|
|
double end = Math.min(visibleEnd, tileStart + tiles[tile].getWidth());
|
|
|
|
if(start >= end)
|
|
{
|
|
continue;
|
|
}
|
|
|
|
double sourceX = start - tileStart;
|
|
double sourceWidth = end - start;
|
|
double destinationX = (start - visibleStart) * pixelsPerBin;
|
|
double destinationWidth = ((end - visibleStart) * pixelsPerBin) - destinationX;
|
|
|
|
gc.drawImage(tiles[tile], sourceX, mWaterfallWriteRow, sourceWidth, firstRows, destinationX, 0,
|
|
destinationWidth, firstRows);
|
|
|
|
if(secondRows > 0)
|
|
{
|
|
gc.drawImage(tiles[tile], sourceX, 0, sourceWidth, secondRows, destinationX, firstRows,
|
|
destinationWidth, secondRows);
|
|
}
|
|
}
|
|
}
|
|
|
|
RenderArea area = new RenderArea(width, height, height);
|
|
|
|
for(SpectralDisplayLayer layer: mLayers)
|
|
{
|
|
gc.save();
|
|
layer.renderWaterfall(gc, area);
|
|
gc.restore();
|
|
}
|
|
|
|
if(mWaterfallPaused.get())
|
|
{
|
|
gc.setFill(getTheme().getCursor());
|
|
gc.setFont(MESSAGE_FONT);
|
|
gc.setTextAlign(TextAlignment.CENTER);
|
|
gc.setTextBaseline(VPos.TOP);
|
|
gc.fillText("PAUSED", width / 2.0, 8);
|
|
}
|
|
|
|
if(!mSpectrumEnabled.get())
|
|
{
|
|
renderZoomIndicator(gc, 8, 8);
|
|
renderMessage(gc, area);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Draws a small overview bar that shows the zoom window position within the full bandwidth.
|
|
*/
|
|
private void renderZoomIndicator(GraphicsContext gc, double x, double y)
|
|
{
|
|
if(mModel.getZoom() == 0 || y < 0)
|
|
{
|
|
return;
|
|
}
|
|
|
|
double width = 80;
|
|
double height = 8;
|
|
double windowX = x + (mModel.getZoomOffset() / mModel.getBinCount() * width);
|
|
double windowWidth = Math.max(2, width / mModel.getZoomMultiplier());
|
|
|
|
gc.setFill(getTheme().getBackground(0.7));
|
|
gc.fillRoundRect(x - 4, y - 4, width + 68, height + 8, 6, 6);
|
|
gc.setStroke(getTheme().getLine());
|
|
gc.setFill(getTheme().getCursor());
|
|
gc.setLineWidth(1.0);
|
|
gc.strokeRect(x + 0.5, y + 0.5, width, height);
|
|
gc.fillRect(windowX + 0.5, y + 0.5, windowWidth, height);
|
|
gc.setFill(getTheme().getLine());
|
|
gc.setFont(ZOOM_FONT);
|
|
gc.setTextAlign(TextAlignment.LEFT);
|
|
gc.setTextBaseline(VPos.CENTER);
|
|
gc.fillText("Zoom " + mModel.getZoomMultiplier() + "x", x + width + 6, y + (height / 2.0) + 0.5);
|
|
}
|
|
|
|
private void renderMessage(GraphicsContext gc, RenderArea area)
|
|
{
|
|
String message = mMessage.get();
|
|
|
|
if(message != null && !message.isEmpty())
|
|
{
|
|
gc.setFill(getTheme().getCursor());
|
|
gc.setFont(MESSAGE_FONT);
|
|
gc.setTextAlign(TextAlignment.CENTER);
|
|
gc.setTextBaseline(VPos.CENTER);
|
|
gc.fillText(message, area.width() / 2.0, area.plotHeight() / 2.0);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Updates the mouse cursor position.
|
|
*/
|
|
private void updateCursor(DisplaySection section, double x, double y, double width)
|
|
{
|
|
mCursor = new DisplayCursor(section, x, y, mModel.getFrequency(x, width));
|
|
requestRender();
|
|
}
|
|
|
|
private void handleMouse(CanvasPane pane, MouseEvent event)
|
|
{
|
|
double width = pane.getWidth();
|
|
|
|
if(event.getEventType() == MouseEvent.MOUSE_MOVED)
|
|
{
|
|
updateCursor(pane.getSection(), event.getX(), event.getY(), width);
|
|
}
|
|
else if(event.getEventType() == MouseEvent.MOUSE_EXITED)
|
|
{
|
|
if(!mDragging)
|
|
{
|
|
mCursor = null;
|
|
requestRender();
|
|
}
|
|
}
|
|
else if(event.getEventType() == MouseEvent.MOUSE_PRESSED)
|
|
{
|
|
if(mContextMenu != null)
|
|
{
|
|
mContextMenu.hide();
|
|
}
|
|
|
|
if(event.getButton() == MouseButton.PRIMARY)
|
|
{
|
|
mDragStartX = event.getX();
|
|
mDragStartOffset = mModel.getZoomOffset();
|
|
}
|
|
}
|
|
else if(event.getEventType() == MouseEvent.MOUSE_DRAGGED)
|
|
{
|
|
if(event.getButton() == MouseButton.PRIMARY && mModel.getZoom() > 0)
|
|
{
|
|
mDragging = true;
|
|
pane.setCursor(Cursor.CLOSED_HAND);
|
|
double bins = (mDragStartX - event.getX()) / mModel.getPixelsPerBin(width);
|
|
mModel.setZoomOffset(mDragStartOffset + bins);
|
|
}
|
|
|
|
updateCursor(pane.getSection(), event.getX(), event.getY(), width);
|
|
}
|
|
else if(event.getEventType() == MouseEvent.MOUSE_RELEASED)
|
|
{
|
|
if(mDragging)
|
|
{
|
|
mDragging = false;
|
|
pane.setCursor(Cursor.CROSSHAIR);
|
|
}
|
|
}
|
|
}
|
|
|
|
private void handleScroll(CanvasPane pane, ScrollEvent event)
|
|
{
|
|
double notch = event.getMultiplierY() > 0 ? event.getMultiplierY() : 40.0;
|
|
mScrollAccumulator += event.getDeltaY();
|
|
int steps = (int)(mScrollAccumulator / notch);
|
|
|
|
if(steps != 0)
|
|
{
|
|
mScrollAccumulator -= steps * notch;
|
|
double width = pane.getWidth();
|
|
long frequency = mModel.getFrequency(event.getX(), width);
|
|
mModel.setZoom(mModel.getZoom() + steps, frequency, width > 0 ? event.getX() / width : 0.5);
|
|
updateCursor(pane.getSection(), event.getX(), event.getY(), width);
|
|
}
|
|
|
|
event.consume();
|
|
}
|
|
|
|
private void showContextMenu(CanvasPane pane, ContextMenuEvent event)
|
|
{
|
|
if(mContextMenu != null)
|
|
{
|
|
mContextMenu.hide();
|
|
}
|
|
|
|
double width = pane.getWidth();
|
|
long frequency = mModel.getFrequency(event.getX(), width);
|
|
double xFraction = width > 0 ? event.getX() / width : 0.5;
|
|
ContextMenu menu = new ContextMenu();
|
|
|
|
//Items of the layers grouped by section, in layer order. A layer without a section is a group of its own.
|
|
Map<Object,List<MenuItem>> groups = new LinkedHashMap<>();
|
|
Map<Object,Map<Object,MenuItem>> listed = new HashMap<>();
|
|
|
|
for(SpectralDisplayLayer layer: mLayers)
|
|
{
|
|
List<MenuItem> items = layer.getContextMenuItems(pane.getSection(), frequency);
|
|
|
|
if(items == null || items.isEmpty())
|
|
{
|
|
continue;
|
|
}
|
|
|
|
Object key = layer.getContextMenuSection() != null ? layer.getContextMenuSection() : layer;
|
|
List<MenuItem> group = groups.computeIfAbsent(key, k -> new ArrayList<>());
|
|
Map<Object,MenuItem> keys = listed.computeIfAbsent(key, k -> new IdentityHashMap<>());
|
|
|
|
for(MenuItem item: items)
|
|
{
|
|
MenuItem earlier = item.getUserData() != null ? keys.get(item.getUserData()) : null;
|
|
|
|
if(earlier != null)
|
|
{
|
|
//The same channel from another layer is listed once, with the other layer's options added
|
|
mergeOptions(earlier, item);
|
|
}
|
|
else
|
|
{
|
|
if(item.getUserData() != null)
|
|
{
|
|
keys.put(item.getUserData(), item);
|
|
}
|
|
|
|
group.add(item);
|
|
}
|
|
}
|
|
}
|
|
|
|
for(Map.Entry<Object,List<MenuItem>> group: groups.entrySet())
|
|
{
|
|
if(group.getKey() instanceof String section)
|
|
{
|
|
menu.getItems().add(SpectralDisplayLayer.createSectionHeader(section));
|
|
}
|
|
|
|
menu.getItems().addAll(group.getValue());
|
|
menu.getItems().add(new SeparatorMenuItem());
|
|
}
|
|
|
|
Menu zoomMenu = new Menu("Zoom");
|
|
ToggleGroup zoomGroup = new ToggleGroup();
|
|
|
|
for(int zoom = SpectralDisplayModel.MIN_ZOOM; zoom <= SpectralDisplayModel.MAX_ZOOM; zoom++)
|
|
{
|
|
final int level = zoom;
|
|
RadioMenuItem item = new RadioMenuItem((1 << zoom) + "x");
|
|
item.setToggleGroup(zoomGroup);
|
|
item.setSelected(mModel.getZoom() == zoom);
|
|
item.setOnAction(e -> mModel.setZoom(level, frequency, xFraction));
|
|
zoomMenu.getItems().add(item);
|
|
}
|
|
|
|
menu.getItems().add(zoomMenu);
|
|
|
|
if(mWaterfallEnabled.get())
|
|
{
|
|
CheckMenuItem pause = new CheckMenuItem("Pause Waterfall");
|
|
pause.setSelected(mWaterfallPaused.get());
|
|
pause.setOnAction(e -> mWaterfallPaused.set(pause.isSelected()));
|
|
menu.getItems().add(pause);
|
|
}
|
|
|
|
alignLabels(menu.getItems());
|
|
ChannelMenus.style(menu, getTheme());
|
|
mContextMenu = menu;
|
|
menu.show(pane, event.getScreenX(), event.getScreenY());
|
|
event.consume();
|
|
}
|
|
|
|
/**
|
|
* Adds the submenu options of a later entry for the same channel that the earlier entry doesn't have, e.g. the
|
|
* radio reference layer's option to view a playlist channel's listing. When the later entry has all of the
|
|
* earlier entry's options, the earlier entry takes the later entry's options in their order, otherwise the
|
|
* missing options are added after a separator.
|
|
*/
|
|
private static void mergeOptions(MenuItem earlier, MenuItem later)
|
|
{
|
|
if(!(earlier instanceof Menu earlierMenu) || !(later instanceof Menu laterMenu))
|
|
{
|
|
return;
|
|
}
|
|
|
|
Set<String> earlierTexts = getOptionTexts(earlierMenu);
|
|
Set<String> laterTexts = getOptionTexts(laterMenu);
|
|
|
|
if(laterTexts.containsAll(earlierTexts))
|
|
{
|
|
if(laterTexts.size() > earlierTexts.size())
|
|
{
|
|
//Items belong to one menu, so they're moved
|
|
List<MenuItem> options = new ArrayList<>(laterMenu.getItems());
|
|
laterMenu.getItems().clear();
|
|
earlierMenu.getItems().setAll(options);
|
|
}
|
|
|
|
return;
|
|
}
|
|
|
|
List<MenuItem> added = new ArrayList<>();
|
|
|
|
for(MenuItem option: laterMenu.getItems())
|
|
{
|
|
if(!(option instanceof SeparatorMenuItem) && option.getText() != null &&
|
|
!earlierTexts.contains(option.getText()))
|
|
{
|
|
added.add(option);
|
|
}
|
|
}
|
|
|
|
if(!added.isEmpty())
|
|
{
|
|
laterMenu.getItems().removeAll(added);
|
|
earlierMenu.getItems().add(ChannelMenus.createSeparator());
|
|
earlierMenu.getItems().addAll(added);
|
|
}
|
|
}
|
|
|
|
private static Set<String> getOptionTexts(Menu menu)
|
|
{
|
|
Set<String> texts = new HashSet<>();
|
|
|
|
for(MenuItem option: menu.getItems())
|
|
{
|
|
if(!(option instanceof SeparatorMenuItem) && option.getText() != null)
|
|
{
|
|
texts.add(option.getText());
|
|
}
|
|
}
|
|
|
|
return texts;
|
|
}
|
|
|
|
/**
|
|
* A context menu places item text in a column after the widest graphic, so when a layer's items show their
|
|
* content as a graphic, the text of the other items is moved into a graphic too, to line up on the left.
|
|
*/
|
|
private static void alignLabels(List<MenuItem> items)
|
|
{
|
|
if(items.stream().noneMatch(item -> item.getGraphic() != null && !(item instanceof SeparatorMenuItem)))
|
|
{
|
|
return;
|
|
}
|
|
|
|
for(MenuItem item: items)
|
|
{
|
|
if(item.getGraphic() == null && item.getText() != null && !(item instanceof SeparatorMenuItem))
|
|
{
|
|
item.setGraphic(new Label(item.getText()));
|
|
item.setText(null);
|
|
}
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Resizable pane that holds a canvas for one display section.
|
|
*/
|
|
private class CanvasPane extends Pane
|
|
{
|
|
private final Canvas mCanvas = new Canvas();
|
|
private final DisplaySection mSection;
|
|
|
|
CanvasPane(DisplaySection section)
|
|
{
|
|
mSection = section;
|
|
getChildren().add(mCanvas);
|
|
setMinSize(0, 20);
|
|
setPrefSize(400, 150);
|
|
setCursor(Cursor.CROSSHAIR);
|
|
addEventHandler(MouseEvent.ANY, event -> handleMouse(this, event));
|
|
addEventHandler(ScrollEvent.SCROLL, event -> handleScroll(this, event));
|
|
addEventHandler(ContextMenuEvent.CONTEXT_MENU_REQUESTED, event -> showContextMenu(this, event));
|
|
}
|
|
|
|
Canvas getCanvas()
|
|
{
|
|
return mCanvas;
|
|
}
|
|
|
|
DisplaySection getSection()
|
|
{
|
|
return mSection;
|
|
}
|
|
|
|
@Override
|
|
protected void layoutChildren()
|
|
{
|
|
double width = Math.floor(getWidth());
|
|
double height = Math.floor(getHeight());
|
|
|
|
if(mCanvas.getWidth() != width || mCanvas.getHeight() != height)
|
|
{
|
|
mCanvas.setWidth(width);
|
|
mCanvas.setHeight(height);
|
|
requestRender();
|
|
}
|
|
}
|
|
}
|
|
}
|