How do I make custom tooltip?

How do I make custom tooltip?

Discuss Lua based Spring scripts (LuaUI widgets, mission scripts, gaia scripts, mod-rules scripts, scripted keybindings, etc...)

Moderator: Moderators

Post Reply
User avatar
zwzsg
Kernel Panic Co-Developer
Posts: 7052
Joined: 16 Nov 2004, 13:08

How do I make custom tooltip?

Post by zwzsg »

How do I make custom tooltip?

I want to make a custom tooltip, that works both when mousehovering buildmenus and mousehovering units.

However, I do not know what entry point to use to modify the tooltip.
- I know about Spring.SetUnitTooltip(unit_id,txt) however, that only works for existing units, one at a time, and once touched the tooltip is fixed for that unit. So, that way, I cannot change the tooltip in buildmenus, and I cannot update the tooltip according to health for instance, unless I run it for every unit every frame, which I'm afraid would be costly.
- I know about function widget:GetTooltip(x,y) but then I have to determine by myself what is the mouse hovering over, and I would rather have the engine tells me if it's over an existing unit or an icon in a buildmenu, and then the ID of the unit or the ID of the Unit Def, instead of having to determine that by my code and risking getting it wrong whenever someone use a slightly tweaked menu or whatever.

Here I found mention of a: WorldTooltip() --> "ttType, data1, data2, data3" special, should be documented in detail. Where do I find the detailed documentation about it? And it says that GetTooltip() --> "x, y" where x,y = screen coordinates. Returns WorldTooltip. and that confuse me a bit. What do you call a worldtooptip anyway?

And can someone answer to my post there, as it's easier to understand with exemples than with winking smileys.
User avatar
thesleepless
Posts: 417
Joined: 24 Oct 2007, 04:49

Re: How do I make custom tooltip?

Post by thesleepless »

i'm curious about this also... unfortunatly it looks like it's hardcoded.

Sim/Units/CommandAI/FactoryCAI.cpp: c.tooltip = "Build: ";
Sim/Units/CommandAI/BuilderCAI.cpp: c.tooltip = "Build: ";

sprintf(tmp, "\nHealth %.0f\nMetal cost %.0f\nEnergy cost %.0f Build time %.0f",

this looks like it would be an issue for mods that don't use metal/energy

really need to make some way to change these tooltips
User avatar
Argh
Posts: 10920
Joined: 21 Feb 2005, 03:38

Re: How do I make custom tooltip?

Post by Argh »

ImageThat's a really big hint, btw. You'll thank me, if you figure it out.
User avatar
zwzsg
Kernel Panic Co-Developer
Posts: 7052
Joined: 16 Nov 2004, 13:08

Re: How do I make custom tooltip?

Post by zwzsg »

So, you just disable current tooltip, get the text string of current tooltip, replace some sub-string in it, then draw it yourself.

But I still wish Lua could have passed the kind of tooltip and the unitID or UnitDefID. Because with your way, Argh, there's no way to also show custom unit information not already present in the default tooltip.


thesleepless: The default is hardcoded, but we can override the default. It's already possible to some extent, like with the command Spring.SetUnitTooltip(unit_id,txt) or the value returned by the call widget:GetTooltip(x,y).


Can I have the missing documentation on WorldTooltip() --> "ttType, data1, data2, data3" ? Hopefully those mysterious data will be what I need.
User avatar
jK
Spring Developer
Posts: 2299
Joined: 28 Jun 2007, 07:30

Re: How do I make custom tooltip?

Post by jK »

Is it that hard to check the source code? ...

-> there are 2 callins:

GetTooltip(x,y)
WorldTooltip("unit",unitID)
WorldTooltip("feature",featureID)
WorldTooltip("ground",posx,posy,posz)
WorldTooltip("selection")
(the WorldTooltip arguments should remind you of Spring.TraceScreenRay)

PS: a Spring.Echo can do wonders, if you don't know what the arguments are ...
User avatar
KDR_11k
Game Developer
Posts: 8293
Joined: 25 Jun 2006, 08:44

Re: How do I make custom tooltip?

Post by KDR_11k »

thesleepless wrote:i'm curious about this also... unfortunatly it looks like it's hardcoded.

Sim/Units/CommandAI/FactoryCAI.cpp: c.tooltip = "Build: ";
Sim/Units/CommandAI/BuilderCAI.cpp: c.tooltip = "Build: ";

sprintf(tmp, "\nHealth %.0f\nMetal cost %.0f\nEnergy cost %.0f Build time %.0f",

this looks like it would be an issue for mods that don't use metal/energy

really need to make some way to change these tooltips
You can easily change the build option tooltips with EditUnitCmdDesc. That doesn't take care of units though. Some mods have no use for the M/E displays.
Gnomre
Imperial Winter Developer
Posts: 1754
Joined: 06 Feb 2005, 13:42

Re: How do I make custom tooltip?

Post by Gnomre »

yawn

Image
Image

It certainly wasn't easy but yeah.
User avatar
thesleepless
Posts: 417
Joined: 24 Oct 2007, 04:49

Re: How do I make custom tooltip?

Post by thesleepless »

that looks great Gnome, any advice on how you went about it?
are you replacing the tooltip entirely?
User avatar
zwzsg
Kernel Panic Co-Developer
Posts: 7052
Joined: 16 Nov 2004, 13:08

Re: How do I make custom tooltip?

Post by zwzsg »

Question is not "Is it do-able?" but "How do I do it?" and more precisely "How do I do it the easiest, cleanest way that involve the less code and respects the more other widgets & interfaces?"

Cool! I'm credited in gnome's picture!

jK wrote:Is it that hard to check the source code? ...
Yes, and thanks.

So, the proper way to do it would be to use GetTooltip for live units, and lots and lots of EditUnitCmdDesc for the buildmenus?
Gnomre
Imperial Winter Developer
Posts: 1754
Joined: 06 Feb 2005, 13:42

Re: How do I make custom tooltip?

Post by Gnomre »

Here's how I handled buildpics basically:

Code: Select all

function widget:Initialize()
	--other stuff was here
	for id,def in pairs(UnitDefs) do				--mousing over buildpics only gives us the name so a lookup table is necessary
		unitDefHumanNames[def.humanName .. def.energyCost .. def.metalCost .. def.buildTime] = id
	end								--costs are necessary because some imp/reb units have the same names
end
In the actual code, I first look for the "Build:" keyword then if I get a match I tear apart the string and get those other values and match them against the table I built.

I know it's not the cleanest way ever but that long string is the only data you get from buildmenus, so if you want any information from the unitdef that's not contained in that string that's pretty much the only way I can think of to get the udid.

I don't know how well it'd fare in other mods (especially KP since there are no cost values, though if the names are all unique that wouldn't matter) or with other widgets like buildbar. It either works fine, or dumps the information into the lower section (like that credits bit--I don't parse those seperately other than word wrapping), or no one uses it so I haven't had any complaints :P

[edit] Just to be clear, I didn't use EditUnitCmdDesc at all.
User avatar
zwzsg
Kernel Panic Co-Developer
Posts: 7052
Joined: 16 Nov 2004, 13:08

Re: How do I make custom tooltip?

Post by zwzsg »

Ok, thanks, I'll manage from there.

The GetTooltip(x,y) and WorldTooltip("unit",unitID) require some IsAbove, and with or without it, I have loads of Stack Overflow and Lua deep errors.

So instead I'll use Argh & Gnome's way of disabling regular tooltip, getting regular tooltip text string with Spring.GetCurrentTooltip(), break it into its component with lots of regexp, complete it with a TraceScreenRay of a GetMouseState (to get the ID of a live unit), then draw it myself with some gl.Text. Not as easy as I'd have wished, but will do all I want including adding new info from live unit and handling gracefully working in cunjunction with other tooltip modifying widgets.
jK wrote:a Spring.Echo can do wonders, if you don't know what the arguments are ...
I use Spring.Echo a lot when debugging, however a Spring.Echo breaks on every nil, and guessing the role of a number from its value is not always easy when you have no idea.
User avatar
lurker
Posts: 3842
Joined: 08 Jan 2007, 06:13

Re: How do I make custom tooltip?

Post by lurker »

Concatenating a nil is what breaks. Do it like this: Spring.Echo("text", param1, "text", param2, param3)
ZellSF
Posts: 1187
Joined: 08 Jul 2006, 19:07

Re: How do I make custom tooltip?

Post by ZellSF »

Argh most likely does it simple, he uses the default tooltip uses the tooltip outlining setting (which makes the background transparent) and the tooltip geometry settings to move it around... All engine stuff so far, no luaui magic.

Then he probably adds a background image under it with luaui. Just my guess anyway. It's a decent idea if you're not looking for anything complicated.
User avatar
Beherith
Posts: 5145
Joined: 26 Oct 2007, 16:21

Re: How do I make custom tooltip?

Post by Beherith »

lurker wrote:Concatenating a nil is what breaks. Do it like this: Spring.Echo("text", param1, "text", param2, param3)
Now that is a useful bit of info for a lua noob like me:)
User avatar
zwzsg
Kernel Panic Co-Developer
Posts: 7052
Joined: 16 Nov 2004, 13:08

Re: How do I make custom tooltip?

Post by zwzsg »

ZellSF wrote:Argh most likely does it simple, he uses the default tooltip uses the tooltip outlining setting (which makes the background transparent) and the tooltip geometry settings to move it around... All engine stuff so far, no luaui magic.

Then he probably adds a background image under it with luaui. Just my guess anyway. It's a decent idea if you're not looking for anything complicated.
No, not really. Opening MERC_SQUAD.sdz then displaying \LuaUI\Widgets\Tooltip_fixes.lua isn't so hard that you have to resort to guessing.
[...] Argh [...]'s way of disabling regular tooltip, getting regular tooltip text string with Spring.GetCurrentTooltip(), break it into its component with lots of regexp, [...], then draw it [...] with some gl.Text. [...]
But as I said, it's ok now, thanks to the hints I was given here I managed:

Image Image Image
ZellSF
Posts: 1187
Joined: 08 Jul 2006, 19:07

Re: How do I make custom tooltip?

Post by ZellSF »

zwzsg wrote:
ZellSF wrote:Argh most likely does it simple, he uses the default tooltip uses the tooltip outlining setting (which makes the background transparent) and the tooltip geometry settings to move it around... All engine stuff so far, no luaui magic.

Then he probably adds a background image under it with luaui. Just my guess anyway. It's a decent idea if you're not looking for anything complicated.
No, not really. Opening MERC_SQUAD.sdz then displaying \LuaUI\Widgets\Tooltip_fixes.lua isn't so hard that you have to resort to guessing.
[...] Argh [...]'s way of disabling regular tooltip, getting regular tooltip text string with Spring.GetCurrentTooltip(), break it into its component with lots of regexp, [...], then draw it [...] with some gl.Text. [...]
But as I said, it's ok now, thanks to the hints I was given here I managed:

Image Image Image
Hey I don't have MERC squad or P.U.R.E installed so I just guessed from an earlier version of P.U.R.E. Besides, that's as simple as a solution you're going to get :p
User avatar
Argh
Posts: 10920
Joined: 21 Feb 2005, 03:38

Re: How do I make custom tooltip?

Post by Argh »

Yeah, that's how I did it in earlier versions, just to get it running. It was pretty crude, but nobody else had done anything with it at all yet, so far as I knew at the time, so it took a while to refine.

In the current build of P.U.R.E., I couple a variant of the code below with some other work that helps set up CommandDescs and other crap at the start of the game, which makes managing all of this very easy.

Oh and... zwzsg doesn't seem to have mentioned that the ToolTip code in Merc Squad is PD... so here it is:

Code: Select all

function widget:GetInfo()
  return {
	name="Tooltip Fixes",
	desc="Fixes the Tooltip display to look more professional and fit Merc Squad",
	author="Argh",
	date="December 19th, 2008",
	license="Public Domain, or the least-restrictive rights in your country of residence",
	layer=0,
	enabled=true
	}
end

local string_format = string.format
local string_find = string.find
local math_floor = math.floor
local math_ceil = math.ceil
local glColor = gl.Color
local glText = gl.Text
local  IsGUIHidden = Spring.IsGUIHidden
local GetGameFrame = Spring.GetGameFrame
local glGetViewSizes = gl.GetViewSizes
local GetUnitDefID = Spring.GetUnitDefID
local GetFeatureDefID = Spring.GetFeatureDefID
local GetUnitHealth = Spring.GetUnitHealth
local GetFeatureHealth = Spring.GetFeatureHealth
local GetUnitResources = Spring.GetUnitResources
local GetFeatureResources = Spring.GetFeatureResources
local GetGroundInfo = Spring.GetGroundInfo

local x,y,textSize,gapSize,xOffset, yOffset = 0,0,0,0,0,0
local myType,myTooltip, line1, line2, line3, ud
local health, maxHealth,metalMake,metalUse,energyMake,energyUse, metalVal = 0,0,0,0,0,0,0
local metalUseFix, metalMakeFix, energyUseFix, energyMakeFix = 0,0,0,0
local RemainingMetal, RemainingEnergy = 0,0
local OtherStuff
local ShutDown = 0
local myLine, xLine = 0,0

function widget:Initialize()
	Spring.SendCommands({"tooltip 0"})
end

function widget:ShutDown()
	ShutDown = 1
	Spring.SendCommands({"tooltip 1"})
	widgetHandler:RemoveWidget()
end

function widget:DrawScreenEffects()
	if GetGameFrame() > 1 and IsGUIHidden() == false and ShutDown == 0 then
		x,y = glGetViewSizes()        
		textSize = 16 * (x / 1600)
		gapSize = 26 * (y / 1200)
		xOffset = 15 *( x  / 1600)
		yOffset = 95 * (y / 1200)
	
		OtherStuff = Spring.GetCurrentTooltip():gsub("Energy cost 0 ",""):gsub("Metal cost","Cost: $"):gsub("├â┬┐├âÔÇ£├âÔÇ║├â┬┐",""):gsub("├â┬┐P├â┬┐P"," "):gsub("├â┬┐├é┬É├é┬É├é┬É/├â┬┐├â┬┐P"," / "):gsub("├âÔé¼├âÔé¼",""):gsub("P0","0"):gsub("Build time 75",""):gsub("Metal","RemoveMe")
		myLine = 0
		xLine = 0		
		for line in string.gmatch(OtherStuff, "([^\n]*)\n?") do
			if myLine == 0 then
				glColor(1,1,1,1)
				glText(line,xOffset, yOffset - xLine,textSize, "n")
			end
			if myLine == 1 then
				glColor(1.0,1.0,0.7,1.0)
				glText(line,xOffset, yOffset - xLine,textSize, "n")
			end
			if myLine == 2 then
				glColor(1.0,0.6,0,1.0)
				glText(line,xOffset, yOffset - xLine,textSize, "n")
			end
			if myLine == 3 then
				if string.find(tostring(line),"RemoveMe") then line = "" end
				glColor(0.0,0.8,1.0,1.0)
				glText(line,xOffset, yOffset - xLine,textSize, "n")
			end
			xLine = xLine + gapSize
			myLine = myLine +1
		end
	end
end
I'm guessing most of you can figure out where to go from there.
Post Reply

Return to “Lua Scripts”