diff --git a/Minecraft.Client/UI/Screens/AbstractContainerScreen.cpp b/Minecraft.Client/UI/Screens/AbstractContainerScreen.cpp index a1723776e..b437694a4 100644 --- a/Minecraft.Client/UI/Screens/AbstractContainerScreen.cpp +++ b/Minecraft.Client/UI/Screens/AbstractContainerScreen.cpp @@ -95,138 +95,11 @@ void AbstractContainerScreen::render(int xm, int ym, float a) { hoveredSlot->hasItem()) { std::shared_ptr item = hoveredSlot->getItem(); - // std::wstring elementName = - // trimString(Language::getInstance()->getElementName(hoveredSlot->getItem()->getDescriptionId())); - std::vector elementName; - std::vector* tooltipLines = - item->getHoverText(minecraft->player, false, elementName); + int xo = (width - imageWidth) / 2; + int yo = (height - imageHeight) / 2; - if (tooltipLines != NULL && tooltipLines->size() > 0) { - int tooltipWidth = 0; - std::vector cleanedLines; - std::vector lineColors; - - for (int lineIndex = 0; lineIndex < (int)tooltipLines->size(); - ++lineIndex) { - std::wstring rawLine = (*tooltipLines)[lineIndex]; - std::wstring clean = L""; - int lineColor = 0xffffffff; - - // 4jcraft: LCE is using HTML font elements for its tooltip - // colors, so make sure to parse them for parity w iggy UI - // - // examples would be enchantment books, potions and music - // discs - size_t fontPos = rawLine.find(L"') { - inTag = false; - } else if (!inTag) { - clean += currentChar; - } - } - - cleanedLines.push_back(clean); - lineColors.push_back(lineColor); - - int lineWidth = font->width(clean); - if (lineWidth > tooltipWidth) { - tooltipWidth = lineWidth; - } - } - - int tooltipX = xm - xo + 12; - int tooltipY = ym - yo - 12; - - int tooltipHeight = 8; - - if (tooltipLines->size() > 1) { - tooltipHeight += 2 + (tooltipLines->size() - 1) * 10; - } - - int bgColor = 0xf0100010; - fillGradient(tooltipX - 3, tooltipY - 4, - tooltipX + tooltipWidth + 3, tooltipY - 3, bgColor, - bgColor); - fillGradient(tooltipX - 3, tooltipY + tooltipHeight + 3, - tooltipX + tooltipWidth + 3, - tooltipY + tooltipHeight + 4, bgColor, bgColor); - fillGradient(tooltipX - 3, tooltipY - 3, - tooltipX + tooltipWidth + 3, - tooltipY + tooltipHeight + 3, bgColor, bgColor); - fillGradient(tooltipX - 4, tooltipY - 3, tooltipX - 3, - tooltipY + tooltipHeight + 3, bgColor, bgColor); - fillGradient(tooltipX + tooltipWidth + 3, tooltipY - 3, - tooltipX + tooltipWidth + 4, - tooltipY + tooltipHeight + 3, bgColor, bgColor); - - int borderStart = 0x505000ff; - int borderFinish = - (borderStart & 0xfefefe) >> 1 | borderStart & 0xff000000; - fillGradient(tooltipX - 3, (tooltipY - 3) + 1, (tooltipX - 3) + 1, - (tooltipY + tooltipHeight + 3) - 1, borderStart, - borderFinish); - fillGradient(tooltipX + tooltipWidth + 2, (tooltipY - 3) + 1, - tooltipX + tooltipWidth + 3, - (tooltipY + tooltipHeight + 3) - 1, borderStart, - borderFinish); - fillGradient(tooltipX - 3, tooltipY - 3, - tooltipX + tooltipWidth + 3, (tooltipY - 3) + 1, - borderStart, borderStart); - fillGradient(tooltipX - 3, tooltipY + tooltipHeight + 2, - tooltipX + tooltipWidth + 3, - tooltipY + tooltipHeight + 3, borderFinish, - borderFinish); - - int currentY = tooltipY; - for (int lineIndex = 0; lineIndex < (int)tooltipLines->size(); - ++lineIndex) { - std::wstring& currentLine = cleanedLines[lineIndex]; - int textColor; - - if (lineIndex == 0) { - textColor = app.GetHTMLColour(item->getRarity()->color); - } else { - textColor = (lineColors[lineIndex] != 0xffffffff) - ? lineColors[lineIndex] - : 0xffaaaaaa; - } - - font->drawShadow(currentLine, tooltipX, currentY, textColor); - - if (lineIndex == 0) { - currentY += 2; - } - - currentY += 10; - } - } + // 4jcraft: abstracted tooltip rendering into a new method + renderTooltip(item, xm - xo, ym - yo); } glPopMatrix(); @@ -237,6 +110,139 @@ void AbstractContainerScreen::render(int xm, int ym, float a) { #endif } +// 4jcraft: extracted from render() into a standalone method so this can be used +// in other derived classes +void AbstractContainerScreen::renderTooltip(std::shared_ptr item, + int x, int y) { + if (item == nullptr) return; + + std::vector elementName; + std::vector* tooltipLines = + item->getHoverText(minecraft->player, false, elementName); + + if (tooltipLines != NULL && tooltipLines->size() > 0) { + int tooltipWidth = 0; + std::vector cleanedLines; + std::vector lineColors; + + for (int lineIndex = 0; lineIndex < (int)tooltipLines->size(); + ++lineIndex) { + std::wstring rawLine = (*tooltipLines)[lineIndex]; + std::wstring clean = L""; + int lineColor = 0xffffffff; + + // 4jcraft: LCE is using HTML font elements for its tooltip + // colors, so make sure to parse them for parity w iggy UI + // + // examples would be enchantment books, potions and music + // discs + size_t fontPos = rawLine.find(L"') { + inTag = false; + } else if (!inTag) { + clean += currentChar; + } + } + + cleanedLines.push_back(clean); + lineColors.push_back(lineColor); + + int lineWidth = font->width(clean); + if (lineWidth > tooltipWidth) { + tooltipWidth = lineWidth; + } + } + + int tooltipX = x + 12; + int tooltipY = y - 12; + int tooltipHeight = 8; + + if (tooltipLines->size() > 1) { + tooltipHeight += 2 + (tooltipLines->size() - 1) * 10; + } + + int bgColor = 0xf0100010; + fillGradient(tooltipX - 3, tooltipY - 4, tooltipX + tooltipWidth + 3, + tooltipY - 3, bgColor, bgColor); + fillGradient(tooltipX - 3, tooltipY + tooltipHeight + 3, + tooltipX + tooltipWidth + 3, tooltipY + tooltipHeight + 4, + bgColor, bgColor); + fillGradient(tooltipX - 3, tooltipY - 3, tooltipX + tooltipWidth + 3, + tooltipY + tooltipHeight + 3, bgColor, bgColor); + fillGradient(tooltipX - 4, tooltipY - 3, tooltipX - 3, + tooltipY + tooltipHeight + 3, bgColor, bgColor); + fillGradient(tooltipX + tooltipWidth + 3, tooltipY - 3, + tooltipX + tooltipWidth + 4, tooltipY + tooltipHeight + 3, + bgColor, bgColor); + + int borderStart = 0x505000ff; + int borderFinish = + (borderStart & 0xfefefe) >> 1 | borderStart & 0xff000000; + fillGradient(tooltipX - 3, (tooltipY - 3) + 1, (tooltipX - 3) + 1, + (tooltipY + tooltipHeight + 3) - 1, borderStart, + borderFinish); + fillGradient(tooltipX + tooltipWidth + 2, (tooltipY - 3) + 1, + tooltipX + tooltipWidth + 3, + (tooltipY + tooltipHeight + 3) - 1, borderStart, + borderFinish); + fillGradient(tooltipX - 3, tooltipY - 3, tooltipX + tooltipWidth + 3, + (tooltipY - 3) + 1, borderStart, borderStart); + fillGradient(tooltipX - 3, tooltipY + tooltipHeight + 2, + tooltipX + tooltipWidth + 3, tooltipY + tooltipHeight + 3, + borderFinish, borderFinish); + + int currentY = tooltipY; + for (int lineIndex = 0; lineIndex < (int)tooltipLines->size(); + ++lineIndex) { + std::wstring& currentLine = cleanedLines[lineIndex]; + int textColor; + + if (lineIndex == 0) { + textColor = app.GetHTMLColour(item->getRarity()->color); + } else { + textColor = (lineColors[lineIndex] != 0xffffffff) + ? lineColors[lineIndex] + : 0xffaaaaaa; + } + + font->drawShadow(currentLine, tooltipX, currentY, textColor); + + if (lineIndex == 0) { + currentY += 2; + } + + currentY += 10; + } + } +} + void AbstractContainerScreen::renderLabels() {} void AbstractContainerScreen::renderSlot(Slot* slot) { diff --git a/Minecraft.Client/UI/Screens/AbstractContainerScreen.h b/Minecraft.Client/UI/Screens/AbstractContainerScreen.h index 74f14cc5d..af1654fbf 100644 --- a/Minecraft.Client/UI/Screens/AbstractContainerScreen.h +++ b/Minecraft.Client/UI/Screens/AbstractContainerScreen.h @@ -29,6 +29,10 @@ protected: // handling. virtual Slot* findSlot(int x, int y); virtual bool isHovering(Slot* slot, int xm, int ym); + // 4jcraft: extracted from render() into a standalone method so this can be + // used in other classes + virtual void renderTooltip(std::shared_ptr item, int x, + int y); private: virtual void renderSlot(Slot* slot);