import {Component, NgZone, OnInit, ViewChild} from '@angular/core'; import {ElectronService} from './core/services'; import {APP_CONFIG} from '../environments/environment'; import {ModelComponentType} from "./game-model/ModelComponentType"; import {MatDrawerContainer} from "@angular/material/sidenav"; import {ModelComponentTypeUtillities} from "./game-model/ModelComponentTypeUtillities"; import {GameModel} from "./game-model/GameModel"; import {EditorComponent} from "./editor/editor.component"; import {ModelComponent} from "./game-model/ModelComponent"; import { ScriptAccountOverviewComponent } from "./side-overviews/script-account-overview/script-account-overview.component"; import {MatDialog} from "@angular/material/dialog"; import {DeleteConfirmationDialogComponent} from "./delete-confirmation-dialog/delete-confirmation-dialog.component"; import {ScriptAccount} from "./game-model/scriptAccounts/ScriptAccount"; import {GamescriptOverviewComponent} from "./side-overviews/gamescript-overview/gamescript-overview.component"; import {SimpleGamesystem} from "./game-model/gamesystems/SimpleGamesystem"; import {ProductGamesystem} from "./game-model/gamesystems/ProductGamesystem"; import {ProductState} from "./game-model/gamesystems/ProductState"; @Component({ selector: 'app-root', templateUrl: './app.component.html', styleUrls: ['./app.component.css'] }) export class AppComponent implements OnInit{ openContent : ModelComponentType | undefined = undefined; @ViewChild('drawer') drawer: MatDrawerContainer|undefined @ViewChild('editor') editor: EditorComponent|undefined @ViewChild('scriptAccountOverview') scriptAccountOverview: ScriptAccountOverviewComponent | undefined @ViewChild('gamesystemOverview') gamesystemOverview: GamescriptOverviewComponent | undefined gameModel: GameModel | undefined constructor(private electronService: ElectronService, private zone: NgZone, private dialog: MatDialog ) { console.log('APP_CONFIG', APP_CONFIG); if (electronService.isElectron) { console.log(process.env); console.log('Run in electron'); console.log('Electron ipcRenderer', this.electronService.ipcRenderer); console.log('NodeJS childProcess', this.electronService.childProcess); electronService.ipcRenderer.on('context-menu', (event: any, message: string) => { this.zone.run(() => { this.onContextMenuMessageRecieved(message); }); }) } else { console.log('Run in browser'); } } onContextMenuMessageRecieved(message: string) { if(message == "edit") { this.onEditModelComponent(); } else if(message == "delete") { this.onDeleteModelComponent(); } else if(message.startsWith("new")) { const splittedMessage = message.split("-"); const modelComponentType = ModelComponentTypeUtillities.fromString(splittedMessage[1]); if(modelComponentType != undefined) { this.onCreateModelComponent(modelComponentType); } else { console.log("[ERROR] [App-Component] Unknown Context-Menu Command!") } } } private onEditModelComponent() { switch (this.openContent!) { case ModelComponentType.SCRIPTACCOUNT: { if(this.scriptAccountOverview!.selectedScriptAccount != undefined) { this.editor!.openGameModelComponent(this.scriptAccountOverview!.selectedScriptAccount); } } break; case ModelComponentType.GAMESYTEM: { if(this.gamesystemOverview!.selectedGamesystem != undefined) { const gamesystem = this.gameModel!.findGamesystem(this.gamesystemOverview!.selectedGamesystemName!); this.editor!.openGameModelComponent(gamesystem!); } } break } } private onDeleteModelComponent() { const affectedModelComponent = this.getSelectedModelComponent(); const dialogRef = this.dialog.open(DeleteConfirmationDialogComponent, {data: affectedModelComponent, minWidth: "400px"}); dialogRef.afterClosed().subscribe(res => { if(res != undefined && res) { if(affectedModelComponent instanceof ScriptAccount) { this.gameModel!.removeScriptAccount(affectedModelComponent); } } }) } private onCreateModelComponent(modelComponentType: ModelComponentType) { switch (modelComponentType) { case ModelComponentType.SCRIPTACCOUNT: this.onCreateNewScriptAccount(); break case ModelComponentType.GAMESYTEM: this.onCreateNewGamesystem(); break } } private onCreateNewScriptAccount() { const createdScriptAccount = this.gameModel!.createScriptAccount("New ScriptAccount"); if(createdScriptAccount != undefined) { this.editor?.openGameModelComponent(createdScriptAccount); } else { console.log("[DEBUG] [App-Component] ScriptAccount could not be created (Name not unique)"); } } private onCreateNewGamesystem() { let parentGamesystemName = undefined if(this.openContent != ModelComponentType.GAMESYTEM) { this.openGamesystemsOverview(); } else { parentGamesystemName = this.gamesystemOverview!.selectedGamesystemName; } const createdGamesystem = this.gameModel!.createGamesystem("New Gamesystem", parentGamesystemName); if(createdGamesystem != undefined) { this.gamesystemOverview!.refresh(); this.editor?.openGameModelComponent(createdGamesystem!); } } private getSelectedModelComponent(): ModelComponent | undefined { if(this.openContent == ModelComponentType.SCRIPTACCOUNT) { if(this.scriptAccountOverview != undefined) { return this.scriptAccountOverview!.selectedScriptAccount; } else { console.log("[WARN] [App.component] ScriptAccountOverview is undefined") } } return undefined; } ngOnInit() { this.gameModel = new GameModel("No More"); this.gameModel.createScriptAccount("Temperature"); this.gameModel.createScriptAccount("Luftfeuchtigkeit"); const weather = new SimpleGamesystem("Weather"); const season = new SimpleGamesystem("Season"); const springState = season.createState("Spring", "Spring, also known as springtime, is one of the four temperate seasons, succeeding winter and preceding summer."); const summerState = season.createState("Summer", "Summer is the hottest and brightest of the four temperate seasons, occurring after spring and before autumn. "); const sunnyState = weather.createState("Sunny", "The sun is shining. No clouds, no rain, no storm."); const rainingState = weather.createState("Raining", "It rains") season.createTransition(springState!, summerState!); weather.createTransition(sunnyState!, rainingState!); const weather_season = new ProductGamesystem("Weather-Season"); weather_season.addChildGamesystem(weather); weather_season.addChildGamesystem(season); weather_season.createState(ProductState.computeProductStateLabel([springState!, sunnyState!]), "", [springState!, sunnyState!]); this.gameModel.addGamesystem(weather_season); } openScriptAccountsOverview() { this.openContent = ModelComponentType.SCRIPTACCOUNT this.drawer!.open(); } openGamesystemsOverview() { this.openContent = ModelComponentType.GAMESYTEM; this.drawer!.open(); } protected readonly ModelComponentType = ModelComponentType; closeContentOverview() { this.drawer!.close(); this.openContent = undefined; } protected readonly ModelComponentTypeUtillities = ModelComponentTypeUtillities; openModelComponent(modelComponent: ModelComponent) { if(this.editor != undefined) { this.editor.openGameModelComponent(modelComponent); } else { console.log("[WARN] [App.Component] Editor is undefined") } } onModelNameUpdate() { if(this.openContent == ModelComponentType.GAMESYTEM) { this.gamesystemOverview!.refresh(); } } }