2d display region and directGui

Hi,
I’m trying to parent directGui elements such as buttons to a created 2d display region and while the parenting works fine, the interaction is messed up. For instance, when the mouse cursor is atop a button, there is no change in text, nor is there any interaction when I click the button. However, when the cursor is outside the display region, the text changes and upon clicking some random area outside the display region, the button presses!
Here is the code:

        dr = base.win.makeDisplayRegion(0.2,0.5,0.2,0.5)
        dr.setSort(20)
        #set the dp color:
        dr.setClearColor(Vec4(0, 0,0, 0.3))
        dr.setClearColorActive(True)

        myCamera2d = NodePath(Camera('myCam2d'))
        lens = OrthographicLens()
        lens.setFilmSize(2, 2)
        lens.setNearFar(-1000, 1000)
        myCamera2d.node().setLens(lens)

        myRender2d = NodePath('myRender2d')
        myRender2d.setDepthTest(False)
        myRender2d.setDepthWrite(False)
        myCamera2d.reparentTo(myRender2d)
        dr.setCamera(myCamera2d)
        
        aspectRatio = base.getAspectRatio()
        myAspect2d = myRender2d.attachNewNode(PGTop('myAspect2d'))
        myAspect2d.setScale(1.0 / aspectRatio, 1.0, 1.0)
        myAspect2d.node().setMouseWatcher(base.mouseWatcherNode)
        
        butt1=DirectButton(text = ("OK", "click!", "rolling over", "disabled"),command=self.clickiWorks, parent=myAspect2d)

Here is how the error looks like:

2d dpError

So how would I go about fixing this error? Is it even an error, or did I forget to do something?

Thanks in advance.

The problem seems to be that you are assigning the default MouseWatcher to your new PGTop node. This MouseWatcher is bound to a DisplayRegion that covers the entire window, so it interprets the mouse coordinates relative to that DisplayRegion instead of your newly created DisplayRegion.

It will probably be best to create a new MouseWatcher as well, which you can then bind to your custom DisplayRegion:

        ...
        aspectRatio = base.getAspectRatio()
        myAspect2d = myRender2d.attachNewNode(PGTop('myAspect2d'))
        myAspect2d.setScale(1.0 / aspectRatio, 1.0, 1.0)
        mw_node = MouseWatcher("my_mouse_watcher")
        mw_node.set_display_region(dr)
        input_ctrl = base.mouseWatcher.parent
        mw = input_ctrl.attach_new_node(mw_node)
        bt_node = ButtonThrower("my_btn_thrower")
        mw.attach_new_node(bt_node)
        myAspect2d.node().setMouseWatcher(mw_node)
        ...

This worked for me.

Thanks, it works perfectly now, I knew I was forgetting to do something.