Skip to content

Commit

Permalink
fix empty names and add sanitization of names
Browse files Browse the repository at this point in the history
  • Loading branch information
Derstilon committed Oct 6, 2023
1 parent 0d9952f commit f3e5138
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 12 deletions.
7 changes: 5 additions & 2 deletions src/ThreeEditor/Simulation/Materials/MaterialManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,13 @@ export class MaterialManager extends THREE.Object3D implements SimulationPropert
private createMaterialPrefabs = () => {
const { editor } = this;
const editorMaterials = MATERIALS.reduce(
([prevMaterials, prevOptions], { name, icru, density }) => [
(
[prevMaterials, prevOptions],
{ name, sanitized_name: sanitizedName, icru, density }
) => [
{
...prevMaterials,
[icru]: new SimulationMaterial(editor, name, icru, density)
[icru]: new SimulationMaterial(editor, name, sanitizedName, icru, density)
},
{
...prevOptions,
Expand Down
11 changes: 7 additions & 4 deletions src/ThreeEditor/Simulation/Materials/SimulationMaterial.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,19 @@ test('constructor default', () => {
});

test('constructor params', () => {
const material = new SimulationMaterial(editor, 'test', 1, 2);
const material = new SimulationMaterial(editor, 'test', 'test', 1, 2);
expect(material).toBeInstanceOf(SimulationMaterial);
expect(material.icru).toBe(1);
expect(material.density).toBe(2);
expect(material.name).toBe('test');
});

test('toJSON', () => {
const material = new SimulationMaterial(editor, 'test', 1, 2);
const material = new SimulationMaterial(editor, 'test', 'test', 1, 2);
expect(material.toJSON()).toEqual({
uuid: material.uuid,
name: 'test',
sanitizedName: 'test',
icru: 1,
density: 2
});
Expand All @@ -42,6 +43,7 @@ test('fromJSON', () => {
const material = SimulationMaterial.fromJSON(editor, {
uuid: 'testUuid',
name: 'test',
sanitizedName: 'test',
icru: 1,
density: 2
});
Expand All @@ -55,6 +57,7 @@ test('fromJSON', () => {
const material2 = SimulationMaterial.fromJSON(editor, {
uuid: 'testUuid',
name: 'test',
sanitizedName: 'test',
icru: 1,
density: 2,
customStoppingPower: true
Expand All @@ -69,7 +72,7 @@ test('fromJSON', () => {
});

test('clone', () => {
const material = new SimulationMaterial(editor, 'test', 1, 2);
const material = new SimulationMaterial(editor, 'test', 'test', 1, 2);
material.customStoppingPower = true;
const clone = material.clone();
expect(clone).toBeInstanceOf(SimulationMaterial);
Expand All @@ -81,7 +84,7 @@ test('clone', () => {
});

test('copy', () => {
const material = new SimulationMaterial(editor, 'test', 1, 2);
const material = new SimulationMaterial(editor, 'test', 'test', 1, 2);
material.customStoppingPower = true;
const copy = new SimulationMaterial(editor);
copy.copy(material);
Expand Down
10 changes: 5 additions & 5 deletions src/ThreeEditor/Simulation/Materials/materials.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ import re
densities = []
additional_materials = [
{ 'icru': 1000, 'name': 'VACUUM', 'density': 0 },
{ 'icru': 0, 'name': 'BLACK HOLE', 'density': 0 }
{ 'icru': 1000, 'name': 'VACUUM', 'sanitized_name': 'vacuum', 'density': 0 },
{ 'icru': 0, 'name': 'BLACK HOLE', 'sanitized_name': 'black_hole', 'density': 0 }
]
with open('./src/shieldhit/microdhi/icru.f', 'r') as icru_file:
for line in icru_file.readlines()[494:565]:
Expand All @@ -26,7 +26,7 @@ with open('./src/shieldhit/microdhi/icru.f', 'r') as icru_file:
densities.extend(numbers)
for material in additional_materials:
print(f" {{ icru: {material['icru']}, name: '{material['name']}', density: {material['density']} }},")
print(f" {{ icru: {material['icru']}, name: '{material['name']}', sanitized_name: '{material['sanitized_name']}', density: {material['density']} }},")
for i, elem in enumerate(elements):
if i > 0 and i < 99:
Expand All @@ -52,8 +52,8 @@ with open('./doc/tex/UsersGuide/07_reftab.tex', 'r') as infile:
*/

export const MATERIALS = [
{ icru: 1000, name: 'VACUUM', density: 0 },
{ icru: 0, name: 'BLACK HOLE', density: 0 },
{ icru: 1000, name: 'VACUUM', sanitized_name: 'vacuum', density: 0 },
{ icru: 0, name: 'BLACK HOLE', sanitized_name: 'black_hole', density: 0 },
{ icru: 1, name: 'HYDROGEN', sanitized_name: 'H', density: 8.3748e-5 },
{ icru: 2, name: 'HELIUM', sanitized_name: 'He', density: 0.000166322 },
{ icru: 3, name: 'LITHIUM', sanitized_name: 'Li', density: 0.534 },
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,12 @@ export function ObjectInfo(props: { editor: YaptideEditor; object: Object3D }) {
value={watchedObject.name}
onChange={value => {
editor.execute(
new SetValueCommand(editor, watchedObject.object, 'name', value)
new SetValueCommand(
editor,
watchedObject.object,
'name',
value.length > 0 ? value : watchedObject.type
)
);
}}
/>
Expand Down

0 comments on commit f3e5138

Please sign in to comment.