Skip to content

Commit

Permalink
Merge pull request #94 from xpadev-net/refactor/locator
Browse files Browse the repository at this point in the history
[修正] 位置計算ロジックを改善
  • Loading branch information
xpadev-net authored Jan 12, 2024
2 parents ea5131b + 050a555 commit 9fea759
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 7 deletions.
2 changes: 1 addition & 1 deletion src/comments/BaseComment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ class BaseComment implements IComment {
*/
constructor(comment: FormattedComment, renderer: IRenderer, index: number) {
this.renderer = renderer;
this.posY = 0;
this.posY = -1;
this.pos = { x: 0, y: 0 };
comment.content = comment.content.replace(/\t/g, "\u2003\u2003");
this.comment = this.convertComment(comment);
Expand Down
2 changes: 1 addition & 1 deletion src/comments/HTML5Comment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class HTML5Comment extends BaseComment {
override readonly pluginName: string = "HTML5Comment";
constructor(comment: FormattedComment, context: IRenderer, index: number) {
super(comment, context, index);
this.posY = 0;
this.posY = -1;
}

override get content() {
Expand Down
2 changes: 1 addition & 1 deletion src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ class NiconiComments {
const getCommentPosStart = performance.now();
if (this.processedCommentIndex + 1 >= end) return;
for (const comment of data.slice(this.processedCommentIndex, end)) {
if (comment.invisible) continue;
if (comment.invisible || (comment.posY > -1 && !lazy)) continue;
if (comment.loc === "naka") {
processMovableComment(comment, this.collision, this.timeline, lazy);
} else {
Expand Down
17 changes: 13 additions & 4 deletions src/utils/comment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -622,6 +622,7 @@ const processMovableComment = (
for (let j = beforeVpos, n = comment.long + 125; j < n; j++) {
const vpos = comment.vpos + j;
const leftPos = getPosX(comment.comment, vpos);
if (timeline[vpos]?.includes(comment)) break;
arrayPush(timeline, vpos, comment);
if (
leftPos + comment.width + config.collisionPadding >=
Expand Down Expand Up @@ -668,19 +669,24 @@ const getMovablePosY = (
}
let posY = 0;
let isChanged = true;
let lastUpdatedIndex: number | undefined = undefined;
while (isChanged) {
isChanged = false;
for (let j = beforeVpos, n = comment.long + 125; j < n; j++) {
for (let j = beforeVpos, n = comment.long + 125; j < n; j += 5) {
const vpos = comment.vpos + j;
const leftPos = getPosX(comment.comment, vpos);
let isBreak = false;
if (lastUpdatedIndex !== undefined && lastUpdatedIndex === vpos) {
return posY;
}
if (
leftPos + comment.width >= config.collisionRange.right &&
leftPos <= config.collisionRange.right
) {
const result = getPosY(posY, comment, collision.right[vpos]);
posY = result.currentPos;
isChanged ||= result.isChanged;
if (result.isChanged) lastUpdatedIndex = vpos;
isBreak = result.isBreak;
}
if (
Expand All @@ -690,6 +696,7 @@ const getMovablePosY = (
const result = getPosY(posY, comment, collision.left[vpos]);
posY = result.currentPos;
isChanged ||= result.isChanged;
if (result.isChanged) lastUpdatedIndex = vpos;
isBreak = result.isBreak;
}
if (isBreak) return posY;
Expand All @@ -715,11 +722,13 @@ const getPosY = (
let isBreak = false;
if (!collision) return { currentPos, isChanged, isBreak };
for (const collisionItem of collision) {
if (collisionItem.index === targetComment.index || collisionItem.posY < 0)
continue;
if (
currentPos < collisionItem.posY + collisionItem.height &&
currentPos + targetComment.height > collisionItem.posY &&
collisionItem.owner === targetComment.owner &&
collisionItem.layer === targetComment.layer
collisionItem.layer === targetComment.layer &&
currentPos < collisionItem.posY + collisionItem.height &&
currentPos + targetComment.height > collisionItem.posY
) {
if (collisionItem.posY + collisionItem.height > currentPos) {
currentPos = collisionItem.posY + collisionItem.height;
Expand Down

0 comments on commit 9fea759

Please sign in to comment.