1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246
|
/*
This file is part of Warzone 2100.
Copyright (C) 1999-2004 Eidos Interactive
Copyright (C) 2005-2021 Warzone 2100 Project
Warzone 2100 is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
Warzone 2100 is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Warzone 2100; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
/** @file
* The tool tip display system.
*/
#include "lib/framework/frame.h"
#include "lib/framework/wzapp.h"
#include "lib/framework/math_ext.h"
#include "lib/ivis_opengl/screen.h"
#include "widget.h"
#include "widgint.h"
#include "tip.h"
#include "lib/ivis_opengl/pieblitfunc.h"
#include <sstream>
#include <iostream>
#include <vector>
#include <string>
/* Time (in milliseconds) after pointing widget required to show the tool tip */
const auto TIP_PAUSE = 200;
/* Time (in milliseconds) required for a tip to be refreshed since the last refresh */
const auto TIP_REFRESH_COOLDOWN = 500;
/* Size of border around tip text */
const auto TIP_HGAP = 6;
const auto TIP_VGAP = 3;
static enum
{
TIP_INACTIVE,
TIP_ACTIVE,
TIP_CLICKED,
} tipState;
struct TipDisplayCache {
std::vector<WzText> wzTip;
std::vector<int> wzTipYStart;
};
static int32_t startTime;
static Vector2i lastMouseCoords;
static WzRect tipRect;
static Vector2i textOffset;
static std::string tipText;
static WIDGET *tipSourceWidget;
static PIELIGHT TipColour;
static TipDisplayCache displayCache;
static nonstd::optional<uint32_t> lastRefreshTime;
/* Initialise the tool tip module */
void tipInitialise(void)
{
tipState = TIP_INACTIVE;
TipColour = WZCOL_WHITE;
}
void tipShutdown()
{
displayCache.wzTip.clear();
}
// Set the global toop tip text colour.
void widgSetTipColour(PIELIGHT colour)
{
TipColour = colour;
}
static std::vector<std::string> splitString(const std::string& str, char delimiter)
{
std::vector<std::string> strings;
std::istringstream str_stream(str);
std::string s;
while (getline(str_stream, s, delimiter)) {
strings.push_back(s);
}
return strings;
}
static void tipStop()
{
tipState = TIP_INACTIVE;
displayCache.wzTip.clear();
tipText = "";
startTime = wzGetTicks();
tipSourceWidget = nullptr;
}
static bool isHighlightedChanged(std::shared_ptr<WIDGET> mouseOverWidget)
{
return !mouseOverWidget || tipSourceWidget != mouseOverWidget.get();
}
static void refreshTip(std::shared_ptr<WIDGET> mouseOverWidget)
{
if (isHighlightedChanged(mouseOverWidget))
{
tipStop();
return;
}
if (lastRefreshTime.has_value() && wzGetTicks() - lastRefreshTime.value() < TIP_REFRESH_COOLDOWN)
{
return;
}
auto newTip = mouseOverWidget->getTip();
lastRefreshTime = wzGetTicks();
if (newTip == tipText)
{
return;
}
tipText = newTip;
if (tipText == "")
{
return;
}
auto fontId = font_regular;
if (auto lockedScreen = mouseOverWidget->screenPointer.lock()) {
fontId = lockedScreen->TipFontID;
}
auto maxLineWidth = 0;
auto lines = splitString(tipText, '\n');
auto totalLineSize = 0;
displayCache.wzTip.resize(lines.size());
displayCache.wzTipYStart.resize(lines.size());
for (size_t n = 0; n < lines.size(); ++n)
{
displayCache.wzTip[n].setText(WzString::fromUtf8(lines[n]), fontId);
maxLineWidth = std::max<int>(maxLineWidth, displayCache.wzTip[n].width());
displayCache.wzTipYStart[n] = totalLineSize;
totalLineSize += displayCache.wzTip[n].lineSize();
}
/* Position the tip box */
auto width = maxLineWidth + TIP_HGAP * 2;
int32_t height = TIP_VGAP * 2 + totalLineSize;
if (!lines.empty())
{
height += displayCache.wzTip.back().belowBase();
}
auto x = clip<SDWORD>(mouseOverWidget->screenPosX() + mouseOverWidget->width() / 2, 0, screenWidth - width - 1);
auto y = std::min(mouseOverWidget->screenPosY() - height - TIP_VGAP, (int)screenHeight);
if (y < 0)
{
/* Position the tip below the button */
y = std::max(mouseOverWidget->screenPosY() + mouseOverWidget->height() + TIP_VGAP, 0);
}
tipRect = WzRect(x - 1, y - 1, width + 2, height + 2);
/* Position the text */
int yOffset = (height - totalLineSize) / 2;
if (!lines.empty())
{
yOffset -= displayCache.wzTip.front().aboveBase();
}
textOffset = Vector2i(
x + TIP_HGAP,
y + yOffset
);
}
static void handleTipInactive()
{
auto mouseCoords = Vector2i(mouseX(), mouseY());
std::shared_ptr<WIDGET> mouseOverWidget;
if (mouseCoords != lastMouseCoords || isHighlightedChanged(mouseOverWidget = getMouseOverWidget().lock())) {
lastMouseCoords = mouseCoords;
startTime = wzGetTicks();
tipSourceWidget = mouseOverWidget.get();
return;
}
if (wzGetTicks() - startTime > TIP_PAUSE)
{
tipState = TIP_ACTIVE;
lastRefreshTime = nonstd::nullopt;
refreshTip(mouseOverWidget);
}
}
static void handleTipActive()
{
if (mousePressed(MOUSE_LMB))
{
tipState = TIP_CLICKED;
return;
}
refreshTip(getMouseOverWidget().lock());
if (tipText.empty())
{
return;
}
/* Draw the tool tip */
iV_ShadowBox(tipRect.x(), tipRect.y(), tipRect.right(), tipRect.bottom(), 1, WZCOL_FORM_LIGHT, WZCOL_FORM_DARK, WZCOL_FORM_TIP_BACKGROUND);
size_t n = 0;
for (auto &line: displayCache.wzTip)
{
line.render(textOffset.x, textOffset.y + displayCache.wzTipYStart[n], TipColour);
++n;
}
}
static void handleTipClicked()
{
if (isHighlightedChanged(getMouseOverWidget().lock()))
{
tipStop();
}
}
/* Update and possibly display the tip */
void tipDisplay()
{
switch (tipState)
{
case TIP_INACTIVE: handleTipInactive(); break;
case TIP_ACTIVE: handleTipActive(); break;
case TIP_CLICKED: handleTipClicked(); break;
}
}
|