MZ I have some doubts about Window_Command this class

Hello everyone, I know that Window_Command.prototype.drawItemBackground() creates a black rectangular background for each item, I want to draw a new background image for the item when it is selected, and then restore the original background when it is unchecked.
Originally, I wanted to judge the current index in the select() method, and then judge the currently selected item according to the index, and draw a different background, but I don't know how to draw a background image of a particular item in select() where it is, please help me, any advice is important to me.
Hello!
As far as I know, you should rewrite the drawBackgroundRect functon in Window_Selectable, that is the "class" holding the background drawing.

Basically, the background (in 'contentsBack' variable ) is a Bitmap object, and you need to manipulate that Bitmap to hold the image you want to use.

I'm pasting the code I could find about that functions. Hope it helps!

JavaScript:
//Window_Selectable

Window_Selectable.prototype.drawBackgroundRect = function(rect) {
    const c1 = ColorManager.itemBackColor1();
    const c2 = ColorManager.itemBackColor2();
    const x = rect.x;
    const y = rect.y;
    const w = rect.width;
    const h = rect.height;
    this.contentsBack.gradientFillRect(x, y, w, h, c1, c2, true);
    this.contentsBack.strokeRect(x, y, w, h, c1);
};

//Window Base - contentsBack creation

Window_Base.prototype.createContents = function() {
    const width = this.contentsWidth();
    const height = this.contentsHeight();
    this.destroyContents();
    this.contents = new Bitmap(width, height);
    this.contentsBack = new Bitmap(width, height);
    this.resetFontSettings();
};

//Bitmap - loading an image function

/**
 * Loads a image file.
 *
 * @param {string} url - The image url of the texture.
 * @returns {Bitmap} The new bitmap object.
 */
Bitmap.load = function(url) {
    const bitmap = Object.create(Bitmap.prototype);
    bitmap.initialize();
    bitmap._url = url;
    bitmap._startLoading();
    return bitmap;
};
Hello!
As far as I know, you should rewrite the drawBackgroundRect functon in Window_Selectable, that is the "class" holding the background drawing.

Basically, the background (in 'contentsBack' variable ) is a Bitmap object, and you need to manipulate that Bitmap to hold the image you want to use.

I'm pasting the code I could find about that functions. Hope it helps!

JavaScript:
//Window_Selectable

Window_Selectable.prototype.drawBackgroundRect = function(rect) {
    const c1 = ColorManager.itemBackColor1();
    const c2 = ColorManager.itemBackColor2();
    const x = rect.x;
    const y = rect.y;
    const w = rect.width;
    const h = rect.height;
    this.contentsBack.gradientFillRect(x, y, w, h, c1, c2, true);
    this.contentsBack.strokeRect(x, y, w, h, c1);
};

//Window Base - contentsBack creation

Window_Base.prototype.createContents = function() {
    const width = this.contentsWidth();
    const height = this.contentsHeight();
    this.destroyContents();
    this.contents = new Bitmap(width, height);
    this.contentsBack = new Bitmap(width, height);
    this.resetFontSettings();
};

//Bitmap - loading an image function

/**
 * Loads a image file.
 *
 * @param {string} url - The image url of the texture.
 * @returns {Bitmap} The new bitmap object.
 */
Bitmap.load = function(url) {
    const bitmap = Object.create(Bitmap.prototype);
    bitmap.initialize();
    bitmap._url = url;
    bitmap._startLoading();
    return bitmap;
};
Hello, leandiez, thanks for your reply.
Since there is only one contentsBack per window, drawBackgroundRect() draws multiple black rectangular backgrounds on the contentsBack (the same number as the item), I've actually drawn a different background for each different item in drawBackgroundRect(), but I don't know how to change the background image when the item is selected, reverts to its original background when it is deselected, all items share a contentsBack, I don't know where to draw the background of the selected item.
@leandiez Just to clarify your response:

If the OP wants to change the background specifically in the Window_Command, any functions that are being inherited from Window_Base or Window_Selectable which need changes should be overwritten as member methods of Window_Command.

Otherwise, those changes would propagate to all types that are inheriting from those lower level Object types.

@Arrose If you want things to change, that would be a part of Window_Command.refresh()
drawItemBackground(index) draws the background for the specified item index. However, if you want to clear it then draw it again, consider redrawItem(index) instead.

To redraw the previous and current items, you could try patching select, e.g. (untested):
JavaScript:
;void (() => {
  const alias = Window_Command.prototype.select;
  Window_Command.prototype.select = function(index) {
    const i0 = this.index();       // remember previous index
    alias.apply(this, arguments);  // do usual stuff
    const i1 = this.index();       // get new index
    if (i0 !== i1) {               // has index changed?
      this.redrawItem(i0);
      this.redrawItem(i1);
    }
  };
})();
drawItemBackground(index) draws the background for the specified item index. However, if you want to clear it then draw it again, consider redrawItem(index) instead.

To redraw the previous and current items, you could try patching select, e.g. (untested):
JavaScript:
;void (() => {
  const alias = Window_Command.prototype.select;
  Window_Command.prototype.select = function(index) {
    const i0 = this.index();       // remember previous index
    alias.apply(this, arguments);  // do usual stuff
    const i1 = this.index();       // get new index
    if (i0 !== i1) {               // has index changed?
      this.redrawItem(i0);
      this.redrawItem(i1);
    }
  };
})();
Wow, thanks for every suggestion, this one might be useful, give me some time, I need to look into it.

Thank you again, caethyril, I succeeded in switching different background images when item is selected and unchecked.
Great! Happy scripting~

Original thread: https://forums.rpgmakerweb.com/threads/i-have-some-doubts-about-window_command-this-class.165285/