forked from Avis-Drone-Labs/FGCS
-
Notifications
You must be signed in to change notification settings - Fork 1
Right click menu #8
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
Open
Saahib13
wants to merge
6
commits into
main
Choose a base branch
from
right_click_menu
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 3 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
a26367d
initial commit
Saahib13 692c4b4
added right clikc funcitonality menu front end
Saahib13 a06c2ab
added sub menu functionality
Saahib13 b5c994b
fixed laggy menu (hopefully)
Saahib13 b695039
laggy menu should be fixed along woth dragging issue. only changed th…
Saahib13 efa68d3
Merge branch 'right_click_menu' of https://github.com/UWARG/IMACS-3.0…
Saahib13 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,252 @@ | ||
| // Base imports | ||
| import React, { useEffect, useRef, useState } from "react" | ||
|
|
||
| // Hooks and helpers | ||
| import { useClipboard } from "@mantine/hooks" | ||
| import { showNotification } from "../../helpers/notification" | ||
| import useContextMenu from "../mapComponents/useContextMenu" | ||
|
|
||
| // UI components | ||
| import ContextMenuItem from "../mapComponents/contextMenuItem" | ||
|
|
||
| const coordsFractionDigits = 7 | ||
|
|
||
| export function useMissionContextMenu() { | ||
| const contextMenuRef = useRef() | ||
| const { clicked, setClicked, points, setPoints } = useContextMenu() | ||
| const [contextMenuPositionCalculationInfo, setContextMenuPositionCalculationInfo] = useState() | ||
| const [clickedGpsCoords, setClickedGpsCoords] = useState({ lng: 0, lat: 0 }) | ||
| const [savedCoordinates, setSavedCoordinates] = useState([]) | ||
| const [showSubmenu, setShowSubmenu] = useState(false) | ||
| const clipboard = useClipboard({ timeout: 500 }) | ||
|
|
||
| useEffect(() => { | ||
| if (contextMenuRef.current && contextMenuPositionCalculationInfo) { | ||
| const contextMenuWidth = Math.round( | ||
| contextMenuRef.current.getBoundingClientRect().width, | ||
| ) | ||
| const contextMenuHeight = Math.round( | ||
| contextMenuRef.current.getBoundingClientRect().height, | ||
| ) | ||
| let x = contextMenuPositionCalculationInfo.clickedPoint.x | ||
| let y = contextMenuPositionCalculationInfo.clickedPoint.y | ||
|
|
||
| if ( | ||
| contextMenuWidth + contextMenuPositionCalculationInfo.clickedPoint.x > | ||
| contextMenuPositionCalculationInfo.canvasSize.width | ||
| ) { | ||
| x = contextMenuPositionCalculationInfo.clickedPoint.x - contextMenuWidth | ||
| } | ||
| if ( | ||
| contextMenuHeight + contextMenuPositionCalculationInfo.clickedPoint.y > | ||
| contextMenuPositionCalculationInfo.canvasSize.height | ||
| ) { | ||
| y = | ||
| contextMenuPositionCalculationInfo.clickedPoint.y - contextMenuHeight | ||
| } | ||
|
|
||
| setPoints({ x, y }) | ||
| } | ||
| }, [contextMenuPositionCalculationInfo]) | ||
|
|
||
| // Close context menu when clicking outside | ||
| useEffect(() => { | ||
| const handleClickOutside = (event) => { | ||
| if (clicked && contextMenuRef.current && !contextMenuRef.current.contains(event.target)) { | ||
| setClicked(false) | ||
| setShowSubmenu(false) | ||
| } | ||
| } | ||
|
|
||
| document.addEventListener('mousedown', handleClickOutside) | ||
| return () => { | ||
| document.removeEventListener('mousedown', handleClickOutside) | ||
| } | ||
| }, [clicked]) | ||
|
|
||
| const handleContextMenu = (e) => { | ||
| e.preventDefault() | ||
| setClicked(true) | ||
| setShowSubmenu(false) | ||
| setClickedGpsCoords(e.lngLat) | ||
| // Save coordinates for future use | ||
| setSavedCoordinates(prev => [...prev, { | ||
| lat: e.lngLat.lat, | ||
| lng: e.lngLat.lng, | ||
| timestamp: new Date().toISOString() | ||
| }]) | ||
| setContextMenuPositionCalculationInfo({ | ||
| clickedPoint: e.point, | ||
| canvasSize: { | ||
| height: e.originalEvent.target.clientHeight, | ||
| width: e.originalEvent.target.clientWidth, | ||
| }, | ||
| }) | ||
| } | ||
|
|
||
| return { | ||
| // state | ||
| clicked, | ||
| setClicked, | ||
| points, | ||
| clickedGpsCoords, | ||
| savedCoordinates, | ||
| showSubmenu, | ||
| setShowSubmenu, | ||
| // refs | ||
| contextMenuRef, | ||
| // handlers | ||
| handleContextMenu, | ||
| // helpers | ||
| clipboard, | ||
| } | ||
| } | ||
|
|
||
| export default function MissionContextMenuOverlay({ ctx }) { | ||
| if (!ctx.clicked) return null | ||
| return ( | ||
| <div | ||
| ref={ctx.contextMenuRef} | ||
| className="absolute bg-falcongrey-700 rounded-md p-1 min-w-[180px]" | ||
| style={{ top: ctx.points.y, left: ctx.points.x }} | ||
| onMouseDown={(e) => e.stopPropagation()} | ||
| onClick={(e) => e.stopPropagation()} | ||
| > | ||
| {/* Copy Coordinates */} | ||
| <ContextMenuItem | ||
| onClick={(e) => { | ||
| e.stopPropagation() | ||
| ctx.clipboard.copy( | ||
| `${ctx.clickedGpsCoords.lat}, ${ctx.clickedGpsCoords.lng}`, | ||
| ) | ||
| showNotification("Copied to clipboard") | ||
| ctx.setClicked(false) | ||
| }} | ||
| > | ||
| <div className="w-full flex justify-between gap-2"> | ||
| <p> | ||
| {ctx.clickedGpsCoords.lat.toFixed(coordsFractionDigits)}, {" "} | ||
| {ctx.clickedGpsCoords.lng.toFixed(coordsFractionDigits)} | ||
| </p> | ||
| <svg | ||
| className="relative -right-1" | ||
| xmlns="http://www.w3.org/2000/svg" | ||
| width="20" | ||
| height="20" | ||
| viewBox="0 0 24 24" | ||
| > | ||
| <path | ||
| fill="currentColor" | ||
| d="M9 18q-.825 0-1.412-.587T7 16V4q0-.825.588-1.412T9 2h9q.825 0 1.413.588T20 4v12q0 .825-.587 1.413T18 18zm0-2h9V4H9zm-4 6q-.825 0-1.412-.587T3 20V7q0-.425.288-.712T4 6t.713.288T5 7v13h10q.425 0 .713.288T16 21t-.288.713T15 22zm4-6V4z" | ||
| /> | ||
| </svg> | ||
| </div> | ||
| </ContextMenuItem> | ||
|
|
||
| {/* Divider */} | ||
| <div className="border-t border-falcongrey-600 my-1"></div> | ||
|
|
||
| {/* Insert Command */} | ||
| <div className="relative"> | ||
| <ContextMenuItem | ||
| onClick={(e) => { | ||
| e.stopPropagation() | ||
| ctx.setShowSubmenu(!ctx.showSubmenu) | ||
| }} | ||
| > | ||
| <div className="w-full flex justify-between gap-2"> | ||
| <span>Insert Command</span> | ||
| <svg | ||
| className={`transform transition-transform ${ctx.showSubmenu ? 'rotate-90' : ''}`} | ||
| xmlns="http://www.w3.org/2000/svg" | ||
| width="16" | ||
| height="16" | ||
| viewBox="0 0 24 24" | ||
| > | ||
| <path | ||
| fill="currentColor" | ||
| d="M8.59 16.59L13.17 12L8.59 7.41L10 6l6 6l-6 6l-1.41-1.41z" | ||
| /> | ||
| </svg> | ||
| </div> | ||
| </ContextMenuItem> | ||
|
|
||
| {/* Insert Submenu */} | ||
| {ctx.showSubmenu && ( | ||
| <div | ||
| className="absolute left-full top-0 ml-1 bg-falcongrey-700 rounded-md p-1 min-w-[140px] shadow-lg" | ||
| onMouseDown={(e) => e.stopPropagation()} | ||
| onClick={(e) => e.stopPropagation()} | ||
| > | ||
| <ContextMenuItem | ||
| onClick={(e) => { | ||
| e.stopPropagation() | ||
| console.log("Insert Waypoint at:", ctx.clickedGpsCoords) | ||
| ctx.setClicked(false) | ||
| ctx.setShowSubmenu(false) | ||
| }} | ||
| > | ||
| <span>Waypoint</span> | ||
| </ContextMenuItem> | ||
| <ContextMenuItem | ||
| onClick={(e) => { | ||
| e.stopPropagation() | ||
| console.log("Insert Land at:", ctx.clickedGpsCoords) | ||
| ctx.setClicked(false) | ||
| ctx.setShowSubmenu(false) | ||
| }} | ||
| > | ||
| <span>Land</span> | ||
| </ContextMenuItem> | ||
| <ContextMenuItem | ||
| onClick={(e) => { | ||
| e.stopPropagation() | ||
| console.log("Insert Takeoff at:", ctx.clickedGpsCoords) | ||
| ctx.setClicked(false) | ||
| ctx.setShowSubmenu(false) | ||
| }} | ||
| > | ||
| <span>Takeoff</span> | ||
| </ContextMenuItem> | ||
| <ContextMenuItem | ||
| onClick={(e) => { | ||
| e.stopPropagation() | ||
| console.log("Insert Return to Launch at:", ctx.clickedGpsCoords) | ||
| ctx.setClicked(false) | ||
| ctx.setShowSubmenu(false) | ||
| }} | ||
| > | ||
| <span>Return to Launch</span> | ||
| </ContextMenuItem> | ||
| </div> | ||
| )} | ||
| </div> | ||
|
|
||
| {/* Delete Command */} | ||
| <ContextMenuItem | ||
| onClick={(e) => { | ||
| e.stopPropagation() | ||
| console.log("Delete command at:", ctx.clickedGpsCoords) | ||
| ctx.setClicked(false) | ||
| }} | ||
| > | ||
| <div className="w-full flex justify-between gap-2"> | ||
| <span>Delete Command</span> | ||
| <svg | ||
| xmlns="http://www.w3.org/2000/svg" | ||
| width="16" | ||
| height="16" | ||
| viewBox="0 0 24 24" | ||
| > | ||
| <path | ||
| fill="currentColor" | ||
| d="M7 21q-.825 0-1.412-.587T5 19V6H4V4h5V3h6v1h5v2h-1v13q0 .825-.587 1.413T17 21zm2-4h2V8H9zm4 0h2V8h-2z" | ||
| /> | ||
| </svg> | ||
| </div> | ||
| </ContextMenuItem> | ||
| </div> | ||
| ) | ||
| } | ||
|
|
||
|
|
||
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit: it seems like the menu briefly renders at its old position before updating to its newly clicked position, causing it to flash across the screen. Try and see if you can find a way to fix this, but not a critical issue