MV Multiline Selectable Window, Scrolling

This is one I'm hoping I can get some help on as I'm feeling less confident about sussing this one out.
The project in question relies on maintaining a log of events the player can view. I've built a hybrid Selectable/Command Window (Base Selectable, "stole" and retooled the relevant Command functions), and it works perfectly.
But, given what its built off, the default drawText() packs all of the text into one line. drawTextEx() handles the straight processing and gives me color codes, that's an easy update. A little slice & dice and we split the lines at \n to draw the multiple lines, that works. itemRect has been updated to encompass the new heights and placements. So far so good.
Now I've got to rejigger what looks like a lot of extra calculations for the scrolling and potentially other elements related to the size of the list.
Has anyone had experience doing this? Happy to take some pointers, also happy to kick someone a few bucks for a clean solution.
Focus on writing the drawItem method for your window: it is responsible for drawing a specific item in the list. If you haven't already, you should also define maxItems to match the item count. Window_Selectable should handle all the rest for you. 👋

As an example, here's the drawItem method of Window_Command:
JavaScript:
Window_Command.prototype.drawItem = function(index) {
    var rect = this.itemRectForText(index);
    var align = this.itemTextAlign();
    this.resetTextColor();
    this.changePaintOpacity(this.isCommandEnabled(index));
    this.drawText(this.commandName(index), rect.x, rect.y, rect.width, align);
};
You see that it gets the draw location, then draws the text at that location.

You can find other examples by searching drawItem = function in rpg_windows.js~

For more specific suggestions/help, consider sharing your code.
These are the relevant functions; I was up on these points pretty easily. "maxItems" has indeed given me some consternation because there's the ITEMS and then there' the LINES, and trying to get things to correlate seem to be the chore.

JavaScript:
Window_MysteryLog.prototype.setLog = function (log) {
    this.clearLog();
    this._list = log;
    this._lineCounts = log.map( function(item){ return (item.match(/\n/g) || []).length + 1; });
}

Window_MysteryLog.prototype.prevLines = function(index) { var sum=0; for(i=0;i<index;i++){ sum += this._lineCounts[i]; } return sum; }

Window_MysteryLog.prototype.itemRect = function(index) {
    var rect = new Rectangle();
    var maxCols = this.maxCols();
    rect.width = this.itemWidth();
    rect.height = this.itemHeight() * this._lineCounts[index];
    rect.x = index % maxCols * (rect.width + this.spacing()) - this._scrollX;

    var lines = Math.max(0,this.prevLines(index));
    rect.y = (Math.floor(lines / maxCols) * this.itemHeight()) - this._scrollY;

    return rect;
};

Window_MysteryLog.prototype.drawItem = function(index) {
    const rect = this.itemRectForText(index);
    const text = this._list[index];

    const lines = text.split("\n"); console.log(lines);

    let y = rect.y;

    for (let i = 0; i < lines.length; i++) {
        this.drawTextEx(lines[i], rect.x, y);
        y += this.lineHeight();
    }
};

mansion_textbox.png

The above is the result; there's an entry above the ones here, and there's a couple below the two-line "warlock" line there. Lines get pushed below the view port right now, on account I'm betting of items versus lines, but I can't seem to locate the places to influence that correctly. I've tried some different attempts at the scrolling or trying to split up the need for items versus lines, but its resulted in more garbage. And I wager its going to be something kinda dumb and obvious, but I'm a little crosseyed at this point and hoping to tag in another brain or two.
(I haven't tested any of the code in this post, but hopefully it'll work or at least be a helpful nudge in the right direction.)

An item is a selectable "block" in the list, so max items is just _list.length. The main problem with item height is that your items are not necessarily all the same height. You're correct in thinking that you'll have to modify the itemRect (for the Y position), but for scrolling purposes I think you might also need to override/patch every inherited method that references itemHeight:
  • topRow - e.g. sum _lineCounts until >= _scrollY to determine row.
  • setTopRow - e.g. sum first N _lineCounts to determine scrollY.
  • maxPageRows - maybe sum _lineCounts * lineHeight() from topRow until exceeding .contents.height?
  • updateCursor - for _cursorAll support, which you might not care about.
Obviously test it as-is first, to see if any extra code is actually needed.

A basic foundation, based on your code:
Spoiler: plugin code
JavaScript:
Window_MysteryLog.prototype.setLog = function (log) {
    this.clearLog();
    this._list = log;
    this._lineCounts = log.map( function(item){ return (item.match(/\n/g) || []).length + 1; });
};

Window_MysteryLog.prototype.drawItem = function(index) {
    const rect = this.itemRectForText(index);
    const text = this._list[index];
    this.drawTextEx(text[i], rect.x, rect.y);
};

Window_MysteryLog.prototype.prevLines = function(index) {
    let sum = 0; for (i = 0; i < index; i++) { sum += this._lineCounts[i]; } return sum;
};

Window_MysteryLog.prototype.itemRect = function(index) {
    const rect = new Rectangle();
    const maxCols = this.maxCols();
    rect.width = this.itemWidth();
    rect.height = this.lineHeight() * this._lineCounts[index];

    rect.x = index % maxCols * (rect.width + this.spacing()) - this._scrollX;
    const lines = Math.max(0, this.prevLines(index));
    rect.y = (Math.floor(lines / maxCols) * this.lineHeight()) - this._scrollY;

    return rect;
};

Window_MysteryLog.prototype.maxItems = function() {
    this._list.length;  // selectable blocks in list
};

Window_MysteryLog.prototype.open = function() {
    Window_Selectable.prototype.open.apply(this, arguments);
    this.select(0);    // select first item in list
    this.refresh();    // redraw contents
    this.activate();   // respond to input once open
};

Window_MysteryLog.prototype.close = function() {
    Window_Selectable.prototype.close.apply(this, arguments);
    this.deactivate();   // stop responding to input
};
drawTextEx can handle new lines ("\n") with no trouble, e.g. see Window_Message or Window_Help, so you shouldn't need to separate those draw calls.

Your custom itemRect looks OK. I think it's just vertically offset because Window_Selectable initialises its _index to -1? I'm guessing that adding .select(0) as I suggested here should fix that. It doesn't have to be in open/close, I just figured that was likely to be suitable. 👋

Just to mention it: I do think it would be much simpler to implement this as 1 line = 1 item! If need be then you could:
Spoiler: 1 line 1 item suggestions
  1. Redirect cursorUp/cursorDown to the corresponding cursorPage methods, to scroll a "page" of items in one go, e.g.
    JavaScript:
    Window_MysteryLog.prototype.cursorDown = function() {
        this.cursorPageDown();
    };
    
    Window_MysteryLog.prototype.cursorUp = function() {
        this.cursorPageUp();
    };
  2. Make each itemRect fill the window, e.g.
    JavaScript:
    Window_MysteryLog.prototype.itemHeight = function() {
        return this.contents.height;
    };
Then there's no need to mess around with custom rect sizes and any related awkwardness~
I'll review and tinker; thanks for taking the time out to write something up!
Regarding your note/suggestion, to make sure I'm understanding correctly...1 item, one page? THAT could answer everything and still make a decent log...I might rejigger a few things but I kinda like that...

EDIT: Hey, whatdyaknow? 🙂 Enforcing a 1-item-per-page and using the standard Y setting in itemRect, it works just fine putting 1 item up! Scrolls through nicely, things seem to be good 😀
I HAD been contemplating using the $gameMessage for some stuff more in this, and I think I'll do just that as remarks and things are introduced and simultaneously copy new details to the log.
Just chiming in to agree with @caethyril that one line per log entry is going to be way less headache. If you can design your log entries around that restriction you can more or less use Window_Selectable with very minor alterations.

Alternatively, and I may not even be understanding what you want to do correctly, but... if you want a ongoing journal-style (e.g. paragraphs) presentation that grows in length as the player unlocks clues, there's an old trick you can use. You would create a custom text sprite that is the width of your window and tall enough to accommodate your text, and you would program the sprite to handle your text wrapping and any other formatting. Then your scrolling inputs would move that sprite up and down within the window's visible content area. Totally doable, but a bit more work than just adapting your text to work with 1 log entry per line.

example.png
For the moment, I think I'm happy with the new/current implementation; I kept the rect updates to the size, but restored placement and hard-coded a page limit of 1 for this particular window. As a result, each page displays one item without issue and I don't have to monkey with some of the other elements. Fairly quick and clean 🙂
I've got a few other ideas now for dealing with the interactions as well. The original intent was to use the log window for the primary narration of the scene, but I think I'll make a few adjustments to the process and use the log as a reminder of past actions over the primary presentation of new information
newbox(1).png
newbox(2).png

Original thread: https://forums.rpgmakerweb.com/threads/multiline-selectable-window-scrolling.178987/