MZ Adding Images to Commands

Hello,

I'm new to RPG Maker and I'm trying to learn how to make plugins for it. For my first project, I'm trying to change how the battle menu works and looks. I'm trying to figure out how to add an image to a command so that it's not just displaying text. I have a Window_HorizCommand set up to display on the battle screen and was able to use addCommand to input the commands there but nothing I do seems to work to draw an image in this scenario. See below for how the menu I'm not sure what I'm missing. Any help would be appreciated!

Thank you.

Capture.PNG
Hi there!

I believe you need to do that on the drawItemBackground or drawBackgroundRect method, inherited from Window_Selectable:

JavaScript:
Window_Selectable.prototype.drawItemBackground = function(index) {
    const rect = this.itemRect(index);
    this.drawBackgroundRect(rect);
};

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);
};

These methods are the ones that draw that dark rectangle on the commands.
So if you want to draw an image behind the text, you can draw it right at the end of the drawBackgroundRect , on the contentsBack

Or you could draw it before the drawItem, inherited from Window_Command:
JavaScript:
Window_Command.prototype.drawItem = function(index) {
    const rect = this.itemLineRect(index);
    const align = this.itemTextAlign();
    this.resetTextColor();
    this.changePaintOpacity(this.isCommandEnabled(index));
    this.drawText(this.commandName(index), rect.x, rect.y, rect.width, align);
};

To draw an image, you could either create a sprite and add it as a child on the window and set its position according to the dark background rect(this.itemRect(index))
JavaScript:
const rect = this.itemRect(index)
const image = ImageManager.loadPicture(filename)
const spr = new Sprite(image)
this.addChild(spr)
spr.move(rect.x, rect.y)

Or you can draw it directly on the contentsBack(I think this is best than above method):
  • "s" means Source
  • "d" means Destination
  • "w" means the width
  • "h" means the height

JavaScript:
const image = ImageManager.loadPicture(filename)
const rect = this.itemRect(index)
image.addLoadListener(() => {
    const sx = 0
    const sy = 0
    const sw = image.width
    const sh = image.height
    const dx = rect.x
    const dy = rect.y
    const dw = rect.width
    const dh = rect.height
    this.contentsBack.blt(image, sx, sy, sw, sh, dx, dy, dw, dh)
})

If you do not want to fit/stretch the image into the dark rectangle, you can choose not to use dw and dh, and they will be the same value as the image width/height.

for reference of the BLT method from bitmap:

JavaScript:
Bitmap.prototype.blt = function(source, sx, sy, sw, sh, dx, dy, dw, dh) {
    dw = dw || sw;
    dh = dh || sh;
    try {
        const image = source._canvas || source._image;
        this.context.globalCompositeOperation = "source-over";
        this.context.drawImage(image, sx, sy, sw, sh, dx, dy, dw, dh);
        this._baseTexture.update();
    } catch (e) {
        //
    }
};

Hope I didn't miss anything! ^^
Hi there!

I believe you need to do that on the drawItemBackground or drawBackgroundRect method, inherited from Window_Selectable:

JavaScript:
Window_Selectable.prototype.drawItemBackground = function(index) {
    const rect = this.itemRect(index);
    this.drawBackgroundRect(rect);
};

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);
};

These methods are the ones that draw that dark rectangle on the commands.
So if you want to draw an image behind the text, you can draw it right at the end of the drawBackgroundRect , on the contentsBack

Or you could draw it before the drawItem, inherited from Window_Command:
JavaScript:
Window_Command.prototype.drawItem = function(index) {
    const rect = this.itemLineRect(index);
    const align = this.itemTextAlign();
    this.resetTextColor();
    this.changePaintOpacity(this.isCommandEnabled(index));
    this.drawText(this.commandName(index), rect.x, rect.y, rect.width, align);
};

To draw an image, you could either create a sprite and add it as a child on the window and set its position according to the dark background rect(this.itemRect(index))
JavaScript:
const rect = this.itemRect(index)
const image = ImageManager.loadPicture(filename)
const spr = new Sprite(image)
this.addChild(spr)
spr.move(rect.x, rect.y)

Or you can draw it directly on the contentsBack(I think this is best than above method):
  • "s" means Source
  • "d" means Destination
  • "w" means the width
  • "h" means the height

JavaScript:
const image = ImageManager.loadPicture(filename)
const rect = this.itemRect(index)
image.addLoadListener(() => {
    const sx = 0
    const sy = 0
    const sw = image.width
    const sh = image.height
    const dx = rect.x
    const dy = rect.y
    const dw = rect.width
    const dh = rect.height
    this.contentsBack.blt(image, sx, sy, sw, sh, dx, dy, dw, dh)
})

If you do not want to fit/stretch the image into the dark rectangle, you can choose not to use dw and dh, and they will be the same value as the image width/height.

for reference of the BLT method from bitmap:

JavaScript:
Bitmap.prototype.blt = function(source, sx, sy, sw, sh, dx, dy, dw, dh) {
    dw = dw || sw;
    dh = dh || sh;
    try {
        const image = source._canvas || source._image;
        this.context.globalCompositeOperation = "source-over";
        this.context.drawImage(image, sx, sy, sw, sh, dx, dy, dw, dh);
        this._baseTexture.update();
    } catch (e) {
        //
    }
};

Hope I didn't miss anything! ^^
Thanks Eliaquim! This is super informative! I'll test out the different methods and see which one works best for me. 🙂
Edit: i got it solved, for anyone that might want to know the file name has to be in ""
and you also cant have this code alone becuase it wont know what to do with the index, you have to somehow integrade it into someting else that will draw the windows for you

sorry to post on this old dead thread but when i change the filename of this to the picture i wanna use in the picture folder it gives me an error of: "[image name] is not defined"
JavaScript:
const image = ImageManager.loadPicture(filename)
const rect = this.itemRect(index)
image.addLoadListener(() => {
const sx = 0
const sy = 0
const sw = image.width
const sh = image.height
const dx = rect.x
const dy = rect.y
const dw = rect.width
const dh = rect.height
this.contentsBack.blt(image, sx, sy, sw, sh, dx, dy, dw, dh)
})

Original thread: https://forums.rpgmakerweb.com/threads/adding-images-to-commands.145866/