MZ Draw Icon inside won command window

Hi,

I have my own Scene and in there is my own window which is derived from Window_Command. Now I wanted draw an Icon for certain commands using the text code '\i[89]' for example.
But instead of drawing the icon, it just shows the text code inside the command.
I hope someone knows a way how to solve this problem.


Greetings, Illya
What did you do to try drawing the icon?
Hi there!

By default, the Window Command does not draw the command names using the method that can draw the escape codes.
But if you use my plugin, Eli Global Escape Codes, you will be able to draw it on any window.
Example:
1636065712809.png


[EDIT] I just read your post again and see that you are building a code yourself, so I guess I can improve my answer in case you want to do it yourself:

There are two methods for drawing text on the windows:

JavaScript:
Window_Base.prototype.drawText = function(text, x, y, maxWidth, align) {
    this.contents.drawText(text, x, y, maxWidth, this.lineHeight(), align);
};

Window_Base.prototype.drawTextEx = function(text, x, y, width) {
    this.resetFontSettings();
    const textState = this.createTextState(text, x, y, width);
    this.processAllText(textState);
    return textState.outputWidth;
};

Make sure you use the drawTextEx for this.
But that will also change the way you set the alignment of your commands.

If you are inserting the command names via plugin parameter, you can just put "\[id]command name"
But in case you are building it with a string, inside your code, you may need to do like that:
"\x1bI[id]"

Basically, add a prefix "\x1b" + escape code + command name.
Example:
"\x1bI[30]Options"
1636066107699.png
Hi there!

By default, the Window Command does not draw the command names using the method that can draw the escape codes.
But if you use my plugin, Eli Global Escape Codes, you will be able to draw it on any window.
Example:
View attachment 205870
Never pass up an opportunity to plug your own stuff, eh Eli? 😉
Never pass up an opportunity to plug your own stuff, eh Eli? 😉
Well, in this case, I think I got ahead of myself xD
Since I notice later that it is the Learning Javascript and not Plugin section xD
Already edited my post haha!
But hey, Op, feel free to also take a look at my code if you want 😅
@Eliaquim Thanks for your help and sorry for the delay in my reply. How you describes it, it works perfectly fine. I have another question and I do hope that you may be able to answer it.
My problem is that if I add the following code inside my window derived from the Window_Command class it add icons before the command name but from the beginning of the icons sprite sheet, and not what icon I wanna have.
JavaScript:
drawItem(index) {
    super.drawItem(index);
    this.drawIcon(index, this.itemRect(index).x + 20, this.itemRect(index).y + 4);
}

My question is if you know a way to specify, using this method, to draw specific icons via the addCommand method.
@Eliaquim Thanks for your help and sorry for the delay in my reply. How you describes it, it works perfectly fine. I have another question and I do hope that you may be able to answer it.
My problem is that if I add the following code inside my window derived from the Window_Command class it add icons before the command name but from the beginning of the icons sprite sheet, and not what icon I wanna have.
JavaScript:
drawItem(index) {
    super.drawItem(index);
    this.drawIcon(index, this.itemRect(index).x + 20, this.itemRect(index).y + 4);
}

My question is if you know a way to specify, using this method, to draw specific icons via the addCommand method.
Hi!

The reason for it to draw the icons at the beginning of the icon sprite sheet is that you are inserting the command index into the argument of the icon index in the draw icon method.

JavaScript:
Window_Base.prototype.drawIcon = function(iconIndex, x, y) {
    const bitmap = ImageManager.loadSystem("IconSet");
    const pw = ImageManager.iconWidth;
    const ph = ImageManager.iconHeight;
    const sx = (iconIndex % 16) * pw;
    const sy = Math.floor(iconIndex / 16) * ph;
    this.contents.blt(bitmap, sx, sy, pw, ph, x, y);
};

And since you are calling the drawIcon method after the super.drawItem(index), there is no way that the icon can be drawn before the command like in the screenshot I showed to you above.
It will overlap the first letters of the command text.

You can try setting this.itemRect(index).x - ImageManager.iconWidth to draw the icon before the command text, but maybe it will go off the content

You can find a way to draw it after, calculating the command name text width and setting the draw icon X coordinate to star on any value from there.

But as I can see, if you want to use the drawIcon method, you will need to overwrite the drawItem function, to first draw the icon, then start drawing the command text after the icon.

Also, you have to find a way to get the icon id you want for each command. I suggest you create an array property on your window or build it with plugin parameters. Then you get the icon id based on the command index:

JavaScript:
const iconArray = [13, 45, 30]

drawItem(index) {
    super.drawItem(index); // You will have to overwrite the function for that.
    const iconId = iconArray[index] //
    this.drawIcon(iconId, this.itemRect(index).x + 20, this.itemRect(index).y + 4);
}

Original thread: https://forums.rpgmakerweb.com/threads/draw-icon-inside-won-command-window.141855/