Torque Shader Engine/GUI/Resources/Tooltips

From TDN

Adding Tooltips to GUI controls in TGEA

Introduction

You may have noticed when editing your GUIs that there is a section for tooltips with the following fields:

  • tooltipprofile (if left blank, the tooltip will use the parent control's profile)
  • ToolTip (the text to display on the tooltip)
  • hovertime (the time to keep the tooltip visible)

...However, if you fill in the ToolTip field, nothing happens when you mouse over your control. This guide will show you how to fix that in TGEA 1.01. This will not work in TGE but there is a resource on the GarageGames website that enables tooltips there. LINK TO RESOURCE FOR TGE ON GG WEBSITE

Guide

In the TGEA 1.01 codebase is a method (GuiControl::renderTooltip) inside guiControl.cpp which is ready to be used to render simple tooltips for GUI controls. However, there is some code that needs to be added to make it work.

THE FOLLOWING SECTION WAS PROVIDED BY FRANK BIGNONE ON THE GARAGE GAMES FORUMS IN ANSWER TO MY QUESTION ABOUT HOW TO MAKE TOOLTIPS WORK IN TGEA:

First, add the following members inside GuiControl.h just after mCanSave:

U32 mTooltipTimer;
Point2I mMousePos;

Then edit the method GuiControl::onMouseEnter, onMouseLeave and onMouseMove:

void GuiControl::onMouseEnter(const GuiEvent &)
{
    mTooltipTimer = Sim::getCurrentTime();  <---ADD
}
void GuiControl::onMouseLeave(const GuiEvent &)
{
    mTooltipTimer = 0;  <---ADD
}
void GuiControl::onMouseMove(const GuiEvent &event)
{
    mMousePos = event.mousePoint;  <---ADD
    [...rest of code is same...]
}

Then add the following lines just before renderChildControls inside GuiControl::onRender:

// Render tooltip
if(mTooltipTimer) renderTooltip(mMousePos);

and add the following lines inside GuiControl::renderTooltip just before the awake test:

if(mTooltipTimer && (mTooltipTimer+mTipHoverTime) < Sim::getCurrentTime())
{
    mTooltipTimer = 0;
    return false;
}

Now, the tooltip will work for all controls that do not forget to execute GuiControl::xxx methods..... and this is not the case for the button control for example.

So, you need to add the following code inside guiButtonBaseCtrl.cpp:

void GuiButtonBaseCtrl::onMouseLeave(const GuiEvent &event)
{
     GuiControl::onMouseLeave(event);  <---ADD
     [...rest of code is same...]
}

and:

void GuiButtonBaseCtrl::onMouseEnter(const GuiEvent &event)
{
     GuiControl::onMouseEnter(event);  <---ADD
     [...rest of code is same...]
}

and the last one inside GuiButtonCtrl::onRender - add just before renderChildControls:

if(mTooltipTimer) GuiControl::renderTooltip(mMousePos);

Now it should work if you set the correct information inside your object in the script and you should have something like this: image:tooltip.jpg

THE FOLLOWING WAS MY ADDITION TO INCLUDE SOME MORE FUNCTIONALITY:

I found that any GUI control type that wants to be able to display tooltips needs to have:

if(mTooltipTimer) GuiControl::renderTooltip(mMousePos);

added to the end of it's onRender method (or just before renderChildControls).


Also, I added a tooltip offset field, so that each case can have a custom offset if needed (I found that keeping the same rendering location of the tooltip for all of my controls did not look the best)


Very simple addition:


In guiControl.h add just after S32 mTipHoverTime:

Point2I mTooltipOffset;

In GuiControl::GuiControl() add somewhere:

mTooltipOffset.set(0, 0); // set this to whatever is most commonly best in your case

In GuiControl::initPersistFields() add into the "ToolTip" group:

addField("offset",			 TypePoint2I,      Offset(mTooltipOffset, GuiControl));

And finally in GuiControl::renderTooltip change this line:

Point2I offset = cursorPos;

to look like this:

Point2I offset = cursorPos + mTooltipOffset;

Now you can change the offset for your tooltip on a case by case basis if needed. You can change the "offset" field in script or directly in the GUI editor.