MCPcopy
hub / github.com/jabbany/CommentCoreLibrary / CommentSpaceAllocator

Class CommentSpaceAllocator

src/core/CommentSpaceAllocator.ts:15–170  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

13}
14
15class CommentSpaceAllocator implements ISpaceAllocator {
16 public _width:number;
17 public _height:number;
18 private _pools:Array<Array<IComment>> = [
19 []
20 ];
21 /**
22 * Number of pixels to avoid from last possible y-offset
23 * @type {number}
24 */
25 public avoid:number = 1;
26
27 /**
28 * Constructs a space allocator
29 * @param width - allocator width pixels (default 0)
30 * @param height - allocator height pixels (default 0)
31 */
32 constructor(width:number = 0, height:number = 0) {
33 this._width = width;
34 this._height = height;
35 }
36
37 /**
38 * Logic to determine if checked comment collides with existing comment
39 * We say that comments collide if the existing comment finishes later
40 * than the checked comment is halfway through
41 *
42 * @param existing - Existing comment;
43 * @param check - Checked comment
44 * @returns {boolean} checked collides with exisiting
45 */
46 public willCollide(existing:IComment, check:IComment):boolean {
47 return existing.stime + existing.ttl >= check.stime + check.ttl / 2;
48 }
49
50 /**
51 * Validates sufficient space for a "bullet path" for the comment.
52 *
53 * @param y - Path starting at y offset (path height is the comment height)
54 * @param comment - Comment instance to test
55 * @param pool - The pool to test in.
56 * @returns {boolean} whether or not a valid path exists in the tested pool.
57 */
58 public pathCheck(y:number, comment:IComment, pool:Array<IComment>):boolean {
59 var bottom = y + comment.height;
60 var right = comment.right;
61 for (var i = 0; i < pool.length; i++) {
62 if (pool[i].y > bottom || pool[i].bottom < y) {
63 // This comment is not in the path bounds
64 continue;
65 } else if (pool[i].right < comment.x || pool[i].x > right) {
66
67 if (this.willCollide(pool[i], comment)) {
68 return false;
69 } else {
70 continue;
71 }
72 } else {

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected