MZ Scale game window via script call?

Hi! I was wondering if anybody's made a plugin for RPG Maker MZ where you can change the scale of the game window via a script call and/or a button press.
There's a script for VX Ace called Fullscreen++ by Zeus81 and I essentially want that functionality for RPG Maker MZ. With Fullscreen++ you can press F6 to scale the screen from 544x416 to 1088x832, exactly doubling the resolution. And if you have a larger display you can scale it up again, allowing you to scale the game until it hits your monitor borders essentially. There's also a button for pressing F5 to go into windowed borderless mode, and you can still scale the game window in fullscreen which is great for people who prefer pixel-perfect resolutions.
I also want this functionality to be able to be called via a script call, since I have a custom options menu with a window scale setting in it.
To be clear, I don't want this to simply increase the game's resolution. I have a custom resolution and I just want the window to appear 2x, 3x etc. larger but stay the same internal resolution when adjusting the setting.
I don't use RPG Maker MZ, I do use RPG Maker MV, so I can only provide You help based on the MV version of the RPGM, here is how You can ( how I set ) window size, it will at the same time expand render view and viewport size:
JavaScript:
qWindow_Setup(){
    // Values I just set by default... didn't rly looked into what they
    // really does... but just call them every time xD
    Graphics._width            =this.qWindow_Width;
    Graphics._height           =this.qWindow_Height;
    Graphics.boxWidth          =this.qWindow_Width;
    Graphics.boxHeight         =this.qWindow_Height;
    SceneManager._screenWidth  =this.qWindow_Width;
    SceneManager._screenHeight =this.qWindow_Height;
    SceneManager._boxWidth     =this.qWindow_Width;
    SceneManager._boxHeight    =this.qWindow_Height;
    // actuall function to resize window, it should just move
    // the game window and have nothing to do with rendering
    window.resizeTo(this.qWindow_Width, this.qWindow_Height);}

Now, to read user input... You can make another duplicate of EventListener...
Don't worry, RPG Maker already made multiple duplicates ( each burn a bit of CPU time )
JavaScript:
        document.addEventListener("keydown",   (_E)=>this.qHandleKeyEvent(_E,true));
        document.addEventListener("keyup",     (_E)=>this.qHandleKeyEvent(_E,false));
And then You ofc. need a function that would handle the event, in my case it is just a function called "qHandleKeyEvent"
You could do something like:
JavaScript:
    qHandleKeyEvent(_E,_IsPressed){
        const _Key=_E.keyCode;
        if(_Key==117){
            // Pressed F6
            // resize window
        }
    }
You can get KeyCodes from website like: https://www.toptal.com/developers/keycode

Now... if You want to make it whole... here is full code that would do it for You:
JavaScript:
class qlResizeWindowKey{
    constructor(){
        Object.assign(this,{
            // Size of the Window in "Small" size in pixels
            qWindow_Width_Min  : 1360,
            qWindow_Height_Min :  768,
           
            // Size of the Window in "Big" size in pixels
            qWindow_Width_Max  : 1920,
            qWindow_Width_Min  : 1080,
           
            qIsFullMode        : 0 // Starting Mode - 0 for Small, 1 for Big
        });

        document.addEventListener("keyup",     (_E)=>this.qHandleKeyEvent(_E));
    }
   
    qHandleKeyEvent(_E){
        const _KeyCode = _E.keyCode;
        if( _KeyCode == 117 ){ // if F6 got pressed
            if(qIsFullMode == 0 ){ // if we are in small window set to big ( MAX )
                qSetWindow( this.qWindow_Width_Max, this.qWindow_Height_Max );
            }else{                 // Else, we must be in Big window, so set to small
                qSetWindow( this.qWindow_Width_Min, this.qWindow_Height_Min );
            }
        }
    }
   
    qSetWindow(_X,_Y){
        Graphics._width            =_X;
        Graphics._height           =_Y;
        Graphics.boxWidth          =_X;
        Graphics.boxHeight         =_Y;
        SceneManager._screenWidth  =_X;
        SceneManager._screenHeight =_Y;
        SceneManager._boxWidth     =_X;
        SceneManager._boxHeight    =_Y;
        window.resizeTo(_X, _Y);
    }
}
var qResizeWK = new qlResizeWindowKey();

Now, You can copy above and create new file with extension .js in Your plugins folder,
then in RPG Maker add Your plugin to the Engine / Project / Game and it's all, script already handles everything else...

That is assuming that MZ can work similar to MV...
And I need to tell You that this code does not MOVE window to the middle of the screen...
I didn't needed moving window yet ( cuz I'm still early in dev of my own game on RPGM )
But if that code works, then let me know, Ill upgrade it for You to also move the window to the middle of the screen 😛
That is assuming that MZ can work similar to MV...
And I need to tell You that this code does not MOVE window to the middle of the screen...
I didn't needed moving window yet ( cuz I'm still early in dev of my own game on RPGM )
But if that code works, then let me know, Ill upgrade it for You to also move the window to the middle of the screen 😛
Hi! Thank you very much for responding! Unfortunately this doesn't work, I get an error message of "Uncaught ReferenceError: qSetWindow is not defined". I even tried it in both MV and MZ and they both have the same error message. If you feel like fixing it and sending me another solution, I'd be happy to test in MZ once again!
I also want this functionality to be able to be called via a script call, since I have a custom options menu with a window scale setting in it.
Have you tried the code associated with F3 and F4 in MV/MZ as a script call?
JavaScript:
Graphics._switchStretchMode();    // F3 - Stretch Screen

Graphics._switchFullScreen();    // F4 - Toggle Full Screen
Have you tried the code associated with F3 and F4 in MV/MZ as a script call?
I didn't know that was a thing before today, so thank you! Though unfortunately that doesn't solve my problem. I want the screen's window to increase in 2x or 3x size, though the resolution of the game stays the same. i.e. the game resolution is still 1024x576 however the window is 3072x1728 when selecting the 3x scale option in my settings menu.
There is a setting in the System 2 category of the Database that changes this in RPG Maker MZ, called Screen Scale, where if you set the number to 2 it does increase the screen to 2048x1152 (and obviously 1 is the default resolution of 1024x576). I'm not sure if there is a script call for this that I could use in-game, however. I did search and have been search the MV/MZ master script call list, though I could never find a script call for this. If there is one and I've somehow been missing it this whole time, I'd happily use that over a plugin!
database.png
I'm not sure if there is a script call for this that I could use in-game, however. I did search and have been search the MV/MZ master script call list, though I could never find a script call for this.
I don't think it has been updated so you can open up the console/devtools during your playtest by pressing f8 or f12 (sometimes f10 depending on if you have other plugins that change it) and type in slowly (it will populate the info for you like a search bar) something like this:
JavaScript:
$dataSystem.advanced.screenScale
When you get to the screenScale part, type it in slowly to see what it suggests and go with that. Change the value to whatever you need (1, 2, or 3).

That being said, there might even be a $gameSystem part of it that saves it to memory so you can try searching through the game system's corescripts (in rmmz objects) for it.

I don't have MZ and it looks like you might have the latest version which I don't have access to so it may or may not be in there.

Hope this helps!
The game calls this on boot:
JavaScript:
Scene_Boot.prototype.adjustWindow = function() {
    if (Utils.isNwjs()) {
        const scale = this.screenScale();
        const xDelta = Graphics.width * scale - window.innerWidth;
        const yDelta = Graphics.height * scale - window.innerHeight;
        window.moveBy(-xDelta / 2, -yDelta / 2);
        window.resizeBy(xDelta, yDelta);
    }
};
By default this is the only time the game checks and applies the Database screenScale value.

For what you've asked, something like this might do the trick (untested):
Spoiler: plugin code
JavaScript:
/*:
 * @target MZ
 * @plugindesc Resize screen in integer steps when pressing F6.
 * @author Caethyril
 * @url https://forums.rpgmakerweb.com/threads/174689/
 * @help Free to use and/or modify for any project, no credit required.
 */
;void (() => {
"use strict";
  /** Identifier for new "scale multiplier" property on {@linkcode multObj}. */
  const P = Symbol();

  /** @returns {object} Object on which {@linkcode P} property is present. */
  const multObj = function() { return Graphics; };

  /**
   * Updates NWJS window scale/position.
   * @param {number} w Target width.
   * @param {number} h Target height.
   */
  const updateScale = function(w, h) {
    if (!w || !h)
      return;
    const dx = w - window.innerWidth;
    const dy = h - window.innerHeight;
    window.moveBy(-dx >> 1, -dy >> 1);
    window.resizeBy(dx, dy);
    alert("resized!");  // for testing
  };

  /**
   * @param {number} scale Scale value.
   * @returns {[number, number]} Scaled game width and height.
   */
  const getScaledSize = function(scale = 1) {
    return [Graphics.width * scale, Graphics.height * scale];
  };

  /**
   * Sets scale multiplier to given value and updates window size.
   * @param {number} scale Value to set for scale multiplier.
   */
  globalThis.setScaleMultiplier = function(scale = 1) {
    if (Utils.isNwjs())
      updateScale(...getScaledSize(multObj()[P] = scale));
  };

  /**
   * Increments scale multiplier, resets to 1 if over-sized, and updates window size.
   * @param {number} [plus=1] Optional, default 1. Value to add to scale multiplier.
   */
  globalThis.addScaleMultiplier = function(plus = 1) {
    if (!Utils.isNwjs())
      return;
    const scale = $dataSystem.advanced.screenScale ?? 1;
    // Size check based on screen ID 0.
    const { width, height } = require("nw.gui").Screen.screens[0].bounds;
    const iw = window.innerWidth;
    const ih = window.innerHeight;
    const dw = window.outerWidth - iw;
    const dh = window.outerHeight - ih;
    const o = multObj();
    let [ w, h ] = getScaledSize(scale * (o[P] += plus));
    if (w + dw > width || h + dh > height)
      [ w, h ] = getScaledSize(scale * (o[P] = 1));
    updateScale(w, h);
  };

  // Patch - init custom scale multiplier
  void (alias => {
    Graphics.initialize = function() {
      this[P] = 1;
      return alias.apply(this, arguments);
    };
  })(Graphics.initialize);

  // Patch - increment/reset multiplier when F6 is pressed.
  void (alias => {
    Graphics._onKeyDown = function(e) {
      if (!e.ctrlKey && !e.altKey && e.keyCode === 117)  // F6
        addScaleMultiplier();
      else
        alias.apply(this, arguments);
    };
  })(Graphics._onKeyDown);

})();
Corresponding script calls would be like setScaleMultiplier(2) or addScaleMultiplier().

I did a little searching but didn't find a way to reliably identify the screen where the game app is currently (reference), so I've simply based the upper bound on the size of the first screen.

[Edit: fixed Graphics.initialize patch as described in my follow-up post.]
The game calls this on boot:
JavaScript:
Scene_Boot.prototype.adjustWindow = function() {
    if (Utils.isNwjs()) {
        const scale = this.screenScale();
        const xDelta = Graphics.width * scale - window.innerWidth;
        const yDelta = Graphics.height * scale - window.innerHeight;
        window.moveBy(-xDelta / 2, -yDelta / 2);
        window.resizeBy(xDelta, yDelta);
    }
};
By default this is the only time the game checks and applies the Database screenScale value.
I noticed that before thanks to bass2yang giving me the idea for it, though I couldn't do anything with it - especially due to my lack of javascript knowledge.
For what you've asked, something like this might do the trick (untested):
It does work! With a couple asterisks;
On line 77 -
Code:
})(Graphics.initialize);
- I had to remove (Graphics.initialize), otherwise my game wouldn't boot. It would throw the error "Failed to initialize graphics." if I kept (Graphics.initialize) in the code of the plugin.
Also, I have to change the screen scale inside the dev console first before F6 changed the screen scale. After I would send at least one screen scale adjustment command, F6 would function properly. Though this is most likely due to one of my other plugins interfering. I have yet to test that.
Additionally, just being nit-picky here, but the game window will be off by a good 20 pixels or so each time I go back and forth between the scaling options.
Regardless, I thank you very much for your help!
I had to remove (Graphics.initialize), otherwise my game wouldn't boot. It would throw the error "Failed to initialize graphics." if I kept (Graphics.initialize) in the code of the plugin.
Ah, I forgot that method returns a value to indicate whether init was successful. Replace this part:
JavaScript:
  void (alias => {
    Graphics.initialize = function() {
      alias.apply(this, arguments);
      this[P] = 1;
    };
  })(Graphics.initialize);
...with this:
JavaScript:
  void (alias => {
    Graphics.initialize = function() {
      this[P] = 1;
      return alias.apply(this, arguments);
    };
  })(Graphics.initialize);
I'll go edit that into the code in my previous post~

Also, I have to change the screen scale inside the dev console first before F6 changed the screen scale.
That should be fixed by the edit I just mentioned.

Additionally, just being nit-picky here, but the game window will be off by a good 20 pixels or so each time I go back and forth between the scaling options.
Is that in position, size, or both? In which directions: X, Y, or both?

Like the core scripts, I've used Graphics.width * scale - window.innerWidth (and similar for height): getScaledSize -> updateScale. The only difference I can see in that area is that I've used >> 1 for integer division by 2, instead of / 2 like the core scripts. Not sure how that could result in what you describe, though. 😴


I also just realised that my screen bounds check ignores DPI scaling and work area restrictions (e.g. space occupied by taskbar). So you may want to try replacing this line:
JavaScript:
const { width, height } = require("nw.gui").Screen.screens[0].bounds;
...with (something like) this:
JavaScript:
const screen = require("nw.gui").Screen.screens[0];
let { width, height } = screen.work_area;
width /= screen.scaleFactor;
height /= screen.scaleFactor;
That should be fixed by the edit I just mentioned.
Yup! Game no longer fails to boot and I can press F6 before sending out a manual screen scale command.
Like the core scripts, I've used Graphics.width * scale - window.innerWidth (and similar for height): getScaledSize -> updateScale. The only difference I can see in that area is that I've used >> 1 for integer division by 2, instead of / 2 like the core scripts. Not sure how that could result in what you describe, though. 😴
Interesting! It may just be a fault of RPG Maker itself, then, lol.
I also just realised that my screen bounds check ignores DPI scaling and work area restrictions (e.g. space occupied by taskbar). So you may want to try replacing this line:
Replaced, though occasionally (thankfully, MUCH LESS frequently) from time to time it will snap to the top left corner of my screen or it will snap up and left by about 200~ pixels from the middle of my screen. Obviously if I spam between different scaling options the offset will happen much more frequently, though it does still happen on its own. Again, it could be a fault of RPG Maker - if you're depending on core script stuff, anyway - or a fault of JavaScript or a fault of my computer/monitor setup, etc. etc.
Again, thank you very much for helping me with this, especially now that it works much smoother! 😀
Is that in position, size, or both? In which directions: X, Y, or both?
To clarify on this, it's not exactly straightforward how it happened before with the previous code. It was about 20 pixels up and 5 pixels left? And it may not have been exactly the same amount of pixels every time though I fail to see how that could happen, so most likely it's faulty memory on that one 😛
Great! It's very possible I've missed something, but if so then (naturally) I can't see what it is. 😅

The outerWidth stuff is to account for the window's title bar and borders, which are typically larger on top/bottom than left/right. But I only use that for deciding on the scale in addScaleMultiplier, so I don't think that's it.

If you're on a multi-monitor setup then yes, there might be various reasons for this kind of thing. (I have no real experience with multiple monitors, so this is just guesswork.) Test with the game window on each monitor, if you haven't already, to see if there's a difference. Otherwise maybe the NWJS moveBy method and/or OS screen settings are getting quirky. E.g. if the window is near the edge of the current screen, and is told to move partially onto the other screen, perhaps its position is capped to the current screen.

Someone more knowledgable might happen along with more concrete answers~ 👋
this.qWindow_Width;
I don't use RPG Maker MZ, I do use RPG Maker MV, so I can only provide You help based on the MV version of the RPGM, here is how You can ( how I set ) window size, it will at the same time expand render view and viewport size:
JavaScript:
qWindow_Setup(){
    // Values I just set by default... didn't rly looked into what they
    // really does... but just call them every time xD
    Graphics._width            =this.qWindow_Width;
    Graphics._height           =this.qWindow_Height;
    Graphics.boxWidth          =this.qWindow_Width;
    Graphics.boxHeight         =this.qWindow_Height;
    SceneManager._screenWidth  =this.qWindow_Width;
    SceneManager._screenHeight =this.qWindow_Height;
    SceneManager._boxWidth     =this.qWindow_Width;
    SceneManager._boxHeight    =this.qWindow_Height;
    // actuall function to resize window, it should just move
    // the game window and have nothing to do with rendering
    window.resizeTo(this.qWindow_Width, this.qWindow_Height);}

Now, to read user input... You can make another duplicate of EventListener...
Don't worry, RPG Maker already made multiple duplicates ( each burn a bit of CPU time )
JavaScript:
        document.addEventListener("keydown",   (_E)=>this.qHandleKeyEvent(_E,true));
        document.addEventListener("keyup",     (_E)=>this.qHandleKeyEvent(_E,false));
And then You ofc. need a function that would handle the event, in my case it is just a function called "qHandleKeyEvent"
You could do something like:
JavaScript:
    qHandleKeyEvent(_E,_IsPressed){
        const _Key=_E.keyCode;
        if(_Key==117){
            // Pressed F6
            // resize window
        }
    }
You can get KeyCodes from website like: https://www.toptal.com/developers/keycode

Now... if You want to make it whole... here is full code that would do it for You:
JavaScript:
class qlResizeWindowKey{
    constructor(){
        Object.assign(this,{
            // Size of the Window in "Small" size in pixels
            qWindow_Width_Min  : 1360,
            qWindow_Height_Min :  768,
          
            // Size of the Window in "Big" size in pixels
            qWindow_Width_Max  : 1920,
            qWindow_Width_Min  : 1080,
          
            qIsFullMode        : 0 // Starting Mode - 0 for Small, 1 for Big
        });

        document.addEventListener("keyup",     (_E)=>this.qHandleKeyEvent(_E));
    }
  
    qHandleKeyEvent(_E){
        const _KeyCode = _E.keyCode;
        if( _KeyCode == 117 ){ // if F6 got pressed
            if(qIsFullMode == 0 ){ // if we are in small window set to big ( MAX )
                qSetWindow( this.qWindow_Width_Max, this.qWindow_Height_Max );
            }else{                 // Else, we must be in Big window, so set to small
                qSetWindow( this.qWindow_Width_Min, this.qWindow_Height_Min );
            }
        }
    }
  
    qSetWindow(_X,_Y){
        Graphics._width            =_X;
        Graphics._height           =_Y;
        Graphics.boxWidth          =_X;
        Graphics.boxHeight         =_Y;
        SceneManager._screenWidth  =_X;
        SceneManager._screenHeight =_Y;
        SceneManager._boxWidth     =_X;
        SceneManager._boxHeight    =_Y;
        window.resizeTo(_X, _Y);
    }
}
var qResizeWK = new qlResizeWindowKey();

Now, You can copy above and create new file with extension .js in Your plugins folder,
then in RPG Maker add Your plugin to the Engine / Project / Game and it's all, script already handles everything else...

That is assuming that MZ can work similar to MV...
And I need to tell You that this code does not MOVE window to the middle of the screen...
I didn't needed moving window yet ( cuz I'm still early in dev of my own game on RPGM )
But if that code works, then let me know, Ill upgrade it for You to also move the window to the middle of the screen 😛
THANK YOU SO MUCH

@randomshiet052, please refrain from necro-posting in a thread. Necro-posting is posting in a thread that has not had posting activity in over 30 days. You can review our forum rules here. Thank you.

Original thread: https://forums.rpgmakerweb.com/threads/scale-game-window-via-script-call.174689/