MV HELP!! How can I draw Icons on my custom command list?

Hi all! I have been creating my own Scene. Its taking a while but I managed to create scene, call it and close it. Now Im populating it with some selectable commands. Im wondering, is there a way I can add Icons to my command names?

The way im creating my command list is:
Code:
Window_CustomHorzCommand.prototype.makeCommandList = function() {
    this.addCommand ("¤ Bree", "command1");
    this.addCommand ("¤ Teotl", "command2");
    this.addCommand ("¤ Kieran", "command3");
};

Instead of the " ¤ " I want to add an icon from the iconset.

Thank you!
Hi @Poryg, I´ve been looking at your tutorials and that's how I achieved to add a new Scene to my game. I use your code on your videos and im wondering, how can I have my selectable list commands closer to one another? So far it looks like this:


Clipboard02.jpg




. And im trying to recreat this:

Clipboard03.jpg


thank you!
Im wondering, is there a way I can add Icons to my command names?
Like most things with scripting/plugins, the answer in this case is "yes", but you will have to put in a bit of effort. More than normal.

Let me explain: In RMMV (and VX/Ace and probably MZ), the "quick" and "simple" way to draw icons in a Window is to use the "drawTextEx()" function. This function parses whatever string that it is fed and detects text codes like "\I[123]" or "\V[456]" - the sort you put into your Show Text event commands to show icons or variable values in your text - and prints the relevant values in their place. So if you had a string like "\I[1] Bree" then "drawTextEx()" would print icon #1 from the iconset onto the screen followed by "Bree".

However, Window_Command does not use "drawTextEx()" when it prints the command names. It uses the basic "drawText()" instead, which does not look for any text codes.

The way it works is that Window_Command's "addCommand()" pushes its arguments, including the name string of the command added, into an Array called "_list" (as in 'command list') and then bulk prints all the command names there on the list using one single function called "drawAllItems()", which is inherited from its parent class, Window_Selectable (which is called in Window_Command's "refresh()" method when it calls Window_Selectable's version of "refresh()").

For each and every command to be printed, "drawAllItems()" then calls the function "drawItem()", adjusting the x/y argument for each time it calls "drawItem()" so that the next command isn't drawn over the previous one on screen. And "drawItem()" itself then calls "drawText()" to actually draw the text.

So you might be thinking: "Well, in that case, why can't I just modify Window_Command's "drawItem()" to use drawTextEx() instead of drawText()"?

You can, but you shouldn't. The reason is because - "drawTextEx()" does not support text alignment. The basic "drawText()" allows you to specify aligning text to left/right/center but "drawTextEx()" has no functionality for that.

Window_Command has a property/function called "itemTextAlign()" that you are meant to be able to use/change in order to set the text alignment of your command names. If you just tried to replace how it uses "drawText()" with "drawTextEx()", you would break that functionality.

So the "simple" way is out.

You can't just use "drawTextEx()" to draw your icons without nasty side effects.

That means you have to do it the "long" way.

You will have to modify Window_Command's "drawItem()" to manually draw the icons. And also shift the x-position fed into "drawText()" so it draws the command name text next to your icon instead of ontop of it.

The function you need to call to draw icons is called "drawIcon()".

"drawIcon()" is what every Window ends up using to display icons on the screen, "drawTextEx()" itself ends up calling "drawIcon()" when it detects a text code to draw an icon. You can check the code for it in Window_Base, it takes 3 arguments (iconIndex, x and y) and reads the iconset image file for the specified icon at the index and print it at the coordinates given.

But you say: "Hey wait, but then how will "drawIcon()" know which icon index you want to use for the command it's drawing? It can't read text codes in the name string, after all and "addCommand()" doesn't pass any icon index data to it."

No, no it doesn't. Not by default. You will have to feed in that data somehow.

Fortunately, Window_Command already provides some infrastructure for including additional information with each command in "addCommand()".

Because the "addCommand()" function actually takes 4 arguments, not 2. The first two are the name and function to call upon selection. The last 2 are "enabled" (true/false value to determine whether or not it is greyed out and unselectable, defaults to being true if nothing is specified) and "ext" (which means "extra data", I think only used in "Window_SkillList" to feed in skill type, defaults to null if nothing is specified).

The "ext" argument will end up being pushed into the "_list" alongside the name and other 2 arguments, allowing you to retrieve the data later.

So you can adjust your "addCommand()"s to look something like this:
JavaScript:
addCommand("Bree", "command1", true, 111)
And then modify "drawItem()" to look something like this:
JavaScript:
Window_CustomHorzCommand.prototype.drawItem = function(index) {
    ...
    ...
    this.drawIcon(this._list[index].ext, rect.x, rect.y);
    this.drawText(this.commandName(index), rect.x + 36, rect.y, rect.width, align);
}

And it should draw icon 111 and then "Bree".
@Traverse, Thank you very much! your workaround worked perfectly!

Capture.JPG

Since I got you here maybe you can help with my other Issue, my second post on this thread describes my problem, basically, why my command list is drawn so apart from each other? I want to have the commands inside de blue box and in a 1 row 2 column format. Like this:
Clipboard03.jpg


Any pointers?
Since I got you here maybe you can help with my other Issue, my second post on this thread describes my problem, basically, why my command list is drawn so apart from each other?

Window_Selectable and all of its variants including Window_Command and Window_HorzCommand which you appear to be building your custom window off, have a property called "maxCols" (maximum number of columns per row of commands).
JavaScript:
Window_Selectable.prototype.maxCols = function() {
    return 1;
};
The "default" value set by Window_Selectable by it is "1" as you can see. One single column, like how the commands in the title screen command menu are listed in.

However Window_HorzCommand overwrites this. By default, HorzCommand-type windows use 4 columns instead of 1.
JavaScript:
Window_HorzCommand.prototype.maxCols = function() {
    return 4;
};
This is meant to be changed depending on the window. For example, the shop command windows (Window_ShopCommand) overwrite this property to have 3 max columns instead of 4. The name input window (Window_NameInput) overwrites it to have 10.

So if you want only 2 columns, you must change it to 2 for your window.

As for why they are so far apart from each other, you need to look at another property, this one belonging to the parent of all Window objects, Window_Base. This property is called "lineHeight":
JavaScript:
Window_Base.prototype.lineHeight = function() {
    return 36;
};
By default, all Windows draw their lines 36 pixels tall. Every single Window, no exceptions. They all refer to this property when they draw their text (you can even see where it is used when drawing text if you check Window_Base's "drawText()" function).

Like with "maxCols", you will need to overwrite this property in your custom window if you want the lines in there to be drawn less than 36 pixels in height.

I want to have the commands inside de blue box

When you created your window inside the scene, if it was a HorzCommand-type window, you needed to put in 2 arguments for it.

It looks like you put in (0, 0) for it when you created it.

I suggest you try changing those numbers and see what happens.
while you might have 1 window, you could make the window smaller,
than add those commands in here, while I made my my buttons differently,
it is setup in 1 window, but using a wider window for more columns and or rows,
you can align them much better from within.

and as Traverse said, starting window is 0,0 (x, y), so you need to align those
commands with the X first, in a window, you move the window and add a
background image behind it or fully in your picture.

so you have a square window a full size background, than your commands are
0,0 and window starts at 0,100 for example.

Original thread: https://forums.rpgmakerweb.com/threads/help-how-can-i-draw-icons-on-my-custom-command-list.135309/