10 posts / 0 new
Last post
hyui
hyui's picture
Offline
Joined: 19 Oct 2011
Posts:
NativeUI Label touch event

Hey,

I'm trying to get touch events on (NativeUI) Labels to work.
So main question: is this possible? I looked at the RockPaperScissorsGameNativeUI example which uses touch events but the example uses MAWidgetHandles instead of Labels.

Specific problem: I really need a Calendar widget and I used the unofficial calendar widget that I found in the cookbook here, which worked fine for MAUI. However, I now use NativeUI instead so I'm trying to rewrite this widget to NativeUI instead.

So I have my Calendar.h:

class Calendar : public CustomEventListener {
. . .
	void customEvent(const MAEvent& event);
	void widgetClicked(MAHandle widgetHandle);
. . . }

And my Calendar.cpp (customEvent does not catch touch events for some reason):

Calendar::Calendar(int x, int y, int width, int height)
  : mX(x), mY(y), mWidth(width), mHeight(height) {
. . .
  Environment::getEnvironment().addCustomEventListener(this);
. . . }

void Calendar::customEvent(const MAEvent& event)
{
	//Trying to catch all events for debugging...
	// If the event does not come from a widget, we just ignore it.
	//if(event.type != EVENT_TYPE_WIDGET)
	//	return;

	// Get the information sent by the widget.
	MAWidgetEventData* widgetEventData = (MAWidgetEventData*) event.data;

	//DEBUG
	char buf[2];
	sprintf(buf, "%i", widgetEventData->eventType);
	//This only triggers event "0" on startup and nothing on touch events
	maAlert("Event", buf, "ok", "0", "0");

	// Check that the event was a click (touch) event.
	if (widgetEventData->eventType == MAW_EVENT_CLICKED)
	{
		// Handle the event emitted by the widget
		widgetClicked(widgetEventData->widgetHandle);
	}
}

void Calendar::widgetClicked(MAHandle widgetHandle)
{
	Vector_each(HorizontalLayout*, itr, mWeeks)
	{
		HorizontalLayout *week = *itr;
		for(int i=0; i<7; i++)
		{
			Label *day = (Label*) week->getChild(i);
			MAHandle handle = day->getWidgetHandle();

			if(widgetHandle == handle)
			{
				//DEBUG
				maAlert("Event", "Hit!", "ok", "0", "0");
				//selectedDate = addDays(selectedDate, i - selectedIndex);
				//formatMonth();
				//informListener();
			}
		}
	}
}

I attach my Calendar code here if anyone is interested in trying to get this to work (the only thing that works so far is the initial rendering of the Calendar). To use it you can add to layout using:

Calendar *cal = new Calendar(...);
yourMainLayout.addChild(cal->getMainLayout());
AttachmentSize
NativeUICalendar.zip4.8 KB

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
Sam Pickard
rival's picture
Offline
Mobile Archmage
Joined: 19 Mar 2009
Posts:

Hi. There is a native ui equivalent of Calendar which is DatePicker. I would recommend instead of converting the calendar widget, use DatePicker instead.

maCreateWidget(MAW_DATE_PICKER) or use the class in NativeUI/DatePicker.h

hyui
hyui's picture
Offline
Joined: 19 Oct 2011
Posts:

Hi,

Thanks for you reply.

I don't want to use DatePicker because it works very differently. I need to have the visualization of the month that your Calendar provides (it looks great!). I want a user to be able to add information to each date (and visualize these edits in the calendar). Also I may need to add so a user can selects several days at ones and add information to all of them and update the visualization accordingly. So I'm heavily dependent on this graphical Calendar.

Sam Pickard
rival's picture
Offline
Mobile Archmage
Joined: 19 Mar 2009
Posts:

OK, I'll look at the native version problem. However, the reason that Android doesn't support this natively is that it will be extremely difficult to use. Small buttons are very difficult for users on touch screen as it is very difficult to judge exactly where to touch, and each day will be a button much smaller than the fingertip of the user.

hyui
hyui's picture
Offline
Joined: 19 Oct 2011
Posts:

You may be right, this can maybe be a bit frustrating to click but I don't see any other good solution for what I want.

I wonder why customEvent seems to work on MAWidgetHandle-objects (as shown in RockPaperScissorNativeUI example) but not on Labels (which owns a MAWidgetHandle)? It would be such a hassle to rewrite Calendar to use MAWidgetHandle instead of Labels... I hope I just missed something in how the customEvent's work but I can't seem to find any other differences from the example to my code except the use of MAWidgetHandle-objects.

Edit: I looked around a bit more, maybe I can use ImageButton-objects instead of Label. I checked the rockpaperscissor example more closely and I saw that the clickable objects there are ImageButtons. I'm gonna see if I can use this instead and I'll report back tomorrow if I get it to work.

raphpl
raphpl's picture
Offline
Joined: 21 Jun 2011
Posts:

You know Labels have getWidgetHandle() method which returns MAWidgetHandle object associated with this Label. So I found out that You can use:

if(MAW_EVENT_CLICKED == widgetEvent->eventType && myLabel->getWidgetHandle() == widgetEvent->widgetHandle){
    //process label click event
} 

I hope that helps.

 

 

hyui
hyui's picture
Offline
Joined: 19 Oct 2011
Posts:
raphpl wrote:

You know Labels have getWidgetHandle() method which returns MAWidgetHandle object associated with this Label. So I found out that You can use:

if(MAW_EVENT_CLICKED == widgetEvent->eventType && myLabel->getWidgetHandle() == widgetEvent->widgetHandle){
    //process label click event
} 

I hope that helps.

 

 

Hi,

I can't use that because the problem is that Label's does not receive the customEvents at all. I tried using ImageButtons today and they received the customEvent fine! However, they can't have text on them on Android. One solution here is to have 31 images with numbers 1-31 on them but I'm not sure I like that idea. I'm gonna see if I can find something else.

Sam Pickard
rival's picture
Offline
Mobile Archmage
Joined: 19 Mar 2009
Posts:

No, the screen receives the event. If you replace all the calendar labels with native labels, the screen will capture the event. You can then get the caption of the label to find the selected day.

hyui
hyui's picture
Offline
Joined: 19 Oct 2011
Posts:
rival wrote:

No, the screen receives the event. If you replace all the calendar labels with native labels, the screen will capture the event. You can then get the caption of the label to find the selected day.

Hi again,

I don't understand what you're saying here. Is there any NativeUI example code somewhere I can check? I can only get Button's and ImageButton's to trigger touch events. It feels like I've tried everything so far, haven't posted all my tries here but now I don't know what else to try.

Sam Pickard
rival's picture
Offline
Mobile Archmage
Joined: 19 Mar 2009
Posts:

Yeah, sorry, my mistake. A button is just a label with a touch event. I meant to type button. If you replace all the calendar labels with native buttons (see the NativeUI example code supplied with the MoSync download) and your screen will catch the event.