2024-05-21 01:06 AM
Hi,
I have been struggling to get my head around the way custom container call backs work.
I have a custom container that has three flex buttons, this container is then used on a single screenview multiple times.
I'd like to be able to identify the flex button pushed along with knowing what custom container this came from. Sounded simple and i am sure it is, my head is just failing to get around the multiple files that this process spreads across.
Solved! Go to Solution.
2024-05-22 11:02 AM
Great thanks, ill give the 2nd one a go :thumbs_up:
2024-05-22 09:29 AM
Hello Justin
Easiest way (?) to make this is use Triggers in custom container and actions in view. Then you dont have to play manually with callbacks.
First create trigger to your CustomContainer:
The variable is used for sending the clicked button index.
And then add interaction like this to all three button:
Set value for example 0 - 2 (red circle) for buttons as index.
Then in the view, add three action for receiving the callbacks.
Then add three interactions like this
Remember to write red circled 'value' !
Then implement container1/2/3Clicked functions for actions, you can copy prototypes from viewBase.hpp file (if you generate the code after adding actions).
Like this in view.cpp
#include <touchgfx/Utils.hpp>
.
.
.
void Screen1View::container1Clicked(int8_t value)
{
touchgfx_printf("Container 1, button %d Clicked\n", value);
}
void Screen1View::container2Clicked(int8_t value)
{
touchgfx_printf("Container 2, button %d Clicked\n", value);
}
void Screen1View::container3Clicked(int8_t value)
{
touchgfx_printf("Container 3, button %d Clicked\n", value);
}
and add to view.hpp
virtual void container1Clicked(int8_t value);
virtual void container2Clicked(int8_t value);
virtual void container3Clicked(int8_t value);
Then it works like this
But certainly this is bit sissy way, It certainly can be done also manually with callbacks. Which method you are considering ?
Br JTP
2024-05-22 10:25 AM
Or like this...manually:
Just set the button interactions to call virtual functions Button1/2/3Clicked at customContainer.
CustomContainer.cpp:
#include <gui/containers/CustomContainer.hpp>
CustomContainer::CustomContainer():
containerCallback(0)
{
}
void CustomContainer::initialize()
{
CustomContainerBase::initialize();
}
void CustomContainer::Button1Clicked()
{
if (containerCallback->isValid())
{
containerCallback->execute(*this,0);
}
}
void CustomContainer::Button2Clicked()
{
if (containerCallback->isValid())
{
containerCallback->execute(*this,1);
}
}
void CustomContainer::Button3Clicked()
{
if (containerCallback->isValid())
{
containerCallback->execute(*this,2);
}
}
void CustomContainer::setAction(GenericCallback< CustomContainer&,uint16_t >& callback)
{
containerCallback = &callback;
}
customContainer.hpp:
#ifndef CUSTOMCONTAINER_HPP
#define CUSTOMCONTAINER_HPP
#include <gui_generated/containers/CustomContainerBase.hpp>
class CustomContainer : public CustomContainerBase
{
public:
CustomContainer();
virtual ~CustomContainer() {}
virtual void initialize();
virtual void Button1Clicked();
virtual void Button2Clicked();
virtual void Button3Clicked();
void setAction(GenericCallback< CustomContainer&,uint16_t >& callback);
protected:
GenericCallback< CustomContainer& , uint16_t >* containerCallback;
};
#endif // CUSTOMCONTAINER_HPP
Screen1View.cpp:
#include <gui/screen1_screen/Screen1View.hpp>
#include <touchgfx/Utils.hpp>
Screen1View::Screen1View()
: CustomContainerClickedCallback(this, &Screen1View::CustomContainerClicked)
{
}
void Screen1View::setupScreen()
{
Screen1ViewBase::setupScreen();
customContainer1.setAction(CustomContainerClickedCallback);
customContainer2.setAction(CustomContainerClickedCallback);
customContainer3.setAction(CustomContainerClickedCallback);
}
void Screen1View::tearDownScreen()
{
Screen1ViewBase::tearDownScreen();
}
void Screen1View::CustomContainerClicked(CustomContainer& item,uint16_t buttonIndex)
{
uint8_t containerIndex=0;
if(&item==&customContainer1)containerIndex=1;
else if(&item==&customContainer2)containerIndex=2;
else if(&item==&customContainer3)containerIndex=3;
touchgfx_printf("Callback received container %d! ButtonIndex: %d\n" ,containerIndex,buttonIndex);
}
Screen1View.hpp:
#ifndef SCREEN1VIEW_HPP
#define SCREEN1VIEW_HPP
#include <gui_generated/screen1_screen/Screen1ViewBase.hpp>
#include <gui/screen1_screen/Screen1Presenter.hpp>
class Screen1View : public Screen1ViewBase
{
public:
Screen1View();
virtual ~Screen1View() {}
virtual void setupScreen();
virtual void tearDownScreen();
void CustomContainerClicked(CustomContainer& item,uint16_t );
protected:
Callback<Screen1View, CustomContainer&,uint16_t > CustomContainerClickedCallback;
};
#endif // SCREEN1VIEW_HPP
Does pretty much the same...:
Br JTP
2024-05-22 11:02 AM
Great thanks, ill give the 2nd one a go :thumbs_up:
2024-05-22 12:47 PM
Great that worked perfectly, thanks so much for this
2024-05-23 11:26 AM
Nice to hear, hope these examples helps also others in future.
Br JTP