static int l_font_draw_tooltip(lua_State *L) { THFont* pFont = luaT_testuserdata<THFont>(L); THRenderTarget* pCanvas = luaT_testuserdata<THRenderTarget>(L, 2); size_t iMsgLen; const char* sMsg = luaT_checkstring(L, 3, &iMsgLen); int iX = static_cast<int>(luaL_checkinteger(L, 4)); int iY = static_cast<int>(luaL_checkinteger(L, 5)); int iScreenWidth = pCanvas->getWidth(); int iW = 200; // (for now) hardcoded width of tooltips uint32_t iBlack = pCanvas->mapColour(0x00, 0x00, 0x00); uint32_t iWhite = pCanvas->mapColour(0xFF, 0xFF, 0xFF); THFontDrawArea oArea = pFont->drawTextWrapped(nullptr, sMsg, iMsgLen, iX + 2, iY + 1, iW - 4, INT_MAX, 0); int iLastX = iX + oArea.iWidth + 3; int iFirstY = iY - (oArea.iEndY - iY) - 1; int iXOffset = iLastX > iScreenWidth ? iScreenWidth - iLastX : 0; int iYOffset = iFirstY < 0 ? -iFirstY : 0; pCanvas->fillRect(iBlack, iX + iXOffset, iFirstY + iYOffset, oArea.iWidth + 3, oArea.iEndY - iY + 2); pCanvas->fillRect(iWhite, iX + iXOffset + 1, iFirstY + 1 + iYOffset, oArea.iWidth + 1, oArea.iEndY - iY); pFont->drawTextWrapped(pCanvas, sMsg, iMsgLen, iX + 2 + iXOffset, iFirstY + 1 + iYOffset, iW - 4); lua_pushinteger(L, oArea.iEndY); return 1; }