MZ How to open window by clicking an image on a scenemap[SOLVED]

hi guys leaning javascript here from the scratch
Soo what I'm doing I created a window that will show in scenemap with
a picture on it and if I click it i can open like item window or equipment window something like
that but i can't find the area i tried to put it on x = 0 and y = 0 so the area that im suppose to touch
is in upper left corner i use console.log to find the area and I click all the area in the map but the event
won't trigger I actually don't know what to do this probably very advance to me right now, I don't know
can you help me out to understand this code a little?
if (TouchInput.isTriggered && TouchInput.x == 0, TouchInput.y == 0) {
console.log ("yes");
}
here's the code that Im doing right now?

JavaScript:
class showimgface extends Window_Base {
        constructor(rect) {
            super(rect);
            this.opacity = 0;
        }
        update() {
            super.update();
            this.sifupdate();
         
        }
       
        async sifupdate() {
            return new Promise((s,f) => {
                try {
                    this.contents.clear();
                    this.drawPicture("inventory", 0, 0);
                   
                } catch (e) {
                    f(e);
                }
            }).then().catch(e=>{
                throw e;
            });
        }
    }
    Window_Base.prototype.drawPicture = function(pictureName, x, y) {
        var bitmap = ImageManager.loadPicture(pictureName);
        var pw = bitmap.width;
        var ph = bitmap.height;
        var sx = 0;
        var sy = 0;
        this.contents.blt(bitmap, sx, sy, pw, ph, x, y);
    };

    const sms = Scene_Map.prototype.start;
    Scene_Map.prototype.start = function() {
        sms.call(this);
        this.sifwindow = new showimgface(new Rectangle(0, 0, 500, 624))
        this.addChild(this.sifwindow);
       
        if (TouchInput.isTriggered && TouchInput.x == 0, TouchInput.y == 0) {
            console.log ("yes");
        }

    }

thank you guys and have a nice day
Notes:
  1. TouchInput.isTriggered is a function, therefore will always be truthy:
    You want to check the result of evaluating that function, i.e. TouchInput.isTriggered().

  2. The x and y properties of TouchInput are screen coordinates (px). You'd typically check a range of coordinates rather than expecting the user to click a specific pixel on-screen.

  3. RMMZ's Rectangle class is a wrapper for PIXI.Rectangle, which has a handy contains method for checking if a given point is inside its bounds:

  4. For checking touch coordinates, this method from RMMZ's core scripts (rmmz_windows.js), may be a helpful reference:
    JavaScript:
    Window_Scrollable.prototype.isTouchedInsideFrame = function() {
        const touchPos = new Point(TouchInput.x, TouchInput.y);
        const localPos = this.worldTransform.applyInverse(touchPos);
        return this.innerRect.contains(localPos.x, localPos.y);
    };
    I.e.
    • Get the screen touch position,
    • Pass that through the game window's screen transform,
    • Check if the resulting point is inside the game window's borders.

  5. I would recommend using the Bitmap#addLoadListener method (rmmz_core.js) for drawing, instead of your async method setup. This ensures synchrony with the main game loop, since load listeners are updated as part of that serial loop. It also means your window won't keep trying to redraw itself every single frame!

  6. Unlike in your code, responsive game windows typically inherit from Window_Selectable. In that case the scene assigns a handler to the game window using the window's setHandler method: the window is then responsible for checking the conditions and invoking the handler via callHandler. Check rmmz_scenes.js and rmmz_windows.js for details/examples.

    Alternatively, it can be done all within window's code, e.g. Window_Selectable#setHelpWindowItem (rmmz_windows.js) updates another window via a reference that was previously given to it by the scene.

  7. You may want to define your drawPicture method on your new window class rather than Window_Base, to avoid naming conflicts with other plugins.

  8. For reasons of readability and consistent code style, consider using PascalCase for class names (e.g. Window_Base) and camelCase for variables/properties (e.g. this.drawPicture).
Putting that all together:
Spoiler: suggestion (untested)
JavaScript:
class Window_ShowImgFace extends Window_Base {

  constructor(rect) {
    super(rect);
    this.opacity = 0;
    // load and then draw inventory.png
    ImageManager.loadPicture("inventory")
                .addLoadListener(this.drawPicture.bind(this));
  }

  update() {
    super.update();
    this.processTouch();
  }

  isTouchedInsideFrame() {
    const touchPos = new Point(TouchInput.x, TouchInput.y);
    const localPos = this.worldTransform.applyInverse(touchPos);
    return this.innerRect.contains(localPos.x, localPos.y);
  }

  processTouch() {
    // show console output when clicked within border
    if (TouchInput.isTriggered() && this.isTouchedInsideFrame()) {
      console.log("Window_ShowImgFace clicked!");
    }
  }

  drawPicture(bmp) {
    // blit bitmap to this window's contents canvas
    const sx = 0;
    const sy = 0;
    const sw = bmp.width;
    const sh = bmp.height;
    const dx = 0;
    const dy = 0;
    this.contents.blt(bmp, sx, sy, sw, sh, dx, dy);
  }

};

(() => {
"use strict";

  const sifWindowRect = function() {
    return new Rectangle(0, 0, 500, 624);
  };

  const alias = Scene_Map.prototype.start;
  Scene_Map.prototype.start = function() {
    alias.apply(this, arguments);
    // add custom window
    this.sifWindow = new Window_ShowImgFace(sifWindowRect())
    this.addChild(this.sifWindow);
  };

})();
[Edit: added destination x/y coordinates to drawPicture.]
Notes:
  1. TouchInput.isTriggered is a function, therefore will always be truthy:
    You want to check the result of evaluating that function, i.e. TouchInput.isTriggered().

  2. The x and y properties of TouchInput are screen coordinates (px). You'd typically check a range of coordinates rather than expecting the user to click a specific pixel on-screen.

  3. RMMZ's Rectangle class is a wrapper for PIXI.Rectangle, which has a handy contains method for checking if a given point is inside its bounds:

  4. For checking touch coordinates, this method from RMMZ's core scripts (rmmz_windows.js), may be a helpful reference:
    JavaScript:
    Window_Scrollable.prototype.isTouchedInsideFrame = function() {
        const touchPos = new Point(TouchInput.x, TouchInput.y);
        const localPos = this.worldTransform.applyInverse(touchPos);
        return this.innerRect.contains(localPos.x, localPos.y);
    };
    I.e.
    • Get the screen touch position,
    • Pass that through the game window's screen transform,
    • Check if the resulting point is inside the game window's borders.

  5. I would recommend using the Bitmap#addLoadListener method (rmmz_core.js) for drawing, instead of your async method setup. This ensures synchrony with the main game loop, since load listeners are updated as part of that serial loop. It also means your window won't keep trying to redraw itself every single frame!

  6. Unlike in your code, responsive game windows typically inherit from Window_Selectable. In that case the scene assigns a handler to the game window using the window's setHandler method: the window is then responsible for checking the conditions and invoking the handler via callHandler. Check rmmz_scenes.js and rmmz_windows.js for details/examples.

    Alternatively, it can be done all within window's code, e.g. Window_Selectable#setHelpWindowItem (rmmz_windows.js) updates another window via a reference that was previously given to it by the scene.

  7. You may want to define your drawPicture method on your new window class rather than Window_Base, to avoid naming conflicts with other plugins.

  8. For reasons of readability and consistent code style, consider using PascalCase for class names (e.g. Window_Base) and camelCase for variables/properties (e.g. this.drawPicture).
Putting that all together:
Spoiler: suggestion (untested)
JavaScript:
class Window_ShowImgFace extends Window_Base {

  constructor(rect) {
    super(rect);
    this.opacity = 0;
    // load and then draw inventory.png
    ImageManager.loadPicture("inventory")
                .addLoadListener(this.drawPicture.bind(this));
  }

  update() {
    super.update();
    this.processTouch();
  }

  isTouchedInsideFrame() {
    const touchPos = new Point(TouchInput.x, TouchInput.y);
    const localPos = this.worldTransform.applyInverse(touchPos);
    return this.innerRect.contains(localPos.x, localPos.y);
  }

  processTouch() {
    // show console output when clicked within border
    if (TouchInput.isTriggered() && this.isTouchedInsideFrame()) {
      console.log("Window_ShowImgFace clicked!");
    }
  }

  drawPicture(bmp) {
    // blit bitmap to this window's contents canvas
    const sx = 0;
    const sy = 0;
    const sw = bmp.width;
    const sh = bmp.height;
    this.contents.blt(bmp, sx, sy, sw, sh);
  }

};

(() => {
"use strict";

  const sifWindowRect = function() {
    return new Rectangle(0, 0, 500, 624);
  };

  const alias = Scene_Map.prototype.start;
  Scene_Map.prototype.start = function() {
    alias.apply(this, arguments);
    // add custom window
    this.sifWindow = new Window_ShowImgFace(sifWindowRect())
    this.addChild(this.sifWindow);
  };

})();
wow that's a lot, I was expecting like two to three line of code lol I got a little overwhelm lol but I will read all the notes that you given me and then try it myself
thank you very much
Sorry for the wall of text, haha. Hopefully you get it working as intended! 👋
1655284095235.png

broooo it works
I copied your code but it wont draw the picture(inventory)
I mess the code a little bit and find out that this code don't work
JavaScript:
 drawPicture(bmp) {
    // blit bitmap to this window's contents canvas
    const sx = 0;
    const sy = 0;
    const sw = bmp.width;
    const sh = bmp.height;
    this.contents.blt(bmp, sx, sy, sw, sh);
  }
so I replace it with the code that I used the first time

JavaScript:
Window_Base.prototype.drawPicture = function(pictureName, x, y) {
        var bitmap = ImageManager.loadPicture(pictureName);
        var pw = bitmap.width;
        var ph = bitmap.height;
        var sx = 0;
        var sy = 0;
        this.contents.blt(bitmap, sx, sy, pw, ph, x, y);
    };

I read your note number 7 can i just use like alias or something?

but anyway bro thanks for the help i only need to change the opacity of the window to find the area that i click i don't have to do trial and error to find the clickable area this make my life easier
Great! 😂
I mess the code a little bit and find out that this code don't work
JavaScript:
  drawPicture(bmp) {
    // blit bitmap to this window's contents canvas
    const sx = 0;
    const sy = 0;
    const sw = bmp.width;
    const sh = bmp.height;
    this.contents.blt(bmp, sx, sy, sw, sh);
  }
😅 Whoops, yes, I think it's just because I forgot the destination x/y coordinates in contents.blt. Thanks for mentioning that, I've edited my previous post!

I read your note number 7 can i just use like alias or something?
An alias/patch stores the original method and typically adds to it in some way. In this case you want to define a new method, so that doesn't really apply. If you only need drawPicture on your new game window, then there is no need to define it on all game windows.

thanks for the help i only need to change the opacity of the window to find the area that i click i don't have to do trial and error to find the clickable area this make my life easier
From your screenshot I'm guessing you solved this? In case it's relevant, I noticed you had this line in your new window's constructor:
JavaScript:
this.opacity = 0;
I.e. make the window invisible. Opacity 255 (default) is fully opaque.
😅 Whoops, yes, I think it's just because I forgot the destination x/y coordinates in contents.blt. Thanks for mentioning that, I've edited my previous post!
it works thanksss....
another step forward in learning java hahaha thanks for the help
You're welcome, happy scripting!

This thread is being closed, due to being solved. If for some reason you would like this thread re-opened, please report this post and leave a message why. Thank you.

(Also: Java and JavaScript are different languages, try not to get them mixed up. 👋)

Original thread: https://forums.rpgmakerweb.com/threads/how-to-open-window-by-clicking-an-image-on-a-scenemap-solved.148530/