@packtrack/layout 1.6.5 → 1.6.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -15,7 +15,7 @@ export declare class Layout {
15
15
  monitors: Monitor[];
16
16
  throttles: Throttle[];
17
17
  get allDistricts(): District[];
18
- static from(document: any): Layout;
18
+ static from(railway: any): Layout;
19
19
  loadMonitor(source: any, parent: District | Layout): Monitor;
20
20
  loadThrottle(source: any, parent: District | Layout): Throttle;
21
21
  loadDistrict(source: any, parent: District | Layout): District;
@@ -35,9 +35,8 @@ export class Layout {
35
35
  }
36
36
  return districts;
37
37
  }
38
- static from(document) {
38
+ static from(railway) {
39
39
  const layout = new Layout();
40
- const railway = document.firstChild;
41
40
  layout.name = railway.getAttribute('name');
42
41
  const version = railway.getAttribute('version');
43
42
  if (version == '1') {
@@ -1,5 +1,6 @@
1
1
  import { SectionPosition } from "./position";
2
2
  import { Router } from "./router";
3
+ import { Span } from "./span";
3
4
  export class Section {
4
5
  name;
5
6
  district;
@@ -60,53 +61,33 @@ export class Section {
60
61
  }
61
62
  }
62
63
  }
64
+ // TODO verifiy reverse
63
65
  getTilesInRange(startPosition, endPosition) {
64
- if (startPosition.section != this && endPosition.section != this) {
65
- return {
66
- offset: {
67
- start: 0,
68
- end: this.tiles[this.tiles.length - 1].pattern.length
69
- },
70
- tiles: [...this.tiles]
71
- };
72
- }
73
- let start = 0;
74
- let end = this.length;
75
- // only use the position limit if it is within our section
76
- if (startPosition.section == this) {
77
- end = startPosition.absolutePosition;
78
- }
79
- if (endPosition.section == this) {
80
- start = endPosition.absolutePosition;
81
- }
82
- // flip if the range was reversed
83
- if (end < start) {
84
- const small = end;
85
- end = start;
86
- start = small;
87
- }
88
- let passed = 0;
66
+ const span = Span.trail(startPosition, endPosition);
89
67
  const tiles = [];
90
- const tileUnitLength = this.length / this.tileLength;
91
- const offset = {
92
- start: 0,
93
- end: 0
94
- };
95
- for (let tile of this.tiles) {
96
- const length = tile.pattern.length * tileUnitLength;
97
- if (start - length <= passed && end + length >= passed) {
98
- tiles.push(tile);
99
- if (start <= passed) {
100
- offset.start = (start + length - passed) * tile.pattern.length / length;
101
- }
102
- if (end >= passed) {
103
- offset.end = 0.5; // (start + length - passed) * tile.pattern.length / length;
104
- }
68
+ let sectionLength = 0;
69
+ if (startPosition.section == endPosition.section) {
70
+ sectionLength = startPosition.section.length;
71
+ tiles.push(...startPosition.section.tiles);
72
+ }
73
+ else {
74
+ sectionLength += startPosition.section.length;
75
+ tiles.push(...startPosition.section.tiles);
76
+ for (let inside of span.inside) {
77
+ sectionLength += inside.length;
78
+ tiles.push(...inside.tiles);
105
79
  }
106
- passed += length;
80
+ sectionLength += endPosition.section.length;
81
+ tiles.push(...endPosition.section.tiles);
82
+ }
83
+ let tileLength = 0;
84
+ for (let tile of tiles) {
85
+ tileLength += tile.pattern.length;
107
86
  }
87
+ const start = tileLength / sectionLength * startPosition.absolutePosition;
88
+ const end = tileLength - tileLength / sectionLength * (endPosition.section.length - endPosition.absolutePosition);
108
89
  return {
109
- offset,
90
+ offset: { start, end },
110
91
  tiles
111
92
  };
112
93
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@packtrack/layout",
3
- "version": "1.6.5",
3
+ "version": "1.6.7",
4
4
  "type": "module",
5
5
  "main": ".built/index.js",
6
6
  "typings": ".built/index.d.ts",
package/source/layout.ts CHANGED
@@ -45,10 +45,8 @@ export class Layout {
45
45
  return districts;
46
46
  }
47
47
 
48
- static from(document: any) {
48
+ static from(railway: any) {
49
49
  const layout = new Layout();
50
-
51
- const railway = document.firstChild!;
52
50
  layout.name = railway.getAttribute('name');
53
51
 
54
52
  const version = railway.getAttribute('version');
package/source/section.ts CHANGED
@@ -2,6 +2,7 @@ import { District } from "./district";
2
2
  import { SectionPosition } from "./position";
3
3
  import { PowerDistrict } from "./power-district/index";
4
4
  import { Router } from "./router";
5
+ import { Span } from "./span";
5
6
  import { Tile } from "./tile";
6
7
  import { Track } from "./track";
7
8
 
@@ -80,68 +81,40 @@ export class Section {
80
81
  }
81
82
  }
82
83
 
84
+ // TODO verifiy reverse
83
85
  getTilesInRange(startPosition: SectionPosition, endPosition: SectionPosition) {
84
- if (startPosition.section != this && endPosition.section != this) {
85
- return {
86
- offset: {
87
- start: 0,
88
- end: this.tiles[this.tiles.length - 1].pattern.length
89
- },
90
-
91
- tiles: [...this.tiles]
92
- };
93
- }
94
-
95
- let start = 0;
96
- let end = this.length;
97
-
98
- // only use the position limit if it is within our section
99
- if (startPosition.section == this) {
100
- end = startPosition.absolutePosition;
101
- }
102
-
103
- if (endPosition.section == this) {
104
- start = endPosition.absolutePosition;
105
- }
106
-
107
- // flip if the range was reversed
108
- if (end < start) {
109
- const small = end;
86
+ const span = Span.trail(startPosition, endPosition);
110
87
 
111
- end = start;
112
- start = small;
113
- }
114
-
115
- let passed = 0;
116
88
  const tiles: Tile[] = [];
89
+ let sectionLength = 0;
117
90
 
118
- const tileUnitLength = this.length / this.tileLength;
119
-
120
- const offset = {
121
- start: 0,
122
- end: 0
123
- };
124
-
125
- for (let tile of this.tiles) {
126
- const length = tile.pattern.length * tileUnitLength;
91
+ if (startPosition.section == endPosition.section) {
92
+ sectionLength = startPosition.section.length;
93
+ tiles.push(...startPosition.section.tiles);
94
+ } else {
95
+ sectionLength += startPosition.section.length;
96
+ tiles.push(...startPosition.section.tiles);
127
97
 
128
- if (start - length <= passed && end + length >= passed) {
129
- tiles.push(tile);
98
+ for (let inside of span.inside) {
99
+ sectionLength += inside.length;
100
+ tiles.push(...inside.tiles);
101
+ }
130
102
 
131
- if (start <= passed) {
132
- offset.start = (start + length - passed) * tile.pattern.length / length;
133
- }
103
+ sectionLength += endPosition.section.length;
104
+ tiles.push(...endPosition.section.tiles);
105
+ }
134
106
 
135
- if (end >= passed) {
136
- offset.end = 0.5; // (start + length - passed) * tile.pattern.length / length;
137
- }
138
- }
107
+ let tileLength = 0;
139
108
 
140
- passed += length;
109
+ for (let tile of tiles) {
110
+ tileLength += tile.pattern.length;
141
111
  }
142
112
 
113
+ const start = tileLength / sectionLength * startPosition.absolutePosition;
114
+ const end = tileLength - tileLength / sectionLength * (endPosition.section.length - endPosition.absolutePosition);
115
+
143
116
  return {
144
- offset,
117
+ offset: { start, end },
145
118
  tiles
146
119
  };
147
120
  }