#include "TextTailManager.h" #include "src/eter_image/MarkManager.h" #include #include namespace ava2 { TextTailManager& TextTailManager::Instance() { static TextTailManager s_instance; return s_instance; } TextTailManager::TextTailManager() = default; void TextTailManager::Clear() { m_characterMap.clear(); m_itemMap.clear(); m_characterList.clear(); m_itemList.clear(); m_selectedItemVID = 0; } void TextTailManager::RegisterCharacterTextTail(uint32_t guildID, uint32_t vid, uint32_t color, float fAddHeight) { auto tail = std::make_shared(); tail->dwVirtualID = vid; tail->guildID = guildID; tail->color = color; tail->fHeight = fAddHeight; tail->isItem = false; tail->bNameFlag = true; // Check if character already has an entry auto it = m_characterMap.find(vid); if (it != m_characterMap.end()) { tail->name = it->second->name; tail->title = it->second->title; tail->level = it->second->level; tail->guildName = it->second->guildName; tail->worldX = it->second->worldX; tail->worldY = it->second->worldY; tail->worldZ = it->second->worldZ; tail->hasWorldPos = it->second->hasWorldPos; } m_characterMap[vid] = tail; } void TextTailManager::SetCharacterPosition(uint32_t vid, float worldX, float worldY, float worldZ) { auto it = m_characterMap.find(vid); if (it != m_characterMap.end()) { it->second->worldX = worldX; it->second->worldY = worldY; it->second->worldZ = worldZ; it->second->hasWorldPos = true; } } void TextTailManager::SetCharacterName(uint32_t vid, const std::string& name) { auto it = m_characterMap.find(vid); if (it != m_characterMap.end()) { it->second->name = name; } else { auto tail = std::make_shared(); tail->dwVirtualID = vid; tail->name = name; m_characterMap[vid] = tail; } } void TextTailManager::SetCharacterTextTailColor(uint32_t vid, uint32_t color) { auto it = m_characterMap.find(vid); if (it != m_characterMap.end()) { it->second->color = color; } } void TextTailManager::AttachTitle(uint32_t vid, const std::string& title, uint32_t color) { if (!m_pkTitleEnable) return; auto it = m_characterMap.find(vid); if (it != m_characterMap.end()) { it->second->title = title; it->second->titleColor = color; } } void TextTailManager::DetachTitle(uint32_t vid) { auto it = m_characterMap.find(vid); if (it != m_characterMap.end()) { it->second->title.clear(); } } void TextTailManager::AttachLevel(uint32_t vid, const std::string& levelText, uint32_t color) { if (!m_pkTitleEnable) return; auto it = m_characterMap.find(vid); if (it != m_characterMap.end()) { it->second->level = levelText; it->second->levelColor = color; } } void TextTailManager::AttachConquerorLevel(uint32_t vid, const std::string& levelText) { AttachLevel(vid, levelText, 0xFF8CC8FF); } void TextTailManager::DetachLevel(uint32_t vid) { auto it = m_characterMap.find(vid); if (it != m_characterMap.end()) { it->second->level.clear(); } } void TextTailManager::SetGuildName(uint32_t vid, const std::string& guildName, uint32_t guildID) { auto it = m_characterMap.find(vid); if (it != m_characterMap.end()) { it->second->guildName = guildName; if (guildID != 0) { it->second->guildID = guildID; } } } void TextTailManager::DeleteCharacterTextTail(uint32_t vid) { m_characterMap.erase(vid); m_characterList.erase(std::remove_if(m_characterList.begin(), m_characterList.end(), [vid](const std::shared_ptr& t) { return t && t->dwVirtualID == vid; }), m_characterList.end()); } void TextTailManager::ShowCharacterTextTail(uint32_t vid) { auto it = m_characterMap.find(vid); if (it == m_characterMap.end()) return; auto tail = it->second; if (!tail->isShowing) { // isShowing <=> in m_characterList tail->isShowing = true; m_characterList.push_back(tail); } } void TextTailManager::RegisterItemTextTail(uint32_t vid, const std::string& text, float worldX, float worldY, float worldZ, int ownerTime, const std::string& ownerName, bool isMine, uint32_t color) { auto tail = std::make_shared(); tail->dwVirtualID = vid; tail->name = text; tail->worldX = worldX; tail->worldY = worldY; tail->worldZ = worldZ; tail->hasWorldPos = true; tail->ownerTime = ownerTime; tail->ownerName = ownerName; tail->isMineItem = isMine; tail->color = color; tail->isItem = true; tail->fHeight = 12.0f; // item height above ground // Calculate initial 2D bounds for mouse picking and overlap resolution float textW = static_cast(text.size()) * 8.0f; bool hasOwner = !ownerName.empty() || ownerTime > 0; tail->xStart = -textW * 0.5f - 6.0f; tail->xEnd = textW * 0.5f + 6.0f; tail->yStart = hasOwner ? -28.0f : -14.0f; tail->yEnd = 6.0f; m_itemMap[vid] = tail; } void TextTailManager::SetItemPosition(uint32_t vid, float worldX, float worldY, float worldZ) { auto it = m_itemMap.find(vid); if (it != m_itemMap.end()) { it->second->worldX = worldX; it->second->worldY = worldY; it->second->worldZ = worldZ; it->second->hasWorldPos = true; } } void TextTailManager::SetItemTextTailOwner(uint32_t vid, const std::string& ownerName, bool isMine) { auto it = m_itemMap.find(vid); if (it != m_itemMap.end()) { it->second->ownerName = ownerName; it->second->isMineItem = isMine; if (!ownerName.empty()) { it->second->yStart = -28.0f; } } } void TextTailManager::DeleteItemTextTail(uint32_t vid) { m_itemMap.erase(vid); m_itemList.erase(std::remove_if(m_itemList.begin(), m_itemList.end(), [vid](const std::shared_ptr& t) { return t && t->dwVirtualID == vid; }), m_itemList.end()); if (m_selectedItemVID == vid) { m_selectedItemVID = 0; } } void TextTailManager::ShowItemTextTail(uint32_t vid) { auto it = m_itemMap.find(vid); if (it == m_itemMap.end()) return; auto tail = it->second; if (!tail->isShowing) { // isShowing <=> in m_itemList tail->isShowing = true; m_itemList.push_back(tail); } } void TextTailManager::RegisterChatTail(uint32_t vid, const std::string& chatText) { auto it = m_characterMap.find(vid); if (it != m_characterMap.end()) { it->second->chatText = chatText; it->second->chatTimer = 0.0f; it->second->hasChat = true; ShowCharacterTextTail(vid); } } void TextTailManager::RegisterInfoTail(uint32_t vid, const std::string& infoText) { auto it = m_characterMap.find(vid); if (it == m_characterMap.end()) { RegisterCharacterTextTail(0, vid, 0xFFFFAA33, 40.0f); SetCharacterName(vid, infoText); ShowCharacterTextTail(vid); } else { RegisterChatTail(vid, infoText); } } bool TextTailManager::IsChatTextTail(uint32_t vid) const { auto it = m_characterMap.find(vid); if (it != m_characterMap.end()) { return it->second->hasChat && it->second->chatTimer < it->second->chatMaxDuration; } return false; } void TextTailManager::ShowAllTextTail() { for (auto& [vid, tail] : m_characterMap) { if (!tail->isShowing) { // isShowing <=> in m_characterList tail->isShowing = true; m_characterList.push_back(tail); } } for (auto& [vid, tail] : m_itemMap) { if (!tail->isShowing) { // isShowing <=> in m_itemList tail->isShowing = true; m_itemList.push_back(tail); } } } void TextTailManager::HideAllTextTail() { for (auto& tail : m_characterList) { if (tail) tail->isShowing = false; } m_characterList.clear(); for (auto& tail : m_itemList) { if (tail) { tail->isShowing = false; tail->isSelected = false; } } m_itemList.clear(); } int TextTailManager::Pick(int ixMouse, int iyMouse,float padding) { float mx = static_cast(ixMouse); float my = static_cast(iyMouse); for (const auto& tail : m_itemList) { if (!tail) continue; float bx1 = tail->x + tail->xStart; float bx2 = tail->x + tail->xEnd; float by1 = tail->y + tail->yStart; float by2 = tail->y + tail->yEnd; if (mx >= bx1 && mx <= bx2 && my >= by1 && my <= by2) { SelectItemName(tail->dwVirtualID); return static_cast(tail->dwVirtualID); } } if(padding>0) { std::shared_ptr picked;float best=padding*padding; for(const auto& tail:m_itemList) { if(!tail)continue; const float dx=mx-std::clamp(mx,tail->x+tail->xStart,tail->x+tail->xEnd); const float dy=my-std::clamp(my,tail->y+tail->yStart,tail->y+tail->yEnd); const float distance=dx*dx+dy*dy; if(distancedwVirtualIDdwVirtualID))) {best=distance;picked=tail;} } if(picked){SelectItemName(picked->dwVirtualID);return int(picked->dwVirtualID);} } return -1; } void TextTailManager::SelectItemName(uint32_t vid) { m_selectedItemVID = vid; for (auto& tail : m_itemList) { if (tail && tail->dwVirtualID == vid) { tail->isSelected = true; } } } bool TextTailManager::GetTextTailPosition(uint32_t vid, float& x, float& y, float& z) const { auto it = m_characterMap.find(vid); if (it != m_characterMap.end()) { x = it->second->x; y = it->second->y; z = it->second->z; return true; } auto itItem = m_itemMap.find(vid); if (itItem != m_itemMap.end()) { x = itItem->second->x; y = itItem->second->y; z = itItem->second->z; return true; } return false; } const std::shared_ptr TextTailManager::GetCharacterTail(uint32_t vid) const { auto it = m_characterMap.find(vid); return it != m_characterMap.end() ? it->second : nullptr; } const std::shared_ptr TextTailManager::GetItemTail(uint32_t vid) const { auto it = m_itemMap.find(vid); return it != m_itemMap.end() ? it->second : nullptr; } void TextTailManager::Update(float dt) { if (dt <= 0.0f) return; for (auto& [vid, tail] : m_characterMap) { if (tail && tail->hasChat) { tail->chatTimer += dt; if (tail->chatTimer >= tail->chatMaxDuration) { tail->hasChat = false; tail->chatText.clear(); } } } for (auto& [vid, tail] : m_itemMap) { if (tail && tail->ownerTime > 0) { // Can tick countdown timer } } } void TextTailManager::UpdateAllTextTail(float playerX, float playerY, float playerZ, const Gr2SceneRenderer* sceneRenderer) { const auto* r = sceneRenderer ? sceneRenderer : m_currentSceneRenderer; for (auto& [vid, tail] : m_characterMap) { if (!tail) continue; if (tail->hasWorldPos) { float dx = tail->worldX - playerX; float dy = tail->worldY - playerY; float dz = tail->worldZ - playerZ; tail->fDistanceFromPlayer = std::sqrt(dx * dx + dy * dy + dz * dz); if (r) { float sx = 0.0f, sy = 0.0f; if (r->ProjectToScreen(tail->worldX, tail->worldY + tail->fHeight, tail->worldZ, sx, sy)) { tail->x = std::floor(sx); tail->y = std::floor(sy); tail->z = 0.0f; } } } } for (auto& [vid, tail] : m_itemMap) { if (!tail) continue; if (tail->hasWorldPos) { float dx = tail->worldX - playerX; float dy = tail->worldY - playerY; float dz = tail->worldZ - playerZ; tail->fDistanceFromPlayer = std::sqrt(dx * dx + dy * dy + dz * dz); if (r) { float sx = 0.0f, sy = 0.0f; if (r->ProjectToScreen(tail->worldX, tail->worldY + tail->fHeight, tail->worldZ, sx, sy)) { tail->x = std::floor(sx); tail->y = std::floor(sy); tail->z = 0.0f; } } } } } void TextTailManager::UpdateShowingTextTail(const Gr2SceneRenderer* sceneRenderer) { const auto* r = sceneRenderer ? sceneRenderer : m_currentSceneRenderer; if (!r) return; for (auto& tail : m_characterList) { if (!tail || !tail->hasWorldPos) continue; float sx = 0.0f, sy = 0.0f; if (r->ProjectToScreen(tail->worldX, tail->worldY + tail->fHeight, tail->worldZ, sx, sy)) { tail->x = std::floor(sx); tail->y = std::floor(sy); } } for (auto& tail : m_itemList) { if (!tail || !tail->hasWorldPos) continue; float sx = 0.0f, sy = 0.0f; if (r->ProjectToScreen(tail->worldX, tail->worldY + tail->fHeight, tail->worldZ, sx, sy)) { tail->x = std::floor(sx); tail->y = std::floor(sy); } } } bool TextTailManager::isIn(const TextTailItem& src, const TextTailItem& tgt) const { float x1Source = src.x + src.xStart; float y1Source = src.y + src.yStart; float x2Source = src.x + src.xEnd; float y2Source = src.y + src.yEnd; float x1Target = tgt.x + tgt.xStart; float y1Target = tgt.y + tgt.yStart; float x2Target = tgt.x + tgt.xEnd; float y2Target = tgt.y + tgt.yEnd; return (x1Source <= x2Target && x2Source >= x1Target && y1Source <= y2Target && y2Source >= y1Target); } void TextTailManager::ArrangeTextTail() { // 1. Arrange Item TextTails to avoid mutual overlap for (size_t i = 0; i < m_itemList.size(); ++i) { auto& pInsert = m_itemList[i]; if (!pInsert) continue; int limitCount = 0; for (size_t j = 0; j < m_itemList.size();) { if (i == j) { ++j; continue; } auto& pCompare = m_itemList[j]; if (!pCompare) { ++j; continue; } if (limitCount >= 20) break; if (isIn(*pInsert, *pCompare)) { pInsert->y = pCompare->y + pCompare->yEnd - pInsert->yStart + 4.0f; j = 0; limitCount++; continue; } ++j; } } // 2. Arrange Character TextTails (compute Level, Title, Guild bounding boxes) for (auto& tail : m_characterList) { if (!tail) continue; float nameWidth = TextWidth(tail->name); float titleWidth = tail->title.empty() ? 0.0f : (TextWidth(tail->title)); float levelWidth = tail->level.empty() ? 0.0f : (TextWidth(tail->level)); float totalW = nameWidth; if (!tail->title.empty()) totalW += titleWidth + 6.0f; if (!tail->level.empty()) totalW += levelWidth + 6.0f; tail->xStart = -totalW * 0.5f - 4.0f; tail->xEnd = totalW * 0.5f + 4.0f; tail->yStart = -16.0f; tail->yEnd = 4.0f; } } void TextTailManager::RenderTextTailBox(SDL_Renderer* renderer, const TextTailItem& tail) { float bx1 = tail.x + tail.xStart; float by1 = tail.y + tail.yStart; float w = tail.xEnd - tail.xStart; float h = tail.yEnd - tail.yStart; SDL_FRect rect{bx1, by1, w, h}; // Dark translucent background bar SDL_SetRenderDrawColor(renderer, 10, 10, 10, 190); SDL_RenderFillRect(renderer, &rect); // Border: Green if selected, golden/subtle grey otherwise if (tail.isSelected || tail.dwVirtualID == m_selectedItemVID) { SDL_SetRenderDrawColor(renderer, 50, 240, 80, 255); // Emerald Green highlight } else { SDL_SetRenderDrawColor(renderer, 45, 45, 45, 230); } SDL_RenderRect(renderer, &rect); } void TextTailManager::RenderText(SDL_Renderer* renderer, float x, float y, const std::string& text, uint32_t color, float scale, bool shadow) { if (text.empty()) return; if (m_textPainter) { m_textPainter(x,y,text,color,scale,shadow); return; } uint8_t a = (color >> 24) & 0xFF; if (a == 0) a = 0xFF; uint8_t r = (color >> 16) & 0xFF; uint8_t g = (color >> 8) & 0xFF; uint8_t b = color & 0xFF; SDL_SetRenderScale(renderer, scale, scale); if (shadow) { // Outline shadow SDL_SetRenderDrawColor(renderer, 10, 10, 10, a); SDL_RenderDebugText(renderer, (x + 1.0f) / scale, (y + 1.0f) / scale, text.c_str()); SDL_RenderDebugText(renderer, (x - 1.0f) / scale, (y) / scale, text.c_str()); SDL_RenderDebugText(renderer, (x) / scale, (y - 1.0f) / scale, text.c_str()); } // Main text SDL_SetRenderDrawColor(renderer, r, g, b, a); SDL_RenderDebugText(renderer, x / scale, y / scale, text.c_str()); SDL_SetRenderScale(renderer, 1.0f, 1.0f); } void TextTailManager::RenderChatBubble(SDL_Renderer* renderer, const TextTailItem& tail) { if (!tail.hasChat || tail.chatText.empty()) return; float textW = TextWidth(tail.chatText); float bubbleW = textW + 16.0f; float bubbleH = 22.0f; float bx = tail.x - bubbleW * 0.5f; float by = tail.y - 36.0f; // Speech bubble background (white/cream) SDL_FRect bgRect{bx, by, bubbleW, bubbleH}; SDL_SetRenderDrawColor(renderer, 245, 245, 235, 240); SDL_RenderFillRect(renderer, &bgRect); // Dark border SDL_SetRenderDrawColor(renderer, 40, 40, 40, 255); SDL_RenderRect(renderer, &bgRect); // Downward notch / pointer SDL_RenderLine(renderer, tail.x - 4.0f, by + bubbleH, tail.x, by + bubbleH + 6.0f); SDL_RenderLine(renderer, tail.x, by + bubbleH + 6.0f, tail.x + 4.0f, by + bubbleH); // Dark text inside speech bubble RenderText(renderer, bx + 8.0f, by + 4.0f, tail.chatText, 0xFF1A1A1A, 1.0f, false); } void TextTailManager::Render(SDL_Renderer* renderer, const Gr2SceneRenderer* sceneRenderer) { if (!renderer) { renderer = m_currentRenderer; } if (!renderer) return; if (sceneRenderer) { UpdateShowingTextTail(sceneRenderer); ArrangeTextTail(); } SDL_SetRenderDrawBlendMode(renderer, SDL_BLENDMODE_BLEND); // 1. Render Item TextTails for (const auto& tail : m_itemList) { if (!tail) continue; RenderTextTailBox(renderer, *tail); float textW = TextWidth(tail->name); float tx = tail->x - textW * 0.5f; float ty = tail->y - 10.0f; // Render item name uint32_t nameColor = tail->isSelected ? 0xFF50F050 : tail->color; RenderText(renderer, tx, ty, tail->name, nameColor, 1.0f, true); // If item has owner protection timer if (!tail->ownerName.empty() || tail->ownerTime > 0) { std::string ownerTag = tail->ownerName; if (tail->ownerTime > 0) { int m = tail->ownerTime / 60; int s = tail->ownerTime % 60; char buf[32]; std::snprintf(buf, sizeof(buf), "(%dm %ds) ", m, s); ownerTag = std::string(buf) + ownerTag; } float owW = TextWidth(ownerTag, 0.9f); uint32_t owColor = tail->isMineItem ? 0xFFFFD700 : 0xFFFF6060; RenderText(renderer, tail->x - owW * 0.5f, ty - 14.0f, ownerTag, owColor, 0.9f, true); } } // 2. Render Character TextTails for (const auto& tail : m_characterList) { if (!tail) continue; float nameW = TextWidth(tail->name); float titleW = tail->title.empty() ? 0.0f : (TextWidth(tail->title)); float levelW = tail->level.empty() ? 0.0f : (TextWidth(tail->level)); float totalW = nameW; if (!tail->title.empty()) totalW += titleW + 6.0f; if (!tail->level.empty()) totalW += levelW + 6.0f; // Original AVA2 keeps the name centered on the actor; level and title // extend to its left rather than moving the whole nameplate to the right. float curX = tail->x + 4.0f - nameW * 0.5f; if (!tail->title.empty()) curX -= titleW + 4.0f; if (!tail->level.empty()) curX -= levelW + 4.0f; float curY = tail->y - 14.0f; // Level tag e.g. "[Lv. 99]" in lime green if (!tail->level.empty()) { RenderText(renderer, curX, curY, tail->level, tail->levelColor, 1.0f, true); curX += levelW + 4.0f; } // Title e.g. "Noble" in gold if (!tail->title.empty()) { RenderText(renderer, curX, curY, tail->title, tail->titleColor, 1.0f, true); curX += titleW + 4.0f; } // Character Name in tail->color (white / red / yellow / etc.) RenderText(renderer, curX, curY, tail->name, tail->color, 1.0f, true); // Guild Name e.g. "[GuildName]" in light cyan above character name, with Guild Mark if (!tail->guildName.empty()) { std::string gStr = "[" + tail->guildName + "]"; float gW = TextWidth(gStr, 0.9f); float totalW = gW; bool hasMark = false; float u0 = 0.0f, v0 = 0.0f, u1 = 0.0f, v1 = 0.0f; uint32_t imgIdx = 0; if (tail->guildID != 0 && GuildMarkManager::Instance().GetMarkUV(tail->guildID, u0, v0, u1, v1, imgIdx)) { hasMark = true; totalW += 16.0f + 4.0f; // 16px mark + 4px spacing } float startX = tail->x - totalW * 0.5f; float guildY = curY - 14.0f; if (hasMark) { SDL_Texture* markTex = GuildMarkManager::Instance().GetSDLTexture(imgIdx, renderer); if (markTex) { SDL_FRect srcRect{ u0 * 512.0f, v0 * 512.0f, (u1 - u0) * 512.0f, (v1 - v0) * 512.0f }; SDL_FRect dstRect{ startX, guildY - 2.0f, 16.0f, 12.0f }; SDL_SetTextureBlendMode(markTex, SDL_BLENDMODE_BLEND); SDL_RenderTexture(renderer, markTex, &srcRect, &dstRect); } startX += 16.0f + 4.0f; } RenderText(renderer, startX, guildY, gStr, 0xFF80D0FF, 0.9f, true); } // Chat speech bubble if (tail->hasChat) { RenderChatBubble(renderer, *tail); } } } } // namespace ava2