cancel
Showing results for 
Search instead for 
Did you mean: 

Multiple Custom Container with FlexButton Callbacks

justin11
Associate III

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.

1 ACCEPTED SOLUTION

Accepted Solutions
justin11
Associate III

Great thanks, ill give the 2nd one a go 👍

View solution in original post

5 REPLIES 5
JTP1
Lead

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:

JTP1_0-1716391808230.png

The variable is used for sending the clicked button index.

And then add interaction like this to all three button:

JTP1_1-1716391869365.png

Set value for example 0 - 2 (red circle) for buttons as index.

Then in the view, add three action for receiving the callbacks.

JTP1_3-1716393323260.png

 

Then add three interactions like this

JTP1_5-1716394474015.png

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

JTP1_6-1716394687722.png

But certainly this is bit sissy way, It certainly can be done also manually with callbacks. Which method you are considering ?

Br JTP

 

 

 

Or like this...manually:

Just set the button interactions to call virtual functions Button1/2/3Clicked at customContainer.

JTP1_1-1716398589992.png

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...:

JTP1_0-1716398470426.png

Br JTP

 

justin11
Associate III

Great thanks, ill give the 2nd one a go 👍

Great that worked perfectly, thanks so much for this

JTP1
Lead

Nice to hear, hope these examples helps also others in future.

Br JTP