-
Notifications
You must be signed in to change notification settings - Fork 1
Create a slidable ruler for frequency measurements #242
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
Merged
Merged
Changes from 11 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
d146549
Create layer for measure tool
naglepuff a10e00d
Implement draggable ruler for measuring frequency
naglepuff 214ca9b
Handle scaling and background color changes
naglepuff 76bc489
Handle switching between un/compressed views
naglepuff a80fac9
Update color display and text location
naglepuff 93ff04d
Add tooltip for frequency measure ruler
naglepuff 25ac708
Use correct icon for NABat spectrogram view
naglepuff ab796a5
Remove event handlers when not using ruler
naglepuff f341c42
grab/grabbing icons, hover state (#247)
BryonLewis 3172d43
Preserve measure tool between un/compressed views
naglepuff 0abe14a
Update yValue when shift+scrolling
naglepuff 8470fba
Don't call enableDrawing before assigning handlers
naglepuff File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,259 @@ | ||
| import geo, { GeoEvent } from 'geojs'; | ||
| import { SpectroInfo } from '../geoJSUtils'; | ||
| import { LayerStyle, LineData, TextData } from './types'; | ||
| import BaseTextLayer from './baseTextLayer'; | ||
|
|
||
| function _determineRulerColor(isDragging: boolean, isDarkMode: boolean) { | ||
| if (isDarkMode) { | ||
| return isDragging ? 'orange' : 'yellow'; | ||
| } | ||
| return isDragging ? 'cyan' : 'blue'; | ||
| } | ||
|
|
||
| export default class MeasureToolLayer extends BaseTextLayer<TextData> { | ||
| // eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
| frequencyRulerLayer: any; | ||
| // eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
| pointAnnotation: any; | ||
| // eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
| lineAnnotation: any; | ||
|
|
||
| rulerOn: boolean; | ||
| dragging: boolean; | ||
| yValue: number; | ||
| hovering: boolean; | ||
|
|
||
| moveHandler: (e: GeoEvent) => void; | ||
| mousedownHandler: (e: GeoEvent) => void; | ||
| hoverHandler: (e: GeoEvent) => void; | ||
| mouseupHandler: () => void; | ||
|
|
||
| constructor( | ||
| // eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
| geoViewerRef: any, | ||
| // eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
| event: (name: string, data: any) => void, | ||
| spectroInfo: SpectroInfo, | ||
| measuring?: boolean, | ||
| yValue?: number | ||
| ) { | ||
| super(geoViewerRef, event, spectroInfo); | ||
|
|
||
| const textLayer = this.geoViewerRef.createLayer('feature', { | ||
| features: ['text'] | ||
| }); | ||
| this.textLayer = textLayer | ||
| .createFeature("text") | ||
| .text((data: TextData) => data.text) | ||
| .style(this.createTextStyle()) | ||
| .position((data: TextData) => ({ | ||
| x: data.x, | ||
| y: data.y, | ||
| })); | ||
|
|
||
| const frequencyRulerLayer = this.geoViewerRef.createLayer("feature", { | ||
| features: ['point', 'line'], | ||
| }); | ||
| this.frequencyRulerLayer = frequencyRulerLayer; | ||
| this.rulerOn = false; | ||
| this.pointAnnotation= null; | ||
| this.lineAnnotation = null; | ||
| this.dragging = false; | ||
| this.yValue = yValue || 0; | ||
| this.color = 'white'; | ||
| this.hovering = false; | ||
|
|
||
| this.textStyle = this.createTextStyle(); | ||
| this.rulerOn = measuring || false; | ||
| if (this.rulerOn) { | ||
| this.enableDrawing(); | ||
| } | ||
|
|
||
| this.moveHandler = (e: GeoEvent) => { | ||
| if (e && this.dragging) { | ||
| this.updateRuler(e.mouse.geo.y); | ||
| } | ||
| }; | ||
| this.hoverHandler = (e: GeoEvent) => { | ||
| if (e) { | ||
| const gcs = this.geoViewerRef.displayToGcs(e.map); | ||
| const p = this.pointAnnotation.data()[0]; | ||
| const dx = Math.abs(gcs.x - p.x); | ||
| const dy = Math.abs(gcs.y - p.y); | ||
| if (Math.sqrt(dx*dx + dy*dy) < 20 || dy < 10) { | ||
| this.event('update:cursor', { cursor: 'grab' }); | ||
| this.hovering = true; | ||
| return; | ||
| } else { | ||
| this.event('update:cursor', { cursor: 'default' }); | ||
| } | ||
| } | ||
| this.hovering = false; | ||
| }; | ||
| this.mousedownHandler = (e: GeoEvent) => { | ||
| if (this.hovering && e.buttons.left) { | ||
| this.geoViewerRef.interactor().addAction({ | ||
| action: 'dragpoint', | ||
| name: 'drag point with mouse', | ||
| owner: 'MeasureToolLayer', | ||
| input: 'left', | ||
| }); | ||
| this.dragging = true; | ||
| this.event('update:cursor', { cursor: 'grabbing' }); | ||
| } | ||
| }; | ||
| this.mouseupHandler = () => { | ||
| this.dragging = false; | ||
| this.geoViewerRef.interactor().removeAction(undefined, undefined, 'MeasureToolLayer'); | ||
| this.updateRuler(this.yValue); | ||
| this.event('update:cursor', { cursor: 'grab' }); | ||
| }; | ||
| } | ||
|
|
||
| enableDrawing() { | ||
| this.rulerOn = true; | ||
| // Frequency ruler | ||
| this.lineAnnotation = this.frequencyRulerLayer.createFeature('line') | ||
| .data([[ | ||
| {x: 0, y: this.yValue}, | ||
| {x: this.spectroInfo.width, y: this.yValue}, | ||
| ]]) | ||
| .style(this.createLineStyle()); | ||
| this.pointAnnotation = this.frequencyRulerLayer.createFeature('point') | ||
| .data([{x: 0, y: this.yValue}]) | ||
| .style(this.createPointStyle()); | ||
| this.geoViewerRef.geoOn(geo.event.mousedown, this.mousedownHandler); | ||
| this.geoViewerRef.geoOn(geo.event.actionmove, this.moveHandler); | ||
| this.geoViewerRef.geoOn(geo.event.mouseup, this.mouseupHandler); | ||
| this.geoViewerRef.geoOn(geo.event.mousemove, this.hoverHandler); | ||
| this.frequencyRulerLayer.draw(); | ||
| this.updateRuler(this.yValue); | ||
| } | ||
|
|
||
| _getTextCoordinates(): { x: number, y: number } { | ||
| const bounds = this.geoViewerRef.bounds(); | ||
| const startX = 0; | ||
| const endX = ((this.compressedView | ||
| ? this.scaledWidth | ||
| : this.spectroInfo.width | ||
| ) || this.spectroInfo.width); | ||
| const left = Math.max(startX, bounds.left); | ||
| const right = Math.min(endX, bounds.right); | ||
| return { x: (left + right) / 2, y: this.yValue }; | ||
| } | ||
|
|
||
| updateRuler(newY: number) { | ||
| if (newY < 0) { | ||
| return; | ||
| } | ||
| this.event("measure:dragged", { yValue: newY }); | ||
| this.yValue = newY; | ||
| const spectroWidth = this.compressedView ? this.scaledWidth : this.spectroInfo.width; | ||
| this.lineAnnotation | ||
| .data([[ | ||
| {x: 0, y: this.yValue}, | ||
| {x: (spectroWidth || this.spectroInfo.width), y: this.yValue}, | ||
| ]]) | ||
| .style(this.createLineStyle()); | ||
| this.pointAnnotation | ||
| .data([{x: 0, y: this.yValue}]) | ||
| .style(this.createPointStyle()); | ||
| this.frequencyRulerLayer.draw(); | ||
| const height = Math.max(this.scaledHeight, this.spectroInfo.height); | ||
| const frequency = height - this.yValue >= 0 | ||
| ? ((height - newY) * (this.spectroInfo.high_freq - this.spectroInfo.low_freq)) / height / 1000 + this.spectroInfo.low_freq / 1000 | ||
| : -1; | ||
| const textValue = `${frequency.toFixed(1)}KHz`; | ||
| const { x: textX, y: textY } = this._getTextCoordinates(); | ||
| this.textData = [ | ||
| { | ||
| text: textValue, | ||
| x: textX, | ||
| y: textY, | ||
| offsetY: 20, | ||
| }, | ||
| ]; | ||
| this.textLayer.data(this.textData).draw(); | ||
| } | ||
|
|
||
| disableDrawing() { | ||
| this.rulerOn = false; | ||
| this.textData = []; | ||
| this.textLayer.data(this.textData).draw(); | ||
| this.clearRulerLayer(); | ||
| this.geoViewerRef.geoOff(geo.event.mousedown, this.mousedownHandler); | ||
| this.geoViewerRef.geoOff(geo.event.mouseup, this.mouseupHandler); | ||
| this.geoViewerRef.geoOff(geo.event.actionmove, this.moveHandler); | ||
| this.geoViewerRef.geoOff(geo.event.mousemove, this.hoverHandler); | ||
| } | ||
|
|
||
| clearRulerLayer() { | ||
| this.pointAnnotation?.data([]); | ||
| this.lineAnnotation?.data([]); | ||
| this.textLayer?.data([]).draw(); | ||
| this.frequencyRulerLayer?.draw(); | ||
| } | ||
|
|
||
| destroy() { | ||
| super.destroy(); | ||
| this.textData = []; | ||
| if (this.frequencyRulerLayer) { | ||
| this.geoViewerRef.deleteLater(this.frequencyRulerLayer); | ||
| } | ||
| } | ||
|
|
||
| setScaledDimensions(scaledWidth: number, scaledHeight: number) { | ||
| // Get the frequency represented by the current ruler | ||
| const height = Math.max(this.scaledHeight, this.spectroInfo.height); | ||
| const frequency = height - this.yValue >= 0 | ||
| ? ((height - this.yValue) * (this.spectroInfo.high_freq - this.spectroInfo.low_freq)) / height / 1000 + this.spectroInfo.low_freq / 1000 | ||
| : -1; | ||
| // Get the new yValue needed based on the updated scaled height | ||
| const newY = scaledHeight - ((frequency - (this.spectroInfo.low_freq / 1000)) * scaledHeight * 1000) / (this.spectroInfo.high_freq - this.spectroInfo.low_freq); | ||
| this.yValue = newY; | ||
| super.setScaledDimensions(scaledWidth, scaledHeight); | ||
| this.clearRulerLayer(); | ||
| if (this.rulerOn) { | ||
| this.enableDrawing(); | ||
| this.updateRuler(this.yValue); | ||
| } | ||
| } | ||
|
|
||
| redraw() { | ||
| if (this.rulerOn) { | ||
| this.updateRuler(this.yValue); | ||
| } | ||
| super.redraw(); | ||
| } | ||
|
|
||
| createTextStyle(): LayerStyle<TextData> { | ||
| return { | ||
| color: () => _determineRulerColor(this.dragging, this.color === 'white'), | ||
| offset: (data: TextData) => ({ | ||
| x: data.offsetX || 0, | ||
| y: data.offsetY || 0, | ||
| }), | ||
| textAlign: 'center', | ||
| textScaled: this.textScaled, | ||
| textBaseline: 'bottom', | ||
| }; | ||
| } | ||
|
|
||
| createPointStyle(): LayerStyle<LineData> { | ||
| return { | ||
| radius: 10, | ||
| fillColor: () => _determineRulerColor(this.dragging, this.color === 'white'), | ||
| strokeColor: () => _determineRulerColor(this.dragging, this.color === 'white'), | ||
| stroke: true, | ||
| strokeWidth: 5, | ||
| }; | ||
| } | ||
|
|
||
| createLineStyle(): LayerStyle<LineData> { | ||
| return { | ||
| strokeColor: () => _determineRulerColor(this.dragging, this.color === 'white'), | ||
| strokeWidth: 2, | ||
| }; | ||
| } | ||
|
|
||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.