MZ "Is Selected" Check

Hi folks,

I am modifying a bit of code from an existing plugin (with permission) to suit my game, and the objective is to have item names in the inventory only display when the "cursor" is over them (ie. when you are selecting it in the inventory, with the displayed name changing as you move between items).

This is the code to display all of the item names in the inventory right now:

Code:
this.drawText(item.name, x + textMargin, y, itemWidth);

and my hope is to add a condition for displaying the name, which checks the item against the current selected item before displaying, like this:

Code:
if(this item is selected){
this.drawText(item.name, x + textMargin, y, itemWidth);
}

The idea is that I only want to see one item name rendered at a time, the one I am hovering over. I am hoping there is a function that gets the currently selected item so I can compare it to the item, and only display the name if they match or something like that.

Is there something that checks for this?

Thanks!

NOTE: The plugin specifically is DM_LimitedInventory, and this window I am modifying is defined on line 2870. It is free if you are able to take a look at it: HERE
I don't know what class of objects you're modifying but the check you're asking for does not exist. Instead if you're editing Window_ItemList, it does not store what item is selected, but it instead stores what index is selected.

My approach for this would look like this, if I wrote a fresh plugin. I do not know how your plugin works so you're going to need to creative apply my suggestion to your context

Code:
//Tell the inventory screen to redraw its contents every time a new item is selected
_aliasItemListSelect = Window_ItemList.prototype.select;
Window_ItemList.prototype.select = function(index) {
    _aliasItemListSelect.call(this);
    this.refresh();
}

//Override
Window_ItemList.prototype.drawItem = function(index) {
    var item = this._data[index];
    if (item) {
        var numberWidth = this.numberWidth();
        var rect = this.itemRect(index);
        rect.width -= this.textPadding();
        this.changePaintOpacity(this.isEnabled(item));
        if (this._index === index) {
            this.drawItemName(item, rect.x, rect.y, rect.width - numberWidth);
        }
        this.drawItemNumber(item, rect.x, rect.y, rect.width);
        this.changePaintOpacity(1);
    }
};
I don't know what class of objects you're modifying but the check you're asking for does not exist. Instead if you're editing Window_ItemList, it does not store what item is selected, but it instead stores what index is selected.

My approach for this would look like this, if I wrote a fresh plugin. I do not know how your plugin works so you're going to need to creative apply my suggestion to your context

Code:
//Tell the inventory screen to redraw its contents every time a new item is selected
_aliasItemListSelect = Window_ItemList.prototype.select;
Window_Selectable.prototype.select = function(index) {
_aliasItemListSelect.call(this);
this.refresh();
}

//Override
Window_ItemList.prototype.drawItem = function(index) {
var item = this._data[Index];
if (item) {
var numberWidth = this.numberWidth();
var rect = this.itemRect(index);
rect.width -= this.textPadding();
this.changePaintOpacity(this.isEnabled(item));
if (this._index === index) {
this.drawItemName(item, rect.x, rect.y, rect.width - numberWidth);
        }
this.drawItemNumber(item, rect.x, rect.y, rect.width);
this.changePaintOpacity(1);
    }
};

This doesn't seem to work in my case, as it is not Window_ItemList that I am modifying, but a custom window from the plugin called Window_ItemContainer.

The plugin specifically is DM_LimitedInventory, and this window is defined on line 2870. it is free if you are able to take a look at it: HERE
What I don't understand is if you don't see a list of items, how will the player know there are items to move the cursor between?

And how will they know when they're at the bottom or top of the list?

And you don't think it would look profoundly strange to be navigating a list of items with a cursor that's just sort of floating there?

You could tweak the enabled method since that's already changing the text color. If you make line 2782 read:
Code:
return this._data[this.index()]==item;

Then add the method:
Code:
Window_ItemContainer.prototype.translucentOpacity = function() {
    return 160;
};

I think - without testing anything - that should make everything that you're not highlighting invisible.
What I don't understand is if you don't see a list of items, how will the player know there are items to move the cursor between?

And how will they know when they're at the bottom or top of the list?

And you don't think it would look profoundly strange to be navigating a list of items with a cursor that's just sort of floating there?

You could tweak the enabled method since that's already changing the text color. If you make line 2782 read:
Code:
return this._data[this.index()]==item;

Then add the method:
Code:
Window_ItemContainer.prototype.translucentOpacity = function() {
    return 160;
};

I think - without testing anything - that should make everything that you're not highlighting invisible.

This just seems to have made the item names slightly transparent, rather than invisible, and doesn't change when selected at all.
here's my solution:

Code:
_aliasItemContainerSelect = Window_ItemContainer.prototype.select;
Window_ItemContainer.prototype.select = function(index) {
	_aliasItemContainerSelect.call(this, index);
	this.refresh();
}

Window_ItemContainer.prototype.drawItemName = function(item, x, y, width, showName) {
    if (item) {
        const iconY = y + (this.lineHeight() - ImageManager.iconHeight) / 2;
        const textMargin = ImageManager.iconWidth + 4;
        const itemWidth = Math.max(0, width - textMargin);
        this.resetTextColor();
        this.drawIcon(item.iconIndex, x, iconY);
        if (showName) {
			this.drawText(item.name, x + textMargin, y, itemWidth);
		}
    }
};

Window_ItemContainer.prototype.drawItem = function(index) {
    const item = this.itemAt(index);
    if (item) {
        const numberWidth = this.numberWidth();
        const rect = this.itemLineRect(index);
        this.changePaintOpacity(this.isEnabled(item));
		this.drawItemName(item, rect.x, rect.y, rect.width - numberWidth, this.index() === index);
        this.drawItemNumber(item, rect.x, rect.y, rect.width);
        this.changePaintOpacity(1);
    }
};

I made an error on the aliasing with the select() function, by forgetting to include the first argument. It's fixed in my new attempt above

However since the information is more difficult to access within the drawItemName function I added the check as a fourth argument
here's my solution:

Code:
_aliasItemContainerSelect = Window_ItemContainer.prototype.select;
Window_ItemContainer.prototype.select = function(index) {
    _aliasItemContainerSelect.call(this, index);
    this.refresh();
}

Window_ItemContainer.prototype.drawItemName = function(item, x, y, width, showName) {
    if (item) {
        const iconY = y + (this.lineHeight() - ImageManager.iconHeight) / 2;
        const textMargin = ImageManager.iconWidth + 4;
        const itemWidth = Math.max(0, width - textMargin);
        this.resetTextColor();
        this.drawIcon(item.iconIndex, x, iconY);
        if (showName) {
            this.drawText(item.name, x + textMargin, y, itemWidth);
        }
    }
};

Window_ItemContainer.prototype.drawItem = function(index) {
    const item = this.itemAt(index);
    if (item) {
        const numberWidth = this.numberWidth();
        const rect = this.itemLineRect(index);
        this.changePaintOpacity(this.isEnabled(item));
        this.drawItemName(item, rect.x, rect.y, rect.width - numberWidth, this.index() === index);
        this.drawItemNumber(item, rect.x, rect.y, rect.width);
        this.changePaintOpacity(1);
    }
};

I made an error on the aliasing with the select() function, by forgetting to include the first argument. It's fixed in my new attempt above

However since the information is more difficult to access within the drawItemName function I added the check as a fourth argument

This is working! Thank you!

Two questions however:
1) It is rendering the name behind the item icons. Is there a way to render it above them (as the item quantities are)? Such as rendering at a higher "Z".
2) If I wanted to add an offset (say render the names 20y higher), where would I add a "+20"? I tried adding it to item.rect.y but that doesn't seem to have worked as I thought it would.
This is working! Thank you!

Two questions however:
1) It is rendering the name behind the item icons. Is there a way to render it above them (as the item quantities are)? Such as rendering at a higher "Z".
2) If I wanted to add an offset (say render the names 20y higher), where would I add a "+20"? I tried adding it to item.rect.y but that doesn't seem to have worked as I thought it would.
The way you Z-sort when drawing two sprites that are part of the same bitmap is to change the order the functions are called. The item name is drawn after the icon, so it would appear on top of the icon.

item does not have a rect objectbut it instead belongs to the Window_ItemContainer instance, you may instead want to try rect.y + 20 or something
The way you Z-sort when drawing two sprites that are part of the same bitmap is to change the order the functions are called. The item name is drawn after the icon, so it would appear on top of the icon.

item does not have a rect objectbut it instead belongs to the Window_ItemContainer instance, you may instead want to try rect.y + 20 or something

Adding it to rect.y does sort-of work, but it also moves the icon down with it.
Actually, I figured it out. I just added the opposite offset to the icon Y and that fixed it!. Now I just have the Z issue where the text renders behind the icons.

If I am understanding you right, the names should be rendering above the icons, but they are not for some reason.

This is what it looks like for me with rect.y+40:
(now just the issue of the Z)

eg.JPG
Adding it to rect.y does sort-of work, but it also moves the icon down with it.

If I am understanding you right, the names should be rendering above the icons, but they are not for some reason.

This is what it looks like for me with rect.y+40:

View attachment 289089
The fix is going to be complicated because that means we have to make drawing the item name the very final step after every icon is drawn.

The new solution would look like this:

Code:
_aliasItemContainerSelect = Window_ItemContainer.prototype.select;
Window_ItemContainer.prototype.select = function(index) {
	_aliasItemContainerSelect.call(this, index);
	this.refresh();
}

Window_ItemContainer.prototype.drawItemName = function(item, x, y, width) {
    if (item) {
        const iconY = y + (this.lineHeight() - ImageManager.iconHeight) / 2;
        const textMargin = ImageManager.iconWidth + 4;
        const itemWidth = Math.max(0, width - textMargin);
        this.resetTextColor();
        this.drawIcon(item.iconIndex, x, iconY);
	//this.drawText(item.name, x + textMargin, y, itemWidth);
    }
};

Window_ItemContainer.prototype.drawAllItems = function() {
    const topIndex = this.topIndex();
    for (let i = 0; i < this.maxVisibleItems(); i++) {
        const index = topIndex + i;
        if (index < this.maxItems()) {
            this.drawItemBackground(index);
            this.drawItem(index);
        }
    }
	if (topIndex <= this.index() && this.index() < topIndex + this.maxVisibleItems()) {
		const item = this.itemAt(this.index())
		if (item) {
			const rect = this.itemLineRect(this.index());
			const textMargin = ImageManager.iconWidth + 4;
			const itemWidth = Math.max(0, rect.width - textMargin);
			this.drawText(item.name, rect.x + 0, rect.y + 0, itemWidth);
		}
	}
};
You position the item name by changing the 0's under the bottom block in drawAllItems()
The fix is going to be complicated because that means we have to make drawing the item name the very final step after every icon is drawn.

The new solution would look like this:

Code:
_aliasItemContainerSelect = Window_ItemContainer.prototype.select;
Window_ItemContainer.prototype.select = function(index) {
_aliasItemContainerSelect.call(this, index);
this.refresh();
}

Window_ItemContainer.prototype.drawItemName = function(item, x, y, width) {
if (item) {
const iconY = y + (this.lineHeight() - ImageManager.iconHeight) / 2;
const textMargin = ImageManager.iconWidth + 4;
const itemWidth = Math.max(0, width - textMargin);
this.resetTextColor();
this.drawIcon(item.iconIndex, x, iconY);
//this.drawText(item.name, x + textMargin, y, itemWidth);
    }
};

Window_ItemContainer.prototype.drawAllItems = function() {
const topIndex = this.topIndex();
for (let i = 0; i < this.maxVisibleItems(); i++) {
const index = topIndex + i;
if (index < this.maxItems()) {
this.drawItemBackground(index);
this.drawItem(index);
        }
    }
if (topIndex <= this.index() && this.index() < topIndex + this.maxVisibleItems()) {
const item = this.itemAt(this.index())
if (item) {
const rect = this.itemLineRect(this.index());
const textMargin = ImageManager.iconWidth + 4;
const itemWidth = Math.max(0, rect.width - textMargin);
this.drawText(item.name, rect.x + 0, rect.y + 0, itemWidth);
        }
    }
};
You position the item name by changing the 0's under the bottom block in drawAllItems()

Hmm, this seems to have squashed the text:

eg.JPG
Hmm, this seems to have squashed the text:

View attachment 289091
Feel free to change the itemWidth value on that bottom block to something more suited to your needs. By default text is limited to the available size for each itemrect which you've dramatically shrunken the size of those rects

Code:
Window_ItemContainer.prototype.select = function(index) {
_aliasItemContainerSelect.call(this, index);
this.refresh();
}

Window_ItemContainer.prototype.drawItemName = function(item, x, y, width) {
if (item) {
const iconY = y + (this.lineHeight() - ImageManager.iconHeight) / 2;
const textMargin = ImageManager.iconWidth + 4;
const itemWidth = Math.max(0, width - textMargin);
this.resetTextColor();
this.drawIcon(item.iconIndex, x, iconY);
//this.drawText(item.name, x + textMargin, y, itemWidth);
    }
};

Window_ItemContainer.prototype.drawAllItems = function() {
    const topIndex = this.topIndex();
    for (let i = 0; i < this.maxVisibleItems(); i++) {
        const index = topIndex + i;
        if (index < this.maxItems()) {
            this.drawItemBackground(index);
            this.drawItem(index);
        }
    }
	if (topIndex <= this.index() && this.index() < topIndex + this.maxVisibleItems()) {
		const item = this.itemAt(this.index())
		if (item) {
			const rect = this.itemLineRect(this.index());
			const textMargin = ImageManager.iconWidth + 4;
			const itemWidth = 240; //Enter in some arbitrary text width
			const xOffset = 40;
			const yOffset = 0;
			const x = Math.min(this.contents.width - itemWidth, rect.x + xOffset);
			this.drawText(item.name, x, rect.y + yOffset, itemWidth, 'center');
		}
	}
};

We'd probably go on and on as we'd also have to write code to prevent that text from going outside of the bitmap object....

EDIT: Added an update that prevents that text from being drawn too far to the right
Feel free to change the itemWidth value on that bottom block to something more suited to your needs. By default text is limited to the available size for each itemrect which you've dramatically shrunken the size of those rects

Code:
Window_ItemContainer.prototype.select = function(index) {
_aliasItemContainerSelect.call(this, index);
this.refresh();
}

Window_ItemContainer.prototype.drawItemName = function(item, x, y, width) {
if (item) {
const iconY = y + (this.lineHeight() - ImageManager.iconHeight) / 2;
const textMargin = ImageManager.iconWidth + 4;
const itemWidth = Math.max(0, width - textMargin);
this.resetTextColor();
this.drawIcon(item.iconIndex, x, iconY);
//this.drawText(item.name, x + textMargin, y, itemWidth);
    }
};

Window_ItemContainer.prototype.drawAllItems = function() {
const topIndex = this.topIndex();
for (let i = 0; i < this.maxVisibleItems(); i++) {
const index = topIndex + i;
if (index < this.maxItems()) {
this.drawItemBackground(index);
this.drawItem(index);
        }
    }
if (topIndex <= this.index() && this.index() < topIndex + this.maxVisibleItems()) {
const item = this.itemAt(this.index())
if (item) {
const rect = this.itemLineRect(this.index());
const textMargin = ImageManager.iconWidth + 4;
const itemWidth = 240; //Hardcoded
this.drawText(item.name, rect.x + 0, rect.y + 0, itemWidth);
        }
    }
};

We'd probably go on and on as we'd also have to write code to prevent that text from going outside of the bitmap object....

Hmm, this is giving me "_aliasItemContainerSelect is not defined"

As to your other point, I considered asking but figured that would be in "Magical Christmas Land" territory and I'd just have to deal with some names being cut off by the neighboring window, haha. I may try to get around it by rendering the item name in a fixed position or something if I have no other choice (or using fewer columns).
Hmm, this is giving me "_aliasItemContainerSelect is not defined"

As to your other point, I considered asking but figured that would be in "Magical Christmas Land" territory and I'd just have to deal with some names being cut off by the neighboring window, haha. I may try to get around it by rendering the item name in a fixed position or something if I have no other choice (or using fewer columns).

I must have missed a spot from copying and pasting. I also posted an updated code that prevents text from being drawn too far to the right

Code:
_aliasItemContainerSelect = Window_ItemContainer.prototype.select;
Window_ItemContainer.prototype.select = function(index) {
	_aliasItemContainerSelect.call(this, index);
	this.refresh();
}
I was missing the top line
I must have missed a spot from copying and pasting. I also posted an updated code that prevents text from being drawn too far to the right

Code:
_aliasItemContainerSelect = Window_ItemContainer.prototype.select;
Window_ItemContainer.prototype.select = function(index) {
    _aliasItemContainerSelect.call(this, index);
    this.refresh();
}
I was missing the top line

This is working perfectly, thank you!
This just seems to have made the item names slightly transparent, rather than invisible
Because I pasted a function without actually modifying it 😛

It should be opacity = 0, not 160. But if you've got a solution, awesome!

Original thread: https://forums.rpgmakerweb.com/threads/is-selected-check.164045/