VGUI Tooltip: Difference between revisions

From Valve Developer Community
Jump to navigation Jump to search
m (added language bar.)
Line 1: Line 1:
A tooltip is a vgui2 element defined in the vgui_controls library, in the file Tooltip.cpp. Tooltips are available in all source games. Tooltips are useful for displaying additional information about something, without taking up any extra space.
{{lang|VGUI Tooltip}}[[Category:VGUI Controls|T]]
A tooltip is a vgui2 element defined in the <code>vgui_controls</code> library, in the file <code>Tooltip.cpp</code>. Tooltips are available in all source games. Tooltips are useful for displaying additional information about something, without taking up any extra space.


[[Image:Tooltip.jpg|thumb|This screenshot was taken from the mod FPS Dota. The tooltip is displaying additonal information about the hero's strength attribute.]]
[[Image:Tooltip.jpg|thumb|This screenshot was taken from the mod FPS Dota. The tooltip is displaying additonal information about the hero's strength attribute.]]


== Example Usage ==
== Example Usage ==
 
* Before using tooltips, be sure to include <code>vgui_controls/tooltip.h</code>.
* Before using tooltips, be sure to include "vgui_controls/tooltip.h"


* Most VGUI elements have tooltips associated with them.
* Most VGUI elements have tooltips associated with them.


* You can call control->GetTooltip() to get the control's tooltip, and then you can call tooltip->SetText to set the tooltip's text.
* You can call <code>control->GetTooltip()</code> to get the control's tooltip, and then you can call <code>tooltip->SetText</code> to set the tooltip's text.


* Alternatively, you can also set the text of a tooltip in a resource file using the keyvalue "tooltiptext".
* Alternatively, you can also set the text of a tooltip in a resource file using the keyvalue <code>tooltiptext</code>.


  "StrengthLabel"
  "StrengthLabel"
Line 34: Line 34:
  }
  }


== Known Problems with Tooltips ==
== Known Problems With Tooltips ==
 
Calling <code>Tooltip::SetText</code> on a tooltip when a tooltip window is visible changes the text for the visible tooltip window. Make sure that you check to make sure no tooltip windows are currently open before calling <code>Tooltip::SetText</code>.
Calling Tooltip::SetText on a tooltip when a tooltip window is visible changes the text for the visible tooltip window. Make sure that you check to make sure no tooltip windows are currently open before calling Tooltip::SetText.


  void Tooltip::SetText(const char *text)
  void Tooltip::SetText(const char *text)
Line 64: Line 63:
  }
  }


The code above was taken from line 89 of tooltip.cpp.
The code above was taken from line 89 of <code>tooltip.cpp</code>.
 
Valve uses a static handler for the current displayed tooltip window (declared on line 28 of tooltip.cpp), <code>static vgui::DHANDLE< TextEntry > s_TooltipWindow;</code>.


I'm not sure why, but as you can see in the code above, towards the end of the function Tooltip::SetText, it changes the text of the active tooltip window. Valve needs to remove this from their code to fix this bug.
Valve uses a static handler for the current displayed tooltip window (declared on line 28 of <code>tooltip.cpp</code>), <code>static vgui::DHANDLE< TextEntry > s_TooltipWindow;</code>.


[[Category:VGUI Controls|T]]
Ii isn't clear why, but as you can see in the code above, towards the end of the function <code>Tooltip::SetText</code>, it changes the text of the active tooltip window. Valve needs to remove this from their code to fix this bug.

Revision as of 07:39, 21 May 2022

English (en)Translate (Translate)

A tooltip is a vgui2 element defined in the vgui_controls library, in the file Tooltip.cpp. Tooltips are available in all source games. Tooltips are useful for displaying additional information about something, without taking up any extra space.

This screenshot was taken from the mod FPS Dota. The tooltip is displaying additonal information about the hero's strength attribute.

Example Usage

  • Before using tooltips, be sure to include vgui_controls/tooltip.h.
  • Most VGUI elements have tooltips associated with them.
  • You can call control->GetTooltip() to get the control's tooltip, and then you can call tooltip->SetText to set the tooltip's text.
  • Alternatively, you can also set the text of a tooltip in a resource file using the keyvalue tooltiptext.
"StrengthLabel"
{
   "ControlName"	"Label"
   "fieldName"		"StrengthLabel"
   "xpos"		"185"
   "ypos"		"2"
   "tooltiptext"	"Each point increases hit points by 19. Each point increases hit point regen."
   "wide"		"100"
   "tall"		"20"
   "autoResize"	"0"
   "pinCorner"		"0"
   "visible"		"1"
   "enabled"		"1"
   "tabPosition"	"0"
   "labelText"		"Strength:"
   "textAlignment"	"west"
   "dulltext"		"0"
   "brighttext"	"0"
   "wrap"		"0"
}

Known Problems With Tooltips

Calling Tooltip::SetText on a tooltip when a tooltip window is visible changes the text for the visible tooltip window. Make sure that you check to make sure no tooltip windows are currently open before calling Tooltip::SetText.

void Tooltip::SetText(const char *text)
{
	_isDirty = true;

	if (!text)
	{
		text = "";
	}

	if (m_Text.Size() > 0)
	{
		m_Text.RemoveAll();
	}

	for (unsigned int i = 0; i < strlen(text); i++)
	{
		m_Text.AddToTail(text[i]);
	}
	m_Text.AddToTail('\0');
	
	if (s_TooltipWindow.Get())
	{
		s_TooltipWindow->SetText(m_Text.Base());
	}
}

The code above was taken from line 89 of tooltip.cpp.

Valve uses a static handler for the current displayed tooltip window (declared on line 28 of tooltip.cpp), static vgui::DHANDLE< TextEntry > s_TooltipWindow;.

Ii isn't clear why, but as you can see in the code above, towards the end of the function Tooltip::SetText, it changes the text of the active tooltip window. Valve needs to remove this from their code to fix this bug.