-
Notifications
You must be signed in to change notification settings - Fork 60
Refactor Zone Creation [AARD-2071]
#1279
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
AlexD717
wants to merge
12
commits into
dev
Choose a base branch
from
alexey/2071/refactor-zone-creation
base: dev
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 4 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
f5e0573
Simple Refactor
AlexD717 9246594
Merge branch 'dev' into alexey/2071/refactor-zone-creation
AlexD717 2d7c70c
Comments
AlexD717 b258fdf
Formatting
AlexD717 6066867
PR Feedback
AlexD717 5ef37d2
Proper Material & Unnecessary Button Removed
AlexD717 93ce105
Formatting
AlexD717 0ecaf41
Remove Unnecessary Cache
AlexD717 e9dbaec
Removed More Unnecessary Code
AlexD717 a94204a
Revert "Removed More Unnecessary Code"
AlexD717 2d5f32c
Merge branch 'dev' into alexey/2071/refactor-zone-creation
AlexD717 0e227f5
Reapply "Removed More Unnecessary Code"
AlexD717 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
141 changes: 24 additions & 117 deletions
141
...i/panels/configuring/assembly-config/interfaces/scoring/ManageProtectedZonesInterface.tsx
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 |
|---|---|---|
| @@ -1,137 +1,44 @@ | ||
| import { Stack } from "@mui/material" | ||
| import { useCallback, useEffect, useState } from "react" | ||
| import { ConfigurationSavedEvent } from "@/events/ConfigurationSavedEvent" | ||
| import type MirabufSceneObject from "@/mirabuf/MirabufSceneObject" | ||
| import { ContactType } from "@/mirabuf/ZoneTypes" | ||
| import { MatchModeType } from "@/systems/match_mode/MatchModeTypes" | ||
| import { PAUSE_REF_ASSEMBLY_CONFIG } from "@/systems/physics/PhysicsTypes" | ||
| import PreferencesSystem from "@/systems/preferences/PreferencesSystem" | ||
| import type { ProtectedZonePreferences } from "@/systems/preferences/PreferenceTypes" | ||
| import World from "@/systems/World" | ||
| import Label from "@/ui/components/Label" | ||
| import ScrollView from "@/ui/components/ScrollView" | ||
| import { AddButton, DeleteButton, EditButton } from "@/ui/components/StyledComponents" | ||
|
|
||
| const saveZones = (zones: ProtectedZonePreferences[] | undefined, field: MirabufSceneObject | undefined) => { | ||
| if (!zones || !field) return | ||
| import ManageZonesBase, { type ManageZonesBaseProps } from "../zones/ManageZonesBase" | ||
|
|
||
| function persistProtectedZones(zones: ProtectedZonePreferences[], field: MirabufSceneObject) { | ||
| const fieldPrefs = field.fieldPreferences | ||
| if (fieldPrefs) fieldPrefs.protectedZones = zones | ||
|
|
||
| PreferencesSystem.savePreferences() | ||
| field.updateProtectedZones() | ||
| } | ||
|
|
||
| type ProtectedZoneRowProps = { | ||
| zone: ProtectedZonePreferences | ||
| save: () => void | ||
| deleteZone: () => void | ||
| selectZone: (zone: ProtectedZonePreferences) => void | ||
| } | ||
|
|
||
| const ProtectedZoneRow: React.FC<ProtectedZoneRowProps> = ({ zone, save, deleteZone, selectZone }) => { | ||
| return ( | ||
| <Stack justifyContent={"space-between"} alignItems={"center"} gap={"1rem"}> | ||
| <Stack direction="row" gap={8}> | ||
| <div className={`w-12 h-12 bg-match-${zone.alliance}-alliance rounded-lg`} /> | ||
| <Stack gap={4} className="w-max"> | ||
| <Label size="sm">{zone.name}</Label> | ||
| <Label size="sm"> | ||
| {zone.penaltyPoints} {zone.penaltyPoints === 1 ? "penalty point" : "penalty points"} | ||
| </Label> | ||
| </Stack> | ||
| </Stack> | ||
| <Stack direction="row-reverse" gap={"0.25rem"} justifyContent={"center"} alignItems={"center"}> | ||
| {EditButton(() => { | ||
| selectZone(zone) | ||
| save() | ||
| })} | ||
|
|
||
| {DeleteButton(() => { | ||
| deleteZone() | ||
| })} | ||
| </Stack> | ||
| </Stack> | ||
| ) | ||
| } | ||
|
|
||
| interface ProtectedZonesProps { | ||
| selectedField: MirabufSceneObject | ||
| initialZones: ProtectedZonePreferences[] | ||
| selectZone: (zone: ProtectedZonePreferences) => void | ||
| } | ||
|
|
||
| const ManageZonesInterface: React.FC<ProtectedZonesProps> = ({ selectedField, initialZones, selectZone }) => { | ||
| const [zones, setZones] = useState<ProtectedZonePreferences[]>(initialZones) | ||
|
|
||
| const saveEvent = useCallback(() => { | ||
| saveZones(zones, selectedField) | ||
| }, [zones, selectedField]) | ||
|
|
||
| useEffect(() => { | ||
| ConfigurationSavedEvent.listen(saveEvent) | ||
|
|
||
| return () => { | ||
| ConfigurationSavedEvent.removeListener(saveEvent) | ||
| } | ||
| }, [saveEvent]) | ||
|
|
||
| useEffect(() => { | ||
| saveZones(zones, selectedField) | ||
|
|
||
| World.physicsSystem.holdPause(PAUSE_REF_ASSEMBLY_CONFIG) | ||
|
|
||
| return () => { | ||
| World.physicsSystem.releasePause(PAUSE_REF_ASSEMBLY_CONFIG) | ||
| } | ||
| }, [selectedField, zones]) | ||
|
|
||
| return ( | ||
| <> | ||
| {zones?.length > 0 ? ( | ||
| <ScrollView> | ||
| <Stack gap={4}> | ||
| {zones.map((zonePrefs: ProtectedZonePreferences, i: number) => ( | ||
| <ProtectedZoneRow | ||
| key={i} | ||
| zone={(() => { | ||
| return zonePrefs | ||
| })()} | ||
| save={() => saveZones(zones, selectedField)} | ||
| deleteZone={() => { | ||
| setZones(zones.filter((_, idx) => idx !== i)) | ||
| saveZones( | ||
| zones.filter((_, idx) => idx !== i), | ||
| selectedField | ||
| ) | ||
| }} | ||
| selectZone={selectZone} | ||
| /> | ||
| ))} | ||
| </Stack> | ||
| </ScrollView> | ||
| ) : ( | ||
| <Label size="md">No protected zones</Label> | ||
| )} | ||
| {AddButton(() => { | ||
| if (zones === undefined) return | ||
|
|
||
| const newZone: ProtectedZonePreferences = { | ||
| name: "New Protected Zone", | ||
| alliance: "blue", | ||
| penaltyPoints: 5, | ||
| parentNode: undefined, | ||
| contactType: ContactType.ROBOT_ENTERS, | ||
| activeDuring: [MatchModeType.AUTONOMOUS, MatchModeType.TELEOP, MatchModeType.ENDGAME], | ||
| deltaTransformation: [1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1], | ||
| } | ||
|
|
||
| saveZones(zones, selectedField) | ||
|
|
||
| selectZone(newZone) | ||
| })} | ||
| </> | ||
| ) | ||
| const baseProps: ManageZonesBaseProps<ProtectedZonePreferences> = { | ||
| selectedField, | ||
| initialZones, | ||
| selectZone, | ||
| getListItem: zone => ({ | ||
| name: zone.name, | ||
| alliance: zone.alliance, | ||
| pointsLabel: `${zone.penaltyPoints} ${zone.penaltyPoints === 1 ? "penalty point" : "penalty points"}`, | ||
| }), | ||
| persistZones: persistProtectedZones, | ||
| createNewZone: () => ({ | ||
| name: "New Protected Zone", | ||
| alliance: "blue", | ||
| penaltyPoints: 5, | ||
| parentNode: undefined, | ||
| contactType: ContactType.ROBOT_ENTERS, | ||
| activeDuring: [MatchModeType.AUTONOMOUS, MatchModeType.TELEOP, MatchModeType.ENDGAME], | ||
| deltaTransformation: [1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1], | ||
| }), | ||
| } | ||
|
|
||
| return <ManageZonesBase {...baseProps} /> | ||
| } | ||
|
|
||
| export default ManageZonesInterface | ||
146 changes: 24 additions & 122 deletions
146
.../ui/panels/configuring/assembly-config/interfaces/scoring/ManageScoringZonesInterface.tsx
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 |
|---|---|---|
| @@ -1,140 +1,42 @@ | ||
| import { Box, Stack } from "@mui/material" | ||
| import { useCallback, useEffect, useState } from "react" | ||
| import { ConfigurationSavedEvent } from "@/events/ConfigurationSavedEvent" | ||
| import type MirabufSceneObject from "@/mirabuf/MirabufSceneObject" | ||
| import { PAUSE_REF_ASSEMBLY_CONFIG } from "@/systems/physics/PhysicsTypes" | ||
| import PreferencesSystem from "@/systems/preferences/PreferencesSystem" | ||
| import type { ScoringZonePreferences } from "@/systems/preferences/PreferenceTypes" | ||
| import World from "@/systems/World" | ||
| import Label from "@/ui/components/Label" | ||
| import ScrollView from "@/ui/components/ScrollView" | ||
| import { AddButton, DeleteButton, EditButton } from "@/ui/components/StyledComponents" | ||
|
|
||
| const saveZones = (zones: ScoringZonePreferences[] | undefined, field: MirabufSceneObject | undefined) => { | ||
| if (!zones || !field) return | ||
| import ManageZonesBase, { type ManageZonesBaseProps } from "../zones/ManageZonesBase" | ||
|
|
||
| function persistScoringZones(zones: ScoringZonePreferences[], field: MirabufSceneObject) { | ||
| const fieldPrefs = field.fieldPreferences | ||
| if (fieldPrefs) fieldPrefs.scoringZones = zones | ||
|
|
||
| PreferencesSystem.savePreferences() | ||
| field.updateScoringZones() | ||
| } | ||
|
|
||
| type ScoringZoneRowProps = { | ||
| zone: ScoringZonePreferences | ||
| save: () => void | ||
| deleteZone: () => void | ||
| selectZone: (zone: ScoringZonePreferences) => void | ||
| } | ||
|
|
||
| const ScoringZoneRow: React.FC<ScoringZoneRowProps> = ({ zone, save, deleteZone, selectZone }) => { | ||
| return ( | ||
| <Stack justifyContent={"space-between"} alignItems={"center"} gap={"1rem"}> | ||
| <Stack direction="row" gap={8}> | ||
| <Box | ||
| className={`w-12 h-12 rounded-lg`} | ||
| sx={{ | ||
| bgcolor: zone.alliance === "red" ? "redAlliance.main" : "blueAlliance.main", | ||
| }} | ||
| /> | ||
| <Stack direction="row" gap={4} className="w-max"> | ||
| <Label size="sm">{zone.name}</Label> | ||
| <Label size="sm"> | ||
| {zone.points} {zone.points === 1 ? "point" : "points"} | ||
| </Label> | ||
| </Stack> | ||
| </Stack> | ||
| <Stack direction={"row-reverse"} gap={"0.25rem"} justifyContent={"center"} alignItems={"center"}> | ||
| {EditButton(() => { | ||
| selectZone(zone) | ||
| save() | ||
| })} | ||
|
|
||
| {DeleteButton(() => { | ||
| deleteZone() | ||
| })} | ||
| </Stack> | ||
| </Stack> | ||
| ) | ||
| } | ||
|
|
||
| interface ScoringZonesProps { | ||
| selectedField: MirabufSceneObject | ||
| initialZones: ScoringZonePreferences[] | ||
| selectZone: (zone: ScoringZonePreferences) => void | ||
| } | ||
|
|
||
| const ManageZonesInterface: React.FC<ScoringZonesProps> = ({ selectedField, initialZones, selectZone }) => { | ||
AlexD717 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| const [zones, setZones] = useState<ScoringZonePreferences[]>(initialZones) | ||
|
|
||
| const saveEvent = useCallback(() => { | ||
| saveZones(zones, selectedField) | ||
| }, [zones, selectedField]) | ||
|
|
||
| useEffect(() => { | ||
| ConfigurationSavedEvent.listen(saveEvent) | ||
|
|
||
| return () => { | ||
| ConfigurationSavedEvent.removeListener(saveEvent) | ||
| } | ||
| }, [saveEvent]) | ||
|
|
||
| useEffect(() => { | ||
| saveZones(zones, selectedField) | ||
|
|
||
| World.physicsSystem.holdPause(PAUSE_REF_ASSEMBLY_CONFIG) | ||
|
|
||
| return () => { | ||
| World.physicsSystem.releasePause(PAUSE_REF_ASSEMBLY_CONFIG) | ||
| } | ||
| }, [selectedField, zones]) | ||
|
|
||
| return ( | ||
| <> | ||
| {zones?.length > 0 ? ( | ||
| <ScrollView> | ||
| <Stack gap={4}> | ||
| {zones.map((zonePrefs: ScoringZonePreferences, i: number) => ( | ||
| <ScoringZoneRow | ||
| key={i} | ||
| zone={(() => { | ||
| return zonePrefs | ||
| })()} | ||
| save={() => saveZones(zones, selectedField)} | ||
| deleteZone={() => { | ||
| setZones(zones.filter((_, idx) => idx !== i)) | ||
| saveZones( | ||
| zones.filter((_, idx) => idx !== i), | ||
| selectedField | ||
| ) | ||
| }} | ||
| selectZone={selectZone} | ||
| /> | ||
| ))} | ||
| </Stack> | ||
| </ScrollView> | ||
| ) : ( | ||
| <Label size="md">No scoring zones</Label> | ||
| )} | ||
| {AddButton(() => { | ||
| if (zones === undefined) return | ||
|
|
||
| const newZone: ScoringZonePreferences = { | ||
| name: "New Scoring Zone", | ||
| alliance: "blue", | ||
| parentNode: undefined, | ||
| points: 0, | ||
| destroyGamepiece: false, | ||
| persistentPoints: false, | ||
| deltaTransformation: [1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1], | ||
| } | ||
|
|
||
| saveZones(zones, selectedField) | ||
|
|
||
| selectZone(newZone) | ||
| })} | ||
| </> | ||
| ) | ||
| const baseProps: ManageZonesBaseProps<ScoringZonePreferences> = { | ||
| selectedField, | ||
| initialZones, | ||
| selectZone, | ||
| getListItem: zone => ({ | ||
| name: zone.name, | ||
| alliance: zone.alliance, | ||
| pointsLabel: `${zone.points} ${zone.points === 1 ? "point" : "points"}`, | ||
| }), | ||
| persistZones: persistScoringZones, | ||
| createNewZone: () => ({ | ||
| name: "New Scoring Zone", | ||
| alliance: "blue", | ||
| parentNode: undefined, | ||
| points: 5, | ||
| destroyGamepiece: false, | ||
| persistentPoints: false, | ||
| deltaTransformation: [1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1], | ||
| }), | ||
| } | ||
|
|
||
| return <ManageZonesBase {...baseProps} /> | ||
| } | ||
|
|
||
| export default ManageZonesInterface | ||
Oops, something went wrong.
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.