I am developing using Ogre14.1 in Windows for making a clickable button in my project. I follow the official ogre documentation link is give below. https://ogrecave.github.io/ogre/api/latest/trays.html
here is my entire Application code
class MyApplication : public ApplicationContext, public InputListener {
public:
ShaderGenerator* shadergen = nullptr;
MyTrayListener mlistener; //see next code
SceneManager* scnMgr = nullptr;
SceneNode* LightNode = nullptr;
SceneNode* CameraNode = nullptr;
Viewport* viewport = nullptr;
void run() {
// Main loop
playing = true;
while (playing) {
getRoot()->renderOneFrame();
}
}
void setup() override {
ApplicationContext::setup();
Root* root = getRoot();
// Get the scene manager
scnMgr = root->createSceneManager();
// Register the input listeners
addInputListener(this);
// Create and set up the scene
createScene(); //adding lights, cameras, etc
addModels(); //added some models
scnMgr->addRenderQueueListener(getOverlaySystem());
}
void loadResources() override
{
addUI(); // ADDED TRAY BUTTONS
}
bool frameRenderingQueued(const Ogre::FrameEvent& evt) override
{
mTrayMgr->frameRendered(evt);
return ApplicationContext::frameRenderingQueued(evt);
}
void addUI() {
cout << "creating UI" << endl;
OgreBites::TrayManager* mTrayMgr = new OgreBites::TrayManager("InterfaceName", getRenderWindow(), &mlistener);
mTrayMgr->setListener(&mlistener);
mTrayMgr->showCursor();
mTrayMgr->showTrays();
mTrayMgr->showFrameStats(TL_BOTTOMLEFT, -1);
Button* b = mTrayMgr->createButton(TL_TOPLEFT,"MyButton", "OK!", 50.0f);
}
};
and here is my TrayListener implementation
class MyTrayListener : public OgreBites::TrayListener {
public:
void buttonHit(OgreBites::Button* button) override {
cout << "buttonHit"; //not printed, function not getting called
if (button->getName() == "MyButton") {
// Handle button click event here
button->setCaption("Clicked!");
}
}
};
It was not giving any errors but buttonHit() function was not triggered.
Edit 1: As understood in comments, I have moved mlistener to class scope:
class MyApplication : public ApplicationContext, public InputListener {
public:
ShaderGenerator* shadergen = nullptr;
SceneManager* scnMgr = nullptr;
MyTrayListener mlistener; // <-----------------
...
}
Edit2: I was wrongly creating TrayManager from setup(), instead of of loadResources() so fixed that. But now TrayManager constructor is throwing exception that file not found at the following line:
mCursor = (Ogre::OverlayContainer*)om.createOverlayElementFromTemplate("SdkTrays/Cursor", "Panel", nameBase + "Cursor");
I have already added "SdkTrays.zip" inside resources.cfg file.
The parent class ApplicationContext loads all resources from resources.cfg in its loadResources(); We didnt override it.