Skip to content

Commit 324e955

Browse files
committed
made app compatible with nw.js
1 parent 40e2aa6 commit 324e955

File tree

18 files changed

+3037
-74
lines changed

18 files changed

+3037
-74
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
/.sass-cache
2929
/connect.lock
3030
/coverage
31+
/nwjs
3132
/libpeerconnection.log
3233
npm-debug.log
3334
testem.log

backend/src/server.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ const app = express();
1818
app.set('port', process.env.PORT || 8080);
1919
app.use(cors());
2020
app.use('/public', express.static(path.join(__dirname, 'public'), {maxAge: 0}));
21-
app.use('/source', express.static(config.pathToCrosscode, {maxAge: 0}));
21+
app.use(express.static(config.pathToCrosscode, {maxAge: 0}));
2222
// app.use(compression());
2323
app.use(logger('dev'));
2424
app.use(bodyParser.json());

webapp/nwjs/package.json

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"name": "cc-map-editor",
3+
"main": "index.html",
4+
"version": "0.0.1",
5+
"window": {
6+
"title": "CrossCode Map Editor",
7+
"width": 1280,
8+
"height": 720,
9+
"icon": "favicon.ico"
10+
}
11+
}

webapp/package.json

Lines changed: 54 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1,54 +1,56 @@
11
{
2-
"name": "map-editor",
3-
"version": "0.0.0",
4-
"license": "MIT",
5-
"scripts": {
6-
"ng": "ng",
7-
"start": "ng serve",
8-
"build": "ng build",
9-
"test": "ng test",
10-
"lint": "ng lint"
11-
},
12-
"private": true,
13-
"dependencies": {
14-
"@angular/animations": "^4.3.5",
15-
"@angular/cdk": "^2.0.0-beta.8",
16-
"@angular/common": "^4.2.4",
17-
"@angular/compiler": "^4.2.4",
18-
"@angular/core": "^4.2.4",
19-
"@angular/flex-layout": "^2.0.0-beta.8",
20-
"@angular/forms": "^4.2.4",
21-
"@angular/http": "^4.2.4",
22-
"@angular/material": "^2.0.0-beta.8",
23-
"@angular/platform-browser": "^4.2.4",
24-
"@angular/platform-browser-dynamic": "^4.2.4",
25-
"@angular/router": "^4.2.4",
26-
"core-js": "^2.4.1",
27-
"jsoneditor": "^5.9.6",
28-
"phaser-ce": "^2.8.7",
29-
"rxjs": "^5.4.2",
30-
"zone.js": "^0.8.14"
31-
},
32-
"devDependencies": {
33-
"@angular/cli": "1.3.1",
34-
"@angular/compiler-cli": "^4.2.4",
35-
"@angular/language-service": "^4.2.4",
36-
"@types/jasmine": "~2.5.53",
37-
"@types/jasminewd2": "~2.0.2",
38-
"@types/jsoneditor": "^5.5.2",
39-
"@types/node": "~6.0.60",
40-
"codelyzer": "~3.1.1",
41-
"jasmine-core": "~2.6.2",
42-
"jasmine-spec-reporter": "~4.1.0",
43-
"karma": "~1.7.0",
44-
"karma-chrome-launcher": "~2.1.1",
45-
"karma-cli": "~1.0.1",
46-
"karma-coverage-istanbul-reporter": "^1.2.1",
47-
"karma-jasmine": "~1.1.0",
48-
"karma-jasmine-html-reporter": "^0.2.2",
49-
"protractor": "~5.1.2",
50-
"ts-node": "~3.2.0",
51-
"tslint": "~5.3.2",
52-
"typescript": "~2.3.3"
53-
}
2+
"name": "cc-map-editor",
3+
"version": "0.1.0",
4+
"license": "MIT",
5+
"scripts": {
6+
"ng": "ng",
7+
"start": "ng serve",
8+
"build": "ng build && cpx nwjs/package.json dist",
9+
"test": "ng test",
10+
"lint": "ng lint"
11+
},
12+
"private": true,
13+
"dependencies": {
14+
"@angular/animations": "^4.3.5",
15+
"@angular/cdk": "^2.0.0-beta.8",
16+
"@angular/common": "^4.2.4",
17+
"@angular/compiler": "^4.2.4",
18+
"@angular/core": "^4.2.4",
19+
"@angular/flex-layout": "^2.0.0-beta.8",
20+
"@angular/forms": "^4.2.4",
21+
"@angular/http": "^4.2.4",
22+
"@angular/material": "^2.0.0-beta.8",
23+
"@angular/platform-browser": "^4.2.4",
24+
"@angular/platform-browser-dynamic": "^4.2.4",
25+
"@angular/router": "^4.2.4",
26+
"core-js": "^2.4.1",
27+
"jsoneditor": "^5.9.6",
28+
"phaser-ce": "^2.8.7",
29+
"rxjs": "^5.4.2",
30+
"zone.js": "^0.8.14"
31+
},
32+
"devDependencies": {
33+
"@angular/cli": "1.3.1",
34+
"@angular/compiler-cli": "^4.2.4",
35+
"@angular/language-service": "^4.2.4",
36+
"@types/jasmine": "~2.5.53",
37+
"@types/jasminewd2": "~2.0.2",
38+
"@types/jsoneditor": "^5.5.2",
39+
"@types/node": "~6.0.60",
40+
"@types/nw.js": "^0.13.7",
41+
"codelyzer": "~3.1.1",
42+
"cpx": "^1.5.0",
43+
"jasmine-core": "~2.6.2",
44+
"jasmine-spec-reporter": "~4.1.0",
45+
"karma": "~1.7.0",
46+
"karma-chrome-launcher": "~2.1.1",
47+
"karma-cli": "~1.0.1",
48+
"karma-coverage-istanbul-reporter": "^1.2.1",
49+
"karma-jasmine": "~1.1.0",
50+
"karma-jasmine-html-reporter": "^0.2.2",
51+
"protractor": "~5.1.2",
52+
"ts-node": "~3.2.0",
53+
"tslint": "~5.3.2",
54+
"typescript": "~2.3.3"
55+
}
5456
}

webapp/src/app/app.module.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ import {Vec2WidgetComponent} from './sidenav/entities/widgets/vec2-widget/vec2-w
3333
import {AddEntityMenuComponent} from './editor/add-entity-menu/add-entity-menu.component';
3434
import {HttpClientModule} from '@angular/common/http';
3535
import {JsonEditorComponent} from './shared/json-editor/json-editor.component';
36+
import {HttpClientService} from './shared/http-client.service';
3637

3738
@NgModule({
3839
declarations: [
@@ -87,6 +88,7 @@ import {JsonEditorComponent} from './shared/json-editor/json-editor.component';
8788
],
8889
providers: [
8990
MapLoaderService,
91+
HttpClientService,
9092
GlobalEventsService,
9193
WidgetRegistryService,
9294
],

webapp/src/app/phaser/phaser.component.ts

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,14 @@ import {AfterViewInit, Component, ElementRef, HostListener, OnDestroy, OnInit} f
22
import * as Phaser from 'phaser-ce';
33
import {MapLoaderService} from '../shared/map-loader.service';
44
import {Subscription} from 'rxjs/Subscription';
5-
import {CrossCodeMap} from '../shared/interfaces/cross-code-map';
65
import {MapPan} from '../shared/phaser/map-pan';
76
import {CCMap} from '../shared/phaser/tilemap/cc-map';
87
import {TileDrawer} from '../shared/phaser/tilemap/tile-drawer';
98
import {EntityManager} from '../shared/phaser/entities/entity-manager';
109
import {GlobalEventsService} from '../shared/global-events.service';
1110
import {EditorView} from '../shared/interfaces/editor-view';
1211
import {Globals} from '../shared/globals';
13-
import {PropSheet, ScalableProp} from '../shared/interfaces/props';
14-
import {HttpClient} from '@angular/common/http';
12+
import {HttpClientService} from '../shared/http-client.service';
1513

1614
@Component({
1715
selector: 'app-phaser',
@@ -33,11 +31,11 @@ export class PhaserComponent implements OnInit, OnDestroy {
3331
constructor(private element: ElementRef,
3432
private mapLoader: MapLoaderService,
3533
private globalEvents: GlobalEventsService,
36-
private http: HttpClient) {
34+
private http: HttpClientService) {
3735
}
3836

3937
ngOnInit() {
40-
this.http.get(Globals.URL + 'api/allFiles').subscribe(res => {
38+
this.http.getAllFiles().subscribe(res => {
4139
this.game = new Phaser.Game(screen.width * window.devicePixelRatio, screen.height * window.devicePixelRatio, Phaser.CANVAS, 'content', {
4240
create: () => this.create(),
4341
update: () => this.update(),
@@ -101,13 +99,13 @@ export class PhaserComponent implements OnInit, OnDestroy {
10199

102100
preload(res) {
103101
// res.data.forEach(json => {
104-
// this.game.load.json(json, Globals.URL + 'source/' + json);
102+
// this.game.load.json(json, Globals.URL + json);
105103
// });
106104
res.images.forEach(img => {
107-
this.game.load.image(img, Globals.URL + 'source/' + img);
105+
this.game.load.image(img, Globals.URL + img);
108106
});
109107

110-
this.game.load.json('definitions.json', Globals.URL + 'public/definitions.json');
108+
this.game.load.json('definitions.json', 'assets/definitions.json');
111109
this.game.load.crossOrigin = 'anonymous';
112110

113111
this.game.load.onLoadComplete.addOnce(() => {

webapp/src/app/shared/globals.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
export class Globals {
2+
static isNwjs = false;
23
static game: Phaser.Game;
34
static TILE_SIZE = 16;
45
static URL = 'http://localhost:8080/';
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
import {Injectable} from '@angular/core';
2+
import {HttpClient} from '@angular/common/http';
3+
import {Observable} from 'rxjs/Observable';
4+
import {Globals} from './globals';
5+
6+
declare let nw: any;
7+
declare let chrome: any;
8+
9+
@Injectable()
10+
export class HttpClientService {
11+
12+
private fs;
13+
private path;
14+
private config;
15+
16+
constructor(private http: HttpClient) {
17+
if (Globals.isNwjs) {
18+
this.fs = requireNode('fs');
19+
this.path = requireNode('path');
20+
try {
21+
this.config = JSON.parse(this.fs.readFileSync(this.path.join(nw.App.dataPath, 'config.json')));
22+
Globals.URL = 'file:///' + this.config.pathToCrosscode;
23+
} catch (e) {
24+
this.selectCcFolder();
25+
}
26+
}
27+
}
28+
29+
getAllFiles(): Observable<Object> {
30+
if (!Globals.isNwjs) {
31+
return this.http.get(Globals.URL + 'api/allFiles');
32+
}
33+
return new Observable(obs => {
34+
try {
35+
const o = {
36+
images: this.listAllFiles(this.config.pathToCrosscode + 'media/', [], 'png'),
37+
data: this.listAllFiles(this.config.pathToCrosscode + 'data/', [], 'json')
38+
};
39+
40+
obs.next(o);
41+
obs.complete();
42+
} catch (e) {
43+
console.error(e);
44+
this.selectCcFolder();
45+
}
46+
});
47+
}
48+
49+
private selectCcFolder() {
50+
const fs = this.fs;
51+
const path = this.path;
52+
const dirInput = document.getElementById('inputDirectory');
53+
54+
dirInput.addEventListener('change', function (evt) {
55+
const ccPath: string = (<any>this).value + '\\';
56+
fs.writeFileSync(path.join(nw.App.dataPath, 'config.json'), JSON.stringify({pathToCrosscode: ccPath}, null, 2));
57+
console.log(nw.App.dataPath);
58+
chrome.runtime.reload();
59+
}, false);
60+
61+
dirInput.click();
62+
}
63+
64+
private listAllFiles(dir: string, filelist, ending: string): string[] {
65+
const files = this.fs.readdirSync(dir);
66+
const that = this;
67+
filelist = filelist || [];
68+
files.forEach(function (file) {
69+
if (that.fs.statSync(dir + file).isDirectory()) {
70+
filelist = that.listAllFiles(dir + file + '/', filelist, ending);
71+
} else if (!ending || file.toLowerCase().endsWith(ending.toLowerCase())) {
72+
filelist.push((dir + file).split(that.config.pathToCrosscode)[1]);
73+
}
74+
});
75+
return filelist;
76+
77+
}
78+
}

webapp/src/app/shared/json-editor/json-editor.component.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ export class JsonEditorComponent implements AfterViewInit {
1313

1414
private editor: JSONEditor;
1515
private options: JSONEditorOptions;
16-
private data;
16+
data;
1717
private key: string;
1818
json = JSON;
1919

webapp/src/app/shared/phaser/helper.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ export class Helper {
9696
}
9797

9898
// load json
99-
game.load.json(key, Globals.URL + 'source/' + key + '.json');
99+
game.load.json(key, Globals.URL + key + '.json');
100100
game.load.onLoadComplete.addOnce(() => {
101101
return callback(game.cache.getJSON(key));
102102
});

0 commit comments

Comments
 (0)