From 406bbe613208cbc2be8ca2aa648bb6266911441f Mon Sep 17 00:00:00 2001 From: devZoGok Date: Fri, 7 Aug 2026 10:30:44 +0300 Subject: [PATCH] tooltips --- concreteGuiManager.cpp | 46 ++++++++++++++++++++++++++- concreteGuiManager.h | 3 ++ singlePlayerButton.cpp | 21 +++++++------ singlePlayerButton.h | 8 +++-- tooltip.cpp | 71 ++++++++++++++++++++++++++++++++++++++++-- tooltip.h | 26 ++++++++++++++-- 6 files changed, 158 insertions(+), 17 deletions(-) diff --git a/concreteGuiManager.cpp b/concreteGuiManager.cpp index 31be54f..f6affcf 100644 --- a/concreteGuiManager.cpp +++ b/concreteGuiManager.cpp @@ -1,4 +1,3 @@ -#include #include #include @@ -60,6 +59,42 @@ namespace battleship{ return concreteGuiManager; } + Tooltip* ConcreteGuiManager::parseTooltip(sol::table &tooltipTbl, Vector3 guiPos){ + sol::table offsetTbl = tooltipTbl["offset"], sizeTbl = tooltipTbl["size"]; + Vector3 offset = Vector3(offsetTbl["x"], offsetTbl["y"], .1); + Vector2 size = Vector2(sizeTbl["x"], sizeTbl["y"]); + + sol::table lineDataTbl = tooltipTbl["lines"]; + int numLines = lineDataTbl.size(); + vector lines; + + for(int i = 0; i < numLines; i++){ + vector> iconsData; + sol::optional iconsTblOpt = lineDataTbl[i + 1]["icons"]; + + if(iconsTblOpt != sol::nullopt){ + sol::table iconsTbl = lineDataTbl[i + 1]["icons"]; + int numIcons = iconsTbl.size(); + + for(int j = 0; j < numIcons; j++) + iconsData.push_back(make_pair(iconsTbl[j + 1]["charId"], iconsTbl[j + 1]["path"])); + } + + sol::table entryTbl = lineDataTbl[i + 1]["entry"]; + wstring entry = (wstring)entryTbl["text"]; + Vector4 color = Vector4::VEC_IJKL; + sol::optional colorTblOpt = entryTbl["color"]; + + if(colorTblOpt != sol::nullopt) + color = Vector4(entryTbl["color"]["r"], entryTbl["color"]["g"], entryTbl["color"]["b"], entryTbl["color"]["a"]); + + lines.push_back(Tooltip::LineData(entry, color, iconsData)); + } + + string fontPath = GameManager::getSingleton()->getPath() + "Fonts/batang.ttf"; + return new Tooltip(guiPos + offset, size, lines, fontPath); + } + //TODO refactor player difficulty and faction listbox selection //TODO remove hardcoded font path values //TODO use configurable map path values @@ -91,12 +126,21 @@ namespace battleship{ bool texturingEnabled = true; } + Tooltip *tooltip = nullptr; + sol::optional tooltipTblOpt = guiTable["tooltip"]; + + if(tooltipTblOpt != sol::nullopt){ + sol::table tbl = guiTable["tooltip"]; + tooltip = parseTooltip(tbl, pos); + } + Button *button = nullptr; string guiScreen = ""; switch(type){ case SINGLE_PLAYER: button = new SinglePlayerButton(pos, size, name); + ((SinglePlayerButton*)button)->setTooltip(tooltip); break; case EDITOR: button = new MapEditorButton(pos, size); diff --git a/concreteGuiManager.h b/concreteGuiManager.h index 06ae6a2..ac23877 100644 --- a/concreteGuiManager.h +++ b/concreteGuiManager.h @@ -2,6 +2,7 @@ #define CONCRETE_GUI_MANAGER_H #include +#include #include #include @@ -71,6 +72,7 @@ namespace battleship{ CONSOLE, TRADE_OFFERS }; + class Tooltip; class ConcreteGuiManager : public vb01Gui::AbstractGuiManager{ public: @@ -99,6 +101,7 @@ namespace battleship{ void parseLuaScript(std::string, std::string = ""); private: ConcreteGuiManager(); + Tooltip* parseTooltip(sol::table&, vb01::Vector3); vb01Gui::Button* parseButton(int); vb01Gui::Listbox* parseGameObjectListbox(); vb01Gui::Listbox* parseListbox(int); diff --git a/singlePlayerButton.cpp b/singlePlayerButton.cpp index 72138f7..11819be 100644 --- a/singlePlayerButton.cpp +++ b/singlePlayerButton.cpp @@ -1,8 +1,11 @@ #include "singlePlayerButton.h" -#include "gameManager.h" #include "concreteGuiManager.h" +#include "gameManager.h" +#include "tooltip.h" #include +#include +#include #include #include @@ -13,14 +16,8 @@ namespace battleship{ using namespace vb01Gui; SinglePlayerButton::SinglePlayerButton(Vector3 pos, Vector2 size, string name) : Button(pos, size, name, GameManager::getSingleton()->getPath() + "Fonts/batang.ttf", GLFW_KEY_S, true) {} - - void SinglePlayerButton::onMouseOver(){ - setColor(Vector4(.8, .8, .8, 1)); - } - - void SinglePlayerButton::onMouseOff(){ - setColor(Vector4(.6, .6, .6, 1)); - } + + SinglePlayerButton::~SinglePlayerButton(){delete tooltip;} void SinglePlayerButton::onClick() { ConcreteGuiManager *guiManager = ConcreteGuiManager::getSingleton(); @@ -32,4 +29,10 @@ namespace battleship{ guiManager->getText("_mainPlayer")->setText(GameManager::getSingleton()->getMainPlayerName()); } + + void SinglePlayerButton::update() { + setColor(mouseOver ? Vector4(.8, .8, .8, 1) : Vector4(.6, .6, .6, 1)); + + if(tooltip) tooltip->getBackground()->getNode()->setVisible(mouseOver); + } } diff --git a/singlePlayerButton.h b/singlePlayerButton.h index 09e0224..4a5bf52 100644 --- a/singlePlayerButton.h +++ b/singlePlayerButton.h @@ -6,13 +6,17 @@ #include namespace battleship{ + class Tooltip; + class SinglePlayerButton : public vb01Gui::Button { public: SinglePlayerButton(vb01::Vector3, vb01::Vector2, std::string); + ~SinglePlayerButton(); void onClick(); - void onMouseOver(); - void onMouseOff(); + void update(); + inline void setTooltip(Tooltip *t){this->tooltip = t;} private: + Tooltip *tooltip = nullptr; }; } diff --git a/tooltip.cpp b/tooltip.cpp index a4d2681..838c3bf 100644 --- a/tooltip.cpp +++ b/tooltip.cpp @@ -1,14 +1,81 @@ #include "tooltip.h" +#include "gameManager.h" + +#include +#include +#include +#include +#include +#include using namespace std; using namespace vb01; namespace battleship{ - Tooltip::Tooltip(Vector2 pos, string entry){ - this->pos = pos; + Tooltip::Tooltip(Vector3 p, Vector2 s, vector lineData, string fontPath) : pos(p), size(s){ + Root *root = Root::getSingleton(); + Material *bgMat = new Material(root->getLibPath() + "gui"); + bgMat->addBoolUniform("texturingEnabled", false); + bgMat->addVec4Uniform("diffuseColor", Vector4(.5, .5, .5, .5)); + + background = new Quad(Vector3(s.x, s.y, 0), false); + background->setMaterial(bgMat); + + Node *bgNode = new Node(p); + bgNode->attachMesh(background); + root->getGuiNode()->attachChild(bgNode); + + string basePath = GameManager::getSingleton()->getPath(); + string iconBasePath = basePath + "Textures/Icons/"; + + for(int i = 0; i < lineData.size(); i++){ + LineData &ld = lineData[i]; + + Material *textMat = new Material(root->getLibPath() + "text"); + textMat->addBoolUniform("texturingEnabled", false); + textMat->addVec4Uniform("diffuseColor", ld.textColor); + + float scale = .2; + + Vector3 offset = Vector3(0, 20 * (i + 1), .01); + Text *text = new Text(fontPath, ld.entryText); + text->setMaterial(textMat); + Node *textNode = new Node(pos + offset - Vector3(0, 3, 0), Quaternion::QUAT_W, scale * Vector3::VEC_IJK); + textNode->addText(text); + bgNode->attachChild(textNode); + + for(pair &iconData: ld.iconsData){ + Vector3 iconOffset = offset; + wstring entry = text->getText(); + + for(int j = 0; j < iconData.first; j++){ + Text::Glyph *glyph = text->getGlyph(entry[j]); + iconOffset.x += scale * (glyph->advance >> 6); + } + + entry.insert(iconData.first, L" "); + text->setText(entry); + + Material *iconMat = new Material(root->getLibPath() + "gui"); + iconMat->addBoolUniform("texturingEnabled", true); + string ip[]{iconBasePath + iconData.second}; + Texture *t = new Texture(ip, 1, false); + iconMat->addTexUniform("textures[0]", t, false); + + Vector2 iconSize = 20 * Vector2::VEC_IJ; + Quad *rect = new Quad(Vector3(iconSize.x, iconSize.y, 0), false); + rect->setMaterial(iconMat); + Node *rectNode = new Node(iconOffset - Vector3(0, iconSize.y, 0)); + rectNode->attachMesh(rect); + bgNode->attachChild(rectNode); + } + } } Tooltip::~Tooltip(){ + Node *node = background->getNode(); + Root::getSingleton()->getGuiNode()->dettachChild(node); + delete node; } void Tooltip::update(){ diff --git a/tooltip.h b/tooltip.h index 68b8020..12316e4 100644 --- a/tooltip.h +++ b/tooltip.h @@ -1,16 +1,36 @@ #ifndef TOOLTIP_H #define TOOLTIP_H -#include +#include + +#include +#include +#include + +namespace vb01{ + class Quad; + class Text; +} namespace battleship{ class Tooltip{ public: - Tooltip(vb01::Vector2, std::string); + struct LineData{ + std::wstring entryText; + vb01::Vector4 textColor; + std::vector> iconsData; + + LineData(std::wstring et, vb01::Vector4 tc, std::vector> id) : entryText(et), textColor(tc), iconsData(id){}; + }; + + Tooltip(vb01::Vector3, vb01::Vector2, std::vector, std::string); ~Tooltip(); void update(); + inline vb01::Quad* getBackground(){return background;} private: - vb01::Vector2 pos; + vb01::Vector3 pos; + vb01::Vector2 size; + vb01::Quad *background = nullptr; }; }