cancel
Showing results for 
Search instead for 
Did you mean: 

How does the scrollwheel item update method work? The itemIndex is different from the currently selected item.

Loppi
Associate III

I use the scrollWheel widget for a menu structure. To set it's content I have used the method discribed in the STM32 Graphics: Getting Started with Scroll List Widget tutorial. The scrollWhel consists of 13 items and I use a dedicated template for the selected item. The automatically generated updateItemCallbackHandler hands itemIndex to scrollWheel1UpdateCenterItem function which I use to set the text for the currrently selected item.

void measureMenuView::scrollWheelMoveDwn(void)
{
	int16_t scrollWheelSelectedItem = scrollWheel1.getSelectedItem();
	scrollWheelSelectedItem++;
	if (scrollWheelSelectedItem > scrollWheel1.getNumberOfItems())
		scrollWheelSelectedItem = scrollWheel1.getNumberOfItems();
	scrollWheel1.animateToItem(scrollWheelSelectedItem);
	scrollWheel1.invalidate();
}
 
 
void measureMenuView::scrollWheel1UpdateCenterItem(secondaryMenuSelected& item, int16_t itemIndex)
{
	item.setTextSelected(itemIndex, scrollWheel1.getNumberOfItems(), 0);
}
 
 
void secondaryMenuSelected::setTextSelected(int itemNo, int numberOfItems,
		int screenNo)
{
	switch (itemNo)// % numberOfItems)
	{
	case 0:
		textSelected1.setTypedText(TypedText(T_OPTION1));
		break;
	case 1:
		textSelected1.setTypedText(TypedText(T_OPTION2));
		break;
	case 2:
		textSelected1.setTypedText(TypedText(T_OPTION3));
		break;
	case 3:
		textSelected1.setTypedText(TypedText(T_OPTION4));
		break;
	case 4:
		textSelected1.setTypedText(TypedText(T_OPTION5));
		break;
	case 5:
		textSelected1.setTypedText(TypedText(T_OPTION6));
		break;
	case 6:
		textSelected1.setTypedText(
				TypedText(T_OPTION7));
		break;
	case 7:
		textSelected1.setTypedText(TypedText(T_OPTION8));
		break;
	case 8:
		textSelected1.setTypedText(TypedText(T_OPTION9));
		break;
	case 9:
		textSelected1.setTypedText(TypedText(T_OPTION10));
		break;
	case 10:
		textSelected1.setTypedText(TypedText(T_OPTION11));
		break;
	case 11:
		textSelected1.setTypedText(TypedText(T_OPTION12));
		break;
	case 12:
		textSelected1.setTypedText(TypedText(T_OPTION13));
		break;
	}
}

It works as intended if I am just aiming to show the correct text but I would like to use the text width of the currently selected item to decide whether I have to scroll the text if it's wide or not. And this does not work because the text width is never (except for 0) the correct width currently selected item. This issue reflects in the textID of textSelected1 which is always one ahead of the currently selected item text.

textWidth = textSelected1.getTextWidth();

Why is that and why does scrollWheel1UpdateCenterItem function gets called multiple times with just one button press.

4 REPLIES 4
Loppi
Associate III

@Martin KJELDSEN​ Could you have a look at my problem please? I already asked several other questions without getting any answers so I am a bit worried nobody can help me on this as well.

MM..1
Chief II

I mean you need get or set in item class , i dont understand textSelected1

My example

item.markit(0);
item.setLabel(touchgfx::TypedText(UI_Menu_Items_D[cursorpos]));
void MenuItem::markit(bool sel)
{
	if(sel) {
		buttonWithLabel1.setLabelColor(touchgfx::Color::getColorFrom24BitRGB(255, 110, 0));
	}
	else {
		buttonWithLabel1.setLabelColor(touchgfx::Color::getColorFrom24BitRGB(255, 255, 255));
	}
	buttonWithLabel1.invalidate();
}
 
void MenuItem::setLabel(TypedText t)
{
	buttonWithLabel1.setLabelText(t);
}

 too width maybe you can get only after resize to current or invalidate ...

Loppi
Associate III

HI @MM..1​, textSelected1 is just a textArea which holds the current option name. The texts get set correctly and I can scroll the option wheel. The issue lies within the scrollWheel1UpdateCenterItem functions and it's callbackhandler. I found while debugging that the itemIndex, except for 0, is already wrong coming from the updateItemCallbackHandler. In my example I got the cases 0 to 12. The itemIndex starts correctly at 0 but then jumps to 2 on the next item in the scrollWheel.

Resize to current text or invalidate sadly does not help as the index is always one ahead of the actual selected item. This is what I have tried.

void secondaryMenuSelected::setTextSelected(int itemNo, int numberOfItems,
		int screenNo)
{
	switch (itemNo % numberOfItems)
	{
	case 0:
		textSelected1.setTypedText(TypedText(T_OPTION1));
		break;
	case 1:
		textSelected1.setTypedText(TypedText(T_OPTION2));
		break;
	case 2:
		textSelected1.setTypedText(TypedText(T_OPTION3));
		break;
	case 3:
		textSelected1.setTypedText(TypedText(T_OPTION4));
		break;
	case 4:
		textSelected1.setTypedText(TypedText(T_OPTION5));
		break;
	case 5:
		textSelected1.setTypedText(TypedText(T_OPTION6));
		break;
	case 6:
		textSelected1.setTypedText(
				TypedText(T_OPTION7));
		break;
	case 7:
		textSelected1.setTypedText(TypedText(T_OPTION8));
		break;
	case 8:
		textSelected1.setTypedText(TypedText(T_OPTION9));
		break;
	case 9:
		textSelected1.setTypedText(TypedText(T_OPTION10));
		break;
	case 10:
		textSelected1.setTypedText(TypedText(T_OPTION11));
		break;
	case 11:
		textSelected1.setTypedText(TypedText(T_OPTION12));
		break;
	case 12:
		textSelected1.setTypedText(TypedText(T_OPTION13));
		break;
	}
	textSelected1.resizeToCurrentText();
	textSelected1.invalidate();
	scrollEnd = false;
 
	textWidth = textSelected1.getTextWidth();
	containerWidth = scrollableContainer1.getWidth();
	if(textWidth); //for tests
	scrollableContainer1.reset();
	scrollableContainer1.invalidateContent();
}

Loppi
Associate III

I have found a rather unsatisfying workaround for getting the correct width. But this solution depends on the correct order ( 1,2,3,4,..12) of the text IDs. In the above showed function setTextSelected I have added another instruction where I subract 1 from the itemIndex to find the actual width of the text. The statement on the else is from the .getWidth() function.

	if (itemNo == 0)
		textWidth = textSelected1.getTextWidth();
	else
	{
		textWidth = TypedText(itemNo - 1).hasValidId() ? TypedText(itemNo - 1).getFont()->getStringWidth(TypedText(itemNo - 1).getTextDirection(), TypedText(itemNo - 1).getText()) : 0;
	}

For the text scrolling I wanted to use an interaction which triggers every N ticks and calls a virtual function. This function then decides based on textWidth and containerWidth whether to scroll the text or not. But scrollableContainer1.getScrolledX() and textSelected1.getTextWidth() get varying results there as well.