Skip to main content
MrJob
Associate II
Feb 1, 2019 3:37 PM UTC
Question

How to use drawString

I don't really understand how to use drawString. The text explaining the function is not very clear in my opinion. And most importantly, it wont draw the string I want it to draw. I'm probably doing something wrong, but I don't know what.

This topic has been closed for replies.

2 replies

Martin KJELDSEN
Principal III
Feb 2, 2019 1:24 PM UTC

Hi @Community member​,

Is the same issue you're having in the other post with the ButtonWithIconAndLabel? Can you post your code?

Thanks

MrJob
MrJobAuthor
Associate II
Feb 4, 2019 8:46 AM UTC

Yes, it is the same issue.

Here is my own code, based on yours.

In my class:

	void PhysicalButton::draw(const Rect& area)
	{
		/* Draw the icons of the button */
		Button::draw(area);
 
		/* Draw the label */
		const Font* fontToDraw = _label_txt.getFont();
		const Unicode::UnicodeChar * string = _label_txt.getText();
		uint8_t textHeightIncludingSpacing = fontToDraw->getMaxTextHeight(string) * fontToDraw->getNumberOfLines(string) + fontToDraw->getSpacingAbove(string);
 
		if ((fontToDraw != NULL) && _label_txt.hasValidId())
		{
			Rect widgetArea;
			widgetArea = Rect(getX(), getY(), this->getWidth(), this->getHeight());
 
			Rect invalidatedArea = Rect(0, this->getHeight() - textHeightIncludingSpacing - 3, this->getWidth(), textHeightIncludingSpacing);
 
			translateRectToAbsolute(widgetArea);
			LCD::StringVisuals visuals(
				fontToDraw,
				_label_color,
				alpha,
				_label_txt.getAlignment(),
				0,
				TEXT_ROTATE_0,
				_label_txt.getTextDirection(),
				0
			);
 
			HAL::lcd().drawString(
				widgetArea,
				invalidatedArea,
				visuals,
				_label_txt.getText()
			);
		}
	}
 
	void PhysicalButton::Setup(
		bool visible,
		bool faded,
		const uint16_t icon_id, 
		const uint16_t title_id)
	{
		int16_t bgX = 0, bgY = 0;
		Bitmap bg, icon;
 
		int16_t iconX = 0, iconY = 0;
//		_label_txt = TypedText(title_id);
 
		/* The mask determines where the button is being placed*/
		switch (_mask)
		{
		case Mmi::Button::Bit::Masks::Hot	:	/* TOP-LEFT */
			bg = Bitmap(BITMAP_BUTTONBACKGROUND1_ID);
			bgY = BG_TOP_Y;
			break;
 
		case Mmi::Button::Bit::Masks::Cold	:	/* TOP-RIGHT */
			bg = Bitmap(BITMAP_BUTTONBACKGROUND4_ID);
			bgY = BG_TOP_Y;
			break;
 
		case Mmi::Button::Bit::Masks::Mix	:	/* MID-LEFT */
			bg = Bitmap(BITMAP_BUTTONBACKGROUND2_ID);
			bgY = BG_MID_Y;
			break;
 
		case Mmi::Button::Bit::Masks::Jug	:	/* MID-RIGHT */
			bg = Bitmap(BITMAP_BUTTONBACKGROUND5_ID);
			bgY = BG_MID_Y;
			break;
 
		case Mmi::Button::Bit::Masks::Back	:	/* BOTTOM-LEFT */
		case Mmi::Button::Bit::Masks::ExHot :
			bg = Bitmap(BITMAP_BUTTONBACKGROUND3_ID);
			bgY = BG_BOT_Y;
			break;
 
		case Mmi::Button::Bit::Masks::Menu :	/* BOTTOM-RIGHT */
			bg = Bitmap(BITMAP_BUTTONBACKGROUND6_ID);
			bgY = BG_BOT_Y;
			break;
		}
 
		/* Set the X pos of the background */
		if (_mask & (Mmi::Button::Bit::Masks::Cold | Mmi::Button::Bit::Masks::Jug | Mmi::Button::Bit::Masks::Menu))
		{
			bgX = 128;
		}
		else
		{
			bgX = 112 - bg.getWidth();
		}
 
		/* Only if the icon_id is valid load the correct icon into the icon bitmap */
		if (icon_id != 0xffff)
		{
			icon = Bitmap(icon_id);
			/* Set the X pos of the Icon */
			if (_mask & (Mmi::Button::Bit::Masks::Cold | Mmi::Button::Bit::Masks::Jug | Mmi::Button::Bit::Masks::Menu))
			{
				iconX = ((3 * bg.getWidth()) / 5) - (icon.getWidth() >> 1);
			}
			else 
			{
				iconX = bg.getWidth() - ((3 * bg.getWidth()) / 5) - (icon.getWidth() >> 1);
			}
 
			/* Y-position always the same */
			iconY = ICON_POS_Y;
 
		}
		/* Otherwise load an empty image */
		else
		{
			icon = Bitmap(BITMAP_IMG_EMPTY_ID);
		}
 
		/* Set all the bitmaps */
		setBitmaps(bg, bg, icon, icon);
 
		/* Set the XY coordinates relative to the bg */
		setIconXY(iconX, iconY);
 
		/* Set the XY coöordinates */
		setXY(bgX, bgY);
 
		/* Set visible */
		setVisible(visible);
 
		/* Set faded */
		if (faded)
		{
			setAlpha(64);
		}
		else
		{
			setAlpha(255);
		}
 
		draw(getAbsoluteRect());
}

In another c-file:

	hotBtn.Setup(true, !waterContainerDisplay_Hot.GetWaterContainer()->IsActive(),
		waterContainerDisplay_Hot.GetWaterContainer()->GetIconID(),
		waterContainerDisplay_Hot.GetWaterContainer()->GetName() );

Do you have enough code like this?

Also, when I use your code, I cannot print the string any lower than the icon of the button.

Thank you for your help!