MV need help fixing Window_Selectable cursor move

@eomereolsson
you almost crack it down, the red one (currently selected) jumps to the top,
while the green arrows works intended now.

when I change the 2 functions, with nextCursorDown (if it exist or not),
the the green arrows goes to the top, and the red one (selected) goes
to the bottom.

as it is now, only the center one doesn't work which is strange. almost.png
Huh, I wanted to do the math to try and figure out why my formula fails for that index. Then I realized: you said you are testing with 60 objects and your screenshots indicate 5 columns. But shouldn't you have 12 rows of 5 entries with no leftover? How do you only have 2 entries in your last row?
I think @ShadowDragon tested its code with 60 items, but he want the code to accomodate any numbers of items, rows and columns. The code would be simple if the the last row of windows were always full, but the trick is to make it behave as he want even when the last row has empty entries.

This is my soluce:

Code:
Window_Selectable.prototype.updateCursorPos = function() {
    const rect = this.itemRect(this._index);
    this.setCursorRect(rect.x, rect.y, rect.width, rect.height);
    this.ensureCursorVisible();
};

Window_Selectable.prototype.cursorDown = function(wrap) {
    const index = this.index();
    const maxItems = this.maxItems();
    const maxCols = this.maxCols();
    if (index < maxItems - maxCols || (wrap && maxCols === 1)) {
        this.select((index + maxCols) % maxItems);
    } else {
        this._index -= (this.maxRows() * maxCols) - maxCols;
        if (this._index < 0) {
            this._index = maxItems - 1;
        }
        this.updateCursorPos();
    }
};

Window_Selectable.prototype.cursorUp = function(wrap) {
    const index = Math.max(0, this.index());
    const maxItems = this.maxItems();
    const maxCols = this.maxCols();
    if (index >= maxCols || (wrap && maxCols === 1)) {
        this.select((index - maxCols + maxItems) % maxItems);
    } else {
        this._index += (this.maxRows() * maxCols) - maxCols;
        if (this._index >= maxItems) {
            this._index = maxItems - 1;
        }
        this.updateCursorPos();
    }
};

Window_Selectable.prototype.cursorRight = function(wrap) {
    if (this._index % this.maxCols() < (this.maxCols() - 1)) {
        this._index++;
        if (this._index >= this.maxItems()) {
            this._index = this.row() * this.maxCols();
        }
        this.updateCursorPos();
    } else if (wrap) {
        const firstIndex = this.row() * this.maxCols();
        if (this.row() < this.maxPageRows()) {
            this._index -= (this.maxCols() - 1);
        } else {
            this._index -= this._index - firstIndex;
        }
        this.updateCursorPos();
    }
};

Window_Selectable.prototype.cursorLeft = function(wrap) {
    if (this._index % this.maxCols() > 0) {
        this._index--;
        this.updateCursor();
    } else if (wrap) {
        this._index += (this.maxCols() - 1);
        if (this._index >= this.maxItems()) {
            this._index = this.maxItems() - 1;
        }
        this.updateCursorPos();
    }
};
Code was built and tested in MZ, then made slight changes to convert it to MV.
I only ran a few tests in MV to get sure it is working. It does the job, but should be tested thoroughly.

Regards
solving code in first post.

This thread is being closed, due to being solved. If for some reason you would like this thread re-opened, please report this post and leave a message why. Thank you.

Original thread: https://forums.rpgmakerweb.com/threads/need-help-fixing-window_selectable-cursor-move.151912/