MZ Need help configuring Main Menu Portrait Mode

EDIT THIS WAS SOLVED. SCROLL DOWN ON THIS ORIGINAL POST FOR THE SOLUTION


Hey hey,

So I've been doing a lot of tweaks and I ran into a bit of a wall trying to fix the alignment of an element I'm drawing on screen.

What I'm looking looking to do is move the Flipped Grimoire and Flipped Weapon sections as far to the right in the Rectangle as possible.

Currently this is the result I'm seeing with the attached "Portrait Stacked" script:

Screenshot 2023-06-14 130043.png



I'm using a plugin @Arthran put together for me that handles the flipped images, hence the need for the "if /else" in there to make sure the names, gauges, and weapons adjust as well. I've attached that as well. I'm playing around with it and manually adjusting the X value. The issue with doing that is adding a hard X value in their doesn't guarantee it will always start at the same place.

The "8" in

const cx = rect.x + gw - grimIconSize - grimoireNameWidth - 8;
and
const cx2 = rect.x + gw - weaponIconSize - weaponNameWidth - 8;

is just to add padding.




My guess is the updated icon size and text widths aren't being accounted for properly in the cx and cx 2 formulas for the Flipped Grimoire and Default


Any ideas?


Bonus Points if we can also get non flipped characters portraits (on the left) to move as far right as possible too.
EDIT: COMPLETE! SEE ATTACHED UPDATED SCRIPTS THAT MAKE THE BELOW MAIN MENU SETUP POSSIBLE ^_^ , note that plugin parameter and plugin commands are in use to designate when portraits should be flipped as you'll note the differences of when Penelope is or isn't flipped
2 Actor Party:
1687690443607.png

4 Actor Party:
1687690545485.png



I haven't tested the icons Y position with font sizes beyond what I set up in the Portrait mode script, so your results may vary if you change it. Any tweaks to that function are welcome for your uses, if you find a "one size fits all" solve for keeping the icon centered be sure to shoot me a message.



Thanks again @Arthran and @NaosoX for getting this Main Menu to the awesomeness that it is so far!
Le Bump!
I dunno if it's just me or not, but your image isn't showing.
I dunno if it's just me or not, but your image isn't showing.
Updated the pic
*volleyball bump!*
Since I don't have your setup, I can't really test any code to be sure that I've solved your issue, so all I can do is point out the issues that are immediately apparent to me and hope it helps you.

There are some issues in this function here:
JavaScript:
Window_Base.prototype.drawResizedIconWithText = function(iconIndex, text, x, y, iconSize, fontSize) {
  var bitmap = ImageManager.loadSystem('IconSet');
  var pw = Window_Base._iconWidth;
  var ph = Window_Base._iconHeight;
  var sx = iconIndex % 16 * pw;
  var sy = Math.floor(iconIndex / 16) * ph;

  var textWidth = this.textWidthEx(text);
  var textHeight = this.contents.fontSize;
  var contentHeight = Math.max(iconSize, textHeight);
  var iconY = y + Math.floor((contentHeight - iconSize) / 2); // Center the icon vertically with the text
  var textY = y + Math.floor((contentHeight - textHeight) / 2); // Center the text vertically with the icon

  var resizedBitmap = new Bitmap(iconSize, iconSize);
  resizedBitmap.blt(bitmap, sx, sy, pw, ph, 0, 0, iconSize, iconSize);

  this.contents.fontSize = fontSize;
  this.contents.blt(resizedBitmap, 0, 0, iconSize, iconSize, x, iconY);
  this.drawText(text, x + iconSize, textY, textWidth);
  this.resetFontSettings();
};

I don't know where you got it from, but it seems to be an MV function, and it's referencing some things that don't exist in MZ: specifically Window_Base._iconWidth and Window_Base._iconHeight. I'm going to guess that you're using FOSSIL, which is preventing this from being an issue in your case, but it would be a good idea to go ahead and replace those references with the MZ equivalents, to remove the FOSSIL dependency.

You're also referencing a textWidthEx function, which actually doesn't exist in either MZ or MV, in the context in which it's being used. Unless you've got some plugin that has created that function for you, then this is likely the crux of your issue. Another problem is that you are attempting to measure the text width *before* you set the front size--which should result in an incorrect measurement even if the textWidthEx function actually existed.

Here is what I believe to be a fixed and MZ-compliant version of the function, though I can't really test it to be sure:
JavaScript:
Window_Base.prototype.drawResizedIconWithText = function(iconIndex, text, x, y, iconSize, fontSize) {
    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.fontSize = fontSize;
    const textWidth = this.textWidth(text);
    const textHeight = this.contents.fontSize;
    const contentHeight = Math.max(iconSize, textHeight);
    const iconY = y + Math.floor((contentHeight - iconSize) / 2); // Center the icon vertically with the text
    const textY = y + Math.floor((contentHeight - textHeight) / 2); // Center the text vertically with the icon

    const resizedBitmap = new Bitmap(iconSize, iconSize);
    resizedBitmap.blt(bitmap, sx, sy, pw, ph, 0, 0, iconSize, iconSize);

    this.contents.blt(resizedBitmap, 0, 0, iconSize, iconSize, x, iconY);
    this.drawText(text, x + iconSize, textY, textWidth);
    this.resetFontSettings();
};

I would also recommend moving the function to a plugin, rather than defining it in the VisuStella plugin parameters. Its current location isn't technically causing any errors, but it's not really a good place to put it, because it's causing the function to be redefined constantly, which is inefficient. Also, from an organizational standpoint, you probably shouldn't be hiding global functions in random hidden places like this.

The other issue is that in your code for drawing the flipped grimoires and weapons, you're using that same non-existent textWidthEx method, and also trying to measure the width based on the default font size (rather than the actual font size that you intend for the text to be). Try this instead, and see if it helps:

JavaScript:
// Draw Flipped Grimoire
const grimSlotId = 0;
const grimoire = actor.equips()[grimSlotId];
const grimoireName = grimoire ? grimoire.name : '';
const grimIcon = grimoire && grimoire.iconIndex > 0 ? grimoire.iconIndex : 0;
const grimIconSize = 24;
const resizedFontSize = 24;
this.contents.fontSize = resizedFontSize;
const grimoireNameWidth = this.textWidth(grimoireName);
const cx = rect.x + gw - grimIconSize - grimoireNameWidth - 8;
sy += lineHeight;
this.drawResizedIconWithText(grimIcon, grimoireName, cx, sy - 80, grimIconSize, resizedFontSize);

// Draw Flipped Weapon
const weaponSlotId = 1;
const weapon = actor.equips()[weaponSlotId];
const weaponName = weapon ? weapon.name : '';
const weaponIcon = weapon && weapon.iconIndex > 0 ? weapon.iconIndex : 0;
const weaponIconSize = 24;
this.contents.fontSize = resizedFontSize;
const weaponNameWidth = this.textWidth(grimoireName);
const cx2 = rect.x + gw - weaponIconSize - weaponNameWidth - 8;
sy += lineHeight;
this.drawResizedIconWithText(weaponIcon, weaponName, cx2, sy - 80, weaponIconSize, resizedFontSize);
Thank you for the advice with the references. And I agree the function is smarted in its own plugin, I've moved it. The tweaks to the Flipped portions is on the right track:

1687655582888.png


However the flipped weapon seems like its left justifying to the left side of the line before it:

1687655615417.png


Which is fine when its longer named items, but if the grimoire has a smaller name:

1687655648120.png


The idea is to have both the Flipped Grimoire and Flipped Weapon lines be right justified the way the gauges and name are.
Thank you for the advice with the references. And I agree the function is smarted in its own plugin, I've moved it. The tweaks to the Flipped portions is on the right track:

View attachment 266755

However the flipped weapon seems like its left justifying to the left side of the line before it:

View attachment 266756

Which is fine when its longer named items, but if the grimoire has a smaller name:

View attachment 266757

The idea is to have both the Flipped Grimoire and Flipped Weapon lines be right justified the way the gauges and name are.
Oops, I didn't catch this error earlier:
JavaScript:
const weaponNameWidth = this.textWidth(grimoireName);

Should be:
JavaScript:
const weaponNameWidth = this.textWidth(weaponName);
And this is what happens when I only ever work on this script after 9pm xD. THat did it. Thank you so much again Art.

1687660039899.png




Now to just make some adjustments so that the icon centers vertically on the text and I'm good to go for that section.




Any idea about the non flipped portrait having the huge gap to its right even though your Flipped portrait is fine?

1687660066560.png
No problem. The portrait gap thing is because the width of the portrait is smaller than the width of the space, and they're being drawn from the leftmost coordinate of the space. Please try this version of the Portrait Flip plugin, and see if it helps.
No problem. The portrait gap thing is because the width of the portrait is smaller than the width of the space, and they're being drawn from the leftmost coordinate of the space. Please try this version of the Portrait Flip plugin, and see if it helps.

Not in front of my computer just yet to test the update yet but want to confirm/clarify that the positioning of the flipped portraits is actually Perfect being left most so I don't want them moved.

I want the "Default" non flipped portraits to be drawn as right most as possible in their rectangle is all
Not in front of my computer just yet to test the update yet but want to confirm/clarify that the positioning of the flipped portraits is actually Perfect being left most so I don't want them moved.

I want the "Default" non flipped portraits to be drawn as right most as possible in their rectangle is all
I understand. That is how it should be in the version that I posted earlier, but I wasn't able to test it, so please let me know how it goes when you get the chance.
you are a magician Art ^_^

1687688494164.png



1687688519639.png


Thank you!

I'll see what I can do about centering the icons in the meantime. Have a good rest of your weekend


EDIT got it:

1687689883893.png


1687689896976.png


Attaching all updated scripts

Will also update the original post with The most up to date Portrait Mode Script, the updated function file and Arts updated plugin as well in case anyone stumbles upon this and wants to do something similar. Thanks again @Arthran . All a shout out to @NaosoX for helping me with the main menu portrait set up originally
This thread is being closed due to being solved. If you’d like it reopened, report it to let us know why. Thank you!

Original thread: https://forums.rpgmakerweb.com/threads/need-help-configuring-main-menu-portrait-mode.158340/