Skip to content

Commit

Permalink
closes #7
Browse files Browse the repository at this point in the history
  • Loading branch information
seiyria committed Aug 8, 2024
1 parent aa98482 commit 12cf568
Show file tree
Hide file tree
Showing 20 changed files with 540 additions and 79 deletions.
104 changes: 99 additions & 5 deletions app/handlers/map.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,103 @@
export function ensureMap(name: string, mapName: string) {}
import * as childProcess from 'child_process';
import * as fs from 'fs-extra';
import { baseUrl } from '../helpers';

export function newMap(name: string, creator: string) {}
export const fixTiledMapPaths = (map: any) => {
map.tilesets.forEach((tileset: any) => {
tileset.image = `../../__assets/spritesheets/${tileset.name.toLowerCase()}.png`;
});
};

export function renameMap(oldName: string, newName: string) {}
export function ensureMap(mapName: string, mapData: any) {
const path = `${baseUrl}/resources/maps/src/content/maps/custom/${mapName}.json`;
fs.writeFileSync(path, JSON.stringify(mapData));
}

export function editMap(name: string) {}
export function newMap(mapName: string, mapAuthor: string) {
const fileName = mapName.replace(/[^a-zA-Z-]/g, '');
const templatePath = `${baseUrl}/resources/maps/src/content/maps/custom/Template.json`;
const path = `${baseUrl}/resources/maps/src/content/maps/custom/${fileName}.json`;

export function editMapSpawnerNames(oldName: string, newName: string) {}
if (!fs.existsSync(templatePath)) {
throw new Error('Template is gone.');
}

if (fs.existsSync(path)) {
throw new Error('Map already exists');
}

const json = fs.readJSONSync(templatePath);
json.properties.creator = mapAuthor;
json.propertytypes.creator = 'string';

fs.writeJSONSync(path, json);

editMap(fileName);

return json;
}

export function copyMap(mapName: string) {
const oldPath = `${baseUrl}/resources/maps/src/content/maps/custom/${mapName}.json`;
const newPath = `${baseUrl}/resources/maps/src/content/maps/custom/${mapName} (copy).json`;

if (fs.existsSync(newPath)) {
throw new Error('A map by that name already exists.');
}

fs.copySync(oldPath, newPath);
}

export function renameMap(oldName: string, newName: string) {
const oldPath = `${baseUrl}/resources/maps/src/content/maps/custom/${oldName}.json`;
const newPath = `${baseUrl}/resources/maps/src/content/maps/custom/${newName}.json`;

if (fs.existsSync(newPath)) {
throw new Error('A map by that name already exists.');
}

fs.moveSync(oldPath, newPath);
}

export function removeMap(mapName: string) {
const oldPath = `${baseUrl}/resources/maps/src/content/maps/custom/${mapName}.json`;
const newPath = `${baseUrl}/resources/maps/src/content/maps/custom/${mapName}.bak.json`;

fs.moveSync(oldPath, newPath, { overwrite: true });
}

export function editMap(mapName: string) {
if (!fs.existsSync(`${baseUrl}/resources/Tiled`)) {
throw new Error('Tiled is not installed.');
}

const path = `${baseUrl}/resources/maps/src/content/maps/custom/${mapName}.json`;

const map = fs.readJsonSync(path);
fixTiledMapPaths(map);
fs.writeJsonSync(path, map);

childProcess.exec(`${baseUrl}/resources/Tiled/tiled.exe "${path}"`);
}

export function editMapSpawnerNames(oldName: string, newName: string) {
fs.readdirSync(`${baseUrl}/resources/maps/src/content/maps/custom`).forEach(
(file) => {
const path = `${baseUrl}/resources/maps/src/content/maps/custom/${file}`;
const json = fs.readJSONSync(path);

let didWrite = false;

json.layers[10].objects.forEach((spawner: any) => {
if (spawner.tag !== oldName) return;

spawner.tag = newName;
didWrite = true;
});

if (didWrite) {
fs.writeJSONSync(path, json);
}
}
);
}
34 changes: 32 additions & 2 deletions app/ipc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,38 @@ export function setupIPC(sendToUI: SendToUI) {
});

ipcMain.on('RENAME_MAP', async (e: any, data: any) => {
handlers.renameMap(data.oldName, data.newName);
sendToUI('renamemap', data);
try {
handlers.renameMap(data.oldName, data.newName);
sendToUI('renamemap', data);
} catch (e) {
sendToUI('notify', {
type: 'error',
text: 'A map by that name already exists.',
});
}
});

ipcMain.on('REMOVE_MAP', async (e: any, data: any) => {
try {
handlers.removeMap(data.mapName);
} catch (e) {
sendToUI('notify', {
type: 'error',
text: 'Could not fully delete map for some reason.',
});
}
});

ipcMain.on('COPY_MAP', async (e: any, data: any) => {
try {
handlers.copyMap(data.mapName);
sendToUI('copymap', data);
} catch (e) {
sendToUI('notify', {
type: 'error',
text: 'A map by that name already exists.',
});
}
});

ipcMain.on('EDIT_MAP', async (e: any, data: any) => {
Expand Down
11 changes: 11 additions & 0 deletions src/app/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@ import { HomeModule } from './home/home.module';

import { provideHotToastConfig } from '@ngxpert/hot-toast';
import { SweetAlert2Module } from '@sweetalert2/ngx-sweetalert2';
import {
NgxFloatUiModule,
NgxFloatUiPlacements,
NgxFloatUiTriggers,
} from 'ngx-float-ui';
import {
provideNgxWebstorage,
withLocalStorage,
Expand All @@ -29,6 +34,12 @@ import { AppComponent } from './app.component';
SharedModule,
HomeModule,
AppRoutingModule,
NgxFloatUiModule.forRoot({
trigger: NgxFloatUiTriggers.hover,
showDelay: 500,
placement: NgxFloatUiPlacements.TOPEND,
appendTo: 'body',
}),
SweetAlert2Module.forRoot({
provideSwal: () => import('sweetalert2/dist/sweetalert2.js'),
}),
Expand Down
12 changes: 2 additions & 10 deletions src/app/home/home.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,7 @@ import { HomeRoutingModule } from './home-routing.module';

import { SweetAlert2Module } from '@sweetalert2/ngx-sweetalert2';
import { AgGridModule } from 'ag-grid-angular';
import {
NgxFloatUiModule,
NgxFloatUiPlacements,
NgxFloatUiTriggers,
} from 'ngx-float-ui';
import { NgxFloatUiModule } from 'ngx-float-ui';
import { SharedModule } from '../shared/shared.module';
import { DialogsComponent } from '../tabs/dialogs/dialogs.component';
import { DroptablesComponent } from '../tabs/droptables/droptables.component';
Expand Down Expand Up @@ -41,11 +37,7 @@ import { HomeComponent } from './home.component';
HomeRoutingModule,
SweetAlert2Module,
AgGridModule,
NgxFloatUiModule.forRoot({
trigger: NgxFloatUiTriggers.hover,
showDelay: 500,
placement: NgxFloatUiPlacements.TOPEND,
}),
NgxFloatUiModule,
],
})
export class HomeModule {}
6 changes: 5 additions & 1 deletion src/app/services/electron.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ export class ElectronService {
});

window.api.receive('newmap', (mapData) => {
this.modService.addMap(mapData);
this.modService.addMap(mapData as { name: string; map: any });
});

window.api.receive('renamemap', (nameData) => {
Expand All @@ -54,6 +54,10 @@ export class ElectronService {
);
});

window.api.receive('copymap', (nameData) => {
this.modService.copyMap(nameData.mapName as string);
});

window.api.receive('json', (jsonData) => {
this.modService.setJSON(jsonData.name as string, jsonData.data);
});
Expand Down
62 changes: 59 additions & 3 deletions src/app/services/mod.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ export class ModService {
const newModData = this.mod();
this.localStorage.store('mod', newModData);
});

this.ensureMapsExist();
}

// mod functions
Expand Down Expand Up @@ -93,11 +95,43 @@ export class ModService {
}

// map functions
public addMap(map: any) {
if (map.name === 'Template') return;
private ensureMapsExist() {
this.mod().maps.forEach((map) => {
window.api.send('ENSURE_MAP', { ...map });
});
}

public importMap(incomingMap: { name: string; map: any }) {
this.addMap(incomingMap);
window.api.send('ENSURE_MAP', { ...incomingMap });
}

public addMap(incomingMap: { name: string; map: any }) {
if (incomingMap.name === 'Template') return;

const mod = this.mod();
mod.maps.push(map);
if (!mod.meta.name) mod.meta.name = incomingMap.name;

const existingMap = mod.maps.findIndex((x) => x.name === incomingMap.name);
if (existingMap !== -1) {
mod.maps.splice(existingMap, 1, incomingMap);
} else {
mod.maps.push(incomingMap);
}

this.updateMod(mod);
}

public copyMap(mapName: string) {
const mod = this.mod();

const existingMap = mod.maps.find((x) => x.name === mapName);
if (!existingMap) return;

const newMap = structuredClone(existingMap);
newMap.name = `${mapName} (copy)`;

mod.maps.push(newMap);

this.updateMod(mod);
}
Expand All @@ -111,6 +145,28 @@ export class ModService {

mapRef.name = newName;

this.updateMapNameAcrossMod(oldName, newName);
this.updateMod(mod);
}

private updateMapNameAcrossMod(oldName: string, newName: string) {
const mod = this.mod();
mod.drops.forEach((droptable) => {
if (droptable.mapName !== oldName) return;

droptable.mapName = newName;
});

this.updateMod(mod);
}

public removeMap(removeMap: { name: string; map: any }) {
const mod = this.mod();
const existingMap = mod.maps.findIndex((x) => x.name === removeMap.name);
if (existingMap !== -1) {
mod.maps.splice(existingMap, 1);
}

this.updateMod(mod);
}

Expand Down
20 changes: 16 additions & 4 deletions src/app/shared/components/cell-buttons/cell-buttons.component.html
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<div class="flex justify-end items-center h-full">
@if(params.showCopyButton) {
<button class="ml-2 btn btn-sm btn-info" (click)="params.copyCallback?.(params.data)">
<button class="ml-2 btn btn-sm btn-info" (click)="params.copyCallback?.(params.data)" floatUi="Copy">
<svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor"
class="size-6">
<path stroke-linecap="round" stroke-linejoin="round"
Expand All @@ -9,8 +9,19 @@
</button>
}

@if(params.showRenameButton) {
<button class="ml-2 btn btn-sm btn-info" (click)="params.renameCallback?.(params.data)" floatUi="Rename">
<svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor"
class="size-6">
<path stroke-linecap="round" stroke-linejoin="round"
d="M19.5 14.25v-2.625a3.375 3.375 0 0 0-3.375-3.375h-1.5A1.125 1.125 0 0 1 13.5 7.125v-1.5a3.375 3.375 0 0 0-3.375-3.375H8.25m0 12.75h7.5m-7.5 3H12M10.5 2.25H5.625c-.621 0-1.125.504-1.125 1.125v17.25c0 .621.504 1.125 1.125 1.125h12.75c.621 0 1.125-.504 1.125-1.125V11.25a9 9 0 0 0-9-9Z" />
</svg>

</button>
}

@if(params.showEditButton) {
<button class="ml-2 btn btn-sm btn-info" (click)="params.editCallback?.(params.data)">
<button class="ml-2 btn btn-sm btn-info" (click)="params.editCallback?.(params.data)" floatUi="Edit">
<svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor"
class="size-6">
<path stroke-linecap="round" stroke-linejoin="round"
Expand All @@ -20,8 +31,9 @@
}

@if(params.showDeleteButton) {
<button class="ml-2 btn btn-sm btn-warning" [swal]="deleteItem"><svg xmlns="http://www.w3.org/2000/svg" fill="none"
viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor" class="size-6">
<button class="ml-2 btn btn-sm btn-warning" [swal]="deleteItem" floatUi="Delete">
<svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor"
class="size-6">
<path stroke-linecap="round" stroke-linejoin="round"
d="m14.74 9-.346 9m-4.788 0L9.26 9m9.968-3.21c.342.052.682.107 1.022.166m-1.022-.165L18.16 19.673a2.25 2.25 0 0 1-2.244 2.077H8.084a2.25 2.25 0 0 1-2.244-2.077L4.772 5.79m14.456 0a48.108 48.108 0 0 0-3.478-.397m-12 .562c.34-.059.68-.114 1.022-.165m0 0a48.11 48.11 0 0 1 3.478-.397m7.5 0v-.916c0-1.18-.91-2.164-2.09-2.201a51.964 51.964 0 0 0-3.32 0c-1.18.037-2.09 1.022-2.09 2.201v.916m7.5 0a48.667 48.667 0 0 0-7.5 0" />
</svg>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<p>editor-base-table works!</p>
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import { Component, inject, signal } from '@angular/core';
import { ModService } from '../../../services/mod.service';

@Component({
selector: 'app-editor-base-table',
templateUrl: './editor-base-table.component.html',
styleUrl: './editor-base-table.component.scss',
})
export class EditorBaseTableComponent<T> {
protected modService = inject(ModService);

protected defaultData = () => ({} as T);

public isEditing = signal<boolean>(false);
public oldData = signal<T | undefined>(undefined);
public editingData = signal<T>(this.defaultData());

public createNew() {
this.isEditing.set(true);
this.editingData.set(this.defaultData());
}

public editExisting(data: T) {
this.isEditing.set(true);
this.oldData.set(structuredClone(data));
this.editingData.set(data);
}

public cancelEditing() {
this.isEditing.set(false);
}

public saveNewData(data: T) {

Check failure on line 33 in src/app/shared/components/editor-base-table/editor-base-table.component.ts

View workflow job for this annotation

GitHub Actions / build (20)

'data' is defined but never used

Check failure on line 33 in src/app/shared/components/editor-base-table/editor-base-table.component.ts

View workflow job for this annotation

GitHub Actions / build (20)

'data' is defined but never used

Check failure on line 33 in src/app/shared/components/editor-base-table/editor-base-table.component.ts

View workflow job for this annotation

GitHub Actions / build (20)

'data' is defined but never used
this.isEditing.set(false);
}

public deleteData(data: T) {}

Check failure on line 37 in src/app/shared/components/editor-base-table/editor-base-table.component.ts

View workflow job for this annotation

GitHub Actions / build (20)

'data' is defined but never used

Check failure on line 37 in src/app/shared/components/editor-base-table/editor-base-table.component.ts

View workflow job for this annotation

GitHub Actions / build (20)

'data' is defined but never used

Check failure on line 37 in src/app/shared/components/editor-base-table/editor-base-table.component.ts

View workflow job for this annotation

GitHub Actions / build (20)

'data' is defined but never used
}
Loading

0 comments on commit 12cf568

Please sign in to comment.