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 { icons } from '../constants'
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
@ -74,7 +76,13 @@ const OpenSimulation = () => {
open={Boolean(anchorEl)}
onClose={handleClose}
>
{simulations.map((simulationName, index) => {
{simulations
.filter(
name =>
simulations.length < 2 ||
name !== getRendererSafely().simulation.name
)
.map((simulationName, index) => {
const simulationData = saveStore.get(simulationName)
if (!simulationData) {
@ -100,7 +108,9 @@ const OpenSimulation = () => {
}
</Icon>
</ListItemIcon>
<Typography>{simulationName}</Typography>
<Typography style={{ flexGrow: 1 }}>
{simulationName}
</Typography>
</MenuItem>
)
})}

View file

@ -30,7 +30,8 @@ export const EnglishTranslation: Translation = {
refresh: 'Refresh',
undo: 'Undo',
'select all': 'Select all',
'delete selection': 'Delete selection'
'delete selection': 'Delete selection',
'delete simulation': 'Delete simulation'
},
messages: {
createdSimulation: name => `Succesfully created simulation '${name}'`,
@ -40,6 +41,7 @@ export const EnglishTranslation: Translation = {
compiledIc: name => `Succesfully compiled circuit '${name}'`,
cleaned: name => `Succesfully cleaned 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',
refresh: 'Todo',
save: 'Todo',
undo: 'Todo'
undo: 'Todo',
'delete simulation': `Todo`
},
createSimulation: {
mode: {
@ -38,9 +39,10 @@ export const DutchTranslation: Translation = {
switchedToSimulation: name =>
`Succesvol veranderd naar simulatie '${name}'`,
savedSimulation: name => `Simulatie succesvol opgeslagen '${name}'`,
compiledIc: name => `Todo: ${name}`,
cleaned: name => `Todo ${name}`,
refreshed: name => `Todo ${name}`,
undone: name => `Todo ${name}`
compiledIc: name => `IC gecompileerd: ${name}`,
cleaned: name => `${name} gewist`,
refreshed: name => `${name} ververst`,
undone: name => `${name} ongedaan gemaakt`,
deletedSimulation: name => `Todo`
}
}

View file

@ -30,7 +30,8 @@ export const RomanianTranslation: Translation = {
'select all': 'Selectează totul',
clean: 'Curăță',
refresh: 'Reîncarcă',
undo: 'Întoarce'
undo: 'Întoarce',
'delete simulation': 'Șterge simulația'
},
messages: {
createdSimulation: name =>
@ -41,6 +42,8 @@ export const RomanianTranslation: Translation = {
compiledIc: name => `Simulația '${name}' a fost compilată 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`,
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
cleaned: NameSentence
undone: NameSentence
deletedSimulation: NameSentence
}
actions: Record<possibleAction, string>
}

View file

@ -1,7 +1,15 @@
import { LocalStore } from '../../storage/classes/LocalStore'
import { RendererState } from '../types/SimulationSave'
import { initSimulation } from '../helpers/initSimulation'
import { defaultSimulationName } from '../constants'
/**
* 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 { deleteSelection } from './helpers/deleteSelection'
import { cleanRenderer } from './helpers/clean'
import { deleteSimulation } from './helpers/deleteSimulation'
export const actionIcons: Record<possibleAction, string> = {
clean: 'clear',
@ -14,7 +15,8 @@ export const actionIcons: Record<possibleAction, string> = {
save: 'save',
undo: 'undo',
'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']
),
...createActionConfig(
'delete simulation',
{
run: deleteSimulation
},
['ctrl', 'shift', 'delete']
),
...createActionConfig('select all', selectAll, ['ctrl', 'a']),
...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'
| 'select all'
| 'delete selection'
| 'delete simulation'

View file

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

View file

@ -50,4 +50,12 @@ export class LocalStore<T> {
currentData[finalKey] = finalValue
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))
}
}