| [25140] | 1 | /* Copyright (C) 2021 Wildfire Games.
|
|---|
| [27965] | 2 | * This file is part of 0 A.D.
|
|---|
| [6830] | 3 | *
|
|---|
| [27965] | 4 | * 0 A.D. is free software: you can redistribute it and/or modify
|
|---|
| [6830] | 5 | * it under the terms of the GNU General Public License as published by
|
|---|
| 6 | * the Free Software Foundation, either version 2 of the License, or
|
|---|
| 7 | * (at your option) any later version.
|
|---|
| 8 | *
|
|---|
| [27965] | 9 | * 0 A.D. is distributed in the hope that it will be useful,
|
|---|
| [6830] | 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|---|
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|---|
| 12 | * GNU General Public License for more details.
|
|---|
| 13 | *
|
|---|
| 14 | * You should have received a copy of the GNU General Public License
|
|---|
| [27965] | 15 | * along with 0 A.D. If not, see <http://www.gnu.org/licenses/>.
|
|---|
| [6830] | 16 | */
|
|---|
| 17 |
|
|---|
| [3802] | 18 | #include "precompiled.h"
|
|---|
| 19 |
|
|---|
| 20 | #include "CTooltip.h"
|
|---|
| 21 |
|
|---|
| [22941] | 22 | #include "gui/CGUI.h"
|
|---|
| [23028] | 23 | #include "gui/SettingTypes/CGUIString.h"
|
|---|
| [22976] | 24 | #include "gui/CGUIText.h"
|
|---|
| [22941] | 25 |
|
|---|
| [3802] | 26 | #include <algorithm>
|
|---|
| 27 |
|
|---|
| [22741] | 28 | CTooltip::CTooltip(CGUI& pGUI)
|
|---|
| [23005] | 29 | : IGUIObject(pGUI),
|
|---|
| [23020] | 30 | IGUITextOwner(*static_cast<IGUIObject*>(this)),
|
|---|
| [25392] | 31 | m_BufferZone(this, "buffer_zone"),
|
|---|
| 32 | m_Caption(this, "caption"),
|
|---|
| 33 | m_Font(this, "font"),
|
|---|
| 34 | m_Sprite(this, "sprite"),
|
|---|
| 35 | m_Delay(this, "delay", 500), // in milliseconds
|
|---|
| 36 | m_TextColor(this, "textcolor"),
|
|---|
| 37 | m_MaxWidth(this, "maxwidth"),
|
|---|
| 38 | m_Offset(this, "offset"),
|
|---|
| 39 | m_Anchor(this, "anchor", EVAlign::BOTTOM),
|
|---|
| 40 | // This is used for tooltips that are hidden/revealed manually by scripts, rather than through the standard tooltip display mechanism
|
|---|
| 41 | m_Independent(this, "independent"),
|
|---|
| 42 | // Private settings:
|
|---|
| 43 | // This is set by GUITooltip
|
|---|
| 44 | m_MousePos(this, "_mousepos"),
|
|---|
| 45 | // If the tooltip is just a reference to another object:
|
|---|
| 46 | m_UseObject(this, "use_object"),
|
|---|
| 47 | m_HideObject(this, "hide_object")
|
|---|
| [3802] | 48 | {
|
|---|
| 49 | // Set up a blank piece of text, to be replaced with a more
|
|---|
| 50 | // interesting message later
|
|---|
| [22679] | 51 | AddText();
|
|---|
| [3802] | 52 | }
|
|---|
| 53 |
|
|---|
| 54 | CTooltip::~CTooltip()
|
|---|
| 55 | {
|
|---|
| 56 | }
|
|---|
| 57 |
|
|---|
| 58 | void CTooltip::SetupText()
|
|---|
| 59 | {
|
|---|
| [16931] | 60 | ENSURE(m_GeneratedTexts.size() == 1);
|
|---|
| [3802] | 61 |
|
|---|
| [25392] | 62 | m_GeneratedTexts[0] = CGUIText(m_pGUI, m_Caption, m_Font, m_MaxWidth, m_BufferZone, m_TextAlign, this);
|
|---|
| [3802] | 63 |
|
|---|
| 64 | // Position the tooltip relative to the mouse:
|
|---|
| 65 |
|
|---|
| [25152] | 66 | const CVector2D& mousepos = m_Independent ? m_pGUI.GetMousePos() : m_MousePos;
|
|---|
| [16931] | 67 |
|
|---|
| [25143] | 68 | float textwidth = m_GeneratedTexts[0].GetSize().Width;
|
|---|
| 69 | float textheight = m_GeneratedTexts[0].GetSize().Height;
|
|---|
| [3802] | 70 |
|
|---|
| [23023] | 71 | CGUISize size;
|
|---|
| [25392] | 72 | size.pixel.left = mousepos.X + m_Offset->X;
|
|---|
| [3802] | 73 | size.pixel.right = size.pixel.left + textwidth;
|
|---|
| [22765] | 74 |
|
|---|
| [23005] | 75 | switch (m_Anchor)
|
|---|
| [3802] | 76 | {
|
|---|
| [25142] | 77 | case EVAlign::TOP:
|
|---|
| [25392] | 78 | size.pixel.top = mousepos.Y + m_Offset->Y;
|
|---|
| [3802] | 79 | size.pixel.bottom = size.pixel.top + textheight;
|
|---|
| 80 | break;
|
|---|
| [25142] | 81 | case EVAlign::BOTTOM:
|
|---|
| [25392] | 82 | size.pixel.bottom = mousepos.Y + m_Offset->Y;
|
|---|
| [3802] | 83 | size.pixel.top = size.pixel.bottom - textheight;
|
|---|
| 84 | break;
|
|---|
| [25142] | 85 | case EVAlign::CENTER:
|
|---|
| [25392] | 86 | size.pixel.top = mousepos.Y + m_Offset->Y - textheight/2.f;
|
|---|
| [3802] | 87 | size.pixel.bottom = size.pixel.top + textwidth;
|
|---|
| 88 | break;
|
|---|
| 89 | default:
|
|---|
| [7161] | 90 | debug_warn(L"Invalid EVAlign!");
|
|---|
| [3802] | 91 | }
|
|---|
| 92 |
|
|---|
| 93 |
|
|---|
| [25577] | 94 | // Reposition the tooltip if it's falling off in the GUI window.
|
|---|
| 95 | const CSize2D windowSize = m_pGUI.GetWindowSize();
|
|---|
| [3802] | 96 |
|
|---|
| 97 | if (size.pixel.top < 0.f)
|
|---|
| [25577] | 98 | {
|
|---|
| 99 | size.pixel.bottom -= size.pixel.top;
|
|---|
| 100 | size.pixel.top = 0.f;
|
|---|
| 101 | }
|
|---|
| 102 | else if (size.pixel.bottom > windowSize.Height)
|
|---|
| 103 | {
|
|---|
| 104 | size.pixel.top -= size.pixel.bottom - windowSize.Height;
|
|---|
| 105 | size.pixel.bottom = windowSize.Height;
|
|---|
| 106 | }
|
|---|
| [8916] | 107 |
|
|---|
| 108 | if (size.pixel.left < 0.f)
|
|---|
| [25577] | 109 | {
|
|---|
| 110 | size.pixel.right -= size.pixel.left;
|
|---|
| 111 | size.pixel.left = 0.f;
|
|---|
| 112 | }
|
|---|
| 113 | else if (size.pixel.right > windowSize.Width)
|
|---|
| 114 | {
|
|---|
| 115 | size.pixel.left -= size.pixel.right - windowSize.Width;
|
|---|
| 116 | size.pixel.right = windowSize.Width;
|
|---|
| 117 | }
|
|---|
| [14764] | 118 |
|
|---|
| [25392] | 119 | m_Size.Set(size, true);
|
|---|
| [3802] | 120 | }
|
|---|
| 121 |
|
|---|
| [23020] | 122 | void CTooltip::UpdateCachedSize()
|
|---|
| 123 | {
|
|---|
| 124 | IGUIObject::UpdateCachedSize();
|
|---|
| 125 | IGUITextOwner::UpdateCachedSize();
|
|---|
| 126 | }
|
|---|
| 127 |
|
|---|
| [16931] | 128 | void CTooltip::HandleMessage(SGUIMessage& Message)
|
|---|
| [3802] | 129 | {
|
|---|
| [23020] | 130 | IGUIObject::HandleMessage(Message);
|
|---|
| [7466] | 131 | IGUITextOwner::HandleMessage(Message);
|
|---|
| [3802] | 132 | }
|
|---|
| 133 |
|
|---|
| [25591] | 134 | void CTooltip::Draw(CCanvas2D& canvas)
|
|---|
| [3802] | 135 | {
|
|---|
| [16931] | 136 | // Normally IGUITextOwner will handle this updating but since SetupText can modify the position
|
|---|
| 137 | // we need to call it now *before* we do the rest of the drawing
|
|---|
| 138 | if (!m_GeneratedTextsValid)
|
|---|
| [3802] | 139 | {
|
|---|
| [16931] | 140 | SetupText();
|
|---|
| 141 | m_GeneratedTextsValid = true;
|
|---|
| 142 | }
|
|---|
| [3802] | 143 |
|
|---|
| [25591] | 144 | m_pGUI.DrawSprite(m_Sprite, canvas, m_CachedActualSize);
|
|---|
| 145 | DrawText(canvas, 0, m_TextColor, m_CachedActualSize.TopLeft());
|
|---|
| [3802] | 146 | }
|
|---|
| [25229] | 147 |
|
|---|
| 148 | float CTooltip::GetBufferedZ() const
|
|---|
| 149 | {
|
|---|
| 150 | // TODO: Find a nicer way of putting the tooltip on top of everything else.
|
|---|
| 151 | return 900.f;
|
|---|
| 152 | }
|
|---|