cancel
Showing results for 
Search instead for 
Did you mean: 

Working with Callbacks

gfxfloro
Senior

Hi,

I am trying to get a simple numpad on screen working by using callbacks, rather than virtual functions.

Here is the Screen1View.cpp.

#include <gui/screen1_screen/Screen1View.hpp>
 
Screen1View::Screen1View() :
//	TextClickedCallback(this, &Screen1View::editTextClickHandler){}
	numpadCallback(this, &Screen1View::numpadClickHandler){}
 
 
void Screen1View::setupScreen()
{
    Screen1ViewBase::setupScreen();
//    editText.setClickAction(TextClickedCallback);
    Button_0.setClickAction(numpadCallback);
    Button_1.setClickAction(numpadCallback);
    Button_2.setClickAction(numpadCallback);
    Button_3.setClickAction(numpadCallback);
    Button_4.setClickAction(numpadCallback);
    Button_5.setClickAction(numpadCallback);
    Button_6.setClickAction(numpadCallback);
    Button_7.setClickAction(numpadCallback);
    Button_8.setClickAction(numpadCallback);
    Button_9.setClickAction(numpadCallback);
    Button_neg.setClickAction(numpadCallback);
    Button_back.setClickAction(numpadCallback);
    Button_dec.setClickAction(numpadCallback);
    Button_OK.setClickAction(numpadCallback);
 
}
 
void Screen1View::tearDownScreen()
{
    Screen1ViewBase::tearDownScreen();
}
 
/*
void Screen1View::editTextClickHandler(const TextAreaWithOneWildcard& t, const ClickEvent& evt)
{
	if (&t == &editText)
	{
		remove(keyboard);
		keyboard.setPosition(380, 240, 400, 220);
		add(keyboard);
		keyboard.invalidate();
	}
}
*/
 
void Screen1View::numpadClickHandler(const ButtonWithLabel& b, const ClickEvent& evt)
{
 TypedText h = *&b.getLabel();
 
}

What I would like to know is, how do I determine the label of the button clicked to know which it is, and how do I implement multiple Callbacks on one screen? I don't get this syntax

Screen1View::Screen1View() :
	numpadCallback(this, &Screen1View::numpadClickHandler){}

How would I implement the second TextClickedCallback like in the code above?

1 ACCEPTED SOLUTION

Accepted Solutions

Sorry, i missed the non-functional code, haha =)

Here are a few things you can do with a button with label. Here i just added a button with label ("New Text") and wrote some code.

TypedText lblText = buttonWithLabel1.getLabelText();
TypedTextId id = lblText.getId();
Unicode::UnicodeChar buf[20];
Unicode::strncpy(buf, lblText.getText(), 20);

Here you have the TypedText, the ID of the typed text and the contents of the buffer (unicode values) associated with that ButtonWithLabel.

/Martin

View solution in original post

6 REPLIES 6
Martin KJELDSEN
Chief III

Hi @gfxfloro​,

You could determine which button was clicked and do something with the typed text (h) in the case of each.

void Screen1View::numpadClickHandler(const ButtonWithLabel& b, const ClickEvent& evt)
{
    TypedText h = *&b.getLabel();
 
    if(&b == &myButton)
    {
        ....
    }
}

Or you could inspect the TypedText ID and learn something from that, or you could extract the unicodes for that ID and inspect those. Not sure what you want to do here.

The correct syntax for initializing callbacks is this - You're linking a handler to the callback which is then called when someone calls callback->execute();

Screen1View::Screen1View() :
numpadCallback(this, &Screen1View::numpadClickHandler),
numpadCallback2(this, &Screen1View::numpadClickHandler2),
...
{
    ....
}

Does that answer your question?

/Martin

Ah, thank you, the callback syntax does make sense, it seemed like the braces belonged to the callback function.

Regarding the buttons, for some reason this line

TypedText h = *&b.getLabel();

doesn't work. The most elegant way would be a switch case in which I read, the labels from the buttons and differentiate from those. From the TouchGFX Documentation the getLabel() function should work, but I seem to have misunderstood something there.

Edit: And is there a way to have multiple ClickHandlers on the screen? the ClickHandlers for the buttons don't respond, when there is already a ClickEvent that triggered for the entire screen.

If you re-direct the event from the View-global handleClickEvent() method, your buttons will have their handlers triggered.

void MyView::handleClickEvent(const ClickEvent& evt)
{
    ...
    View::handleClickEvent(evt);
}

/Martin

Sorry, i missed the non-functional code, haha =)

Here are a few things you can do with a button with label. Here i just added a button with label ("New Text") and wrote some code.

TypedText lblText = buttonWithLabel1.getLabelText();
TypedTextId id = lblText.getId();
Unicode::UnicodeChar buf[20];
Unicode::strncpy(buf, lblText.getText(), 20);

Here you have the TypedText, the ID of the typed text and the contents of the buffer (unicode values) associated with that ButtonWithLabel.

/Martin

gfxfloro
Senior

Thanks Martin, that did the trick!

Great! Let me know if there's anything else ^_^

/Martin