MV need help fixing Window_Selectable cursor move

I made the cursor Right and Left working correctly and want to add it to my core.
as there windows require them to work, and the normal doesn't work.

the window on my bestiary plugin (with permission) is made with Window_Command which work
normally UP and Down in a loop.

item scene (base) does not work on end and stays there, so to fix that, I made the fix working
on Left and Right, but the Up and Down needs to be fixed.

so tiny table below:

[0]--[1]--[2]--[3]--[4]
[n]--[n]--[n]--[n]--[n]
[n]--[n]--[n]--[n]--[n]
[x]--[y]--[z]

the idea for UP is :
if index =0, it goes to x,
if index = 1, it goes to y,
if index = 2, it goes to z,
if index = 3, it goes to z (as it is the last index)

the same in reverse, z to 2, y to 1, x to 0.

I got the code below (will update if UP and Down is working from the solution given) so everyone
can use the code in the plugin to make the Window_Selectable working as intended in MV.

PS: it is for cursorUp and cursorDown, Right and Left is working correctly.


JavaScript:
    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) {
        const index = this.index();
        const maxItems = this.maxItems();
        const maxCols = this.maxCols();
        const horizontal = this.isHorizontal();
        if (maxCols >= 2 && (index < maxItems - 1 || (wrap && horizontal))) {
            this.select((index + 1) % maxItems);
        } else {
            this.select(0);
        }
    };
    
    Window_Selectable.prototype.cursorLeft = function(wrap) {
        const index = Math.max(0, this.index());
        const maxItems = this.maxItems();
        const maxCols = this.maxCols();
        const horizontal = this.isHorizontal();
        if (maxCols >= 2 && (index > 0 || (wrap && horizontal))) {
            this.select((index - 1 + maxItems) % maxItems);
        } else {
            this.select(maxItems - 1);
        }
    };

EDIT: thanks to eomereolsson (for first cursorUp working.
EDIT2: thanks to TSR, UP and Down works perfectly.

Code has been updated fully working, copy paste in a plugin if you want it
working in your item, weapon, skills, custom window selectable menu's.

it works in each scene and loop perfectly.
So what do you need help with here?
to get the cursorUp and cursorDown to work in a loop.

the right and left is doing that so far, but I dont know how to do the
same thing for up and down with starting on index === x or index > 0
or however that exactly work.

the base Window_Command that has 1 column loop when you keep
pressing up, but does not work if you have more.

so I hope to fix that when you press up, it goes to the bottom,
reading the x,y,z correctly in the tabel I made.

as I dont know how many enemies/Items I add, it can be on the bottom
for 1-5, so it need to go to the last index or just below it if exist.
to get the cursorUp and cursorDown to work in a loop.

the right and left is doing that so far, but I dont know how to do the
same thing for up and down with starting on index === x or index > 0
or however that exactly work.

the base Window_Command that has 1 column loop when you keep
pressing up, but does not work if you have more.

so I hope to fix that when you press up, it goes to the bottom,
reading the x,y,z correctly in the tabel I made.

as I dont know how many enemies/Items I add, it can be on the bottom
for 1-5, so it need to go to the last index or just below it if exist.
For looping down, check whether this.row() === this.topRow() + 3, and if so select this.index() - 15.
For looping up, check whether this.row() === 0, and if so select this.index() + 15.
but the 3 or 15 can change, so is there something automatically,
same for right/left for maxItem to calculate it automatically?

because +15 probably dont work for the bestiary if it has 100 enemies,
and inventory 9-15.

right and left default is in the middle of the "else if", same if true
for up and down, but the main check and last check if < or >
is something to check if 1 or 2 fails, so down on x goes to 0,
while 0 on up goes to x.

so I dont think +3 and +15 can potentionally work, it need to
work on ANY number of rows/colums.
You should be able to figure something out with modulo but I don't currently have the time to maths it.
I'm struggle with it, so if you got the time or anyone able, please
leave the code behind 🙂

as I really need to make all my windows work correctly.
Dang, that was a surprisingly tough nut to crack, but I think I got it 😅
JavaScript:
Window_Selectable.prototype.cursorDown = function(wrap) {
 //default code
    var index = this.index();
    var maxItems = this.maxItems();
    var maxCols = this.maxCols();
    if (index < maxItems - maxCols || (wrap && maxCols === 1)) {
        this.select((index + maxCols) % maxItems);
    }
 //my addition
 else {
  this.select(index % maxCols);
 }
};

Window_Selectable.prototype.cursorUp = function(wrap) {
 //default code
    var index = this.index();
    var maxItems = this.maxItems();
    var maxCols = this.maxCols();
    if (index >= maxCols || (wrap && maxCols === 1)) {
        this.select((index - maxCols + maxItems) % maxItems);
    }
 //my addition
 else {
  if(index < (maxItems % maxCols)) {
   this.select(maxItems - maxCols + (maxCols - maxItems % maxCols) + index);
  }
  else {
   this.select(maxItems - 1 - (maxItems % maxCols));
  }
 }
};
I tested it, but it is working for 99%, so almost perfect 🙂

because when UP on top left (index 0), it goes to the far bottom right corner
which is not the last index, as there is 1 below it.
so when pressing right goes there, so your code almost got it 🙂

also UP does go to last index bottom right, so not last index if some is empty.
I don't know the javascript, I'll leave that to you, but this is what I would try:

I assume every slot has an ID. And to go to the right you do +1, go to the left you go -1, go up you go -5 and down you go +5, right?

Then if the ID's look like this:

Code:
01 02 03 04 05
06 07 08 09 10
11 12 13 14 15
16 17 18 19 20

Then simple make a condition:

If current is 16 or higher, then down = -15,
Else down = +5

If current is 5 or lower, then up = +15
Else up = -5
@JohnDoeNews that wont work in this case 🙂 but I fixed the cursorUp,
the only thing is cursorDown, to go to the last item on the bottom, else
the one on top.

if it was evented, it would be easier, but this dont work in this path 🙂
So when you are in the bottom row, second column and you want to move down, you land on top row second column?

Then how about this?

When moving to slot ID > (items length-1), then slot ID = 0
When moving to slot ID < 0, then slot ID = (items length-1)

This way it would not stay in the same column, but it will move to the very first or very last slot in the queue.

Edit: Changed items length to items length -1, since you start your IS with 0, not with 1.
it isn't that simple, as I got the same problem with my inventory/equip window,
this issue is also in the bestiary, but instead editing the bestiary, I use the
Window_Selectable to fix all menu's in 1 sweep =)

if you go to the default itemscene, you see it doesn't go back to the top when
you are on the last row.

same applies when you press Right and it stops on the bottom.

this fix the left/right/up/down arrows to go back to the top/bottom if you need
to go there.

and its a bit to complex to fix it nicely. but at least 3 functions are working.
only the cursorDown is missing something to get to the last item if there is more.
before going to the top right above it.
it functions otherwise perfectly, once that works, everyone is free to take the code,
add it as the plugin and all windows with more colums than 2 will go back to the top.

no credits required.

I dont know why it isn't as default like that, maybe it is in MZ, but not in MV.
because when UP on top left (index 0), it goes to the far bottom right corner
which is not the last index, as there is 1 below it.
Dang, that did not happen in my testing. I tested with a setup like this:
Code:
0 1
2 3
4
In hindsight I should probably also have tested the "easy"configuration of a number of items exactly divisible by the number of rows 😅

It was bugged for that configuration, but i added something so it should now also work for that.
JavaScript:
Window_Selectable.prototype.cursorUp = function(wrap) {
    //default code
    var index = this.index();
    var maxItems = this.maxItems();
    var maxCols = this.maxCols();
    if (index >= maxCols || (wrap && maxCols === 1)) {
        this.select((index - maxCols + maxItems) % maxItems);
    }
    //my addition
    else {
        if((maxItems % maxCols) == 0) {
            this.select(maxItems - maxCols + index);
        }
        else if(index < (maxItems % maxCols)) {
            this.select(maxItems - maxCols + (maxCols - maxItems % maxCols) + index);
        }
        else {
            this.select(maxItems - 1 - (maxItems % maxCols));
        }
    }
};

@JohnDoeNews the fun part of this puzzle is that neither the number of items nor the number of columns is fixed. So any hard coded operations won't work.
@eomereolsson I did the fix the "cursorUp" though (as it is added on top
of the code). which was "this.select(maxItems - (maxItems % maxCols));"
so when pressing UP on the 3rd column goes to the last index, so no -1 needed.

your edit might work correctly, but while this UP is fixed, its rather "DOWN"
that dont go to the last index before going back up.

EDIT:
after quick test on your altered code, the UP dont work and still bottom right corner.
but -1 on the last part does go there, else -1 the last index ^^.

so there is a thing missing on the cursorDown somewhere.
after quick test on your altered code, the UP dont work and still bottom right corner.
Huh, that didn't happen in my testing anymore. Could you please tell me with how many items and how many columns you are testing?

its rather "DOWN"
that dont go to the last index before going back up.
Wait, you want that to happen? Then of course my code is "broken", because that is not what I originally understood that you wanted. Let's look at the following basic configuration:
Code:
0 1 2
3 4 5
6 7 8

I understood you wanted the following results:
Code:
0 > up > 6
1 > up > 7
2 > up > 8

6 > down > 0
7 > down > 1
8 > down > 2

Now I think you want the following (could you please confirm this understanding is correct before I take another stab at it 😅)
Code:
0 > up > 8
1 > up > 0
2 > up > 1

6 > down > 7
7 > down > 8
8 > down > 0
I'm testing it with 60objects

result is the same, but on cursorUp there is something weird either.
I explain later, give me a moment for illustration on "down".

EDIT:
cursor UP is weird, and might a minor modification.
JavaScript:
//this.select(maxItems - (maxItems % maxCols));  // when 1 object on bottom row
this.select(maxItems +1 - (maxItems % maxCols)); // when 2+ objects on bottom row

when there is 1 object, you need the top one (which is ruled out), if you have 2+
it needs the bottom one in order to work.

the Cursor Down goes to the bottom left corner, instead of the last item.
if on top of the 3 column, it should go to the 2nd colum, on the bottom,
if on 4th, to the 2nd colum, and when "down again", to the 2nd colum on top.
how it currently works is illustrated in the image below.
(red text, green arrows how it should selection on the 3rd, 4th, 5th, should
go to the last index, before jumping to the top.
sample.png

Edit 2:
I understood you wanted the following results:
Code:
0 > up > 6
1 > up > 7
2 > up > 8

6 > down > 0
7 > down > 1
8 > down > 2
this is correct and that is also working, just the one illustration, so when it is the
last item, it goes to top 2nd column (in this case), depending on the last item
in the bottom row.
Once again my example proves to be insufficent. Communication - the bane of all software development.
Let's now look at the following example:
Code:
0 1 2
3 4 5
6 7

My old understanding of what you wanted:
Code:
0 > up > 6
1 > up > 7
2 > up > 5

6 > down > 0
7 > down > 1
5 > down > 2

EDIT:
Had some connection issues and apparently my last paragraph got lost. So here it is again:

My new understanding of what you want:

Code:
0 > up > 6
1 > up > 7
2 > up > 7

6 > down > 0
7 > down > 1
5 > down > 7
simple of the way:
0 > up > 6
1 > up > 7
2 > up > 7 (fixed) but if 2 below, with +1, else with 1 on bottom, nothing)
see code with // as it should be a way to reconize it somehow for both.

6 > down > 0 (working)
7 > down > 2 (working)
5 > down > 7 (not working) as it should select the last item.
which goed from 7 > down > 2
Ok, since I think I got what you want this time for reals, I took another stab at it.
JavaScript:
Window_Selectable.prototype.cursorDown = function(wrap) {
    //default code
    var index = this.index();
    var maxItems = this.maxItems();
    var maxCols = this.maxCols();
    if (index < maxItems - maxCols || (wrap && maxCols === 1)) {
        this.select((index + maxCols) % maxItems);
    }
    //my addition
    else {
        if(this._nextCursorDownIndex && this._nextCursorDownIndex >= 0) {
            this.select(this._nextCursorDownIndex);
            this._nextCursorDownIndex = -1;
        }
        else if(index < maxItems - (maxItems % maxCols)) {
            this._nextCursorDownIndex = index % maxCols;
            this.select(maxItems - 1);
        }
        else {
            this.select(index % maxCols);
        }
    }
};

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