File: CTooltip.cpp

package info (click to toggle)
0ad 0.0.17-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 51,248 kB
  • ctags: 46,933
  • sloc: cpp: 223,208; ansic: 31,240; python: 16,343; perl: 4,083; sh: 1,011; makefile: 915; xml: 733; java: 621; ruby: 229; erlang: 53; sql: 40
file content (176 lines) | stat: -rw-r--r-- 5,195 bytes parent folder | download | duplicates (2)
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
/* Copyright (C) 2013 Wildfire Games.
 * This file is part of 0 A.D.
 *
 * 0 A.D. 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.
 *
 * 0 A.D. 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 0 A.D.  If not, see <http://www.gnu.org/licenses/>.
 */

// CTooltip: GUI object used for tooltips

#include "precompiled.h"

#include "CTooltip.h"
#include "CGUI.h"

#include <algorithm>

CTooltip::CTooltip()
{
	// If the tooltip is an object by itself:
	AddSetting(GUIST_float,					"buffer_zone");
	AddSetting(GUIST_CGUIString,			"caption");
	AddSetting(GUIST_CStrW,					"font");
	AddSetting(GUIST_CGUISpriteInstance,	"sprite");
	AddSetting(GUIST_int,					"delay");
	AddSetting(GUIST_CColor,				"textcolor");
	AddSetting(GUIST_float,					"maxwidth");
	AddSetting(GUIST_CPos,					"offset");
	AddSetting(GUIST_EVAlign,				"anchor");
	AddSetting(GUIST_EAlign,				"text_align");
	// This is used for tooltips that are hidden/revealed manually by scripts, rather than through the standard tooltip display mechanism
	AddSetting(GUIST_bool,					"independent");

	// If the tooltip is just a reference to another object:
	AddSetting(GUIST_CStr,					"use_object");
	AddSetting(GUIST_bool,					"hide_object");

	// Private settings:
	AddSetting(GUIST_CPos,					"_mousepos");

	// Defaults
	GUI<int>::SetSetting(this, "delay", 500);
	GUI<EVAlign>::SetSetting(this, "anchor", EVAlign_Bottom);
	GUI<EAlign>::SetSetting(this, "text_align", EAlign_Left);

	// Set up a blank piece of text, to be replaced with a more
	// interesting message later
	AddText(new SGUIText());
}

CTooltip::~CTooltip()
{
}

void CTooltip::SetupText()
{
	if (!GetGUI())
		return;

	ENSURE(m_GeneratedTexts.size()==1);

	CStrW font;
	if (GUI<CStrW>::GetSetting(this, "font", font) != PSRETURN_OK || font.empty())
		font = L"default";

	float buffer_zone = 0.f;
	GUI<float>::GetSetting(this, "buffer_zone", buffer_zone);

	CGUIString caption;
	GUI<CGUIString>::GetSetting(this, "caption", caption);

	float max_width = 0.f;
	GUI<float>::GetSetting(this, "maxwidth", max_width);

	*m_GeneratedTexts[0] = GetGUI()->GenerateText(caption, font, max_width, buffer_zone, this);


	// Position the tooltip relative to the mouse:

	CPos mousepos, offset;
	EVAlign anchor;
	bool independent;
	
	GUI<bool>::GetSetting(this, "independent", independent);
	if (independent)
		mousepos = GetMousePos();
	else
		GUI<CPos>::GetSetting(this, "_mousepos", mousepos);

	GUI<CPos>::GetSetting(this, "offset", offset);
	GUI<EVAlign>::GetSetting(this, "anchor", anchor);

	// TODO: Calculate the actual width of the wrapped text and use that.
	// (m_Size.cx is >max_width if the text wraps, which is not helpful)
	float textwidth = std::min(m_GeneratedTexts[0]->m_Size.cx, (float)max_width);
	float textheight = m_GeneratedTexts[0]->m_Size.cy;

	CClientArea size;
	size.pixel.left = mousepos.x + offset.x;
	size.pixel.right = size.pixel.left + textwidth;
	switch (anchor)
	{
	case EVAlign_Top:
		size.pixel.top = mousepos.y + offset.y;
		size.pixel.bottom = size.pixel.top + textheight;
		break;
	case EVAlign_Bottom:
		size.pixel.bottom = mousepos.y + offset.y;
		size.pixel.top = size.pixel.bottom - textheight;
		break;
	case EVAlign_Center:
		size.pixel.top = mousepos.y + offset.y - textheight/2.f;
		size.pixel.bottom = size.pixel.top + textwidth;
		break;
	default:
		debug_warn(L"Invalid EVAlign!");
	}


	// Reposition the tooltip if it's falling off the screen:

	extern int g_xres, g_yres;
	float screenw = (float)g_xres, screenh = (float)g_yres;

	if (size.pixel.top < 0.f)
		size.pixel.bottom -= size.pixel.top, size.pixel.top = 0.f;
	else if (size.pixel.bottom > screenh)
		size.pixel.top -= (size.pixel.bottom-screenh), size.pixel.bottom = screenh;

	if (size.pixel.left < 0.f)
		size.pixel.right -= size.pixel.left, size.pixel.left = 0.f;
	else if (size.pixel.right > screenw)
		size.pixel.left -= (size.pixel.right-screenw), size.pixel.right = screenw;

	GUI<CClientArea>::SetSetting(this, "size", size);
}

void CTooltip::HandleMessage(SGUIMessage &Message)
{
	IGUITextOwner::HandleMessage(Message);
}

void CTooltip::Draw() 
{
	float z = 900.f; // TODO: Find a nicer way of putting the tooltip on top of everything else

	if (GetGUI())
	{
		CGUISpriteInstance *sprite;
		GUI<CGUISpriteInstance>::GetSettingPointer(this, "sprite", sprite);

		// Normally IGUITextOwner will handle this updating but since SetupText can modify the position
		// we need to call it now *before* we do the rest of the drawing
		if (!m_GeneratedTextsValid)
		{
			SetupText();
			m_GeneratedTextsValid = true;
		}

		GetGUI()->DrawSprite(*sprite, 0, z, m_CachedActualSize);

		CColor color;
		GUI<CColor>::GetSetting(this, "textcolor", color);

		DrawText(0, color, m_CachedActualSize.TopLeft(), z+0.1f);
	}
}