If you are triggering a warning from hardware, I assume you want to call this function from the Model.cpp file.
I've accomplished this by creating a BaseLayer custom container with all elements common to all screens (such as warning modals) having their code defined within. This BaseLayer is added to all screens. I register a pointer in Model.cpp to the current instance of the BaseLayer. Then when I need the modal, I (carefully) call the function directly from the Model to the BaseLayer. This BaseLayer pointer needs to be updated every time it is instantiated or destroyed (i.e. every screen change), and nullptr checks need to be done in Model.cpp before it is used otherwise you risk a hardfault.
Here's how to do it:
- Create a "BaseLayer" custom container. Any common elements you need on all screens should be added here (such as your modal window).
- Create a method in BaseLayer that pops up your warning window.
- Create a global Model pointer in Model.cpp.
- Define a member pointer of type BaseLayer* in Model.hpp
- In Model::Model() constructor, set the BaseLayer pointer to nullptr, and set your Model pointer to this
- In your BaseLayer code, create an extern Model* reference.
- In your BaseLayer::BaseLayer() constructor, register this instance of the BaseLayer in the Model by setting the model's BaseLayer pointer to this (via the extern Model pointer)
- Create a BaseLayer::~BaseLayer() destructor, and in it make sure to reset that BaseLayer pointer in Model back to nullptr (again via the extern Model pointer)
- When your hardware event happens in your Model.cpp code, check if the BaseLayer pointer is null and if not, call the function that displays the modal.
It's not really "elegant" insofar as it doesn't follow the MVP pattern. However, I tried following the MVP pattern and calling a modellistener function to alert the presenters to call a view method to call a BaseLayer method... it's way too much hassle. Breaking the "rules" and selectively using a global pointer from the model to the baselayer is much easier as long as it is used sparingly and carefully.
Hope this helps. I've heard rumors of "Template Screens" coming in future TGFX versions.