From 93cd617352d3bbc5e40b5106fa7d5d854e6a9cd9 Mon Sep 17 00:00:00 2001 From: cophilot Date: Sun, 22 Dec 2024 13:51:20 +0100 Subject: [PATCH] update: Sun 22 Dec 2024 01:51:20 PM CET --- .../add-object-form.component.html | 6 +- .../add-object-form.component.sass | 1 + .../add-object-form.component.ts | 57 +++++++++++++++---- src/app/home/home.component.html | 2 + .../object-meta-data.component.html | 36 ++++++++---- .../object-meta-data.component.ts | 13 +++++ src/app/service/object.service.ts | 32 +++++++++++ 7 files changed, 123 insertions(+), 24 deletions(-) diff --git a/src/app/add-object-form/add-object-form.component.html b/src/app/add-object-form/add-object-form.component.html index 552ad04..eca13c8 100644 --- a/src/app/add-object-form/add-object-form.component.html +++ b/src/app/add-object-form/add-object-form.component.html @@ -1,6 +1,6 @@
-

Add Object

+

{{ heading }}

Wrong input!

Name:

@@ -41,6 +41,8 @@

Add Object

- +
diff --git a/src/app/add-object-form/add-object-form.component.sass b/src/app/add-object-form/add-object-form.component.sass index ec7d57b..6af5409 100644 --- a/src/app/add-object-form/add-object-form.component.sass +++ b/src/app/add-object-form/add-object-form.component.sass @@ -19,6 +19,7 @@ animation: slideInFromRight 0.3s box-shadow: 0 0 10px 0 #ffffff color: white + z-index: 1000 .row display: flex flex-direction: row diff --git a/src/app/add-object-form/add-object-form.component.ts b/src/app/add-object-form/add-object-form.component.ts index 24d8558..a59e45b 100644 --- a/src/app/add-object-form/add-object-form.component.ts +++ b/src/app/add-object-form/add-object-form.component.ts @@ -1,9 +1,10 @@ -import { Component, Output, EventEmitter } from '@angular/core'; +import { Component, Output, EventEmitter, Input } from '@angular/core'; import { ObjectService } from '../service/object.service'; import { VVector } from '../utils/VVector'; import { CdkDrag } from '@angular/cdk/drag-drop'; import { NgIf } from '@angular/common'; import { FormsModule } from '@angular/forms'; +import { Obj } from '../utils/Obj'; @Component({ selector: 'app-add-object-form', @@ -13,6 +14,10 @@ import { FormsModule } from '@angular/forms'; standalone: true, }) export class AddObjectFormComponent { + @Input() editObject: Obj | undefined; + + heading = 'Add Object'; + name: string = ''; startX: number = 0; startY: number = 0; @@ -28,26 +33,58 @@ export class AddObjectFormComponent { constructor(private objectService: ObjectService) {} + ngOnInit() { + if (!this.editObject) { + return; + } + + this.name = this.editObject.name; + this.startX = this.editObject.x; + this.startY = this.editObject.y; + this.vX = this.editObject.velocity.x; + this.vY = this.editObject.velocity.y; + this.radius = this.editObject.radius; + this.weight = this.editObject.weight; + this.color = this.editObject.color; + this.heading = 'Edit Object'; + } + add() { if (this.name === '' || this.radius <= 0 || this.color === '') { this.wrongInput = true; return; } - this.objectService.addNewObject( - this.name, - this.startX, - this.startY, - this.radius, - this.weight, - new VVector(this.vX, this.vY), - this.color - ); + + if (this.editObject) { + this.objectService.editObject( + this.name, + this.startX, + this.startY, + this.radius, + this.weight, + new VVector(this.vX, this.vY), + this.color + ); + } else { + this.objectService.addNewObject( + this.name, + this.startX, + this.startY, + this.radius, + this.weight, + new VVector(this.vX, this.vY), + this.color + ); + } + this.name = ''; this.startX = 0; this.startY = 0; this.radius = 10; this.color = 'red'; this.wrongInput = false; + this.editObject = undefined; + this.closeForm.emit(); } close() { diff --git a/src/app/home/home.component.html b/src/app/home/home.component.html index a0e92e2..cbd3eb4 100644 --- a/src/app/home/home.component.html +++ b/src/app/home/home.component.html @@ -15,10 +15,12 @@ *ngFor="let object of getObjectService().getObjects()" [object]="object" > + +
- {{ getObjectService().getSelectedObjectAsObject().name }} + {{ obj().name }}
- +
X - {{ trimNumber(getObjectService().getSelectedObjectAsObject().x) }} + {{ trimNumber(obj().x) }}
Y - {{ trimNumber(getObjectService().getSelectedObjectAsObject().y) }} + {{ trimNumber(obj().y) }} +
+
+
+ Absolut Velocity + {{ + trimNumber(getAbsolutVelocity(obj().velocity.x, obj().velocity.y)) + }}
@@ -29,13 +40,9 @@ {{ "(" + - trimNumber( - getObjectService().getSelectedObjectAsObject().velocity.x - ) + + trimNumber(obj().velocity.x) + " , " + - trimNumber( - getObjectService().getSelectedObjectAsObject().velocity.y - ) + + trimNumber(obj().velocity.y) + ")" }} @@ -43,11 +50,16 @@
Weight - {{ getObjectService().getSelectedObjectAsObject().weight }} + {{ addTousandSeparator(obj().weight) }}
Radius - {{ getObjectService().getSelectedObjectAsObject().radius }} + {{ obj().radius }}
+ diff --git a/src/app/object-meta-data/object-meta-data.component.ts b/src/app/object-meta-data/object-meta-data.component.ts index 72ca052..d634195 100644 --- a/src/app/object-meta-data/object-meta-data.component.ts +++ b/src/app/object-meta-data/object-meta-data.component.ts @@ -7,13 +7,26 @@ import { ObjectService } from '../service/object.service'; styleUrls: ['./object-meta-data.component.sass'], }) export class ObjectMetaDataComponent { + showAddObjectForm = false; constructor(private objectService: ObjectService) {} getObjectService() { return this.objectService; } + obj() { + return this.objectService.getSelectedObjectAsObject(); + } + trimNumber(number: number): string { return number.toFixed(1); } + + addTousandSeparator(number: number): string { + return number.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ','); + } + + getAbsolutVelocity(x: number, y: number): number { + return Math.sqrt(x * x + y * y); + } } diff --git a/src/app/service/object.service.ts b/src/app/service/object.service.ts index 35b0026..a806990 100644 --- a/src/app/service/object.service.ts +++ b/src/app/service/object.service.ts @@ -24,6 +24,31 @@ export class ObjectService { return this.objects; } + checkName(name: string) { + return !this.objects.find((object) => object.name === name); + } + + editObject( + name: string, + startX: number, + startY: number, + radius: number, + weight: number, + velocity: VVector, + color: string = 'red' + ): void { + if (!this.selectedObject) { + return; + } + this.deleteObject(this.selectedObject.id, false); + this.addObject( + new Obj(name, startX, startY, radius, weight, velocity, color), + false + ); + this.selectedObject = null; + LocalStorageService.saveObjects(this.objects); + } + addNewObject( name: string, startX: number, @@ -81,6 +106,13 @@ export class ObjectService { this.selectedObject = null; } + editSelectedObject(): void { + if (this.selectedObject) { + this.deleteObject(this.selectedObject.id); + } + this.selectedObject = null; + } + getObjectsForExport(): any[] { return this.objects.map((object) => { return {