MZ Replace HP MP TP text with Icons

Afternoon All,

Anyone know a clean way to replace the the Text of Basic Gauges (HP, MP, TP) with icons? I'd like a solution that scales across all windows where [SIZE=15px][B]this.placeGauge[/B][/SIZE] or [SIZE=15px][B]this.placeBasicGauges[/B][/SIZE] is being used.


Effectively Going from:


1712940585168.png


To:

1712940567691.png



Any help is appreciated, if there's a plugin that'll handle it that's also welcome.


Disclaimer: I am not using Skill/States Core so I won't be able to use a solution based in that.
Maybe something like: https://hakuenstudio.itch.io/eli-super-text

I used that to replace the text with icon codes using \i[x] in the database.
Maybe something like: https://hakuenstudio.itch.io/eli-super-text

I used that to replace the text with icon codes using \i[x] in the database.

Unfortunately not working for me for some reason.
MZ's gauges are sprites, Sprite_Gauge; text codes are for windows, not sprites.

Instead you could try something like this:
Spoiler: plugin code
JavaScript:
/*:
 * @target MZ
 * @plugindesc Icons for gauge labels.
 * @author Caethyril
 * @url https://forums.rpgmakerweb.com/threads/167682/
 * @help Free to use and/or modify for any project, no credit required.
 */
;void (() => {
'use strict';

  /** Maps gauge type to icon index, for label. Unlisted types are unaffected. */
  const ICONS = {
    hp: 84,
    mp: 67,
    tp: 69
  };

  /** X draw offset for gauge label icons. */
  const DX = 0;

  /** Y draw offset for gauge label icons. */
  const DY = 0;

  /**
   * @param {Bitmap} bitmap Reference bitmap.
   * @param {number} iconIndex Index of icon to draw.
   * @param {number} x X draw coordinate.
   * @param {number} y Y draw coordinate.
   * @returns {boolean} `true` iff icon was drawn.
   */
  const drawIcon = function(bitmap, iconIndex = 0, x = 0, y = 0) {
    if (iconIndex > 0 && bitmap instanceof Bitmap) {
      // cf Window_Base.prototype.drawIcon
      const b  = ImageManager.loadSystem("IconSet");
      const pw = ImageManager.iconWidth;
      const ph = ImageManager.iconHeight;
      const sx = (iconIndex % 16) * pw;
      const sy = Math.floor(iconIndex / 16) * ph;
      bitmap.blt(b, sx, sy, pw, ph, x, y);
      return true;
    }
    return false;
  };

  // Patch - draw icon 
  const alias = Sprite_Gauge.prototype.drawLabel;
  Sprite_Gauge.prototype.drawLabel = function() {
    const i = ICONS[this._statusType];
    if (i > 0) {
      const x = DX + (this.labelOutlineWidth() >> 1);
      const y = DY + this.labelY();
      drawIcon(this.bitmap, i, x, y);
    } else
      alias.apply(this, arguments);
  };

})();
Edit the ICONS numbers, in the code, as appropriate. I did a quick test and it seemed OK for me~
Unfortunately not working for me for some reason.
Do you have the Eli Book plugin
MZ's gauges are sprites, Sprite_Gauge; text codes are for windows, not sprites.

Instead you could try something like this:
Spoiler: plugin code
JavaScript:
/*:
 * @target MZ
 * @plugindesc Icons for gauge labels.
 * @author Caethyril
 * @url https://forums.rpgmakerweb.com/threads/167682/
 * @help Free to use and/or modify for any project, no credit required.
 */
;void (() => {
'use strict';

  /** Maps gauge type to icon index, for label. Unlisted types are unaffected. */
  const ICONS = {
    hp: 84,
    mp: 67,
    tp: 69
  };

  /** X draw offset for gauge label icons. */
  const DX = 0;

  /** Y draw offset for gauge label icons. */
  const DY = 0;

  /**
   * @param {Bitmap} bitmap Reference bitmap.
   * @param {number} iconIndex Index of icon to draw.
   * @param {number} x X draw coordinate.
   * @param {number} y Y draw coordinate.
   * @returns {boolean} `true` iff icon was drawn.
   */
  const drawIcon = function(bitmap, iconIndex = 0, x = 0, y = 0) {
    if (iconIndex > 0 && bitmap instanceof Bitmap) {
      // cf Window_Base.prototype.drawIcon
      const b  = ImageManager.loadSystem("IconSet");
      const pw = ImageManager.iconWidth;
      const ph = ImageManager.iconHeight;
      const sx = (iconIndex % 16) * pw;
      const sy = Math.floor(iconIndex / 16) * ph;
      bitmap.blt(b, sx, sy, pw, ph, x, y);
      return true;
    }
    return false;
  };

  // Patch - draw icon
  const alias = Sprite_Gauge.prototype.drawLabel;
  Sprite_Gauge.prototype.drawLabel = function() {
    const i = ICONS[this._statusType];
    if (i > 0) {
      const x = DX + (this.labelOutlineWidth() >> 1);
      const y = DY + this.labelY();
      drawIcon(this.bitmap, i, x, y);
    } else
      alias.apply(this, arguments);
  };

})();
Edit the ICONS numbers, in the code, as appropriate. I did a quick test and it seemed OK for me~
This works great! thank you @caethyril


Would it be a heavy lift to have the icon resizable?


Do you have the Eli Book plugin

Yes lol, I use his plugins already for other stuff

Follow up here @caethyril , adjusting the DY cuts off the Icon if I use -10

1712959270153.png


Same for 10

1712959301823.png



Adjusting the DX seemed to be fine though

1712962126516.png
adjusting the DY cuts off the Icon if I use -10
That's because the icon is being painted to the gauge bitmap's canvas. The canvas starts at 0, but you're trying to start painting at labelY() (default 3) - 10, so the top 7 px will be cut off.

I hadn't noticed earlier, but there's a similar issue for any Y-shift: Sprite_Gauge has a default height of 32 px, which is exactly enough for a standard-size icon. If your icons have no blank space around them, try DY = -3.

Would it be a heavy lift to have the icon resizable?
Bitmap#blt recognises optional destination width/height arguments, e.g. draw at 16x16 px:
JavaScript:
bitmap.blt(b, sx, sy, pw, ph, x, y, 16, 16);
However, scaling pixel art rarely gives good results, so:
  1. If you want the icon smaller, I'd suggest just making it smaller in its 32x32 px box on the icon sheet.

  2. If you want it larger, it'd probably be best to use an entirely different approach. E.g. add the gauge icon as a new child sprite of the gauge. Maybe like this:
    Spoiler: plugin code v2
    JavaScript:
    /*:
     * @target MZ
     * @plugindesc Images for gauge labels.
     * @author Caethyril
     * @url https://forums.rpgmakerweb.com/threads/167682/
     * @help Images should be placed in the img/system subfolder.
     * Edit the plugin code to customise image filenames.
     *
     * Free to use and/or modify for any project, no credit required.
     */
    ;void (() => {
    'use strict';
    
      /**
       * Maps gauge type to `img/system` filename, for label.
       * Unlisted types are unaffected.
       */
      const DATA = {
        hp: "gaugeLabel_hp",
        mp: "gaugeLabel_mp",
        tp: "gaugeLabel_tp"
      };
    
      /** X position (parent coords) for gauge label image. */
      const X = 0;
    
      /** Y position (parent coords) for gauge label image. */
      const Y = 0;
    
      /** Identifier for new "image label child sprite" property on `Sprite_Gauge`. */
      const P = "_imageLabelSprite";
    
      /**
       * Ensures child sprite for gauge label image is present when appropriate.
       * @this Sprite_Gauge
       * @returns {boolean} `true` iff image label sprite is now present.
       */
      const ensureGaugeImageLabel = function() {
        const t = this._statusType;
        const s = DATA[t];
        if (s) {
          if (!this[P]) {
            const c = this[P] = new Sprite(ImageManager.loadSystem(s));
            c.move(X, Y);
            this.addChild(c);
          }
          return true;
        }
        return false;
      };
    
      // Patch - use image for gauge label instead, when appropriate.
      const alias = Sprite_Gauge.prototype.drawLabel;
      Sprite_Gauge.prototype.drawLabel = function() {
        if (!ensureGaugeImageLabel.call(this))
          alias.apply(this, arguments);
      };
    
    })();
I actually went the #1 route of just making the icon smaller directly on the tilesheet.



The second script you sent over draws the icon independently?
Nice! 😂

The second script you sent over draws the icon independently?
Yes, it makes the "icon" its own sprite. As a child of the gauge it still moves with the gauge, but is otherwise much less limited display-wise.
This thread is being closed due to being reported as solved. If you need it reopened, let us know. Thank you!

Original thread: https://forums.rpgmakerweb.com/threads/replace-hp-mp-tp-text-with-icons.167682/