delete simulation

This commit is contained in:
Matei Adriel 2019-07-25 17:40:28 +03:00
parent cddb050dfe
commit 96ae3f44a1
11 changed files with 112 additions and 42 deletions

View file

@ -13,6 +13,8 @@ import { switchTo } from '../../saving/helpers/switchTo'
import { SimulationError } from '../../errors/classes/SimulationError' import { SimulationError } from '../../errors/classes/SimulationError'
import { icons } from '../constants' import { icons } from '../constants'
import { useTranslation } from '../../internalisation/helpers/useLanguage' import { useTranslation } from '../../internalisation/helpers/useLanguage'
import { getTemplateSafely } from '../../logic-gates/helpers/getTemplateSafely'
import { getRendererSafely } from '../../logic-gates/helpers/getRendererSafely'
/** /**
* Returns a list with the names of all saved simulations * Returns a list with the names of all saved simulations
@ -74,7 +76,13 @@ const OpenSimulation = () => {
open={Boolean(anchorEl)} open={Boolean(anchorEl)}
onClose={handleClose} onClose={handleClose}
> >
{simulations.map((simulationName, index) => { {simulations
.filter(
name =>
simulations.length < 2 ||
name !== getRendererSafely().simulation.name
)
.map((simulationName, index) => {
const simulationData = saveStore.get(simulationName) const simulationData = saveStore.get(simulationName)
if (!simulationData) { if (!simulationData) {
@ -100,7 +108,9 @@ const OpenSimulation = () => {
} }
</Icon> </Icon>
</ListItemIcon> </ListItemIcon>
<Typography>{simulationName}</Typography> <Typography style={{ flexGrow: 1 }}>
{simulationName}
</Typography>
</MenuItem> </MenuItem>
) )
})} })}

View file

@ -30,7 +30,8 @@ export const EnglishTranslation: Translation = {
refresh: 'Refresh', refresh: 'Refresh',
undo: 'Undo', undo: 'Undo',
'select all': 'Select all', 'select all': 'Select all',
'delete selection': 'Delete selection' 'delete selection': 'Delete selection',
'delete simulation': 'Delete simulation'
}, },
messages: { messages: {
createdSimulation: name => `Succesfully created simulation '${name}'`, createdSimulation: name => `Succesfully created simulation '${name}'`,
@ -40,6 +41,7 @@ export const EnglishTranslation: Translation = {
compiledIc: name => `Succesfully compiled circuit '${name}'`, compiledIc: name => `Succesfully compiled circuit '${name}'`,
cleaned: name => `Succesfully cleaned simulation '${name}'`, cleaned: name => `Succesfully cleaned simulation '${name}'`,
refreshed: name => `Succesfully refreshed simulation '${name}'`, refreshed: name => `Succesfully refreshed simulation '${name}'`,
undone: name => `Succesfully undone simulation '${name}'` undone: name => `Succesfully undone simulation '${name}'`,
deletedSimulation: name => `Succesfully deleted simulation '${name}'`
} }
} }

View file

@ -18,7 +18,8 @@ export const DutchTranslation: Translation = {
clean: 'Todo', clean: 'Todo',
refresh: 'Todo', refresh: 'Todo',
save: 'Todo', save: 'Todo',
undo: 'Todo' undo: 'Todo',
'delete simulation': `Todo`
}, },
createSimulation: { createSimulation: {
mode: { mode: {
@ -38,9 +39,10 @@ export const DutchTranslation: Translation = {
switchedToSimulation: name => switchedToSimulation: name =>
`Succesvol veranderd naar simulatie '${name}'`, `Succesvol veranderd naar simulatie '${name}'`,
savedSimulation: name => `Simulatie succesvol opgeslagen '${name}'`, savedSimulation: name => `Simulatie succesvol opgeslagen '${name}'`,
compiledIc: name => `Todo: ${name}`, compiledIc: name => `IC gecompileerd: ${name}`,
cleaned: name => `Todo ${name}`, cleaned: name => `${name} gewist`,
refreshed: name => `Todo ${name}`, refreshed: name => `${name} ververst`,
undone: name => `Todo ${name}` undone: name => `${name} ongedaan gemaakt`,
deletedSimulation: name => `Todo`
} }
} }

View file

@ -30,7 +30,8 @@ export const RomanianTranslation: Translation = {
'select all': 'Selectează totul', 'select all': 'Selectează totul',
clean: 'Curăță', clean: 'Curăță',
refresh: 'Reîncarcă', refresh: 'Reîncarcă',
undo: 'Întoarce' undo: 'Întoarce',
'delete simulation': 'Șterge simulația'
}, },
messages: { messages: {
createdSimulation: name => createdSimulation: name =>
@ -41,6 +42,8 @@ export const RomanianTranslation: Translation = {
compiledIc: name => `Simulația '${name}' a fost compilată cu succes`, compiledIc: name => `Simulația '${name}' a fost compilată cu succes`,
cleaned: name => `Simulația '${name}' a fost curățată cu succes`, cleaned: name => `Simulația '${name}' a fost curățată cu succes`,
refreshed: name => `Simulația '${name}' a fost reîncărcată cu succes`, refreshed: name => `Simulația '${name}' a fost reîncărcată cu succes`,
undone: name => `Acțiunea a fost întoarsă` undone: name => `Acțiunea a fost întoarsă`,
deletedSimulation: name =>
`Simulația '${name}' a fost ștearsă cu succes`
} }
} }

View file

@ -34,6 +34,7 @@ export interface Translation {
refreshed: NameSentence refreshed: NameSentence
cleaned: NameSentence cleaned: NameSentence
undone: NameSentence undone: NameSentence
deletedSimulation: NameSentence
} }
actions: Record<possibleAction, string> actions: Record<possibleAction, string>
} }

View file

@ -1,7 +1,15 @@
import { LocalStore } from '../../storage/classes/LocalStore' import { LocalStore } from '../../storage/classes/LocalStore'
import { RendererState } from '../types/SimulationSave' import { RendererState } from '../types/SimulationSave'
import { initSimulation } from '../helpers/initSimulation'
import { defaultSimulationName } from '../constants'
/** /**
* This store is used to save all simulations. * This store is used to save all simulations.
*/ */
export const saveStore = new LocalStore<RendererState>('saves') const saveStore = new LocalStore<RendererState>('saves')
if (!saveStore.ls().length) {
initSimulation(defaultSimulationName, 'project')
}
export { saveStore }

View file

@ -7,6 +7,7 @@ import { createActionConfig } from './helpers/createActionConfig'
import { selectAll } from './helpers/selectAll' import { selectAll } from './helpers/selectAll'
import { deleteSelection } from './helpers/deleteSelection' import { deleteSelection } from './helpers/deleteSelection'
import { cleanRenderer } from './helpers/clean' import { cleanRenderer } from './helpers/clean'
import { deleteSimulation } from './helpers/deleteSimulation'
export const actionIcons: Record<possibleAction, string> = { export const actionIcons: Record<possibleAction, string> = {
clean: 'clear', clean: 'clear',
@ -14,7 +15,8 @@ export const actionIcons: Record<possibleAction, string> = {
save: 'save', save: 'save',
undo: 'undo', undo: 'undo',
'select all': 'select_all', 'select all': 'select_all',
'delete selection': 'delete' 'delete selection': 'delete',
'delete simulation': 'delete_forever'
} }
/** /**
@ -43,6 +45,13 @@ export const SidebarActions: Record<possibleAction, SidebarAction> = {
}, },
['ctrl', 'delete'] ['ctrl', 'delete']
), ),
...createActionConfig(
'delete simulation',
{
run: deleteSimulation
},
['ctrl', 'shift', 'delete']
),
...createActionConfig('select all', selectAll, ['ctrl', 'a']), ...createActionConfig('select all', selectAll, ['ctrl', 'a']),
...createActionConfig('delete selection', deleteSelection, ['delete']) ...createActionConfig('delete selection', deleteSelection, ['delete'])
} }

View file

@ -0,0 +1,24 @@
import { SimulationRenderer } from '../../simulationRenderer/classes/SimulationRenderer'
import { saveStore } from '../../saving/stores/saveStore'
import { removeElement } from '../../../common/lang/arrays/helpers/removeElement'
import { initSimulation } from '../../saving/helpers/initSimulation'
import { defaultSimulationName } from '../../saving/constants'
import { randomItem } from '../../internalisation/helpers/randomItem'
import { switchTo } from '../../saving/helpers/switchTo'
export const deleteSimulation = (renderer: SimulationRenderer) => {
const current = renderer.simulation.name
const others = saveStore.ls()
removeElement(others, current)
if (!others.length) {
initSimulation(defaultSimulationName, 'project')
}
const switchTarget = randomItem(others)
switchTo(switchTarget)
// actually delete simulation
saveStore.delete(current)
}

View file

@ -8,3 +8,4 @@ export type possibleAction =
| 'undo' | 'undo'
| 'select all' | 'select all'
| 'delete selection' | 'delete selection'
| 'delete simulation'

View file

@ -253,7 +253,9 @@ export class SimulationRenderer {
const offset = invert( const offset = invert(
relativeTo(this.lastMousePosition, worldPosition) relativeTo(this.lastMousePosition, worldPosition)
).map( )
const scaledOffset = offset.map(
(value, index) => value * this.camera.transform.scale[index] (value, index) => value * this.camera.transform.scale[index]
) as vector2 ) as vector2
@ -269,7 +271,7 @@ export class SimulationRenderer {
if ((this.mouseState >> 1) & 1) { if ((this.mouseState >> 1) & 1) {
this.camera.transform.position = add( this.camera.transform.position = add(
this.camera.transform.position, this.camera.transform.position,
invert(offset) invert(scaledOffset)
) )
this.spawnCount = 0 this.spawnCount = 0

View file

@ -50,4 +50,12 @@ export class LocalStore<T> {
currentData[finalKey] = finalValue currentData[finalKey] = finalValue
localStorage.setItem(this.name, JSON.stringify(currentData)) localStorage.setItem(this.name, JSON.stringify(currentData))
} }
public delete(key = 'index') {
const all = this.getAll()
delete all[key]
localStorage.setItem(this.name, JSON.stringify(all))
}
} }